Search Unity

Rendering depth

Discussion in 'Shaders' started by BearishSun, Sep 16, 2010.

  1. BearishSun

    BearishSun

    Joined:
    Dec 1, 2008
    Posts:
    175
    How to render depth in Unity 3? I just want to have the current depth buffer displayed on a plane, like any other texture.

    In Unity 2.6 I used to do this:
    Code (csharp):
    1.  
    2.  
    3. Shader "Custom/Render depth buffer" {
    4.     SubShader {
    5.         Tags { "RenderType"="Opaque" }
    6.         Pass {
    7.             Fog { Mode Off }
    8.  
    9.         CGPROGRAM
    10.         #pragma vertex vert
    11.         #pragma fragment frag
    12.         #include "UnityCG.cginc"
    13.  
    14.         uniform samplerRECT _CameraDepthTexture;
    15.  
    16.         struct v2f {
    17.             float4 vertex : POSITION;
    18.             float2 texcoord : TEXCOORD0;
    19.         };
    20.  
    21.         v2f vert( v2f v ) {
    22.             v2f o;
    23.             o.vertex = mul(glstate.matrix.mvp, v.vertex);
    24.             o.texcoord = v.texcoord;
    25.             return o;
    26.         }
    27.  
    28.         half4 frag(v2f i) : COLOR
    29.         {
    30.             return texRECT(_CameraDepthTexture, i.texcoord);
    31.         }
    32.         ENDCG
    33.  
    34.         }
    35.     }
    36.     FallBack Off
    37. }
    38.  
    And in a script attached to the camera I set " camera.depthTextureMode = DepthTextureMode.Depth;"

    However in Unity 3, this returns a gray texture. The shader definitely works, although it seems there are no proper depth values in _CameraDepthTexture, or I'm interpreting them incorrectly.
     
  2. BearishSun

    BearishSun

    Joined:
    Dec 1, 2008
    Posts:
    175
    If I switch the camera to deferred rendering it renders fine. Switching it to forward rendering nothing seems to get written to the depth texture.

    If I activate deferred rendering, hit Play, hit Stop, switch to forward rendering and hit Play I notice my depth texture still works, but doesn't get updated anymore. If I hit Stop, and Play again now the depth texture seems to be cleared and doesn't update.
     
  3. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    It should work in both forward deferred rendering (and it certainly does in our test suites). More details please how to reproduce the problem (best file a bug with complete project that reproduces the issue).

    When I try your shader set camera's depth texture mode from script, it works for me as well. (Unity 3.0.0f1, Mac OS X 10.6.4, GeForce 8600M).
     
  4. bournifle

    bournifle

    Joined:
    Jan 13, 2010
    Posts:
    31
    Your shader is working fine for me in Unity 3 f1 for MacOSX (forward and deferred rendering), but I had to modify it for Unity 2.6.1 :

    Code (csharp):
    1. Shader "Custom/Render depth buffer" {
    2.    SubShader {
    3.       Tags { "RenderType"="Opaque" }
    4.       Pass {
    5.          Fog { Mode Off }
    6.  
    7.       CGPROGRAM
    8.       #pragma vertex vert
    9.       #pragma fragment frag
    10.       #include "UnityCG.cginc"
    11.      
    12.       uniform float4   _CameraDepthTexture_ST;
    13.       uniform samplerRECT _CameraDepthTexture;
    14.  
    15.       struct v2f {
    16.          float4 vertex : POSITION;
    17.          float2 texcoord : TEXCOORD0;
    18.       };
    19.  
    20.       v2f vert( v2f v ) {
    21.          v2f o;
    22.          o.vertex = mul(glstate.matrix.mvp, v.vertex);
    23.          o.texcoord = TRANSFORM_TEX(v.texcoord, _CameraDepthTexture);
    24.          return o;
    25.       }
    26.  
    27.       half4 frag(v2f i) : COLOR
    28.       {
    29.          return texRECT(_CameraDepthTexture, i.texcoord);
    30.       }
    31.       ENDCG
    32.  
    33.       }
    34.    }
    35.    FallBack Off
    36. }  
    I added the TRANSFORM_TEX macro and a new var _CameraDepthTexture_ST to your shader, because in OpenGL, RECT texture coordinates are in pixels :
    http://unity3d.com/support/documentation/Components/SL-PlatformDifferences.html
     
  5. BearishSun

    BearishSun

    Joined:
    Dec 1, 2008
    Posts:
    175
    Hi, sorry for the slow response. I added an example project and some screenshots to my bug report. It's case #374352.

    Here's deferred when not playing


    Here's deferred when playing (only time when its rendered right, and ONLY in scene, and not game view)


    Here's forward. Either playing or not playing it's the same problem.


    If you have deferred on, and switch to forward rendering and hit play, the depth texture will render, but wont get updated. (I assume it remained from the deferred rendering and wasn't cleared). But if you stop and play once more, now it will not show it anymore until you go back to deferred.

    The exact shader code is (different from my first post):

    Code (csharp):
    1.  
    2.  
    3. Shader "Custom/Render depth buffer" {
    4.  
    5.     SubShader {
    6.  
    7.         Tags { "RenderType"="Opaque" }
    8.  
    9.         Pass {
    10.  
    11.             Fog { Mode Off }
    12.  
    13.  
    14.  
    15.         CGPROGRAM
    16.  
    17.         #pragma vertex vert
    18.  
    19.         #pragma fragment frag
    20.  
    21.         #include "UnityCG.cginc"
    22.  
    23.  
    24.  
    25.         sampler2D _CameraDepthTexture;
    26.  
    27.         float4 _CameraDepthTexture_ST;
    28.  
    29.  
    30.  
    31.         struct v2f {
    32.  
    33.             float4 vertex : POSITION;
    34.  
    35.             float2 texcoord : TEXCOORD0;
    36.  
    37.         };
    38.  
    39.  
    40.  
    41.         v2f vert( v2f v ) {
    42.  
    43.             v2f o;
    44.  
    45.             o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    46.  
    47.             o.texcoord = TRANSFORM_TEX(v.texcoord, _CameraDepthTexture);
    48.  
    49.             return o;
    50.  
    51.         }
    52.  
    53.  
    54.  
    55.         half4 frag(v2f i) : COLOR
    56.  
    57.         {
    58.  
    59.             float depth = tex2D (_CameraDepthTexture, i.texcoord);
    60.  
    61.             depth = LinearEyeDepth (depth);
    62.  
    63.  
    64.  
    65.             return depth;
    66.  
    67.         }
    68.  
    69.         ENDCG
    70.  
    71.  
    72.  
    73.         }
    74.  
    75.     }
    76.  
    77.     FallBack Off
    78.  
    79. }
    80.  
     
  6. spkim921

    spkim921

    Joined:
    Mar 30, 2017
    Posts:
    2
    Where this code should be ??