Lightmap Baking: Principles, Workflow, and Optimization
What is Lightmap Baking
A lightmap is a texture that stores static lighting information of a scene. Baking refers to offline computation of global illumination (including direct light, indirect light, shadows, color bouncing, etc.) and saving the result into a lightmap. At runtime, models sample the lightmap via UV mapping to obtain precomputed lighting effects, eliminating the need for real-time complex lighting calculations, greatly improving performance. Lightmaps are widely used in game development for mobile and low-end devices.
Principles of Lightmaps
The baking process typically uses ray tracing or radiosity algorithms to simulate the propagation of photons in the scene and compute the incident lighting on each surface point. Results can be stored as:
- **Lightmaps**: 2D textures recording diffuse lighting colors.
- **Light Probes**: For indirect lighting of dynamic objects.
- **Reflection Probes**: Recording environment reflection information.
Baking results are bound to static objects, so static objects in the scene must not move at runtime.
Baking Workflow
In Unity and Unreal, lightmap baking typically involves the following steps:
- **Mark Static Objects**: Mark non-moving objects as Static (Unity) or set Mobility to Static (Unreal).
- **Set Up UVs**: Ensure the model has a suitable second UV set (for lightmaps), which must be unique and non-overlapping.
- **Configure Lights**: Set lights to Baked or Mixed mode.
- **Adjust Baking Parameters**: Such as resolution, indirect lighting intensity, ambient light, etc.
- **Start Baking**: The engine offline computes lighting and generates lightmaps.
- **Apply**: At runtime, lightmaps are automatically sampled.
- High runtime performance, no need for real-time global illumination calculations.
- Realistic lighting effects, supporting soft shadows, color bouncing, etc.
- Suitable for mobile and low-end hardware.
- Only for static scenes; dynamic objects cannot use them.
- Baking is time-consuming; scene modifications require re-baking.
- Lightmaps consume memory and storage space.
- Cannot respond to real-time lighting changes.
- **Reduce Lightmap Resolution**: Distant or less important objects can use lower resolution.
- **Use Light Probes**: Provide approximate indirect lighting for dynamic objects.
- **Compress Lightmaps**: Use ASTC, ETC2, etc., to reduce memory.
- **Merge UV Islands**: Optimize UV layout to reduce texture space waste.
- **Avoid Seams**: Ensure enough padding between adjacent UV islands to prevent bleeding.
Advantages and Disadvantages of Lightmaps
**Advantages**:
**Disadvantages**:
Optimization and Considerations
Frequently Asked Questions
What is the difference between lightmaps and real-time global illumination?
Lightmaps are precomputed offline and sampled at runtime; real-time global illumination computes at runtime, supports dynamic changes but has high performance cost.
How do dynamic objects benefit from lightmaps?
Use light probes; dynamic objects sample nearby probes to get approximate indirect lighting.
Why do we need a second UV set?
The first UV set may have overlaps and stretching; the second UV set is dedicated to lightmaps and needs to be unique and non-overlapping.
How long does baking light take?
It depends on scene complexity, parameters, and hardware, ranging from minutes to hours.
Is lightmap necessary on mobile?
Not mandatory, but highly recommended because mobile performance is limited, and lightmaps can significantly improve frame rate.
FAQ
What is the difference between lightmaps and real-time global illumination?
Lightmaps are precomputed offline and sampled at runtime; real-time global illumination computes at runtime, supports dynamic changes but has high performance cost.
How do dynamic objects benefit from lightmaps?
Use light probes; dynamic objects sample nearby probes to get approximate indirect lighting.
Why do we need a second UV set?
The first UV set may have overlaps and stretching; the second UV set is dedicated to lightmaps and needs to be unique and non-overlapping.
How long does baking light take?
It depends on scene complexity, parameters, and hardware, ranging from minutes to hours.
Is lightmap necessary on mobile?
Not mandatory, but highly recommended because mobile performance is limited, and lightmaps can significantly improve frame rate.