Algorithm Search⌘K

Techniquecore

Parallax mapping

Also called offset mapping.

Shift the texture coordinates along the view direction by the height read from a height map, to fake the parallax of surface relief.

Parallax mapping moves the texture coordinate of each pixel by an offset that depends on the view direction and on the depth stored in a height map. Points of the relief that lie deep then shift away from the viewer, as they would on a real rough surface, and the texture seems to have depth when the camera moves. Bump mapping and normal mapping change only the normal, so the texture stays fixed on the polygon. Parallax mapping applies one offset, computed from the depth at the starting point. That is cheap, but it is right only where the relief is smooth. Parallax occlusion mapping marches the view ray through the height field to find the true intersection, and displacement mapping moves the geometry itself.

In plain words

Look at a brick wall from the side. The mortar between the bricks lies deeper, so you see it a little behind and to the side of where it would be on a flat picture of the wall.

Parallax mapping fakes this. For each pixel, read how deep the surface is at that spot. Then slide the texture lookup a little along the direction you look in, by an amount that grows with the depth. Deep spots slide more and high spots slide less, so the picture shifts as the camera moves, like a real surface with depth.

It uses one read of the depth per pixel, so it is fast. It is only a guess, and it fails on steep or tall relief.

When it applies

  • The relief is shallow and smooth compared with the size of the texture: bricks with soft edges, cobblestones, bark, worn stone.
  • The surface is seen from angles near the normal, not grazing.
  • There is a normal map for the lighting, and a height or depth map in the same texture space. Often the depth is in the alpha channel of the normal map.
  • The budget allows one more texture read per pixel, but not a loop.
  • It does not apply when the relief is steep or tall, when it must hide itself, or at grazing angles. Use parallax occlusion mapping then.

The template

The work happens in tangent space, as in normal mapping. The inputs are the texture coordinate uv0uv_0 of the pixel, the unit vector vv from the surface toward the viewer in the tangent frame, with vz>0v_z > 0, a depth map mm with values in [0,1][0, 1] (0 at the top, at the polygon), and a scale ss, the depth of the relief in texture units.

  1. Transform the view vector into the tangent frame with the matrix of normal mapping, v=(v⋅T,v⋅B,v⋅N)v = (v \cdot T, v \cdot B, v \cdot N).
  2. Read the depth at the start, d=m(uv0)d = m(uv_0).
  3. Compute the offset. Kaneko and others use uv′=uv0−s d (vx,vy)/vzuv' = uv_0 - s \, d \, (v_x, v_y)/v_z, the point where a ray at depth dd would be.
  4. Or, with Welsh's offset limiting, use uv′=uv0−s d (vx,vy)uv' = uv_0 - s \, d \, (v_x, v_y), which drops the division by vzv_z.
  5. Read every texture of the material, including the normal map, at uv′uv', and shade the pixel as usual.

Step 3 is exact when the depth is the same, dd, at every point between uv0uv_0 and uv′uv', because the view ray then reaches depth dd exactly at uv′uv'. This is a proved property. Where the depth changes along the ray, the error grows with the slope of the relief and with 1/vz1/v_z. Step 4 keeps the offset at most s d≤ss \, d \le s, because ∣(vx,vy)∣≤1|(v_x, v_y)| \le 1. This is also proved, and it hides the worst errors at grazing angles, at the cost of less parallax there. The scale ss, and an optional bias added to dd, are tuning.

Classic examples

A trace. Take uv0=(0.5,0.5)uv_0 = (0.5, 0.5), s=0.1s = 0.1 and a view with vx/vz=−2v_x / v_z = -2 and vy=0v_y = 0, so v=(−2,0,1)/5v = (-2, 0, 1)/\sqrt5. Let the depth along the ray be m=0.3+0.6r2m = 0.3 + 0.6 r^2 at depth rr of the ray, the example of parallax occlusion mapping. The true intersection is at r∗≈0.392r^* \approx 0.392, so the true point has u≈0.5+0.2⋅0.392=0.578u \approx 0.5 + 0.2 \cdot 0.392 = 0.578.

