Skip to content

armgit / kiln

Public Imported from GitHub
0

Build and revise procedural 3D assets with your coding agent. Local MCP server, CLI and TypeScript engine with rendered review, editable source and GLB export.

25.3 KB
Raw Blame
import { expect, test } from 'bun:test';
import { BoxGeometry, DirectionalLight, Group, HalfFloatType, InstancedBufferAttribute, InstancedMesh, Layers, Mesh, MeshStandardMaterial, MeshStandardNodeMaterial, OrthographicCamera, PerspectiveCamera, Scene, SkinnedMesh } from 'three/webgpu';
import { attribute, positionLocal, uniform, vec3 } from 'three/tsl';
import { cachedSunShadow } from '../../src/shadows/cache';
import type { CachedSunShadowOptions } from '../../src/shadows/cache';
import { keepsShadowMask, layerBit, ShadowLayers, suppressedCasters } from '../../src/shadows/layers';

const material = new MeshStandardMaterial(), geometry = new BoxGeometry();
const S_BIT = 1 << 30, D_BIT = 1 << 31;
const box = (name: string, cast = true) => { const m = new Mesh(geometry, material); m.name = name; m.castShadow = cast; return m; };
const main = Object.assign(new PerspectiveCamera(), { name: 'main' }), mirror = Object.assign(new PerspectiveCamera(), { name: 'mirror' });

function farmSun() {
  const light = new DirectionalLight(); light.name = 'sun'; light.position.set(25, 42, 20); light.castShadow = true; light.shadow.mapSize.setScalar(2048);
  Object.assign(light.shadow.camera, { left: -44, right: 44, top: 44, bottom: -44, near: 1, far: 115 }); light.shadow.camera.updateProjectionMatrix();
  return light;
}
/**
 * Drives one child ShadowNode through three r186's own updateBefore gating (per camera and frame dedupe, never during
 * pre-compile, needsUpdate cleared only when the depth version held). updateShadow is stubbed: it logs the casters the
 * shadow camera would draw (visible through ancestors, on a camera layer, castShadow) and bumps the depth version on the
 * first render, as the first updateRenderTarget does.
 */
function drive(node: any, scene: Scene) {
  let first = true;const log: (string | number)[][] = [];
  node.shadowMap = { depthTexture: { version: 0 }, dispose() {} };
  node.updateShadow = (frame: { frameId: number }) => {
    node._depthVersionCached = node.shadowMap.depthTexture.version; if (first) { node.shadowMap.depthTexture.version++; first = false; }
    const drawn: (string | number)[] = [frame.frameId]; const layers: Layers = node.shadow.camera.layers;
    scene.traverseVisible(o => { if ((o as Mesh).isMesh && o.castShadow && o.layers.test(layers)) drawn.push(o.name); });
    log.push(drawn);
  };
  return { log, draw: (frameId: number, camera: PerspectiveCamera) => node.updateBefore({ renderer: { _isPreCompiling: false }, camera, frameId }) };
}
function setup(o: Partial<CachedSunShadowOptions> = {}) {
  const scene = new Scene(), light = farmSun(); scene.add(light, light.target); scene.updateMatrixWorld(true);
  const cache = cachedSunShadow({ light, settleFrames: 3, ...o }), composite = (light.shadow as any).shadowNode.node;
  const S = drive(composite.aNode, scene), D = drive(composite.bNode, scene);let frame = 0;
  /** One frame: the scene's update at SystemOrder.shadows, then every camera that draws a receiver. */
  const step = (cameras = [main]) => { cache.update(); for (const c of cameras) { S.draw(frame, c); D.draw(frame, c); } frame++; };
  return { scene, light, cache, S, D, step, composite };
}

