Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Can't access _LastCameraDepthTexture in image effect shader

Discussion in 'Shaders' started by Deleted User, May 7, 2017.

  1. Deleted User

    Deleted User

    Guest

    I am trying to sample _LastCameraDepthTexture in an image effect shader, but the sample appears to be zero all the time. Anyone knows anything about this builtin? (_CameraDepthTexture behaves the same)

    Code (csharp):
    1.  
    2. Shader "Hidden/SalzFog"
    3. {
    4.     Properties
    5.     {
    6.         _MainTex ("Texture", 2D) = "white" {}
    7.     }
    8.     SubShader
    9.     {
    10.         // No culling or depth
    11.         Cull Off ZWrite Off ZTest Always
    12.  
    13.         Pass
    14.         {
    15.             CGPROGRAM
    16.             #pragma vertex vert_img
    17.             #pragma fragment frag
    18.          
    19.             #include "UnityCG.cginc"
    20.          
    21.             sampler2D _MainTex;
    22.             sampler2D_float _LastCameraDepthTexture;
    23.  
    24.             fixed4 frag (v2f_img i) : SV_Target
    25.             {
    26.                 fixed4 col = tex2D(_MainTex, i.uv);
    27.                 float depth = SAMPLE_DEPTH_TEXTURE(_LastCameraDepthTexture, i.uv);
    28.                 return col * depth; // it's just black
    29.             }
    30.             ENDCG
    31.         }
    32.      
    33.     }
    34. //    Fallback "Diffuse"
    35. }
    36.  
     
  2. Deleted User

    Deleted User

    Guest

    It might be worth adding that I do indeed set the camera to generate a depth texture.

    Code (csharp):
    1.     void Start() {
    2.         Camera c = GetComponent<Camera> ();
    3.         c.depthTextureMode = DepthTextureMode.Depth;
    4.     }
     
  3. Deleted User

    Deleted User

    Guest

    Well, I'm silly. It did work, but near plane distance is white (1.0) and far plane distance is black (0.0). Because of the non-linearity of the depth value only being very close to an object would yield a non-black pixel.

    Now I want to convert this into a distance value. If the value was linear, the distance would be near + (1 - depth) * (far - near), but it is not linear. Is there documentation on how exactly the depth value is encoded?

    Code (csharp):
    1.             fixed4 frag (v2f_img i) : SV_Target
    2.             {
    3.                 fixed4 col = tex2D(_MainTex, i.uv);
    4.                 float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv);
    5.                 float near = 0.1;
    6.                 float far = 100;
    7.                 float dist = near + (1 - depth) * (far - near);
    8.                 return col * (far - dist) / far; // already pitch-black after a distance of 1 unit
    9.             }
     
    Last edited by a moderator: May 10, 2017
  4. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
  5. Deleted User

    Deleted User

    Guest

    Thanks for the reply! I found Linear01Depth() and LinearEyeDepth() from UnityCG.cginc to work perfectly. Easy basic fog post process!

    Code (csharp):
    1.             fixed4 frag (v2f_img i) : SV_Target
    2.             {
    3.                 fixed4 col = tex2D(_MainTex, i.uv);
    4.                 float z = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv);
    5.                 float dist = LinearEyeDepth(z);
    6.                 float fact = exp(-0.1 * dist);
    7.                 return col * fact + fixed4(0.15, 0.12, 0.08, 0) * (1 - fact);
    8.             }
    9.  
     
  6. shibi2017

    shibi2017

    Joined:
    Jan 18, 2018
    Posts:
    153
    Hi Im using 2 cam in scene and the _LastCameraDepthTexture turn out to be black, but the _CameraDepthTexture woeks, can you share you settings or do you know why?

    using _LastCameraDepthTexture and _CameraDepthTexture:
    upload_2022-4-22_13-35-29.png upload_2022-4-22_13-35-38.png
     

    Attached Files: