Rendering camera scene depth to rendertexture

How would i render the camera scene depth to a render texture?

I use the following code, but it doesnt work. The target render texture is just green.
EDIT: I have an ortographic camera by the way.

using UnityEngine;
using System.Collections;

public class DepthTexture : MonoBehaviour {
	public void Awake() {
		transform.camera.depthTextureMode = DepthTextureMode.Depth;
		transform.camera.targetTexture.format = RenderTextureFormat.Depth;
	}
}

what do you want to do? It is “green” because depth format is not color, but float-point number
You want some effect? then you need to customize shaders to write z by themselves into rt and read it after (iirc nor on dx9 nor on open gl you can’t just sample depth texture and grad depth value)
Or you can try to hook up to deferred shading :wink: It creates rt for depth. Read manuals - there should be something about grabbing that buffer if possible.

When i saw RenderTextureFormat.Depth in the manual, i thought it already calculates the z according to camera near and far clipping. But looks like we do it.
Anyways, thanks for clearing that up.

By the way, while you are around. I think you should check the depth texture part of the manual as the example there is not working.

Okay, here is my renderwithshader code:

using UnityEngine;
using System.Collections;

public class DepthTexture : MonoBehaviour {
	public Shader shader;
	public void Awake() {
		//transform.camera.depthTextureMode = DepthTextureMode.DepthNormals;
		//transform.camera.targetTexture.format = RenderTextureFormat.Depth;
		if (shader)
			transform.camera.SetReplacementShader(shader, null);
	}
}

and my depth rendering shader:

Shader "Orthographic Depth" {
	
	SubShader {
		Pass {
			Fog { Mode Off }
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag

			struct v2f {
				float4 pos : POSITION;
				float3 Z : TEXCOORD0;
			};

			v2f vert (float4 vertex : POSITION) {
				v2f o;
				float4 oPos = mul(UNITY_MATRIX_MVP, vertex);
				o.pos = oPos;
				o.Z = oPos.zzz;
				return o;
			}
			half4 frag( v2f i ) : COLOR {
				return i.Z.xxxx;
			}
			ENDCG
		}
	}
}

It still is very green.

hm, return i.Z.xxxx - you can’t have green only, or you are doing smth wrong :wink:

You got it all wrong. It is depth-stencil texture - it is completely different from your render target. You should use usual rt

Look at edge detection in Image Effect (Pro) package

It just doesnt work, i have 2 cameras in scene 1 main and 1 for render texture which is ortographic and looking down to the scene from above.
Scene just consists various cubes and spheres so theres something to render.
There is a plain object which is looking at my main camera so i can see what the outcome of the depth rendering.
ortohraphic camera renders with the above shader, and i see the depth is rendered but its totally green shaded, not grey.

I need to take one shot of the scene depth and save it as an image, so the way its done in edge detection shader is no use to my case. I need to make the above shader work. Please help.

you should render in usual color render target

Aha, i just reloaded my scene and now it works great.