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 How do I use an unlit ShaderGraph shader in a custom renderer?

Discussion in 'Shader Graph' started by btristan, Jul 6, 2020.

  1. btristan

    btristan

    Joined:
    Oct 15, 2018
    Posts:
    95
    I am trying to render a full-screen quad with some material to a RenderTexture.

    If I use the Unlit/Color shader it renders fine. (see the green squares in the screenshots below)

    If I use a simple shader graph it renders fine in the scene view and only a pixel-wide stripe on the top and left in the game view. (thee the blue squares in the screenshots below)

    Screenshot from 2020-07-06 11-14-08.png

    If I use the Unlit URP shader then nothing renders at all. (which is fine)

    Game view:
    Screenshot from 2020-07-06 11-10-53.png

    Scene view:
    Screenshot from 2020-07-06 11-18-35.png

    What parts of the forward renderer do I need to replicate in order to have the shader graph shader render correctly?
     
  2. btristan

    btristan

    Joined:
    Oct 15, 2018
    Posts:
    95
    I even tried doing this as a renderer feature with and got the same results. I have a feeling that Unity is expecting me to do something in my render pass but IDK what.

    In case it helps, here is my full screen quad pass code:

    Code (CSharp):
    1.     public class FullScreenQuadPass : ScriptableRenderPass
    2.     {
    3.         private const string ProfilerTag = nameof(FullScreenQuadPass);
    4.  
    5.         [NotNull]
    6.         private readonly FullScreenQuadRenderer.SettingsContainer _settings;
    7.  
    8.         private readonly ProfilingSampler _profilingSampler;
    9.  
    10.         public FullScreenQuadPass([NotNull] FullScreenQuadRenderer.SettingsContainer settings)
    11.         {
    12.             _profilingSampler = new ProfilingSampler(ProfilerTag);
    13.             _settings = settings;
    14.  
    15.             renderPassEvent = RenderPassEvent.AfterRenderingPostProcessing;
    16.         }
    17.  
    18.         public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
    19.         {
    20.             if (!_settings.Material)
    21.             {
    22.                 Debug.LogWarning("Null material used in full screen quad pass, skipping");
    23.                 return;
    24.             }
    25.  
    26.             CommandBuffer cmd = CommandBufferPool.Get(ProfilerTag);
    27.  
    28.             Camera camera = renderingData.cameraData.camera;
    29.  
    30.             using (new ProfilingScope(cmd, _profilingSampler))
    31.             {
    32.                 cmd.BeginSample("Clear Target");
    33.                 cmd.ClearRenderTarget(true, true, camera.backgroundColor);
    34.                 cmd.EndSample("Clear Target");
    35.  
    36.                 cmd.BeginSample("Render");
    37.                 cmd.SetViewProjectionMatrices(Matrix4x4.identity, Matrix4x4.identity);
    38.                 cmd.DrawMesh(RenderingUtils.fullscreenMesh, Matrix4x4.identity, _settings.Material);
    39.                 cmd.EndSample("Render");
    40.             }
    41.  
    42.             context.ExecuteCommandBuffer(cmd);
    43.             CommandBufferPool.Release(cmd);
    44.         }
    45.     }
     
  3. btristan

    btristan

    Joined:
    Oct 15, 2018
    Posts:
    95
    Fixed it!

    Turns out you just need to specify the shader pass. The default of -1 does not play well with URP shaders:

    Code (CSharp):
    1. // drawing shader pass 0 is critical here: the default is -1 which does not work with URP shaders
    2. cmd.DrawMesh(RenderingUtils.fullscreenMesh, Matrix4x4.identity, material, 0, 0);
    Note that I have only tested this with Unity 2010.1.0b14 and URP 8.1.0.
     
    Lahcene and Jonathan_L like this.