test('mask math: kit bits sit above every scene layer and keep their own shadow masks', () => {
  expect(ShadowLayers).toEqual({ static: 30, live: 31 });
  const l = new Layers(); l.set(31); expect(l.test({ mask: layerBit(31) } as Layers)).toBe(true);
  expect([layerBit(30), layerBit(31)]).toEqual([S_BIT, D_BIT]);
  expect([keepsShadowMask(0), keepsShadowMask(1), keepsShadowMask(5), keepsShadowMask(S_BIT), keepsShadowMask(D_BIT), keepsShadowMask(S_BIT | D_BIT)]).toEqual([false, false, true, true, true, true]);
  for (const layers of [{ static: 0, live: 2 }, { static: 3, live: 3 }, { static: 1, live: 32 }]) expect(() => cachedSunShadow({ light: farmSun(), layers })).toThrow('distinct');
  const custom = cachedSunShadow({ light: farmSun(), layers: { static: 1, live: 2 } });
  expect([custom.staticShadow.camera.layers.mask, custom.liveShadow.camera.layers.mask]).toEqual([2, 4]);
});

test('install: two armed clones with their own placeholder lights, combined with min; one custom node per light', () => {
  const light = farmSun(), filter = () => 0; Object.assign(light.shadow, { filterNode: filter, mapType: HalfFloatType });
  const cache = cachedSunShadow({ light, liveMapSize: 1024 }), template = light.shadow as any, composite = template.shadowNode.node;
  const S = cache.staticShadow as any, D = cache.liveShadow as any;
  expect(composite.method).toBe('min');
  const nodes = [composite.aNode, composite.bNode];
  expect(nodes.map(n => [n.isShadowNode, n.shadow])).toEqual([[true, S], [true, D]]);
  expect(nodes[0].light).not.toBe(nodes[1].light);
  for (const n of nodes) {
    expect(n.light).not.toBe(light); expect(n.light.shadow).toBe(n.shadow); expect(n.light.castShadow).toBe(true);
    expect(n.light.matrixWorld).toBe(light.matrixWorld); expect(n.light.target).toBe(light.target); expect(n.light.parent).toBeNull();
  }
  expect(nodes.map(n => n.light.name)).toEqual(['sun static', 'sun live']);
  expect(new Set([template, S, D]).size).toBe(3); expect(new Set([template.camera, S.camera, D.camera]).size).toBe(3);
  expect([S.filterNode, D.filterNode, S.mapType, D.mapType]).toEqual([filter, filter, HalfFloatType, HalfFloatType]);
  expect([S.autoUpdate, S.needsUpdate, D.autoUpdate, D.needsUpdate]).toEqual([false, true, false, true]);
  expect([S.camera.layers.mask, D.camera.layers.mask]).toEqual([S_BIT, D_BIT]);
  expect([S.mapSize.x, D.mapSize.x, template.mapSize.x, template.mapSize.y]).toEqual([2048, 1024, 2048, 2048]);
  expect(light.castShadow).toBe(true); expect(template.map).toBeNull();
  expect(() => cachedSunShadow({ light })).toThrow('custom shadow node');
});

test('track adds the static bit per object to casters, suppressed casters and stand-ins, once', () => {
  const { scene, light, cache } = setup(), group = new Group(), caster = box('caster'), quiet = box('quiet', false), dropped = box('dropped', false), standIn = box('standIn');
  const batch = new InstancedMesh(geometry, material, 2); batch.castShadow = true; suppressedCasters.add(dropped);
  standIn.userData.kilnShadowStandIn = true; standIn.castShadow = false; standIn.layers.set(29); light.shadow.camera.layers.enable(29);
  group.add(caster, quiet, dropped, standIn, batch); scene.add(group);
  expect(cache.track(scene).added).toBe(4);
  expect([caster, dropped, batch].map(m => m.layers.mask)).toEqual([1 | S_BIT, 1 | S_BIT, 1 | S_BIT]);
  expect([standIn.layers.mask, quiet.layers.mask, group.layers.mask]).toEqual([1 << 29 | S_BIT, 1, 1]);
  expect(cache.track(scene).added).toBe(0); expect(cache.track(group, { movable: () => true }).added).toBe(0);
  expect(cache.stats).toMatchObject({ staticCasters: 4, liveCasters: 0 });
});

