目录索引
译文
在我们的着色器中,可以找到至少三个编译指令(pragma)。这些都是处理器指令,包含在 Cg 或 HLSL 中,其作用是帮助我们的着色器识别和编译某些函数,如果没有这些指令这些函数就无法被识别。
#pragma vertex vert 指令允许将名为 vert 的函数作为顶点着色器编译到 GPU。这一点至关重要,因为如果没有这行代码,GPU 将无法将“vert”函数识别为顶点着色器,我们将无法使用模型信息,也无法将这些信息传递给 片元着色器 并最终投射到屏幕上。
如果我们检查 USB_simple_color 着色器中的pass,我们会发现以下代码:
// 允许GPU将“vert”函数视作顶点着色器并编译
#pragma vertex vert
// 将“vert”函数视作顶点着色器
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_TRANSFER_FOG(o, o.vertex);
return o;
}
#pragma fragment frag 指令的作用与 pragma vertex 相似,不同之处在于它允许名为“frag”的函数作为片元着色器编译到GPU。
// 允许GPU将“frag”函数视作片元着色器并编译
#pragma fragment frag
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
UNITY_APPLY_FOG(i.fogCoords, col);
return col;
}
与之前介绍的两个指令不同的是,#pragma multi_compile_fog 指令具有双重功能。首先,multi_compile 指的是一种着色器变体,它允许我们在着色器中生成具有不同功能的变体。其次,“_fog ”启用了 Unity 的照明(Lighting)窗口中的雾功能,这意味着如果我们进入“环境/其他设置”选项卡,就可以启用或禁用着色器的雾选项。
原文对照
In our shader, we can find at least three default pragmas. These are processor directives and are included in Cg or HLSL. Their function is to help our shader recognize and compile certain functions that could not otherwise be recognized as such.
The #pragma vertex vert allows the vertex shader stage called vert to be compiled to the GPU as a vertex shader. This is essential since without this line of code the GPU will not be able to recognize the “vert” function as the vertex shader stage, therefore, we will not be able to use information from our objects and neither pass information to the fragment shader stage to be projected onto the screen.
If we look at the pass that is included in the USB_simple_color shader, we will find the following line of code related to the concept previously explained.
// allow us to compile the "vert" function as a vertex shader
#pragma vertex vert
// use "vert" as vertex shader
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_TRANSFER_FOG(o, o.vertex);
return o;
}
The #pragma fragment frag directive serves the same function as the pragma vertex, with the difference that it allows the fragment shader stage called “frag” to compile in the code as a fragment shader.
// allow us to compile the "frag" function as a fragment shader.
#pragma fragment frag
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
UNITY_APPLY_FOG(i.fogCoords, col);
return col;
}
Unlike previous directives, the #pragma multi_compile_fog has a double function. Firstly, multi_compile refers to a shader variant that allows us to generate variants with different functionalities within our shader. Secondly, the word “_fog” enables the fog functionalities from the Lighting window in Unity, this means that, if we go to that tab, Environment / Other Setting, we can activate or deactivate our shader’s fog options.
暂无评论内容