Skip to content

Commit

Permalink
Merge pull request #9565 from kalosma/shadowmap_fade_opt
Browse files Browse the repository at this point in the history
ShadowMap fade option
  • Loading branch information
ebogo1 authored Jun 1, 2021
2 parents bc84cb1 + 0147aee commit 2711313
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 9 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

### 1.83 - 2021-07-01

##### Additions :tada:

- Added `options.fadingEnabled` parameter to `ShadowMap` to control whether shadows fade out when the light source is close to the horizon. [#9565](https://github.com/CesiumGS/cesium/pull/9565)

### 1.82.1 - 2021-06-01

- This is an npm only release to fix the improperly published 1.82.0.
Expand Down
30 changes: 21 additions & 9 deletions Source/Scene/ShadowMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import ShadowMapShader from "./ShadowMapShader.js";
* @param {Boolean} [options.softShadows=false] Whether percentage-closer-filtering is enabled for producing softer shadows.
* @param {Number} [options.darkness=0.3] The shadow darkness.
* @param {Boolean} [options.normalOffset=true] Whether a normal bias is applied to shadows.
* @param {Boolean} [options.fadingEnabled=true] Whether shadows start to fade out once the light gets closer to the horizon.
*
* @exception {DeveloperError} Only one or four cascades are supported.
*
Expand Down Expand Up @@ -117,6 +118,14 @@ function ShadowMap(options) {
this.darkness = defaultValue(options.darkness, 0.3);
this._darkness = this.darkness;

/**
* Determines whether shadows start to fade out once the light gets closer to the horizon.
*
* @type {Boolean}
* @default true
*/
this.fadingEnabled = defaultValue(options.fadingEnabled, true);

/**
* Determines the maximum distance of the shadow map. Only applicable for cascaded shadows. Larger distances may result in lower quality shadows.
*
Expand Down Expand Up @@ -1421,15 +1430,18 @@ function checkVisibility(shadowMap, frameState) {
scratchCartesian2
);
var dot = Cartesian3.dot(surfaceNormal, lightDirection);

// Shadows start to fade out once the light gets closer to the horizon.
// At this point the globe uses vertex lighting alone to darken the surface.
var darknessAmount = CesiumMath.clamp(dot / 0.1, 0.0, 1.0);
shadowMap._darkness = CesiumMath.lerp(
1.0,
shadowMap.darkness,
darknessAmount
);
if (shadowMap.fadingEnabled) {
// Shadows start to fade out once the light gets closer to the horizon.
// At this point the globe uses vertex lighting alone to darken the surface.
var darknessAmount = CesiumMath.clamp(dot / 0.1, 0.0, 1.0);
shadowMap._darkness = CesiumMath.lerp(
1.0,
shadowMap.darkness,
darknessAmount
);
} else {
shadowMap._darkness = shadowMap.darkness;
}

if (dot < 0.0) {
shadowMap._outOfView = true;
Expand Down
36 changes: 36 additions & 0 deletions Specs/Scene/ShadowMapSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,42 @@ describe(
});
});

it("disables shadow fading", function () {
box.show = true;
floor.show = true;

var center = new Cartesian3.fromRadians(longitude, latitude, height);
scene.camera.lookAt(
center,
new HeadingPitchRange(0.0, CesiumMath.toRadians(-70.0), 5.0)
);

// Create light camera pointing straight down
var lightCamera = new Camera(scene);
lightCamera.lookAt(center, new Cartesian3(0.0, 0.0, 1.0));

scene.shadowMap = new ShadowMap({
context: scene.context,
lightCamera: lightCamera,
});

// Render with light looking straight down
var shadowedColor = renderAndReadPixels();

// Move the light close to the horizon
lightCamera.lookAt(center, new Cartesian3(1.0, 0.0, 0.01));

// Render with faded shadows
var horizonShadowedColor = renderAndReadPixels();

// Render with unfaded shadows
scene.shadowMap.fadingEnabled = false;
renderAndCall(function (rgba) {
expect(horizonShadowedColor).not.toEqual(shadowedColor);
expect(rgba).not.toEqual(horizonShadowedColor);
});
});

function depthFramebufferSupported() {
var framebuffer = new Framebuffer({
context: scene.context,
Expand Down

0 comments on commit 2711313

Please sign in to comment.