Introduction

Modelling every brick, rivet, scratch and wrinkle with real triangles would take millions of them. Instead, games use a trick that makes flat surfaces look bumpy by changing how they’re lit. It’s called normal mapping, and it’s on almost every surface in modern games. This post covers:

  • Why changing the normal is enough to fake bumps
  • What a normal map is, and why it’s mostly blue
  • Tangent space: the local frame that makes normal maps reusable
  • The limits of the trick

It builds on Basic Lighting and Textures and UV Coordinates.

Lighting Only Sees the Normal

In Basic Lighting, the brightness of a surface depended almost entirely on its normal, the direction it faces, compared with the direction to the light. The lighting maths never looks at the actual shape of the triangles. So if each pixel of a flat surface uses a slightly different normal, tilted the way a bump would be tilted, it will be lit as if the bump were really there: bright on the side facing the light, dark on the side facing away, with highlights in the right places.

A normal map is a texture that stores, for every texel, which way the surface should face there.

Try It

The wall below is a single flat square: two triangles. The cube is 12. The light moves by itself (press Pause to stop it, or drag to turn the view); toggle the normal map.

A flat surface lit with and without a normal map

Things to notice:

  • With the normal map, the bricks look raised and the mortar looks sunken, and the shading changes correctly as the light moves. Turn it off and the illusion vanishes.
  • Turn the camera so you’re looking along the wall at a grazing angle. The edge is still perfectly straight, and the bricks never hide the mortar behind them. A normal map changes lighting, not shape.
  • Choose Show: The normal map texture. It’s mostly a pale blue-purple, with pink and green tints on the slopes. The next section explains why.

Why Normal Maps Are Blue

A normal is a direction: three numbers (x, y, z), each between −1 and +1. A texture stores colours from 0 to 1. So each component is squeezed into the colour range with colour = normal × 0.5 + 0.5, and the shader undoes it with normal = colour × 2 − 1.

A normal pointing straight out of a flat surface is (0, 0, 1). Encoded, that’s the colour (0.5, 0.5, 1.0): a light blue-purple. Most of a surface is mostly flat, so most of the map is that colour. Where the surface slopes towards +x, the red channel goes up (pinkish); towards +y, green goes up. That’s the familiar look of every normal map.

Normal maps are usually generated automatically, either from a height map like the one in the demo (by measuring how steeply the height changes in each direction), or by “baking” the detail from a very high-detail sculpted model onto a simpler game model.

How the demo makes its normal map from the height map

For each texel, it measures the slope of the height map: the height of the texel to the right minus the one to the left (how steeply it rises going across, dh/du), and the one above minus the one below (how steeply it rises going up, dh/dv). These are multiplied by a strength (6 here) to exaggerate the bumps. Then:

normal = normalize(−dh/du, −dh/dv, 1)

A flat area has no slope, giving (0, 0, 1): straight out. Where the height rises to the right, the normal tips the opposite way, to the left, just as the side of a hill that rises to the right faces left. The function normalize rescales it to length 1 (Vectors). Finally each part, from −1 to 1, is stored as a colour from 0 to 1 by halving it and adding 0.5, as described above.

Tangent Space

There’s a catch. The normal map says “tilt towards +x”, but +x of what? The wall faces the camera and the cube’s faces point in six different directions, yet they share the same normal map.

The answer is that normal maps are stored in tangent space: a little coordinate frame that sits on the surface at each point, lined up with the texture:

  • T, the tangent: the direction across the surface in which the texture’s u increases
  • B, the bitangent: the direction in which v increases
  • N, the surface’s own normal, pointing out

Map x means “along T”, y means “along B” and z means “along N”. So the same map works on any surface, at any angle, and even on surfaces that bend and animate. The tangents are calculated from each triangle’s positions and UVs and stored with the vertices, alongside the normals. In the shader, converting the map’s normal into world space is a small matrix multiplication:

vec3 m = texture(uNormalMap, vUV).rgb * 2.0 - 1.0;       // decode: 0..1 to -1..1
vec3 n = normalize(T * m.x + B * m.y + N * m.z);          // tangent space to world space
// ...then light with n instead of N

(Here uNormalMap is the normal map texture and vUV this pixel’s texture coordinate; m is the normal read from the map, in tangent space; n is the result in world space. .rgb picks out the red, green and blue channels of the colour read from the texture; * 2.0 - 1.0 turns 0 to 1 back into −1 to 1.)

Choose Show: The final normals, as colours to see the result in world space, and compare the cube’s faces: the same map gives different world-space normals on each.

Under the hood

Since a normal has length 1, its z can always be worked out from x and y (using Pythagoras’ theorem: z = √(1 − xÂČ âˆ’ yÂČ)). So games usually store only two channels and rebuild z in the shader, using special compressed two-channel formats, which keep normals far more accurate than a general colour format would. And because normal maps are data, not colours, they must never be stored as sRGB textures (sRGB and gamma): decoding them as colours would bend every normal.

The Limits of the Trick

Normal mapping changes only the lighting, so it breaks down where the real shape would show:

  • Silhouettes stay flat, as you saw at grazing angles.
  • Bumps can’t hide each other or cast shadows onto each other.
  • Very deep detail looks unconvincing up close.

Related techniques go further, at more cost: parallax mapping shifts the texture coordinates per pixel to fake depth, and displacement mapping with tessellation actually moves vertices to create real geometry.

Questions You Might Have

Why do some normal maps look upside down?

There are two conventions for which way the map’s green channel (y) points, often called OpenGL style (+y up) and DirectX style (+y down). Using a map made for the other one makes bumps look like dents in one direction; flipping the green channel fixes it.

Summary

  • Lighting depends on the normal, so tilting the normal per pixel fakes bumps.
  • A normal map stores those normals in a texture as colour = normal × 0.5 + 0.5: mostly blue.
  • Normals are stored in tangent space (tangent, bitangent, normal), so one map works on any surface.
  • It changes lighting, not shape: silhouettes stay flat.

Further Reading