Version: 2022.2
Language : English
ShaderLab command: ZTest
ShaderLab legacy functionality

ShaderLab command: ZWrite

Sets whether the depth bufferA memory store that holds the z-value depth of each pixel in an image, where the z-value is the depth for each rendered pixel from the projection plane. More info
See in Glossary
contents are updated during rendering. Normally, ZWrite is enabled for opaque objects and disabled for semi-transparent ones.

Disabling ZWrite can lead to incorrect depth ordering. In this case, you need to sort geometry on the CPU.

Render pipeline compatibility

Feature name Built-in Render PipelineA series of operations that take the contents of a Scene, and displays them on a screen. Unity lets you choose from pre-built render pipelines, or write your own. More info
See in Glossary
Universal Render Pipeline (URP) High Definition Render Pipeline (HDRP) Custom SRP
ZWrite Yes Yes Yes Yes

Usage

This command makes a change to the render state. Use it in a Pass block to set the render state for that Pass, or use it in a SubShader block to set the render state for all Passes in that SubShader.

Signature Example syntax Function
ZWrite [state] ZWrite Off Enables or disables writing to the depth buffer.

Valid parameter values

Parameter Value Function
state On Enables writing to the depth buffer.
Off Disables writing to the depth buffer.

Examples

This example code demonstrates the syntax for using this command in a Pass block.

Shader "Examples/CommandExample"
{
    SubShader
    {
         // The rest of the code that defines the SubShader goes here.

        Pass
        {    
              // Disables writing to the depth buffer for this Pass
              ZWrite Off
            
              // The rest of the code that defines the Pass goes here.
        }
    }
}

This example code demonstrates the syntax for using this command in a SubShader block.

Shader "Examples/CommandExample"
{
    SubShader
    {
         // Disables writing to the depth buffer for this SubShader
         ZWrite Off

         // The rest of the code that defines the SubShader goes here.        

        Pass
        {    
              // The rest of the code that defines the Pass goes here.
        }
    }
}
ShaderLab command: ZTest
ShaderLab legacy functionality