Version: 2022.3
Language : English
ShaderLab command: ZClip
ShaderLab command: ZWrite

ShaderLab command: ZTest

Sets the conditions under which geometry passes or fails depth testing.

Depth testing allows GPUs that have “Early-Z” functionality to reject geometry early in the pipeline, and also ensures correct ordering of the geometry. You can change the conditions of depth testing to achieve visual effects such as object occlusion.

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
ZTest 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
ZTest [operation] ZTest Less Set the conditions under which geometry passes or fails depth testing.

Valid parameter values

Parameter Value Function
operation
Disabled Disable the depth test.
Never Draw no geometry, regardless of distance.
Less Draw geometry that is in front of existing geometry. Do not draw geometry that is at the same distance as or behind existing geometry.
Equal Draw geometry that is at the same distance as existing geometry. Do not draw geometry that is in front of or behind existing geometry.
LEqual Draw geometry that is in front of or at the same distance as existing geometry. Do not draw geometry that is behind existing geometry.

This is the default value.
Greater Draw geometry that is behind existing geometry. Do not draw geometry that is at the same distance as or in front of existing geometry.
NotEqual Draw geometry that is not at the same distance as existing geometry. Do not draw geometry that is at the same distance as existing geometry.
GEqual Draw geometry that is behind or at the same distance as existing geometry. Do not draw geometry that is in front of existing geometry.
Always No depth testing occurs. Draw all geometry, regardless of distance.

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
        {    
              // Sets the depth test operation to Equal for all pixels in this Pass
              // You would typically do this if you want to render the geometry exactly where already rendered geometry is
              ZTest Equal
            
              // 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
    {
        // Sets the depth test operation to Equal for all pixels in this Pass
        // You would typically do this if you want to render the geometry exactly where already rendered geometry is
        ZTest Equal

         // 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: ZClip
ShaderLab command: ZWrite
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961