Version: 6000.3
语言: 中文
禁用写入着色器中的深度缓冲区
在着色器中设置混合模式

检查或写入着色器中的模具缓冲区

模板缓冲区保存每像素 8 位值的内存存储。在 Unity 中,可以使用模板缓冲区来标记像素,然后仅渲染到通过模板作的像素。更多信息
请参阅术语表
为每个存储一个 8 位整数值像素计算机图像中的最小单位。像素大小取决于您的屏幕分辨率。像素光照是在每个屏幕像素下计算的。更多信息
请参阅术语表
在帧缓冲区中。在执行片段之前着色器在 GPU 上运行的程序。更多信息
请参阅术语表
对于给定像素,GPU 可以将模具缓冲区中的当前值与给定的参考值进行比较。这称为模板测试。如果模具测试通过,GPU 将执行深度测试。如果模具测试失败,GPU 将跳过该像素的其余处理。这意味着您可以使用模具缓冲区作为掩码来告诉 GPU 要绘制哪些像素,以及要丢弃哪些像素。

通常将模具缓冲区用于特殊效果,例如传送门或镜子。此外,有时在渲染硬阴影或构造实体几何体 (CSG) 时使用模具缓冲区。

您可以使用Stencil命令执行两项不同的作:配置模具测试,以及配置 GPU 写入模具缓冲区的内容。您可以在同一个命令中执行这两项作,但最常见的用例是创建一个Shader 对象Shader 类的实例,Shader 对象是着色器程序和 GPU 指令的容器,以及告诉 Unity 如何使用它们的信息。将它们与材质一起使用,以确定场景的外观。更多信息
请参阅术语表
遮罩其他着色器对象无法绘制到的屏幕区域。为此,需要将第一个着色器对象配置为始终通过模具测试并写入模具缓冲区,并将其他对象配置为执行模具测试而不是写入模具缓冲区。

使用Ref,ReadMaskComp参数来配置模具测试。使用Ref,WriteMask,Pass,FailZFail配置模具写入作的参数。

此命令对渲染状态进行更改。在Pass块来设置该通道的渲染状态,或在SubShader块来设置该子着色器中所有通道的渲染状态。

钢网测试方程为:

(ref & readMask) comparisonFunction (stencilBufferValue & readMask)

例子

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

        Pass
        {    
             // All pixels in this Pass will pass the stencil test and write a value of 2 to the stencil buffer
             // You would typically do this if you wanted to prevent subsequent shaders from drawing to this area of the render target or restrict them to render to this area only
             Stencil
             {
                 Ref 2
                 Comp Always
                 Pass Replace
             }            

             // The rest of the code that defines the Pass goes here.
        }
    }
}

此示例代码演示了在 SubShader 块中使用此命令的语法。

Shader "Examples/CommandExample"
{
    SubShader
    {
             // All pixels in this SubShader pass the stencil test only if the current value of the stencil buffer is less than 2
             // You would typically do this if you wanted to only draw to areas of the render target that were not "masked out"
             Stencil
             {
                 Ref 2
                 Comp Less
             }  

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

        Pass
        {    
              // The rest of the code that defines the Pass goes here.
        }
    }
}

其他资源

禁用写入着色器中的深度缓冲区
在着色器中设置混合模式