castle-web-cli 0.4.229 → 0.4.231

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (259) hide show
  1. package/dist/agent-prompts.d.ts +2 -0
  2. package/dist/agent-prompts.js +16 -8
  3. package/dist/agent.d.ts +2 -0
  4. package/dist/agent.js +21 -21
  5. package/dist/api.d.ts +28 -2
  6. package/dist/api.js +74 -16
  7. package/dist/castle-host/host.d.ts +91 -0
  8. package/dist/castle-host/host.js +860 -3
  9. package/dist/castleJson.d.ts +44 -0
  10. package/dist/castleJson.js +131 -0
  11. package/dist/check.js +27 -1
  12. package/dist/contentPack.d.ts +49 -0
  13. package/dist/contentPack.js +273 -0
  14. package/dist/devExperiments.d.ts +21 -0
  15. package/dist/devExperiments.js +139 -0
  16. package/dist/devHub.d.ts +55 -0
  17. package/dist/devHub.js +187 -0
  18. package/dist/devSaveStorage.d.ts +36 -0
  19. package/dist/devSaveStorage.js +469 -0
  20. package/dist/devSessionChild.js +25 -13
  21. package/dist/devSessionServer.d.ts +14 -1
  22. package/dist/devSessionServer.js +237 -46
  23. package/dist/devSessionStorage.d.ts +9 -0
  24. package/dist/devSessionStorage.js +66 -1
  25. package/dist/experimentAssignment.d.ts +15 -0
  26. package/dist/experimentAssignment.js +41 -0
  27. package/dist/importCheckHook.d.ts +40 -0
  28. package/dist/importCheckHook.js +171 -0
  29. package/dist/importRegression.d.ts +15 -0
  30. package/dist/importRegression.js +100 -0
  31. package/dist/importTrust.d.ts +2 -0
  32. package/dist/importTrust.js +27 -0
  33. package/dist/importUpdateHook.d.ts +18 -0
  34. package/dist/importUpdateHook.js +31 -8
  35. package/dist/importUpdateHookRunner.js +19 -7
  36. package/dist/imports.d.ts +16 -0
  37. package/dist/imports.js +141 -31
  38. package/dist/index.js +3 -1
  39. package/dist/init.d.ts +3 -0
  40. package/dist/init.js +126 -9
  41. package/dist/install.js +4 -1
  42. package/dist/localPaths.js +4 -0
  43. package/dist/login.js +2 -2
  44. package/dist/platformDoc.js +3 -8
  45. package/dist/playPagePolicy.js +3 -1
  46. package/dist/remix.js +5 -0
  47. package/dist/routerTaskReport.d.ts +28 -0
  48. package/dist/routerTaskReport.js +56 -0
  49. package/dist/save-deck.d.ts +18 -0
  50. package/dist/save-deck.js +142 -6
  51. package/dist/serve.d.ts +8 -0
  52. package/dist/serve.js +99 -14
  53. package/dist/serverBundle.d.ts +1 -1
  54. package/dist/serverBundle.js +5 -1
  55. package/dist/webSourceSync.d.ts +2 -0
  56. package/dist/webSourceSync.js +2 -0
  57. package/kits/base/CLAUDE.md +2 -3
  58. package/kits/base/castle.json +1 -1
  59. package/kits/base/sdk/README.md +406 -53
  60. package/kits/base/sdk/agent.js +4 -4
  61. package/kits/base/sdk/avatar.d.ts +12 -0
  62. package/kits/base/sdk/avatar.js +23 -0
  63. package/kits/base/sdk/castle.d.ts +9 -0
  64. package/kits/base/sdk/castle.js +5 -0
  65. package/kits/base/sdk/commands.d.ts +223 -0
  66. package/kits/base/sdk/commands.js +16 -0
  67. package/kits/base/sdk/context.d.ts +6 -0
  68. package/kits/base/sdk/experiments.d.ts +15 -0
  69. package/kits/base/sdk/experiments.js +83 -0
  70. package/kits/base/sdk/hub.d.ts +85 -0
  71. package/kits/base/sdk/hub.js +107 -0
  72. package/kits/base/sdk/multiplayer.d.ts +45 -1
  73. package/kits/base/sdk/multiplayer.js +158 -8
  74. package/kits/base/sdk/save.d.ts +197 -0
  75. package/kits/base/sdk/save.js +525 -0
  76. package/kits/base/sdk/saveCoordinator.d.ts +39 -0
  77. package/kits/base/sdk/saveCoordinator.js +252 -0
  78. package/kits/base/sdk/server/hub.d.ts +169 -0
  79. package/kits/base/sdk/server/hub.js +125 -0
  80. package/kits/base/sdk/server/platformHandle.d.ts +42 -1
  81. package/kits/base/sdk/server/storage.d.ts +58 -4
  82. package/kits/base/sdk/server/storage.js +25 -5
  83. package/kits/base/sdk/server/wrapper.d.ts +36 -2
  84. package/kits/base/sdk/server/wrapper.js +92 -11
  85. package/kits/base/sdk/store.d.ts +3 -2
  86. package/kits/base/sdk/store.js +5 -0
  87. package/kits/base/sdk/transport.d.ts +2 -1
  88. package/kits/base/sdk/transport.js +13 -1
  89. package/kits/hub-game/.prettierrc +5 -0
  90. package/kits/hub-game/CLAUDE.md +149 -0
  91. package/kits/hub-game/blueprints/avatar-0.scene +28 -0
  92. package/kits/hub-game/blueprints/avatar-1.scene +28 -0
  93. package/kits/hub-game/blueprints/avatar-2.scene +28 -0
  94. package/kits/hub-game/blueprints/avatar-3.scene +28 -0
  95. package/kits/hub-game/blueprints/bean/avatar-0.scene +28 -0
  96. package/kits/hub-game/blueprints/bean/avatar-1.scene +28 -0
  97. package/kits/hub-game/blueprints/bean/avatar-2.scene +28 -0
  98. package/kits/hub-game/blueprints/bean/avatar-3.scene +28 -0
  99. package/kits/hub-game/blueprints/blocky/avatar-0.scene +28 -0
  100. package/kits/hub-game/blueprints/blocky/avatar-1.scene +28 -0
  101. package/kits/hub-game/blueprints/blocky/avatar-2.scene +28 -0
  102. package/kits/hub-game/blueprints/blocky/avatar-3.scene +28 -0
  103. package/kits/hub-game/blueprints/pixel/avatar-0.scene +28 -0
  104. package/kits/hub-game/blueprints/pixel/avatar-1.scene +28 -0
  105. package/kits/hub-game/blueprints/pixel/avatar-2.scene +28 -0
  106. package/kits/hub-game/blueprints/pixel/avatar-3.scene +28 -0
  107. package/kits/hub-game/castle.json +96 -0
  108. package/kits/hub-game/code/client/attention.js +133 -0
  109. package/kits/hub-game/code/client/avatar.js +288 -0
  110. package/kits/hub-game/code/client/cabinets.js +90 -0
  111. package/kits/hub-game/code/client/chat.js +135 -0
  112. package/kits/hub-game/code/client/chrome.js +1035 -0
  113. package/kits/hub-game/code/client/doorFrame.js +15 -0
  114. package/kits/hub-game/code/client/firstJoin.js +14 -0
  115. package/kits/hub-game/code/client/grab.js +376 -0
  116. package/kits/hub-game/code/client/hookLayer.js +236 -0
  117. package/kits/hub-game/code/client/idle.js +326 -0
  118. package/kits/hub-game/code/client/keys.js +48 -0
  119. package/kits/hub-game/code/client/pack.js +232 -0
  120. package/kits/hub-game/code/client/packFormat.js +397 -0
  121. package/kits/hub-game/code/client/say.js +127 -0
  122. package/kits/hub-game/code/client/sha256.js +80 -0
  123. package/kits/hub-game/code/client/sound.js +147 -0
  124. package/kits/hub-game/code/client/state.js +143 -0
  125. package/kits/hub-game/code/client/talk.js +233 -0
  126. package/kits/hub-game/code/client/tiers.js +480 -0
  127. package/kits/hub-game/code/client/together.js +440 -0
  128. package/kits/hub-game/code/client/wear.js +149 -0
  129. package/kits/hub-game/code/server/actors.js +627 -0
  130. package/kits/hub-game/code/server/blocks.js +103 -0
  131. package/kits/hub-game/code/server/cabinets.js +309 -0
  132. package/kits/hub-game/code/server/game.js +137 -0
  133. package/kits/hub-game/code/server/grab.js +248 -0
  134. package/kits/hub-game/code/server/history.js +96 -0
  135. package/kits/hub-game/code/server/hub.js +1998 -0
  136. package/kits/hub-game/code/server/idle.js +412 -0
  137. package/kits/hub-game/code/server/index.js +64 -0
  138. package/kits/hub-game/code/server/labels.js +59 -0
  139. package/kits/hub-game/code/server/manifest.js +554 -0
  140. package/kits/hub-game/code/server/npcs.js +733 -0
  141. package/kits/hub-game/code/server/party.js +439 -0
  142. package/kits/hub-game/code/server/place.js +43 -0
  143. package/kits/hub-game/code/server/say.js +83 -0
  144. package/kits/hub-game/code/server/wear.js +142 -0
  145. package/kits/hub-game/code/systems/hub.js +2347 -0
  146. package/kits/hub-game/docs/content-pack.md +151 -0
  147. package/kits/hub-game/docs/contract.md +1209 -0
  148. package/kits/hub-game/docs/style.md +137 -0
  149. package/kits/hub-game/drawings/blocky/castle-eye.sprite +58 -0
  150. package/kits/hub-game/drawings/blocky/dash.sprite +59 -0
  151. package/kits/hub-game/drawings/blocky/pom.sprite +59 -0
  152. package/kits/hub-game/drawings/blocky/tabby.sprite +59 -0
  153. package/kits/hub-game/drawings/pixel/castle-arm.sprite +13 -0
  154. package/kits/hub-game/drawings/pixel/castle-body.sprite +23 -0
  155. package/kits/hub-game/drawings/pixel/castle-leg.sprite +12 -0
  156. package/kits/hub-game/drawings/pixel/kai-arm.sprite +14 -0
  157. package/kits/hub-game/drawings/pixel/kai-head.sprite +24 -0
  158. package/kits/hub-game/drawings/pixel/kai-leg.sprite +15 -0
  159. package/kits/hub-game/drawings/pixel/kai-torso.sprite +18 -0
  160. package/kits/hub-game/drawings/pixel/mimi-arm.sprite +13 -0
  161. package/kits/hub-game/drawings/pixel/mimi-head.sprite +23 -0
  162. package/kits/hub-game/drawings/pixel/mimi-leg.sprite +15 -0
  163. package/kits/hub-game/drawings/pixel/mimi-torso.sprite +18 -0
  164. package/kits/hub-game/drawings/pixel/nox-arm.sprite +13 -0
  165. package/kits/hub-game/drawings/pixel/nox-head.sprite +21 -0
  166. package/kits/hub-game/drawings/pixel/nox-leg.sprite +14 -0
  167. package/kits/hub-game/drawings/pixel/nox-torso.sprite +18 -0
  168. package/kits/hub-game/eslint.config.js +20 -0
  169. package/kits/hub-game/fonts.generated.js +9 -0
  170. package/kits/hub-game/index.html +22 -0
  171. package/kits/hub-game/main.jsx +6 -0
  172. package/kits/hub-game/models/avatar-0.pxmodel +21 -0
  173. package/kits/hub-game/models/avatar-1.pxmodel +24 -0
  174. package/kits/hub-game/models/avatar-2.pxmodel +27 -0
  175. package/kits/hub-game/models/avatar-3.pxmodel +22 -0
  176. package/kits/hub-game/models/bean/avatar-0.pxmodel +19 -0
  177. package/kits/hub-game/models/bean/avatar-1.pxmodel +24 -0
  178. package/kits/hub-game/models/bean/avatar-2.pxmodel +22 -0
  179. package/kits/hub-game/models/bean/avatar-3.pxmodel +24 -0
  180. package/kits/hub-game/models/blocky/avatar-0.pxmodel +21 -0
  181. package/kits/hub-game/models/blocky/avatar-1.pxmodel +30 -0
  182. package/kits/hub-game/models/blocky/avatar-2.pxmodel +28 -0
  183. package/kits/hub-game/models/blocky/avatar-3.pxmodel +27 -0
  184. package/kits/hub-game/models/pixel/avatar-0.pxmodel +24 -0
  185. package/kits/hub-game/models/pixel/avatar-1.pxmodel +16 -0
  186. package/kits/hub-game/models/pixel/avatar-2.pxmodel +19 -0
  187. package/kits/hub-game/models/pixel/avatar-3.pxmodel +23 -0
  188. package/kits/hub-game/package-lock.json +365 -0
  189. package/kits/hub-game/package.json +32 -0
  190. package/kits/hub-game/scenes/main.scene +318 -0
  191. package/kits/hub-game/scripts/check.mjs +339 -0
  192. package/kits/hub-game/scripts/fixtures/sdk-stub.mjs +3 -0
  193. package/kits/hub-game/scripts/gen-avatars-bean.mjs +748 -0
  194. package/kits/hub-game/scripts/gen-avatars-blocky.mjs +607 -0
  195. package/kits/hub-game/scripts/gen-avatars-pixel.mjs +771 -0
  196. package/kits/hub-game/scripts/gen-avatars.mjs +621 -0
  197. package/kits/hub-game/scripts/pack.mjs +192 -0
  198. package/kits/hub-game/scripts/test-actors.mjs +666 -0
  199. package/kits/hub-game/scripts/test-attention.mjs +103 -0
  200. package/kits/hub-game/scripts/test-avatar.mjs +472 -0
  201. package/kits/hub-game/scripts/test-ban-join.mjs +194 -0
  202. package/kits/hub-game/scripts/test-blocks.mjs +351 -0
  203. package/kits/hub-game/scripts/test-chat.mjs +98 -0
  204. package/kits/hub-game/scripts/test-classic.mjs +590 -0
  205. package/kits/hub-game/scripts/test-door-jump.mjs +131 -0
  206. package/kits/hub-game/scripts/test-door.mjs +56 -0
  207. package/kits/hub-game/scripts/test-grab-client.mjs +328 -0
  208. package/kits/hub-game/scripts/test-grab.mjs +331 -0
  209. package/kits/hub-game/scripts/test-hub-ops.mjs +835 -0
  210. package/kits/hub-game/scripts/test-hub-trip.mjs +1518 -0
  211. package/kits/hub-game/scripts/test-idle.mjs +518 -0
  212. package/kits/hub-game/scripts/test-interest.mjs +413 -0
  213. package/kits/hub-game/scripts/test-keys.mjs +50 -0
  214. package/kits/hub-game/scripts/test-manifest.mjs +270 -0
  215. package/kits/hub-game/scripts/test-no-forms.mjs +16 -0
  216. package/kits/hub-game/scripts/test-npcs.mjs +724 -0
  217. package/kits/hub-game/scripts/test-pack.mjs +424 -0
  218. package/kits/hub-game/scripts/test-party.mjs +369 -0
  219. package/kits/hub-game/scripts/test-say.mjs +149 -0
  220. package/kits/hub-game/scripts/test-tiers.mjs +666 -0
  221. package/kits/hub-game/scripts/test-together.mjs +215 -0
  222. package/kits/hub-game/theme.style +3 -0
  223. package/kits/multiplayer-3d/castle.json +5 -5
  224. package/kits/multiplayer-3d/code/client/avatars.js +101 -1
  225. package/kits/multiplayer-3d/code/server/players.js +4 -1
  226. package/kits/multiplayer-3d/code/server/sceneBodies.js +27 -6
  227. package/kits/multiplayer-3d/code/server/world.js +20 -7
  228. package/kits/multiplayer-3d/code/systems/multiplayer.js +222 -11
  229. package/kits/physics-2d/CLAUDE.md +2 -0
  230. package/kits/physics-2d/castle.json +2 -2
  231. package/kits/physics-2d/engine/art.js +13 -0
  232. package/kits/physics-2d/engine/assets.js +4 -2
  233. package/kits/physics-2d/engine/scene.js +117 -5
  234. package/kits/physics-2d/package.json +1 -1
  235. package/kits/physics-2d/scripts/test-skip-unchanged.mjs +254 -0
  236. package/kits/physics-3d/CLAUDE.md +285 -1
  237. package/kits/physics-3d/behaviors/Player.jsx +74 -1
  238. package/kits/physics-3d/castle.json +3 -3
  239. package/kits/physics-3d/engine3d/Scene3DPlayer.jsx +33 -5
  240. package/kits/physics-3d/engine3d/actornode.js +253 -0
  241. package/kits/physics-3d/engine3d/artobjects.js +2 -2
  242. package/kits/physics-3d/engine3d/materials.js +29 -1
  243. package/kits/physics-3d/engine3d/nearshadows.js +600 -0
  244. package/kits/physics-3d/engine3d/unchanged.js +153 -0
  245. package/kits/physics-3d/engine3d/world3d.js +527 -5
  246. package/kits/physics-3d/package.json +1 -1
  247. package/kits/physics-3d/scripts/test-camera-follow.mjs +533 -0
  248. package/kits/physics-3d/scripts/test-near-shadows.mjs +1073 -0
  249. package/kits/physics-3d/scripts/test-static-actors.mjs +662 -0
  250. package/kits/physics-3d/scripts/test-static-passes.mjs +459 -0
  251. package/kits/physics-3d/scripts/test-static-physics.mjs +128 -0
  252. package/kits/physics-3d/systems/physics3d.js +187 -5
  253. package/kits/real-time/castle.json +2 -2
  254. package/kits/real-time/code/client/connection.js +77 -7
  255. package/kits/real-time/code/client/gameHooks.js +63 -3
  256. package/kits/real-time/code/client/messages.js +7 -0
  257. package/kits/real-time/code/server/gameHooks.js +24 -0
  258. package/kits/real-time/code/server/session.js +390 -22
  259. package/package.json +2 -2
