目录索引
译文
要想理解这两个概念,我们需要先了解什么是 Z 缓冲(深度缓冲)和深度测试的工作原理。
在正式开始之前,我们需要先了解的是:像素是有深度值的。这些值被存储在一个叫做深度缓冲的地方,它决定了屏幕上一个物体是在另一个物体的前面还是后面。
深度测试是一个决定是否更新深度缓冲区中像素的测试。
我们已经知道,像素的 RGB 颜色被存储在颜色缓冲(Color Buffer)中。Z 缓冲增加了一个“像素到相机的深度”的值,但只适用于那些在视锥体内的面。如此一来就允许两个像素颜色相同但深度不同。
离相机越近的物体,在Z缓冲中的值越小。Z缓冲中值较小的像素会覆盖值较大的像素。
假设现在场景中有一个相机和一些预制件,我们把它们都摆放在世界空间的“Z”坐标轴上。为什么要放在 Z 轴上?因为 Z 缓冲的“Z”就来自于这样一个事实: Z 值表示的是 Z 轴上摄像机与物体之间的距离,而 X 和 Y 值则表示的是屏幕上的水平和垂直位移。
“缓冲”一词指的是“内存空间(memory space)”,数据可以被暂时性地存放在其中。 Z 缓冲指的是场景中的物体与摄像机之间的深度值,这些深度值被分配给每个像素。
比如说,现在我们有一个6*6大小、总共36个像素的屏幕。
每当我们在场景中摆放物体时,这个物体就占据了屏幕的一块区域。好,现在我们往场景中摆一个4*4大小的蓝色正方形,占据了第8 ~ 29格像素(如下图所示),该区域内的所有像素都会被激活并变成蓝色,像素的信息被同时写入 Z 缓冲和颜色缓冲。
![图片[1]-《Unity着色器圣经》3.2.0 | SubShader剔除与深度测试-软件开发学习笔记](https://gamedevfan.cn/wp-content/uploads/2025/05/image-33-1024x629.jpeg)
现在,往场景中摆放一个新的黄色矩形,它离相机更近。为了与前一个矩形区分开来,新加入的矩形更小一些,占据了第15 ~ 29格像素(如下图所示)。我们可以看到,这个区域已经被新加入的矩形覆盖了,发生什么了?由于黄色矩形距离相机更近,这将覆盖 Z 缓冲和颜色缓冲的值,替换之前的颜色。
![图片[2]-《Unity着色器圣经》3.2.0 | SubShader剔除与深度测试-软件开发学习笔记](https://gamedevfan.cn/wp-content/uploads/2025/05/image-32-1024x630.jpeg)
如果在场景中继续添加一个离相机更近的新物体,就会又发生一遍刚才提到的过程。总之,Z 缓冲和颜色缓冲的值将被离摄像机最近的物体覆盖。
生成吸引人的视觉效果的方法之一是修改 Z 缓冲的值。为此,我们将在接下来的几个小节中讨论三个内容: 剔除、深度写入和深度测试。
类似于标签,裁剪和深度测试也可以被写在子着色器或 Pass 语义块中。具体写在哪里取决于我们想达到的目标效果和 pass 的数量。
为了更好的理解这个概念,让我们假设我们要创建一个用来表示钻石表面的着色器。我们需要准备两个 pass,第一个用来表示钻石的背景颜色,第二个表示钻石的表面光泽。由于我们需要两个 pass 来实现不同的功能,因此有必要在每个 pass 内分别配置剔除选项。
原文对照
To understand both concepts, we must first know how Z-Buffer (also known as Depth Buffer) and Depth Testing work.
Before starting, we must consider that the pixels have depth values. These values are stored in the Depth Buffer, which determines if an object goes in front of or behind another on the screen.
On the other hand, Depth Testing is a conditional that determines whether a pixel will be updated or not in the Depth Buffer.
As we already know, a pixel has an assigned value that is measured in RGB color and stored in the Color Buffer. The Z-Buffer adds an extra value that measures the depth of a pixel in terms of distance to the camera, but only for those surfaces that are within its frustum, this permits two pixels to be the same in color, but different in depth.
The closer the object is to the camera, the lower the Z-Buffer value, and pixels with lower buffer values overwrite pixels with higher values.
To understand the concept, let’s assume that we have a camera and some primitives in our scene, all positioned on the “Z” space axis. Now, why on the z-axis? The “Z” in Z-Buffer comes from the fact that the Z value measures the distance between the camera and an object on the “Z” axis of space, while the “X and Y” values measure horizontal and vertical displacement on the screen.
The word “Buffer” refers to a “memory space” in which data will be temporarily stored, therefore, Z-Buffer refers to the depth values between the objects in our scene and the camera, which are assigned to each pixel.
For example, we are going to draw a screen with a total of 36 pixels.
Every time we position an object in our scene, that object occupies a certain pixel area on the screen. So, let’s assume that we want to position a green square in the scene. Given its nature, it will occupy from pixel 8 to 29, therefore, all the pixels inside this area are activated and painted green, likewise, this information will be sent to both the Z-Buffer and the Color Buffer.
![图片[1]-《Unity着色器圣经》3.2.0 | SubShader剔除与深度测试-软件开发学习笔记](https://gamedevfan.cn/wp-content/uploads/2025/05/image-33-1024x629.jpeg)
We position a new square in the scene, this time in red and closer to the camera. To differentiate it from the previous one, we will make this square smaller, occupying from pixel 15 to 29. As we can see, this area is already occupied by the information from the initial square, so what happens here? Since the red square is at a shorter distance from the camera, this overwrites the values of both the Z-Buffer and the Color Buffer, activating the pixels in this area, replacing the previous color.
![图片[2]-《Unity着色器圣经》3.2.0 | SubShader剔除与深度测试-软件开发学习笔记](https://gamedevfan.cn/wp-content/uploads/2025/05/image-32-1024x630.jpeg)
In the case of adding a new element to the scene that is even closer to the camera, this process will be repeated in the same way. In conclusion, the values of the Z-Buffer and Color Buffer will be overwritten by the object that is closest to the camera.
One way to generate attractive visual effects is by modifying the Z-Buffer values. For this, we will talk about three options that are included in Unity: Cull, ZWrite, and ZTest.
Like Tags, culling and depth testing options can be written in different fields: within the SubShader field or the Pass field. The position will depend on the result we want to achieve and the number of passes we want to work with.
To understand this concept, let’s assume that we want to create a shader to represent the surface of a diamond. For this, we will need two passes: The first we will use for the background color of the diamond, and the second for the shine of its surface. In this hypothetical case, since we require two passes that fulfil different functions, it will be necessary to configure the Culling options within each pass independently.
暂无评论内容