_CameraDepthTexture is always white

Hello,

I’m trying to make a shader which basically displays Black if the material is Opqaue and the Depth if the material is of rendertype Water

I’m trying to do this in the editor so I bake this texture which saves in the scene.
Currently doing this all from inside OnInspectorGUI from Editor

        camera.enabled = false;
        camera.orthographic = true;
        camera.nearClipPlane = 0.01f;
        camera.backgroundColor = Color.black;
        camera.depthTextureMode |= DepthTextureMode.Depth;
        camera.clearFlags = CameraClearFlags.Color;
        camera.depth = 100.0f;
        camera.renderingPath = RenderingPath.Forward;
        camera.RenderWithShader(replacementShader, "RenderType");

Shader

Shader "Water Ambient Render Depth" {
    SubShader{
    Tags { "RenderType" = "Water" }
    Pass {
        CGPROGRAM

        #pragma vertex vert
        #pragma fragment frag
        #include "UnityCG.cginc"

        sampler2D _CameraDepthTexture;

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

        v2f vert(appdata_img v) {
            v2f o;
            o.pos = UnityObjectToClipPos(v.vertex);
            o.uv = v.texcoord.xy;
            return o;
        }

        half4  frag(v2f i) : COLOR{
            half depth = half(Linear01Depth(tex2D(_CameraDepthTexture, i.uv).r));

            return half4(depth,depth,depth,depth);
        }
        ENDCG
    }
    }
    SubShader{
    Tags { "RenderType" = "Opaque" }
    Pass {
        CGPROGRAM

        #pragma vertex vert
        #pragma fragment frag
        #include "UnityCG.cginc"

        struct v2f {
            float4 pos : SV_POSITION;
        };
        v2f vert(appdata_base v) {
            v2f o;
            o.pos = UnityObjectToClipPos(v.vertex);
            return o;
        }
        half4 frag(v2f i) : SV_Target{
            return half4(0.0f,0.0f,0.0f,1.0f);
        }
        ENDCG
    }
        }
   
}

But all i get is this

The white squares are water both at different heights. But as you can see they are a solid white.

Any advice for be great!

Best,
Tom

As far as I know if you’re using DepthTextureMode.DepthNormals then the depth and normals get written to _CameraDepthNormals and not _CameraDepthTexture, then to extract depth you use DecodeDepthNormal( tex2D(_CameraDepthNormals, uv), depth, normal)

But I only want the depth not the normals so surely _CameraDepthTexture and DepthTextureMode.Depth would work… right?

Yeah that would work I think

That’s what we already doing, but it’s not working

reducing FarClippingPlanes value might solve this issue

Thanks for the response. I believe we solved this in the end.

What was the solution @RubberBandGames ?

Changing the clipping planes will allow you to visualize the depth easier - with large far clipping planes depth values are all very small