test('arming: both maps render on the first two frames, a mover goes live and back, the live map clears once', () => {
  const { scene, cache, S, D, step } = setup(), still = box('still'), mover = box('mover'), rig = new Group();
  rig.add(mover); scene.add(still, rig); scene.updateMatrixWorld(true); cache.track(scene, { movable: n => n === rig });
  step([]); step([]);                                                    // no receiver drawn yet: the arms wait
  step(); step(); step();
  expect(S.log).toEqual([[2, 'still', 'mover'], [3, 'still', 'mover']]); // first render recreates the texture: two frames
  expect(D.log).toEqual([[2], [3]]);
  rig.position.x = 1; rig.updateMatrixWorld(true); step();               // frame 5: promote, static map re-armed without it
  expect(mover.layers.mask).toBe(1 | D_BIT);
  expect([S.log.at(-1), D.log.at(-1)]).toEqual([[5, 'still'], [5, 'mover']]);
  expect(cache.stats).toMatchObject({ staticCasters: 1, liveCasters: 1, staticRenders: 2 });
  expect(cache.stats.pendingSettle).toBeGreaterThan(0);
  rig.position.x = 2; rig.updateMatrixWorld(true); step([main, mirror]); // frame 6: live map only, once for two cameras
  expect(S.log.length).toBe(3); expect(D.log.slice(-2)).toEqual([[5, 'mover'], [6, 'mover']]);
  step(); step();                                                        // frames 7, 8: unchanged, still live
  expect(D.log.at(-1)).toEqual([8, 'mover']);
  step();                                                                // frame 9: settled, back to static, live map cleared
  expect(mover.layers.mask).toBe(1 | S_BIT);
  expect([S.log.at(-1), D.log.at(-1)]).toEqual([[9, 'still', 'mover'], [9]]);
  step(); step();
  expect([S.log.length, D.log.length]).toEqual([4, 7]);
  expect(cache.stats).toMatchObject({ staticCasters: 2, liveCasters: 0, staticRenders: 3, pendingSettle: 0 });
});

test('watched casters also go live on ancestor visibility, castShadow and instance changes; unwatched never do', () => {
  const { scene, cache, S, step } = setup(), still = box('still'), mover = box('mover'), rig = new Group(), batch = new InstancedMesh(geometry, material, 3);
  batch.name = 'batch'; batch.castShadow = true; rig.add(mover); scene.add(still, rig, batch); scene.updateMatrixWorld(true);
  cache.track(scene, { movable: n => n === rig || n === batch }); step(); step();
  const live = (m: Mesh, change: () => void) => { change(); step(); const promoted = (m.layers.mask & D_BIT) !== 0; for (let i = 0; i < 3; i++) step(); return promoted && (m.layers.mask & S_BIT) !== 0; };
  expect(live(mover, () => { rig.visible = false; })).toBe(true);
  expect(live(mover, () => { rig.visible = true; })).toBe(true);
  expect(live(mover, () => { mover.castShadow = false; })).toBe(true);
  expect(live(batch, () => { batch.instanceMatrix.needsUpdate = true; })).toBe(true);
  expect(live(batch, () => { batch.count = 1; })).toBe(true);
  const renders = cache.stats.staticRenders, logged = S.log.length;
  still.position.x = 3; still.updateMatrixWorld(true); step(); step();
  expect([still.layers.mask, cache.stats.staticRenders, S.log.length]).toEqual([1 | S_BIT, renders, logged]);
  const skinned = new SkinnedMesh(geometry, material); skinned.castShadow = true; rig.add(skinned); cache.track(rig, { movable: () => true });
  for (let i = 0; i < 6; i++) step();
  expect(skinned.layers.mask).toBe(1 | D_BIT);                           // bones move without a matrixWorld change
});

