Search Unity

Render depth distance

Discussion in 'Shaders' started by SunnySunshine, Oct 9, 2014.

  1. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    976
    You can render the depth in unity using the render depth shader as shader replacement for camera:

    Shader "Hidden/Render Depth" {
    SubShader {
    Tags { "RenderType"="Opaque" }
    Pass {
    Fog { Mode Off }

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

    struct v2f {
    float4 pos : POSITION;
    #ifdef UNITY_MIGHT_NOT_HAVE_DEPTH_TEXTURE
    float2 depth : TEXCOORD0;
    #endif
    };

    v2f vert( appdata_base v ) {
    v2f o;
    o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    UNITY_TRANSFER_DEPTH(o.depth);
    return o;
    }

    half4 frag(v2f i) : COLOR {
    UNITY_OUTPUT_DEPTH(i.depth);
    }

    ENDCG

    }
    }

    Fallback Off

    }

    The fading distance is very short though. Is there a way to increase it?

     
  2. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    Change the near and far clipping planes of the camera.
     
  3. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    976
    Only the near clipping plane seems to do any significant change, but I need it to be where it is. Changing the far clipping plane doesn't do anything. The whole scene is basically just white anyway.
     
  4. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Multiply depth.
     
  5. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    976
    If you mean o.depth or i.depth - I already tried that and it had no effect (unless multiplied by 0). Which is to be expected because all those wrappers do is basically setting a variable, and returning a ratio:

    Code (CSharp):
    1.     #define UNITY_TRANSFER_DEPTH(oo) oo = o.pos.zw
    2.     #define UNITY_OUTPUT_DEPTH(i) return i.x/i.y
    Or what do you mean with depth?
     
  6. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    _whateverInputFloat * i.depth

    in the shader you listed. Try it in frag with a constant ie:

    UNITY_OUTPUT_DEPTH(i.depth * 2);
     
  7. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
  8. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    976
    That will not compile. Altering i.depth before usage in UNITY_OUTPUT_DEPTH will not do anything, as it is using a ratio. Multiplying the result of UNITY_OUTPUT_DEPTH will simply make the result darker or lighter without actually affecting the gradient distance.

    Linear would be nice yes. Though I'm not sure about fairly easy - I hardly know any shader code at all. :)
     
  9. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    float vz = mul(UNITY_MATRIX_MV, v.vertex).z;
    float depth = _offset + abs( (1 - clamp(-vz / _farDepth, 0, 2)) * _depthScale);

    needs float _offset, _farDepth, _depthScale

    I use this for my depth of field - it results in a pretty good range and decent amount of control starting at the camera. But you probably can do better with daniel's advice.
     
    SunnySunshine likes this.
  10. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    I am trying to calculate the distance between the camera and a vertex or pixel. Then I could retrieve the depth of the vertex/pixel.
    Here is a good solution : Depth as distance to camera plane in GLSL

    Code (CSharp):
    1. varying float distToCamera;
    2. void main(){
    3. vec4 cs_position = glModelViewMatrix * gl_Vertex;
    4. distToCamera =-cs_position.z;
    5. gl_Position = gl_ProjectionMatrix * cs_position;}
    With this example the depth is dependent to camera distance from the object which is logical and normal.
    But I would like to constrain this value. I would like the same depth and value if I am near from the object or if I am far. That's why I am talking about a "normalized" depth.

    Here is an example of what I am trying to achieve. On the left you can see that the depth is dependant to the camera distance from the object. And on the right even if the camera moves back from the object, the depth remains the same.



    Is it possible ? How ?
     
  11. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    458
    Think of depth like fog, when its far away you can't see anything (black), when its near you, you can sort of see (white). Let me demonstrate
    Depth at normal
    Now when you want to increase the far distance, your increasing the range but your losing some of the detail, edges, or surfaces on the nearest side.
    Depth Increased
    But if you decrease the far distance, your losing the depth far away, but edges, details, and surfaces become more defined.
    Depth Decreased
    So what this guy above my comment wants is basically unachievable with depth, but you can do that if you apply a texture to the object. Im gonna assume your making a sub-surface scattering shader that needs a thickness calculator or texture, you can do that with a texture that represents the thickness of the object in certain areas, but it won't be possible to do it with depth. Because thats like trying to keep springs all the way down the farther you move away from them.
     
  12. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,880
    This doesnt help?

    Code (CSharp):
    1. Shader "Z" {
    2.     Properties {
    3.     }
    4.     SubShader {
    5.         Tags { "RenderType"="Opaque" }
    6.         LOD 200
    7.  
    8.         Pass {
    9.             Lighting Off Fog { Mode Off }
    10.  
    11.             CGPROGRAM
    12.             #pragma vertex vert
    13.             #pragma fragment frag
    14.             #pragma fragmentoption ARB_precision_hint_fastest
    15.  
    16.             struct a2v {
    17.                 float4 vertex : POSITION;
    18.                 fixed4 color : COLOR;
    19.             };
    20.  
    21.             struct v2f {
    22.                 float4 pos : SV_POSITION;
    23.                 half dist : TEXCOORD0;
    24.             };
    25.  
    26.             v2f vert (a2v v) {
    27.                 v2f o;
    28.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    29.                 o.dist = mul(UNITY_MATRIX_IT_MV, v.vertex).z;
    30.                 return o;
    31.             }
    32.  
    33.             fixed4 frag(v2f i) : COLOR {
    34.                 return fixed4(i.dist, i.dist, i.dist, 1);
    35.             }
    36.             ENDCG
    37.         }
    38.     }
    39.     FallBack Off
    40. }
     
  13. EliasHasle

    EliasHasle

    Joined:
    Sep 6, 2017
    Posts:
    1
    Yes, it is possible if you pass uniforms that can be used to calculate the object distance in the same space as the fragment distance then you can subtract the object distance to retrieve the "relative depth". If you need it relative to the nearest point, this can be approximated by subtracting the radius and position of the bounding sphere.