June 17th, 2009
Why are decals important?
The decal is one of my favorite 3D graphics tools. It serves so many purposes: covering seams, spicing up tiling textures, adding unique details, and allowing dynamic cosmetic environmental changes. Here is a naked 'land chunk' model, with no decals:
Here is the same model again after adding a rock decal and a moss decal:
As you can see, the surface is now more interesting and detailed, and the transition between the ground and the 'land chunk' is less abrupt and unrealistic.
How do decals work?
As each decal is placed, it creates a small 'base normal' map (the 'normal' of a surface is a vector perpendicular to it) and blurs it slightly to remove any seams. Here is what the base normal map looks like for these decals. The red, green, and blue channels represent the X, Y and Z axes respectively, so the surfaces pointing up (+Y) are green, surfaces facing 'right' (+X) are red, and surfaces facing 'in' (+Z) are blue.
The decals use the alpha channel of their color map to control their transparency. Here is the base normal map color combined with the decal map alpha, so we can see how much of the base mesh shows through.
The base normal map is then combined with the decal normal map to get the final surface normals. This is used to make the lighting respond to the surface details of the decals, such as the roughness of the moss and the smooth roundness of the rocks.
Here is what the direct diffuse lighting looks like on the final normal map. This is easy to calculate because we are using world-space normals -- we just have to calculate the dot product of the light vector and the normal vector in the fragment shader. We don't have to worry at all about tangents and bitangents.
Finally, we modulate the decals by their color channels, and add ambient lighting. The decals blend into the scene very well because they are using the same lighting model as everything else.