test('watched morph influences promote with an unchanged matrix, settle, and release their history on untrack', () => {
  const { scene, cache, S, D, step } = setup(), g = geometry.clone();
  g.morphAttributes.position = [g.getAttribute('position').clone(), g.getAttribute('position').clone()];
  for (const [target, positions] of g.morphAttributes.position.entries()) {
    for (let i = 0; i < positions.count; i++) positions.setY(i, positions.getY(i) + target + 1);
  }
  const morph = new Mesh(g, material); morph.name = 'morph'; morph.castShadow = true;
  scene.add(morph); scene.updateMatrixWorld(true);
  const matrix = [...morph.matrixWorld.elements], tracked = cache.track(scene, { movable: n => n === morph });
  step(); step(); step();
  morph.morphTargetInfluences![1] = .75; step([main, mirror]);
  expect(morph.matrixWorld.elements).toEqual(matrix);
  expect(morph.layers.mask).toBe(1 | D_BIT);
  expect([S.log.at(-1), D.log.at(-1)]).toEqual([[3], [3, 'morph']]);
  step(); step(); expect(morph.layers.mask).toBe(1 | D_BIT);
  step(); expect(morph.layers.mask).toBe(1 | S_BIT); // unchanged influence values calm after settleFrames
  const settled = [S.log.length, D.log.length];
  morph.morphTargetInfluences = [...morph.morphTargetInfluences!]; step();
  expect([S.log.length, D.log.length]).toEqual(settled); // array identity is not deformation
  morph.morphTargetInfluences![1] = 0; step(); expect(morph.layers.mask).toBe(1 | D_BIT);
  tracked.untrack(); morph.morphTargetInfluences![0] = 1; step();
  expect(morph.layers.mask).toBe(1); expect(cache.stats.liveCasters).toBe(0);
  // Re-adding snapshots today's pose, not the previous registration's influences.
  cache.track(scene, { movable: n => n === morph }); step(); step(); step();
  expect(morph.layers.mask).toBe(1 | S_BIT);
  cache.dispose(); g.dispose();
});

test('a watched caster also notices morph target addition and removal', () => {
  const { scene, cache, step } = setup(), morph = box('morph'); scene.add(morph); scene.updateMatrixWorld(true);
  cache.track(scene, { movable: n => n === morph }); step(); step();
  morph.morphTargetInfluences = [0, 1]; step(); expect(morph.layers.mask).toBe(1 | D_BIT);
  for (let i = 0; i < 3; i++) step(); expect(morph.layers.mask).toBe(1 | S_BIT);
  morph.morphTargetInfluences = [0]; step(); expect(morph.layers.mask).toBe(1 | D_BIT);
  for (let i = 0; i < 3; i++) step();
  delete morph.morphTargetInfluences; step(); expect(morph.layers.mask).toBe(1 | D_BIT);
  cache.dispose();
});

test('shader uniforms and custom instance attributes use explicit live or invalidation, without inspecting shader graphs', () => {
  const { scene, cache, S, D, step } = setup(), liveRoot = new Group(), offset = uniform(0);
  const g = geometry.clone(), motion = new InstancedBufferAttribute(new Float32Array([0, 1]), 1);
  g.setAttribute('motion', motion);
  const shader = new MeshStandardNodeMaterial(); shader.positionNode = positionLocal.add(vec3(offset, 0, 0));
  const instanceShader = new MeshStandardNodeMaterial(); instanceShader.positionNode = positionLocal.add(vec3(attribute('motion', 'float'), 0, 0));
  const vertex = new Mesh(geometry, shader), instances = new InstancedMesh(g, instanceShader, 2), discrete = new Mesh(geometry, shader);
  vertex.name = 'vertex'; instances.name = 'instances'; discrete.name = 'discrete';
  for (const m of [vertex, instances, discrete]) m.castShadow = true;
  liveRoot.add(vertex, instances); scene.add(liveRoot, discrete); scene.updateMatrixWorld(true);
  const matrices = [vertex, instances, discrete].map(m => [...m.matrixWorld.elements]), instanceVersion = instances.instanceMatrix.version;
  cache.track(scene, { live: n => n === liveRoot, movable: () => true }); step(); step(); step();
  const staticRenders = S.log.length;
  offset.value = 2; motion.setX(0, 5); motion.needsUpdate = true; step([main, mirror]);
  expect([vertex, instances, discrete].map(m => m.matrixWorld.elements)).toEqual(matrices);
  expect(instances.instanceMatrix.version).toBe(instanceVersion);
  expect(S.log.length).toBe(staticRenders); expect(D.log.at(-1)).toEqual([3, 'vertex', 'instances']);
  // A discrete edit of the unpinned caster is the caller's responsibility.
  cache.invalidate('shader pose'); step(); expect(S.log.at(-1)).toEqual([4, 'discrete']);
  cache.dispose(); g.dispose(); shader.dispose(); instanceShader.dispose();
});

