Search Unity

Resolved Custom Shader not showing on Depth Texture

Discussion in 'Shaders' started by MrVSM, Nov 26, 2022.

  1. MrVSM

    MrVSM

    Joined:
    Mar 24, 2019
    Posts:
    12
    Hi, I wrote a basic shader and added a shadow caster pass, but it is not showing up on the depth texture. Objects in the scene with default URP shaders do properly show depth, so I think my project settings are ok and the issue is with my code. Is there something wrong with my shadow caster pass?

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

    MrVSM

    Joined:
    Mar 24, 2019
    Posts:
    12