#Can't blit material into Render Texture

1 messages · Page 1 of 1 (latest)

misty hatch
#

I am trying to blit material made in ShaderGraph onto RenderTexture. And it doesn't work
The code of my script:

using UnityEngine.UI;

#if UNITY_EDITOR
using UnityEditor;
#endif

[ExecuteInEditMode]
public class TerrainGenerator : MonoBehaviour
{
    [SerializeField] private Terrain terrain;
    [SerializeField] private Material material;
    
    public RenderTexture renderTexture;
    
    [ContextMenu("Generate Terrain texture")]
    public void GenerateTerrainTexture()
    {
        if (terrain == null || material == null || renderTexture == null)
        {
            Debug.Log("Terrain generation failed");
            return;
        }

        Graphics.Blit(Texture2D.whiteTexture, renderTexture, material);
    }

    private void OnValidate()
    {
        if(renderTexture == null)
        {
            renderTexture = RenderTexture.GetTemporary(1024, 1024, 0, RenderTextureFormat.RFloat);
        }
    }
}

[CustomEditor(typeof(TerrainGenerator))]
public class TerrainGeneratorEditor : Editor
{
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        TerrainGenerator terrainGenerator = (TerrainGenerator)target;
        
        if (GUILayout.Button("Generate Terrain texture"))
        {
            terrainGenerator.GenerateTerrainTexture();
        }
        
        RenderTexture texture = terrainGenerator.renderTexture;
        
        if(texture){
            GUILayout.Label("Preview", EditorStyles.boldLabel);
                
            Rect rect = GUILayoutUtility.GetAspectRect(texture.width / (float)texture.height);
            EditorGUI.DrawPreviewTexture(rect, texture);
        }
    }
}```
My shader (screenshot)

Please help me fix this
upbeat flame
#

Don't know if this is the only issue, but as graphs generate multiple passes (i.e. for shadow casting, depth/normal texture generation, etc) you should use a blit overload that specifies the pass index.
Likely pass 0, which is the main forward or unlit pass.

Graphics.Blit(Texture2D.whiteTexture, renderTexture, material, 0);