Normal Maps and Tangent Space
Lighting depends on the normal, not on the geometry. If you tilt the normals of a flat surface pixel by pixel, the light reacts as if the surface had grooves, bricks or pores, even though it is still two triangles. That is what a normal map does: a texture whose RGB values store a normal direction for every texel. It is the cheapest detail in real-time graphics, and almost every surface in a modern game uses one.
From height to normal
The simplest way to see it is to start from a height function and compute its slope. If h(x, y) is the height of a bumpy surface, its normal is
n = normalize(vec3(-dh/dx, -dh/dy, 1))The two derivatives are estimated by sampling h a small step away in each direction (finite differences). This is bump mapping; a normal map is the same result baked into a texture so the shader does not have to compute it.
The left half below shows the normals as colors, n * 0.5 + 0.5: the bluish-purple image you know from normal-map textures (a flat normal (0, 0, 1) encodes as (0.5, 0.5, 1.0)). The right half uses the same normals for lighting, with a light that circles around. Drag with the mouse to move the divider.
float hash(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }
float valueNoise(vec2 p) {
vec2 i = floor(p), f = fract(p);
vec2 u = f * f * (3.0 - 2.0 * f);
return mix(mix(hash(i), hash(i + vec2(1, 0)), u.x),
mix(hash(i + vec2(0, 1)), hash(i + vec2(1, 1)), u.x), u.y);
}
// Height of the surface: rounded tiles plus a little noise.
float height(vec2 p) {
vec2 cell = fract(p * 4.0) - 0.5;
float tile = 1.0 - smoothstep(0.30, 0.45, max(abs(cell.x), abs(cell.y)));
return tile * 0.6 + valueNoise(p * 24.0) * 0.08;
}
vec3 normalFromHeight(vec2 p) {
float e = 0.002;
float dx = (height(p + vec2(e, 0.0)) - height(p - vec2(e, 0.0))) / (2.0 * e);
float dy = (height(p + vec2(0.0, e)) - height(p - vec2(0.0, e))) / (2.0 * e);
return normalize(vec3(-dx * 0.05, -dy * 0.05, 1.0)); // 0.05 = bump strength
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 p = fragCoord / iResolution.y;
vec3 n = normalFromHeight(p);
float split = iMouse.z > 0.0 ? iMouse.x : iResolution.x * 0.5;
vec3 color;
if (fragCoord.x < split) {
color = n * 0.5 + 0.5; // encode -1..1 as 0..1
} else {
vec3 lightDir = normalize(vec3(cos(iTime), sin(iTime), 0.7));
float diffuse = max(dot(n, lightDir), 0.0);
color = vec3(0.75, 0.55, 0.4) * (0.1 + 0.9 * diffuse);
}
if (abs(fragCoord.x - split) < 1.0) color = vec3(1.0);
fragColor = vec4(color, 1.0);
}The normal on the left is in tangent space: z points straight out of the surface, x along the texture's U direction and y along V. That is the space normal-map textures are stored in, because it does not depend on how the surface is oriented in the world.
Tangent space and the TBN matrix
A tangent-space normal has to be rotated into world space before it can be lit. Each vertex carries three vectors that do it:
| Vector | Points along | Comes from |
|---|---|---|
| Tangent | the direction U increases on the surface | computed from UVs at import (stored as a vec4; w = handedness sign) |
| Bitangent | the direction V increases | cross(N, T) * tangent.w in the shader |
| Normal | out of the surface | the mesh normal |
Put them as the columns of a 3×3 matrix and one multiply does the change of basis:
// vertex shader: build the basis in world space
vec3 N = normalize(normalMatrix * normal);
vec3 T = normalize(mat3(modelMatrix) * tangent.xyz);
T = normalize(T - N * dot(N, T)); // re-orthogonalize (Gram-Schmidt)
vec3 B = cross(N, T) * tangent.w; // w flips mirrored UVs
vTBN = mat3(T, B, N);
// fragment shader: decode the texel and rotate it
vec3 nTS = texture(uNormalMap, vUv).xyz * 2.0 - 1.0;
vec3 nWS = normalize(vTBN * nTS);The raymarched sphere below has no mesh and no UVs, so it builds its own: longitude and latitude give uv, the tangent points east around the sphere, and the bitangent north. The "texture" is a procedural brick pattern computed in tangent space, then rotated by the TBN matrix exactly as above.
const float PI = 3.14159265;
// Tangent-space normal of a brick wall at uv (0..1 per brick row/column).
vec3 brickNormal(vec2 uv) {
vec2 b = uv * vec2(12.0, 8.0);
b.x += step(1.0, mod(b.y, 2.0)) * 0.5; // offset every other row
vec2 f = fract(b) - 0.5;
// slope near the mortar lines: tilt toward the brick's center
vec2 edge = smoothstep(0.38, 0.5, abs(f)) * sign(f);
return normalize(vec3(-edge * 1.2, 1.0));
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 p = (2.0 * fragCoord - iResolution.xy) / iResolution.y;
vec3 ro = vec3(0.0, 0.0, 3.0);
vec3 rd = normalize(vec3(p, -1.8));
// ray-sphere intersection (radius 1 at the origin)
float b = dot(ro, rd);
float h = b * b - (dot(ro, ro) - 1.0);
vec3 color = vec3(0.08, 0.09, 0.12);
if (h > 0.0) {
vec3 pos = ro + rd * (-b - sqrt(h));
// spin the sphere so the bricks move
float a = iTime * 0.4;
mat2 rot = mat2(cos(a), -sin(a), sin(a), cos(a));
vec3 q = pos;
q.xz = rot * q.xz;
vec3 N = normalize(pos);
vec2 uv = vec2(atan(q.z, q.x) / (2.0 * PI) + 0.5, acos(clamp(q.y, -1.0, 1.0)) / PI);
vec3 T = normalize(cross(vec3(0.0, 1.0, 0.0), N)); // east (undefined exactly at the poles)
vec3 B = cross(N, T); // north
mat3 TBN = mat3(T, B, N);
bool flat_ = fragCoord.x < iResolution.x * 0.5;
vec3 n = flat_ ? N : normalize(TBN * brickNormal(uv));
vec3 lightDir = normalize(vec3(-0.6, 0.7, 0.8));
float diffuse = max(dot(n, lightDir), 0.0);
vec3 albedo = vec3(0.7, 0.32, 0.22);
color = albedo * (0.08 + 0.92 * diffuse);
}
fragColor = vec4(pow(color, vec3(1.0 / 2.2)), 1.0);
}The left half uses the geometric normal: a smooth ball. The right half uses the normal map: the silhouette is still a perfect circle, but the inside looks like bricks. That is the limit of normal mapping: it changes shading, not shape. Silhouettes, self-shadowing and parallax still show the truth, which is why close-up surfaces add parallax occlusion mapping or real displacement (tessellation, Nanite-style dense meshes).
The green channel: OpenGL vs DirectX
Normal maps come in two flavors that differ only in the sign of y (the green channel):
| Convention | Green means | Used by |
|---|---|---|
| OpenGL / Y+ | the normal tilts toward +V (up in the texture) | Blender, Godot, Unity, Three.js, glTF |
| DirectX / Y- | the normal tilts toward -V | Unreal Engine, 3ds Max, many Substance presets for DirectX targets |
Load one with the other's convention and the lighting inverts on one axis: bumps that should catch light from above look lit from below, so they read as dents. The fix is a checkbox, not a repaint: flip the green channel on import (Unreal's texture setting Flip Green Channel, or nTS.y = -nTS.y in your shader).
This shader shows the same bumps decoded both ways. Notice how the right half's top edges are dark where the left half's are bright:
float height(vec2 p) {
vec2 c = fract(p * 3.0) - 0.5;
return smoothstep(0.35, 0.1, length(c)); // round domes
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 p = fragCoord / iResolution.y;
float e = 0.002;
float dx = (height(p + vec2(e, 0)) - height(p - vec2(e, 0))) / (2.0 * e);
float dy = (height(p + vec2(0, e)) - height(p - vec2(0, e))) / (2.0 * e);
vec3 n = normalize(vec3(-dx * 0.08, -dy * 0.08, 1.0)); // the "texture", OpenGL convention
bool directX = fragCoord.x > iResolution.x * 0.5;
if (directX) n.y = -n.y; // decoded with the wrong convention
vec3 lightDir = normalize(vec3(0.0, 0.8, 0.6)); // light from the top of the screen
float d = max(dot(n, lightDir), 0.0);
vec3 color = vec3(0.9, 0.8, 0.6) * (0.15 + 0.85 * d);
if (abs(fragCoord.x - iResolution.x * 0.5) < 1.0) color = vec3(1.0, 0.2, 0.2);
fragColor = vec4(color, 1.0);
}Getting normal maps right in production
- Bake and render with the same tangent basis. A baked normal map is only correct for the exact tangents the baker used. The standard is MikkTSpace (used by Blender, Substance, Unity, Unreal, Godot and glTF tools): let your exporter and engine compute MikkTSpace tangents and leave them alone. Seams and faint shading stripes on a baked asset almost always mean mismatched tangents.
- Import normal maps as linear data, not sRGB. Unity's texture type "Normal map", Unreal's
TC_Normalmapcompression setting and Godot's Normal Map import option do that. Decoding a normal map with the sRGB curve bends every normal. - Compressed formats often store only X and Y (BC5, or DXT5nm with X in alpha) and the shader rebuilds
z = sqrt(1 - x² - y²). Engines do this in their unpack helper (UnpackNormalin Unity), which is one reason to use it instead of* 2 - 1by hand. - Renormalize after interpolation and after the TBN multiply. Interpolated vectors are shorter than 1.
- Mipmaps average normals, which shortens them and flattens distant detail; techniques like Toksvig or specular anti-aliasing convert that lost length into extra roughness so distant normal-mapped surfaces do not sparkle.
Test yourself
What color does a perfectly flat tangent-space normal (0, 0, 1) encode to in a normal map?
- (0, 0, 1) pure blue
- (0.5, 0.5, 1.0) light purple-blue
- (1, 1, 1) white
- (0.5, 0.5, 0.5) grey
A normal map from an Unreal project looks inverted (bumps read as dents) in Godot. What is the usual fix?
- Invert all three channels
- Flip the green (Y) channel
- Mark the texture as sRGB
- Rotate the UVs by 90 degrees
What can a normal map NOT change?
- Diffuse shading
- Specular highlights
- The object's silhouette
- Perceived surface detail