Photon Mapping
Shoots photons from lights into the scene, stores where they land, then estimates indirect and caustic radiance at shading points by looking at nearby photons. Biased but smooth and excellent for caustics.
Also known as PM
In the family Light transport & global illumination, Density-estimation light transport
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
Photon mapping splits rendering into two passes. In the photon pass, packets of flux are emitted from light sources and scattered through the scene by Russian roulette; each time a photon lands on a diffuse surface, its position, incoming direction and power are stored in a spatial structure (a kd-tree or a hash grid). A dedicated caustic map holds photons that went through specular bounces; a coarser global map holds the rest.
In the render pass, rays from the camera hit surfaces. At a hit, the renderer gathers the k nearest photons (or those within a radius), and divides their summed flux by the area of the search disk. That is a kernel density estimate of irradiance, multiplied by the BSDF. Caustics are read directly from the caustic map. Indirect diffuse is usually evaluated at one extra bounce (final gathering) so the photon map's low-frequency noise does not appear on screen.
The estimate is biased: radius blurs detail, and error only vanishes in the limit.
When to choose it
Reach for it when
- caustics under glass or water are missing
- path tracing struggles with light through specular surfaces
- need efficient indirect lighting with focused light paths
- pool of light patterns through refractive objects
Choose it for caustics, light concentrated by glass or water, and smooth diffuse interreflection, especially where unbiased methods show intense noise.
Prefer Progressive Photon Mapping when memory or fixed photon counts are a problem and you want convergence to the right answer. Prefer Unidirectional Path Tracing for general scenes where a reference is needed. Prefer Vertex Connection and Merging when you want photon-style merging inside an MIS framework. Prefer Instant Radiosity (Virtual Point Lights) when only diffuse GI at interactive rates is needed.
Field marks
- Blotchy low-frequency noise and light leaking across thin walls or at corners because the search disk crosses geometry
- Fixed photon count fails to resolve fine caustic detail; sharp caustic edges are blurred
- Memory grows with photon count, and a map has to be rebuilt for any moving light or object
- Specular surfaces seen directly require explicit ray tracing; the map only represents diffuse reflectors
Presets
Photon Mapping — sparse photon splats (left) vs cone-filtered k-NN caustic & GI gather (right)
{
"P_GATHER_RAD": 0.55,
"P_CAUSTIC_GAIN": 1.35,
"P_LIGHT_SWEEP": 0.75,
"P_SPLIT": 0.48
}Show GLSL (160 lines)
const float P_GATHER_RAD = 0.55; // P Gather Radius [0.15, 1.2, 0.03]
const float P_CAUSTIC_GAIN = 1.35; // P Caustic Power [0.2, 2.5, 0.05]
const float P_LIGHT_SWEEP = 0.75; // P Light Motion [0.0, 2.0, 0.05]
const float P_SPLIT = 0.48; // P Splats / Gather Split [0.0, 1.0, 0.02]
float hash21(vec2 p) {
p = fract(p * vec2(127.1, 311.7));
p += dot(p, p + 34.5);
return fract(p.x * p.y);
}
vec2 hash22(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * vec3(0.1031, 0.1030, 0.0973));
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.xx + p3.yz) * p3.zy);
}
// Cornell box with central dielectric glass sphere (matId=4) focusing a refractive caustic on floor
vec2 mapScene(vec3 p) {
vec2 res = vec2(p.y + 0.85, 1.0); // floor
float dCeil = 1.05 - p.y; if (dCeil < res.x) res = vec2(dCeil, 1.0);
float dBack = p.z + 1.35; if (dBack < res.x) res = vec2(dBack, 1.0);
float dLeft = p.x + 1.28; if (dLeft < res.x) res = vec2(dLeft, 2.0); // warm crimson wall
float dRight = 1.28 - p.x; if (dRight < res.x) res = vec2(dRight, 3.0); // cool teal wall
// Glass crystal sphere resting on floor
float dGlass = length(p - vec3(0.0, -0.43, -0.15)) - 0.42;
if (dGlass < res.x) res = vec2(dGlass, 4.0);
return res;
}
vec3 calcNormal(vec3 p) {
vec2 e = vec2(0.0022, 0.0);
return normalize(vec3(
mapScene(p + e.xyy).x - mapScene(p - e.xyy).x,
mapScene(p + e.yxy).x - mapScene(p - e.yxy).x,
mapScene(p + e.yyx).x - mapScene(p - e.yyx).x
));
}
// Focused refractive caustic density from glass sphere at (0, -0.43, -0.15)
vec3 evalCaustic(vec3 p, vec3 lPos, float gatherR) {
vec3 sphC = vec3(0.0, -0.43, -0.15);
vec3 toSph = normalize(sphC - lPos);
// Intersection of refracted chief ray with floor plane y = -0.85
float tF = (-0.85 - sphC.y) / min(toSph.y, -0.15);
vec2 focusXZ = sphC.xz + toSph.xz * tF * 0.82;
vec2 d = p.xz - focusXZ;
float r = length(d);
float sigma = 0.075 + 0.14 * gatherR;
// Subtle chromatic dispersion across R, G, B wavelengths (IOR 1.50, 1.52, 1.55)
float cR = exp(-pow(length(d - toSph.xz * 0.025) / (sigma * 0.95), 2.0));
float cG = exp(-pow(r / sigma, 2.0));
float cB = exp(-pow(length(d + toSph.xz * 0.025) / (sigma * 1.05), 2.0));
// Bright cusp ring typical of spherical aberration caustic
float ring = exp(-pow((r - 0.14) / (0.04 + 0.04 * gatherR), 2.0)) * 0.55;
float floorMask = smoothstep(-0.72, -0.84, p.y);
return (vec3(cR, cG, cB) * 1.65 + vec3(0.95, 0.88, 1.0) * ring) * floorMask;
}
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 isSmooth = screenX >= split;
float gatherR = clamp(P_GATHER_RAD, 0.12, 1.3);
float causGain = clamp(P_CAUSTIC_GAIN, 0.1, 2.8);
float sweep = iTime * max(P_LIGHT_SWEEP, 0.0);
vec3 lPos = vec3(0.42 * sin(sweep * 0.85), 0.96, -0.10 + 0.25 * cos(sweep * 0.6));
vec3 ro = vec3(0.0, -0.02, 2.28);
vec3 rd = normalize(vec3(uv, -1.52));
vec3 col = vec3(0.02);
float t = 0.0;
float matId = 0.0;
bool hit = false;
for (int i = 0; i < 64; i++) {
vec2 h = mapScene(ro + rd * t);
if (h.x < 0.002) { hit = true; matId = h.y; break; }
t += h.x;
if (t > 9.0) break;
}
if (hit) {
vec3 p = ro + rd * t;
vec3 n = calcNormal(p);
if (matId > 3.5) {
// Refractive glass sphere with Schlick Fresnel + internal caustic glint
vec3 v = -rd;
float nv = max(dot(n, v), 0.0);
float F = 0.04 + 0.96 * pow(1.0 - nv, 5.0);
vec3 rDir = reflect(rd, n);
vec3 tDir = refract(rd, n, 1.0 / 1.52);
// Sample room walls along reflected and refracted directions
vec3 envRefl = mix(vec3(0.14, 0.18, 0.24), vec3(0.95, 0.92, 0.85), smoothstep(0.1, 0.85, rDir.y));
vec3 wallTint = mix(vec3(0.88, 0.24, 0.18), vec3(0.14, 0.72, 0.78), smoothstep(-0.7, 0.7, tDir.x));
vec3 lDir = normalize(lPos - p);
float spec = pow(max(dot(n, normalize(lDir + v)), 0.0), 96.0);
col = mix(wallTint * 0.62 + vec3(0.22, 0.30, 0.36), envRefl, F) + vec3(1.4) * spec;
} else {
vec3 alb = vec3(0.76, 0.75, 0.73);
if (matId > 1.5 && matId < 2.5) alb = vec3(0.86, 0.22, 0.18);
if (matId > 2.5) alb = vec3(0.16, 0.70, 0.74);
vec3 lVec = lPos - p;
float lDist = length(lVec);
vec3 lDir = lVec / max(lDist, 1e-4);
float diff = max(dot(n, lDir), 0.0) / (0.65 + lDist * lDist * 0.38);
// Sphere shadow on floor (dark under glass except where focused caustic lands!)
vec3 oc = p - vec3(0.0, -0.43, -0.15);
float bS = dot(oc, lDir);
float cS = dot(oc, oc) - 0.42 * 0.42;
float sh = (bS < 0.0 && (bS * bS - cS) > 0.0) ? 0.16 : 1.0;
// Indirect diffuse photon-map bounce from red & teal walls
float leftProx = clamp(1.0 - (p.x + 1.28) * 0.55, 0.0, 1.0);
float rightProx = clamp(1.0 - (1.28 - p.x) * 0.55, 0.0, 1.0);
vec3 indirect = vec3(0.86, 0.22, 0.18) * leftProx * 0.28 + vec3(0.16, 0.70, 0.74) * rightProx * 0.28 + vec3(0.14);
vec3 caustic = evalCaustic(p, lPos, gatherR) * causGain;
if (!isSmooth) {
// Left side: discrete stored photon splats in the 3D photon map (visualizing pass-1 deposition)
vec2 cell = floor(p.xz * 24.0 + p.y * 12.0);
vec2 jitter = hash22(cell);
vec2 local = fract(p.xz * 24.0 + p.y * 12.0) - jitter;
float rDot = length(local);
float photonDot = smoothstep(0.24, 0.08, rDot);
float densityProb = clamp(length(caustic) * 0.55 + 0.18, 0.0, 1.0);
float keep = step(1.0 - densityProb, hash21(cell + 17.1));
vec3 splatCol = (length(caustic) > 0.12) ? (caustic * 2.2 + vec3(0.8, 0.85, 1.0) * 0.4) : (indirect * 2.4);
col = alb * (diff * sh * 0.72 + indirect * 0.35) + splatCol * photonDot * keep * 1.15;
} else {
// Right side: cone-filtered k-NN radiance estimate over the photon map
col = alb * (diff * sh * 0.85 + indirect * 0.92) + caustic * (0.45 + 0.55 * alb);
}
// Ceiling area emitter quad
if (p.y > 1.03 && length(p.xz - lPos.xz) < 0.28) {
col = vec3(3.2, 3.0, 2.7);
}
}
}
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 |
|---|---|---|---|
| photon-mapping-live | Tesla T4 1920x1080 | 12.4× baseline (moderate) · 0.386 ms / 0.410 ms | opengl-timer-query · 36 frames · GPU Weight Atlas · Colab T4 sweep |
| photon-mapping-live | NVIDIA L4 1920x1080 | 11.4× baseline (moderate) · 0.182 ms / 0.183 ms · 2.1× faster on L4 | opengl-timer-query · Cross-architecture validation |
| photon-mapping-live | NVIDIA A100-SXM4-40GB 1920x1080 | 7.24× baseline (moderate) · 0.209 ms / 0.209 ms · 1.9× faster on A100 | opengl-timer-query · Cross-architecture validation |
Lineage
Crosses
- Mirrored refractive caustics via photon mapping and oblique planar reflections: Combining a caustic photon map with an oblique-clipped planar mirror pass renders sharp, physically focused refractive caustics both on diffuse floors and cleanly reflected inside polished marble tables, mirrors, or still water basins.
- Unidirectional Path Tracing: The camera pass uses ordinary path tracing for direct light and final gathering on the photon map.
Sources
- primaryHenrik Wann Jensen (1996). Global Illumination using Photon Maps. Eurographics.Verified via crossref · 2026-11-29doi:10.1007/978-3-7091-7484-5_3
- tutorialMatt Pharr, Wenzel Jakob, Greg Humphreys. Stochastic Progressive Photon Mapping (Physically Based Rendering, 3rd edition). pbr-book.org.Verified via live page · 2026-11-29
- implementationmmp (2015). mmp/pbrt-v3. Source code for pbrt, the renderer described in the third edition of "Physically Based Rendering: From Theory To Implementation", by Matt Pharr, Wenzel Jakob, and Greg Humphreys.GitHub API · license BSD-2-Clause · 2026-11-29