Search Unity

Resolve z-fighting with tiebreaker variable in manual z-test

Discussion in 'General Graphics' started by Shinyclef, Sep 25, 2017.

  1. Shinyclef

    Shinyclef

    Joined:
    Nov 20, 2013
    Posts:
    505
    Hello,

    I'm building a game where players can place blocks where they like. I provide tools to make it easy to align edges. By design, blocks can overlap. Unfortunately, this makes it really easy to introduce z-fighting.

    I would like to perform my own manual z test that works as follows:
    If the fragment is being compared to another fragment whose depth is 'very close', then use the fragment whose variable 'x' is larger.

    When I generate my block meshes, I'd like to store an incrementing int in the vertex data of that block so that in the case of a z-depth clash, take the block that was generated last.

    Can anyone help me figure out how I might accomplish this? Can the depth buffer store a second variable and can I use it in a manual test? Also, will it be performant?

    Thanks.
     
  2. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    That won't really work. The depth buffer has some extra room, the stencil buffer, but you can't really store a depth value there.

    You could disable z-testing altogether and just draw the blocks front back to front.
     
  3. Shinyclef

    Shinyclef

    Joined:
    Nov 20, 2013
    Posts:
    505
    The 'blocks' are part of larger meshes which overlap, so I don't have any reliable order and hence the z-testing is necessary. I can stored data in each 'blocks' vertex data. I can state this another way by saying "I know which triangle I would like to draw last in the event of a 'very close' z-depth by looking at some vertex data".

    I'll look into what the stencil buffer is. Is there really no other way to implement custom z-test behaviour?
     
  4. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    There are plenty of ways to configure the z-testing, but in the case of "very close" that won't help much. Could you share some screenshots? That might help visualizing your case.