ZTest and Z buffer direction

I realize that the Z buffer can have a different direction depending on the version of the graphics interface that is used, according to Unity - Manual: Writing shaders for different graphics APIs.

In shader code, does ZTest Less|Greater|LEqual etc. work any differently on different platforms?

Thanks
Andy

Unity appears to flip the ZTest mode for you when they’re using a reversed z, so you don’t need to think about it (unless you’re manually supplying your own projection matrix to the shader, in which case you do).

1 Like

Okay, makes sense, Thanks.

Except, pardon me, how does a custom projection matrix influence “ZTest”?

I understand that reading a Z buffer value from inside a shader method is different by buffer direction.

If you’re using a custom projection matrix instead of one supplied by Unity, Unity may be expecting a reversed Z, so will be flipping the ZTest on you meaning you either need to flip your projections as well or manually flipping your shader’s ZTest as well so when Unity flips it again it’ll be what you actually want. The hard part is I don’t know if Unity has anything on the script side that you can use to know when the z is being reversed so there might be some guess and check there.