[In this reprinted #altdevblogaday article, game programmer Simon Yeung explains Spherical Harmonic Lighting, Monte Carlo integration, lighting with SH functions, Zonal Harmonics, and more.]
Spherical Harmonics(SH) functions are a set of orthogonal basis functions defined in spherical coordinates using imaginary numbers. In this post, we use the following conversion between spherical and cartesian coordinates:
Since we are dealing with real value functions, we only need to deal with real spherical harmonics functions which in the form of:
The index l
of the SH function is called the band index which is an integer >=
0 and index m
is an integer with range -l<=m<=l
, so there will be (2l
+ 1) functions in a given band. You may refer to the Appendix A2 of Stupid Spherical Harmonics(SH) Trick
to look up the evaluated value of the SH basis function for a pair of (l
The linear combination of SH basis functions with scalar values can be used to approximate a function as below:
With an approximation up to band l
- 1, which n×n
coefficients are needed.
So the remaining problem to approximate a function is to compute the coefficient c which can be solved either analytically or numerically by
Monte Carlo Integration.
Monte Carlo Integration
To compute a definite integral numerically, we can consider the Monte Carlo Estimator:
When the number of samples, N, is large enough, the estimator F will equal to the definite integral because considering the expected value of F:
When number of samples,N, is large enough, by the law of large numbers, the estimator F will converge to the definite integral. Therefore, we can calculate the coefficient of the SH basis functions by using Monte Carlo Estimator.
Properties of Spherical Harmonics Function
There are 2 important properties properties of SH functions:
First, it is rotationally invariant.
Where the rotated function g
is still a SH function which its coefficients can be computed by using the coefficients of f
. For details of rotating a general SH functions, you can refer to the section 'Rotating Spherical Harmonics' in Spherical Harmonics Lighting: The Gritty Details
Second, when integrating 2 SH projected functions over the spherical domain, the results will equals to dot product of their SH coefficients (due to the SH basis functions are orthogonal):
This is a nice property that we can calculate the integration over the spherical domain by a dot product of the SH coefficients.
Lighting With SH functions
When performing lighting calculation, we need to solve the rendering equation
For shading lambert diffuse surface without shadow, we can simplify the rendering equation into:
To solve this integral, we can project the functions L
) and max(N.ω
, 0) into SH functions using Monte Carlo Integration, then by the property 2 described above, the integral equals to dot product of the SH coefficients of the 2 SH projected functions.
If a SH projected function is rotational symmetric about a fixed axis, it is called Zonal Harmonics(ZH). If this axis is the z-axis, this will make the ZH function only depends on θ
, which will result in only one non-zero coefficient in each band with m
= 0. Then rotation of the ZH function can be greatly simplified. When the ZH function is rotated to a new axis d
, the coefficients of the rotated SH function will equals to:
,which is faster than the general SH rotation. The ZH function is well suit to approximate the function max(N.ω
, 0) in the above diffuse surface rendering equation since the SH projected L
) is usually done in world space while the shading surface can be re-oriented to the same space to perform lighting calculation.