{
 "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",
 "subdomain": "wave-optics BRDFs",
 "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",
 "summary": "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.",
 "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.\n\nProjecting 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.\n\nFor **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": "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.\n\nPrefer [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": {
  "class": "cheap",
  "notes": "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."
 },
 "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"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "doi:10.1145/311535.311546",
   "type": "paper",
   "title": "Diffraction shaders",
   "authors": [
    "Jos Stam"
   ],
   "year": 1999,
   "venue": "Proceedings of the 26th annual conference on Computer graphics and interactive techniques  - SIGGRAPH '99",
   "url": "https://doi.org/10.1145/311535.311546",
   "ids": {
    "doi": "10.1145/311535.311546"
   },
   "license": null,
   "extra": {
    "csl_type": "proceedings-article"
   },
   "provenance": {
    "title": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/311535.311546",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "authors": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/311535.311546",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "year": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/311535.311546",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "venue": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/311535.311546",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "url": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/311535.311546",
     "checked_at": "2026-11-30T17:59:51.239Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-30T17:59:51.239Z",
   "title_sim": 1,
   "__bib": "stam1999diffraction"
  },
  {
   "role": "secondary",
   "key": "doi:10.1145/3130800.3130840",
   "type": "paper",
   "title": "Scratch iridescence",
   "authors": [
    "Sebastian Werner",
    "Zdravko Velinov",
    "Wenzel Jakob",
    "Matthias B. Hullin"
   ],
   "year": 2017,
   "venue": "ACM Transactions on Graphics",
   "url": "https://doi.org/10.1145/3130800.3130840",
   "ids": {
    "doi": "10.1145/3130800.3130840"
   },
   "license": null,
   "extra": {
    "csl_type": "journal-article"
   },
   "provenance": {
    "title": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/3130800.3130840",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "authors": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/3130800.3130840",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "year": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/3130800.3130840",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "venue": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/3130800.3130840",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "url": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/3130800.3130840",
     "checked_at": "2026-11-30T17:59:51.239Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-30T17:59:51.239Z",
   "title_sim": 1,
   "__bib": "werner2017scratch"
  },
  {
   "role": "implementation",
   "key": "gh:spite/codevember-2026",
   "type": "code",
   "title": "spite/codevember-2026",
   "description": "",
   "authors": [
    "spite"
   ],
   "year": 2026,
   "venue": "GitHub",
   "url": "https://github.com/spite/codevember-2026",
   "ids": {
    "repo": "spite/codevember-2026"
   },
   "license": "MIT",
   "extra": {
    "stars": 45,
    "stars_checked": "2026-11-30",
    "archived": false,
    "fork": false,
    "pushed_at": "2026-11-30T15:47:17Z",
    "language": "JavaScript",
    "homepage": null,
    "license_name": "MIT License"
   },
   "provenance": {
    "title": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/spite/codevember-2026",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "authors": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/spite/codevember-2026",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "year": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/spite/codevember-2026",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "venue": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/spite/codevember-2026",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "url": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/spite/codevember-2026",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "license": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/spite/codevember-2026",
     "checked_at": "2026-11-30T17:59:51.239Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-30T17:59:51.239Z",
   "title_sim": null,
   "__bib": "spite2026spite"
  }
 ],
 "presets": [
  {
   "id": "tangent-frame-diffraction-grating-live",
   "title": "Tangent-Frame Diffraction Grating — plain brushed metal (left) vs multi-order wave-optical grating hologram (right)",
   "runtime": "glsl-fragment",
   "params": {
    "P_PITCH_UM": 1.45,
    "P_DIFFRACT": 1.2,
    "P_ROT_SPEED": 0.65,
    "P_SPLIT": 0.44
   },
   "basis": "original",
   "license_note": "MIT — written for this index.",
   "code": "const float P_PITCH_UM = 1.45; // P Grating Pitch um [0.8, 2.8, 0.05]\nconst float P_DIFFRACT = 1.20; // P Diffraction Gain [0.2, 2.0, 0.05]\nconst float P_ROT_SPEED = 0.65; // P Disc Spin [0.0, 2.2, 0.05]\nconst float P_SPLIT = 0.44; // P Metal / Diffraction [0.0, 1.0, 0.02]\n\nmat2 rot2(float a) { float c = cos(a), s = sin(a); return mat2(c, -s, s, c); }\n\n// Spectral wavelength-to-RGB response for lambda in micrometers [0.38, 0.72]\nvec3 lambdaToRGB(float lam) {\n  float t = (lam - 0.38) / (0.72 - 0.38);\n  if (t < 0.0 || t > 1.0) return vec3(0.0);\n  vec3 c = clamp(vec3(\n    1.45 - abs(t - 0.76) * 3.8 + 0.32 * exp(-pow((t - 0.12) * 5.2, 2.0)),\n    1.42 - abs(t - 0.48) * 3.4,\n    1.48 - abs(t - 0.18) * 3.6\n  ), 0.0, 1.0);\n  float env = smoothstep(0.0, 0.08, t) * smoothstep(1.0, 0.90, t);\n  return c * c * env;\n}\n\n// Sculpted holographic optical disc + embossed concentric bevel rings\nfloat mapDisc(vec3 p, float spin) {\n  vec3 q = p;\n  q.yz = rot2(0.45) * q.yz;\n  q.xz = rot2(spin * 0.42) * q.xz;\n  float r = length(q.xz);\n  float disc = max(r - 0.88, abs(q.y) - 0.055);\n  float hole = 0.16 - r;\n  float bevel = abs(r - 0.56) - 0.025;\n  float d = max(disc, hole);\n  d -= 0.012 * exp(-bevel * bevel * 900.0);\n  return d * 0.85;\n}\n\nvec3 calcNormal(vec3 p, float spin) {\n  vec2 e = vec2(0.002, 0.0);\n  return normalize(vec3(\n    mapDisc(p + e.xyy, spin) - mapDisc(p - e.xyy, spin),\n    mapDisc(p + e.yxy, spin) - mapDisc(p - e.yxy, spin),\n    mapDisc(p + e.yyx, spin) - mapDisc(p - e.yyx, spin)\n  ));\n}\n\nvec3 aces(vec3 x) {\n  x = max(x, 0.0);\n  return (x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14);\n}\n\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord) {\n  vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;\n  float screenX = fragCoord.x / max(iResolution.x, 1.0);\n  float split = clamp(P_SPLIT, 0.0, 1.0);\n  bool isDiff = screenX >= split;\n  float pitch = clamp(P_PITCH_UM, 0.75, 3.0);\n  float gain = clamp(P_DIFFRACT, 0.1, 2.2);\n  float spin = iTime * max(P_ROT_SPEED, 0.0);\n\n  vec3 ro = vec3(0.0, 0.15, 2.22);\n  vec3 rd = normalize(vec3(uv, -1.52));\n  vec3 col = mix(vec3(0.016, 0.020, 0.034), vec3(0.048, 0.058, 0.092), smoothstep(-0.55, 0.55, uv.y));\n\n  float t = 0.0;\n  bool hit = false;\n  for (int i = 0; i < 56; i++) {\n    float d = mapDisc(ro + rd * t, spin);\n    if (d < 0.002) { hit = true; break; }\n    t += d;\n    if (t > 5.0) break;\n  }\n\n  if (hit) {\n    vec3 p = ro + rd * t;\n    vec3 n = calcNormal(p, spin);\n    vec3 q = p;\n    q.yz = rot2(0.45) * q.yz;\n    q.xz = rot2(spin * 0.42) * q.xz;\n\n    // Radial tangent frame in disc space (concentric circular optical grooves + holographic sector modulation)\n    vec3 radObj = normalize(vec3(q.x, 0.0, q.z) + vec3(1e-5, 0.0, 0.0));\n    float sector = floor((atan(q.z, q.x) + 3.14159265) * 4.0 / 6.2831853);\n    float sectorTilt = (length(q.xz) > 0.56) ? (sector * 0.35) : 0.0;\n    vec3 gObj = vec3(\n      radObj.x * cos(sectorTilt) - radObj.z * sin(sectorTilt),\n      0.0,\n      radObj.x * sin(sectorTilt) + radObj.z * cos(sectorTilt)\n    );\n    // Transform groove vector back to world space\n    vec3 gWorld = gObj;\n    gWorld.xz = rot2(-spin * 0.42) * gWorld.xz;\n    gWorld.yz = rot2(-0.45) * gWorld.yz;\n    gWorld = normalize(gWorld - n * dot(gWorld, n) + vec3(1e-5));\n\n    vec3 l1 = normalize(vec3(0.55 * cos(iTime * 0.45), 0.78, 0.62));\n    vec3 l2 = normalize(vec3(-0.62, 0.52, 0.58));\n    vec3 v = -rd;\n\n    // Base metallic silver substrate reflection\n    vec3 h1 = normalize(l1 + v);\n    vec3 h2 = normalize(l2 + v);\n    float specBase = pow(max(dot(n, h1), 0.0), 48.0) + 0.65 * pow(max(dot(n, h2), 0.0), 36.0);\n    float fres = 0.62 + 0.38 * pow(1.0 - max(dot(n, v), 0.0), 4.0);\n    float brush = 0.90 + 0.10 * sin(length(q.xz) * 180.0);\n    vec3 metalCol = vec3(0.42, 0.45, 0.50) * brush * (0.25 + 0.55 * max(dot(n, l1), 0.0))\n                  + vec3(0.95, 0.97, 1.0) * specBase * fres * 0.85;\n\n    if (!isDiff) {\n      col = metalCol;\n    } else {\n      // Multi-order grating equation along tangent groove normal gWorld:\n      // d * |dot(l + v, gWorld)| = |m| * lambda   for integer orders m = 1, 2, 3!\n      float u1 = abs(dot(l1 + v, gWorld));\n      float u2 = abs(dot(l2 + v, gWorld));\n      vec3 diffSum = vec3(0.0);\n      for (int m = 1; m <= 3; m++) {\n        float fm = float(m);\n        float wOrder = 1.0 / (fm * 0.85 + 0.15);\n        float lam1 = (pitch * u1) / fm;\n        float lam2 = (pitch * u2) / fm;\n        // Spectral coherence window around each diffraction order\n        diffSum += (lambdaToRGB(lam1) + 0.65 * lambdaToRGB(lam2)) * wOrder;\n      }\n      // Micro-groove envelope fade at grazing\n      float vis = smoothstep(0.05, 0.25, max(dot(n, v), 0.0));\n      col = metalCol * 0.55 + diffSum * gain * vis * 1.15;\n    }\n  }\n\n  float sep = smoothstep(0.0035, 0.0008, abs(screenX - split));\n  col = mix(col, vec3(0.95, 0.97, 1.0), sep);\n\n  fragColor = vec4(pow(clamp(aces(col), 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);\n}",
   "execution_fidelity": "conceptual-diagram",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-30",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "tangent-frame-diffraction-grating",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-30T20:35:56.000Z",
     "renderer": "Headless EGL Tesla T4/PCIe/SSE2, OpenGL 3.30 Core, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.556,
      "std": 0.2589,
      "min": 0.1264,
      "max": 0.9524
     },
     "image": "/specimens/tangent-frame-diffraction-grating-live.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "thin-film-iridescence",
  "anisotropic-ggx",
  "real-time-glints",
  "spectral-upsampling-sigmoid"
 ],
 "extends": [],
 "supersedes": [],
 "combines_with": [
  {
   "id": "normal-mapping-mikktspace",
   "why": "Provides consistent orthonormal tangent frames across curved meshes so grating grooves orient without seam discontinuities."
  },
  {
   "id": "agx-tonemapping",
   "why": "Compresses highly saturated spectral diffraction orders into display gamut gracefully without harsh hue clipping."
  }
 ],
 "provenance": {
  "prose": "original text written for this index (research draft, editorially reviewed)",
  "sources_verified_at": "2026-11-30T17:59:51.239Z",
  "draft": "data/drafts/materials/tangent-frame-diffraction-grating.json"
 },
 "solves": [
  "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"
 ],
 "authority": "Stam, 1999",
 "url": "https://texelatlas.vercel.app/t/tangent-frame-diffraction-grating/",
 "execution_fidelity": "conceptual-diagram",
 "fidelity_label": "Visual Diagram",
 "benchmark_eligible": false,
 "gpu_weight": {
  "weight_x": 10.63,
  "tier": "moderate",
  "p50_ms": 0.3496,
  "p95_ms": 0.356,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 32,
  "preset_id": "tangent-frame-diffraction-grating-live",
  "execution_fidelity": "conceptual-diagram",
  "fidelity_label": "Visual Diagram",
  "benchmark_eligible": false,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 8.33,
   "tier": "moderate",
   "p50_ms": 0.1325,
   "p95_ms": 0.1331,
   "speedup_x": 2.64,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 7.21,
   "tier": "moderate",
   "p50_ms": 0.2077,
   "p95_ms": 0.2084,
   "speedup_x": 1.68,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}