I’ve learned a lot in the last few days about lightmapping, the process of “baking” lighting effects into the texture on a 3D object. I’ve heard it described as a “poor man’s lighting effect.” For me, that’s a true statement. I can’t currently afford the pro version of Unity, which includes dynamic, real-time shadows, so the next best option is to map static shadows into my scenes.
Despite being less flashy, lightmapping is considerably less expensive in terms of processing. Ultimately, I think it’s a fantastic value. The effect is great, and the cost is minimal.
Lightmapping is a tough concept to get your head around. Wikipedia has a brief article on it, but it’s kind of technical. Here’s the gist:
- Generally the best way to control lighting is in your 3D modeling application (the image to the right was rendered in my 3D app). You can build a number of objects, texture them, and light them in whatever way you choose. The trouble is that rendering images with sophisticated lighting is much too slow for a real-time game engine.
- When applying textures to 3D objects for games, you have to “unwrap” the object onto a flat surface that then gets painted with a texture (a tedious process called UV mapping). The texture is then wrapped back around the object.
- Most 3D modeling applications now allow objects to have multiple UV sets. In other words, the main texture can use one set of UV coordinates, and another texture can be attached to the second set.
- The lightmapping process takes advantage of this second UV set by converting lighting information from the 3D application into a flat texture that can be applied to an object on top of the primary texture. The result is an object that looks like it’s being lit, even when no lights are present.
- Since lighting is one of the most processor-intensive tasks in real-time 3D processing, lightmapping is tremendously beneficial from a performance standpoint. The downside is that the “baked” lighting isn’t dynamic and won’t apply to any moving objects in the scene (well, you could do it, but it would look pretty strange).
These before and after images illustrate the point. The first one is a screenshot of a scene with no lighting and no lightmapping. The second image uses lightmapped textures, but still has no in-game lights. As you can see, the cube that should be lit by the light coming through the window (if this were the real world), is not receiving any light at all, since there are actually no lights in the scene.
Probably the best approach for me is to use some combination of lightmapping and in-game lighting. The result won’t be perfect, but it will be close enough until I can spring for the pro version of Unity.