Magic Mushrooms - XNA Based Light Map Baker
After spending a good portion of my career at Bigant Studios building and maintaining a set of scripts for baking lightmaps in Maya across many PC's, I felt there had to be a better way. I had looked into programs like Beast, but could not convince people to splash out on the licencing fees.
The demands of lighting in games were getting higher and higher, and often Maya and Mental ray were getting difficult to match the effects of game shaders while baking. I wanted to look into in engine baking, but before pitching this to the team I decided to have a go at it at home, and see what sort of results and bake times I could get. To this end I used XNA as a base to produce a radiosity light map baking prototype.
The method was fairly simple:
- I rendered out position, and normal information into a set of arrays for later use at each texel in the lightmap.
- I then rendered the forward lighting results directly into a lightmap.
- Another buffer was then rendered into to collect each bounce of lighting, using the previous buffer to light the scene (starting with the forward lighting).
- Every few frames all the bounces were composted into the light maps used in the scene.
- There are 3 lightmaps rendered. One for each basis direction pointing out at 3 different directions, 45` off from the normal (as pictured below). This allows the bounce lighting to be affected by the normal map. As you know what light is coming from where.
- Normally light values are gathered by rendering a hemispherical cubemap. However, as I was gathering 3 different basis directions, I found I was able to get away with rendering a single wide field of view face per a direction.
I felt that the difficulty of implementation was not so much that it isn't worth rolling an internal solution even in a small production house. Maintaining a set of bake scripts and giving the lighter any kind of reasonable feedback is a not a trivial task. I feel the in house (or game specific library like Beast) win out for the following reasons.
- The lighter gets actual feedback immediately. The direct lighting bakes in no time at all and gives a pretty good start at lighting feedback.
- The baker can start baking very low rez, and rebuild every time the lighting is changed, slowly building up to the high rez textures in the background. In my demo I was able to fly around and observe the lighting baking on the go. This fast feedback is invaluable to lighters.
- What you see is what you get. The bounce lighting is calculated with your in game shaders, and feedback is provided using them.
- Any sort of texture your game requires can be baked. I can't really stress this enough. Often I found myself at work needing to roll custom Mental Ray shaders to get the job done, and even then, it could be tough to get the exact results I wanted with all the inputs for the variety of in game shaders we used.
Some Resources used: