CDF
January 14, 2017, 12:55am
1
Hi, not sure what the problem is here, but when I try to add light mapping to my shader the output is very dark.
I’m not using directional lightmapping. And using Unity 5.5
Here’s a really simple shader that just samples the lightmap color * some user color
Shader "Custom/Simple Lightmap"
{
Properties
{
_Color("Color", Color) = (1, 1, 1, 1)
_Strength("Strength", Float) = 1
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f
{
float2 lightuv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
fixed4 _Color;
float _Strength;
v2f vert (appdata_full v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.lightuv = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = _Color * _Strength; //Lightmaps identical when _Strength = 4???
col.rgb *= DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.lightuv));
return col;
}
ENDCG
}
}
}
Here’s the comparison to the Mobile Unlit (Supports Lightmap) shader:
Strength = 4:
Strength = 1:
Why do I need to multiply the output by 4 to get the correct result?
CDF
January 14, 2017, 2:17am
2
Ok, I compiled a surface shader and dug through the code. Looks like you also need to apply Color from the Directional Light as well as the baked light.
Seems to be working now:
Shader "Custom/Simple Lightmap"
{
Properties
{
_Color("Color", Color) = (1, 1, 1, 1)
}
SubShader
{
Tags { "RenderType"="Opaque" "LightMode" = "ForwardBase" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
struct v2f
{
float2 uv : TEXCOORD0;
float2 lightuv : TEXCOORD1;
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
fixed4 light : COLOR1;
};
fixed4 _Color;
v2f vert (appdata_full v)
{
v2f o;
o.uv = v.texcoord;
o.vertex = UnityObjectToClipPos(v.vertex);
o.lightuv = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
o.color = v.color;
half3 worldNormal = UnityObjectToWorldNormal(v.normal);
half nl = max(0, dot(worldNormal, _WorldSpaceLightPos0.xyz));
o.light = nl * _LightColor0;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 Albedo = _Color;
fixed4 c = Albedo * i.light;
half3 indirectDiffuse = DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.lightuv));
c.rgb += Albedo * indirectDiffuse;
return c;
}
ENDCG
}
}
}
** Would love to know how the Unlit (Supports Lightmap) shader works without adding light color??
I have a sample without light color and it has detail texture
Shader "Unlit_VertexColor_Lightmap"
{
Properties
{
_MainTex ("Base", 2D) = "white" {}
_DetailTex ("Detail", 2D) = "white" {}
_DetailPower("Detail Power ",Float) = 2
}
SubShader
{
Tags { "RenderType"="Opaque" }
Pass
{
CGPROGRAM
#include "UnityCG.cginc"
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile LIGHTMAP_ON LIGHTMAP_OFF
struct v2f
{
half4 color : COLOR;
fixed4 pos : SV_POSITION;
fixed2 uv[3] : TEXCOORD0;
};
sampler2D _MainTex;
fixed4 _MainTex_ST;
sampler2D _DetailTex;
half _DetailPower;
#ifdef LIGHTMAP_ON
fixed4 unity_LightmapST;
sampler2D unity_Lightmap;
#endif
v2f vert(appdata_full v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv[0] = TRANSFORM_TEX(v.texcoord, _MainTex);
o.uv[2] = v.texcoord.xy * _MainTex_ST.xy*7 + _MainTex_ST.zw;
#ifdef LIGHTMAP_ON
o.uv[1] = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
#endif
o.color = v.color;
return o;
}
fixed4 frag(v2f i) : COLOR
{
fixed4 c = tex2D(_MainTex, i.uv[0]) * i.color;
c*=tex2D(_DetailTex, i.uv[2])*_DetailPower;
#ifdef LIGHTMAP_ON
c.rgb *= DecodeLightmap(tex2D(unity_Lightmap, i.uv[1]));
#endif
return c;
}
ENDCG
}
}
}