目录索引
译文
内置渲染管线(Built-in)和可编程渲染管线(SRP)都支持剔除命令,它能够在像素深度处理中选择删除模型的哪个面。这是什么意思?回想一下,模型对象有内表面和外表面。默认情况下,外表面是可见而内表面是不可见的的(Cull Back)。但是,还有其他几种情况:
- Cull Off 关闭剔除,模型的内外表面都被渲染
- Cull Back 默认情况,剔除背面(背对摄像机的面),渲染正面(面向摄像机的面)
- Cull Front 剔除正面,渲染背面
由此可见,剔除命令有三个可选的值:Back, Front 和 Off。剔除的默认值是背面剔除,但在通常情况下,出于优化目的,与剔除相关的代码行在着色器中是不可见的。如果我们要修改剔除选项,必须在关键字“Cull”后面加上我们要使用的模式。
Shader "InspectorPath/shaderName"
{
Properties { … }
SubShader
{
// Cull Off
// Cull Front
Cull Back
}
}
我们也可以用”UnityEngine.Rendering.CullMode” 动态修改材质检查器中的剔除选项,该依赖项通过枚举绘制器声明并作为参数传递给函数。
Shader "InspectorPath/shaderName"
{
Properties
{
[Enum(UnityEngine.Rendering.CullMode)]
_Cull ("Cull", Float) = 0
}
SubShader
{
Cull [_Cull]
}
}
另一个有用的选项是 SV_IsFrontFace 语义,它允许我们在两个网格面上投射不同的颜色和纹理。为此,我们只需在片元着色器阶段声明一个布尔变量并将该语义作为参数赋值即可。
fixed4 frag (v2f i, bool face : SV_IsFrontFace) : SV_Target
{
fixed4 colFront = tex2D(_FrontTexture, i.uv);
fixed4 colBack = tex2D(_BackTexture, i.uv);
return face ? colFront : colBack;
}
值得一提的是,只有在着色器中将“剔除”命令设置为“关闭(Off)”时,该选项才会起作用。
原文对照
This property, compatible in both Built-in RP and Scriptable RP, controls which face of a polygon will be removed in the pixel depth processing. What does this mean? Recall that a polygon object has inner faces and outer ones. By default, the outer faces are visible (Cull Back); however, we can activate the inner faces by following the scheme shown below.
- Cull Off Both faces of the object are rendered.
- Cull Back The back faces of the object are rendered, by default.
- Cull Front The front faces of the object are rendered.
This command has three values which are: Back, Front and Off. By default, “Back” is active, however, generally, the line of code associated with culling is not visible in the shader for optimization purposes. If we want to modify the culling options, we must add the word “Cull” followed by the mode we want to use.
Shader "InspectorPath/shaderName"
{
Properties { … }
SubShader
{
// Cull Off
// Cull Front
Cull Back
}
}
We can also dynamically configure the Culling options in the Unity Inspector through the dependency “UnityEngine.Rendering.CullMode” which is declared from the Enum drawer and passed as an argument in the function.
Shader "InspectorPath/shaderName"
{
Properties
{
[Enum(UnityEngine.Rendering.CullMode)]
_Cull ("Cull", Float) = 0
}
SubShader
{
Cull [_Cull]
}
}
Another helpful option occurs through the semantics SV_IsFrontFace, which allows us to project different colors and textures on both mesh faces. To do so, we simply declare a boolean variable and assign such semantics as an argument in the fragment shader stage.
fixed4 frag (v2f i, bool face : SV_IsFrontFace) : SV_Target
{
fixed4 colFront = tex2D(_FrontTexture, i.uv);
fixed4 colBack = tex2D(_BackTexture, i.uv);
return face ? colFront : colBack;
}
It is worth mentioning that this option only works when the “Cull” command has been previously set to “Off” in the shader.
暂无评论内容