Drawing

Normal mapping and tangent space

Normal mapping makes a flat surface look bumpy. It does not move any vertex. It only changes the normal that lighting uses, pixel by pixel, with values read from a texture. This page explains how the normals are stored, the small coordinate frame they are stored in, and the sign and transform details that are easy to get wrong. The last part shows how RASM does it.

In short

  • Lighting depends on the normal. A normal map stores a normal per texel, so a flat triangle can be lit like a detailed surface.
  • Each channel stores one component: value = (n + 1) / 2. The texture must be read as plain numbers (UNORM), never as sRGB.
  • The normals are in tangent space: x along the texture's u, y along its v, z out of the surface. The TBN matrix turns them into world space: n = T·x + B·y + N·z.
  • Meshes store T and a sign w = ±1. The bitangent is B = w · cross(N, T). The sign handles mirrored UVs.
  • Normals move to world space with the inverse-transpose of the model matrix; tangents move with the model matrix itself. A mirroring transform flips the sign.
  • Without stored tangents, a shader can build the frame from screen-space derivatives of the position and the UV.

1Why normal mapping

The brightness of a lit surface depends on the angle between its normal and the light (n·l, see PBR shading). Small details like bricks, scratches or pores change that angle many times inside one pixel-sized area. Modelling them as real triangles would need millions of triangles. Normal mapping keeps the triangles few and stores only the normals of the detail, in a texture.

the normal used for lighting light the strip below: what the eye sees, dark where the normal turns away
Real bumps many triangles lit like bumps, bumpy outline Flat 2 triangles every pixel lit the same Flat + normal map 2 triangles + 1 texture lit like bumps, flat outline
The normals come from the texture, so they can change from pixel to pixel, inside one triangle. Normal mapping fakes the lighting, not the shape. The outline stays flat, the bumps do not hide each other, and they cast no shadows. For large shapes, real geometry is still needed.

A normal map is usually baked:

  1. Detailed meshan artist builds it, with millions of triangles
  2. Bakea tool records the normals of the detailed mesh
  3. Normal mapa texture laid over a simple mesh

It can also be painted, or made from a height map.

2Storing a normal in a texture

A unit normal has 3 components, each between −1 and 1. A texture channel stores 0 to 1 (as 0 to 255 in 8 bits). So each component is moved and scaled into that range, and back when it is read:

stored = (n + 1) / 2          n = stored · 2 − 1
One component n −1 0 1 stored 0 0.5 1 byte 0 128 255 Three normals, three texels 64,128,238 (−0.5, 0, 0.87) 128,128,255 (0, 0, 1) 191,128,238 (0.5, 0, 0.87)
A flat area is (128, 128, 255), the typical lavender blue. Red shows tilt along x, green tilt along y. The values are mostly z: that is why normal maps look light blue.
8 bits have no exact middle

The byte 128 reads back as 128 / 255 · 2 − 1 = 0.0039, not 0. The exact middle would be 127.5, so a "flat" texel is very slightly tilted.

Three rules for reading the map:

UNORM 128 → 0.5 sRGB 128 → 0.22

Read it as UNORM (linear), never sRGB

The values are numbers, not colors. An sRGB format would apply the sRGB curve on read, and 128 would come back as about 0.22 instead of 0.5: every normal bends. See Color spaces.

as read: too short normalized: length 1

Normalize after reading

8-bit rounding, filtering between texels, and mipmaps all shorten the vector a little.

bump dent right convention wrong one

Know the green convention

"OpenGL style" maps (glTF, Blender) store +y as up in the image. "DirectX style" maps store +y as down. Using one as the other flips every bump in y: lit from below instead of above.

The fix: y = −y

Try it. The square below is flat. Each pixel reads three bytes from the map on the left, turns them back into a normal with the formula above, normalizes it, and computes n·l.

Turn the light around the square: the bright sides of the bumps follow the sun. Switch the map off to see the flat square. Then read green with the other convention, and compare the bright sides with the sun again.

3Tangent space

In which coordinate system are the stored normals? There are two choices.

the mesh's axes

Object space

The normal is stored in the mesh's own coordinates. Simple to use: rotate with the object and done.

But the map works only for that one mesh, it cannot tile or be reused, and it breaks when the mesh bends (skinning). It uses all colors, so it compresses badly.

z: out of the surface a small frame at every point

