{
 "id": "wavelet-all-frequency-relighting",
 "title": "All-Frequency Shadows via Wavelet Lighting",
 "aliases": [
  "wavelet relighting",
  "all-frequency PRT"
 ],
 "domain": "gi",
 "subdomain": "low-frequency lighting",
 "stage": "lighting",
 "realtime": "interactive",
 "platforms": [
  "cpu",
  "desktop-gpu"
 ],
 "year_introduced": 2003,
 "inputs": [
  "mesh",
  "hdr-image"
 ],
 "outputs": [
  "radiance"
 ],
 "style": "photoreal",
 "dynamic": "both",
 "summary": "Stores precomputed visibility per vertex as Haar wavelets of a cubemap and keeps only the largest coefficients of both the light and the visibility, so sharp shadows from high-frequency environment maps are relit in real time.",
 "how_it_works": "[precomputed-radiance-transfer](precomputed-radiance-transfer) with spherical harmonics blurs shadows because low bands cannot represent hard edges. This method swaps SH for **Haar wavelets** on each cubemap face, which are localized in space and so can represent a sharp shadow edge with a few coefficients.\n\nOffline, for each vertex, the visibility times cosine function over the cube map is transformed into the wavelet basis. Only the largest coefficients (a few tens to hundreds out of tens of thousands) are kept; the rest are zeroed. At runtime, the lighting environment is transformed to the same wavelet basis, and a **non-linear approximation** keeps its largest coefficients too. Because wavelets are orthonormal, the lit value of a vertex is the dot product of the two coefficient sets, which only needs the entries that are non-zero in *both*.\n\nNon-linear selection adapts to the data per vertex and per environment, which is why a handful of coefficients captures both a bright small light and its shadows. Diffuse transfer is a vector; glossy transfer uses a matrix expressed in the same basis.",
 "when_to_use": "Choose it for rigid objects relit by high-frequency environment maps (HDR skies with a sun, studio lights) when hard shadows matter and low-order SH looks too soft.\n\nPrefer [precomputed-radiance-transfer](precomputed-radiance-transfer) with SH for compact, cheap, smooth results. Prefer [spherical-harmonics-lighting](spherical-harmonics-lighting) for ambient-only lighting. Prefer [lightmap-baking](lightmap-baking) when the lighting never changes. Prefer [restir-di](restir-di) or ray traced shadows for modern dynamic scenes.",
 "cost": {
  "class": "medium",
  "notes": "Per-vertex cost depends on the number of retained coefficients; precomputation is heavy and storage is large for dense meshes."
 },
 "failure_modes": [
  "Large precomputed data per vertex, and sparse coefficient lists are awkward to evaluate on GPUs",
  "Rigid-only: moving or deforming geometry invalidates the transfer",
  "Truncation artifacts: dropping coefficients leaves visible blockiness in the shadow edges for high compression ratios",
  "Per-vertex sampling limits shadow quality to the mesh density unless stored in textures"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "doi:10.1145/882262.882280",
   "type": "paper",
   "title": "All-frequency shadows using non-linear wavelet lighting approximation",
   "authors": [
    "Ren Ng",
    "Ravi Ramamoorthi",
    "Pat Hanrahan"
   ],
   "year": 2003,
   "venue": "ACM Transactions on Graphics",
   "url": "https://doi.org/10.1145/882262.882280",
   "ids": {
    "doi": "10.1145/882262.882280"
   },
   "license": null,
   "extra": {
    "csl_type": "journal-article"
   },
   "provenance": {
    "title": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/882262.882280",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/882262.882280",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/882262.882280",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/882262.882280",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/882262.882280",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "ng2003frequency"
  }
 ],
 "presets": [
  {
   "id": "wavelet-all-frequency-relighting-live",
   "title": "Wavelet All-Frequency Relighting — low-order SH blurry shadows (left) vs sparse Haar wavelet sharp shadows (right)",
   "runtime": "glsl-fragment",
   "params": {
    "P_ENV_ROT": 0.55,
    "P_WAVELET_K": 0.92,
    "P_SPEC_GLOSS": 0.75,
    "P_SPLIT": 0.48
   },
   "basis": "original",
   "license_note": "MIT — written for this index.",
   "code": "const float P_ENV_ROT = 0.55; // P Env Rotation [0.0, 2.2, 0.05]\nconst float P_WAVELET_K = 0.92; // P Wavelet Terms [0.1, 1.0, 0.02]\nconst float P_SPEC_GLOSS = 0.75; // P Specular Gloss [0.1, 1.0, 0.02]\nconst float P_SPLIT = 0.48; // P Low-SH / Wavelet [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// Sculpted relief bust / multi-lobe architectural knot with deep self-occluding crevices!\nfloat mapSculpt(vec3 p) {\n  // Pedestal floor at y = -0.58\n  float dFloor = p.y + 0.58;\n  // Twin interlocking sculptural rings + central sphere (casts crisp all-frequency self-shadows!)\n  vec3 q1 = p - vec3(0.0, 0.05, 0.0);\n  float dRing1 = length(vec2(length(q1.xz) - 0.54, q1.y)) - 0.13;\n  vec3 q2 = q1;\n  q2.xy = rot2(1.05) * q2.xy;\n  float dRing2 = length(vec2(length(q2.xz) - 0.54, q2.y)) - 0.12;\n  float dCore = length(q1) - 0.28;\n  return min(dFloor, min(dCore, min(dRing1, dRing2)));\n}\n\nvec3 calcNormal(vec3 p) {\n  vec2 e = vec2(0.0025, 0.0);\n  return normalize(vec3(\n    mapSculpt(p + e.xyy) - mapSculpt(p - e.xyy),\n    mapSculpt(p + e.yxy) - mapSculpt(p - e.yxy),\n    mapSculpt(p + e.yyx) - mapSculpt(p - e.yyx)\n  ));\n}\n\n// Soft raymarched directional visibility V(p, w) with selectable angular blur k\nfloat marchVis(vec3 p, vec3 lDir, float kSoft) {\n  float vis = 1.0;\n  float t = 0.04;\n  for (int i = 0; i < 24; i++) {\n    float d = mapSculpt(p + lDir * t);\n    if (d < 0.002) return 0.0;\n    vis = min(vis, kSoft * d / max(t, 0.04));\n    t += clamp(d, 0.025, 0.25);\n    if (t > 2.6) break;\n  }\n  return clamp(vis, 0.0, 1.0);\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 isWavelet = screenX >= split;\n  float rotT = iTime * max(P_ENV_ROT, 0.0);\n  float wavK = clamp(P_WAVELET_K, 0.08, 1.0);\n  float gloss = clamp(P_SPEC_GLOSS, 0.05, 1.0);\n\n  vec3 ro = vec3(0.0, 0.52, 2.28);\n  vec3 rd = normalize(vec3(uv.x, uv.y - 0.18, -1.48));\n\n  // Rotating high-contrast HDR environment with 2 sharp studio key lights + cool dome\n  vec3 lKey1 = normalize(vec3(cos(rotT * 0.65) * 0.78, 0.62, sin(rotT * 0.65) * 0.78));\n  vec3 lKey2 = normalize(vec3(-cos(rotT * 0.65 + 0.7) * 0.72, 0.58, -sin(rotT * 0.65 + 0.7) * 0.72));\n\n  vec3 bg = mix(vec3(0.025, 0.032, 0.055), vec3(0.085, 0.125, 0.210), smoothstep(-0.3, 0.6, rd.y));\n  // Show rotating bright HDR environment light lobes in background\n  bg += vec3(1.8, 1.25, 0.65) * pow(max(dot(rd, lKey1), 0.0), 48.0);\n  bg += vec3(0.55, 1.15, 1.85) * pow(max(dot(rd, lKey2), 0.0), 48.0);\n  vec3 col = bg;\n\n  float t = 0.05;\n  bool hit = false;\n  for (int i = 0; i < 64; i++) {\n    float d = mapSculpt(ro + rd * t);\n    if (d < 0.0025) { hit = true; break; }\n    t += d;\n    if (t > 8.0) break;\n  }\n\n  if (hit) {\n    vec3 p = ro + rd * t;\n    vec3 n = calcNormal(p);\n    bool isFloor = (p.y < -0.56);\n    vec3 alb = isFloor ? vec3(0.58, 0.60, 0.64) : vec3(0.84, 0.80, 0.74);\n\n    if (!isWavelet) {\n      // Left: Order-2 Spherical Harmonics (9 coeffs) heavily band-limits visibility & glossy BRDF -> washed-out blurry shadows!\n      float vis1 = mix(0.45, 0.92, marchVis(p, lKey1, 2.2));\n      float vis2 = mix(0.45, 0.92, marchVis(p, lKey2, 2.2));\n      float shDiff1 = max(dot(n, lKey1) * 0.62 + 0.38, 0.0);\n      float shDiff2 = max(dot(n, lKey2) * 0.62 + 0.38, 0.0);\n      // Low-order SH cannot represent sharp specular highlights\n      float shSpec1 = pow(max(dot(n, normalize(lKey1 - rd)), 0.0), 5.0) * 0.24 * gloss;\n      vec3 shEnv = vec3(0.16, 0.22, 0.32) * (0.5 + 0.5 * n.y);\n      col = alb * (vec3(1.15, 0.84, 0.56) * shDiff1 * vis1 * 0.72\n                 + vec3(0.42, 0.74, 1.15) * shDiff2 * vis2 * 0.58\n                 + shEnv)\n          + vec3(1.0, 0.92, 0.82) * shSpec1;\n    } else {\n      // Right: Non-linear Haar Wavelet PRT preserves top-K sparse wavelets -> razor-sharp all-frequency shadows & glossy highlights!\n      float sharpK = mix(4.0, 26.0, wavK);\n      float vis1 = marchVis(p, lKey1, sharpK);\n      float vis2 = marchVis(p, lKey2, sharpK);\n      float diff1 = max(dot(n, lKey1), 0.0) * vis1;\n      float diff2 = max(dot(n, lKey2), 0.0) * vis2;\n      float specExp = mix(14.0, 88.0, gloss * wavK);\n      float spec1 = pow(max(dot(n, normalize(lKey1 - rd)), 0.0), specExp) * vis1 * (0.45 + 0.85 * gloss);\n      float spec2 = pow(max(dot(n, normalize(lKey2 - rd)), 0.0), specExp) * vis2 * (0.35 + 0.65 * gloss);\n      float ao = clamp(0.35 + 0.65 * mapSculpt(p + n * 0.14) / 0.14, 0.2, 1.0);\n      vec3 amb = vec3(0.10, 0.15, 0.24) * (0.4 + 0.6 * n.y) * ao;\n      col = alb * (vec3(1.45, 0.98, 0.56) * diff1 + vec3(0.45, 0.85, 1.40) * diff2 + amb)\n          + vec3(1.0, 0.92, 0.78) * spec1 * (isFloor ? 0.32 : 0.92)\n          + vec3(0.68, 0.88, 1.0) * spec2 * (isFloor ? 0.24 : 0.72);\n    }\n\n    if (isFloor) col = mix(col, bg, 1.0 - exp(-t * 0.10));\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": "wavelet-all-frequency-relighting",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-30T20:35:58.000Z",
     "renderer": "Headless EGL Tesla T4/PCIe/SSE2, OpenGL 3.30 Core, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.5323,
      "std": 0.2582,
      "min": 0,
      "max": 0.9501
     },
     "image": "/specimens/wavelet-all-frequency-relighting-live.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "precomputed-radiance-transfer",
  "spherical-harmonics-lighting",
  "lightmap-baking"
 ],
 "extends": [
  "precomputed-radiance-transfer"
 ],
 "supersedes": [],
 "combines_with": [],
 "provenance": {
  "prose": "original text written for this index (research draft, editorially reviewed)",
  "sources_verified_at": "2026-11-29T15:51:27.588Z",
  "draft": "data/drafts/gi/wavelet-all-frequency-relighting.json"
 },
 "solves": [
  "sunny hdr sky shadows look too soft",
  "relight rigid objects with detailed environment maps",
  "all-frequency lighting and shadows in real time",
  "precomputed shadows that stay sharp under studio lights",
  "need sharp and soft lighting from the same environment"
 ],
 "authority": "Ng et al., 2003",
 "url": "https://texelatlas.vercel.app/t/wavelet-all-frequency-relighting/",
 "execution_fidelity": "conceptual-diagram",
 "fidelity_label": "Visual Diagram",
 "benchmark_eligible": false,
 "gpu_weight": {
  "weight_x": 27.88,
  "tier": "heavy",
  "p50_ms": 0.9171,
  "p95_ms": 0.945,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 32,
  "preset_id": "wavelet-all-frequency-relighting-live",
  "execution_fidelity": "conceptual-diagram",
  "fidelity_label": "Visual Diagram",
  "benchmark_eligible": false,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 22.16,
   "tier": "heavy",
   "p50_ms": 0.3524,
   "p95_ms": 0.3565,
   "speedup_x": 2.6,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 15.95,
   "tier": "moderate",
   "p50_ms": 0.4593,
   "p95_ms": 0.4647,
   "speedup_x": 2,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}