Skip to content

Commit

Permalink
Merge pull request #12063 from CesiumGS/self-shadowing
Browse files Browse the repository at this point in the history
Update self-shadowing function for direct lighting
  • Loading branch information
ggetz authored Jul 3, 2024
2 parents 290f01d + 97959aa commit a0ef08f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
15 changes: 15 additions & 0 deletions Apps/Sandcastle/gallery/glTF PBR Extensions.html
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,24 @@
onselect: () => {
imageBasedLighting.sphericalHarmonicCoefficients = coefficients;
imageBasedLighting.specularEnvironmentMaps = environmentMapURL;
imageBasedLighting.imageBasedLightingFactor =
Cesium.Cartesian2.ONE;
},
},
{
text: "Procedural sky lighting",
onselect: () => {
imageBasedLighting.sphericalHarmonicCoefficients = undefined;
imageBasedLighting.specularEnvironmentMaps = undefined;
imageBasedLighting.imageBasedLightingFactor =
Cesium.Cartesian2.ONE;
},
},
{
text: "Direct lighting only",
onselect: () => {
imageBasedLighting.imageBasedLightingFactor =
Cesium.Cartesian2.ZERO;
},
},
];
Expand Down Expand Up @@ -194,6 +205,10 @@
text: "Barn Lamp",
onselect: () => loadModel(2583726),
},
{
text: "Metal-Roughness Spheres",
onselect: () => loadModel(2635364),
},
];
Sandcastle.addToolbarMenu(modelOptions, "modelsToolbar");
loadModel(2584329);
Expand Down
8 changes: 8 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Change Log

### 1.120 - 2024-08-01

#### @cesium/engine

##### Fixes :wrench:

- Updated geometric self-shadowing function to improve direct lighting on models using physically-based rendering. [#12063](https://github.com/CesiumGS/cesium/pull/12063)

### 1.119 - 2024-07-01

#### @cesium/engine
Expand Down
36 changes: 18 additions & 18 deletions packages/engine/Source/Shaders/Builtin/Functions/pbrLighting.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -41,31 +41,31 @@ float GGX_anisotropic(float roughness, float tangentialRoughness, vec3 halfwayDi
}
#endif

float smithVisibilityG1(float NdotV, float roughness)
{
// this is the k value for direct lighting.
// for image based lighting it will be roughness^2 / 2
float k = (roughness + 1.0) * (roughness + 1.0) / 8.0;
return NdotV / (NdotV * (1.0 - k) + k);
}

/**
* Estimate the geometric self-shadowing of the microfacets in a surface,
* using the Schlick GGX approximation of a Smith visibility function.
* using the Smith Joint GGX visibility function.
* Note: Vis = G / (4 * NdotL * NdotV)
* see Eric Heitz. 2014. Understanding the Masking-Shadowing Function in Microfacet-Based BRDFs. Journal of Computer Graphics Techniques, 3
* see Real-Time Rendering. Page 331 to 336.
* see https://google.github.io/filament/Filament.md.html#materialsystem/specularbrdf/geometricshadowing(specularg)
*
* @param {float} roughness The roughness of the material.
* @param {float} alphaRoughness The roughness of the material, expressed as the square of perceptual roughness.
* @param {float} NdotL The cosine of the angle between the surface normal and the direction to the light source.
* @param {float} NdotV The cosine of the angle between the surface normal and the direction to the camera.
*/
float smithVisibilityGGX(float roughness, float NdotL, float NdotV)
float smithVisibilityGGX(float alphaRoughness, float NdotL, float NdotV)
{
// Avoid divide-by-zero errors
NdotL = clamp(NdotL, 0.001, 1.0);
NdotV += 0.001;
return (
smithVisibilityG1(NdotL, roughness) *
smithVisibilityG1(NdotV, roughness)
) / (4.0 * NdotL * NdotV);
float alphaRoughnessSq = alphaRoughness * alphaRoughness;

float GGXV = NdotL * sqrt(NdotV * NdotV * (1.0 - alphaRoughnessSq) + alphaRoughnessSq);
float GGXL = NdotV * sqrt(NdotL * NdotL * (1.0 - alphaRoughnessSq) + alphaRoughnessSq);

float GGX = GGXV + GGXL;
if (GGX > 0.0)
{
return 0.5 / GGX;
}
return 0.0;
}

/**
Expand Down

0 comments on commit a0ef08f

Please sign in to comment.