{
 "id": "spherical-harmonics-lighting",
 "title": "Spherical-Harmonic Lighting (Irradiance Environment Maps)",
 "aliases": [
  "SH lighting",
  "SH irradiance",
  "L2 SH"
 ],
 "domain": "gi",
 "subdomain": "low-frequency lighting",
 "stage": "lighting",
 "realtime": "realtime",
 "platforms": [
  "webgl2",
  "webgpu",
  "desktop-gpu",
  "mobile-gpu",
  "unity",
  "unreal",
  "godot"
 ],
 "year_introduced": 2001,
 "inputs": [
  "hdr-image"
 ],
 "outputs": [
  "irradiance-probes"
 ],
 "style": "photoreal",
 "dynamic": "both",
 "summary": "Projects an environment's radiance onto nine spherical-harmonic coefficients per color channel, from which diffuse irradiance for any surface normal is evaluated with a tiny quadratic polynomial.",
 "how_it_works": "Diffuse reflection is a cosine-weighted average of incoming light, and that cosine kernel is smooth. Its spectrum falls off quickly, with about 99 percent of the energy in the first three bands (order 0, 1, 2). So instead of storing a cubemap and convolving it per pixel, you project the radiance onto **spherical harmonics**: nine coefficients `L_lm` per color channel (27 floats).\n\nProjection is a one-time integral over the sphere, done by Monte Carlo sampling or by summing a cubemap with solid-angle weights. Irradiance for a normal `n` is then a quadratic form in `n`: the zeroth band contributes a constant, the first band a linear term, the second a quadratic one, with fixed band attenuation constants taken from the cosine lobe's SH coefficients. In a shader this is a dozen multiply-adds.\n\nBecause the representation is linear, **blending probes or dynamic lights is just adding coefficient vectors**, and rotations are matrix operations on coefficients. Windowing (a gentle filter that reduces ringing) is a common cleanup when the source has high dynamic range.",
 "when_to_use": "Choose it for diffuse ambient light from a sky or probe, for dynamic objects inside [irradiance-volumes](irradiance-volumes), and as the basis for many baked-lighting formats.\n\nPrefer [split-sum-ibl](split-sum-ibl) alongside it for the specular half of image-based lighting; SH cannot represent a sharp reflection. Prefer [precomputed-radiance-transfer](precomputed-radiance-transfer) when you also want self-shadowing and interreflection of the object itself. Prefer a prefiltered diffuse cubemap when you want exact cosine convolution without L2 ringing.",
 "cost": {
  "class": "cheap",
  "notes": "A few dozen ALU ops per pixel and 27 floats of storage per probe."
 },
 "failure_modes": [
  "Ringing: strong, small lights produce negative irradiance or bright halos on the opposite side (visible with a sun in the environment)",
  "Cannot represent sharp directional lights or shadows; the result is a soft ambient wash",
  "L1-only variants lose contrast compared with a true cosine convolution",
  "Banding or color shifts when HDR values are clamped before projection, since peaks dominate the coefficients"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "doi:10.1145/383259.383317",
   "type": "paper",
   "title": "An efficient representation for irradiance environment maps",
   "authors": [
    "Ravi Ramamoorthi",
    "Pat Hanrahan"
   ],
   "year": 2001,
   "venue": "Proceedings of the 28th annual conference on Computer graphics and interactive techniques",
   "url": "https://doi.org/10.1145/383259.383317",
   "ids": {
    "doi": "10.1145/383259.383317"
   },
   "license": null,
   "extra": {
    "csl_type": "proceedings-article"
   },
   "provenance": {
    "title": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/383259.383317",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/383259.383317",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/383259.383317",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/383259.383317",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/383259.383317",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "ramamoorthi2001efficient"
  },
  {
   "role": "tutorial",
   "key": "url:https://www.ppsloan.org/publications/StupidSH36.pdf",
   "type": "tutorial",
   "title": "Stupid Spherical Harmonics (SH) Tricks",
   "authors": [
    "Peter-Pike Sloan"
   ],
   "year": null,
   "venue": "GDC 2008",
   "url": "https://www.ppsloan.org/publications/StupidSH36.pdf",
   "ids": {},
   "license": null,
   "extra": {
    "http_status": 200,
    "live": true,
    "archive": {
     "url": "http://web.archive.org/web/20261022023943/http://www.ppsloan.org/publications/StupidSH36.pdf",
     "timestamp": "20261022023943"
    },
    "page_title": null,
    "host": "ppsloan.org",
    "title_confirmed_by_page": false,
    "title_unconfirmed": false
   },
   "provenance": {
    "title": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "http",
     "endpoint": "GET https://www.ppsloan.org/publications/StupidSH36.pdf → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": null,
   "__bib": "sloanstupid"
  }
 ],
 "presets": [
  {
   "id": "sh-l2-irradiance-comparison",
   "title": "L2 SH irradiance vs brute-force convolution",
   "runtime": "glsl-fragment",
   "params": {
    "samples": 192,
    "albedo": 0.8
   },
   "code": "// GLSL ES 3.0, Shadertoy-style. Left: brute-force cosine-convolved env. Right: order-2 SH irradiance.\nconst int N = 192;            // projection samples\nconst float ALBEDO = 0.8;\nvec3 env(vec3 d){\n  vec3 sky = mix(vec3(0.55,0.7,1.0), vec3(0.15,0.3,0.7), clamp(d.y,0.0,1.0));\n  vec3 gnd = vec3(0.22,0.16,0.1);\n  vec3 c = d.y > 0.0 ? sky : gnd;\n  vec3 sun = normalize(vec3(0.6,0.5,0.3));\n  c += vec3(9.0,7.0,4.5) * pow(max(dot(d,sun),0.0), 48.0);\n  return c;\n}\nvec3 fib(int i){\n  float y = 1.0 - 2.0*(float(i)+0.5)/float(N);\n  float r = sqrt(1.0-y*y), a = 2.399963*float(i);\n  return vec3(r*cos(a), y, r*sin(a));\n}\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord){\n  vec2 uv = (fragCoord - 0.5*iResolution.xy) / iResolution.y;\n  vec3 bg = mix(vec3(0.12), vec3(0.2), uv.y+0.5);\n  float side = uv.x < 0.0 ? -1.0 : 1.0;\n  vec2 q = uv - vec2(0.25*side, 0.0);\n  float rad = 0.22;\n  if (dot(q,q) > rad*rad) { fragColor = vec4(bg,1.0); return; }\n  vec2 p = q/rad;\n  vec3 n = vec3(p.x, p.y, sqrt(1.0-dot(p,p)));\n  float ca = cos(iTime*0.5), sa = sin(iTime*0.5);\n  n = vec3(ca*n.x + sa*n.z, n.y, -sa*n.x + ca*n.z);\n  vec3 Lc[9]; for (int k=0;k<9;k++) Lc[k] = vec3(0.0);\n  vec3 ref = vec3(0.0);\n  for (int i=0;i<N;i++){\n    vec3 d = fib(i); vec3 L = env(d);\n    ref += L*max(dot(n,d),0.0);\n    Lc[0] += L*0.282095;\n    Lc[1] += L*0.488603*d.y; Lc[2] += L*0.488603*d.z; Lc[3] += L*0.488603*d.x;\n    Lc[4] += L*1.092548*d.x*d.y; Lc[5] += L*1.092548*d.y*d.z;\n    Lc[6] += L*0.315392*(3.0*d.z*d.z-1.0);\n    Lc[7] += L*1.092548*d.x*d.z; Lc[8] += L*0.546274*(d.x*d.x-d.y*d.y);\n  }\n  float w = 4.0*3.14159265/float(N);\n  for (int k=0;k<9;k++) Lc[k] *= w;\n  ref *= w/3.14159265;\n  vec3 E = 0.429043*Lc[8]*(n.x*n.x-n.y*n.y) + 0.743125*Lc[6]*n.z*n.z + 0.886227*Lc[0] - 0.247708*Lc[6]\n         + 2.0*0.429043*(Lc[4]*n.x*n.y + Lc[7]*n.x*n.z + Lc[5]*n.y*n.z)\n         + 2.0*0.511664*(Lc[3]*n.x + Lc[1]*n.y + Lc[2]*n.z);\n  vec3 sh = max(E, 0.0)/3.14159265;\n  vec3 col = ALBEDO * (side < 0.0 ? ref : sh);\n  fragColor = vec4(pow(col/(1.0+col), vec3(1.0/2.2)), 1.0);\n}",
   "basis": "0",
   "license_note": "written for this index; SH constants from the Ramamoorthi-Hanrahan irradiance formula",
   "execution_fidelity": "faithful-pass",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-29",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "spherical-harmonics-lighting",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-29T15:51:50.471Z",
     "renderer": "Chromium 145.0.7632.6 headless, WebGL2 via ANGLE/SwiftShader, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.2791,
      "std": 0.185,
      "unique_colors": 64
     },
     "image": "/specimens/sh-l2-irradiance-comparison.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "irradiance-volumes",
  "precomputed-radiance-transfer",
  "split-sum-ibl",
  "wavelet-all-frequency-relighting"
 ],
 "extends": [],
 "supersedes": [],
 "combines_with": [
  {
   "id": "split-sum-ibl",
   "why": "SH supplies the diffuse term and the split-sum prefiltered map supplies the specular term of environment lighting."
  }
 ],
 "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/spherical-harmonics-lighting.json"
 },
 "solves": [
  "need cheap diffuse ambient light from a sky",
  "environment lighting stored in a few numbers",
  "dynamic objects need soft lighting from probes",
  "diffuse irradiance without sampling a cubemap many times",
  "compact baked lighting representation"
 ],
 "authority": "Ramamoorthi & Hanrahan, 2001",
 "url": "https://texelatlas.vercel.app/t/spherical-harmonics-lighting/",
 "execution_fidelity": "faithful-pass",
 "fidelity_label": "Faithful Pass",
 "benchmark_eligible": true,
 "gpu_weight": {
  "weight_x": 69.22,
  "tier": "extreme",
  "p50_ms": 2.2773,
  "p95_ms": 2.4565,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 40,
  "preset_id": "sh-l2-irradiance-comparison",
  "execution_fidelity": "faithful-pass",
  "fidelity_label": "Faithful Pass",
  "benchmark_eligible": true,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 52.75,
   "tier": "heavy",
   "p50_ms": 0.8387,
   "p95_ms": 0.8689,
   "speedup_x": 2.72,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 48.78,
   "tier": "heavy",
   "p50_ms": 1.4049,
   "p95_ms": 1.4285,
   "speedup_x": 1.62,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}