Skip to content

Commit

Permalink
Account for ground albedo
Browse files Browse the repository at this point in the history
  • Loading branch information
ggetz committed Oct 15, 2024
1 parent 15ff41c commit 2b3907c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Apps/Sandcastle/gallery/glTF PBR Extensions.html
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
imageBasedLighting.sphericalHarmonicCoefficients = undefined;
imageBasedLighting.specularEnvironmentMaps = undefined;
imageBasedLighting.imageBasedLightingFactor = Cesium.Cartesian2.ONE;
scene.light.intensity = 1.0;
scene.light.intensity = 2.0;
},
},
{
Expand Down
15 changes: 12 additions & 3 deletions packages/engine/Source/Scene/DynamicEnvironmentMapManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import ConvolveSpecularMapVS from "../Shaders/ConvolveSpecularMapVS.js";
* @property {number} [brightness=1.0] The brightness of light emitted from the environment. 1.0 uses the unmodified emitted environment color. Less than 1.0 makes the light darker while greater than 1.0 makes it brighter.
* @property {number} [saturation=1.0] The saturation of the light emitted from the environment. 1.0 uses the unmodified emitted environment color. Less than 1.0 reduces the saturation while greater than 1.0 increases it.
* @property {Color} [groundColor=DynamicEnvironmentMapManager.AVERAGE_EARTH_GROUND_COLOR] Solid color used to represent the ground.
* @property {number} [groundAlbedo=0.31] The percentage of light reflected from the ground. The average earth albedo is 0.31.
*/

/**
Expand Down Expand Up @@ -186,6 +187,13 @@ function DynamicEnvironmentMapManager(options) {
options.groundColor,
DynamicEnvironmentMapManager.AVERAGE_EARTH_GROUND_COLOR,
);

/**
* The percentage of light reflected from the ground. The average earth albedo is 0.31.
* @type {number}
* @default 0.31
*/
this.groundAlbedo = defaultValue(options.groundAlbedo, 0.31);
}

Object.defineProperties(DynamicEnvironmentMapManager.prototype, {
Expand Down Expand Up @@ -480,7 +488,8 @@ function updateRadianceMap(manager, frameState) {
u_faceDirection: () => CubeMap.getDirection(face, scratchCartesian),
u_positionWC: () => position,
u_brightnessSaturationGammaIntensity: () => adjustments,
u_groundColor: () => manager.groundColor,
u_groundColor: () =>
manager.groundColor.withAlpha(manager.groundAlbedo),
},
persists: true,
owner: manager,
Expand Down Expand Up @@ -827,11 +836,11 @@ DynamicEnvironmentMapManager.prototype.destroy = function () {
};

/**
* Average hue of ground color on earth, a warm gray.
* Average hue of ground color on earth, a warm green-gray.
* @type {Color}
*/
DynamicEnvironmentMapManager.AVERAGE_EARTH_GROUND_COLOR = Object.freeze(
Color.fromCssColorString("#423c35"),
Color.fromCssColorString("#717145"),
);

export default DynamicEnvironmentMapManager;
21 changes: 12 additions & 9 deletions packages/engine/Source/Shaders/ComputeRadianceMapFS.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ uniform vec3 u_faceDirection; // Current cubemap face
uniform vec3 u_positionWC;
uniform mat4 u_enuToFixedFrame;
uniform vec4 u_brightnessSaturationGammaIntensity;
uniform vec4 u_groundColor;
uniform vec4 u_groundColor; // alpha component represent albedo

vec4 getCubeMapDirection(vec2 uv, vec3 faceDir) {
vec2 scaledUV = uv * 2.0 - 1.0;
Expand Down Expand Up @@ -69,19 +69,22 @@ void main() {
vec4 sceneSkyBoxColor = czm_textureCube(czm_environmentMap, lookupDirection);
vec3 skyBackgroundColor = mix(czm_backgroundColor.rgb, sceneSkyBoxColor.rgb, sceneSkyBoxColor.a);

// Interpolate the ground color based on distance and sun exposure
// Apply intensity to sky light
float intensity = u_brightnessSaturationGammaIntensity.w;
vec4 adjustedSkyColor = skyColor;
adjustedSkyColor.rgb = skyColor.rgb * intensity;

// Compute ground color based on amount of reflected light
vec3 groundColor = u_groundColor.rgb * u_groundColor.a * adjustedSkyColor.rgb;

// Interpolate the ground color based on angle of sun exposure and distance from the ground
vec3 up = normalize(u_positionWC);
float occlusion = max(dot(lightDirectionWC, up), 0.15); // Ensure a low-level of ambiant light reflected from the ground.
vec3 groundColor = mix(vec3(0.0), u_groundColor.rgb, occlusion * u_groundColor.a * (1.0 - d / radius));
float occlusion = max(dot(lightDirectionWC, up), 0.15);
groundColor = mix(vec3(0.0), u_groundColor.rgb, occlusion * (1.0 - d / radius));

// Only show the stars when not obscured by the ellipsoid
vec3 backgroundColor = czm_branchFreeTernary(czm_isEmpty(intersection), skyBackgroundColor, groundColor);

// Apply intensity to sky only
float intensity = u_brightnessSaturationGammaIntensity.w;
vec4 adjustedSkyColor = skyColor;
adjustedSkyColor.rgb = skyColor.rgb * intensity;

vec4 color = vec4(mix(backgroundColor, adjustedSkyColor.rgb, adjustedSkyColor.a), 1.0);

float brightness = u_brightnessSaturationGammaIntensity.x;
Expand Down

0 comments on commit 2b3907c

Please sign in to comment.