This is the fifth part of a tutorial series about creating a custom scriptable render pipeline. It makes it possible to bake static lighting into maps and probes.

This tutorial is made with Unity 2019.2.18f1.

Baking Static Light

Up to this point we've calculated all lighting while rendering, but this isn't the only option. Lighting can also be calculated ahead of time and stored in light maps and probes. There are two main reasons why this is done: to reduce the amount of realtime calculations and to add indirect lighting that cannot be calculated at runtime. The latter is part of what's collectively known as global illumination: light that's not coming from light sources directly, but indirectly via reflection, from the environment, or from emissive surfaces.

The downside of baked lighting is that it is static so cannot change at runtime. It also needs to be stored, which increases both build size and memory usage.

What about realtime global illumination? Unity uses the Enlighten system for realtime global illumination, but this has been deprecated so we won't use it. Besides that reflection probes can be rendered at runtime to create specular environment reflections, but we won't cover them in this tutorial.

Scene Lighting Settings Global Illumination is configured per scene, via the Scene tab of the Lighting window. Baked lighting is enabled via the Baked Global Illumination toggle under Mixed Lighting. There's also a Lighting Mode option, which we'll set the Baked Indirect, which means that we bake all static indirect lighting. If your project was created in Unity 2019.2 or earlier then you'll also see an option to enable realtime lighting, which should be disabled. If your project was created in Unity 2019.3 or later then that option won't be shown. Baked indirect lighting only. Further down is a Lightmapping Settings section that can be used to control the lightmapping process, which is done by the Unity editor. I'll use the default settings except that LightMap Resolution is reduced to 20, Compress Lightmaps is disabled, and Directional Mode is set to Non-Directional. I also use the Progressive CPU lightmapper. Lightmapping settings. What does the Directional mode do? It also bakes directionality data, which makes it possible to have normal maps affect incoming baked light. As we don't support normal mapping at this point there's no reason to enable it.

Static Objects To demonstrate baked lighting I created a scene with a green plane as the ground, a few boxes and spheres, and a structure in the center that only has one open side so its interior is fully shadowed. Scene with dark interior. Same scene without ceiling. The scene has a single directional light with its Mode set to Mixed. This tells Unity that it should bake the indirect lighting for this light. Besides that the light still works like a regular realtime light. Mixed-mode light. I also include the ground plane and all cubes—including those that form the structure—in the baking process. They'll be the objects from which the light bounces off, thus becoming indirect. This is done by enabling the Contribute Global Illumination toggle of their MeshRenderer components. Enabling this also automatically switches their Receive Global Illumination mode to Lightmaps, which means that the indirect light that reaches their surfaces get baked into the light map. You can also enable this mode by enabling Contribute GI from the object's Static dropdown list, or by making it fully static. Contribute global illumination enabled. Once enabled, the scene's lighting will be baked again, assuming that Auto Generate is enabled in the Lighting window, otherwise you'll have to press the Generate Lighting button. Lightmapping settings also shows up in the MeshRenderer components, including a view of the light map that contains the object. Map of baked received indirect light. Shouldn't there be a lot of green indirect light? Yes. We'll get to that later. The spheres don't show up in the light map because they don't contribute to global illumination and are thus considered dynamic. They'll have to depend on light probes, which we'll cover later. Static objects could also be excluded from the map by switching their Receive Global Illumination mode back to Light Probes. They'll still affect the baked results, but won't take up space in the light map.