Multi-Order Tangent-Frame Diffraction Grating & Spectral Replay Hologram
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.
Also known as diffraction shader, grating iridescence, spectral replay hologram, Stam diffraction BRDF
In the family Materials, BRDFs & texturing, wave-optics BRDFs
Where it runs
Built inOfficial add-onPluginNone foundNot researched
| Engine | Support | How |
|---|---|---|
| three.js r187 | None found | |
| Babylon.js 9.38.0 | None found | |
| Godot 4.7 | None found | |
| Unity | None found | |
| Unreal 5.7 | None found | |
| Blender 5.2 | None found | |
| WebGPU W3C | None found | |
| Metal | None found |
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 choose it
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
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 when interference comes from a smooth dielectric coating thickness (soap bubbles, oil slicks, oxide layers) without directional grooves. Prefer Anisotropic GGX when brushed metal needs elongated highlights without rainbow spectral splitting. Prefer Real-time glints (distributed binomial laws) when discrete stochastic micro-facets dominate rather than coherent periodic fringes.
Field marks
- 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
Presets
Tangent-Frame Diffraction Grating — plain brushed metal (left) vs multi-order wave-optical grating hologram (right)
{
"P_PITCH_UM": 1.45,
"P_DIFFRACT": 1.2,
"P_ROT_SPEED": 0.65,
"P_SPLIT": 0.44
}Show GLSL (132 lines)
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.
Measured GPU weight & timing
| Preset | GPU | Weight / p50 / p95 | Method |
|---|---|---|---|
| tangent-frame-diffraction-grating-live | Tesla T4 1920x1080 | 10.6× baseline (moderate) · 0.350 ms / 0.356 ms | opengl-timer-query · 32 frames · GPU Weight Atlas · Colab T4 sweep |
| tangent-frame-diffraction-grating-live | NVIDIA L4 1920x1080 | 8.33× baseline (moderate) · 0.133 ms / 0.133 ms · 2.6× faster on L4 | opengl-timer-query · Cross-architecture validation |
| tangent-frame-diffraction-grating-live | NVIDIA A100-SXM4-40GB 1920x1080 | 7.21× baseline (moderate) · 0.208 ms / 0.208 ms · 1.7× faster on A100 | opengl-timer-query · Cross-architecture validation |
Lineage
From the citation graph (Crossref reference lists): cited by Thin-film iridescence.
Crosses
- Normal mapping with MikkTSpace: Provides consistent orthonormal tangent frames across curved meshes so grating grooves orient without seam discontinuities.
- AgX: Compresses highly saturated spectral diffraction orders into display gamut gracefully without harsh hue clipping.
Sources
- primaryJos Stam (1999). Diffraction shaders. Proceedings of the 26th annual conference on Computer graphics and interactive techniques - SIGGRAPH '99.Verified via crossref · 2026-11-30doi:10.1145/311535.311546
- secondarySebastian Werner, Zdravko Velinov, Wenzel Jakob, Matthias B. Hullin (2017). Scratch iridescence. ACM Transactions on Graphics.Verified via crossref · 2026-11-30doi:10.1145/3130800.3130840