test('invalidate re-arms the static map and records why; prime fills the live map once, then clears it', () => {
  const { scene, cache, S, D, step } = setup(), still = box('still'), mover = box('mover'), rig = new Group();
  rig.add(mover); scene.add(still, rig); scene.updateMatrixWorld(true); cache.track(scene, { movable: n => n === rig });
  step(); step(); step();
  cache.invalidate('door'); cache.invalidate('door'); cache.invalidate('tier');
  expect(cache.stats).toMatchObject({ invalidations: 3, reasons: { door: 2, tier: 1 }, staticRenders: 4 });
  step(); step();
  expect(S.log.slice(2)).toEqual([[3, 'still', 'mover']]);
  cache.prime();
  expect(cache.liveShadow.camera.layers.mask).toBe(S_BIT | D_BIT);
  step();
  expect([S.log.at(-1), D.log.at(-1)]).toEqual([[5, 'still', 'mover'], [5, 'still', 'mover']]);
  step();
  expect(cache.liveShadow.camera.layers.mask).toBe(D_BIT); expect(D.log.at(-1)).toEqual([6]);
  step(); expect(D.log.at(-1)).toEqual([6]);
  expect(cache.stats.pendingSettle).toBe(0);
});

test('a prime before the first frame holds its mask until three clears the flag', () => {
  const { scene, cache, D, step } = setup(), still = box('still');
  scene.add(still); scene.updateMatrixWorld(true); cache.track(scene); cache.prime();
  step(); step(); step(); step();
  expect(D.log).toEqual([[0, 'still'], [1, 'still'], [2]]);
});

test('template sync: extents, depth range and zoom reach the live map every frame, the static map on re-arm; sampling values reach both every frame', () => {
  const { light, cache, step } = setup(), S = cache.staticShadow, D = cache.liveShadow; step();
  Object.assign(light.shadow.camera, { left: -30, right: 30, top: 20, bottom: -20, near: 2, far: 90, zoom: 2 });
  Object.assign(light.shadow, { bias: -.001, normalBias: .05, radius: 3, intensity: .6 });
  cache.update();
  const expected = new OrthographicCamera(-30, 30, 20, -20, 2, 90); expected.zoom = 2;
  const pick = (s: typeof S) => { const c = s.camera as OrthographicCamera; return [c.left, c.right, c.top, c.bottom, c.near, c.far, c.zoom, s.bias, s.normalBias, s.radius, s.intensity]; };
  expect(pick(D)).toEqual([-30, 30, 20, -20, 2, 90, 2, -.001, .05, 3, .6]);
  expected.coordinateSystem = D.camera.coordinateSystem; expected.updateProjectionMatrix();
  expect(D.camera.projectionMatrix.elements).toEqual(expected.projectionMatrix.elements);
  // RK-7b: bias, normal bias, radius and intensity are sampling uniforms (ShadowNode reference()), so the static map's
  // receivers take them at once; its camera keeps the extents its depth was rendered with until a re-arm.
  expect(pick(S)).toEqual([-44, 44, 44, -44, 1, 115, 1, -.001, .05, 3, .6]);
  cache.invalidate('extent');
  expect(pick(S)).toEqual(pick(D)); expect(S.camera.projectionMatrix.elements).toEqual(expected.projectionMatrix.elements);
});

test('a resize re-arms both maps and moves the real light mapSize sentinel monotonically', () => {
  const { light, cache, step } = setup(), S = cache.staticShadow, D = cache.liveShadow, seen = [light.shadow.mapSize.y];
  step(); step();
  for (const size of [1024, 2048, 1024]) {
    light.shadow.mapSize.setScalar(size); cache.update();
    expect([S.mapSize.x, S.mapSize.y, D.mapSize.x, D.mapSize.y, S.needsUpdate, D.needsUpdate, light.shadow.mapSize.x]).toEqual([size, size, size, size, true, true, size]);
    expect(light.shadow.mapSize.y).toBeGreaterThan(Math.max(...seen, size)); seen.push(light.shadow.mapSize.y);
    step(); step(); cache.update(); expect(light.shadow.mapSize.y).toBe(seen.at(-1)!);
  }
  cache.dispose(); expect(light.shadow.mapSize.toArray()).toEqual([1024, 1024]);
  const small = setup({ liveMapSize: 512 }); small.light.shadow.mapSize.setScalar(1024); small.cache.update();
  expect([small.cache.staticShadow.mapSize.x, small.cache.liveShadow.mapSize.x]).toEqual([1024, 512]);
});

