Need help with Lightmapping and Surface shader
== This is a cross-post from Unity3D Reddit as well as official forum - you can answer anywhere==
Hi,
I have been busting my head over this problem for quite some time now, and I'm at my wits end. I have contradicting and incomplete documentation on the matter.
The TL;DR version are these 3 images with respective Imgur comments: http://imgur.com/a/k2W8B
Onwards to the problem: I have some prefabs (trees in this case) in my scene. They are static, and the only directional light in the scene is Baked type. There are no realtime shadow casters. This gives me the following result:
Incorrect Baked Lighting: http://imgur.com/tgWLM9t
As you can see it's all noisy and splotchy all over the place. This is because the quads that make up the canopy are arbitrarily oriented (a.k.a. all over the damn place), and they are shaded according to their angle towards the light.
Bear with me as I attempt to explain what I'm trying to achieve. I have switched my scene from baked, to all-realtime lighting and shadows. This is the same problem as displayed in realtime conditions:
Realtime reproduction of the problem: http://imgur.com/kCbGcqE
Now, using the realtime lighting as example, THIS is what I'm trying to achieve:
Correctly shaded trees: http://imgur.com/5U06kn3
This fix was very simple, as within my surface shader I needed only add this in my vertex function:
v.normal = _WorldSpaceLightPos0.xyz;
This simply flips all the normals towards the directional light source. How can I achieve this with Lightmapped lighting? I am aiming for very low hardware requirements of the game and I'd prefer not to use realtime shadow casters if at all possible.
The shader which produces correct Realtime result can be found on this Pastebin link, however it does not work with lightmapping at all as it shows all black surface - apparently, lightmapping and custom Surface shaders do not play well.
The shader which produces incorrect lightmapped and realtime result (first two images) can be found on this Pastebin link - the only difference is the definition of a custom Lighting Model code as so:
Code (CSharp):
#pragma surface surf LambertZ ... and then:
inline half4 LightingLambertZ(SurfaceOutput s, half3 lightDir, half atten)
{
half4 c;
/* half NdotL = dot(s.Normal, lightDir);
c.rgb = s.Albedo * _LightColor0.rgb * (atten * NdotL);*/
c.rgb = s.Albedo * _LightColor0.rgb * (atten);
c.a = s.Alpha;
return c;
return half4(1,1,1,1);
}
This bit does change the way the shader interacts with realtime lights, but it has absolutely no effect on lightmapping whatsoever.
Now, if you're reading this far, thank you so much, and to save you time, let me finish off by listing all the things I so far have tried and which did not help me:
Making a custom META pass to change the way Lightmapper receives data, via this example code, because this is literally not documented
Trying to override any of the provided Lighting methods via documentation which is severly lacking. An example of overriding the actual GI method which is used for lightmapping here, very last example does not appear to actually do anything. The compiler will complain about function definitions, but once compiled it does not do anything - I've tried simply outputting gibberish on color and light channels, or moving vertices to 0,0,0 - it does not make any change between Lightmap bakes whatsoever.
Trying to affect Normals in vertex function, surface functions or overriden Lighting methods - they reliably work on realtime lights but there's no change at all in baked lighting - is the fact I'm using "surface shader" approach of any relevance? Should I rework it into standard vertex/fragment shader?
So there we go. Sorry for long post. Any smallest hint of help will be greatly appreciated because, like I said, I'm going crazy over this completely mysterious part of Unity's rendering pipeline.
P.S. I just realised that identical scene and lighting setup is significantly brighter in realtime lighting conditions, than baked lighting. Throw that into the overflowing bucket of sighs.