- Home /
In a shader designed for Forward rendering, unity_Lightmap seems to be correct in the base pass and incorrect in the add pass. Am I doing something wrong or is this a bug?
Hey folks! I'm running into a strange problem. Basically, when I try to access the baked lightmap from the Forward Base pass of my shader, it seems fine, but when I do exactly the same thing from the Add pass, there are some errors.
Here's the minimal possible version of the shader that exhibits the problem. Basically all I'm doing here is returning the lightmap in each pass -- no attenuation or realtime shadows or anything else.
Shader "Lightmap Issue Demo"
{
Properties {}
SubShader
{
Pass {
Tags { "RenderType"="Opaque" "LightMode"="ForwardBase" }
LOD 100
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fwdbase
#include "UnityCG.cginc"
#include "AutoLight.cginc"
struct appdata {
fixed4 pos : POSITION;
fixed2 texcoord1 : TEXCOORD1;
};
struct v2f {
fixed4 pos : SV_POSITION;
fixed2 light_uv : TEXCOORD0;
LIGHTING_COORDS(1,2)
};
v2f vert (appdata v) {
v2f o;
o.pos = UnityObjectToClipPos(v.pos);
o.light_uv = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
TRANSFER_VERTEX_TO_FRAGMENT(o);
return o;
}
fixed4 frag (v2f i) : SV_Target {
return 0;
fixed4 baked_light = saturate(fixed4(DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.light_uv)),1));
return baked_light;
}
ENDCG
}
Pass {
Tags { "LightMode" = "ForwardAdd" }
LOD 100
Blend One One
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fwdadd_fullshadows
#include "UnityCG.cginc"
#include "AutoLight.cginc"
struct appdata {
fixed4 vertex : POSITION;
fixed2 texcoord1 : TEXCOORD1;
};
struct v2f {
fixed4 vertex : SV_POSITION;
fixed2 light_uv : TEXCOORD0;
LIGHTING_COORDS(1,2)
};
v2f vert(appdata v) {
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.light_uv = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
TRANSFER_VERTEX_TO_FRAGMENT(o);
return o;
}
float4 frag(v2f i) : COLOR {
//return 0;
fixed4 baked_light = saturate(fixed4(DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.light_uv)),1));
return baked_light;
}
ENDCG
}
}
Fallback "VertexLit"
}
This is what it looks like when I disable the add pass and view only the forward pass (I know this lightmap looks a little sloppy but it's correct):
And with only the add pass (and one nearby point light):
Interestingly, it's only wrong for specific submeshes of specific objects. This seems to be completely random. There doesn't seem to be anything in common between the submeshes that don't work.
Any ideas? Is it just not possible to reliably access the lightmap from an add pass? I know this is an unusual thing to do but it's necessary for the effect I'm trying to achieve.
Thanks a bunch!