Get an 20% OFF using the RELEASE code on your book purchase. For a limited time.

Introduction to the shader programming language

3.2.3. ShaderLab ZTest

This post is also available in…

ZTest controls how Depth Testing should be performed and is generally used in multi-pass shaders to generate differences in colors and depths. This property has seven different values ​​which are:

  • Less.
  • Greater.
  • LEqual.
  • GEqual.
  • Equal.
  • NotEqual.
  • Always.

Which they correspond to a comparison operation. 

  • ZTest Less: (<) Draw the objects in front. It ignores objects that are at the same distance or behind the shader object. 
  • ZTest Greater: (>) Draws the object behind. It does not draw objects that are at the same distance or in front of the shader object. 
  • ZTest LEqual: (≤) Default value. Draws the objects that are in front of or at the same distance. It does not draw objects behind the shader object. 
  • ZTest GEqual: (≥) Draws the objects behind or at the same distance. Does not draw objects in front of the shader object. 
  • ZTest Equal: (==) Draws objects that are at the same distance. Does not draw objects in front of or behind the shader object. 
  • ZTest NotEqual: (!=) Draws objects that are not at the same distance. Does not draw objects that are the same distance from the shader object. 
  • ZTest Always: Draws all pixels regardless of the distance of the objects relative to the camera.

To understand this command, we will do the following exercise: Let’s suppose we have two objects in our scene; a cube and a sphere. The sphere is in front of the cube relative to the camera and the pixel depth is as expected.

(Fig. 031)

If we position the sphere behind the cube then again, the depth values ​​ will be as expected, why? Because the Z-Buffer is storing depth values ​​for each pixel on the screen. The depth values are calculated concerning the proximity of an object to the camera. 

(Fig. 032)

Now, what would happen if we activated ZTest Always? In this case, Depth Testing would not be done, therefore, all pixels would appear at the same depth on screen. 

(Fig. 033)

Its syntax is the following:

Shader “InspectorPath / shaderName”
{
    Properties {} ...
    SubShader
    {
        Tags { "Queue" = "Transparent" “RenderType”=”Transparent”} 
        ZTest LEqual
    }
}

Follow us to stay informed about all the latest news, updates, and more.

Join the group to share your experiences with other developers.

Subscribe to our channel and keep learning game dev!

jettelly-logo

Jettelly Team

We are a team of indie developers with more than 9 years of experience in video games. As an independent studio, we have developed Nom Noms in which we published with Hyperbeard in 2019. We are currently developing The Unity Shader Bible.

Follow us on our social networks.