# Render depth distance

**URL:** https://discussions.unity.com/t/render-depth-distance/553038
**Category:** Shaders & Materials
**Tags:** Shaders
**Created:** [October 9, 2014, 8:20am UTC](https://discussions.unity.com/t/render-depth-distance/553038 "2014-10-09T08:20:25Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![SunnySunshine](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/sunnysunshine/32/374723_2.png) [@SunnySunshine](https://discussions.unity.com/u/SunnySunshine)
#### Post date: [October 9, 2014, 8:20am UTC](https://discussions.unity.com/t/render-depth-distance/553038/1 "2014-10-09T08:20:25Z")

</div>

You can render the depth in unity using the render depth shader as shader replacement for camera:

Shader

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?

---

<div class="post-metadata">

### Author: ![Daniel-Brauer](https://avatars.discourse-cdn.com/v4/letter/d/f475e1/32.png) [@Daniel-Brauer](https://discussions.unity.com/u/Daniel-Brauer)
#### Post date: [October 9, 2014, 6:51pm UTC](https://discussions.unity.com/t/render-depth-distance/553038/2 "2014-10-09T18:51:23Z")

</div>

Change the near and far clipping planes of the camera.

---

<div class="post-metadata">

### Author: ![SunnySunshine](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/sunnysunshine/32/374723_2.png) [@SunnySunshine](https://discussions.unity.com/u/SunnySunshine)
#### Post date: [October 9, 2014, 7:56pm UTC](https://discussions.unity.com/t/render-depth-distance/553038/3 "2014-10-09T19:56:20Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![hippocoder](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/hippocoder/32/442865_2.png) [@hippocoder](https://discussions.unity.com/u/hippocoder)
#### Post date: [October 9, 2014, 7:58pm UTC](https://discussions.unity.com/t/render-depth-distance/553038/4 "2014-10-09T19:58:06Z")

</div>

Multiply depth.

---

<div class="post-metadata">

### Author: ![SunnySunshine](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/sunnysunshine/32/374723_2.png) [@SunnySunshine](https://discussions.unity.com/u/SunnySunshine)
#### Post date: [October 9, 2014, 9:32pm UTC](https://discussions.unity.com/t/render-depth-distance/553038/5 "2014-10-09T21:32:47Z")

</div>

> [@hippocoder](#):
>
> Multiply depth.

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:

```csharp
    #define UNITY_TRANSFER_DEPTH(oo) oo = o.pos.zw
    #define UNITY_OUTPUT_DEPTH(i) return i.x/i.y

```

Or what do you mean with depth?

---

<div class="post-metadata">

### Author: ![hippocoder](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/hippocoder/32/442865_2.png) [@hippocoder](https://discussions.unity.com/u/hippocoder)
#### Post date: [October 9, 2014, 10:59pm UTC](https://discussions.unity.com/t/render-depth-distance/553038/6 "2014-10-09T22:59:04Z")

</div>

\_whateverInputFloat \* i.depth

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

UNITY\_OUTPUT\_DEPTH(i.depth \* 2);

---

<div class="post-metadata">

### Author: ![Daniel-Brauer](https://avatars.discourse-cdn.com/v4/letter/d/f475e1/32.png) [@Daniel-Brauer](https://discussions.unity.com/u/Daniel-Brauer)
#### Post date: [October 9, 2014, 11:24pm UTC](https://discussions.unity.com/t/render-depth-distance/553038/7 "2014-10-09T23:24:43Z")

</div>

My guess is you want linear depth values instead of the ones used for the depth buffer. You could probably fairly easily modify this shader to do it:

[http://wiki.unity3d.com/index.php?title=AlphaClipsafe](http://wiki.unity3d.com/index.php?title=AlphaClipsafe)

---

<div class="post-metadata">

### Author: ![SunnySunshine](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/sunnysunshine/32/374723_2.png) [@SunnySunshine](https://discussions.unity.com/u/SunnySunshine)
#### Post date: [October 9, 2014, 11:56pm UTC](https://discussions.unity.com/t/render-depth-distance/553038/8 "2014-10-09T23:56:30Z")

</div>

> [@hippocoder](#):
>
> \_whateverInputFloat \* i.depth
> 
> in the shader you listed. Try it in frag with a constant ie:
> 
> UNITY\_OUTPUT\_DEPTH(i.depth \* 2);

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.

> [@Daniel Brauer](#):
>
> My guess is you want linear depth values instead of the ones used for the depth buffer. You could probably fairly easily modify this shader to do it:
> 
> [http://wiki.unity3d.com/index.php?title=AlphaClipsafe](http://wiki.unity3d.com/index.php?title=AlphaClipsafe)

Linear would be nice yes. Though I’m not sure about fairly easy - I hardly know any shader code at all. 🙂

---

<div class="post-metadata">

### Author: ![hippocoder](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/hippocoder/32/442865_2.png) [@hippocoder](https://discussions.unity.com/u/hippocoder)
#### Post date: [October 10, 2014, 2:00am UTC](https://discussions.unity.com/t/render-depth-distance/553038/9 "2014-10-10T02:00:23Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![MaT227](https://avatars.discourse-cdn.com/v4/letter/m/f07891/32.png) [@MaT227](https://discussions.unity.com/u/MaT227)
#### Post date: [October 13, 2014, 7:04am UTC](https://discussions.unity.com/t/render-depth-distance/553038/10 "2014-10-13T07:04:21Z")

</div>

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](http://stackoverflow.com/questions/16131963/depth-as-distance-to-camera-plane-in-glsl)

```csharp
varying float distToCamera;
void main(){
vec4 cs_position = glModelViewMatrix * gl_Vertex;
distToCamera =-cs_position.z;
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.

![](https://europe1.discourse-cdn.com/unity/original/4X/9/e/0/9e0c490410f2732fee01c068aa07be6eed6ab2be.gif)

Is it possible ? How ?

---

<div class="post-metadata">

### Author: ![Frostbite23](https://avatars.discourse-cdn.com/v4/letter/f/ccd318/32.png) [@Frostbite23](https://discussions.unity.com/u/Frostbite23)
#### Post date: [October 18, 2014, 1:30am UTC](https://discussions.unity.com/t/render-depth-distance/553038/11 "2014-10-18T01:30:08Z")

</div>

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](http://puu.sh/cgwuI/e3ad2bb13d.png)  
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](http://puu.sh/cgwAZ/2b072f5f1a.png)  
But if you decrease the far distance, your losing the depth far away, but edges, details, and surfaces become more defined.  
[Depth Decreased](http://puu.sh/cgwFH/434eb9ed7e.png)  
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.

---

<div class="post-metadata">

### Author: ![aubergine](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/aubergine/32/890563_2.png) [@aubergine](https://discussions.unity.com/u/aubergine)
#### Post date: [October 18, 2014, 8:18am UTC](https://discussions.unity.com/t/render-depth-distance/553038/12 "2014-10-18T08:18:29Z")

</div>

> [@MaT.](#):
>
> 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](http://stackoverflow.com/questions/16131963/depth-as-distance-to-camera-plane-in-glsl)
> 
> ```csharp
> varying float distToCamera;
> void main(){
> vec4 cs_position = glModelViewMatrix * gl_Vertex;
> distToCamera =-cs_position.z;
> 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.
> 
> ![](https://europe1.discourse-cdn.com/unity/original/4X/9/e/0/9e0c490410f2732fee01c068aa07be6eed6ab2be.gif)
> 
> Is it possible ? How ?

This doesnt help?

```csharp
Shader "Z" {
    Properties {
    }
    SubShader {
        Tags { "RenderType"="Opaque" }
        LOD 200

        Pass {
            Lighting Off Fog { Mode Off }

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma fragmentoption ARB_precision_hint_fastest

            struct a2v {
                float4 vertex : POSITION;
                fixed4 color : COLOR;
            };

            struct v2f {
                float4 pos : SV_POSITION;
                half dist : TEXCOORD0;
            };

            v2f vert (a2v v) {
                v2f o;
                o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                o.dist = mul(UNITY_MATRIX_IT_MV, v.vertex).z;
                return o;
            }

            fixed4 frag(v2f i) : COLOR {
                return fixed4(i.dist, i.dist, i.dist, 1);
            }
            ENDCG
        }
    }
    FallBack Off
}

```

---

<div class="post-metadata">

### Author: ![EliasHasle](https://avatars.discourse-cdn.com/v4/letter/e/71c47a/32.png) [@EliasHasle](https://discussions.unity.com/u/EliasHasle)
#### Post date: [February 4, 2021, 1:22pm UTC](https://discussions.unity.com/t/render-depth-distance/553038/13 "2021-02-04T13:22:02Z")

</div>

> [@MaT227](#):
>
> 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.  
> …  
> Is it possible ? How ?

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.
