Search Unity

Enable depth texture for editor camera?

Discussion in 'General Graphics' started by Pyronious2, Apr 16, 2015.

  1. Pyronious2

    Pyronious2

    Joined:
    Aug 21, 2014
    Posts:
    2
    I can enable the depth texture for in-game cameras using the instructions here:

    http://docs.unity3d.com/Manual/SL-CameraDepthTexture.html

    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
     
  2. iSinner

    iSinner

    Joined:
    Dec 5, 2013
    Posts:
    201
    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.
     
  3. Pyronious2

    Pyronious2

    Joined:
    Aug 21, 2014
    Posts:
    2
    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?
     
  4. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    Bumping this to add this script to make depth texture mode set in edit mode (and runtime also):

    Code (CSharp):
    1. using UnityEngine;
    2. [ExecuteInEditMode]
    3. public class EnableDepthTexture : MonoBehaviour
    4. {
    5.     void Start()
    6.     {
    7.         Camera.main.depthTextureMode = DepthTextureMode.Depth;
    8.     }
    9. }
    Attach to any object in the scene, and it should work.
     
    manurocker95 and mahdi1375unity like this.
  5. Gigabitten_Gaming

    Gigabitten_Gaming

    Joined:
    Jul 10, 2017
    Posts:
    32
    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.
     
  6. Gigabitten_Gaming

    Gigabitten_Gaming

    Joined:
    Jul 10, 2017
    Posts:
    32
    Five years later and there is still no solution to this? Very irritating, this is.
     
  7. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    Swap
    Start()
    with
    OnEnable()
    .
     
    st-VALVe and DrJBN like this.
  8. CGDever

    CGDever

    Joined:
    Dec 17, 2014
    Posts:
    156
    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 :/ 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.
     
    a52 likes this.
  9. a52

    a52

    Joined:
    Mar 15, 2018
    Posts:
    18
    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.
     
    Last edited: Oct 16, 2023