Bake a texture from shader (858776)

I would like to export my grid created in Shader Graph as a texture, but the exported png file doesn’t contain any graphics.

    public int size = 900;
    public Material material;
    public void ExportAsPng()
    {
        // Create a temporary RenderTexture of the same size as the texture
        RenderTexture tmp = RenderTexture.GetTemporary(
                            size,
                            size,
                            0,
                            RenderTextureFormat.Default,
                            RenderTextureReadWrite.Linear);

        // Blit the pixels on texture to the RenderTexture
        Graphics.Blit(null, tmp, material);

        // Backup the currently set RenderTexture
        RenderTexture previous = RenderTexture.active;

        // Set the current RenderTexture to the temporary one we created
        RenderTexture.active = tmp;

        // Create a new readable Texture2D to copy the pixels to it
        Texture2D myTexture2D = new Texture2D(size, size);

        // Copy the pixels from the RenderTexture to the new Texture
        myTexture2D.ReadPixels(new Rect(0, 0, tmp.width, tmp.height), 0, 0);
        myTexture2D.Apply();

        // Reset the active RenderTexture
        RenderTexture.active = previous;

        // Release the temporary RenderTexture
        RenderTexture.ReleaseTemporary(tmp);

#if UNITY_EDITOR
        string folder = $"{Directory.GetParent(Application.dataPath)}/Images";
#else
        string folder = $"{Application.dataPath}/Images";
#endif
        string path = Path.Combine(folder, "Test.png");
        byte[] bytes = myTexture2D.EncodeToPNG();
        File.WriteAllBytes(path, bytes);
    }

Did you ever figure this out?

Hi, nope. If anything changes, I will write here.

Graphics.Blit method won’t work with Shader Graph files.
You can check how Shader Graph uses PreviewManager.cs for rendering preview nodes.

If anyone needs ready to use solution, check Shader Graph Baker tool on the Asset Store:
8600481--1153764--upload_2022-11-20_19-11-34.png