光照贴图生成:
场景如上
他们的
设置为lightmap static
灯光设置:baked
打开window - lighting窗口
这样就生成光照贴图了,此时,关闭灯源
依旧有光照的效果,即使移动物体,影子也在
手册搜索:
unity_lightmap
unity_Lightmap
======================================================
//http://edu.manew.com/course/96/learn#lesson/1724
Shader "Sbin/Texture2"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f
{
float4 pos:POSITION;
float2 uv:TEXCOORD0;
float2 uv2:TEXCOORD1;
};
sampler2D _MainTex;
sampler2D unity_Lightmap;
// float tiling_x;
// float tiling_y;
// float offset_x;
// float offset_y;
//上面四个值形成的float4,unity系统会对_MainTex_ST和unity_LightmapST进行赋值
float4 _MainTex_ST;
float4 unity_LightmapST;
v2f vert (appdata_full v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = v.texcoord.xy;
//o.uv.x = o.uv.x * _MainTex_ST.x + _MainTex_ST.z;
//o.uv.y = o.uv.y * _MainTex_ST.y + _MainTex_ST.w;
//#define TRANSFORM_TEX(tex,name) (tex.xy * name##_ST.xy + name##_ST.zw)
o.uv = TRANSFORM_TEX(o.uv,_MainTex);
//光照贴图
o.uv2 = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
return o;
}
fixed4 frag (v2f i) : COLOR
{
fixed4 col = tex2D(_MainTex, i.uv);
//光照贴图
fixed4 lmtex = tex2D(unity_Lightmap, i.uv2);
half3 lm = DecodeLightmap (lmtex);
col.rgb *= lm;
return col;
}
ENDCG
}
}
}