Shader multi_compile

一、新建一个Shader和一个Material

 
 
  1. Shader "Custom/Multi_Compile" {
  2. SubShader {
  3. Pass {
  4. CGPROGRAM
  5. #pragma vertex vert
  6. #pragma fragment frag
  7. #pragma multi_compile MY_multi_1 MY_multi_2 //告诉Unity编译两个不同版本的Shader
  8. #include "UnityCG.cginc"
  9. struct vertOut {
  10. float4 pos:SV_POSITION;
  11. };
  12. vertOut vert(appdata_base v)
  13. {
  14. vertOut o;
  15. o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
  16. return o;
  17. }
  18. float4 frag(vertOut i):COLOR
  19. {
  20. float4 c = float4(0, 0, 0, 0);
  21. #ifdef MY_multi_1
  22. c = float4(0, 1, 0, 0);//输出绿色
  23. #endif
  24. #ifdef MY_multi_2
  25. c = float4(0, 0, 1, 0);//输出蓝色
  26. #endif
  27. return c;
  28. }
  29. ENDCG
  30. }
  31. }
  32. FallBack "Diffuse"
  33. }

二、新建个Cube, 挂上脚本Multi_Compile.cs

 
 
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Multi_Compile : MonoBehaviour {
  5.  
  6. public bool multi_1;
  7. // Use this for initialization
  8. void Start () {
  9. if (multi_1) {
  10. Shader.EnableKeyword ("MY_multi_1");
  11. Shader.DisableKeyword ("MY_multi_2");
  12. } else {
  13. Shader.EnableKeyword ("MY_multi_2");
  14. Shader.DisableKeyword ("MY_multi_1");
  15. }
  16. }
  17. }
运行效果,当multi_1=true时:

multi_true.png

运行效果,当multi_1=false时:

multi_false.png

猜你喜欢

转载自blog.csdn.net/qq_14914623/article/details/80725444