I’m trying to make a painting system following the base of this video.
And I’m stuck in the part of copying the Command Buffer texture into a texture without clearing it, but I can’t get it to work
I’m trying to get this to work in VR using the OpenXR module.
This is my code
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.UI;
public class Painter : MonoBehaviour
{
[SerializeField] Camera _camera;
[SerializeField] Shader _drawShader;
private RenderTexture _splatMap;
private RenderTexture _temp;
private Material _currentMaterial;
private Material _drawMaterial;
[SerializeField] RawImage _paintMap;
[SerializeField] RawImage _mask;
[SerializeField] Texture2D _originalMask;
RaycastHit hit;
private MeshRenderer _renderer;
[SerializeField] Transform _test;
[SerializeField] float _hardness;
[SerializeField] float _radius;
[SerializeField] float _strength = 1.0f;
// Start is called before the first frame update
void Start()
{
_drawMaterial = new Material(_drawShader);
_currentMaterial = GetComponent<MeshRenderer>().sharedMaterial;
_renderer = GetComponent<MeshRenderer>();
_splatMap = new RenderTexture(1024, 1024,0, RenderTextureFormat.ARGBHalf);
_temp = new RenderTexture(1024, 1024,0, RenderTextureFormat.ARGBHalf);
Graphics.Blit(_currentMaterial.GetTexture("_Mask"), _splatMap);
}
// Update is called once per frame
void Update()
{
//if(Input.GetMouseButton(0))
// {
// if(Physics.Raycast(_camera.ScreenPointToRay(Input.mousePosition), out hit))
//{
if(!_test)
return;
Shader.SetGlobalVector("_Center", (Vector4)_test.position);
Shader.SetGlobalFloat("_Hardness", _hardness);
Shader.SetGlobalFloat("_Radius", _radius);
Shader.SetGlobalFloat("_PainterStrength", _strength);
CommandBuffer command = new CommandBuffer();
command.name = "UV Space Render";
command.SetRenderTarget(_splatMap);
command.DrawRenderer(_renderer, _drawMaterial, 0);
Graphics.ExecuteCommandBuffer(command);
CopyTexture();
command.Release();
//}
//}
}
void CopyTexture()
{
Graphics.Blit(_splatMap, _temp);
_currentMaterial.SetTexture("_Mask", _temp);
_mask.texture = _splatMap;
_paintMap.texture = _temp;
}
}