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 Depth texture visualize shader

Discussion in 'Universal Render Pipeline' started by GiftedMamba, Apr 1, 2023.

  1. GiftedMamba

    GiftedMamba

    Joined:
    Feb 25, 2017
    Posts:
    46
    Hi there!

    I'm currently learning how to write shaders in Unity. I've been following some tutorials on how to write a shader that utilizes depth texture in URP, but unfortunately, I'm unable to achieve the same result as in the tutorial for some unknown reasons. It seems that SampleSceneDepth always returns 0, regardless of the position of my test object.

    To check this idea, I decided to write a simple shader that would visualize the depth texture. However, for some reason, the color returned by the shader is always black. I have already enabled the DepthTexture checkbox on the URP asset. Here's the code for my shader. Can anyone please tell me what I did wrong?
    Code (CSharp):
    1. Shader "Custom/DepthTextureVisualize"
    2. {
    3.     Properties {}
    4.     SubShader
    5.     {
    6.         Tags
    7.         {
    8.             "RenderType" = "Opaque"
    9.             "RenderPipeline" = "UniversalPipeline"
    10.         }
    11.  
    12.         Pass
    13.         {
    14.             HLSLPROGRAM
    15.             #pragma vertex vert
    16.             #pragma fragment frag
    17.            
    18.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    19.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"
    20.            
    21.             struct appdata
    22.             {
    23.                 float4 positionOS : POSITION;
    24.             };
    25.  
    26.             struct v2f
    27.             {
    28.                 float4 positionHCS : SV_POSITION;
    29.             };
    30.            
    31.             v2f vert(appdata IN)
    32.             {
    33.                 v2f OUT;
    34.                 OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
    35.                 return OUT;
    36.             }
    37.            
    38.             float4 frag(v2f IN) : SV_Target
    39.             {
    40.                 float2 UV = IN.positionHCS.xy / _ScaledScreenParams.xy;
    41.                 float depth = SampleSceneDepth(UV);
    42.  
    43.                 return float4(depth, depth, depth, 1.0);
    44.             }
    45.             ENDHLSL
    46.         }
    47.     }
    48. }
     
  2. GiftedMamba

    GiftedMamba

    Joined:
    Feb 25, 2017
    Posts:
    46
    In case if someone has the same situation in future. I found solution - it appeared that in my scene existed only objects with my shaders, which do not write to ZBuffer and as far as I understood that was the reason, why depth texture was black. After adding objects with Lit URP shaders everything started to work.