《Unity3D ShaderLab开发实战详解》第8章 基于光照贴图的烘焙照明

在Shader中使用Lightmap的简单的例子

Shader "Self-Illum/NewSurfaceShader"
{
    Properties{}

	SubShader
	{
		Tags{"RenderType" = "Opaque"}
		Pass
		{
			Tags{ "LightMode" = "ForwardBase"}
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"
			#include "Lighting.cginc"
			#pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON//开启LIGHTMAP_OFF LIGHTMAP_ON这两个宏。

			struct vertOut {
				float4 pos:SV_POSITION;
				float2 lmuv:TEXCOORD0;
			};
			vertOut vert(appdata_full v) {
				vertOut o;
				o.pos = UnityObjectToClipPos(v.vertex);
				o.lmuv = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;//计算光照贴图uv。
				return o;
			}
			float4 frag(vertOut i) :COLOR{
			#ifdef LIGHTMAP_ON 
				//如果应用了光照贴图就提取光照贴图。
				return float4(DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.lmuv)), 1);
			#else
				//如果没有应用光照贴图就显示绿色。
				return float4(0, 1, 0, 1);
			#endif
			}
			ENDCG
		}               
    }
    FallBack "Diffuse"
}

在Shader中使用Lightmap的完整的例子

Shader "Self-Illum/NewSurfaceShader"
{
    Properties{}

	SubShader
	{
		Tags{"RenderType" = "Opaque"}
		Pass
		{
			Tags{ "LightMode" = "ForwardBase"}
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"
			#include "Lighting.cginc"
			#pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON//开启LIGHTMAP_OFF LIGHTMAP_ON这两个宏。

			struct vertOut {
				float4 pos:SV_POSITION;
				float4 color:COLOR;
				float3 litDir:TEXCOORD0;
				float3 worldN:TEXCOORD1;
				float2 uv:TEXCOORD2;
				
				#ifdef LIGHTMAP_ON 
				float2 uv2:TEXCOORD3;
				#endif
			};
			
			vertOut vert(appdata_full v) {
				float4 worldV = mul(_Object2World, v.vertex);
				float4 worldN = mul(float3x3(_Object2World), SCALE_NORMAL);
				float3 color = Shade4PointLights(unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0, unity_LightColor[0], unity_LightColor[1], unity_LightColor[2], unity_LightColor[3], unity_4LightAtten0, worldV.xyz, worldN);
				
				vertOut o;
				o.pos = UnityObjectToClipPos(v.vertex);
				o.worldN = worldN;
				o.litDir = WorldSpaceLightDir(v.vertex);
				o.color = color;
				o.uv = v.texcoord1.xy;
				#ifdef LIGHTMAP_ON 
					o.uv2 = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
				#endif
				
				return o;
			}
			
			float4 frag(vertOut i) :COLOR{
				float4 c = max(0, dot(i.worldN, normalize(i.litDir))) * _LightColor0;
				#ifdef LIGHTMAP_ON 
					c += float4(DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv2)), 0);
				#endif
				return c;
			}
			ENDCG
		}      
		
		Pass
		{
			Tags{ "LightMode" = "ForwardAdd"}
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"
			#include "Lighting.cginc"
			#pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON//开启LIGHTMAP_OFF LIGHTMAP_ON这两个宏。

			struct vertOut {
				float4 pos:SV_POSITION;
				float3 litDir:TEXCOORD0;
				float3 worldN:TEXCOORD1;
				float2 atten:TEXCOORD2;
			};
			
			vertOut vert(appdata_full v) {
				float4 worldN = mul(float3x3(_Object2World), SCALE_NORMAL);
				
				vertOut o;
				o.pos = UnityObjectToClipPos(v.vertex);
				o.litDir = WorldSpaceLightDir(v.vertex);
				float dist = length(o.litDir);
				o.atten = 1 / (1+dist*dist*_WorldSpaceLightPos0.w);
				o.worldN = worldN;
				return o;
			}
			
			float4 frag(vertOut i) :COLOR{
				float4 c = max(0, dot(i.worldN, normalize(i.litDir))) * _LightColor0 * i.atten;
				return c;
			}
			ENDCG
		}           
    }
    FallBack "Diffuse"
}
发布了41 篇原创文章 · 获赞 4 · 访问量 3892

猜你喜欢

转载自blog.csdn.net/weixin_42487874/article/details/103264176
今日推荐