Skip to content

Instantly share code, notes, and snippets.

@romainguy
Last active April 22, 2024 10:36
Show Gist options
  • Save romainguy/6415cbf511233c063a1abe691ad4883b to your computer and use it in GitHub Desktop.
Save romainguy/6415cbf511233c063a1abe691ad4883b to your computer and use it in GitHub Desktop.
Spherical Harmonics lighting
// Uniform array of 4 vec3 for SH environment lighting
// Computed from "Ditch River" (http://www.hdrlabs.com/sibl/archive.html)
static const vec3 sphericalHarmonics[4] = {
{ 0.754554516862612, 0.748542953903366, 0.790921515418539 },
{ -0.083856548007422, 0.092533500963210, 0.322764661032516 },
{ 0.308152705331738, 0.366796330467391, 0.466698181299906 },
{ -0.188884931542396, -0.277402551592231, -0.377844212327557 }
};
// Fragment shader, "n" is the normal at the shading point
vec3 Irradiance_SphericalHarmonics(const vec3 n) {
return max(
sphericalHarmonics[0]
+ sphericalHarmonics[1] * (n.y)
+ sphericalHarmonics[2] * (n.z)
+ sphericalHarmonics[3] * (n.x)
, 0.0);
}
// When computing indirect diffuse lighting:
vec3 indirectDiffuse = Irradiance_SphericalHarmonics(n) * Fd_Lambert();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment