目录索引
译文
关键词枚举会在材质检查器中生成一个弹出式样式菜单。与开关不同的是,这种绘制器允许我们为着色器配置多达九种不同的状态。类似我们在上一小节中所提到的,在声明关键词枚举时,我们也需要在一对中括号中添加 “KeywordEnum”,然后列出要使用的状态集。
[KeywordEnum(StateOff, State01, etc...)]
_PropertyName ("Display name", Float) = 0
在上面给出的例子中,我们列出了关键词枚举的状态,其中第一种状态对应默认状态(StateOff)。接着我们继续进行属性声明、在材质检查器中显示名称、数据类型,最后使用默认值进行初始化。
要想在我们的代码中使用绘制器,我们不仅可以使用 shader_feature 变体,还可以使用 multi_compile 变体,具体选择哪一个取决于我们希望在最终导出的项目中包含的变体数量。
我们已经知道 shader_feature 只会导出材质检查器中选中的变体,然而,multi_compile 可以导出着色器中所有能找到的变体(不管它们有没有被使用过)。鉴于这个特性,multi_compile 是导出或编译那些运行过程中可能存在多种状态的最佳选择(例如 超级马里奥 中无敌星的状态)。
为了更好地理解这个概念,让我们来看看这个例子:
Shader "InspectorPath/shaderName"
{
Properties
{
// 声明绘制器
[KeywordEnum(Off, Red, Blue)]
_Options ("Color Options", Float) = 0
}
SubShader
{
Pass
{
CGPROGRAM
…
// 声明变体与状态
#pragma multi_compile _OPTIONS_OFF _OPTIONS_RED _OPTIONS_BLUE
…
half4 frag (v2f i) : SV_Target
{
half4 col = tex2D(_MainTex, i.uv);
// 不同状态
#if _OPTIONS_OFF
return col;
#elif _OPTIONS_RED
return col * float4(1, 0, 0, 1);
#elif _OPTIONS_BLUE
return col * float4(0, 0, 1, 1);
#endif
}
ENDCG
}
}
}
在这个例子中,我们声明了一个叫做“_Options”的具有三种状态(关闭/红/蓝)的关键词枚举属性。接着,我们在 CGPROGRAM 语义块下的 multi_compile 变体中声明几种状态的常量。
#pragma multi_compile _OPTIONS_OFF _OPTIONS_RED _OPTIONS_BLUE
最后,通过这些状态,我们得以定义着色器主纹理的三种不同颜色的输出。
原文对照
This drawer generates a pop-up style menu in the material inspector. Unlike a Toggle, this drawer allows you to configure up to nine different states for the shader. To execute it we must add the word “KeywordEnum” in brackets and then list the set of states that we are going to use.
[KeywordEnum(StateOff, State01, etc...)]
_PropertyName ("Display name", Float) = 0
In the previous example, we add the KeywordEnum drawer in brackets, and then we list its states, where the first corresponds to the default state (StateOff). We continue with the property declaration, display name in the material inspector, its Float data type and finally, we initialize with its default value.
To declare this drawer within our code, we can use both the shader variant shader_feature and multi_compile. The choice will depend on the number of variants that we want to include in the final build.
As we already know, shader_feature will only export the selected variant from the material inspector, whereas multi_compile exports all variants that are found in the shader, regardless of whether they are used or not. Given this feature, multi_compile is great for exporting or compiling multiple states that will change at execution time (e.g. star status in Super Mario).
To understand its implementation, we will perform the following operation:
Shader "InspectorPath/shaderName"
{
Properties
{
// declare drawer Toggle
[KeywordEnum(Off, Red, Blue)]
_Options ("Color Options", Float) = 0
}
SubShader
{
Pass
{
CGPROGRAM
…
// declare pragma and conditions
#pragma multi_compile _OPTIONS_OFF _OPTIONS_RED _OPTIONS_BLUE
…
half4 frag (v2f i) : SV_Target
{
half4 col = tex2D(_MainTex, i.uv);
// generate conditions
#if _OPTIONS_OFF
return col;
#elif _OPTIONS_RED
return col * float4(1, 0, 0, 1);
#elif _OPTIONS_BLUE
return col * float4(0, 0, 1, 1);
#endif
}
ENDCG
}
}
}
In this example, we declare a KeywordEnum type property called “_Options” and configure three states for it (Off, Red and Blue). Later we add them to the multi_compile found in CGPROGRAM and declare them as constants.
#pragma multi_compile _OPTIONS_OFF _OPTIONS_RED _OPTIONS_BLUE
Finally, using the conditionals, we define the three states for our shader that correspond to color changes for the main texture.
暂无评论内容