fix(globe): Navara's shadowed sun leaked a light per frame

Navara 0.2.1's transparent pass refreshes its copy of every scene light
each frame with copy(light, true). For the sun's cascaded shadow lights
(three-csm CascadedDirectionalLights) that copy clones every cascade, so
each frame brought new DirectionalLights with new ids and three's
WebGLLights uniforms cache (keyed by id) grew by about 1.2 KB a frame in
the looks whose sun casts shadows (minimal, photoreal).

src/globe/navara/cascadedLightCopy.js patches the copy to update the
cascades it has in place (pose, colour, intensity, target, shadow
settings and camera) when the counts match, and keeps the rebuild
otherwise; internals.js finds the class among the view's scene lights
(private view._scenes.light) and applies it when the globe is built.
Tests on three-csm's own class: the original copy changes the light ids,
the patched one keeps the lights and takes the values.

Post-GC JS heap (CDP HeapProfiler.collectGarbage every 10 s, the app on
?engine=navara rendering continuously at 60 fps): minimal 4.33 → 0.20
MB/min over 70 s and 0.06 MB/min over 180 s; photoreal 4.34 → 0.19
MB/min; cinematic (no shadow) 0.34. Added to the upstream asks.
This commit is contained in:
German Khokhlov committed 2026-10-16 18:41:20 +04:00
1 parent c327a25744
commit 8812117013
5 files changed
+185 -2

No files matched your search

@@ -0,0 +1,73 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import csm from '@navaramap/three-csm';
import { patchCascadedLightCopy } from './cascadedLightCopy.js';
const { CascadedDirectionalLights } = csm;
function sun(count = 3) {
const light = new CascadedDirectionalLights(0xffffff, 2);
light.setCount(count);
light.cascadedLights.forEach((cascade, index) => {
cascade.position.set(index, 2, 3);
cascade.target.position.set(index, 0, 0);
cascade.updateMatrixWorld(true);
cascade.target.updateMatrixWorld(true);
cascade.shadow.bias = -0.001 * (index + 1);
});
return light;
}
test("Navara 0.2.1's copy clones new lights every call (the leak)", () => {
const source = sun();
const copy = new CascadedDirectionalLights();
copy.copy(source, true);
const ids = copy.cascadedLights.map((light) => light.id);
copy.copy(source, true);
assert.notDeepEqual(
copy.cascadedLights.map((light) => light.id),
ids,
);
});
test('patched, a copy keeps its lights and takes the values', () => {
const restore = patchCascadedLightCopy(CascadedDirectionalLights.prototype);
try {
const source = sun();
const copy = new CascadedDirectionalLights();
copy.copy(source, true); // first copy: builds the lights
const lights = [...copy.cascadedLights];
const targets = lights.map((light) => light.target);
source.cascadedLights[1].intensity = 5;
source.cascadedLights[1].color.set(0xff0000);
source.cascadedLights[2].position.set(9, 9, 9);
source.cascadedLights[2].target.position.set(7, 0, 0);
source.cascadedLights[2].updateMatrixWorld(true);
source.cascadedLights[2].target.updateMatrixWorld(true);
source.cascadedLights[0].shadow.camera.left = -77;
copy.copy(source, true);
assert.deepEqual(copy.cascadedLights, lights, 'the same light objects');
assert.deepEqual(
copy.cascadedLights.map((light) => light.target),
targets,
);
assert.equal(copy.cascadedLights[1].intensity, 5);
assert.equal(copy.cascadedLights[1].color.getHex(), 0xff0000);
assert.deepEqual(copy.cascadedLights[2].position.toArray(), [9, 9, 9]);
assert.equal(copy.cascadedLights[2].target.matrixWorld.elements[12], 7);
assert.equal(copy.cascadedLights[0].shadow.camera.left, -77);
assert.equal(copy.cascadedLights[2].shadow.bias, -0.003);
// A different cascade count rebuilds, as before.
source.setCount(4);
copy.copy(source, true);
assert.equal(copy.cascadedLights.length, 4);
assert.equal(
patchCascadedLightCopy(CascadedDirectionalLights.prototype),
restore,
'idempotent',
);
} finally {
restore();
}
assert.equal(CascadedDirectionalLights.prototype.copy.gevInPlace, undefined);
});