There is a story that in the early days of color television, someone pulled off a successful scam selling kits that claimed to upgrade existing black and white TVs to display a color picture. This was done using a bit of plastic that fitted over the TV screen and tinted the top third of the display blue, the middle green, and the bottom brown, on the assumption that most things in life have sky at the top, trees in the middle, and soil underneath. It wasn't the most robust solution, and I suspect the people who bought this kit were not terribly happy, but it would have worked OK for a few carefully chosen images!
Hemisphere lighting is basically just a shader implementation of the same concept. Most conventional lighting models evaluate some sort of equation for the more important few lights in a scene, and then add in a constant ambient term as an approximation of all the leftover bits and pieces. This works nicely for scenes with many complex light sources, but it's less than ideal for outdoor environments where all the direct light is coming from the sun.
With only a single light source available, fully half of every object will be in shadow, and thus will be illuminated only by the ambient term. This gets even worse in overcast or rainy weather conditions, because as the sun is obscured by clouds its direct contribution becomes less, and almost all of the light in the scene ends up being provided by the catch-all ambient constant. A constant amount of light results in a constant color, which makes things look flat and boring. It's also clearly wrong: just step outside on a foggy morning and notice that even though the sun itself may by entirely hidden, there is still enough variation in light levels that you can easily make out the contours of whatever you are looking at.
The problem with conventional lighting models is that in the real world, the majority of light does not come directly from a single source. In an outdoor setting, some of it does indeed come straight from the sun, but more comes equally from all parts of the sky, and still more is reflected back from the ground and other surrounding objects. These indirect light sources are extremely important, because they will often be providing a much larger percentage of the total illumination than the sun itself.
Hemisphere lighting is a simple way of emulating the indirect light contributions found in a typical outdoor scene. Any kind of complex radiosity lighting could be modeled by encoding the surrounding light sources into an HDR (high dynamic range) cubemap, but it is impractical to update such a cubemap in realtime as large numbers of objects move around the world. So we need to approximate, cutting down the complexities of the real world into a more efficient realtime model.
The lighting model I am about to explain was designed for fast moving objects in outdoor environments. Its goals were to tie the moving objects in with their surroundings, to convey a sensation of speed, and to be capable of rendering large numbers of meshes at a good framerate on first generation shader hardware.
It combines a crude form of radiosity lighting with world to object shadowing using just one texture lookup and four pixel shader blend instructions. Approximation is the name of the game here, with performance being by far the most important consideration!
The sun is easy to represent in the lighting model: a per-vertex dot3 can handle it quite nicely. With this taken out of the equation, we are left with a pattern of light sources that can be roughly divided into:
This is trivial to evaluate in a vertex shader: just set your sky and ground colors as constants, and use the y component of the vertex normal to interpolate between them.
Hemisphere lighting avoids the flatness that can result from a constant ambient term, but it also poses a question: what should you use for the ground color? Bearing in mind our goal of making moving objects fit in with their surroundings, it would be good if this could change appropriately depending on your location in the world.
The solution is obvious: encode the ground color into a texture as a large, top-down image of the landscape. This map can then be sampled at a position corresponding to the location of the object, or even better, offset at some distance (a meter or so works well) along the vertex normal. Adding this offset stretches out the sample area to include a larger region of the ground image, and introduces some horizontal lighting variation in addition to the vertical ground to sky transition.
The results may not be exactly what a high-end renderer would describe as radiosity lighting, but it can be a remarkably good approximation. The underside of an object picks up color from the ground directly beneath it, while the sides are influenced by the scenery slightly off to each side, and the top is affected entirely by the sky color.
Making The Map
Ground color maps can easily be generated by taking a screenshot of your level, viewed from above with an orthographic projection. The results can be improved if you preprocess the mesh by removing polygons that are too high above the ground surface, and rotating vertical polygons to face upwards so elements like the sides of fences will contribute to the radiosity colors. I also found it useful to add about 10% of random noise to the resulting texture, as this introduces a subtle speed-dependent flicker that gives an effective sense of motion as you move around the world.
A 1024x1024 texture (only half a megabyte when encoded in DXT1 format) is sufficient to represent a couple of square miles of landscape with enough precision to make out details such as alternating colors along the rumble strip at the edge of a racetrack, or dappled patterns of light and shadow in a forest scene.
Once you have the ground color encoded in a texture, it seems like static environment shadows ought to "just work" if you put a dark patch in the relevant portion of the radiosity map. Compared to other shadowing techniques, this is highly approximate but also incredibly cheap. Plus it can be very effective -- especially for complex shadow patterns such as a forest floor.
Unfortunately, it doesn't "just work". The problem with using the radiosity map to encode shadows is that even if you darken down the ground color, the sky color is still a constant and therefore it won't be affected. There are several possible solutions to this problem:
At runtime, the ground color can be looked up directly from the modified radiosity map. Except for areas of shadow, this will now lack any variation in brightness, but the changes in hue are enough for the technique to remain effective.
To calculate the sky color, in your pixel shader dot the ground color with (1/3, 1/3, 1/3), thus converting it to greyscale. Because of the brightness normalization, this will produce a constant value for all non-shadowed areas, or a darker shade of grey if you are in shadow. Multiplying this value by the sky-color scaling constant gives a correctly shadowed version of the sky term.
Combining the ground color and shadow information into a single texture creates one final dilemma: where should this texture be sampled? The radiosity lighting works best if the sample position is offset along the vertex normal, but that is blatantly incorrect for shadowing, where the map should be sampled directly at the vertex position.
A hack-like compromise is to apply an offset along the left to right component of the normal, but not in the front/back direction, so polygons facing forward or backwards will sample the radiosity map at their exact position, while side facing polygons use an offset sample point. Since objects usually travel roughly along their forward axis, this maintains a nice solid transition as they move in and out of shadow, while still allowing radiosity tints to be picked up from either side of their exact location.
The complete lighting model combines four elements:
Listing 1. Hemisphere Lighting Shader.
#define VS_CONST_0 c.x
#define VS_EYEPOS 1 // object space eye position
#define VS_CAMERA1 2 // 4x4 object to screen matrix
#define VS_ENVMAP1 6 // 3x3 object to world matrix
#define VS_FOG 9 // fog transform vector
#define VS_AMBIENT 10 // ambient light color
#define VS_RADIOSITY_U 13 // radiosity U mapping
#define VS_RADIOSITY_SIDE 15 // object sideways offset
#define VS_RADIOSITY_SAT 16 // ground vs. sky vector
// transform the vertex position
// calculate the fog amount
// output the base texture coords
// **************** RADIOSITY HEMISPHERE ****************
// stretch the radiosity lookup area to either side of the model
// planar map the radiosity texture
dp4 oT1.x, r0, c[VS_RADIOSITY_U]
// calculate the ground to sky radiosity tween factor
// **************** FRESNEL / SPECULAR CUBEMAP ****************
// calculate and normalize the eye->vertex vector
// dot the vertex normal with eye->vert
// fresnel term = (1 - r1.x) * reflectivity amount
// also output a non-fresnel version of the reflectivity amount
// reflect the view direction through the vertex normal
// transform the environment map sample location into worldspace
// **************** DOT3 SUNLIGHT ****************
// let's do a boring old school per vertex diffuse light, too...
def c0, 0.3333, 0.3333, 0.3333, 0.3333
tex t0 // base texture
// envmap + specular
// radiosity hemisphere
// per vertex sunlight