Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Graphics.Blit with a material is just giving me a big blue box

Discussion in 'General Graphics' started by BionicWombatGames, Feb 16, 2023.

  1. BionicWombatGames

    BionicWombatGames

    Joined:
    Oct 5, 2020
    Posts:
    33
    I've got an unlit shader that generates an image, and I'm trying to get that shader to generate a simple Texture2D that I can then save to disk. I'm using what seems to be standard code, but texture it is giving me is always just a blue box no matter what I change.

    Code (CSharp):
    1.  
    2.     RenderTexture renderTexture = RenderTexture.GetTemporary(res, res, 0);
    3.     Texture2D texture = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.RGBA32, false);
    4.     Material m = new Material(baseMat);
    5.  
    6.     Graphics.SetRenderTarget(renderTexture);
    7.     GL.Clear(true, true, Color.black);
    8.     Graphics.Blit(texture, renderTexture, m);
    9.     Graphics.SetRenderTarget(null);
    10.  
    11.     RenderTexture.active = renderTexture;
    12.     texture.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
    13.     texture.Apply();
    14.  
    15.     GetComponent<Renderer>().material.mainTexture = texture;
    What am I missing, here? If I try to write the texture to disk using
    tex.EncodeToPNG(); the output it gives me is just a transparent box with nothing in it. My shader is set to generate a simple checkboard pattern, so that's the output I'm expecting.
     
  2. BionicWombatGames

    BionicWombatGames

    Joined:
    Oct 5, 2020
    Posts:
    33
    I've solved the problem, or perhaps more correctly, ChatGPT has solved the problem for me.

    This needs to be added to the tags of your SubShader:
    "RenderTexture"="True"


    Or in context:

    Code (csharp):
    1.  
    2. SubShader {
    3.         Tags {"Queue"="Transparent" "RenderType"="Opaque" "RenderTexture"="True"}
    4.         Pass {
    5.             ...
    Now, there is a problem if your shader is built in ShaderGraph, as ShaderGraph does not support custom tags for some reason. In my case I ended up rewriting my shader in native code because it was simple, but that thread covers a lot of other possible cases.

    My solution here likely also covers previous times when this question has been asked and remained at least partially unresolved:

    https://forum.unity.com/threads/render-to-texture-without-a-camera.235053/
    https://forum.unity.com/threads/save-texture-generated-by-a-shader.326561/
     
  3. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    553
    Hm, that's weird. Is that new in the SRP? Definitely wasn't needed in the BIRP. Also can't find any documentation about it.
     
    wwWwwwW1 likes this.
  4. wwWwwwW1

    wwWwwwW1

    Joined:
    Oct 31, 2021
    Posts:
    631
    I've heard that Graphics.Blit() is not supported by HDRP or URP, so it should be a hidden feature in BiRP.
     
  5. BionicWombatGames

    BionicWombatGames

    Joined:
    Oct 5, 2020
    Posts:
    33
    I'm using URP.
     
  6. wwWwwwW1

    wwWwwwW1

    Joined:
    Oct 31, 2021
    Posts:
    631
    Just found something from this thread.
    So, it's actually possible to use Graphics.Blit() in SRP, but its behavior may not be the same in BiRP.
     
    c0d3_m0nk3y likes this.
  7. wwWwwwW1

    wwWwwwW1

    Joined:
    Oct 31, 2021
    Posts:
    631
    You can create a renderer feature to generate these textures with shader graph in URP. (check this thread)
     
    BionicWombatGames likes this.
  8. gstier

    gstier

    Joined:
    Nov 18, 2013
    Posts:
    5
    Graphics.Blit works with a shader graph shader if you specify pass 0 like so:
    Graphics.Blit(null, blitTarget, blitMaterial, 0); No need for the RenderTexture=True tag.
     
    triduzak likes this.
  9. triduzak

    triduzak

    Joined:
    Jan 24, 2021
    Posts:
    3
    Thank you! You saved me from madness :))