Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

ZTest Less vs. ZTest LEqual for one-pass rendering

Discussion in 'Shaders' started by Jessy, Aug 4, 2010.

  1. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Do you know if there's ever a time when ZTest Less is going to give different results than ZTest LEqual? I'm not talking about the cases where you're using another pass in the same SubShader, where LEqual will yield the same results as Equal.

    I tried a shader with a Ztest Less pass, and a Ztest Greater pass. I figured maybe there'd be some 1-pixel error with a plane intersecting the mesh; in this case, I used a near clip value of .01 and a far clip value of 99999, which is the maximum delta I can get before Unity throws errors and the camera fails to work at all. Even with the mesh right by the far clip plane, I couldn't get any pixels from the plane to show up between the in-front and in-back sections of the mesh.

    Furthermore, I can't even get ZTest Equal to work when two polygons are coplanar - the Equal pass instead has to be in the same SubShader as another pass that used a different ZTest, in order for the Equal pass to show up.

    To be clear, I don't get the impression this is a bug. I just wonder if, in theory, ZTest Less and Ztest LEqual could yield different results under the most common circumstances. And is the reason that Ztest LEqual is the default, is that you can use it for all the passes in a shader, without having to define the ZTest?

    Additionally, if you have any information on whether ZTest Less or ZTest Equal are measurably faster than ZTest LEqual (for the appropriate cases where results are identical), particularly for iOS devices, where things can behave strangely*, please let me know.

    * I did some tests and noticed no performance change from using ZTest Always and ZWrite Off, in cases where the rendered results would be identical to those seen from the defaults, ZTest LEqual and ZWrite On.
     
  2. Lulucifer

    Lulucifer

    Joined:
    Jul 8, 2012
    Posts:
    358
    Hi,Jessy, I just get caught the same insane thing as you,
    But I worked out this damn thing only happens when camera rendering path is derferred Lighting
    :)
     
  3. Lulucifer

    Lulucifer

    Joined:
    Jul 8, 2012
    Posts:
    358
    For the insane situation,like Less ,Greater, in deferred,I do it by ZWirte off,
    It just works,but there is something un_nature lighting related to depth,
    dont know why
    here are two pics
    $2013-02-02_141307.png
    $2013-02-02_141240.png
    the first pic is in forward,Lighting is fine
     
    Last edited: Feb 2, 2013
  4. Martin-Kraus

    Martin-Kraus

    Joined:
    Feb 18, 2011
    Posts:
    617
    I cannot test anything now, but I would assume that the "common circumstance" where it matters is when you use the exact same geometry in both passes. That might also be the reason for the default of "ZTest LEqual": if you render the same geometry a second time you probably have reasons for it and want to see something from the 2nd pass.
     
    Last edited: Feb 2, 2013
  5. Lulucifer

    Lulucifer

    Joined:
    Jul 8, 2012
    Posts:
    358
    the insane thing in deferred lighting like this:
    $2013-02-01_231932.png
    the material use Ztest Less, It is so different compared with ZTest Lequal:(
     
  6. Lulucifer

    Lulucifer

    Joined:
    Jul 8, 2012
    Posts:
    358
    Code (csharp):
    1.  
    2. Shader "ZTest" {
    3. Properties {
    4.     _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    5. }
    6. SubShader {
    7.     Tags { "RenderType"="Opaque" "Queue"="Geometry+300"}
    8.     pass{
    9.         ZTest Less
    10.         Material{Diffuse(1,1,1,1)}
    11.         Lighting On
    12.         SetTexture[_MainTex]{combine texture*primary}
    13.     }
    14.     }
    15. }
    16.  
    and here is the code