The depth buffer is indeed updated every time something with ZWrite On is drawn. The _CameraDepthTexture does not because it is not the current depth buffer. It is a texture generated during a pre-pass of the scene that renders all of the visible shadow receiving opaque objects using their shadowcaster pass, copying the resulting depth buffer from that into the _CameraDepthTexture before rendering the scene again normally.
If you’re looking to do a volume rendering, you’d have to do that using a command buffer to render the backface depth into a render texture before the main scene renders, or you could render the depth value as the back face pass’s color and use a GrabPass() to sample it.
So there’s no way to render the first passes (back faces) before the _CameraDepthTexture is set and then rendering the second pass afterwards? I can’t think how to tell it to render the object twice.
Well, yes. You can make a shadowcaster pass for your shader that only renders the back faces like your first pass does, but this has a lot of implications. The depth texture is used for the main directional light shadow receiving, so if your object is transparent the objects behind it will receive shadows as if they’re where the back faces of your object is, and any post process effects that use the depth, like SSAO, depth of field, etc. will also be doing it’s stuff using that back face depth. It also means your object will be casting shadows using its back faces, unless you disable it on the renderer component.
Well, it’s an option. I might not necessarily want shadows. Interesting idea.
I did a google search and saw another post of yours! Basically I could use two cameras, and the first camera could render the back faces and the second camera could render the front faces. I’d have to set some flag in the material to tell it which to do. Unless there was some way to get the current camera ID in the shader… probably not.
What I did was clone the mesh object. Then have a script that flips the normals.
Draw it once in the geometry queue so it puts the backfaces on the z-buffer
Then on the other I have my transparent depth-dependent shader.
I hope there’s a better way of doing it! But at least I got it working.