Surf Depth Shader

I’m using frag shader for show depth texture and it works. When I,m trying do the same in the surface shader I’m seeing black screen. Why?))

    void OnRenderImage(RenderTexture src, RenderTexture dest)
    {
        Material mat = Resources.Load("materials/TestBuffer", typeof(Material)) as Material;
        Graphics.Blit(src, dest, mat);
    }

working shader

Shader "Custom/ZBufTest" {
    SubShader{
        Tags{ "RenderType" = "Opaque" }

        Pass{
        CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            sampler2D _CameraDepthTexture;

            struct v2f {
                float4 pos : SV_POSITION;
                float4 scrPos:TEXCOORD1;
            };

            v2f vert(appdata_base v) {
                v2f o;
                o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                o.scrPos = ComputeScreenPos(o.pos);

                return o;
            }

            half4 frag(v2f i) : COLOR
            {
                float depthValue = Linear01Depth(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.scrPos)).r);
             
                depth.r = depthValue;
                depth.g = depthValue;
                depth.b = depthValue;

                depth.a = 1;
                return depth;
            }
        ENDCG
        }
    }
    FallBack "Diffuse"
}

notworking shader

Shader "Custom/ZBufTest"
{
    Properties
    {

    }
    SubShader
    {
        CGPROGRAM

        #include "UnityCG.cginc"
        #pragma surface surf Lambert vertex:vert alpha addshadow

        #pragma target 3.0

        sampler2D _CameraDepthTexture;

        struct Input
        {
            float2 uv_MainTex;
            float4 screenPos;

            float eyeDepth;
        };


        void vert(inout appdata_full v, out Input o)
        {
            UNITY_INITIALIZE_OUTPUT(Input, o);

            float4 vpos = mul(UNITY_MATRIX_MVP, v.vertex);
            o.screenPos = ComputeScreenPos(vpos);
        }

        void surf (Input IN, inout SurfaceOutput o)
        {
    
            float depthValue = Linear01Depth(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(IN.screenPos)).r);

            half4 depth;
          
            depth.r = depthValue;
            depth.g = depthValue;
            depth.b = depthValue;
          
            depth.a = 1;
          

            o.Albedo = depth;

        }
        ENDCG
    }
    FallBack "Diffuse"
}

Surface shaders don’t work properly with blit, and they shouldn’t be expected to. You’re setting the Albedo (the surface color) to the depth, but a material rendered using blit will receive no lighting information and always render as black (albedo * no lighting). You can try doing o.Emission = depth instead, but the shader is still doing all of the work to calculate lighting it doesn’t have!

I’ve tried to use even o.Albedo = float3(1, 1, 1) and o.Emission = float3(1, 1, 1), but screen is still black))
Actually I need to use depth buffer for adjusting water opacity near coastline. Maybe instead blit I need to use other approach?

Ok. I transfer this surf shader to water material, so it works and I’m getting landscape mesh depth from buffer. If I need to clip water pixels along coastline, how i can compare it with water z value (water use ZWrite off)? Also, how can I add some random for clipping