I have an depth-mask object that writes to the depth buffer, but nowhere else (ZWrite On, ColorMask 0).
I need to sample this object’s depth value using SAMPLE_DEPTH_TEXTURE later in the rendering process, for an image effect.
When I render it in opaque geometry before the skybox (Queue < 2500), the skybox doesn’t render behind it, because (I’m assuming) it uses early depth “culling” (?) to avoid overdraw for optimization.
When I render it in transparent geometry after the skybox (Queue > 2500), the skybox renders correctly, and the frame debugger shows the depth texture as correct too, but SAMPLE_DEPTH_TEXTURE seems to ignore the new value. I presume this is because _CameraDepthTexture is only written to in the “Resolve Depth” step, which happens before the skybox render for the aforementioned optimizations.
I need to have the skybox properly rendered behind the invisible object, and the object’s depth written for sampling. How can I do this?
The easiest solution would be to render the skybox before opaque geometry (and therefore before depth resolution), even if it comes at the cost of some overdraw. However, I don’t know of any option that does this. Even modifying the render queue in the built-in shaders seems to do nothing (it’s likely hardcoded?). I could write my own skybox shader and apply it to custom geometry, but it’s a lot of trouble and I’d rather avoid it.
Likewise, I could fix it by forcing another depth resolution (writing the depth texture to _CameraDepthTexture), but I don’t know how to do that (from my CommandBuffer, trying to Blit() from Depth warns that the render texture is not found).
I also tried using _LastCameraDepthTexture, but it doesn’t seem to work either.
Any help / suggestion is welcome!