[UnityShader基础]06.#pragma multi_compile

参考链接:

https://blog.csdn.net/qq826364410/article/details/81774741

https://docs.unity3d.com/Manual/SL-MultipleProgramVariants.html

1.#pragma multi_compile MY_multi_1 MY_multi_2

定义了两个shader关键字,一个是MY_multi_1,另一个是MY_multi_2

MultiCompile.shader

 Shader "Custom/MultiCompile"
{
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile MY_multi_1 MY_multi_2 #include "UnityCG.cginc" struct appdata
{
float4 vertex : POSITION;
}; struct v2f
{
float4 vertex : SV_POSITION;
}; v2f vert (appdata v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
return o;
} fixed4 frag (v2f i) : SV_Target
{
fixed4 col = fixed4(, , , );
#ifdef MY_multi_1
col = fixed4(, , , );
#endif
#ifdef MY_multi_2
col = fixed4(, , , );
#endif
return col;
}
ENDCG
}
}
}

测试如下,来回按下QW,即可以看到两种颜色的切换

 using UnityEngine;

 public class MultiCompile : MonoBehaviour {

     void Update ()
{
if (Input.GetKeyDown(KeyCode.Q))
{
Shader.EnableKeyword("MY_multi_1");
Shader.DisableKeyword("MY_multi_2");
}
else if(Input.GetKeyDown(KeyCode.W))
{
Shader.EnableKeyword("MY_multi_2");
Shader.DisableKeyword("MY_multi_1");
}
}
}

2.#pragma multi_compile __ MY_multi_3

定义了一个shader关键字,MY_multi_3

MultiCompile2.shader

 Shader "Custom/MultiCompile2"
{
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile __ MY_multi_3 #include "UnityCG.cginc" struct appdata
{
float4 vertex : POSITION;
}; struct v2f
{
float4 vertex : SV_POSITION;
}; v2f vert (appdata v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
return o;
} fixed4 frag (v2f i) : SV_Target
{
fixed4 col = fixed4(, , , );
#ifdef MY_multi_3
col = fixed4(, , , );
#endif
return col;
}
ENDCG
}
}
}

测试如下,来回按下ER,即可以看到两种颜色的切换

 using UnityEngine;

 public class MultiCompile2 : MonoBehaviour {

     void Update ()
{
if (Input.GetKeyDown(KeyCode.E))
{
Shader.EnableKeyword("MY_multi_3");
}
else if (Input.GetKeyDown(KeyCode.R))
{
Shader.DisableKeyword("MY_multi_3");
}
}
}
上一篇:Android开发UI之补间动画-布局添加动画


下一篇:sqlserver 2008 局域网跨服务器T-SQL操作(二)