Lightmaps (Unity Lightmapper)

This section will go over lightmapping using the Unity Lightmapper.

Lightmapping in Unity Tutorial

What are Lightmaps?

Lightmapping is extremely important if you want a Space to run well, on as many systems as possible, and have good lighting. Lightmapping is a method of creating high quality lighting in a digital space by 'baking' the light information into a second layer of UVMaps (texture placement information) on the objects in the scene.

The kind of lighting information that can be used with lightmapping is light and shadow information, but even emissive information from materials can be utilised.

Due to the nature of this low cost, high quality method, objects are not able to be dynamic (animated) after that information has been baked. But as high quality real time lighting is too expensive in web based environments, lightmapping is basically the best way to have great lighting in your environment.

Overview

  • Make sure the imported models have Generate Lightmap UVs (if you want Unity to take care of creating the second UVMap information for you)

  • Set any object you want to bake into the lightmap to Static

  • Set any lights you want to bake onto the objects to Baked

  • Set up the Lightmap settings and generate the lightmap

Setting up your objects

1. Generate Lightmap UVs

For lightmaps to bake the information your objects need a second layer of UVs to bake onto. You can do this yourself in an external application like Blender etc. But if you are putting the scene together in Unity it is somewhat easier and faster to let Unity do this for you at the expense of using the texture space as less efficiently.

1. If you want Unity to unwrap your objects for lightmapping, select your model file in the Project window and then go to Inspector / model tab > Generate Lightmap UVs. Then select Apply. A window may popup asking if you want to apply the change to the children objects as well.

NB : If you bake and your lightmaps look terrible (overlapped UVs, large dark blocks for no reason, etc.) then not having correct second UVs is probably the reason.

2. Set Static Objects

  1. Select objects to be lightmapped in the scene or Hierarchy and set it to Static in the top right of the objects Inspector. If this is not set, the lightmapping will not see it. Static actually sets a number of things, the main one is 'Contribute GI'. This can be seen if you use the arrow to the right of 'Static'. You will most likely want 'Reflection Probe Static' as well so that it will be baked into any reflection probes.

Lighting Setup

1. Adjust Light Settings

  1. Set any lights to be used for lightmapping to Baked, not realtime, using Light / Inspector > Mode. If this is not set the lightmapping will not use this light. If you use Mixed the light will be both a real time and a baked light. As noted it is not recommended to use more than one or two realtime lights unless you know your space runs well on low end systems.

2. As an example moving forward, this scene has one real time directional light (the one that comes with the template), an area light (which has to be baked), a spotlight, and a point light. All lights (apart from the realtime light) will be set to Baked. One object with emissive materials (see the materials tutorial for details) will also be added.

2. Adjust Lightmap settings

  1. Open the Lighting Tab from Window / Rendering / Lighting to access the lightmapping Settings if it hasn’t been already. Drag and drop it according to your preferred layout location.

2. Turn down most of the default settings in the Lightmapping tab in order to speed up renders and make sure to set the Directional Mode is set to Non-Directional.

3. You can use Progressive GPU (Preview) (in the Aqua box above) for faster, but less accurate lightmaps, or Progressive CPU for more accurate but slower lightmaps. It is recommended to use Progressive CPU for final bakes.

4. Note that turning off Ambient Occlusion is optional as this does add a lot to the result at the expense of baking time. Adjust these settings depending on your CPU/GPU.

Baking the Lightmaps

1. Generate the Lightmaps

  1. Once the objects are set to static, the lights are set to Baked, and the settings are set to something more reasonable, press the Generate Lightmaps at the bottom of the lighting Tab to create your lightmaps. Depending on your scene complexity, lightmap settings and computer this can take anywhere from 10 seconds to an hour (or more).

If you are using the recommended settings above the quality will be low but bake faster. This stage is more like a guide for how the lighting will appear, and you can adjust the settings as needed. Spend a little time with the lightmapping settings and get a feel for the differences each one does and how long that affects the baking time.

2. Once baked, you can check the resulting Lightmap textures in the Baked Lightmaps section in the Lighting Tab. You can use this to visually see the result of the unwrapping process, and can sometimes guide you with trouble shooting (overlapping second UVs or sections of your space that don’t require baking (polygons that are never seen such as the other side of a ceiling or floor). Select an object in the space to highlight in yellow so you can see where it is on the Lightmap.

Optimising your Lightmaps

1. Refine the Results

  1. Modify Lighting / Scene / Lightmapping Settings > Lightmap resolution to adjust how much space the polygons take up on the lightmap. Increasing this will (potentially) increase the amount of Lightmaps you have (and therefore resulting file size) when the previous lightmap is ‘full’, but it increases the amount of space the polygons take up on the lightmap texture (eg. better quality). Finding the sweet spot between quality and number of lightmaps is half the fun.

These renders are done on a Ryzen 5950X, 32Gb RAM, 2070RTX.

  1. Adjust the Samples options to increase the quality of the lightmap at the expense of time to bake. So once you have a good feeling about your lightmap, turn these up before your final build. That said, the difference in results get harder and harder to see at the higher levels.

2. Setting Lighting / Scene > Max lightmap size to 2048 is a good starting point, you can of course adjust this depending on the needs of your space. It is not recommended to go to 4096 or above however as that increases the texture size significantly.

3. Isolate the results of your baked lights, by turning down/off your real time light/s and the environment/ambient lighting under Lighting / Environment / Environment Lighting > Source and/or Intensity multiplier. Your scene will look different when you turn these back on, but it’s a good way to understand the effect of each light…so bounce between the two (or three) states.

2. Optimise Results

  1. Artifacts should not have Static applied as they will affect, and be affected by, the environment and they might not be there forever (as artifacts can be modified or turned off). So a safe bet is just to turn all Artifacts’ Static to off. Portals are fine however as they cannot be edited in the space.

2. Make sure that the Lightmap resolution is set to the level that fills up the last lightmap. Check what your lightmaps look like in the Lighting Tab / Baked Lightmaps section. If there is any blank space on the final map, then turn up the Lightmap resolution (even by a decimal level) until there isn’t. Make sure that it doesn’t spill over into a new lightmap, and have most of it blank. Lightmaps will take up considerable file size, so keep this in mind when you are choosing how many you create. 2-3 at 2048 should be plenty. However this could be more or less depending on the complexity of your space.

This lightmap has nearly half the space wasted.

3. It is possible to make an object take more texture space in the lightmap. Select the static object and increase/decrease the Mesh Renderer / Lightmapping > Scale under Lightmapping. Putting that higher will mean it takes more space in the lightmap, less and it will be smaller.

Troubleshooting

  1. Unity Lightmapper sometimes has issues with light leaking between separate objects (eg. a box for a wall and a box for the floor). You can try to mitigate this by :

    Create smaller polygons. A huge single polygon floor will probably look bad as the area will be too ‘big’ for precision baking, in this case you could make a gridded plane instead.

    Make the objects that overlap into one object (in blender etc) and join them at the transition. Light can’t leak if there's nowhere to leak.

  2. As noted, if many/all of your objects look like the lightmapping is messed up, there is a reasonable chance those objects have not been set to Generate Lightmap UVs correctly and the lightmaps are overlapping. Check out Generating Lightmap UVs at the beginning of this tutorial for more information.

Last updated