Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

ZTest and Z buffer direction

Discussion in 'Shaders' started by andybe, Mar 30, 2017.

  1. andybe

    andybe

    Joined:
    Nov 15, 2013
    Posts:
    14
    I realize that the Z buffer can have a different direction depending on the version of the graphics interface that is used, according to https://docs.unity3d.com/Manual/SL-PlatformDifferences.html.

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

    Thanks
    Andy
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    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).
     
    moonflower likes this.
  3. andybe

    andybe

    Joined:
    Nov 15, 2013
    Posts:
    14
    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.
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    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.