How to get a depth map of a scene? (depthTextureMode not doing it)

I’m trying to render a scene as a depth map, for export into another process (generating autostereograms, in case you’re curious). But I’m struggling to get Unity to do this. Here’s what I’ve tried:

  1. Added a script to the camera which, in Start, does
GetComponent<Camera>().depthTextureMode = DepthTextureMode.Depth;

This results in a little note appearing at the bottom of the Camera inspector:

“Info: renders Depth texture”. Sounds promising! And you can see I have set the Target Texture to a RenderTexture I created in the project.

But the result is not a depth map. The contents of my RenderTexture are an ordinary full rendering, including colors, lights, and shadows. Clearly I’ve missed some vital step.

Anybody know how to make this work? Or, is there an easier way to generate a depth map for a Unity scene?

1 Like

@JoeStrout Did you ever figure this out? Trying to do the same thing.

Not really. I think I set up a scene with unlit shaders that supported fog, and then used a linear fog. But it was a kludge, for sure.

How is the rendertexture settings ? Does it have a depth component ?

Also which pipeline is used and which render mode, forward or deferred ?