How to render a normal map

A poster to the Rhino newsgroup recently asked for an easy way to render a normal map based on a model in Rhino.  This simple surface shader can be used to render such a tangent-space normal map with Air:

surface RenderTangentNormalMap(
 float ReverseZ = 1;
)
{
 normal NN=normalize(faceforward(N,I));
 if (ReverseZ!=0) setzcomp(NN,-zcomp(NN));
 NN=0.5+0.5*NN;
 Ci = NN;
 Oi = 1;
}

Here’s an archive with the source code and a pre-compiled binary for Air.  Air users can install the shader by unzipping the archive to the shaders directory of their Air installation.

To generate a normal map, first be sure the output image gamma is set to 1 (RhinoAir users can find this control on the Air Display page in the Rhino Options dialog).  Assign the above shader to your objects, then render an orthographic viewport.  You should see an image with colors similar to those in this sample:

To apply the normal map to an object for rendering, assign the VTangentNormalMap shader as the displacement shader for the object, and set the shader’s TextureName parameter to the file name of the previously generated normal map.  Here’s a simple rendering using the above map:

This entry was posted in Shaders and tagged , , . Bookmark the permalink.