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 _CameraDepthTexture is always white

Discussion in 'Shaders' started by RubberBandGames, Feb 22, 2022.

  1. RubberBandGames

    RubberBandGames

    Joined:
    Jul 20, 2020
    Posts:
    165
    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

    Code (CSharp):
    1.         camera.enabled = false;
    2.         camera.orthographic = true;
    3.         camera.nearClipPlane = 0.01f;
    4.         camera.backgroundColor = Color.black;
    5.         camera.depthTextureMode |= DepthTextureMode.Depth;
    6.         camera.clearFlags = CameraClearFlags.Color;
    7.         camera.depth = 100.0f;
    8.         camera.renderingPath = RenderingPath.Forward;
    Code (CSharp):
    1.         camera.RenderWithShader(replacementShader, "RenderType");
    Shader
    Code (CSharp):
    1. Shader "Water Ambient Render Depth" {
    2.     SubShader{
    3.     Tags { "RenderType" = "Water" }
    4.     Pass {
    5.         CGPROGRAM
    6.  
    7.         #pragma vertex vert
    8.         #pragma fragment frag
    9.         #include "UnityCG.cginc"
    10.  
    11.         sampler2D _CameraDepthTexture;
    12.  
    13.         struct v2f {
    14.             float4 pos : SV_POSITION;
    15.             float2 uv : TEXCOORD0;
    16.         };
    17.  
    18.         v2f vert(appdata_img v) {
    19.             v2f o;
    20.             o.pos = UnityObjectToClipPos(v.vertex);
    21.             o.uv = v.texcoord.xy;
    22.             return o;
    23.         }
    24.  
    25.         half4  frag(v2f i) : COLOR{
    26.             half depth = half(Linear01Depth(tex2D(_CameraDepthTexture, i.uv).r));
    27.  
    28.             return half4(depth,depth,depth,depth);
    29.         }
    30.         ENDCG
    31.     }
    32.     }
    33.     SubShader{
    34.     Tags { "RenderType" = "Opaque" }
    35.     Pass {
    36.         CGPROGRAM
    37.  
    38.         #pragma vertex vert
    39.         #pragma fragment frag
    40.         #include "UnityCG.cginc"
    41.  
    42.         struct v2f {
    43.             float4 pos : SV_POSITION;
    44.         };
    45.         v2f vert(appdata_base v) {
    46.             v2f o;
    47.             o.pos = UnityObjectToClipPos(v.vertex);
    48.             return o;
    49.         }
    50.         half4 frag(v2f i) : SV_Target{
    51.             return half4(0.0f,0.0f,0.0f,1.0f);
    52.         }
    53.         ENDCG
    54.     }
    55.         }
    56.    
    57. }
    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
     
  2. cgDoofus

    cgDoofus

    Joined:
    Jun 15, 2021
    Posts:
    48
    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)
     
  3. RubberBandGames

    RubberBandGames

    Joined:
    Jul 20, 2020
    Posts:
    165
    But I only want the depth not the normals so surely _CameraDepthTexture and DepthTextureMode.Depth would work.. right?
     
  4. cgDoofus

    cgDoofus

    Joined:
    Jun 15, 2021
    Posts:
    48
    Yeah that would work I think
     
  5. RubberBandGames

    RubberBandGames

    Joined:
    Jul 20, 2020
    Posts:
    165
    That's what we already doing, but it's not working
     
  6. somesome2314

    somesome2314

    Joined:
    Feb 23, 2021
    Posts:
    1
    reducing FarClippingPlanes value might solve this issue
     
  7. RubberBandGames

    RubberBandGames

    Joined:
    Jul 20, 2020
    Posts:
    165
    Thanks for the response. I believe we solved this in the end.
     
  8. andrewsirkoch

    andrewsirkoch

    Joined:
    Dec 9, 2019
    Posts:
    2