使用multi_compile编译Shader的多个版本

作者:追风剑情 发布于:2014-7-11 20:24 分类:Shader

一、新建一个Shader和一个Material

Shader "Custom/Multi_Compile" {
	SubShader {
		Pass {
		CGPROGRAM
		#pragma vertex vert
		#pragma fragment frag
		#pragma multi_compile MY_multi_1 MY_multi_2 //告诉Unity编译两个不同版本的Shader
		#include "UnityCG.cginc"
		struct vertOut {
			float4 pos:SV_POSITION;
		};
		vertOut vert(appdata_base v)
		{
			vertOut o;
			o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
			return o;
		}
		float4 frag(vertOut i):COLOR
		{
			float4 c = float4(0, 0, 0, 0);
			#ifdef MY_multi_1
			c = float4(0, 1, 0, 0);//输出绿色
			#endif
			#ifdef MY_multi_2
			c = float4(0, 0, 1, 0);//输出蓝色
			#endif
			return c;
		}
		ENDCG
		}
	} 
	FallBack "Diffuse"
}

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

using UnityEngine;
using System.Collections;

public class Multi_Compile : MonoBehaviour {

	public bool multi_1;
	// Use this for initialization
	void Start () {
		if (multi_1) {
			Shader.EnableKeyword ("MY_multi_1");
			Shader.DisableKeyword ("MY_multi_2");
		} else {
			Shader.EnableKeyword ("MY_multi_2");
			Shader.DisableKeyword ("MY_multi_1");
		}
	}
}
运行效果,当multi_1=true时:

multi_true.png

运行效果,当multi_1=false时:

multi_false.png

标签: multi_compile

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号