Skip to content

Commit

Permalink
Clean up image-based lighting shader and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeshurun Hembd committed May 7, 2024
1 parent 3553aa6 commit b56c3cf
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ vec3 lambertianDiffuse(vec3 diffuseColor)

vec3 fresnelSchlick2(vec3 f0, vec3 f90, float VdotH)
{
// TODO: clamp not necessary? VdotH is already clamped to [0.0, 1.0]
// return f0 + (f90 - f0) * pow(1.0 - VdotH, 5.0)
return f0 + (f90 - f0) * pow(clamp(1.0 - VdotH, 0.0, 1.0), 5.0);
// TODO: manually multiply instead of pow(). See
// https://stackoverflow.com/a/68793086/10082269
float versine = 1.0 - VdotH;
// pow(versine, 5.0) is slow. See https://stackoverflow.com/a/68793086/10082269
float versineSquared = versine * versine;
return f0 + (f90 - f0) * versineSquared * versineSquared * versine;
}

float smithVisibilityG1(float NdotV, float roughness)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ vec3 proceduralIBL(
float roughness = pbrParameters.roughness;
vec3 f0 = pbrParameters.f0;

// TODO: don't assume 0.04 reflectance? Use values from specular extension
float inverseRoughness = 1.04 - roughness;
inverseRoughness *= inverseRoughness;
vec3 sceneSkyBox = czm_textureCube(czm_environmentMap, r).rgb * inverseRoughness;
Expand All @@ -54,21 +53,17 @@ vec3 proceduralIBL(
specularIrradiance = mix(specularIrradiance, belowHorizonColor, smoothstep(aroundHorizon, farBelowHorizon, reflectionDotNadir) * inverseRoughness);
specularIrradiance = mix(specularIrradiance, nadirColor, smoothstep(farBelowHorizon, 1.0, reflectionDotNadir) * inverseRoughness);

// Luminance model from page 40 of http://silviojemma.com/public/papers/lighting/spherical-harmonic-lighting.pdf
// See the "CIE Clear Sky Model" referenced on page 40 of https://3dvar.com/Green2003Spherical.pdf
#ifdef USE_SUN_LUMINANCE
// See the "CIE Clear Sky Model" referenced on page 40 of https://3dvar.com/Green2003Spherical.pdf
// Angle between sun and zenith.
// TODO: perform dot product in eye coordinates (use positionEC) to avoid the matrix multiply
float LdotZenith = clamp(dot(normalize(czm_inverseViewRotation * l), vWC), 0.001, 1.0);
float S = acos(LdotZenith);
// Angle between zenith and current pixel
// TODO: perform dot product in eye coordinates
float NdotZenith = clamp(dot(normalize(czm_inverseViewRotation * n), vWC), 0.001, 1.0);
// Angle between sun and current pixel
float gamma = acos(NdotL);
// TODO: use multiplication instead of pow(x, 2.0)
float numerator = ((0.91 + 10.0 * exp(-3.0 * gamma) + 0.45 * pow(NdotL, 2.0)) * (1.0 - exp(-0.32 / NdotZenith)));
float denominator = (0.91 + 10.0 * exp(-3.0 * S) + 0.45 * pow(LdotZenith, 2.0)) * (1.0 - exp(-0.32));
float numerator = ((0.91 + 10.0 * exp(-3.0 * gamma) + 0.45 * NdotL * NdotL) * (1.0 - exp(-0.32 / NdotZenith)));
float denominator = (0.91 + 10.0 * exp(-3.0 * S) + 0.45 * LdotZenith * LdotZenith) * (1.0 - exp(-0.32));
float luminance = model_luminanceAtZenith * (numerator / denominator);
#endif

Expand Down Expand Up @@ -139,6 +134,7 @@ vec3 textureIBL(
) {
vec3 v = -positionEC;
vec3 n = normalEC;
vec3 l = normalize(lightDirectionEC);
vec3 h = normalize(v + l);

float NdotV = abs(dot(n, v)) + 0.001;
Expand Down

0 comments on commit b56c3cf

Please sign in to comment.