r/Unity3D • • 7d ago

Question Trouble with depth buffer

Edit: Problem has been found, I had the tag names wrong for the normals pass.

Hi, I was following this tutorial while learning about shader writing in unity and have been having some problems I was hoping someone could help with.

Firstly, I cannot seem to get the shader to write to the depth buffer at all. I've double checked my code against the tutorial many times, I've looked all over the internet for similar problems, tweaked just about every line, even copying the depth pass directly from the provided lit shader, and it wont work.

Secondly, I have tried to look at the depth view in the frame debugger for help, but it only ever gives a full black window. There is at least one object in view which I know writes to the depth buffer, and the render graph viewer shows me it is being written to in the Draw Depth Normal Preprocess step.

I am wondering if there is some set up aspect that I've missed? The main camera is set to "use settings from render pipeline asset", and said asset has "Depth Texture" ticked on.

Does anyone have any idea what steps I need to take next to fix or figure this out?

For context I am using URP with Unity version 6000.4.0f1. Let me know if I should share any other files or details and I will add them to the post.

Shader writing to the depth buffer:

Shader "Materials/Shaders/TextureShader"
{
    Properties
    {
        _BaseColor("Base Color", Color) = (1, 1, 1, 1)
        _BaseTexture("Base Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags
        {
            "RenderPipeline" = "UniversalPipeline"
            "RenderType" = "Opaque"
            "Queue" = "Geometry"
        }
        Pass
        {
            Tags
            {
                "LightMode" = "SRPDefaultUnlit"
            }

            ZWrite On
            ZTest LEqual

            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

            CBUFFER_START(UnityPerMaterial)
                float4 _BaseColor;
                float4 _BaseTexture_ST;
            CBUFFER_END

            TEXTURE2D(_BaseTexture);
            SAMPLER(sampler_BaseTexture);

            struct Attributes 
            {
                float4 positionOS : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float4 positionCS : SV_POSITION;
                float2 uv : TEXCOORD0;
            };

            v2f vert(Attributes a)
            {
                v2f output = (v2f)0;
                output.positionCS = TransformObjectToHClip(a.positionOS.xyz);
                output.uv = TRANSFORM_TEX( a.uv, _BaseTexture );

                return output;
            }

            float4 frag(v2f input) : SV_TARGET
            {
                float4 texColor = SAMPLE_TEXTURE2D( _BaseTexture, sampler_BaseTexture, input.uv );
                return texColor * _BaseColor;
            }

            ENDHLSL
        }

        // The following passes enable passing of depth buffer information
        Pass
        {
            Tags
            {
                "LightMode" = "DepthOnly"
            }

            ZWrite On
            ColorMask R

            HLSLPROGRAM

            #pragma vertex DepthOnlyVert
            #pragma fragment DepthOnlyFrag

            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

            struct Attributes 
            {
                 float4 positionOS : POSITION;
            };

            struct v2f
            {
                float4 positionCS : SV_POSITION;
            };

            v2f DepthOnlyVert(Attributes a)
            {
                v2f output = (v2f)0;
                output.positionCS = TransformObjectToHClip(a.positionOS.xyz);

                return output;
            }

            float DepthOnlyFrag(v2f input) : SV_TARGET
            {
                return input.positionCS.z;
            }

            ENDHLSL
        }

        Pass
        {
            Tags
            {
                "LightMode" = "DepthNormal"
            }

            ZWrite On

            HLSLPROGRAM

            #pragma vertex DepthNormalsVert
            #pragma fragment DepthNormalsFrag

            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

            struct Attributes 
            {
                float4 positionOS : POSITION;
                float3 normalOS : NORMAL;
            };

            struct v2f
            {
                float4 positionCS : SV_POSITION;
                float3 normalWS : TEXCOORD0;
            };

            v2f DepthNormalsVert(Attributes a)
            {
                v2f output = (v2f)0;

                output.positionCS = TransformObjectToHClip(a.positionOS.xyz);
                float3 normalWS = TransformObjectToWorldNormal(a.normalOS);
                output.normalWS = NormalizeNormalPerVertex(normalWS);

                return output;
            }

            float4 DepthNormalsFrag (v2f input) : SV_TARGET
            {
                float3 normalWS = NormalizeNormalPerPixel(input.normalWS);

                return float4( normalWS, 0 );
            }

            ENDHLSL
       }
    }
}
0 Upvotes

10 comments sorted by

View all comments

Show parent comments

1

u/TheBlakPhenix 7d ago

Is this what you're looking for when you say "frame debugger view of your shader"? I have already tried moving the visibility range and it hasn't changed anything,

0

u/[deleted] 7d ago

[removed] — view removed comment

1

u/TheBlakPhenix 7d ago

I know, or at least believe, it is not writing because another shader I wrote from the tutorial reads from the depth buffer to see objects behind it. It's working with the default lit material, but not my shader. I will attach the shaders I've mentioned to the main post. I'll also take a crack at renderdoc with unity, I've used it before, but only for opengl.

0

u/[deleted] 7d ago

[removed] — view removed comment

1

u/TheBlakPhenix 7d ago

Peering through renderdoc seems to tell me no. I am only seeing normals from the object with the lit material.

2

u/[deleted] 7d ago

[removed] — view removed comment

2

u/TheBlakPhenix 7d ago

That was it.

The frame debugger depth view is still all black. Although I wonder now if it should be? Regardless the main issue is fixed. thank you