So I am quarreled by the rendering order of an object because of the following example and I hope one of you can help me understand.
We turn off ZWrite (i.e. writing to the depth buffer) for a transparent object A.
Would this not make it so that now, any object closer than A to the camera, would be overlapped by A?
I mean, it can’t depend on the render queue since Transparent objects are drawn AFTER opaque ones.
Why turn it off? If the color buffer uses the depth buffer to accurately estimate which surface pixels to draw (Depth Testing), would then not writing to the depth buffer mean the color buffer would just draw A every time even if there’s an object in front?
Obviously there is something I am not understanding in regards to how an object is rendered, or in what order.
I attached an image of what I am trying to understand as to how it is being drawn.
ZWrite controls if an object writes to the depth buffer, not if it tests against it, which is what ZTest does. Testing against the depth buffer will cause pixels that fail the test to not be rendered, and thus appear occluded by that surface. Transparent materials still usually have ZTest LEqual enabled (that is the default if it’s not specified), meaning any opaque objects that have already rendered and did write to the depth buffer can be used by the transparent materials to depth test against.
The depth buffer, along with depth testing, exist to ensure opaque objects sort correctly regardless of the order they render in. The reason transparent objects don’t (usually) render to the depth is because the depth buffer can only be used to ensure opaque surfaces are rendered correctly since there’s only one color & depth stored per pixel, you can only really choose to reject or replace the current pixel’s color & depth, or blend on top of the existing color. If two transparent surfaces render out of order there’s no way to “place” a surface “behind” one that’s already been rendered without knowing the color of that previously rendered transparent surface. In the case of using ZWrite On on a transparent surface the best case scenario is the surfaces are already rendering in the correct order an ZWrite does absolutely nothing compared to having it off, and worst case the closer surfaces render first and cause the surface further away to get depth rejected potentially causing random “holes” in the mesh depending on the mesh’s triangle order.