---
id: "tangent-frame-diffraction-grating"
title: "Multi-Order Tangent-Frame Diffraction Grating & Spectral Replay Hologram"
aliases: [diffraction shader, grating iridescence, spectral replay hologram, Stam diffraction BRDF]
domain: "materials"
stage: "material"
realtime: "realtime"
platforms: [webgl2, webgpu, desktop-gpu, mobile-gpu, unity, unreal, blender]
year_introduced: 1999
inputs: [normals, uv-atlas, material-params, gbuffer, radiance]
outputs: [radiance]
style: "both"
dynamic: "dynamic"
cost: "cheap"
authority: "Stam, 1999"
url: "https://texelatlas.vercel.app/t/tangent-frame-diffraction-grating/"
sources_verified_at: "2026-11-30"
---
# Multi-Order Tangent-Frame Diffraction Grating & Spectral Replay Hologram (Stam, 1999)

> Evaluates wave-optical grating diffraction in a surface tangent frame, summing integer diffraction orders into spectral RGB lobes to render holographic foils, optical discs, and white-light replay holograms in real time.

## Reach for it when

- render rainbow sheen on cds dvds brushed vinyl or insect wings
- micro-grooved metal surfaces lack angle-dependent spectral iridescence
- thin film shader does not capture periodic groove diffraction orders
- align anisotropic rainbow reflection bands along mesh uv tangents
- simulate holographic foil and diffraction stickers in real time

## How it works

Periodic microscopic grooves or recorded interference fringes scatter incident light into angle-dependent spectral orders rather than a single mirror lobe. At each shaded point, the shader builds a local **tangent frame** `(T, B, N)` whose tangent aligns with the grating groove direction—derived either from UV derivatives, polar coordinates on discs, or a brushed-foil anisotropy map.

Projecting the half-vector `H = L + V` (the optical path-difference vector between light and view directions) onto the groove axis gives the grating phase offset `u = dot(H, T)`. For a grating pitch `d` and non-zero integer order `m`, constructive interference selects the wavelength `lambda = d * |u| / |m|`. Summing visible orders `m = ±1, ±2, …` weighted by groove-profile envelope falloff and converting each wavelength to linear RGB (via CIE color-matching curves or spectral basis fits) produces sharp rainbow dispersion bands.

For **white-light reflection holograms** (such as dichromated gelatin or foil cards), a pre-baked depth or multi-view parallax field is sampled along the refracted view ray inside the plate and modulated by this grating dispersion term, replaying a floating 3D motif whose hue shifts continuously as the card tilts.

## When to use it

Choose it when shading periodic micro-structured surfaces—optical media, embossed holographic trading cards, security foils, or diffraction-etched metals—where dispersion depends on groove orientation and viewing angle.

Prefer [thin-film-iridescence](thin-film-iridescence) when interference comes from a smooth dielectric coating thickness (soap bubbles, oil slicks, oxide layers) without directional grooves. Prefer [anisotropic-ggx](anisotropic-ggx) when brushed metal needs elongated highlights without rainbow spectral splitting. Prefer [real-time-glints](real-time-glints) when discrete stochastic micro-facets dominate rather than coherent periodic fringes.

## Cost

cheap — Evaluating the grating condition across a handful of integer diffraction orders and converting wavelengths to RGB takes tens of ALU instructions per light sample, staying well under 1 ms at 1080p on mid-range desktop GPUs.
Measured GPU Weight: 10.63× baseline (moderate) — 0.3496 ms median / 0.356 ms p95 on Tesla T4/PCIe/SSE2 @ 1920x1080 (opengl-timer-query).
Ada L4 scaling: 8.33× baseline (moderate) — 0.1325 ms median (2.64× faster on L4).
Ampere A100 scaling: 7.21× baseline (moderate) — 0.2077 ms median (1.68× faster on A100).

## Failure modes

- Aliasing and shimmering when UV seams or high-frequency tangent maps flip groove orientation across adjacent pixels
- Over-saturated out-of-gamut spectral spikes if narrow-band wavelengths are summed without bandwidth integration or gamut-aware tonemapping
- Zero-order specular energy and diffracted orders can violate energy conservation unless balanced against the base microfacet BRDF
- Point-sampled grating pitch assumes strictly periodic grooves and misses broadband blur caused by irregular groove spacing

## Dataflow

