6 uniform samplerCube environmentMap;
8 const float PI = 3.14159265359;
11 // sample dir = hemisphere's orientation
12 vec3 normal = normalize(localPos);
13 vec3 irradiance = vec3(0);
15 vec3 up = vec3(0, 1, 0);
16 vec3 right = cross(up, normal);
17 up = cross(normal, right);
19 const float sampleDelta = 0.025;
21 for (float phi = 0; phi < 2 * PI; phi += sampleDelta) {
22 for (float theta = 0; theta < 0.5 * PI; theta += sampleDelta) {
23 vec3 tangentSample = vec3(sin(theta) * cos(phi), sin(theta) * sin(phi), cos(theta));
24 // tangent space -> world space
25 vec3 sampleVec = tangentSample.x * right + tangentSample.y * up + tangentSample.z * normal;
26 irradiance += texture(environmentMap, sampleVec).rgb * cos(theta) * sin(theta);
31 irradiance = (PI * irradiance) / numSamples;
33 fragColor = vec4(irradiance, 1);