Enable depth texture for editor camera?

I can enable the depth texture for in-game cameras using the instructions here:

However I don’t know how to enable the depth texture for the editor’s scene camera. This means I can’t see the shader I’m working on (which requires a depth texture) unless I’m running the game. Is there a way to specify that the editor camera should generate a depth texture?

Thanks!

PM

From release notes.
Scene view camera renders the depth texture if you set the game view camera to render it, so the reason why you don’t see your shader might be something else.

Thanks for the info. At the moment the game camera only has a depth texture when the game is running (the code that enables it is in the Start() function). How would I go about enabling it in editor mode?

Bumping this to add this script to make depth texture mode set in edit mode (and runtime also):

using UnityEngine;
[ExecuteInEditMode]
public class EnableDepthTexture : MonoBehaviour
{
    void Start()
    {
        Camera.main.depthTextureMode = DepthTextureMode.Depth;
    }
}

Attach to any object in the scene, and it should work.

2 Likes

Derp. I know this is three years too late, but Start() functions don’t work in the editor using [ExecuteAlways] or [ExecuteInEditMode].

So this is a wrong solution.

Five years later and there is still no solution to this? Very irritating, this is.

Swap Start() with OnEnable().

2 Likes

Bump with this problem too.
I have editor camera, I used Camera.main.depthTextureMode = DepthTextureMode.Depth; in OnEnable(), I use _CameraDepthTexture and the shader result is visible only in GameView :confused: Editor’s camera doesn’t render anything.
I have being trying to start\stop game simulation - no success, editor’s camera doesn’t show anything.

1 Like

Similar issue here. I have a URP postprocessing render feature (using the Scene Depth node in Shader graph) that replaces the render with its depth texture. In game, it works mostly fine.

However, in scene view, it looks like this – all black and white. It stays like this even if I have an editor script to enable depth textures. (I’m guessing this is because the script given above enables depth textures on the in-game camera… but not the Scene view camera.)

Being unable to see depth texture based shaders in Scene view makes it really hard to test them!

Note: It DOES work if you switch to game view while the game isn’t running, so it’s not a Play vs Edit mode thing. It seems to be an issue with the Scene camera never rendering depth textures, or doing so improperly.