首先打开Lab_1下的场景,如下图所示的是场景在烘焙(Bake)之前的样子,场景中的物体都是静态的(只有静态的物体才会被Unity输出到Beast中进行烘焙。这里也放了几个非静态的物体进行对比,其中有一个非静态的自发光物体。
8.1.2烘焙的前提:静态物体Shader "Self-Illumin/Lighting/LightMapping/Lab_1/EmissionLM" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_Color ("Illumi Color", Color) = (1,1,1,1)
_EmissionLM ("Emission (Lightmapper)", Float) = 1
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
pass{
Tags{ "LightMode"="Vertex"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
uniform float4 _Color;
float4 _Illumi;
struct vertOut{
float4 pos:SV_POSITION;
float4 color:COLOR;
};
vertOut vert(appdata_base v)
{
float3 c=ShadeVertexLights(v.vertex,v.normal);
vertOut o;
o.pos=mul(UNITY_MATRIX_MVP,v.vertex);
o.color=_Color*float4(c,1);
return o;
}
float4 frag(vertOut i):COLOR
{
return i.color+_Color;
}
ENDCG
}//end pass
}
}
下面看黄色光源,其Lightmapping Mode被设置为RealTime Only,所以其未被烘焙。当烘焙结束之后,不论是黄色光源还是其他光源,不再影响被烘焙过的静态物体,但是非静态物体继续受灯光的光照影响。我们可以通过GUI控制移动光源1(白色光源)和光源2(绿色光源)的位置,改变光源1的颜色观察这一现象。移动绿色光源后的效果,注意它只影响了非静态的球体。
烘培后:
8.1.5应用光照贴图到VertexLit渲染路径下的材质中Shader "Tut/Lighting/LightMapping/Lab_1/myVertexLit" {
Properties {
_MainTex("MainTexture",2d)="white"{}
_Color ("Base Color", Color) =(1,1,1,1)
}
SubShader {
pass{
Tags{ "LightMode"="Vertex"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
uniform float4 _Color;
struct vertOut{
float4 pos:SV_POSITION;
float4 color:COLOR;
};
vertOut vert(appdata_base v)
{
float3 c=ShadeVertexLights(v.vertex,v.normal);
vertOut o;
o.pos=mul(UNITY_MATRIX_MVP,v.vertex);
o.color=_Color*float4(c,1);
return o;
}
float4 frag(vertOut i):COLOR
{
return i.color;
}
ENDCG
}//end pass
}
}
这只有一个LightMode = Vertex的Pass的Shader,我们发现应用了该材质的静态Sphere影响
了烘焙的结果,比较明显的是其投递的阴影。如果你眼力够好,还会发现它也影响了GI,当然,
重要的是当烘焙结束之后,它就消失了,任何光源都无法使其现形。与其相对照的是应用了
// Upgrade NOTE: commented out 'float4 unity_LightmapST', a built-in variable
// Upgrade NOTE: commented out 'sampler2D unity_Lightmap', a built-in variable
// Upgrade NOTE: replaced tex2D unity_Lightmap with UNITY_SAMPLE_TEX2D
Shader "Tut/Lighting/LightMapping/Lab_1/VertexLMRGBM" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_Color ("Base Color", Color) =(1,1,1,1)
}
SubShader {
pass{
Tags{ "LightMode"="Vertex"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
uniform float4 _Color;
struct vertOut{
float4 pos:SV_POSITION;
float4 color:COLOR;
};
vertOut vert(appdata_base v)
{
float3 c=ShadeVertexLights(v.vertex,v.normal);
vertOut o;
o.pos=mul(UNITY_MATRIX_MVP,v.vertex);
o.color=_Color*float4(c,1);
return o;
}
float4 frag(vertOut i):COLOR
{
return i.color;
}
ENDCG
}//end pass
Pass {
Tags{"LightMode"="VertexLMRGBM"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
// sampler2D unity_Lightmap;
// float4 unity_LightmapST;
struct appdata {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
float2 texcoord1: TEXCOORD1;
};
struct v2f {
float4 pos : SV_POSITION;
float2 txuv : TEXCOORD0;
float2 lmuv : TEXCOORD1;
};
v2f vert (appdata v) {
v2f o;
o.pos = mul( UNITY_MATRIX_MVP, v.vertex );
o.txuv = TRANSFORM_TEX(v.texcoord.xy,_MainTex);
o.lmuv = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
return o;
}
half4 frag( v2f i ) : COLOR {
half4 col = tex2D(_MainTex, i.txuv.xy);
half4 lm = UNITY_SAMPLE_TEX2D(unity_Lightmap, i.lmuv.xy);
col.rgb = col.rgb * DecodeLightmap(lm);
return col;
}
ENDCG
}
}
}
Shader "Tut/Lighting/LightMapping/Lab_1/VertexLMRGBM_1" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_Color ("Base Color", Color) =(1,1,1,1)
}
SubShader {
Pass {
Tags{"LightMode"="VertexLMRGBM"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
uniform float4 _Color;
sampler2D _MainTex;
float4 _MainTex_ST;
// sampler2D unity_Lightmap;
// float4 unity_LightmapST;
struct v2f {
float4 pos : SV_POSITION;
float2 txuv : TEXCOORD0;
float2 lmuv : TEXCOORD1;
float4 color:COLOR;
};
v2f vert (appdata_full v) {
float3 c=ShadeVertexLights(v.vertex,v.normal);
v2f o;
o.pos = mul( UNITY_MATRIX_MVP, v.vertex );
o.txuv = TRANSFORM_TEX(v.texcoord.xy,_MainTex);
o.lmuv = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
o.color=_Color*float4(c,1);
return o;
}
half4 frag( v2f i ) : COLOR {
half4 col = tex2D(_MainTex, i.txuv.xy);
half4 lm = UNITY_SAMPLE_TEX2D(unity_Lightmap, i.lmuv.xy);
col.rgb = col.rgb * DecodeLightmap(lm);
return col+i.color;
}
ENDCG
}
}
}