Thursday, March 26, 2015

Porting to Unity 5: lightmapping

(Post updated 21-2-2016! Scroll down for the updated info)
We decided to port the game to Unity 5 already because of the promised benefits of the new physics engine, nav mesh improvements among other things. It was quite a big task. We spent a lot of time planning the transition and researching the differences. Here's how it all went down...

That's it for this post, and thanks for reading!

Naw, but seriously here's some more details about...


  Anyone who has experience in switching from 4.x series to 5 series or has done any research on the matter, knows that all the work put into lightmapping is going to the bin because of the new lighting engine. Beast lightmapping has been removed completely and replaced with Enlighten engine. This is problematic in the middle of a project because you basically have to learn lightmapping all over again.

So how to get identical results and settings? What I very quickly realized was that I just had to forget everything I knew and re-learn this new engine and just try to match the lighting setups by eye. It took me couple of days to figure out settings for my purposes and get similar results as before.

First you might want to create your own lightmapping parameters. I'm no expert on the matter, but I got some major improvements compared to using presets so here's the values that I changed to make the baking a bit faster, less blurry and less glitchy:

Irradiance Budget - I bumped this up from the default 128.

Backface and Tolerance - I turned these all the way down to eliminate dark spot glitches on some assets

Blur Radius - This is 2 by default. I switched it to 1 and got a lot sharper shadows.

Direct Light Quality - I turned this way up from the default of 64 and got a lot more accurate results

AO Anti-aliasing samples - Lowered from 16 to 8. Didn't notice any sacrifice in quality in my scenes. Higher anti-aliasing samples is probably more noticeable on higher resolution lightmaps, but this works for my purposes.

Now I had to match the light, ambient and GI settings to your project. Fortunately the lighting setup in Island Delta is very simple, consisting of only ambient light and one directional light (+ small point & spot lights).

Baked Resolution - This is set insanely high by default (40 texels per unit). I turn this all the way down to 2-5 for test-bakes, and 14-15 for final bakes. The effect of this value is quite similar to 4.6 series since it's basically the resolution of the lightmap.

Indirect Resolution - This is 2 by default. I switched it to 1 and didn't notice much difference although I'd imagine this being somewhat important when more accurate results are needed. In any case this parameter has an impact on baking time.

Ambient Occlusion - AO intensity was capped to 1 before Unity 5 patch releases. I don't need higher values, but some folks might find it useful.

Max Distance - The default of 1 was way too high for this project.

Final Gather - I could not get this one to work. It' s supposed to give cleaner results, but it just messed things up for me. It also adds to the baking time so I'm quite happy not having to use it. I'll get back to this later if I get better results though.


So now I have my own lightmap parameters and Lighting settings which I use these for all my different lighting setups. I only change ambient color and directional light settings based on the lighting condition that I want.

Of course those settings and parameters don't get me anywhere without tweaking the light intensities and colors to match the previous Beast results. I did this by just eyeballing the results. There was lots of comparing and tweaking to balance the ambient light and directional light and in the end I managed to get the look I wanted.

The lights have Bounce Intensity value which plays an important role in the baking process. I used Bounce Intensity of 1 in this bright scene:

While the directional light in this dark environment has a Bounce Intensity of 0:

The process was not all rainbows and sunshine though. There are problems; some assets get weird artifacts such as dark spots and light is leaking and wrapping in weird ways for some assets (see the update further down this post). This can be controlled (somewhat) with the models import options. If you're generating lightmap UV's within Unity, here's some settings you should try to eliminate strange artifacts:

Hard Angle - it is set to 88 by default, try to double it. If it doesn't work, try a much lower value. Tweak, test, tweak again, succeed.

Angle Error - For the most problematic assets, I turned this down to 1.

Area Error - Same thing as the angle error.

I also had to double-check my problematic assets for normal and tangent problems. Some of the assets indeed had bad normals which didn't occur with Beast, but I'm still struggling with some assets. I get the most problems with the most simple ones such as these:

Notice the dark areas

UPDATE: some of the weird light leaking was solved by making the offended geometry completely solid, with no open faces nor cracks left where the light could find its way through.
This is something I should've known already, but sometimes one starts to optimize the assets too soon (deleting invisible backfaces for lowering poly & vertex-count for example).

If you really want control over lightmapping, you should unwrap your models manually using the second uv channel and disable Unity's "Generate Lightmap UVs" for the specific object.
I've found this to be the only way to get trickier objects to map correctly.
Use the whole 0-1 uv map scale for the object to get the most available space for it. The dark areas -problem in the picture above can be eliminated with proper uv mapping. The automatic system just doesn't always cut it.

Anyways, the lightmapping tweaking is almost done and only minor glitches remain. I'm somewhat happy with the results. I'll be writing follow-up post when I get the glitches fixed.
There's also going to be at least one other post about Unity 5 specific things later.

That's it for this one, thanks for reading!

-Sami Kuronen