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.
A normal map is usually baked:
- Detailed meshan artist builds it, with millions of triangles
- Bakea tool records the normals of the detailed mesh
- 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
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:
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.
Normalize after reading
8-bit rounding, filtering between texels, and mipmaps all shorten the vector a little.
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.
3Tangent space
In which coordinate system are the stored normals? There are two choices.
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.
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:
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
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:
Try the sum. The picture is the side view again (y = 0), with a surface that can tilt.
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.
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)
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?
All three vectors
Storing all three of T, B, N per vertex costs 9 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.
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.
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.
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.
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:
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).
The right door: det M > 0
The frame keeps its handedness. w stays as it is.
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):
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.
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.
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²))
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
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
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
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.
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
| Part | This page | RASM |
|---|---|---|
| Texture format | UNORM, maybe two channels | An 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. |
| Strength | multiply x, y | The same, with the glTF scale (default 1). |
| Where tangents come from | Lengyel or MikkTSpace on load | Only 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. |
| Bitangent | w · cross(N, T) | The same, with the sign of w. |
| Gram–Schmidt | yes | Yes, in the fragment shader. A tangent that becomes 0 falls back to the derivative frame. |
| Normal transform | inverse-transpose | normalMatrix = 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 transform | M, and w · sign(det M) | The same, in vertMain; the sign is the handedness output. |
| Derivative frame | section 8 | The 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 filtering | renormalize, Toksvig | The 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
| What | Where |
|---|---|
| Tangents on import | extractGLTFRaws in src/assets/meshImport.cpp |
| Vertex layout | Vertex in include/rasm/mesh.h; VertexFull in shaders/lib/core.slang |
| Normal matrix | Renderer::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 tangents | skinMain in shaders/passes/skin.slang |
| Normal, tangent and sign to world space | vertMain in shaders/passes/rasm.slang |
| The TBN and the derivative frame | getNormalFromMap in shaders/lib/pbr.slang |
| Normal texture format | loadMaterial in src/assets/material.cpp |
tangent.wis +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 setMATERIAL_NORMAL_XY, whichwriteMaterialRowdoes 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: invertMain, in theAnimatepass and in theSkinpass alike.
Further reading
- Eric Lengyel, Computing Tangent Space Basis Vectors for an Arbitrary Mesh (2001), and his book Mathematics for 3D Game Programming and Computer Graphics.
- Morten S. Mikkelsen, Simulation of Wrinkled Surfaces Revisited, master's thesis (2008): MikkTSpace.
- Morten S. Mikkelsen, Bump Mapping Unparametrized Surfaces on the GPU, Journal of Graphics, GPU, and Game Tools (2010).
- Christian Schüler, Normal Mapping Without Precomputed Tangents, in ShaderX5 (2006), and its 2013 follow-up.
- Michael Toksvig, Mipmapping Normal Maps, Journal of Graphics Tools (2005).
- The Khronos glTF 2.0 specification: the TANGENT attribute and normalTexture.
- Tomas Akenine-Möller and others, Real-Time Rendering, 4th edition: the chapter on texturing (bump and normal mapping).