0
votes

I'm trying to draw to a texture with the mouse. Mouse coords are given to a shader which outputs to a render texture (I've tried both regular RenterTexture and CustomRenderTexture which is affected directly by a material) and it doesn't seem to work. I can tell from the material that the mouse's input is obtained, but nothing is visible on the rendertexture.

I'm starting to suspect that rendertextures aren't fully working in HDRP? Hoping someone can point in the direction of what could be the real issue.

I'm on Unity 2019.3.0f3, shaders is HDRP Unlit Graph

This is my script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DrawWithMouse : MonoBehaviour
{
    public Camera _camera;
    public CustomRenderTexture _splatmap;

    public Material _drawMaterial;

    private RaycastHit _hit;

    [Range(1, 100)]
    public float _brushSize = 1f;
    [Range(0, 10)]
    public float _brushStrength = 1f;

    private readonly float m_GUIsize = 256;
    private readonly int m_RenderTexSize = 1024;

    void Start()
    {
        _splatmap = new CustomRenderTexture(m_RenderTexSize, m_RenderTexSize, RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.Linear)
        {
            name = "splatmap_CRT_generated",
            initializationColor = Color.black,
            initializationSource = CustomRenderTextureInitializationSource.Material,
            initializationMaterial = _drawMaterial,
            material = _drawMaterial,
            doubleBuffered = true,
            updateMode = CustomRenderTextureUpdateMode.OnDemand
        };

        _drawMaterial.SetVector("_DrawColor", Color.red);
        _drawMaterial.SetTexture("_SplatMap", _splatmap);
    }

    void Update()
    {
        if (Input.GetKey(KeyCode.Mouse0))
        {
            if (Physics.Raycast(_camera.ScreenPointToRay(Input.mousePosition), out _hit, 100f))
            {
                _drawMaterial.SetVector("_InputPoint", new Vector4(_hit.textureCoord.x, _hit.textureCoord.y, 0, 0));
                _drawMaterial.SetFloat("_BrushStrength", _brushStrength);
                _drawMaterial.SetFloat("_BrushSize", _brushSize);
            }
        }
    }


    private void OnGUI()
    {
        GUI.DrawTexture(new Rect(0, 0, m_GUIsize, m_GUIsize), _splatmap, ScaleMode.StretchToFill, false, 1);
    }

}
1
OK, so 2019.3 is a beta.. have you considered reporting it as an issue to unity? ans HRDP in 2019.2 is still preview - BugFinder
@BugFinder I was searching for others in the same situation, hoping there is something wrong with my approach, but it might very well be a bug in the client. - Mads M

1 Answers

0
votes

CustomRenderTextures and URP do not seem to play nicely together either, at least not in builds, as of 2020.1.2f1. I eventually had to give up and hack a solution together with a dedicated camera, layer and quad, because nothing I tried (and I tried a lot) would persuade the CustomRenderTexture to even initialise in a PC build, despite working perfectly in the editor.