test('dispose: both shadow nodes, both shadows, then the custom node; only the bits it added come off', () => {
  const scene = new Scene(), light = farmSun(), cache = cachedSunShadow({ light, settleFrames: 1 }), composite = (light.shadow as any).shadowNode.node, order: string[] = [];
  const spy = (o: any, name: string) => { const original = o.dispose.bind(o); o.dispose = () => { order.push(name); original(); }; };
  spy(composite.aNode, 'static node'); spy(composite.bNode, 'live node'); spy(cache.staticShadow, 'static shadow'); spy(cache.liveShadow, 'live shadow');
  const plain = box('plain'), owned = box('owned'), mover = box('mover'), rig = new Group(); owned.layers.enable(30); rig.add(mover); scene.add(light, plain, owned, rig); scene.updateMatrixWorld(true);
  cache.track(scene, { movable: n => n === rig }); mover.position.y = 1; mover.updateMatrixWorld(); cache.update();
  expect(mover.layers.mask).toBe(1 | D_BIT);
  cache.dispose(); cache.dispose();
  expect(order).toEqual(['static node', 'live node', 'static shadow', 'live shadow']);
  expect('shadowNode' in light.shadow).toBe(false);
  expect([plain.layers.mask, owned.layers.mask, mover.layers.mask]).toEqual([1, 1 | S_BIT, 1]);
  expect(() => { cache.update(); cache.invalidate('late'); cache.prime(); }).not.toThrow();
  expect(cache.stats.invalidations).toBe(0);
  expect(() => cache.track(scene)).toThrow('disposed');
  expect(() => cachedSunShadow({ light }).dispose()).not.toThrow();
});

// Wave-B review fixes (tmp/drawcalls/wave-b-review/fix-kit). Finding ids name the review items.
test('RK-3 track tags only casters the shadow camera draws: its own layers, or layer 0 where three would use the viewing camera mask', () => {
  const { scene, light, cache, S, step } = setup(), kept = box('onLayer0'), two = box('onLayer2'), five = box('onLayer5');
  light.shadow.camera.layers.mask = 1 | 1 << 2; two.layers.set(2); five.layers.set(5);
  scene.add(kept, two, five); scene.updateMatrixWorld(true);
  expect(cache.track(scene).added).toBe(2); step(); step();
  expect(S.log.at(-1)!.slice(1)).toEqual(['onLayer0', 'onLayer2']);
  expect(five.layers.mask).toBe(1 << 5);
  const fallback = setup(), zero = box('zero'), other = box('other'); other.layers.set(5); fallback.scene.add(zero, other); fallback.scene.updateMatrixWorld(true);
  expect(fallback.cache.track(fallback.scene).added).toBe(1); expect([zero.layers.mask, other.layers.mask]).toEqual([1 | S_BIT, 1 << 5]);
});

test('RK-5 install refuses a light whose stock shadow map a receiver already built', () => {
  const light = farmSun(); (light.shadow as any).map = { isRenderTarget: true };
  expect(() => cachedSunShadow({ light })).toThrow('before any receiver');
  expect('shadowNode' in light.shadow).toBe(false);
});

test('RK-6 drift under the tolerance is not motion: a creeping caster settles; creep past it from the last counted pose goes live', () => {
  const { scene, cache, D, step } = setup(), rig = new Group(), rowan = box('rowan'); rig.add(rowan); scene.add(rig); rig.position.set(4, 0, 4); scene.updateMatrixWorld(true);
  cache.track(scene, { movable: n => n === rig }); step(); step(); step();
  const renders = D.log.length;
  for (let i = 0; i < 60; i++) { rig.position.x += (i % 2 ? -1 : 1) * 1e-14; rig.updateMatrixWorld(true); step(); }
  expect([cache.stats.liveCasters, D.log.length - renders, rowan.layers.mask]).toEqual([0, 0, 1 | S_BIT]);
  for (let i = 0; i < 2; i++) { rig.position.x += 4e-7; rig.updateMatrixWorld(true); step(); }
  expect(rowan.layers.mask).toBe(1 | S_BIT);                             // 0.8 µm from the counted pose
  rig.position.x += 4e-7; rig.updateMatrixWorld(true); step();
  expect(rowan.layers.mask).toBe(1 | D_BIT);                             // 1.2 µm: motion
  const exact = setup({ tolerance: 0 }), r2 = new Group(), m2 = box('m2'); r2.add(m2); exact.scene.add(r2); exact.scene.updateMatrixWorld(true);
  exact.cache.track(exact.scene, { movable: n => n === r2 }); exact.step(); exact.step();
  r2.position.x += 1e-14; r2.updateMatrixWorld(true); exact.step(); expect(m2.layers.mask).toBe(1 | D_BIT);
  expect(() => cachedSunShadow({ light: farmSun(), tolerance: -1 })).toThrow('tolerance');
});