- Inputs: normals, uv-atlas, material-params, gbuffer, radiance
- Outputs: radiance
- Platforms: webgl2, webgpu, desktop-gpu, mobile-gpu, unity, unreal, blender

## Presets

### Tangent-Frame Diffraction Grating — plain brushed metal (left) vs multi-order wave-optical grating hologram (right) (glsl-fragment; rendered 2026-11-30)

```json
{
  "P_PITCH_UM": 1.45,
  "P_DIFFRACT": 1.2,
  "P_ROT_SPEED": 0.65,
  "P_SPLIT": 0.44
}
```

```glsl
const float P_PITCH_UM = 1.45; // P Grating Pitch um [0.8, 2.8, 0.05]
const float P_DIFFRACT = 1.20; // P Diffraction Gain [0.2, 2.0, 0.05]
const float P_ROT_SPEED = 0.65; // P Disc Spin [0.0, 2.2, 0.05]
const float P_SPLIT = 0.44; // P Metal / Diffraction [0.0, 1.0, 0.02]

mat2 rot2(float a) { float c = cos(a), s = sin(a); return mat2(c, -s, s, c); }

// Spectral wavelength-to-RGB response for lambda in micrometers [0.38, 0.72]
vec3 lambdaToRGB(float lam) {
  float t = (lam - 0.38) / (0.72 - 0.38);
  if (t < 0.0 || t > 1.0) return vec3(0.0);
  vec3 c = clamp(vec3(
    1.45 - abs(t - 0.76) * 3.8 + 0.32 * exp(-pow((t - 0.12) * 5.2, 2.0)),
    1.42 - abs(t - 0.48) * 3.4,
    1.48 - abs(t - 0.18) * 3.6
  ), 0.0, 1.0);
  float env = smoothstep(0.0, 0.08, t) * smoothstep(1.0, 0.90, t);
  return c * c * env;
}

// Sculpted holographic optical disc + embossed concentric bevel rings
float mapDisc(vec3 p, float spin) {
  vec3 q = p;
  q.yz = rot2(0.45) * q.yz;
  q.xz = rot2(spin * 0.42) * q.xz;
  float r = length(q.xz);
  float disc = max(r - 0.88, abs(q.y) - 0.055);
  float hole = 0.16 - r;
  float bevel = abs(r - 0.56) - 0.025;
  float d = max(disc, hole);
  d -= 0.012 * exp(-bevel * bevel * 900.0);
  return d * 0.85;
}

vec3 calcNormal(vec3 p, float spin) {
  vec2 e = vec2(0.002, 0.0);
  return normalize(vec3(
    mapDisc(p + e.xyy, spin) - mapDisc(p - e.xyy, spin),
    mapDisc(p + e.yxy, spin) - mapDisc(p - e.yxy, spin),
    mapDisc(p + e.yyx, spin) - mapDisc(p - e.yyx, spin)
  ));
}

vec3 aces(vec3 x) {
  x = max(x, 0.0);
  return (x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14);
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
  vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
  float screenX = fragCoord.x / max(iResolution.x, 1.0);
  float split = clamp(P_SPLIT, 0.0, 1.0);
  bool isDiff = screenX >= split;
  float pitch = clamp(P_PITCH_UM, 0.75, 3.0);
  float gain = clamp(P_DIFFRACT, 0.1, 2.2);
  float spin = iTime * max(P_ROT_SPEED, 0.0);

  vec3 ro = vec3(0.0, 0.15, 2.22);
  vec3 rd = normalize(vec3(uv, -1.52));
  vec3 col = mix(vec3(0.016, 0.020, 0.034), vec3(0.048, 0.058, 0.092), smoothstep(-0.55, 0.55, uv.y));

  float t = 0.0;
  bool hit = false;
  for (int i = 0; i < 56; i++) {
    float d = mapDisc(ro + rd * t, spin);
    if (d < 0.002) { hit = true; break; }
    t += d;
    if (t > 5.0) break;
  }

  if (hit) {
    vec3 p = ro + rd * t;
    vec3 n = calcNormal(p, spin);
    vec3 q = p;
    q.yz = rot2(0.45) * q.yz;
    q.xz = rot2(spin * 0.42) * q.xz;

    // Radial tangent frame in disc space (concentric circular optical grooves + holographic sector modulation)
    vec3 radObj = normalize(vec3(q.x, 0.0, q.z) + vec3(1e-5, 0.0, 0.0));
    float sector = floor((atan(q.z, q.x) + 3.14159265) * 4.0 / 6.2831853);
    float sectorTilt = (length(q.xz) > 0.56) ? (sector * 0.35) : 0.0;
    vec3 gObj = vec3(
      radObj.x * cos(sectorTilt) - radObj.z * sin(sectorTilt),
      0.0,
      radObj.x * sin(sectorTilt) + radObj.z * cos(sectorTilt)
    );
    // Transform groove vector back to world space
    vec3 gWorld = gObj;
    gWorld.xz = rot2(-spin * 0.42) * gWorld.xz;
    gWorld.yz = rot2(-0.45) * gWorld.yz;
    gWorld = normalize(gWorld - n * dot(gWorld, n) + vec3(1e-5));

    vec3 l1 = normalize(vec3(0.55 * cos(iTime * 0.45), 0.78, 0.62));
    vec3 l2 = normalize(vec3(-0.62, 0.52, 0.58));
    vec3 v = -rd;

    // Base metallic silver substrate reflection
    vec3 h1 = normalize(l1 + v);
    vec3 h2 = normalize(l2 + v);
    float specBase = pow(max(dot(n, h1), 0.0), 48.0) + 0.65 * pow(max(dot(n, h2), 0.0), 36.0);
    float fres = 0.62 + 0.38 * pow(1.0 - max(dot(n, v), 0.0), 4.0);
    float brush = 0.90 + 0.10 * sin(length(q.xz) * 180.0);
    vec3 metalCol = vec3(0.42, 0.45, 0.50) * brush * (0.25 + 0.55 * max(dot(n, l1), 0.0))
                  + vec3(0.95, 0.97, 1.0) * specBase * fres * 0.85;

    if (!isDiff) {
      col = metalCol;
    } else {
      // Multi-order grating equation along tangent groove normal gWorld:
      // d * |dot(l + v, gWorld)| = |m| * lambda   for integer orders m = 1, 2, 3!
      float u1 = abs(dot(l1 + v, gWorld));
      float u2 = abs(dot(l2 + v, gWorld));
      vec3 diffSum = vec3(0.0);
      for (int m = 1; m <= 3; m++) {
        float fm = float(m);
        float wOrder = 1.0 / (fm * 0.85 + 0.15);
        float lam1 = (pitch * u1) / fm;
        float lam2 = (pitch * u2) / fm;
        // Spectral coherence window around each diffraction order
        diffSum += (lambdaToRGB(lam1) + 0.65 * lambdaToRGB(lam2)) * wOrder;
      }
      // Micro-groove envelope fade at grazing
      float vis = smoothstep(0.05, 0.25, max(dot(n, v), 0.0));
      col = metalCol * 0.55 + diffSum * gain * vis * 1.15;
    }
  }

  float sep = smoothstep(0.0035, 0.0008, abs(screenX - split));
  col = mix(col, vec3(0.95, 0.97, 1.0), sep);

  fragColor = vec4(pow(clamp(aces(col), 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);
}
```

