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

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:
2655423--187188--withoutDirLight.png
and this one, when a directional light is casting shadows:
2655423--187192--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?

1 Like

Well I searched a bit more about this annoying problem…

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.