This is part 16 of a tutorial series about rendering. Last time, we rendered our own deferred lights. In this part, we'll move on to lightmapping.

This tutorial was made with Unity 5.6.0.

Lightmapping

Performing lighting calculations is expensive. Deferred rendering allows us to use many lights, but shadows are still a limiting factor. If our scene is dynamic, then we cannot avoid performing these calculations. But if both the lights and the geometry are unchanging, then we could calculate the lighting once and reuse it. This allows use to put many lights in our scenes, without having to render them at run time. It also makes it possible to use area lights, which are not available as realtime lights.

How much of the lighting is pre-computed can vary. In this tutorial, we'll go all the way and bake everything into lightmaps. So there won't be any dynamic lighting at all.

To try out lightmapping, I've created a simple test scene that has a simple structure that provides shadow, plus a few spheres placed around and inside it. Everything has the default Unity material.

Lightmapping test scene.

Baked Lights To begin lightmapping, change the Mode of the only light object to Baked instead of Realtime. Baked main directional light. After turning the main directional light into a baked light, it will no longer be included in dynamic lighting. From the point of view of dynamic objects, the light doesn't exist. The only thing that remains is the environmental lighting, which is still based on the main light. No more direct lighting. To actually enable lightmapping, turn on Baked Global Illumination in the Mixed Lighting section of the lighting window. Then set the Lighting Mode to Baked Indirect. Despite the name, this also includes direct lighting. It's just typically used to add indirect light to a scene. Also, make sure that Realtime Global Illumination is disabled, because we're not supporting that yet. Baked indirect mode.

Static Geometry The objects of the scene are all supposed to be fixed. They should never be moved. To communicate this to Unity, mark them as static. You can do that by enabling the Static toggle at the top right of the inspector window. Do lights also have to be marked as static? No, that is not required. The lights only have to be set to the appropriate mode. There are various subsystems that care about whether things are static or not. Static also has a dropdown menu that you can use to fine-tune which systems treat the object as being static. Right now we only care about lightmapping, but it's simplest to just make everything completely static. Static object. Whether an object is static for lightmapping purposes can also be seen and edited via the inspector of its mesh renderer. Lightmap-static object. Now that all the objects are static, they will be included in the lightmapping process. Scene with baked lighting. Note that the lightmapped result is less bright than when using realtime lighting. That's because the specular lighting is missing, it's only diffuse lighting. Specular lighting depends on the view angle, so it depends on the camera. Typically, the camera is mobile, so it cannot be included in lightmapping. This limitation means that lightmapping can work for subtle lights and dull surfaces, but not for strong direct lights or shiny surfaces. If you want specular lighting, you'll have to use realtime lights. So you often end up with a mix of baked and realtime lights. Why don't I get baked light? To make sure that the lightmaps are actually generated and updated when required, enable Auto Generate at the bottom of the lighting window. Otherwise, you have to manually generate new lightmaps. Automatically generate lightmaps.

Lightmapping Settings The lighting window contains a section dedicated to lightmapping settings. Here you can make a trade-off between quality, size, and baking time. You can also switch between the Enlighten lightmapping engine and the Progressive lightmapper. The latter incrementally generates lightmaps, giving priority to what's visible in the scene view, which is convenient while editing. I used Enlighten for this tutorial. Default lightmap settings. Before you do anything else, set the Directional Mode to Non-Direction. We'll deal with the other mode later. Non-directional lightmaps. The baked lighting is stored in textures. You can view them by switching the lighting window from Scene to Global Maps mode. With the default settings, my test scene easily fits in a single 1024×1024 map. The lightmap. Unity's default objects all have UV coordinates configured for lightmapping. For imported meshes, you either provide your own coordinates, or let Unity generate them for you. After baking, the texture unwraps can been seen in the lightmap. How much space they require depends on the object's size in the scene and the lightmap resolution settings. If they don't fit in a single map, Unity will create additional maps. Lightmap resolution makes a big difference. Which settings are best varies per project. You have to tweak the settings until you arrive at a good trade-off. Note that the visual quality also greatly depends on the quality of the texture unwrap used for lightmapping. The presence of absence of texture seams can produce obvious artifacts. Unity's default sphere is a good example of this. It is not suited for lightmapping.

Indirect Light While baking light means that we lose specular lighting, we gain indirect lighting. This is light that bounces off multiple surfaces before it reaches our eyes. Because light bounces around corners, areas that would otherwise be shadowed still get illuminated. We cannot compute this at realtime, but we can include bounced light while baking. To clearly see the difference between realtime and baked lighting, set the intensity of the environmental lighting to zero. This eliminates the skybox, so all light comes from the directional light only. No environmental lighting, realtime vs. lightmapped. Each time that a photon bounces, it loses some of its energy, getting colored by the material it interacted with. Unity takes this into consideration when baking indirect light. As a result, objects get colored based on what's nearby. Green floor, realtime vs. lightmapped. Emissive surfaces also affect baked light. They become indirect light sources. Emissive floor, realtime vs. lightmapped. A special setting for indirect light is Ambient Occlusion. This refers to the shadowing of indirect light that happens in corners and creases. It is an artificial boost, which can enhance the feeling of depth. Using ambient occlusion. The ambient occlusion effect is solely based on how hidden a surface is. It doesn't consider where the light is actually coming from. This doesn't always make sense, for example in combination with emissive surfaces. Obviously wrong ambient occlusion.