Attribution: MIT — written for this index.

## Combines with

- [Normal mapping with MikkTSpace](https://texelatlas.vercel.app/t/normal-mapping-mikktspace.md): Provides consistent orthonormal tangent frames across curved meshes so grating grooves orient without seam discontinuities.
- [AgX](https://texelatlas.vercel.app/t/agx-tonemapping.md): Compresses highly saturated spectral diffraction orders into display gamut gracefully without harsh hue clipping.

## Sources

1. [primary] Jos Stam (1999) Diffraction shaders. Proceedings of the 26th annual conference on Computer graphics and interactive techniques  - SIGGRAPH '99. https://doi.org/10.1145/311535.311546 (verified via crossref on 2026-11-30)
2. [secondary] Sebastian Werner, Zdravko Velinov, Wenzel Jakob, Matthias B. Hullin (2017) Scratch iridescence. ACM Transactions on Graphics. https://doi.org/10.1145/3130800.3130840 (verified via crossref on 2026-11-30)
3. [implementation] spite (2026) spite/codevember-2026. https://github.com/spite/codevember-2026 License: MIT. (verified via github on 2026-11-30)

## Cite this entry

Texel. "Multi-Order Tangent-Frame Diffraction Grating & Spectral Replay Hologram." https://texelatlas.vercel.app/t/tangent-frame-diffraction-grating/ (accessed 2026-12-01).
