Need help with Lightmapping effect

UPDATE: I Have posted a reddit thread with better worded question and some examples here: Reddit - Dive into anything - this post has been edited to copy the reddit post!


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: Imgur: The magic of the Internet

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:

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:

Now, using the realtime lighting as example, THIS is what I’m trying to achieve:

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:

      #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

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.

Bump :confused: Still haven’t figured it out.

I can provide the example scene and current shader that needs modifying if needed.

Did you define custom normals for the leaves? If not, that seems like the easiest way of accomplishing what you desire. In blender, this can be done using the blend4web plugin (you only need the addon near the bottom of the page, not the full package which builds webgl applications).

1 Like

Hi, while I appreciate this tool I haven’t seen before,

Normals of the leaves at design-time isn’t important - they are already “correct”. This Alder tree is from the Standard Assets package of Unity. AO baked in and all.

What I need is for the leaves’ normals to always be treated as pointing towards the sun. The goal is to have uniformly lit leaves, BUT still have them cast and receive baked shadows. This, coupled with a vertex shader which imitates swaying in the wind, should yield a very good looking trees with minimal performance impact - I aim for very low hardware requirements.

I believe I have found a clue as to how to proceed. The documentation lists the following overrideable functions within Lightning Model code:

            inline fixed4 LightingTonemapped_SingleLightmap (SurfaceOutput s, fixed4 color)

            inline fixed4 LightingTonemapped_DualLightmap (SurfaceOutput s, fixed4 totalColor, fixed4 indirectOnlyColor, half indirectFade)

            inline fixed4 LightingTonemapped_StandardLightmap (SurfaceOutput s, fixed4 color, fixed4 scale, bool surfFuncWritesNormal)

However I’m confused - there appears to be two diferent copies of the documentation/manual, each telling me different things?

https://docs.unity3d.com/455/Documentation/Manual/SL-SurfaceShaderLightingExamples.html

Which is correct?

How do I get uniformly lit / unshaded Lightmapped object? How to modify normals during lightmap bake? OR, How to apply lightmaps myself so the object is uniformly lit regardless of surface normal?

This shouldn’t be so hard. Anybody?

The opening post has been re-worked to better explain the problem, which I hope may yield some responses.

I apologize for triple-posting and bumping of the thread in advance :frowning: Somebody please help!