@@ -0,0 +1,1073 @@
1
+ // `"engine": { "shadows": { "distance": n } }` (engine3d/nearshadows.js), from
2
+ // the world's side. The rule it is built to:
3
+ //
4
+ // The scene's sun, its shadow object, its shadow camera, its map and its
5
+ // target are never written by this mode. What the mode needs is its own
6
+ // objects, attached only for the duration of one render call inside
7
+ // try/finally. Outside that call the world is as main leaves it.
8
+ //
9
+ // and the terms under which the mode draws a frame at all (the list at the top
10
+ // of nearshadows.js): not re-entered, PCFSoft on this frame and the last, the
11
+ // sun still the scene's casting light.
12
+ //
13
+ // So nearly every scenario here is one comparison: a world given frames of a
14
+ // scene with the key and a twin given the same frames of a scene without it
15
+ // must have the same sun, field for field, matrices and maps included,
16
+ // whatever happened in between (a throw, a listener that re-enters, a dispose,
17
+ // a change of shadow type, a sun replaced). The rest is what the engine's own
18
+ // shadow is while it is on the sun: the sun's direction, depth range, texel
19
+ // size and bias, one size whichever way the camera looks, whole texels, and no
20
+ // caster culled whose shadow can land in the view.
21
+ //
22
+ // The shadow pass is three.js's own (`WebGLShadowMap`, from its source, the
23
+ // file the build is made of), given a renderer that draws nothing: it
24
+ // allocates and replaces the maps, calls `updateMatrices` and `getFrustum`,
25
+ // culls, and calls `onBeforeShadow`, as in a browser. What stands in for
26
+ // `WebGLRenderer.render` round it is: the scene's and the camera's matrices
27
+ // brought up to date, and the lights that cast collected from the scene.
28
+ //
29
+ // What a scene file can say is the runtime's to read
30
+ // (physics-2d scripts/test-skip-unchanged.mjs).
31
+ // test-static-actors.mjs runs it (`npm run check`); ONLY=<part of a name> runs
32
+ // those alone, and KEEP_GOING=1 lists every scenario that fails.
33
+ import assert from 'node:assert/strict';
34
+ import fs from 'node:fs';
35
+ import * as THREE from 'three';
36
+ import { WebGLShadowMap } from 'three/src/renderers/webgl/WebGLShadowMap.js';
37
+ // Registers the resolver the kit's modules need under node.
38
+ import './test-static-passes.mjs';
39
+
40
+ const { renderWorld, disposeWorld } = await import('../engine3d/world3d.js');
41
+
42
+ let seed = 20261004;
43
+ function random() {
44
+ seed = (seed + 0x6d2b79f5) | 0;
45
+ let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
46
+ t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
47
+ return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
48
+ }
49
+
50
+ function makeSun() {
51
+ const sun = new THREE.DirectionalLight(0xffffff, 1.15);
52
+ sun.position.set(6, 18, -6);
53
+ sun.castShadow = true;
54
+ sun.shadow.mapSize.set(2048, 2048);
55
+ Object.assign(sun.shadow.camera, { near: 0.5, far: 50, top: 85, bottom: -35, left: -75, right: 75 });
56
+ sun.shadow.camera.updateProjectionMatrix();
57
+ return sun;
58
+ }
59
+
60
+ // The sun as world3d.js `addLights` makes it, in a world whose renderer has
61
+ // three.js's shadow pass and draws nothing. `world.targets` is every map (and
62
+ // blur map) the pass allocated, with the shadow it was for and how often it
63
+ // was disposed; `world.drew` the shadow each render found on the sun;
64
+ // `world.cast` the objects the last shadow pass drew.
65
+ const worlds = [];
66
+
67
+ function makeWorld(aspect = 412 / 870) {
68
+ const scene = new THREE.Scene();
69
+ const sun = makeSun();
70
+ scene.add(sun);
71
+ const camera = new THREE.PerspectiveCamera(50, aspect, 0.1, 1000);
72
+ look(camera, 0, 9, 0);
73
+ const world = {
74
+ scene,
75
+ camera,
76
+ nodes: new Map(),
77
+ parked: new Map(),
78
+ models: new Map(),
79
+ skipping: { on: false },
80
+ modelSigs: new Map(),
81
+ snapshots: new WeakMap(),
82
+ instances: new Map(),
83
+ nearShadows: null,
84
+ targets: [],
85
+ drew: [],
86
+ cast: [],
87
+ onRender: null,
88
+ onDispose: null,
89
+ };
90
+ const noop = () => {};
91
+ const gl = {
92
+ getRenderTarget: () => null,
93
+ getActiveCubeFace: () => 0,
94
+ getActiveMipmapLevel: () => 0,
95
+ setRenderTarget: noop,
96
+ clear: noop,
97
+ renderBufferDirect: (shadowCamera, fog, geometry, material, object) => world.cast.push(object),
98
+ localClippingEnabled: false,
99
+ properties: { get: () => ({}) },
100
+ state: {
101
+ setBlending: noop,
102
+ setScissorTest: noop,
103
+ viewport: noop,
104
+ buffers: { depth: { getReversed: () => false, setTest: noop }, color: { setClear: noop } },
105
+ },
106
+ };
107
+ const capabilities = { maxTextureSize: 4096 };
108
+ const shadowMap = new WebGLShadowMap(gl, { update: (object) => object.geometry }, capabilities);
109
+ shadowMap.enabled = true;
110
+ shadowMap.type = THREE.PCFSoftShadowMap;
111
+ world.renderer = { dispose() {}, capabilities, shadowMap };
112
+ // Nothing of the renderer's is written: three.js's own function, always.
113
+ world.passRender = shadowMap.render;
114
+ world.replacedPass = 0;
115
+ world.composer = {
116
+ render() {
117
+ scene.updateMatrixWorld();
118
+ camera.updateMatrixWorld();
119
+ const lights = [];
120
+ scene.traverseVisible((object) => {
121
+ if (object.isLight && object.castShadow && object.layers.test(camera.layers)) lights.push(object);
122
+ });
123
+ world.drew.push(sun.shadow);
124
+ if (shadowMap.render !== world.passRender) world.replacedPass += 1;
125
+ world.cast = [];
126
+ world.onRender?.(sun.shadow);
127
+ if (world.contextLost) return;
128
+ shadowMap.render(lights, scene, camera);
129
+ for (const shadow of new Set([...lights.map((light) => light.shadow), sun.shadow, world.nearShadows?.shadow])) noteTargets(world, shadow);
130
+ },
131
+ };
132
+ world.own = sun.shadow;
133
+ worlds.push(world);
134
+ return { world, sun, own: sun.shadow };
135
+ }
136
+
137
+ // After any `renderWorld` call returns, either the mode drew that frame or
138
+ // nothing of the mode's is allocated. Asked of every world a scenario made,
139
+ // as the scenario left it.
140
+ function nothingAllocatedUnlessDrawn(world) {
141
+ const near = world.nearShadows?.shadow ?? null;
142
+ if (near && world.drew.at(-1) === near) return;
143
+ assert.equal(near, null, 'the last frame was not drawn by the mode, and the world holds its shadow');
144
+ assert.equal(world.nearShadows?.frame ?? null, null);
145
+ assert.equal(world.nearShadows?.depth ?? 0, 0, 'no call is still counted');
146
+ assert.equal(world.replacedPass, 0, "the renderer's shadow pass was never replaced, in a frame or after");
147
+ assert.equal(world.renderer.shadowMap.render, world.passRender);
148
+ assert.equal(world.sun?.shadow ?? world.own, world.own, 'the sun has its own shadow');
149
+ const freed = mine(world, world.own).map((seen) => seen.disposed);
150
+ assert.deepEqual(freed, freed.map(() => 1), 'the last frame was not drawn by the mode, and a map of its is allocated');
151
+ }
152
+
153
+ // Writes down the maps a shadow has that the world has not seen yet.
154
+ function noteTargets(world, shadow) {
155
+ for (const target of [shadow?.map, shadow?.mapPass]) {
156
+ if (!target || world.targets.some((seen) => seen.target === target)) continue;
157
+ const seen = { target, of: shadow, disposed: 0 };
158
+ target.addEventListener('dispose', () => {
159
+ seen.disposed += 1;
160
+ world.onDispose?.(seen);
161
+ });
162
+ world.targets.push(seen);
163
+ }
164
+ }
165
+
166
+ // The follow camera: `height` up, looking down at a point 9 m off at `angle`.
167
+ function look(camera, x, z, angle, height = 7) {
168
+ camera.position.set(x, height, z);
169
+ camera.lookAt(x + Math.sin(angle) * 9, 0.5, z - Math.cos(angle) * 9);
170
+ }
171
+
172
+ const asked = (distance) => ({ nearShadowDistance: distance });
173
+
174
+ // Everything of the scene's own that a frame could write.
175
+ function sunState(world, sun, own) {
176
+ const box = own.camera;
177
+ const target = (map) => (map ? { size: [map.width, map.height], filter: map.texture.minFilter, disposed: world.targets.find((seen) => seen.target === map)?.disposed } : null);
178
+ return {
179
+ shadowIsItsOwn: sun.shadow === own,
180
+ position: sun.position.toArray(),
181
+ matrixWorld: sun.matrixWorld.toArray(),
182
+ target: sun.target.position.toArray(),
183
+ targetWorld: sun.target.matrixWorld.toArray(),
184
+ box: [box.left, box.right, box.top, box.bottom, box.near, box.far],
185
+ projection: box.projectionMatrix.toArray(),
186
+ boxAt: box.position.toArray(),
187
+ boxWorld: box.matrixWorld.toArray(),
188
+ shadowMatrix: own.matrix.toArray(),
189
+ mapSize: own.mapSize.toArray(),
190
+ map: target(own.map),
191
+ mapPass: target(own.mapPass),
192
+ maps: world.targets.filter((seen) => seen.of === own).map((seen) => seen.disposed),
193
+ bias: [own.normalBias, own.bias, own.intensity, own.radius],
194
+ userData: { ...own.userData },
195
+ ownMethods: Object.keys(own).filter((key) => typeof own[key] === 'function'),
196
+ castShadow: sun.castShadow,
197
+ inScene: sun.parent !== null,
198
+ update: [own.autoUpdate, own.needsUpdate],
199
+ };
200
+ }
201
+
202
+ function frames(world, runtime, count = 3) {
203
+ for (let i = 0; i < count; i++) renderWorld(world, runtime);
204
+ }
205
+
206
+ // The maps the engine allocated for its own shadow.
207
+ const mine = (world, own) => world.targets.filter((seen) => seen.of !== own && seen.of.camera !== undefined && !world.theirs?.includes(seen.of));
208
+
209
+ // Two worlds made alike. `steps` gets a world and `key(distance)`, which is a
210
+ // runtime that asked for that distance in the one and a runtime that asked for
211
+ // nothing in its twin. Their suns must end the same, and when the engine
212
+ // keeps no shadow of its own, every map it allocated was freed exactly once.
213
+ function sameAsWithoutTheKey(steps, { thenDispose = false } = {}) {
214
+ const keyed = makeWorld();
215
+ const twin = makeWorld();
216
+ steps(keyed, (distance) => asked(distance));
217
+ steps(twin, () => asked(0));
218
+ if (thenDispose) {
219
+ disposeWorld(keyed.world);
220
+ disposeWorld(twin.world);
221
+ }
222
+ assert.deepEqual(sunState(keyed.world, keyed.sun, keyed.own), sunState(twin.world, twin.sun, twin.own));
223
+ if (!keyed.world.nearShadows?.shadow) {
224
+ const freed = mine(keyed.world, keyed.own).map((seen) => seen.disposed);
225
+ assert.deepEqual(freed, freed.map(() => 1), "each of the engine's own maps is freed once");
226
+ }
227
+ if (thenDispose) assert.equal(keyed.world.nearShadows, null, 'nothing is kept');
228
+ assert.equal(mine(twin.world, twin.own).length, 0, 'the twin made none');
229
+ return keyed;
230
+ }
231
+
232
+ // What the engine's own shadow was in the last render.
233
+ function nearOf(world, own) {
234
+ const near = world.drew.at(-1);
235
+ assert.notEqual(near, own, "the render found the engine's own shadow on the sun");
236
+ return near;
237
+ }
238
+
239
+ const texelOf = (shadow) => [
240
+ (shadow.camera.right - shadow.camera.left) / shadow.mapSize.x,
241
+ (shadow.camera.top - shadow.camera.bottom) / shadow.mapSize.y,
242
+ ];
243
+
244
+ // A caster on the ground at x, z.
245
+ function caster(world, x, z) {
246
+ const mesh = new THREE.Mesh(new THREE.SphereGeometry(0.4), new THREE.MeshBasicMaterial());
247
+ mesh.position.set(x, 0.4, z);
248
+ mesh.castShadow = true;
249
+ world.scene.add(mesh);
250
+ return mesh;
251
+ }
252
+
253
+ const drewOwn = (world, own, from = 0) => world.drew.slice(from).every((shadow) => shadow === own);
254
+
255
+ const scenarios = {
256
+ 'a scene without the key leaves the sun alone'() {
257
+ const { world, sun, own } = makeWorld();
258
+ frames(world, asked(0), 1);
259
+ const first = sunState(world, sun, own);
260
+ frames(world, asked(0));
261
+ frames(world, undefined);
262
+ assert.deepEqual(sunState(world, sun, own), first);
263
+ assert.deepEqual(first.box, [-75, 75, 85, -35, 0.5, 50]);
264
+ assert.deepEqual(first.map, { size: [2048, 2048], filter: THREE.NearestFilter, disposed: 0 });
265
+ assert.deepEqual(first.position, [6, 18, -6]);
266
+ assert.deepEqual(first.ownMethods, []);
267
+ assert.equal(world.targets.length, 1);
268
+ assert.ok(drewOwn(world, own));
269
+ assert.equal(world.nearShadows, null);
270
+ },
271
+
272
+ 'an editor gives no runtime, and a scene with the key is drawn the one way'() {
273
+ const { world, sun, own } = makeWorld();
274
+ frames(world, undefined, 1);
275
+ const first = sunState(world, sun, own);
276
+ frames(world);
277
+ assert.deepEqual(sunState(world, sun, own), first);
278
+ assert.ok(drewOwn(world, own));
279
+ assert.equal(world.nearShadows, null);
280
+ },
281
+
282
+ "frames with the key write nothing of the sun's"() {
283
+ const { world, own } = sameAsWithoutTheKey(({ world: w }, key) => {
284
+ frames(w, asked(0), 2);
285
+ for (let i = 0; i < 20; i++) {
286
+ look(w.camera, (random() - 0.5) * 300, (random() - 0.5) * 300, random() * 7);
287
+ frames(w, key(40), 1);
288
+ }
289
+ });
290
+ assert.equal(world.drew.filter((shadow) => shadow !== own).length, 19, 'all but the first, which is drawn plain');
291
+ assert.equal(world.drew[2], own);
292
+ assert.equal(mine(world, own).length, 1);
293
+ },
294
+
295
+ 'a world that only ever drew with the key: its first frame is a plain one'() {
296
+ const { world, own } = sameAsWithoutTheKey(({ world: w }, key) => frames(w, key(40), 5), { thenDispose: true });
297
+ assert.deepEqual(world.drew.map((shadow) => shadow === own), [true, false, false, false, false]);
298
+ assert.deepEqual(world.targets.map((seen) => [seen.of === own, seen.disposed]), [
299
+ [true, 0],
300
+ [false, 1],
301
+ ]);
302
+ },
303
+
304
+ "the engine's own shadow has the sun's direction, depth range, texel size and bias"() {
305
+ const { world, sun, own } = makeWorld();
306
+ frames(world, asked(40));
307
+ const near = nearOf(world, own);
308
+ const box = near.camera;
309
+ assert.equal(near.isDirectionalLightShadow, true);
310
+ assert.deepEqual([box.near, box.far], [0.5, 50]);
311
+ assert.ok(box.right === -box.left && box.top === -box.bottom, 'a box round its camera');
312
+ assert.ok(box.right < 75, `smaller than the sun's own (${box.right})`);
313
+ assert.deepEqual(near.mapSize.toArray(), [near.map.width, near.map.height], 'three.js allocated its map at its size');
314
+ const [texel, ownTexel] = [texelOf(near), texelOf(own)];
315
+ assert.ok(Math.abs(texel[0] - ownTexel[0]) < 1e-12 && Math.abs(texel[1] - ownTexel[1]) < 1e-12, `texels the size of the sun's own (${texel})`);
316
+ assert.deepEqual([near.normalBias, near.bias, near.intensity, near.radius], [own.normalBias, own.bias, own.intensity, own.radius]);
317
+ assert.equal(own.normalBias, (150 / 2048) * 0.6, "which was fitted to the sun's own box");
318
+ // Its camera stands where the sun does from its target, over its own.
319
+ const toSun = sun.position.clone().sub(sun.target.position);
320
+ const back = new THREE.Vector3().setFromMatrixColumn(box.matrixWorld, 2);
321
+ assert.ok(back.distanceTo(toSun.clone().normalize()) < 1e-9, 'looking along the sun');
322
+ assert.ok(Math.abs(box.position.y - toSun.y) < 1e-9, 'from the height the sun is above its target');
323
+ assert.deepEqual([near.autoUpdate, sun.shadow === own], [true, true]);
324
+ },
325
+
326
+ "a deck's own bias, intensity and depth range are the engine's shadow's too"() {
327
+ const { world, sun, own } = makeWorld();
328
+ frames(world, asked(0), 1);
329
+ own.normalBias = 0.2;
330
+ own.intensity = 0.3;
331
+ own.camera.far = 80;
332
+ frames(world, asked(40));
333
+ const near = nearOf(world, own);
334
+ assert.deepEqual([near.normalBias, near.intensity, near.camera.far], [0.2, 0.3, 80]);
335
+ assert.deepEqual([own.normalBias, own.intensity, own.camera.far], [0.2, 0.3, 80]);
336
+ assert.equal(near.autoUpdate, true);
337
+ assert.equal(sun.shadow, own);
338
+ },
339
+
340
+ 'the box is one size whichever way the camera looks, and moves in whole texels'() {
341
+ const { world, own } = makeWorld();
342
+ frames(world, asked(40), 2);
343
+ const near = nearOf(world, own);
344
+ const box = near.camera;
345
+ const [right, top] = [box.right, box.top];
346
+ const size = near.mapSize.toArray();
347
+ const steps = texelOf(near);
348
+ const axisX = new THREE.Vector3().setFromMatrixColumn(box.matrixWorld, 0);
349
+ const axisY = new THREE.Vector3().setFromMatrixColumn(box.matrixWorld, 1);
350
+ const whole = (value, step) => Math.abs(value / step - Math.round(value / step)) < 1e-6;
351
+ for (let i = 0; i < 200; i++) {
352
+ look(world.camera, (random() - 0.5) * 300, (random() - 0.5) * 300, random() * 7, 2 + random() * 10);
353
+ frames(world, asked(40), 1);
354
+ assert.deepEqual([box.right, box.top, ...near.mapSize.toArray()], [right, top, ...size]);
355
+ assert.ok(whole(box.position.dot(axisX), steps[0]) && whole(box.position.dot(axisY), steps[1]), 'on the texel grid');
356
+ }
357
+ assert.equal(mine(world, own).length, 1, 'one map through all of it');
358
+ // Moved along the sun's direction, the box stays where it is; moved half a
359
+ // texel across in fifty steps, it moves at most once, by one texel.
360
+ look(world.camera, 3, 9, 0);
361
+ frames(world, asked(40), 1);
362
+ const at = box.position.clone();
363
+ world.camera.position.addScaledVector(new THREE.Vector3().setFromMatrixColumn(box.matrixWorld, 2), 1.7);
364
+ frames(world, asked(40), 1);
365
+ assert.ok(box.position.distanceTo(at) < 1e-9);
366
+ let moves = 0;
367
+ for (let i = 0; i < 50; i++) {
368
+ const last = box.position.clone();
369
+ world.camera.position.addScaledVector(axisX, steps[0] / 100);
370
+ frames(world, asked(40), 1);
371
+ const moved = box.position.clone().sub(last);
372
+ if (moved.length() < 1e-9) continue;
373
+ moves += 1;
374
+ assert.ok(Math.abs(Math.abs(moved.dot(axisX)) - steps[0]) < 1e-6 && Math.abs(moved.dot(axisY)) < 1e-6, 'by one texel');
375
+ }
376
+ assert.ok(moves <= 1, `moved ${moves} times in half a texel`);
377
+ },
378
+
379
+ 'no caster whose shadow can land in the view is culled, and one behind the camera is'() {
380
+ const { world, sun, own } = makeWorld();
381
+ // The depth range is the sun's own business; this is about the outline.
382
+ Object.assign(own.camera, { near: -1000, far: 1000 });
383
+ const distance = 40;
384
+ const toSun = sun.position.clone().sub(sun.target.position).normalize();
385
+ const mesh = new THREE.Mesh(new THREE.SphereGeometry(0.4));
386
+ const behind = caster(world, 0, 0);
387
+ let kept = 0;
388
+ let culled = 0;
389
+ frames(world, asked(distance), 1);
390
+ for (let i = 0; i < 300; i++) {
391
+ look(world.camera, (random() - 0.5) * 200, (random() - 0.5) * 200, random() * 7, 3 + random() * 8);
392
+ const forward = world.camera.getWorldDirection(new THREE.Vector3());
393
+ // On the ground behind the camera, inside the box: three.js alone would draw it.
394
+ behind.position.copy(world.camera.position).addScaledVector(forward.clone().setY(0).normalize(), -10).setY(0.4);
395
+ frames(world, asked(distance), 1);
396
+ const frustum = nearOf(world, own).getFrustum();
397
+ for (let j = 0; j < 10; j++) {
398
+ // A point the camera sees within the distance, and a caster between it and the sun.
399
+ const ray = new THREE.Vector3((random() - 0.5) * 2, (random() - 0.5) * 2, 0.5).unproject(world.camera).sub(world.camera.position);
400
+ const seen = ray.multiplyScalar((random() * distance) / ray.dot(forward)).add(world.camera.position);
401
+ mesh.position.copy(seen).addScaledVector(toSun, random() * 60);
402
+ mesh.updateMatrixWorld();
403
+ assert.ok(frustum.intersectsObject(mesh), `a caster over ${seen.toArray()} is drawn`);
404
+ kept += 1;
405
+ }
406
+ // Left out of the pass though the box alone would have drawn it.
407
+ if (!world.cast.includes(behind) && new THREE.Frustum().copy(frustum).intersectsObject(behind)) culled += 1;
408
+ }
409
+ assert.equal(kept, 3000);
410
+ assert.ok(culled > 100, `casters behind the camera left out of the pass (${culled})`);
411
+ },
412
+
413
+ // The six a review of the first version (727de2d1) broke it with.
414
+
415
+ "review 1.1: a map's dispose listener that draws a frame without the key"() {
416
+ const { world, own } = sameAsWithoutTheKey(({ world: w }, key) => {
417
+ // Any map being freed draws a frame of a scene without the key.
418
+ let entered = 0;
419
+ w.onDispose = () => {
420
+ entered += 1;
421
+ if (entered < 4) renderWorld(w, asked(0));
422
+ };
423
+ frames(w, asked(0), 2);
424
+ frames(w, key(40));
425
+ // The window changes shape: the engine's map is the wrong size.
426
+ w.camera.aspect = 2;
427
+ w.camera.updateProjectionMatrix();
428
+ frames(w, key(40));
429
+ frames(w, asked(0), 2);
430
+ });
431
+ assert.deepEqual(world.targets.filter((seen) => seen.of === own).map((seen) => seen.disposed), [0], "the sun's own map is never freed");
432
+ assert.equal(world.nearShadows.off, true, 'and the frame that was entered put the mode off');
433
+ },
434
+
435
+ 'review 1.2: a frame that throws while it is drawn'() {
436
+ const { world, own } = sameAsWithoutTheKey(({ world: w }, key) => {
437
+ frames(w, asked(0), 1);
438
+ frames(w, key(40));
439
+ w.onRender = () => {
440
+ throw new Error('the composer failed');
441
+ };
442
+ assert.throws(() => renderWorld(w, key(40)), /the composer failed/);
443
+ w.onRender = null;
444
+ });
445
+ assert.equal(world.nearShadows.frame, null);
446
+ assert.equal(world.nearShadows.shadow, null, 'the frame that threw left the mode');
447
+ assert.deepEqual(mine(world, own).map((seen) => seen.disposed), [1]);
448
+ // The player's loop stops on a throw; its cleanup is `disposeWorld`.
449
+ disposeWorld(world);
450
+ assert.equal(world.nearShadows, null);
451
+ assert.deepEqual(mine(world, own).map((seen) => seen.disposed), [1]);
452
+ },
453
+
454
+ 'review 1.3: a scene without the key that sets its sun up before its first frame'() {
455
+ const { world, sun, own } = sameAsWithoutTheKey(({ world: w, sun: s, own: o }, key) => {
456
+ frames(w, asked(0), 1);
457
+ frames(w, key(40));
458
+ // The next scene's own code, before the scene is first drawn.
459
+ s.target.position.set(100, 0, 100);
460
+ s.target.updateMatrixWorld();
461
+ s.position.set(106, 18, 94);
462
+ Object.assign(o.camera, { left: -6, right: 6, top: 6, bottom: -6 });
463
+ o.camera.updateProjectionMatrix();
464
+ frames(w, asked(0), 2);
465
+ });
466
+ assert.deepEqual(sun.target.position.toArray(), [100, 0, 100]);
467
+ assert.deepEqual([own.camera.left, own.camera.right, own.camera.top, own.camera.bottom], [-6, 6, 6, -6]);
468
+ assert.equal(own.normalBias, (12 / 2048) * 0.6, 'and the bias is fitted to its box');
469
+ assert.equal(world.nearShadows, null);
470
+ },
471
+
472
+ 'review 1.4: texels and bias are the same at 32:9 and at 200 m'() {
473
+ for (const [aspect, distance] of [
474
+ [32 / 9, 40],
475
+ [412 / 870, 200],
476
+ [32 / 9, 200],
477
+ [1, 5000],
478
+ ]) {
479
+ const { world, own } = makeWorld(aspect);
480
+ frames(world, asked(0), 1);
481
+ const bias = own.normalBias;
482
+ frames(world, asked(distance));
483
+ const near = nearOf(world, own);
484
+ const [texel, ownTexel] = [texelOf(near), texelOf(own)];
485
+ assert.ok(Math.abs(texel[0] - ownTexel[0]) < 1e-12, `${aspect} ${distance}: texels as wide (${texel[0]} / ${ownTexel[0]})`);
486
+ assert.ok(Math.abs(texel[1] - ownTexel[1]) < 1e-12, `${aspect} ${distance}: and as tall (${texel[1]} / ${ownTexel[1]})`);
487
+ assert.deepEqual([near.normalBias, own.normalBias], [bias, bias], `${aspect} ${distance}: the bias`);
488
+ assert.ok(near.mapSize.x * near.mapSize.y <= 2048 * 2048, `${aspect} ${distance}: a map no larger than the sun's own (${near.mapSize.toArray()})`);
489
+ assert.ok(Math.max(near.mapSize.x, near.mapSize.y) <= 4096, `${aspect} ${distance}: nor than the renderer's largest`);
490
+ assert.ok(near.reach < distance && near.reach > 10, `${aspect} ${distance}: drawn as far as fits (${near.reach})`);
491
+ }
492
+ // A renderer whose largest texture is small clamps the same way.
493
+ const { world, own } = makeWorld();
494
+ world.renderer.capabilities.maxTextureSize = 512;
495
+ frames(world, asked(40));
496
+ const near = nearOf(world, own);
497
+ assert.ok(Math.max(near.mapSize.x, near.mapSize.y) <= 512 && near.reach < 40);
498
+ assert.ok(Math.abs(texelOf(near)[1] - 120 / 2048) < 1e-12);
499
+ },
500
+
501
+ "review 1.5: the light's and the shadow camera's matrices after stopping and after disposing"() {
502
+ const farAway = ({ world: w }, key) => {
503
+ frames(w, asked(0), 1);
504
+ look(w.camera, 100, -80, 2);
505
+ frames(w, key(40));
506
+ };
507
+ // Stopped by a frame without the key.
508
+ sameAsWithoutTheKey((made, key) => {
509
+ farAway(made, key);
510
+ frames(made.world, asked(0), 1);
511
+ });
512
+ // Stopped by `disposeWorld`, with no frame after.
513
+ const { sun, own } = sameAsWithoutTheKey(farAway, { thenDispose: true });
514
+ assert.deepEqual(new THREE.Vector3().setFromMatrixPosition(sun.matrixWorld).toArray(), [6, 18, -6]);
515
+ assert.deepEqual(own.camera.position.toArray(), [6, 18, -6]);
516
+ },
517
+
518
+ "review 1.6: a getFrustum of the deck's own on the sun's shadow"() {
519
+ let theirs;
520
+ const { world, own } = sameAsWithoutTheKey(({ world: w, own: o }, key) => {
521
+ const frustum = function deckFrustum() {
522
+ return Object.getPrototypeOf(o).getFrustum.call(o);
523
+ };
524
+ if (key(1).nearShadowDistance) theirs = frustum;
525
+ o.getFrustum = frustum;
526
+ frames(w, asked(0), 1);
527
+ frames(w, key(40));
528
+ frames(w, asked(0), 1);
529
+ });
530
+ assert.equal(own.getFrustum, theirs);
531
+ assert.equal(world.nearShadows, null);
532
+ },
533
+
534
+ // The seven a review of the second (cf794377) broke it with. The answer to
535
+ // each is a term: the mode does not draw that frame.
536
+
537
+ 'review 2.1: renderWorld entered from the shadow pass (onBeforeShadow)'() {
538
+ const { world, sun, own } = sameAsWithoutTheKey(({ world: w, sun: s, own: o }, key) => {
539
+ look(w.camera, 120, 0, 0);
540
+ const first = caster(w, 120, -4);
541
+ caster(w, 121, -5);
542
+ frames(w, key(40));
543
+ let entered = false;
544
+ first.onBeforeShadow = () => {
545
+ if (entered) return;
546
+ entered = true;
547
+ renderWorld(w, key(40));
548
+ assert.equal(s.shadow, o, 'when the call inside returns the sun has its own shadow');
549
+ };
550
+ renderWorld(w, key(40));
551
+ // (Without the key the sun's fixed box does not reach this far, and the
552
+ // caster is never drawn.)
553
+ assert.equal(entered, Boolean(key(1).nearShadowDistance));
554
+ frames(w, key(40));
555
+ });
556
+ assert.equal(sun.shadow, own);
557
+ assert.equal(world.nearShadows.off, true, 'the mode is off for this world');
558
+ assert.equal(world.nearShadows.shadow, null);
559
+ assert.ok(drewOwn(world, own, -4), 'the call inside and every frame after are plain');
560
+ frames(world, asked(0), 2);
561
+ frames(world, asked(40));
562
+ assert.ok(drewOwn(world, own, -5) && world.nearShadows.off, 'and it stays off through a scene without the key');
563
+ disposeWorld(world);
564
+ assert.equal(world.nearShadows, null);
565
+ },
566
+
567
+ "review 2.2: a frame with the key drawn from the dispose of the engine's map as it is resized"() {
568
+ const { world, sun, own } = sameAsWithoutTheKey(({ world: w, sun: s, own: o }, key) => {
569
+ frames(w, key(40));
570
+ let entered = 0;
571
+ w.onDispose = (seen) => {
572
+ if (seen.of === o) return;
573
+ entered += 1;
574
+ if (entered > 1) return;
575
+ const drawn = w.drew.length;
576
+ renderWorld(w, key(40));
577
+ assert.ok(drewOwn(w, o, drawn), 'the frame inside is plain');
578
+ assert.equal(s.shadow, o);
579
+ };
580
+ look(w.camera, 120, 0, 0);
581
+ w.camera.aspect = 2;
582
+ w.camera.updateProjectionMatrix();
583
+ const drawn = w.drew.length;
584
+ renderWorld(w, key(40));
585
+ assert.ok(drewOwn(w, o, drawn), 'and so is the frame it interrupted, which had drawn nothing yet');
586
+ assert.equal(w.drew.length - drawn, key(1).nearShadowDistance ? 2 : 1);
587
+ frames(w, key(40));
588
+ });
589
+ assert.equal(sun.shadow, own);
590
+ assert.equal(world.nearShadows.off, true);
591
+ assert.ok(drewOwn(world, own, -3));
592
+ },
593
+
594
+ "review 2.4: a blur map on the engine's shadow is freed wherever its map is"() {
595
+ const other = makeWorld();
596
+ frames(other.world, asked(40));
597
+ const near = nearOf(other.world, other.own);
598
+ near.mapPass = new THREE.WebGLRenderTarget(4, 4);
599
+ noteTargets(other.world, near);
600
+ other.world.camera.aspect = 2;
601
+ other.world.camera.updateProjectionMatrix();
602
+ frames(other.world, asked(40), 1);
603
+ assert.deepEqual(mine(other.world, other.own).map((seen) => seen.disposed), [1, 1, 0], 'on a resize, before the next map is made');
604
+ assert.equal(near.mapPass, null);
605
+ near.mapPass = new THREE.WebGLRenderTarget(4, 4);
606
+ noteTargets(other.world, near);
607
+ frames(other.world, asked(0), 1);
608
+ assert.deepEqual(mine(other.world, other.own).map((seen) => seen.disposed), [1, 1, 1, 1], 'and when the mode is dropped');
609
+ },
610
+
611
+ 'review 2.5: the world disposed by a call from inside a frame with the key'() {
612
+ const { world, sun, own } = sameAsWithoutTheKey(
613
+ ({ world: w, sun: s, own: o }, key) => {
614
+ frames(w, key(40));
615
+ let depth = 0;
616
+ w.onRender = () => {
617
+ depth += 1;
618
+ if (depth === 1) {
619
+ renderWorld(w, key(40));
620
+ assert.deepEqual([w.disposed, w.nearShadows, s.shadow === o], [true, null, true], 'when the call inside returns');
621
+ } else if (depth === 2) {
622
+ disposeWorld(w);
623
+ assert.equal(s.shadow, o, 'its own shadow is back at once');
624
+ }
625
+ };
626
+ renderWorld(w, key(40));
627
+ w.onRender = null;
628
+ assert.equal(depth, 2);
629
+ },
630
+ { thenDispose: true },
631
+ );
632
+ // A frame after it does not start again.
633
+ const drawn = world.drew.length;
634
+ frames(world, asked(40));
635
+ assert.ok(drewOwn(world, own, drawn));
636
+ assert.equal(world.nearShadows, null);
637
+ assert.equal(sun.shadow, own);
638
+ },
639
+
640
+ 'review 2.6: the sun taken out of the scene and another put in'() {
641
+ let other;
642
+ const { world, sun, own } = sameAsWithoutTheKey(({ world: w, sun: s }, key) => {
643
+ frames(w, key(40));
644
+ w.scene.remove(s);
645
+ const next = makeSun();
646
+ next.shadow.camera.right = 30;
647
+ w.scene.add(next);
648
+ w.theirs = [next.shadow];
649
+ if (key(1).nearShadowDistance) other = next;
650
+ frames(w, key(40));
651
+ });
652
+ assert.ok(drewOwn(world, own, -3), 'the light that left is given nothing');
653
+ assert.equal(sun.shadow, own);
654
+ assert.equal(world.nearShadows.shadow, null);
655
+ assert.equal(other.shadow.map.width, 2048, 'and the one that came draws its own box, as without the key');
656
+ assert.equal(other.shadow.isDirectionalLightShadow && other.shadow.camera.right, 30);
657
+ // Put back, it is the scene's casting light again.
658
+ world.scene.remove(other);
659
+ world.scene.add(sun);
660
+ frames(world, asked(40));
661
+ assert.notEqual(world.drew.at(-1), own);
662
+ // A second casting light ahead of it in the scene: not the mode's to draw.
663
+ world.scene.remove(sun);
664
+ world.scene.add(other, sun);
665
+ frames(world, asked(40));
666
+ assert.ok(drewOwn(world, own, -3));
667
+ // And one that stops casting.
668
+ world.scene.remove(other);
669
+ frames(world, asked(40));
670
+ sun.castShadow = false;
671
+ frames(world, asked(40), 1);
672
+ assert.equal(world.nearShadows.shadow, null, 'dropped when the sun stops casting');
673
+ },
674
+
675
+ 'review 2.7: shadows are complete out to the distance; what is said about farther is that it is not promised'() {
676
+ const { world, own } = makeWorld();
677
+ // A level camera: the ground 45 m ahead is in view, past the distance.
678
+ world.camera.position.set(0, 2, 0);
679
+ world.camera.lookAt(0, 2, -10);
680
+ const far = caster(world, 0, -45);
681
+ const near = caster(world, 0, -30);
682
+ frames(world, asked(40));
683
+ nearOf(world, own);
684
+ assert.ok(world.cast.includes(near), 'a caster within the distance is drawn');
685
+ assert.ok(world.cast.includes(far), 'and this one past it happens to be: nothing cuts receivers off');
686
+ for (const file of ['../engine3d/nearshadows.js', '../CLAUDE.md', '../../../experiments/hub-world/README.md']) {
687
+ const text = fs.readFileSync(new URL(file, import.meta.url), 'utf8').replace(/\s*\n\s*(\/\/|>)?\s*/g, ' ');
688
+ assert.ok(!/past `distance` nothing is shadowed|drawn only out to|shadows stop `distance`/.test(text), `${file} does not say shadows stop at the distance`);
689
+ }
690
+ },
691
+
692
+ // The four a review of the third (61a9cff9) found.
693
+
694
+ 'review 3.1: a caster over where the shader looks a visible point up, a normal bias away'() {
695
+ const { world, own } = makeWorld();
696
+ // A deck's own small box and large bias: 12 m, 0.2 m.
697
+ Object.assign(own.camera, { left: -6, right: 6, top: 6, bottom: -6, near: -1000, far: 1000 });
698
+ own.camera.updateProjectionMatrix();
699
+ frames(world, asked(0), 1);
700
+ own.normalBias = 0.2;
701
+ const mesh = new THREE.Mesh(new THREE.SphereGeometry(0.001));
702
+ let checked = 0;
703
+ for (let i = 0; i < 200; i++) {
704
+ look(world.camera, (random() - 0.5) * 20, (random() - 0.5) * 20, random() * 7, 2 + random() * 3);
705
+ frames(world, asked(40), i === 0 ? 2 : 1);
706
+ const near = nearOf(world, own);
707
+ assert.equal(near.normalBias, 0.2);
708
+ const frustum = near.getFrustum();
709
+ const forward = world.camera.getWorldDirection(new THREE.Vector3());
710
+ for (let j = 0; j < 20; j++) {
711
+ // A point on the edge of the view within the distance drawn; the shader
712
+ // looks its shadow up at the point moved along its normal by the bias,
713
+ // which may be any way; a caster there, or a filter tap off, shadows it.
714
+ const edge = j % 2 ? [Math.sign(random() - 0.5), (random() - 0.5) * 2] : [(random() - 0.5) * 2, Math.sign(random() - 0.5)];
715
+ const ray = new THREE.Vector3(edge[0], edge[1], 0.5).unproject(world.camera).sub(world.camera.position);
716
+ const seen = ray.multiplyScalar((random() * near.reach) / ray.dot(forward)).add(world.camera.position);
717
+ const normal = new THREE.Vector3(random() - 0.5, random() - 0.5, random() - 0.5).normalize();
718
+ const tap = new THREE.Vector3().setFromMatrixColumn(near.camera.matrixWorld, j % 3 ? 0 : 1).multiplyScalar((random() - 0.5) * 6 * texelOf(near)[0]);
719
+ mesh.position.copy(seen).addScaledVector(normal, 0.2).add(tap);
720
+ mesh.updateMatrixWorld();
721
+ assert.ok(frustum.intersectsObject(mesh), `a caster at the lookup for ${seen.toArray()} is drawn`);
722
+ checked += 1;
723
+ }
724
+ }
725
+ assert.equal(checked, 4000);
726
+ },
727
+
728
+ 'review 3.2: a frame that throws is no frame before the next'() {
729
+ const other = makeWorld();
730
+ frames(other.world, asked(40));
731
+ other.world.onRender = () => {
732
+ throw new Error('while the mode drew');
733
+ };
734
+ assert.throws(() => renderWorld(other.world, asked(40)), /while the mode drew/);
735
+ other.world.onRender = null;
736
+ frames(other.world, asked(40), 2);
737
+ assert.deepEqual(other.world.drew.slice(-2).map((shadow) => shadow === other.own), [true, false]);
738
+ // Nor is the first frame with the key, when that is the one that throws.
739
+ const fresh = makeWorld();
740
+ fresh.world.onRender = () => {
741
+ throw new Error('the first frame');
742
+ };
743
+ assert.throws(() => renderWorld(fresh.world, asked(40)), /the first frame/);
744
+ fresh.world.onRender = null;
745
+ frames(fresh.world, asked(40), 2);
746
+ assert.deepEqual(fresh.world.drew.slice(-2).map((shadow) => shadow === fresh.own), [true, false]);
747
+ },
748
+
749
+ 'review 3.3: a frame drawn plain for any term frees what the mode had'() {
750
+ const terms = {
751
+ 'a camera that is not a perspective one'(w) {
752
+ w.camera = new THREE.OrthographicCamera(-5, 5, 5, -5, 0.1, 100);
753
+ },
754
+ 'another shadow type'(w) {
755
+ w.renderer.shadowMap.type = THREE.PCFShadowMap;
756
+ },
757
+ 'a sun that stopped casting'(w, sun) {
758
+ sun.castShadow = false;
759
+ },
760
+ 'a sun out of the scene'(w, sun) {
761
+ w.scene.remove(sun);
762
+ },
763
+ "a box of the sun's own with no size"(w, sun) {
764
+ sun.shadow.camera.right = sun.shadow.camera.left;
765
+ },
766
+ };
767
+ for (const [name, change] of Object.entries(terms)) {
768
+ const { world, sun, own } = makeWorld();
769
+ frames(world, asked(40));
770
+ const near = nearOf(world, own);
771
+ const map = near.map;
772
+ change(world, sun);
773
+ renderWorld(world, asked(40));
774
+ assert.equal(world.drew.at(-1), own, `${name}: drawn plain`);
775
+ assert.equal(world.nearShadows.shadow, null, `${name}: the mode's shadow is gone`);
776
+ assert.deepEqual([near.map, world.targets.find((seen) => seen.target === map).disposed], [null, 1], `${name}: and its map freed, at once`);
777
+ }
778
+ },
779
+
780
+ 'review 3.4: a frame that was re-entered leaves nothing allocated when it returns'() {
781
+ const { world, own } = makeWorld();
782
+ frames(world, asked(40));
783
+ const near = nearOf(world, own);
784
+ const map = world.targets.find((seen) => seen.target === near.map);
785
+ world.onRender = () => {
786
+ world.onRender = null;
787
+ renderWorld(world, asked(40));
788
+ assert.deepEqual([world.nearShadows.shadow, near.map, map.disposed], [null, null, 1], 'freed when the call inside returns');
789
+ };
790
+ renderWorld(world, asked(40));
791
+ assert.deepEqual([world.nearShadows.off, world.nearShadows.shadow, map.disposed], [true, null, 1]);
792
+ assert.equal(mine(world, own).length, 1, 'and the frame it interrupted made no other');
793
+ },
794
+
795
+ // The three a review of the fourth (bf446241) found.
796
+
797
+ 'review 4.1: a frame on which three.js will not draw the shadow pass is drawn plain'() {
798
+ // The sun's own settings, which a deck with the key may change: the world
799
+ // ends as its twin does.
800
+ const suns = {
801
+ "the sun's own shadow not due an update"(sun) {
802
+ sun.shadow.autoUpdate = false;
803
+ },
804
+ 'the sun hidden'(sun) {
805
+ sun.visible = false;
806
+ },
807
+ 'the sun on a layer the camera does not see'(sun) {
808
+ sun.layers.set(3);
809
+ },
810
+ };
811
+ for (const [name, change] of Object.entries(suns)) {
812
+ const { world, own } = sameAsWithoutTheKey(({ world: w, sun }, key) => {
813
+ frames(w, key(40));
814
+ // The window changes shape too, so the mode's map would be a new one.
815
+ w.camera.aspect = 2;
816
+ w.camera.updateProjectionMatrix();
817
+ change(sun);
818
+ frames(w, key(40), 2);
819
+ });
820
+ assert.ok(drewOwn(world, own, -2), `${name}: drawn plain`);
821
+ assert.equal(world.nearShadows.shadow, null, name);
822
+ }
823
+ // The renderer's shadow settings, which a deck with the key must leave
824
+ // alone: nothing is promised of such a deck's shadows, but the frame is
825
+ // plain and nothing of the mode's is left.
826
+ const settings = {
827
+ 'the shadow map not due an update'(pass) {
828
+ pass.autoUpdate = false;
829
+ },
830
+ 'the shadow map disabled'(pass) {
831
+ pass.enabled = false;
832
+ },
833
+ 'another shadow type'(pass) {
834
+ pass.type = THREE.PCFShadowMap;
835
+ },
836
+ };
837
+ for (const [name, change] of Object.entries(settings)) {
838
+ const { world, own } = makeWorld();
839
+ frames(world, asked(40));
840
+ change(world.renderer.shadowMap);
841
+ frames(world, asked(40), 2);
842
+ assert.ok(drewOwn(world, own, -2), `${name}: drawn plain`);
843
+ assert.equal(world.nearShadows.shadow, null, name);
844
+ }
845
+ // And a frame the mode drew that ends with no map on its shadow (three.js
846
+ // drew nothing: the context was lost) is not one the mode drew.
847
+ const { world, own } = makeWorld();
848
+ frames(world, asked(40));
849
+ world.camera.aspect = 2;
850
+ world.camera.updateProjectionMatrix();
851
+ world.contextLost = true;
852
+ renderWorld(world, asked(40));
853
+ world.contextLost = false;
854
+ assert.notEqual(world.drew.at(-1), own, 'the mode took the frame');
855
+ assert.equal(world.nearShadows.shadow, null, 'and left it when it ended with no map');
856
+ frames(world, asked(40), 2);
857
+ assert.deepEqual(world.drew.slice(-2).map((shadow) => shadow === own), [true, false], 'the next frame is plain');
858
+ },
859
+
860
+ 'review 4.2: fitting the bias throws after frames the mode drew'() {
861
+ const throwers = {
862
+ 'a bias that throws when read'(sun) {
863
+ const was = Object.getOwnPropertyDescriptor(sun.shadow, 'normalBias');
864
+ Object.defineProperty(sun.shadow, 'normalBias', {
865
+ configurable: true,
866
+ get() {
867
+ throw new Error('no bias');
868
+ },
869
+ });
870
+ return () => Object.defineProperty(sun.shadow, 'normalBias', was);
871
+ },
872
+ 'a sun with no shadow'(sun) {
873
+ const own = sun.shadow;
874
+ sun.shadow = null;
875
+ return () => {
876
+ sun.shadow = own;
877
+ };
878
+ },
879
+ };
880
+ for (const [name, change] of Object.entries(throwers)) {
881
+ const { world, own } = makeWorld();
882
+ const sun = world.scene.children[0];
883
+ frames(world, asked(40));
884
+ const near = nearOf(world, own);
885
+ const map = world.targets.find((seen) => seen.target === near.map);
886
+ const undo = change(sun);
887
+ assert.throws(() => renderWorld(world, asked(40)), undefined, name);
888
+ assert.deepEqual([world.nearShadows.shadow, near.map, map.disposed, world.nearShadows.depth], [null, null, 1, 0], `${name}: nothing of the mode's is left`);
889
+ undo();
890
+ assert.equal(sun.shadow, own);
891
+ frames(world, asked(40), 2);
892
+ assert.deepEqual(world.drew.slice(-2).map((shadow) => shadow === own), [true, false], `${name}: the frame that threw is no frame before`);
893
+ }
894
+ },
895
+
896
+ "review 4.3: frames with the key drawn from the dispose of the mode's map as a scene without the key drops it"() {
897
+ const { world, own } = sameAsWithoutTheKey(({ world: w, own: o }, key) => {
898
+ frames(w, key(40));
899
+ let entered = 0;
900
+ w.onDispose = (seen) => {
901
+ if (seen.of === o) return;
902
+ entered += 1;
903
+ if (entered > 1) return;
904
+ const drawn = w.drew.length;
905
+ renderWorld(w, key(40));
906
+ renderWorld(w, key(40));
907
+ assert.ok(drewOwn(w, o, drawn), 'both are plain');
908
+ assert.equal(w.nearShadows.shadow, null, 'and make nothing');
909
+ };
910
+ frames(w, asked(0), 1);
911
+ w.onDispose = null;
912
+ assert.equal(entered, key(1).nearShadowDistance ? 1 : 0);
913
+ frames(w, key(40), 3);
914
+ });
915
+ assert.deepEqual(mine(world, own).map((seen) => seen.disposed), [1], 'the one map the mode ever made');
916
+ assert.equal(world.nearShadows.off, true, 'the call that was entered put the mode off');
917
+ assert.ok(drewOwn(world, own, -6), 'so no frame after follows a frame with the key');
918
+ },
919
+
920
+ // The three a review of the fifth (219e9ca2) found.
921
+
922
+ "review 5.2: the frame throws and then the dispose listener of the mode's map throws"() {
923
+ const { world, sun, own } = makeWorld();
924
+ frames(world, asked(40));
925
+ const near = nearOf(world, own);
926
+ const map = world.targets.find((seen) => seen.target === near.map);
927
+ near.mapPass = new THREE.WebGLRenderTarget(4, 4);
928
+ noteTargets(world, near);
929
+ world.onRender = () => {
930
+ throw new Error('the frame failed');
931
+ };
932
+ world.onDispose = (seen) => {
933
+ if (seen === map) throw new Error('the listener failed');
934
+ };
935
+ assert.throws(() => renderWorld(world, asked(40)), /the listener failed/);
936
+ world.onRender = null;
937
+ world.onDispose = null;
938
+ assert.deepEqual([world.nearShadows.depth, world.nearShadows.shadow, world.nearShadows.frame, world.nearShadows.off], [0, null, null, false], 'not counted as inside a call, nothing attached, not off');
939
+ assert.deepEqual([sun.shadow === own, near.map, near.mapPass], [true, null, null]);
940
+ assert.deepEqual(mine(world, own).map((seen) => seen.disposed), [1, 1], 'and the blur map was freed though the map listener threw');
941
+ frames(world, asked(40), 3);
942
+ assert.deepEqual(world.drew.slice(-3).map((shadow) => shadow === own), [true, false, false], 'the mode draws again after one plain frame');
943
+ // The same from `disposeWorld`: the rest of the world is released all the same.
944
+ world.onDispose = () => {
945
+ throw new Error('the listener failed');
946
+ };
947
+ let released = 0;
948
+ world.renderer.dispose = () => {
949
+ released += 1;
950
+ };
951
+ assert.throws(() => disposeWorld(world), /the listener failed/);
952
+ world.onDispose = null;
953
+ assert.deepEqual([world.nearShadows, released, sun.shadow === own], [null, 1, true]);
954
+ },
955
+
956
+ "review 5.3: the depth range is the sun's own, and said to be"() {
957
+ const { world, sun, own } = makeWorld();
958
+ // The origin is in view, 11.4 m from the camera; casters up the light from it.
959
+ const toSun = sun.position.clone().normalize();
960
+ const within = caster(world, 0, 0);
961
+ within.position.copy(toSun).multiplyScalar(15);
962
+ const beyond = caster(world, 0, 0);
963
+ beyond.position.copy(toSun).multiplyScalar(25);
964
+ const farSide = caster(world, 0, 0);
965
+ farSide.position.copy(toSun).multiplyScalar(-25);
966
+ frames(world, asked(0), 1);
967
+ const plain = [within, beyond, farSide].map((mesh) => world.cast.includes(mesh));
968
+ frames(world, asked(40));
969
+ nearOf(world, own);
970
+ const near = [within, beyond, farSide].map((mesh) => world.cast.includes(mesh));
971
+ assert.deepEqual(plain, [true, false, true], "the sun's own camera does not draw a caster 25 m up the light");
972
+ assert.deepEqual(near, plain, "and the mode's draws what it draws");
973
+ for (const file of ['../engine3d/nearshadows.js', '../CLAUDE.md']) {
974
+ const text = fs.readFileSync(new URL(file, import.meta.url), 'utf8').replace(/\s*\n\s*(\/\/|>)?\s*/g, ' ');
975
+ assert.ok(/complete out to `distance` for casters (inside|within) /.test(text), `${file} limits the claim to the depth range`);
976
+ assert.ok(text.includes('19.4 m toward the sun and 30.1 m away'), `${file} gives the range for the engine's sun`);
977
+ }
978
+ },
979
+
980
+ // And what the rule has to hold through besides.
981
+
982
+ "nothing of the renderer's is written"() {
983
+ const { world } = makeWorld();
984
+ const pass = world.renderer.shadowMap;
985
+ const before = { render: pass.render, keys: Object.keys(pass).sort(), values: [pass.enabled, pass.autoUpdate, pass.needsUpdate, pass.type] };
986
+ const now = () => ({ render: pass.render, keys: Object.keys(pass).sort(), values: [pass.enabled, pass.autoUpdate, pass.needsUpdate, pass.type] });
987
+ world.onRender = () => assert.deepEqual(now(), before, 'while a frame is drawn');
988
+ frames(world, asked(40));
989
+ frames(world, asked(0), 2);
990
+ frames(world, asked(40));
991
+ world.onRender = () => {
992
+ throw new Error('a frame fails');
993
+ };
994
+ assert.throws(() => renderWorld(world, asked(40)), /a frame fails/);
995
+ world.onRender = null;
996
+ assert.deepEqual(now(), before);
997
+ disposeWorld(world);
998
+ assert.deepEqual(now(), before);
999
+ assert.equal(world.replacedPass, 0);
1000
+ },
1001
+
1002
+ 'disposing the world between frames'() {
1003
+ const { world, own } = sameAsWithoutTheKey(
1004
+ ({ world: w }, key) => {
1005
+ frames(w, asked(0), 1);
1006
+ frames(w, key(40));
1007
+ },
1008
+ { thenDispose: true },
1009
+ );
1010
+ const drawn = world.drew.length;
1011
+ frames(world, asked(40));
1012
+ assert.ok(drewOwn(world, own, drawn));
1013
+ assert.equal(world.nearShadows, null);
1014
+ },
1015
+
1016
+ "review 7.1: a sun whose shadow camera is zoomed or offset is drawn plain"() {
1017
+ for (const set of [(camera) => { camera.zoom = 2; }, (camera) => camera.setViewOffset(2048, 2048, 0, 0, 1024, 1024)]) {
1018
+ const { world, own } = makeWorld();
1019
+ frames(world, asked(40));
1020
+ set(own.camera);
1021
+ own.camera.updateProjectionMatrix();
1022
+ const drawn = world.drew.length;
1023
+ frames(world, asked(40));
1024
+ assert.ok(drewOwn(world, own, drawn), 'the zoomed sun draws its own shadow');
1025
+ nothingAllocatedUnlessDrawn(world);
1026
+ }
1027
+ },
1028
+
1029
+ 'a camera that is not a perspective one, a shadow replaced mid-frame'() {
1030
+ const { world, sun, own } = makeWorld();
1031
+ frames(world, asked(40));
1032
+ world.camera = new THREE.OrthographicCamera(-5, 5, 5, -5, 0.1, 100);
1033
+ const drawn = world.drew.length;
1034
+ frames(world, asked(40));
1035
+ assert.ok(drewOwn(world, own, drawn));
1036
+ assert.equal(world.nearShadows.frame, null);
1037
+ // A deck that gives the sun another shadow while a frame is drawn keeps it.
1038
+ world.camera = new THREE.PerspectiveCamera(50, 1, 0.1, 1000);
1039
+ const theirs = sun.shadow.clone();
1040
+ world.onRender = () => {
1041
+ sun.shadow = theirs;
1042
+ };
1043
+ renderWorld(world, asked(40));
1044
+ world.onRender = null;
1045
+ assert.equal(sun.shadow, theirs);
1046
+ frames(world, asked(40));
1047
+ assert.equal(sun.shadow, theirs);
1048
+ },
1049
+ };
1050
+
1051
+ export async function run() {
1052
+ let count = 0;
1053
+ const failed = [];
1054
+ for (const [name, scenario] of Object.entries(scenarios)) {
1055
+ if (process.env.ONLY && !name.includes(process.env.ONLY)) continue;
1056
+ try {
1057
+ worlds.length = 0;
1058
+ scenario();
1059
+ worlds.forEach(nothingAllocatedUnlessDrawn);
1060
+ count += 1;
1061
+ } catch (err) {
1062
+ err.message = `${name}: ${err.message}`;
1063
+ if (!process.env.KEEP_GOING) throw err;
1064
+ failed.push(`FAILED ${err.message.split('\n')[0]}`);
1065
+ }
1066
+ }
1067
+ if (failed.length) throw new Error(`${failed.length} near-shadow scenarios failed\n${failed.join('\n')}`);
1068
+ return count;
1069
+ }
1070
+
1071
+ if (process.argv[1] === new URL(import.meta.url).pathname) {
1072
+ console.log(`near shadows: ${await run()} scenarios`);
1073
+ }