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

How to view Camera's Depth Texture?

Discussion in 'Scripting' started by yty, Aug 1, 2011.

  1. yty

    yty

    Joined:
    Aug 10, 2009
    Posts:
    82
    c#
    Code (csharp):
    1.  
    2. function OnEnable () {
    3.     camera.depthTextureMode = DepthTextureMode.Depth;
    4. }:confused:
    5.  
    shader
    Code (csharp):
    1.  
    2. sampler2D _CameraDepthTexture;
    3. v2f vert( appdata_img v )
    4. {
    5.     v2f o;
    6.     o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
    7.     o.uv =  v.texcoord.xy;
    8.     return o;
    9. }    
    10.  
    11. half4 fragThin (v2f i) : COLOR
    12. {      
    13.     half4 depth= tex2D (_CameraDepthTexture, i.uv);
    14.            
    15.     return depth;
    16. }
    But why the results of a white, can not see anything?
     
  2. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Code (csharp):
    1. sampler2D _CameraDepthTexture;
    2. v2f vert( appdata_img v )
    3. {
    4.     v2f o;
    5.     o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
    6.     o.uv =  v.texcoord.xy;
    7.     return o;
    8. }    
    9.  
    10. half4 fragThin (v2f i) : COLOR
    11. {      
    12.     half4 depth = half4(Linear01Depth(tex2D(_CameraDepthTexture, i.uv).r);
    13.            
    14.     return depth;
    15. }
     
  3. yty

    yty

    Joined:
    Aug 10, 2009
    Posts:
    82
    Thank you very much to reply my post in the forum

    Why only use Linear01Depth, can not LinearEyeDepth?

    Only Linear01Depth in play mode to be effective, LinearEyeDepth nothing in game mode.

    uniform float4 _ZBufferParams;

    // Z buffer to linear 0..1 depth (0 at eye, 1 at far plane)
    inline float Linear01Depth( float z )
    {
    return 1.0 / (_ZBufferParams.x * z + _ZBufferParams.y);
    }
    // Z buffer to linear depth
    inline float LinearEyeDepth( float z )
    {
    return 1.0 / (_ZBufferParams.z * z + _ZBufferParams.w);
    }
     
    Last edited: Aug 2, 2011
  4. yty

    yty

    Joined:
    Aug 10, 2009
    Posts:
    82
    Also, I want to ask can depthTexture to gui draw on it?

    For example rendertexture.
    GUI.DrawTexture (new Rect(0, 0, 128, 128), m_ReflectionTexture, ScaleMode.StretchToFill, false);

    This will not find _CameraDepthTexture
    GUI.DrawTexture (new Rect(0, 0, 128, 128), _CameraDepthTexture, ScaleMode.StretchToFill, false);?
     
  5. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    _CameraDepthTexture is a texture to use in shaders, you can't use it outside no
     
  6. Steven-1

    Steven-1

    Joined:
    Sep 11, 2010
    Posts:
    455
    is the _CameraDepthTexture only usable in vert and frag shaders ? not in surface shaders ?
    Cause I can't get this to work in a surface shader
     
  7. Marrt

    Marrt

    Joined:
    Feb 7, 2012
    Posts:
    613