Lightmap Baking in Blender for Three.js

Knowing how to bake lightmaps can be crucial for optimizing the performance of a WebGL scene. Achieving realistic and efficient lighting in Three.js using real-time lights can be challenging. By learning how to bake a lightmap in Blender and implement it in Three.js, you can significantly improve both the visual quality and performance of your 3D web applications.

This tutorial focuses on using Blender's native functionality without relying on third-party plugins.

Alt text

Step-by-Step Guide

1. Prepare Your Scene in Blender

Open your 3D model in Blender. If you're starting from scratch, import or create your 3D model in the Blender environment.

2. Set Up Lighting

Configure your desired lighting setup using Blender's lighting tools. This may include:

  • Sun lamps for directional lighting
  • Point lamps for localized light sources
  • Area lamps for soft, diffuse lighting
  • Emissive materials for objects that emit light

Experiment with different lighting setups to achieve the look you want for your scene.

3. Create UV Maps

Create a new set of UVs for your lightmap. You have several options for UV unwrapping:

  • Use the "Lightmap Pack" option, which is often a good choice for lightmaps
  • Employ other UV unwrapping methods if they better suit your model's geometry

Important Note: Three.js uses the second set of UVs for lightmap textures. If you already have a UV map for an ambient occlusion texture, consider reusing it for the lightmap. This approach can help reduce your model's file size.

4. Prepare for Baking

Create a new image to bake your lightmap into. At this point, you have two options:

  1. Merged Objects Approach: Temporarily merge all your objects into one for the baking process. This can simplify the baking procedure.
  2. Individual Objects Approach: Keep your scene as is, but be prepared for a more complex baking process using Blender's native tools.

When creating the image for baking:

  • Use a 32-bit float type, as we're essentially creating an HDR file
  • HDR files are crucial for high-quality lightmap textures
  • Experiment with different color spaces; "Non-Color" often yields good results

For a visual comparison of different color space results, check out this experiment: Lightmap Color Space Comparison

5. Select Objects and Textures

Select the object(s) you want to bake the lightmap for, as well as the textures you're baking onto them.

6. Configure Baking Settings

Adjust your baking settings in Blender:

  • Select "Diffuse" as the bake type
  • Enable both "Direct" and "Indirect" contributions
  • Set an appropriate resolution for your lightmap (e.g., 1024x1024 or 2048x2048)

7. Bake the Lightmap

Initiate the baking process. Depending on your scene's complexity and chosen resolution, this may take some time.

8. Save and Export

Once the baking is complete:

  • Save the resulting image in HDR or EXR format
  • These formats preserve the high dynamic range information crucial for quality lightmaps

Using the Lightmap in Three.js

With your baked lightmap ready, you can now use it in your Three.js project:

  1. Load your 3D model and the lightmap texture in Three.js
  2. Assign the lightmap to your model's material
  3. Adjust the lightmap's intensity as needed

Here's a basic example of how to apply a lightmap in Three.js:

const textureLoader = new THREE.TextureLoader();
const lightMap = textureLoader.load('path/to/your/lightmap.exr');
// Assuming you've loaded your model
model.traverse((child) => {
if (child.isMesh) {
child.material.lightMap = lightMap;
child.material.lightMapIntensity = 1; // Adjust as needed
child.material.needsUpdate = true;
}
});

Conclusion

By baking lightmaps in Blender and implementing them in Three.js, you can achieve high-quality, realistic lighting in your WebGL scenes while maintaining excellent performance. This technique is especially valuable for complex scenes or applications targeting mobile devices or lower-end hardware.

Remember to experiment with different lighting setups, baking settings, and color spaces to find the optimal balance between visual quality and performance for your specific project.