Tangent space (the usual choice)

The normal is stored relative to the surface itself: z is "straight out of the surface". The same brick map then works on a wall, a floor or a curved column, it tiles, and it bends with the mesh.

Since z is always positive, two channels are enough (section 9).

Tangent space is a small coordinate frame at each point of the surface, made of three unit vectors:

On the surface T B N In the texture T B u grows up
T and B lie in the surface and follow the texture's layout on it. So "tilted to the right in the image" always means "tilted along T", whatever way the surface faces.

texel (x, y, z) = x steps along T, y steps along B, z steps along N

T
the tangent: the direction on the surface in which the texture coordinate u grows
B
the bitangent: the direction on the surface of the texture's "up" (the stored +y)
N
the normal: out of the surface
"Binormal"

B is also called the "binormal". That name is wrong for surfaces (it comes from curves), but it is very common.

4The TBN matrix

Put T, B and N (in world space) as the columns of a 3×3 matrix. Multiplying a tangent-space vector by it gives the same vector in world space:

TBN matrix T B N columns × x y z from the texture = T·x + B·y + N·z the world-space normal T N T·x N·z n
On the right, the sum seen from the side (here y = 0). The inverse (the transpose) goes the other way: from world to tangent space. Some engines move the light and view vectors into tangent space in the vertex shader instead, and light in tangent space. Today most engines move the normal to world space, as here.

Try the sum. The picture is the side view again (y = 0), with a surface that can tilt.

T, and the step T·x N, and the step N·z (dashed) the normal from the texel
Choose a texel, then tilt the surface: the stored bytes stay the same and the world-space normal turns with the surface. Set the texel's x to 0 to get N itself.

With (0, 0, 1), the flat texel, the result is N: no change. When T, B, N are unit length and at right angles, the inverse of the matrix is its transpose.

5Where tangents come from, and the sign w

A triangle exists twice: in the texture, with its UVs, and on the surface, with its positions. The tangent and bitangent follow from both.

In the texture p0 p1 p2 (Δu1, Δv1) (Δu2, Δv2) u v the same triangle On the surface e1 e2 p0 p1 p2 T B T: where u grows B: where v grows
Each edge on the surface is some steps along T plus some steps along B. The UV differences say how many.

For one triangle, the tangent and bitangent follow from its positions and UVs. Take the two edges e1 = p1 − p0, e2 = p2 − p0, and the UV differences (Δu1, Δv1), (Δu2, Δv2). We look for the directions T (where u grows) and B (where v grows) with

e1 = Δu1 · T + Δv1 · B
e2 = Δu2 · T + Δv2 · B

r = 1 / (Δu1 · Δv2 − Δu2 · Δv1)
T = r · (Δv2 · e1 − Δv1 · e2)
B = r · (Δu1 · e2 − Δu2 · e1)
Which way is "up"?

Here B is where v grows. That is the texture's "up" when v grows toward the top of the image (the OpenGL habit). When v grows downward, as in glTF, where (0, 0) is the top-left corner, the texture's up is −B.