method depth used uu
step 3, one offset d=0.3d = 0.3 0.5+0.2⋅0.3=0.5600.5 + 0.2 \cdot 0.3 = 0.560
step 4, offset limiting d=0.3d = 0.3 0.5+0.0894⋅0.3=0.5270.5 + 0.0894 \cdot 0.3 = 0.527
iterative, 8 steps 0.3920.392 0.5780.578
march of POM 0.4090.409 0.5820.582

The single offset gets about two thirds of the way to the true point. Offset limiting gets less, which is its purpose at grazing angles.

Brick and stone walls. The classic use is a wall with a normal map and a depth map in its alpha channel. The mortar lines shift as the camera moves, which a normal map alone cannot do.

Floors in games. Cobblestone and tile floors use parallax mapping for distant surfaces, and switch to parallax occlusion mapping near the camera, where the errors would show.

Decals. Footprints, bullet holes and cracks get depth from a small depth map at the cost of one read.

Variations

  • Offset limiting. Step 4, from Welsh (2004). The parallax is weaker at grazing angles, but the texture no longer swims away.
  • Iterative parallax mapping. Repeat steps 2 and 3 with the depth at the last offset: rk+1=m(uv0+rkΔ)r_{k+1} = m(uv_0 + r_k \Delta) with Δ=−s(vx,vy)/vz\Delta = -s (v_x, v_y)/v_z. This is a fixed-point iteration for the intersection depth r∗=m(uv0+r∗Δ)r^* = m(uv_0 + r^* \Delta). It converges when the map r↦m(uv0+rΔ)r \mapsto m(uv_0 + r \Delta) has a slope of absolute value below 1 near r∗r^*, by the contraction argument. In the trace the slope is 1.2r∗≈0.471.2 r^* \approx 0.47, and the iteration goes 0.3540.354, 0.3750.375, 0.3850.385, and so on to 0.3920.392.
  • Slope-based offset. Some versions also use the normal from the normal map to tilt the offset, which follows the local slope of the relief.
  • Parallax occlusion mapping. Replace the single read by a march along the ray. See parallax occlusion mapping.

Pitfalls

  • Swimming at grazing angles. As vz→0v_z \to 0 the offset of step 3 grows without a bound, and the texture slides across the surface. Use offset limiting, clamp vzv_z, or fade the effect out.
  • Steep edges. At the edge of a brick the depth jumps, so the offset jumps too, and the texture tears or doubles along the edge.
  • No occlusion. A high point never hides a deep point behind it. Tall relief looks warped instead of solid.
  • Depth and height mixed up. If the map stores height (1 at the top), use 1−h1 - h as the depth. The wrong sign moves the deep parts toward the viewer.
  • Wrong tangent frame. The view vector must be in the same frame as the texture axes, as for normal maps. A wrong sign of the bitangent moves the relief the wrong way.
  • Mip level of the shifted read. The shift happens in the pixel shader, so the derivatives of uv′uv' include the derivatives of the offset. Use the derivatives of uv0uv_0 for the reads at uv′uv' to avoid seams.

Exercises

  1. Show that step 3 gives the exact intersection when mm is constant along the ray.
  2. For a depth map that is a plane, m=a+b rm = a + b \, r along the ray with 0≤b<10 \le b < 1, find the true intersection and the error of step 3 as a function of bb.
  3. Show that the offset of step 4 is at most ss, and compare it with the offset of step 3 for vz=0.1v_z = 0.1.
  4. Run the iterative version on m=0.3+0.6r2m = 0.3 + 0.6 r^2 and count the steps until the error is below 0.0010.001. Compare with the bound from the contraction factor 0.470.47.
  5. Explain why parallax mapping needs the normal map to be read at uv′uv' and not at uv0uv_0.

References

  1. Kaneko and others, Detailed shape representation with parallax mapping, ICAT 2001, the whole paper (the shift of the texture coordinate by the height and the view direction)
  2. Terry Welsh, Parallax mapping with offset limiting, Infiscape, 2004, the offset without the division by the normal component of the view vector (general locator)
  3. Tomas Akenine-Möller, Eric Haines, Naty Hoffman, Angelo Pesce, Michał Iwanicki and Sébastien Hillaire, Real-Time Rendering, fourth edition, CRC Press, 2018, Section 6.8, Parallax Mapping, from page 214

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • three classic examples