#ZWrite is confusing me ( ꩜ ᯅ ꩜;)

1 messages · Page 1 of 1 (latest)

winter dirge
#

For context, I'm expanding on an existing Ocean shader.

I need ZWrite Off because I need to read the scene depth of stuff under my Ocean plane
BUT, I also need to have ZWrite On because my fog render feature will just cover it up in fog if there is no depth written.

So my question is basically this: how can I use the scene depth texture in my shader BEFORE writing to it?

In my head I thought I would just write a render pass with Zwrite Off to render the Ocean color and then after that write a depthOnly pass with Zwrite on. I've had 0 luck attempting this so far. It seems like I made this method up in my head and it's not real. Maybe this is something that's solvable with render features? idk, but I tried to use the render graph system b4 and that shi was scary.

oblique summit
#

ZWrite shouldn't be important for reading Scene Depth, the shader just needs to be in the Transparent queue (Render Queue of 2501 or later) so the read occurs after the _CameraDepthTexture has been generated.

So should be okay to force ZWrite On in the water shader (or if that messes with sorting, another material that only writes to depth by also using ColorMask 0). But that alone won't stop the fog covering it up, because that writes to the depth buffer which is different from the depth texture.

URP generates the depth texture either by copying the depth buffer (usually once after opaques) or as a prepass, which uses the "DepthOnly" pass in opaque shaders.

So you need a custom renderer feature to either :

  • enqueue another CopyDepth pass after transparents to "update" the depth texture with the depth buffer again (not sure if all platforms support this though)
  • or grab the depth texture and render "DepthOnly" pass for transparent objects into it directly

The latter method was made kinda difficult in 2022 versions but it seems RenderGraph changes in newer versions now expose cameraDepthTexture in UniversalResourceData so looks more promising... hopefully this works :
(tested in Unity 6.1)
(to clarify, water shader needs DepthOnly pass, and may need ZWrite On - at least for scene view)

#

If you get that working might also want to test a build, I've only tested in editor