Each vertex then averages the tangents of the triangles around it (Lengyel's method). What does a vertex store?

N T B 9 floats

All three vectors

Storing all three of T, B, N per vertex costs 9 floats.

N T w ±1 7 floats

N, T and a sign

Most formats store N, T and one more number instead: the tangent sign w, either +1 or −1. The shader rebuilds the bitangent.

B = w · cross(N, T)

cross(N, T) is at right angles to both, so it already points along B or exactly against it. w says which one. Why can it be −1? Because of mirrored UVs.

The texture u grows up mirror line left half: the texture, mirrored right half: the texture T (u grows) B cross(N, T) w = −1 T (u grows) B = cross(N, T) w = +1
N points out of the page. Vertices on the mirror line belong to both halves. They must be split into two vertices, one per sign, or the tangents average to nothing there.

Artists often map only one half of a symmetric object and mirror it, to save texture space. On the mirrored half, u runs the other way, so T flips. B (the texture's up) does not. T, B, N now form a left-handed frame, and cross(N, T) points against B.

With w = −1, B = w · cross(N, T) points the right way again, and the bumps are lit from the correct side on both halves.

MikkTSpace: one answer for everyone

A normal map is only correct with the exact tangents that the baking tool used.

the baker's frame the engine's frame

Two different rules

Different averaging rules (by area, by angle, split or not at UV seams) give slightly different tangents, and the result shows as seams and wrong shading on curved areas.

baker and engine: the same frame

MikkTSpace in both

MikkTSpace (Morten Mikkelsen, 2008) fixes this by being one precisely defined algorithm that bakers and engines all implement the same way. Blender, Unity, Unreal and Substance use it, and the glTF format says that a mesh without tangents should get MikkTSpace tangents.

6Interpolation, and Gram–Schmidt

The vertex shader passes N and T to the fragment shader, and the rasterizer interpolates them across the triangle. Interpolated unit vectors are a little too short, and T is no longer exactly at right angles to N. The fix is the Gram–Schmidt step: remove the part of T that points along N, then normalize.

N T, interpolated − N (N·T) T′ = normalize(T − N (N·T)) Example N = (0, 1, 0) T = (0.9, 0.3, 0.1) N·T = 0.3 T − 0.3·N = (0.9, 0, 0.1) T′ = (0.994, 0, 0.110) Then B = w · cross(N, T′) is also exact.
Normalize N first. Gram–Schmidt needs a unit N to remove the right amount.

7Moving normals and tangents to world space

Vertices move to world space with the model matrix M. Tangents lie in the surface, like a small edge, so they move the same way: Tworld = M · T (the 3×3 part, no translation). Normals do not. If M scales one axis more than the others, M · N is no longer at right angles to the surface:

the tangent the normal moved with M: wrong the normal moved with the inverse-transpose: right
Before: slope at 45° N (0.707, 0.707) T (0.707, −0.707) scale x by 2 After: M = scale(2, 1) M·T → (0.894, −0.447) M·N → (0.894, 0.447) ✗ (M⁻¹)ᵀ·N → (0.447, 0.894) ✓ dot with the new tangent: M·N: 0.60 (not at right angles) inverse-transpose: 0.00
The normal must stay at right angles to every tangent. The matrix that keeps it there is the inverse-transpose (M−1)T. For a pure rotation it equals M; with uniform scale it equals M up to a length, which normalizing removes.
Why it works, in one line

N ⊥ T means NTT = 0. After the transform we need N′T (MT) = 0. With N′ = (M−1)T N, the product is NT M−1 M T = NTT = 0.

Mirroring transforms

A model matrix with a negative determinant mirrors the object (for example a scale of −1 on one axis, used to place a left door from a right door). The mirror keeps T and N correct, but it turns cross(N, T) around: the frame changes handedness, just as with mirrored UVs. So the sign must change too: wworld = w · sign(det M).

T B = cross(N, T)

The right door: det M > 0

The frame keeps its handedness. w stays as it is.

T B cross(N, T)

The left door: det M < 0

cross(N, T) now points against B. w changes its sign.

8A frame without stored tangents

Some meshes have no tangents. Instead of computing them on load, the fragment shader can build a frame on the fly from screen-space derivatives. The GPU shades pixels in 2×2 blocks, so any value has a difference to its right neighbour (ddx) and to the one below (ddy). For the position p and the texture coordinate (u, v):

On screen ddx: one pixel right ddy: one pixel down the same steps On the surface dp/dx dp/dy T u grows →
The grid on the surface is the texture's. This is the method of Mikkelsen (2010) and Schüler (2013). It costs a few instructions per pixel and no vertex data.
dp/dx = ddx(p)     duv/dx = ddx(uv)
dp/dy = ddy(p)     duv/dy = ddy(uv)

det = du/dx · dv/dy − dv/dx · du/dy
T  ∝ (dp/dx · dv/dy − dp/dy · dv/dx) · sign(det)
B  ∝ (dp/dy · du/dx − dp/dx · du/dy) · sign(det)

These are the same two equations as for a triangle (section 5), with the pixel steps as the two edges. A negative det means mirrored UVs: the sign keeps the frame right.

one vertex position normal uv tangent not needed

Upside

No tangents to compute, store or transform. Mirrored UVs and mirroring transforms work by themselves, because the frame comes from what is on screen.

one flat frame per triangle

Downside

The frame is flat per triangle, so it does not match a baked map on curved, smooth surfaces as well as MikkTSpace does. The derivatives are coarse at triangle edges and noisy at grazing angles. If the UVs do not change (det = 0), there is no frame at all.

9Two-channel maps and strength

A tangent-space normal points out of the surface, so z > 0. And it has length 1. So z can be computed from x and y:

z = sqrt(max(0, 1 − x² − y²))
x z x = 0.6 z = √(1 − 0.36) = 0.8 The texel R: x G: y B: not stored z = sqrt(max(0, 1 − x² − y²)) the shader rebuilds z
The length is 1, so x and y fix a point on the circle, and z is its height.

Why do it? Block-compressed two-channel formats (BC5, also called ATI2 or 3Dc; EAC RG on mobile) store x and y with much better quality than a three-channel format at the same size. The price is one more square root in the shader.

With (0.8, 0.7) the sum is above 1: max(0, …) keeps the square root safe, and z = 0.

Many formats also have a strength (glTF calls it scale): a number that multiplies x and y before the vector is normalized.

Strength 0

The surface is flat.

Strength 1

The map as it is stored.

Strength above 1

The bumps are stronger.

10Worked example

A floor. Its normal points up, N = (0, 1, 0), and its u direction runs along +X, so T = (1, 0, 0). One texel holds the bytes (128, 191, 231). The light comes from above and from +Z: l = (0, 0.707, 0.707).

1. Decode the texel

(128, 191, 231) the bytes in the texture / 255 · 2 − 1 (0.004, 0.498, 0.812) length 0.952 normalize (0.004, 0.523, 0.852) tangent space B, the texture's "up" N n 32° x ≈ 0: no tilt along T. y = 0.52: the normal leans toward B.

The normal leans toward the texture's up direction by about 32° (cos⁻¹ 0.852). This texel is on the upper slope of a bump.

2. Build the frame

cross(N, T) = cross((0,1,0), (1,0,0)) = (0, 0, −1) T B w = +1 B = (0, 0, −1) the texture's up is −Z T B w = −1 (mirrored UVs) B = (0, 0, +1) the texture's up is +Z The floor from above: +X is to the right, +Z is down the page, N points at you.

The texture's "up" is −Z on the normal floor and +Z on its mirrored copy. Same T, same N, different B: only w tells them apart. The sign comes with the vertex. A shader that ignores it lights one of the two copies wrong.

3. To world space, and light it

n = T·0.004 + B·0.523 + N·0.852 w = +1 n = (0.004, 0.852, −0.523) l n n·l = 0.23 0.852·0.707 − 0.523·0.707 w = −1 n = (0.004, 0.852, +0.523) l n n·l = 0.97 0.852·0.707 + 0.523·0.707

The floor from the side: +Z is to the right, the dashed line is the flat floor's normal. The flat floor would give n·l = 0.707. The bump turns away from the light on one copy (darker, 0.23) and toward it on the other (brighter, 0.97).

A wrong sign does not make anything black. It makes bumps lit from the wrong side, which is easy to miss: the surface looks like dents instead of bumps.

11Pitfalls

Everything leans the same way; the surface looks tilted
Normal map loaded as sRGB. Use a UNORM format.
Bumps look like dents, lit from below
Green channel convention mixed up. Flip y, or convert the texture once.
One half of a mirrored model is lit inside out
Tangent sign ignored. Use B = w · cross(N, T).
Wrong shading on stretched objects only
Normal moved with M, not its inverse-transpose. Use (M−1)T, or keep scales uniform.
A mirrored copy of an object has inverted bumps
Mirroring transform not folded into w. Use w · sign(det M).
Faint seams, wrong shading on curves
Tangents differ from the baker's. Use MikkTSpace in both.
Bumps fade and shine too much at a distance
Mipmaps of a normal map averaged as colors. Renormalize; better: turn the lost length into roughness (Toksvig, LEAN).

12In RASM

RASM does tangent-space normal mapping in the fragment shader, in getNormalFromMap (shaders/lib/pbr.slang), which shadePBR calls when the material has a normal texture. It supports both kinds of frame from this page: the stored tangent with its sign (sections 5 to 7), and the frame from derivatives (section 8) when the mesh has no tangents.

CPU · at load glTF import TANGENT read only with NORMAL w snapped to −1 or +1 no TANGENT, generated normals, or an OBJ file: w = 0 VertexFull normal tangent (xyz, w) GPU · vertMain N = normalMatrix · normal (inverse-transpose: CPU or Animate pass) T = mat3(worldMatrix) · tangent.xyz w = tangent.w · handedness handedness = −1 when det < 0 then interpolated to fragMain GPU · getNormalFromMap tangentNormal = texel.xyz · 2 − 1; tangentNormal.xy *= scale; N = normalize(N) |w| > 0.5: the authored frame T = normalize(T − N (N·T)) B = cross(N, T) · sign(w) w = 0, or T has no length frame from ddx, ddy of position and UV if the UV determinant is ~0: return N
Both frames end the same way: normalize(T * x + B * y + N * z). shadePBR then flips the result on a double-sided back face.

Where RASM follows the textbook, and where it differs

PartThis pageRASM
Texture formatUNORM, maybe two channelsAn image is R8G8B8A8_UNORM, all three channels read. A two-channel KTX2 normal map is BC5_UNORM or EAC_R11G11_UNORM: the material row gets MATERIAL_NORMAL_XY and getNormalFromMap rebuilds z = √(1 − x² − y²) before the scale. Its RGBA8 fallback gets z on the CPU (texture compression).
Green convention+y up or down+y up, as glTF defines it.
Strengthmultiply x, yThe same, with the glTF scale (default 1).
Where tangents come fromLengyel or MikkTSpace on loadOnly from the file: glTF TANGENT, used only when the primitive has NORMAL too (the glTF rule). RASM computes no tangents: no MikkTSpace. Meshes without them use the derivative frame.
"No tangent" marker—tangent.w = 0. Import snaps a stored w to ±1, so 0 is free for this.
Bitangentw · cross(N, T)The same, with the sign of w.
Gram–SchmidtyesYes, in the fragment shader. A tangent that becomes 0 falls back to the derivative frame.
Normal transforminverse-transposenormalMatrix = transpose(inverse(mat3(model))), computed per instance on the CPU in writeInstanceRows. A sub-mesh that a clip moves gets it from the Animate pass instead, in its InstanceTransform row; getNormalMatrix picks the one to read. A normal of length 0 becomes (0, 0, 1).
Skinned and morphed vertices—The Skin pass poses them before any view draws (skinning): it adds the morph deltas to the normal and the tangent and renormalizes them, then moves the normal with the normal matrix of the mixed joint matrix and the tangent with the mix itself, and flips w when the mix mirrors. A tangent with w = 0 stays "no tangent".
Tangent transformM, and w · sign(det M)The same, in vertMain; the sign is the handedness output.
Derivative framesection 8The same idea. The bitangent's sign is chosen against dP/dv, so B points toward decreasing v and matches the authored frame (fixed in B19).
Mipmap filteringrenormalize, ToksvigThe mip levels are made on the CPU at load by averaging the stored values, like any UNORM texture. The shader renormalizes the final normal. No roughness adjustment.
No normal map—The interpolated vertex normal, normalized; no frame is built.
Missing normals—A glTF primitive without NORMAL gets smooth normals, averaged from its triangles on import, and its tangents are dropped. (The glTF specification asks for flat normals in that case.)

Where it lives in code

WhatWhere
Tangents on importextractGLTFRaws in src/assets/meshImport.cpp
Vertex layoutVertex in include/rasm/mesh.h; VertexFull in shaders/lib/core.slang
Normal matrixRenderer::writeInstanceRows in src/renderer/gpuScene.cpp; for animated sub-meshes animateMain in shaders/passes/animate.slang; read through getNormalMatrix in shaders/lib/core.slang
Posed normals and tangentsskinMain in shaders/passes/skin.slang
Normal, tangent and sign to world spacevertMain in shaders/passes/rasm.slang
The TBN and the derivative framegetNormalFromMap in shaders/lib/pbr.slang
Normal texture formatloadMaterial in src/assets/material.cpp
Keep these true
  • tangent.w is +1, −1 or 0, and 0 means "no tangent". A new importer must snap w or leave it 0.
  • Normal maps are UNORM: R8G8B8A8_UNORM, or a KTX2 format picked for a data slot. A two-channel format must set MATERIAL_NORMAL_XY, which writeMaterialRow does from the texture's format.
  • The authored frame and the derivative frame must agree: B points toward decreasing v in both (B19).
  • Tangents move with the model matrix, normals with normalMatrix, and the sign with the determinant: in vertMain, in the Animate pass and in the Skin pass alike.

Further reading