Techniqueadvanced
Spherical harmonic lighting
Also called SH lighting.
Project lighting and transfer functions onto low-order spherical harmonics, so that integrals over the sphere become dot products of coefficients.
Spherical harmonic lighting stores functions on the sphere of directions, such as the light that arrives at a point or the way a surface responds to light, as a few coefficients in the basis of real spherical harmonics. The basis is orthonormal, so the integral of the product of two functions over the sphere becomes the dot product of their coefficient vectors. With order 3 (bands 0, 1 and 2, nine coefficients), a light probe costs nine numbers per color channel, and the diffuse light for any normal is a short polynomial in the normal, which is the irradiance environment map. With more coefficients and a precomputed transfer function per point, the dot product also includes shadows and bounced light, which is precomputed radiance transfer. The basis suits smooth, low frequency light. Sharp light such as a small sun needs many bands, and a truncated projection rings.
In plain words
Any pattern of light around a point can be written as a sum of standard patterns, like a sound is a sum of tones. The first pattern is the same in all directions. The next three are brighter on one side and darker on the other, along each axis. The next five have more lobes, and so on. A smooth light, like an overcast sky, needs only the first few patterns, so you store only how much of each it has, a short list of numbers. The useful trick is that you can do the same for the response of a surface, and then the total light the surface sends out is the sum of the products of the two lists. A sharp light, like the sun, needs many patterns, and cutting the list short gives ripples.
When it applies
- An integral over the sphere of the product of two functions must be computed many times, for example light times a response, at every pixel or vertex.
- Both functions are smooth, or at least one of them is, so that a few bands capture what matters. The clamped cosine of a diffuse surface is such a function.
- The lighting changes over time or space, so that precomputing the full integral for each lighting is not possible, but projecting the lighting each frame is cheap.
- Rotations of the lighting, blends between probes and sums of lights are needed, since each of them acts on the coefficients directly.
The template
- Choose the order. Order means bands and coefficients. Order 3 (9 coefficients) is common for irradiance; orders 4 to 6 for precomputed transfer.
- Project the lighting. , by a sum over the texels of a map weighted by their solid angle, by Monte Carlo, or in closed form for simple lights.
- Project the transfer function. For a diffuse surface without shadows this is the clamped cosine, with known coefficients. With shadows or bounces it is precomputed per point.
- Optionally window. Multiply band by a factor to reduce ringing.
- Shade. Take the dot product of the two coefficient vectors.
SH-SHADE(L, T)- 1
for each texel j of the map, with direction d_j and solid angle Δω_j: - 2
for each (l, m) with l < n: c[l, m] ← c[l, m] + L(d_j) · Y_lm(d_j) · Δω_j - 3
t ← the coefficients of the transfer function ▷ hole: analytic or precomputed - 4
for each l: c[l, ·] ← w_l · c[l, ·] ▷ hole: the window, w_l = 1 for none - 5
return Σ_{l, m} c[l, m] · t[l, m]
Projection (lines L1 and L2). The real spherical harmonics , for and , are orthonormal on the sphere. Up to band 2, they are polynomials in the components of the direction: a constant, three linear terms and five quadratic terms. The sum is a quadrature of the projection integral; a cube map must weight each texel by its solid angle.
Transfer (line L3). For an unshadowed diffuse surface with normal , the transfer is , and its coefficients are with , and . This follows from the Funk-Hecke theorem, because the function depends only on the angle to .
Windowing (line L4). A truncated projection of a sharp light rings, the Gibbs phenomenon on the sphere, and can give negative light. Sloan (2008) describes windows such as sigma factors and the Hanning window, which damp the high bands, at the cost of blur.
Shading (line L5). By orthonormality, for the projected functions and (a proved property). If one of the two functions lies in the span of the first harmonics, the dot product equals the exact integral of the other times it.
Classic examples
- Diffuse light from an environment map. The irradiance environment map uses nine coefficients and the clamped cosine; the error is at most of the total light, a proved bound.
- Probes in a level. An irradiance volume stores nine coefficients per probe of a grid and blends them by position.
- Shadows and bounces. Precomputed radiance transfer projects the visibility and the bounced light of a static object per vertex, and shades by the dot product with the projected lighting.
- Many small lights. Project each point light at its direction and add their coefficients; a point light of power in direction has the coefficients . The sum then shades like one light probe, for the cost of one.
- Ambient light in image-based lighting. The diffuse part of a captured map is stored as nine coefficients.
Variations
- Rotation. Each band is closed under rotation, so a rotation acts by a matrix per band. Band 1 rotates like a vector. Zonal harmonics, the functions symmetric about an axis, rotate by evaluating at the new axis.
- Products. The product of two functions with coefficients each has more bands. Its projection back to coefficients needs the triple product tensor of the basis, not a dot product, and costs much more.
- Other bases. The hemisphere-only bases, the ambient cube of six colors, spherical Gaussians and wavelets on a cube map trade the smoothness and rotation properties of spherical harmonics for sharper features. Wavelets such as the Haar wavelet represent all frequencies with a sparse set of coefficients.
- Two bands. Order 2 (four coefficients) stores a constant and a direction, which captures the main direction of the light; it is cheaper, and often enough for small or distant probes.
- Deringing. Fit coefficients by least squares with a positivity constraint, or apply windows per band.
Pitfalls
- Missing solid angles in line L2. A sum over cube map texels with equal weights overweights the face corners and gives wrong coefficients. Use the exact texel solid angle.
- Sign and order conventions. Libraries differ in the order of the coefficients, the sign of the terms with odd (the Condon-Shortley phase) and the axis taken as the pole. Mixing two conventions in lines L2 and L3 gives wrong results that are hard to see. Test with a single directional light, whose result is known.
- Ringing at line L4. Without a window, a bright small light gives negative values on the far side of the sphere. With a window that is too strong, the light loses its direction. Choose for the brightest light of the scene.
- Too few bands for the job. Nine coefficients give good irradiance but not glossy reflection; a sharp shadow from the sun cannot be represented by a few bands at all.
- Applying the cosine twice. If the coefficients already contain the clamped cosine factors (irradiance), multiplying again at line L5 by the cosine transfer blurs twice. Keep track of whether a vector stores radiance or irradiance.
Exercises
- Project the uniform environment . Answer: , and every other coefficient is 0 by orthogonality to the constant.
- A unit directional light from has coefficients . Give the three that are not zero up to band 2. Answer: , and ; all the others vanish at the pole.
- Evaluate the order 3 projection of the light of Exercise 2 at . Why is it not infinite like the true light, and what is it? Answer: by the addition theorem, , so the value is . A truncated projection of a delta function is a smooth lobe of finite height.
- Show that the diffuse result of a directional light from on a surface with normal is of the exact cosine result with nine coefficients. Answer: .
References
- Ravi Ramamoorthi and Pat Hanrahan, An efficient representation for irradiance environment maps, SIGGRAPH 2001, 497 to 500, Section 2, the real spherical harmonics up to order 2 and the coefficients of the clamped cosine
- Peter-Pike Sloan, Jan Kautz and John Snyder, Precomputed radiance transfer for real-time rendering in dynamic, low-frequency lighting environments, ACM Transactions on Graphics 21(3), 2002, 527 to 536, the projection of lighting and transfer onto spherical harmonics, the dot product of coefficients and the rotation of the lighting (general locator)
- Robin Green, Spherical Harmonic Lighting, The Gritty Details, Game Developers Conference 2003, the definition of the real basis, Monte Carlo projection, the dot product of coefficients and rotation (general locator)
- Peter-Pike Sloan, Stupid Spherical Harmonics (SH) Tricks, Game Developers Conference 2008, the ringing of truncated projections and windowing with sigma factors or a Hanning window, and the remark that bright lights need windowing even for irradiance
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag HY3PQF Edit this page History Report an error Markdown