decoration left 1
decoration left 2
decoration rigth

Show/Hide search bar
black cat logo variable logo
[12 Dec 2012]

Calculation of normal texture

Normal map is an image, each pixel of which contains normal. It's used for per pixel modification of normals in normal mapping method. Normal map is generated from a height map or from a high-polygonal model. In the height map, pixels with small intensities (darker) represent lower regions and pixels with higher intensities (brighter) - elevated regions. Examples of height map and normal map are below. White circle may be a coin elevated over a table.
Height map And respective normal map
You can generate normal maps in Adobe Photoshop with help of NVIDIA plugin for Photoshop, in Mudbox from a high-poly model, and in many other applications.
Normal is calculated for each pixel of the normal map and is stored in texture tangent space. Texture tangent space means that X and Y axes are parallel (tangent) to sides of the image, and Z axis is perpendicular to the image (pointing from the screen; to you). Example of fragment of height map and its isometric interpretation is depicted in the following image. It also shows axes of tangent space. Pixel with orange dot is pixel with coordinates (i, j). We will calculate its normal. And adjacent pixels by X axis are marked with green dots, by Y axis - with red dots.
Fragment of height map Isometric representation of height map and tangent space axes
Simplest calculation of pixel's normal requires values of height of four adjacent pixels. These values are used to calculate height difference between next and previous pixel in X and in Y direction. H(i,j) - function that returns height of a pixel with coordinates (i, j), and α - value for scaling (smaller value leads to smoother changes in normal map).
Differences in height for adjacent pixels
From these two differences in height we can form vectors A and B, that will represent direction of height change by X and Y axes accordingly. Cross product of these vectors is normal of current pixel. Complexity of calculations can be decreased by taking into account constants of A and B vectors. Final normal also requires normalization.
Last row is formula for pixel
Don't forget to normalilze result.
Now, normal can be saved to normal map. Usually each pixel of a texture contains three components (for each color channel - red, green and blue). Normal also has three components, and values of each are in [-1,1] range. But values of color in pixel are in range [0,255]. It's required to transform normal's components to color range, and it is done as:
Coding of normal to RGB color
This value is saved to normal map. Normal map has bluish tint because Z component of normal is always positive, and it corresponds to blue channel. If adjacent values of pixel are same, then pixel normal will be equal to (0,0,1). Such normal corresponds to plain surface and will be coded to RGB as:
Flat surface has bluish tint
Normals also can be calculated with help of more that four adjacent pixels. This leads to more smooth transitions of normals across normal map.

Sun and Black Cat- Igor Dykhta (igor dykhta email) 2007-2014