Write to the depth buffer

Hello there,

I added the post processing stack with a depth of field effect recently and noticed that not all shaders of mine write in the depth buffer. I also noticed, that fallback shaders do handle that problem for me. I do have some shaders, that change the geometry and rotation of the object. So I’m wondering:

How can you write into the depth buffer (In forward rendering mode)? I did find this topic, but sadly this is no clear solution for me. Is there any special tag I need to use, or do I have to use Depth textures? I’m developing for mobile, so I’m looking for a performant solution, which should be out there. The unity shader manage to do it after all as well.

I’m not sure, but if you still interested, I just added this after main pass, and got depth like on builtin shaders, dont know if it’s right way to do it, but worked for me so far. Red things - my own shader with vertex and fragment programs, no special tags except the one below.

Pass {
    Tags { "LightMode" = "ShadowCaster" }
}

Thanks for the answer, that is actually way simpler than in the VertexLit shader from unity.

I’m still stuggeling to use the shadowCaster properly. Why for example is the stencil buffer not applying. Here is a simple example:

Shader "Custom/StencilTest" {
    Properties{
        [HDR] _Color("Main Color", Color) = (1,1,1,1)
    }
    SubShader
    {
        Tags{ "RenderType" = "Opaque" "Queue" = "Geometry+50" }

        Stencil
        {
            Ref 2
            Comp NotEqual
            Pass IncrSat
            Fail Keep
            ZFail Keep
        }

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            fixed4 _Color;

            struct appdata
            {
                float4 vertex : POSITION;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
            };

            v2f vert(appdata i)
            {
                v2f o;
                o.vertex = mul(UNITY_MATRIX_MVP, i.vertex);
                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                return _Color;
            }
            ENDCG
        }

        Pass{
            Tags{ "LightMode" = "ShadowCaster" }
        }
    }
}

The stencil information does apply to the color rendering, but not the shadow. Is there any way around this?