3D engine that AI agents drive
Give a wide cascade map a margin toward the light so the far slice holds
Nguyen Xuan Anc349d83957b7
diff --git a/changes/504-far-cascade.md b/changes/504-far-cascade.mdnew file mode 100644index 00000000..4ab4977b--- /dev/null+++ b/changes/504-far-cascade.md@@ -0,0 +1 @@+- Hold the farthest sun cascade while the camera orbits (issue #504): a wide cascade map was redrawn almost every frame because its depth range had no margin on the light side, so any drift toward the sun left it. The wide map now reaches the same 10 percent further toward the light, and an orbiting camera on the container port redraws the farthest cascade 1 time in 40 frames instead of 25. A still camera and the nearest cascade fit exactly as before, so a still frame draws the same pixels.diff --git a/changes/504-far-cascade.size.md b/changes/504-far-cascade.size.mdnew file mode 100644index 00000000..f9519a5e--- /dev/null+++ b/changes/504-far-cascade.size.md@@ -0,0 +1,2 @@+- entry:cascaded-shadows: grew 7 bytes gzip (10559 to 10566) for the light-side margin of a wide cascade map; the farthest cascade of the container port redraws 1 frame in 40 while orbiting instead of 25+- entry:core, scene:lit-box and scene:agent-scene: unchanged to the bytediff --git a/src/cascaded-shadows.js b/src/cascaded-shadows.jsindex 234ca52e..5c448c2d 100644--- a/src/cascaded-shadows.js+++ b/src/cascaded-shadows.js@@ -146,14 +146,14 @@ function boundingSphere(points) {* does not change its size, and the sphere centre is snapped to whole texels so the shadow edges do not crawl.* Returns the world size of a texel and the depth range in metres.*/-export function writeCascadeMatrix(target, { center, radius }, towardLight, resolution, casterReach) {+export function writeCascadeMatrix(target, { center, radius }, towardLight, resolution, casterReach, extraReach = 0) {const rotation = mat4.lookAt(mat4.create(), [0, 0, 0], [-towardLight[0], -towardLight[1], -towardLight[2]], Math.abs(towardLight[1]) > 0.99 ? [0, 0, 1] : [0, 1, 0]);const lightSpaceCenter = vec3.transformMat4(vec3.create(), center, rotation);const texel = (2 * radius) / resolution;const snappedX = Math.round(lightSpaceCenter[0] / texel) * texel;const snappedY = Math.round(lightSpaceCenter[1] / texel) * texel;const snappedZ = Math.round(lightSpaceCenter[2] / texel) * texel;- const near = -(snappedZ + casterReach);+ const near = -(snappedZ + casterReach + extraReach);const far = -(snappedZ - radius);const projection = mat4.ortho(mat4.create(), snappedX - radius, snappedX + radius, snappedY - radius, snappedY + radius, near, far);mat4.multiply(target, projection, rotation);@@ -250,7 +250,9 @@ export class CascadedShadows {hold.fit = tight;} else if (!fitCovers(hold.fit, sphere, towardLight, this.casterReach)) {const wide = { matrix: new Float32Array(16), towardLight: vec3.clone(towardLight) };- Object.assign(wide, writeCascadeMatrix(wide.matrix, { center: sphere.center, radius: sphere.radius * (1 + this.holdMargin) }, towardLight, this.resolution, this.casterReach));+ // The depth range gets the same margin on the light side, so a slice that drifts toward the light stays covered too.+ const margin = sphere.radius * this.holdMargin;+ Object.assign(wide, writeCascadeMatrix(wide.matrix, { center: sphere.center, radius: sphere.radius + margin }, towardLight, this.resolution, this.casterReach, margin));hold.fit = wide;}return hold.fit;diff --git a/tests/cascaded-shadows.test.js b/tests/cascaded-shadows.test.jsindex aa2c6897..2f8b2ee4 100644--- a/tests/cascaded-shadows.test.js+++ b/tests/cascaded-shadows.test.js@@ -97,6 +97,14 @@ test('an update redraws only the slices whose matrix changed, and every slice whassert.deepEqual(drawnLayers.splice(0), [1, 2, 4], 'a changed caster redraws every slice');});+test('a far slice that drifts toward the light keeps its map, because the wide fit also reaches further on the light side', () => {+ const cascades = createCascadedShadows({ count: 2, resolution: 2048 });+ const sphereAlongLight = distance => ({ center: TOWARD_LIGHT.map(value => value * distance), radius: 80 });+ cascades.chooseFit(1, sphereAlongLight(0), TOWARD_LIGHT);+ const wide = cascades.chooseFit(1, sphereAlongLight(1), TOWARD_LIGHT);+ assert.equal(cascades.chooseFit(1, sphereAlongLight(4), TOWARD_LIGHT), wide, 'a drift toward the light stays covered');+});+test('a moving camera keeps a far slice\'s map until the slice leaves it, and a stopped camera settles on the exact fit', () => {const cascades = createCascadedShadows({ count: 2, resolution: 2048 });const sphereAt = x => ({ center: [x, 0, -100], radius: 80 });