test('RK-7a a mesh that starts casting after track() joins on the next track() of its root', () => {
  const { scene, cache, S, step } = setup(), late = box('late', false); scene.add(late); scene.updateMatrixWorld(true);
  expect(cache.track(scene).added).toBe(0); late.castShadow = true;
  expect(cache.track(scene).added).toBe(1); cache.invalidate('castShadow'); step(); step();
  expect(S.log.at(-1)!.slice(1)).toEqual(['late']);
});

test('RK-7c track returns an untrack that clears only the casters that call tagged and re-arms the static map', () => {
  const { scene, cache, S, D, step } = setup(), old = new Group(), next = new Group(), rig = new Group(), mover = box('mover'), wall = box('wall'), kept = box('kept');
  rig.add(mover); old.add(rig, wall); next.add(kept); scene.add(old, next); scene.updateMatrixWorld(true);
  const a = cache.track(old, { movable: n => n === rig }), b = cache.track(next);
  expect([a.added, b.added]).toEqual([2, 1]); step(); step();
  rig.position.x = 1; rig.updateMatrixWorld(true); step();             // frame 2: the mover goes live
  expect(mover.layers.mask).toBe(1 | D_BIT);
  const renders = cache.stats.staticRenders; scene.remove(old); a.untrack(); a.untrack();
  expect([mover.layers.mask, wall.layers.mask, kept.layers.mask]).toEqual([1, 1, 1 | S_BIT]);
  expect(cache.stats).toMatchObject({ staticCasters: 1, liveCasters: 0, staticRenders: renders + 1, reasons: { untrack: 1 } });
  step();                                                                // frame 3: the static map without the old world, the live map cleared once
  expect([S.log.at(-1), D.log.at(-1)]).toEqual([[3, 'kept'], [3]]);
  rig.position.x = 2; rig.updateMatrixWorld(true); step(); expect(mover.layers.mask).toBe(1);
  expect(cache.track(old).added).toBe(2);
  b.untrack(); cache.dispose(); expect([kept.layers.mask, mover.layers.mask]).toEqual([1, 1]);
});

test('live() casters, whose motion the watch cannot see, draw in the live map every frame they are shown, then once more to clear', () => {
  const { scene, cache, S, D, step } = setup(), still = box('still'), cars = new Group(), car = new InstancedMesh(geometry, material, 4);
  car.name = 'car'; car.castShadow = true; cars.add(car); scene.add(still, cars); scene.updateMatrixWorld(true);
  const t = cache.track(scene, { live: n => n === cars, movable: () => true });
  expect([t.added, still.layers.mask, car.layers.mask]).toEqual([2, 1 | S_BIT, 1 | D_BIT]);
  step(); step(); step();
  expect(S.log).toEqual([[0, 'still'], [1, 'still']]); expect(D.log).toEqual([[0, 'car'], [1, 'car'], [2, 'car']]);
  expect(cache.stats).toMatchObject({ staticCasters: 1, liveCasters: 1, pendingSettle: 0 });
  cars.visible = false; step(); step();
  expect(D.log.slice(-2)).toEqual([[2, 'car'], [3]]); expect(cache.stats.liveCasters).toBe(0);
  car.count = 0; cars.visible = true; step(); expect(D.log.length).toBe(4);   // nothing to draw: no render
  car.count = 4; step(); expect(D.log.at(-1)).toEqual([6, 'car']);
  expect(S.log.length).toBe(2);                                          // live casters never re-arm the static map
  cache.dispose(); expect([car.layers.mask, still.layers.mask]).toEqual([1, 1]);
});