LightMap光照Shader的實現

1,按照具體的步驟來生成LightMap文件後我們來寫Shader

shader "LightMap/LightMapSH"{

	Properties{
		_MainTex("Main tex",2D) = "white"{}
	}
	SubShader{
		pass{

			CGPROGRAM
			#pragma vertex vertx
			#pragma fragment frag
			#include "unitycg.cginc"

			sampler2D _MainTex;
			float4 _MainTex_ST; //unity 默認的紋理採樣變量 後面加 ST
	
			struct v2f{
				float4 pos : POSITION;
				float2 uv : TEXCOORD0;
				float2 uv2 : TEXCOORD1;
			};

			v2f vertx(appdata_full v)
			{	
				v2f o;
				o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
				o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
				o.uv2 = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
				return o;
			}

			fixed4 frag(v2f IN):COLOR
			{	
				float3 lm = DecodeLightmap(UNITY_SAMPLE_TEX2D( unity_Lightmap,IN.uv2 ));
				fixed4 color = tex2D(_MainTex,IN.uv);
				color.rgb *= lm ;

				return color ;
			}

			ENDCG
		}
	}

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章