Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Unexpected content in camera depth texture when directional light shadows are enabled

Discussion in 'General Graphics' started by Aberdyne, May 30, 2016.

  1. Aberdyne

    Aberdyne

    Joined:
    Mar 17, 2015
    Posts:
    64
    Hello everyone,

    I'm trying to use the main camera depth texture in a shader (with _CameraDepthTexture). My setup is this: I have a camera that renders the scene without some layer (by disabling it in culling mask), then in an image effect script's OnRenderImage function I call RenderWithShader (with my shader) with another camera that has this layer only. My shader use the camera depth texture to test for object depth manually and not draw what's hidden behind objects of the scene.

    This works very well when no directional light is present, or, to be more precise, when this directional light cast no shadows, but as soon as it casts shadows my shader has different content for _CameraDepthTexture.

    I noticed that as soon as there is a directional light casting shadows, the rendering pipeline differs slightly:
    This screenshot shows what appears in the frame debugger when NO directional light is casting shadows:
    withoutDirLight.png
    and this one, when a directional light is casting shadows:
    withDirLight.png

    UpdateDepthTexture is called in the second case, clearing camera depth texture before each render thus breaking my manual depth testing.

    Is this is normal or is this a bug?
     
    Last edited: May 30, 2016
  2. Aberdyne

    Aberdyne

    Joined:
    Mar 17, 2015
    Posts:
    64
    Well I searched a bit more about this annoying problem...

    http://forum.unity3d.com/threads/accessing-depth-buffer-from-a-surface-shader.404380/#post-2641786

    It seems that having a directional light with shadows automatically trigger the creation of a camera depth texture for the other camera even if you set depthTextureMode to DepthTextureMode.None on it... It makes sense if the camera depth texture is needed for the shadows.

    So, the only workaround I found was to disable the involved lights while rendering the other camera...

    I wonder if there's another way to keep _CameraDepthTexture as rendered from main camera.