When writing Unity Shader, there are times when you need to write shader that can support Unity’s built-in Lightmap or Light probe. If you’re writing with Surface, you don’t have to worry about that. Unity will compile automatically, but if you’re writing with Vert& Frag writes shader, these need to add their own code to call.
Unity has a built-in lightMap call
To make the unity built-in data and various macro definitions (such as LIGHTMAP_OFF in this article) work, you need to add #pragma:
pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
Prior to Unity5.0, there were two built-in parameters that needed to be declared. With Unity5.0, they were not required:
half4 unity_LightmapST;
sampler2D unity_Lightmap;
Lightmap is a UV2 that USES the model, so next declare uV2 in the vertex input structure:
float2 texcoord1 : TEXCOORD1;
In another vertex structure, define the UV value used to receive UV2:
ifndef LIGHTMAP_OFF
half2 uvLM : TEXCOORD4;
endif
Assign uvLM to the vert function:
ifndef LIGHTMAP_OFF
o.uvLM = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
endif
Then the frag function samples the lightMap map and adds it to the main color:
ifndef LIGHTMAP_OFF
fixed3 lm = DecodeLightmap (UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uvLM.xy));
col.rgb*=lm;
endif
In the above code, DecodeLightmap is used to decode unity’s built-in LightMap. This is because the LightMap baked by unity is a 32-bit HDR map. On the desktop side, the code of the LightMap is RGBM, while on the mobile side, in most cases, the code of the LightMap is double-ldr, so different coding methods should be provided for different platforms. DecodeLightmap is here, it can decode the light map for different platforms
VF version code 01:
Shader “PengLu/Unlit/TextureLM” {
Properties {
_MainTex (” Base (RGB) “, 2D) = “white” {}
}
SubShader {
Tags {” RenderType “=” Opaque “}
LOD 100
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
#pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
#include "UnityCG.cginc"
struct appdata_t {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
float2 texcoord1 : TEXCOORD1;
};
struct v2f {
float4 vertex : SV_POSITION;
half2 texcoord : TEXCOORD0;
#ifndef LIGHTMAP_OFF
half2 uvLM : TEXCOORD1;
#endif
UNITY_FOG_COORDS(1)
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert (appdata_t v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
#ifndef LIGHTMAP_OFF
o.uvLM = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
#endif
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.texcoord);
UNITY_APPLY_FOG(i.fogCoord, col);
UNITY_OPAQUE_ALPHA(col.a);
#ifndef LIGHTMAP_OFF
fixed3 lm = DecodeLightmap (UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uvLM.xy));
col.rgb*=lm;
#endif
return col;
}
ENDCG
}
}
}
A call of Unity’s built-in Light Probes
In shader we call Light Probes using Half3 ShadeSH9(Half4 Normal) defined by Unity. Light Probes lighting USES a simulation called Sphere Harmonic, or SH, so in ShadeSH9 the normal in a world coordinate is needed to determine the Light on the surface of the object.
First we define a parameter SHLighting in the vertex output structure:
fixed3 SHLighting : COLOR;
Then assign it to a vertex function:
float3 worldNormal = mul((float3x3)_Object2World, v.normal); Get normal in world coordinates
o.SHLighting= ShadeSH9(float4(worldNormal,1)) ;
VF version code 02:
Shader “PengLu/Unlit/TextureLM” {
Properties {
_MainTex (” Base (RGB) “, 2D) = “white” {}
_SHLightingScale(” LightProbe influence scale “,float) = 1
}
SubShader {
Tags {” Queue “=” Geometry “” LightMode” = “ForwardBase” “RenderType” = “Opaque”}
LOD 100
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct v2f {
float4 vertex : SV_POSITION;
half2 texcoord : TEXCOORD0;
fixed3 SHLighting : COLOR;
UNITY_FOG_COORDS(1)
};
sampler2D _MainTex;
float4 _MainTex_ST;
float _SHLightingScale;
v2f vert (appdata_base v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
float3 worldNormal = mul((float3x3)_Object2World, v.normal);
o.SHLighting= ShadeSH9(float4(worldNormal,1)) ;
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.texcoord);
col.rgb*=i.SHLighting;
UNITY_APPLY_FOG(i.fogCoord, col);
UNITY_OPAQUE_ALPHA(col.a);
return col*_SHLightingScale;
}
ENDCG
}
}
}