《Unity着色器圣经》3.2.3 | ShaderLab深度测试

目录索引

译文

深度测试(ZTest)通常用于在有多 pass 的着色器中生成颜色和深度差异。该属性有七个不同的值,分别是:

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

它们与比较操作相对应。

ZTest Less: (<) 绘制位于现有几何体前面的几何体。不绘制位于现有几何体相同距离或后面的几何体。

ZTest Greater: (>) 绘制位于现有几何体后面的几何体。不绘制位于现有几何体相同距离或前面的几何体。

ZTest LEqual: (≤) 默认值。绘制位于现有几何体前面或相同距离的几何体。不绘制位于现有几何体后面的几何体。

ZTest GEqual: (≥) 绘制位于现有几何体后面或相同距离的几何体。不绘制位于现有几何体前面的几何体。

ZTest Equal: (==) 绘制位于现有几何体相同距离的几何体。不绘制位于现有几何体前面的或后面的几何体。

ZTest NotEqual: (! =) 绘制不位于现有几何体相同距离的几何体。不绘制位于现有几何体相同距离的几何体。

ZTest Always: 不进行深度测试。绘制所有几何体,无论距离如何。

为了理解深度测试,我们将做以下练习: 假设场景中有两个物体(一个立方体和一个球体),相对于摄像机而言球体位于立方体的前方,像素深度符合预期。

图片[1]-《Unity着色器圣经》3.2.3 | ShaderLab深度测试-软件开发学习笔记
Fig. 3.2.3a

如果我们将球体放置在立方体后面,深度值也和我们预期的一样,为什么呢?因为 Z 缓冲存储的是屏幕上每个像素的深度值,深度值是根据物体与相机之间的深度距离计算得出的。

图片[2]-《Unity着色器圣经》3.2.3 | ShaderLab深度测试-软件开发学习笔记
Fig. 3.2.3b

现在,如果我们将深度测试设置成“总是(Always)”会发生什么呢?在这种情况下,深度测试并不会发生,所有像素在屏幕上显示的深度是相同的。

图片[3]-《Unity着色器圣经》3.2.3 | ShaderLab深度测试-软件开发学习笔记
Fig. 3.2.3c

深度测试的语法如下所示:

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

原文对照

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: (<) Draws the objects in front. It ignores objects that are at the same distance or behind the shader object.

ZTest Greater: (>) Draws the objects 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.

图片[1]-《Unity着色器圣经》3.2.3 | ShaderLab深度测试-软件开发学习笔记
Fig. 3.2.3a

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.

图片[2]-《Unity着色器圣经》3.2.3 | ShaderLab深度测试-软件开发学习笔记
Fig. 3.2.3b

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.

图片[3]-《Unity着色器圣经》3.2.3 | ShaderLab深度测试-软件开发学习笔记
Fig. 3.2.3c

Its syntax is the following:

Shader "InspectorPath/shaderName" 
{ 
    Properties { … } 
    SubShader 
    { 
        Tags { "Queue"="Transparent" "RenderType"="Transparent" } 
        ZTest LEqual 
    } 
}
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容