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

Introduction to the shader programming language

3.2.2. ShaderLab ZWrite

This post is also available in…

This command controls the writing of the surface pixels of an object to the Z-Buffer, that is, it allows us to ignore or respect the depth distance between the camera and an object. ZWrite has two values ​​which are: On and Off, where “On” corresponds to its default value. We generally use this command when working with transparencies, e.g., when we activate the Blending options.

  • ZWrite Off, for transparency. 
  • ZWrite On, default value.

Why should we disable the Z-Buffer when working with transparencies? Mainly because of the translucent pixel overlay (Z-fighting). When we work with semi-transparent objects, it is common that the GPU does not know which object lies in front of another, producing an overlapping effect between pixels when we move the camera in the scene. To fix this problem, we must simply deactivate the Z-Buffer by turning the ZWrite command to “Off” as the following example shows: 

Shader “InspectorPath / shaderName”
{
    Properties {} …
    SubShader
    {
        Tags { "Queue" = "Transparent" “RenderType”=”Transparent”} 
        Blend SrcAlpha OneMinusSrcAlpha
        ZWrite Off
    }
}

The Z-fighting occurs when we have two or more objects at the same distance from the camera, causing identical values in the Z-Buffer. 

(Fig. 030)

This effect occurs when trying to render a pixel at the end of the rendering pipeline. Since the Z-Buffer cannot determine which element is behind the other, it produces flickering lines that change shape depending on the camera’s position.

To correct this issue, we simply need to disable the Z-Buffer using the “ZWrite off” command.

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.