Search Unity

Resolved Depth Texture from Custom Shader Trouble

Discussion in 'Universal Render Pipeline' started by MrVSM, Nov 25, 2022.

  1. MrVSM

    MrVSM

    Joined:
    Mar 24, 2019
    Posts:
    12
    Hello, below is the code for a shader that does not show up on the depth texture. Built in URP shaders do properly show up on the depth texture, so I think my project settings are ok and the issue is with the shader I wrote. I did add a shadow caster pass, however, is there something wrong with it that I am not seeing? I also tried adding a Fallback shader to one of the default URP, and that also did not work.

    Code (CSharp):
    1. Shader "Custom/Land"
    2. {
    3.     Properties
    4.     {
    5.         _BaseMap ("Base Texture", 2D) = "white" {}
    6.         _LightDir ("Light Direction", Vector) = (-1, 1 ,-1, 0)
    7.         _AmbientLight ("Ambient Light", Vector) = (0.1, 0.1, 0.1, 0.1)
    8.     }
    9.  
    10.     SubShader
    11.     {
    12.         Tags {
    13.             "RenderPipeline"="UniversalPipeline"
    14.             "Queue"="Geometry"
    15.         }
    16.  
    17.         HLSLINCLUDE
    18.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    19.             CBUFFER_START(UnityPerMaterial)
    20.                 float4 _BaseMap_ST;
    21.                 float4 _LightDir;
    22.                 float4 _AmbientLight;
    23.             CBUFFER_END
    24.         ENDHLSL
    25.  
    26.         Pass
    27.         {
    28.             Name "ForwardPsuedoLit"
    29.             Tags { "LightMode"="UniversalForward" }
    30.  
    31.             HLSLPROGRAM
    32.                 #pragma vertex vert
    33.                 #pragma fragment frag
    34.  
    35.                 TEXTURE2D(_BaseMap);
    36.                 SAMPLER(sampler_BaseMap);
    37.  
    38.                 struct MeshData
    39.                 {
    40.                     float4 positionOS : POSITION;
    41.                     float3 normalOS : NORMAL;
    42.                     float2 uv : TEXCOORD0;
    43.                     float4 color: COLOR;
    44.                 };
    45.  
    46.                 struct v2f
    47.                 {
    48.                     float4 positionCS : SV_POSITION;
    49.                     float3 normalWS : NORMAL;
    50.                     float2 uv : TEXCOORD0;
    51.                     float4 color : COLOR;
    52.                 };
    53.  
    54.                 v2f vert (MeshData i)
    55.                 {
    56.                     v2f o;
    57.                     VertexPositionInputs positionInputs = GetVertexPositionInputs(i.positionOS.xyz);
    58.                     VertexNormalInputs normalInputs = GetVertexNormalInputs(i.normalOS);
    59.                     o.positionCS = positionInputs.positionCS;
    60.                     o.normalWS = normalInputs.normalWS;
    61.                     o.uv = TRANSFORM_TEX(i.uv, _BaseMap);
    62.                     o.color = i.color;
    63.                     return o;
    64.                 }
    65.  
    66.                 float4 frag (v2f i) : SV_Target
    67.                 {
    68.                     float NdotL = dot(_LightDir, i.normalWS);
    69.                     if (NdotL < 0) { NdotL = 0; }
    70.                     return i.color * NdotL + _AmbientLight;
    71.                 }
    72.             ENDHLSL
    73.         }
    74.  
    75.         Pass {
    76.             Name "ShadowCaster"
    77.             Tags { "LightMode"="ShadowCaster" }
    78.             ZWrite On
    79.             ZTest LEqual
    80.             ColorMask 0
    81.  
    82.             HLSLPROGRAM
    83.                 #pragma vertex vert
    84.                 #pragma fragment frag
    85.                 #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
    86.  
    87.                 struct MeshData {
    88.                     float4 positionOS : POSITION;
    89.                 };
    90.  
    91.                 struct v2f {
    92.                     float4 positionCS : SV_POSITION;
    93.                 };
    94.  
    95.                 v2f vert(MeshData i) {
    96.                     v2f o;
    97.                     VertexPositionInputs positionInputs = GetVertexPositionInputs(i.positionOS.xyz);
    98.                     o.positionCS = positionInputs.positionCS;
    99.                     return o;
    100.                 }
    101.  
    102.                 float4 frag(v2f i) : SV_TARGET {
    103.                     return 0;
    104.                 }
    105.             ENDHLSL
    106.         }
    107.     }
    108. }
    109.  
     
  2. MrVSM

    MrVSM

    Joined:
    Mar 24, 2019
    Posts:
    12
  3. wwWwwwW1

    wwWwwwW1

    Joined:
    Oct 31, 2021
    Posts:
    769
    Hi, "ShadowCaster" does not handle depth in URP, what you need is a "DepthOnly" pass. (see URP's Lit shader)

    They are basically the same, so I think you can copy the "ShadowCaster" pass and rename it to "DepthOnly".
     
  4. MrVSM

    MrVSM

    Joined:
    Mar 24, 2019
    Posts:
    12
    Hi wwWwwwW1, Thank you for the response! Thanks to your tip I was able to get it to work! After playing around with it, naming the pass "DepthNormalsOnly" did the trick and has it properly working.

    I'm a bit more confused on how all this works than when I started.

    https://docs.unity.cn/Packages/com....nual/urp-shaders/urp-shaderlab-pass-tags.html

    The documentation suggests DepthNormalsOnly "Use this value in combination with UniversalForwardOnly in the Deferred Rendering Path". I don't have a UniversalForwardOnly pass tag, so I'm not sure why this is the tag to work, but hey, it's working. I tried making a simple shader with only a single DepthOnly Pass to see if it worked then, and no, even if there is only a single pass in the entire shader, still the DepthNormalsOnly is the tag to make it work.

    I tried looking for example documentation or lit shader code on https://github.com/Unity-Technologies/Graphics but could not find it. Apologies if I am just having a dumb moment and can't properly google the source, but do you happen to have a unity technologies official URP shader code documentation? If not, there are a lot of non-unity-official people making information posts and learning tutorials, so I should still study all I can from anywhere I can.
     
  5. wwWwwwW1

    wwWwwwW1

    Joined:
    Oct 31, 2021
    Posts:
    769
    Glad to hear that you finally solved the problem.

    It seems that you need the shader to render to "_CameraNormalsTexture", instead of "_CameraDepthTexture".

    You can force a shader to always render in Forward path by using a tag named "UniversalForwardOnly".

    But effects like SSAO uses GBuffer in Deferred path, so there's a "DepthNormalsOnly" to render to "_CameraNormalsTexture" & GBuffer Normals. (and the depth buffer)

    If you change the tag name to "DepthNormals", this pass will not render to GBuffer Normals anymore. (It'll use GBuffer pass if exists)


    There're no detailed URP shader documentation but it's on the roadmap.

    By now, I suggest checking URP's default shaders & shader includes.
     
    ViolentMonk likes this.
  6. MrVSM

    MrVSM

    Joined:
    Mar 24, 2019
    Posts:
    12
    Thank you for the help and information! I will continue studying and will read and review those sources.
     
    wwWwwwW1 likes this.