castle-web-cli 0.4.231 → 0.4.233

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 (368) hide show
  1. package/dist/agent-failures.d.ts +8 -0
  2. package/dist/agent-failures.js +19 -2
  3. package/dist/agent-prompts.d.ts +1 -1
  4. package/dist/agent-prompts.js +10 -3
  5. package/dist/agent.d.ts +64 -3
  6. package/dist/agent.js +170 -81
  7. package/dist/castle-host/host.d.ts +1 -0
  8. package/dist/castle-host/host.js +51 -3
  9. package/dist/castleJson.d.ts +4 -2
  10. package/dist/castleJson.js +2 -0
  11. package/dist/check.d.ts +3 -1
  12. package/dist/check.js +11 -1
  13. package/dist/devSessionStorage.js +47 -1
  14. package/dist/importPinMismatch.d.ts +22 -0
  15. package/dist/importPinMismatch.js +77 -0
  16. package/dist/importRegression.js +3 -15
  17. package/dist/imports.d.ts +10 -3
  18. package/dist/imports.js +88 -20
  19. package/dist/index.js +5 -4
  20. package/dist/init.js +1 -1
  21. package/dist/platformDoc.js +22 -0
  22. package/dist/routerFences.d.ts +5 -0
  23. package/dist/routerFences.js +12 -2
  24. package/dist/save-deck.d.ts +5 -0
  25. package/dist/save-deck.js +37 -4
  26. package/dist/serve.js +3 -0
  27. package/dist/serverBundle.js +5 -3
  28. package/dist/shell/assets/{index-D2tWM896.js → index-BjOxR-lW.js} +4 -4
  29. package/dist/shell/assets/index-MVLfNYd9.css +1 -0
  30. package/dist/shell/index.html +2 -2
  31. package/dist/taskSteer.d.ts +13 -0
  32. package/dist/taskSteer.js +22 -2
  33. package/kits/base/castle.json +1 -1
  34. package/kits/base/package-lock.json +1 -1
  35. package/kits/base/sdk/README.md +79 -2
  36. package/kits/base/sdk/avatar.d.ts +6 -2
  37. package/kits/base/sdk/avatar.js +12 -4
  38. package/kits/base/sdk/castle.d.ts +1 -1
  39. package/kits/base/sdk/commands.d.ts +14 -0
  40. package/kits/base/sdk/hub.d.ts +6 -2
  41. package/kits/base/sdk/hub.js +13 -0
  42. package/kits/base/sdk/multiplayer.js +17 -1
  43. package/kits/base/sdk/save.js +2 -1
  44. package/kits/base/sdk/server/hub.d.ts +87 -0
  45. package/kits/base/sdk/server/hub.js +70 -0
  46. package/kits/base/sdk/server/platformHandle.d.ts +31 -1
  47. package/kits/base/sdk/server/storage.d.ts +13 -0
  48. package/kits/base/sdk/server/storage.js +46 -0
  49. package/kits/base/sdk/server/wrapper.d.ts +36 -5
  50. package/kits/base/sdk/server/wrapper.js +83 -8
  51. package/kits/hub-game/CLAUDE.md +352 -12
  52. package/kits/hub-game/blueprints/avatar-10.scene +28 -0
  53. package/kits/hub-game/blueprints/avatar-11.scene +28 -0
  54. package/kits/hub-game/blueprints/avatar-12.scene +28 -0
  55. package/kits/hub-game/blueprints/avatar-13.scene +28 -0
  56. package/kits/hub-game/blueprints/avatar-14.scene +28 -0
  57. package/kits/hub-game/blueprints/avatar-15.scene +28 -0
  58. package/kits/hub-game/blueprints/avatar-4.scene +28 -0
  59. package/kits/hub-game/blueprints/avatar-5.scene +28 -0
  60. package/kits/hub-game/blueprints/avatar-6.scene +28 -0
  61. package/kits/hub-game/blueprints/avatar-7.scene +28 -0
  62. package/kits/hub-game/blueprints/avatar-8.scene +28 -0
  63. package/kits/hub-game/blueprints/avatar-9.scene +28 -0
  64. package/kits/hub-game/code/client/actorBudget.js +47 -0
  65. package/kits/hub-game/code/client/attention.js +11 -2
  66. package/kits/hub-game/code/client/avatar.js +345 -32
  67. package/kits/hub-game/code/client/avatarCap.js +228 -0
  68. package/kits/hub-game/code/client/avatarColors.js +70 -0
  69. package/kits/hub-game/code/client/avatarFaces.js +21 -0
  70. package/kits/hub-game/code/client/avatarShare.js +251 -0
  71. package/kits/hub-game/code/client/ballot.js +467 -0
  72. package/kits/hub-game/code/client/build.js +22 -0
  73. package/kits/hub-game/code/client/chat.js +12 -0
  74. package/kits/hub-game/code/client/chrome.js +1334 -50
  75. package/kits/hub-game/code/client/companion.js +716 -0
  76. package/kits/hub-game/code/client/companionDress.js +99 -0
  77. package/kits/hub-game/code/client/decor.js +509 -0
  78. package/kits/hub-game/code/client/earned.js +131 -0
  79. package/kits/hub-game/code/client/emoteBubble.js +75 -0
  80. package/kits/hub-game/code/client/grab.js +363 -20
  81. package/kits/hub-game/code/client/hats.js +26 -0
  82. package/kits/hub-game/code/client/hookLayer.js +328 -15
  83. package/kits/hub-game/code/client/hudClear.js +126 -0
  84. package/kits/hub-game/code/client/idle.js +163 -22
  85. package/kits/hub-game/code/client/interestCap.js +81 -0
  86. package/kits/hub-game/code/client/keys.js +36 -0
  87. package/kits/hub-game/code/client/labelsNear.js +205 -0
  88. package/kits/hub-game/code/client/menuIcons.js +34 -0
  89. package/kits/hub-game/code/client/pack.js +6 -2
  90. package/kits/hub-game/code/client/packFormat.js +3 -3
  91. package/kits/hub-game/code/client/rehello.js +167 -0
  92. package/kits/hub-game/code/client/shot.js +92 -0
  93. package/kits/hub-game/code/client/sound.js +63 -8
  94. package/kits/hub-game/code/client/soundscape.js +1151 -0
  95. package/kits/hub-game/code/client/soundscapeActions.js +99 -0
  96. package/kits/hub-game/code/client/soundscapeAmbience.js +453 -0
  97. package/kits/hub-game/code/client/soundscapeBaked.js +143 -0
  98. package/kits/hub-game/code/client/soundscapeBusker.js +217 -0
  99. package/kits/hub-game/code/client/soundscapeCourt.js +200 -0
  100. package/kits/hub-game/code/client/soundscapeCrowd.js +331 -0
  101. package/kits/hub-game/code/client/soundscapeCues.js +362 -0
  102. package/kits/hub-game/code/client/soundscapeMap.js +181 -0
  103. package/kits/hub-game/code/client/soundscapeMarket.js +176 -0
  104. package/kits/hub-game/code/client/soundscapeMusic.js +126 -0
  105. package/kits/hub-game/code/client/soundscapeMusicEngine.js +460 -0
  106. package/kits/hub-game/code/client/soundscapeSimlish.js +737 -0
  107. package/kits/hub-game/code/client/soundscapeSongs.js +127 -0
  108. package/kits/hub-game/code/client/soundscapeSongsRound3.js +63 -0
  109. package/kits/hub-game/code/client/soundscapeSynthesis.js +168 -0
  110. package/kits/hub-game/code/client/soundscapeUi.js +211 -0
  111. package/kits/hub-game/code/client/soundscapeVoice.js +1209 -0
  112. package/kits/hub-game/code/client/soundscapeWaves.js +206 -0
  113. package/kits/hub-game/code/client/spin.js +101 -0
  114. package/kits/hub-game/code/client/state.js +131 -1
  115. package/kits/hub-game/code/client/stuck.js +259 -0
  116. package/kits/hub-game/code/client/talk.js +538 -47
  117. package/kits/hub-game/code/client/tiers.js +541 -27
  118. package/kits/hub-game/code/client/toastBand.js +37 -0
  119. package/kits/hub-game/code/client/together.js +98 -1
  120. package/kits/hub-game/code/client/visibility.js +43 -0
  121. package/kits/hub-game/code/client/wear.js +470 -97
  122. package/kits/hub-game/code/client/yours.js +199 -0
  123. package/kits/hub-game/code/companionSpec.js +45 -0
  124. package/kits/hub-game/code/privateText.js +7 -0
  125. package/kits/hub-game/code/server/actions.js +133 -0
  126. package/kits/hub-game/code/server/actors.js +14 -1
  127. package/kits/hub-game/code/server/ask.js +155 -0
  128. package/kits/hub-game/code/server/ballot.js +539 -0
  129. package/kits/hub-game/code/server/cells.js +100 -0
  130. package/kits/hub-game/code/server/companion.js +82 -0
  131. package/kits/hub-game/code/server/cues.js +70 -0
  132. package/kits/hub-game/code/server/decor.js +269 -0
  133. package/kits/hub-game/code/server/earned.js +461 -0
  134. package/kits/hub-game/code/server/friendBests.js +166 -0
  135. package/kits/hub-game/code/server/friendsAmong.js +232 -0
  136. package/kits/hub-game/code/server/grab.js +296 -11
  137. package/kits/hub-game/code/server/history.js +5 -0
  138. package/kits/hub-game/code/server/hub.js +1317 -52
  139. package/kits/hub-game/code/server/idle.js +30 -11
  140. package/kits/hub-game/code/server/index.js +2 -2
  141. package/kits/hub-game/code/server/labels.js +28 -2
  142. package/kits/hub-game/code/server/manifest.js +424 -8
  143. package/kits/hub-game/code/server/menu.js +133 -0
  144. package/kits/hub-game/code/server/npcs.js +830 -80
  145. package/kits/hub-game/code/server/party.js +13 -2
  146. package/kits/hub-game/code/server/players.js +22 -0
  147. package/kits/hub-game/code/server/say.js +8 -2
  148. package/kits/hub-game/code/server/shot.js +110 -0
  149. package/kits/hub-game/code/server/standing.js +124 -0
  150. package/kits/hub-game/code/server/vis.js +113 -0
  151. package/kits/hub-game/code/server/wear.js +128 -11
  152. package/kits/hub-game/code/server/yours.js +59 -0
  153. package/kits/hub-game/code/shared/cues.js +9 -0
  154. package/kits/hub-game/code/shared/decor.js +41 -0
  155. package/kits/hub-game/code/shared/shot.js +19 -0
  156. package/kits/hub-game/code/shared/spin.js +57 -0
  157. package/kits/hub-game/code/systems/hub.js +1083 -85
  158. package/kits/hub-game/docs/content-pack.md +1 -1
  159. package/kits/hub-game/docs/contract.md +2440 -61
  160. package/kits/hub-game/docs/home-pieces.md +64 -0
  161. package/kits/hub-game/models/avatar-10.pxmodel +28 -0
  162. package/kits/hub-game/models/avatar-11.pxmodel +28 -0
  163. package/kits/hub-game/models/avatar-12.pxmodel +27 -0
  164. package/kits/hub-game/models/avatar-13.pxmodel +24 -0
  165. package/kits/hub-game/models/avatar-14.pxmodel +29 -0
  166. package/kits/hub-game/models/avatar-15.pxmodel +24 -0
  167. package/kits/hub-game/models/avatar-4.pxmodel +29 -0
  168. package/kits/hub-game/models/avatar-5.pxmodel +27 -0
  169. package/kits/hub-game/models/avatar-6.pxmodel +26 -0
  170. package/kits/hub-game/models/avatar-7.pxmodel +25 -0
  171. package/kits/hub-game/models/avatar-8.pxmodel +24 -0
  172. package/kits/hub-game/models/avatar-9.pxmodel +30 -0
  173. package/kits/hub-game/models/earned/badge-round.pxmodel +100 -0
  174. package/kits/hub-game/models/earned/badge-shield.pxmodel +117 -0
  175. package/kits/hub-game/models/earned/badge-star.pxmodel +87 -0
  176. package/kits/hub-game/models/earned/belt-plain.pxmodel +72 -0
  177. package/kits/hub-game/models/earned/held-ball.pxmodel +43 -0
  178. package/kits/hub-game/models/earned/held-lantern.pxmodel +85 -0
  179. package/kits/hub-game/models/earned/held-net.pxmodel +57 -0
  180. package/kits/hub-game/models/earned/held-trophy.pxmodel +109 -0
  181. package/kits/hub-game/models/earned/pin-round.pxmodel +62 -0
  182. package/kits/hub-game/models/earned/sash-plain.pxmodel +66 -0
  183. package/kits/hub-game/models/earned/square/belt-plain.pxmodel +104 -0
  184. package/kits/hub-game/models/earned/square/sash-plain.pxmodel +98 -0
  185. package/kits/hub-game/models/hats/bat-clip.pxmodel +206 -0
  186. package/kits/hub-game/models/hats/beekeeper.pxmodel +149 -0
  187. package/kits/hub-game/models/hats/cap.pxmodel +162 -0
  188. package/kits/hub-game/models/hats/captain.pxmodel +178 -0
  189. package/kits/hub-game/models/hats/crooked-top.pxmodel +172 -0
  190. package/kits/hub-game/models/hats/flower-crown.pxmodel +451 -0
  191. package/kits/hub-game/models/hats/foam-crown.pxmodel +179 -0
  192. package/kits/hub-game/models/hats/lantern.pxmodel +230 -0
  193. package/kits/hub-game/models/hats/laurel.pxmodel +533 -0
  194. package/kits/hub-game/models/hats/lighthouse.pxmodel +96 -0
  195. package/kits/hub-game/models/hats/sailor.pxmodel +141 -0
  196. package/kits/hub-game/models/hats/souwester.pxmodel +131 -0
  197. package/kits/hub-game/models/hats/straw.pxmodel +139 -0
  198. package/kits/hub-game/models/hats/sunflower.pxmodel +406 -0
  199. package/kits/hub-game/models/hats/sweatband.pxmodel +112 -0
  200. package/kits/hub-game/models/hats/witch.pxmodel +147 -0
  201. package/kits/hub-game/package-lock.json +2 -0
  202. package/kits/hub-game/package.json +2 -1
  203. package/kits/hub-game/scripts/check.mjs +78 -11
  204. package/kits/hub-game/scripts/companion-follower-wire.json +1 -0
  205. package/kits/hub-game/scripts/fixtures/decor-keyless-draw.json +121 -0
  206. package/kits/hub-game/scripts/fixtures/decor-keyless.json +293 -0
  207. package/kits/hub-game/scripts/fixtures/engine-models-stub.mjs +23 -0
  208. package/kits/hub-game/scripts/fixtures/fake-dom.mjs +107 -0
  209. package/kits/hub-game/scripts/fixtures/interest-legacy.json +385 -0
  210. package/kits/hub-game/scripts/fixtures/npc-notes-v1.77.jsonl +4 -0
  211. package/kits/hub-game/scripts/fixtures/vis-shared-wire.json +1 -0
  212. package/kits/hub-game/scripts/gen-avatars.mjs +933 -16
  213. package/kits/hub-game/scripts/gen-earned.mjs +285 -0
  214. package/kits/hub-game/scripts/gen-hats.mjs +827 -0
  215. package/kits/hub-game/scripts/grab.before-orbit.mjs +400 -0
  216. package/kits/hub-game/scripts/soundscape-wave-analysis.mjs +125 -0
  217. package/kits/hub-game/scripts/test-actions-client.mjs +488 -0
  218. package/kits/hub-game/scripts/test-actions.mjs +298 -0
  219. package/kits/hub-game/scripts/test-actor-budget.mjs +44 -0
  220. package/kits/hub-game/scripts/test-actors.mjs +19 -17
  221. package/kits/hub-game/scripts/test-ask-client.mjs +358 -0
  222. package/kits/hub-game/scripts/test-ask.mjs +269 -0
  223. package/kits/hub-game/scripts/test-attention.mjs +10 -1
  224. package/kits/hub-game/scripts/test-avatar-cap.mjs +379 -0
  225. package/kits/hub-game/scripts/test-avatar-colors.mjs +72 -0
  226. package/kits/hub-game/scripts/test-avatar-models.mjs +181 -0
  227. package/kits/hub-game/scripts/test-avatar-share.mjs +318 -0
  228. package/kits/hub-game/scripts/test-avatar-wire.mjs +753 -0
  229. package/kits/hub-game/scripts/test-avatar.mjs +540 -7
  230. package/kits/hub-game/scripts/test-ballot.mjs +1001 -0
  231. package/kits/hub-game/scripts/test-build.mjs +31 -0
  232. package/kits/hub-game/scripts/test-cells.mjs +71 -0
  233. package/kits/hub-game/scripts/test-chat.mjs +694 -0
  234. package/kits/hub-game/scripts/test-classic.mjs +2 -0
  235. package/kits/hub-game/scripts/test-companion-chat.mjs +198 -0
  236. package/kits/hub-game/scripts/test-companion-client.mjs +1167 -0
  237. package/kits/hub-game/scripts/test-companion.mjs +259 -0
  238. package/kits/hub-game/scripts/test-decor-client.mjs +303 -0
  239. package/kits/hub-game/scripts/test-decor.mjs +457 -0
  240. package/kits/hub-game/scripts/test-earned-client.mjs +459 -0
  241. package/kits/hub-game/scripts/test-earned.mjs +1194 -0
  242. package/kits/hub-game/scripts/test-emote-bubble.mjs +110 -0
  243. package/kits/hub-game/scripts/test-friend-bests.mjs +435 -0
  244. package/kits/hub-game/scripts/test-grab-client.mjs +186 -3
  245. package/kits/hub-game/scripts/test-grab-orbit.mjs +918 -0
  246. package/kits/hub-game/scripts/test-grab.mjs +864 -3
  247. package/kits/hub-game/scripts/test-hats.mjs +483 -0
  248. package/kits/hub-game/scripts/test-hub-leaving.mjs +203 -0
  249. package/kits/hub-game/scripts/test-hub-ops.mjs +439 -2
  250. package/kits/hub-game/scripts/test-hub-trip.mjs +1228 -1
  251. package/kits/hub-game/scripts/test-hud-clear.mjs +1000 -0
  252. package/kits/hub-game/scripts/test-idle.mjs +361 -1
  253. package/kits/hub-game/scripts/test-interest-cap.mjs +36 -0
  254. package/kits/hub-game/scripts/test-interest-cells.mjs +493 -0
  255. package/kits/hub-game/scripts/test-interest-client.mjs +118 -0
  256. package/kits/hub-game/scripts/test-keys.mjs +46 -1
  257. package/kits/hub-game/scripts/test-labels.mjs +596 -0
  258. package/kits/hub-game/scripts/test-manifest.mjs +282 -0
  259. package/kits/hub-game/scripts/test-menu-client.mjs +480 -0
  260. package/kits/hub-game/scripts/test-menu.mjs +296 -0
  261. package/kits/hub-game/scripts/test-npcs.mjs +1718 -12
  262. package/kits/hub-game/scripts/test-object-entry.mjs +76 -0
  263. package/kits/hub-game/scripts/test-pack.mjs +7 -0
  264. package/kits/hub-game/scripts/test-party.mjs +22 -1
  265. package/kits/hub-game/scripts/test-picker.mjs +314 -0
  266. package/kits/hub-game/scripts/test-rehello.mjs +1039 -0
  267. package/kits/hub-game/scripts/test-relations-among.mjs +222 -0
  268. package/kits/hub-game/scripts/test-relations-refresh.mjs +188 -0
  269. package/kits/hub-game/scripts/test-say.mjs +59 -2
  270. package/kits/hub-game/scripts/test-shot.mjs +756 -0
  271. package/kits/hub-game/scripts/test-soundscape.mjs +5432 -0
  272. package/kits/hub-game/scripts/test-spin.mjs +314 -0
  273. package/kits/hub-game/scripts/test-standing.mjs +416 -0
  274. package/kits/hub-game/scripts/test-stuck.mjs +1903 -0
  275. package/kits/hub-game/scripts/test-tier-cells.mjs +706 -0
  276. package/kits/hub-game/scripts/test-tiers-render.mjs +339 -0
  277. package/kits/hub-game/scripts/test-tiers.mjs +666 -3
  278. package/kits/hub-game/scripts/test-toast-band.mjs +61 -0
  279. package/kits/hub-game/scripts/test-try-on.mjs +940 -0
  280. package/kits/hub-game/scripts/test-vis.mjs +343 -0
  281. package/kits/hub-game/scripts/test-visibility.mjs +134 -0
  282. package/kits/hub-game/scripts/test-yours-chrome.mjs +166 -0
  283. package/kits/hub-game/scripts/test-yours.mjs +372 -0
  284. package/kits/multiplayer-3d/CLAUDE.md +50 -0
  285. package/kits/multiplayer-3d/castle.json +5 -5
  286. package/kits/multiplayer-3d/code/client/avatars.js +7 -0
  287. package/kits/multiplayer-3d/code/client/gait.js +65 -0
  288. package/kits/multiplayer-3d/code/client/nameTags.js +59 -4
  289. package/kits/multiplayer-3d/code/options.js +5 -0
  290. package/kits/multiplayer-3d/code/server/ownership.js +10 -2
  291. package/kits/multiplayer-3d/code/server/sceneBodies.js +12 -0
  292. package/kits/multiplayer-3d/code/server/world.js +7 -0
  293. package/kits/multiplayer-3d/code/systems/multiplayer.js +74 -4
  294. package/kits/multiplayer-3d/package.json +2 -1
  295. package/kits/multiplayer-3d/scripts/fixtures/README.md +11 -0
  296. package/kits/multiplayer-3d/scripts/fixtures/ownership-legacy.json +2 -0
  297. package/kits/multiplayer-3d/scripts/ownership-harness.mjs +102 -0
  298. package/kits/multiplayer-3d/scripts/ownership-loader.mjs +46 -0
  299. package/kits/multiplayer-3d/scripts/ownership-worker.mjs +15 -0
  300. package/kits/multiplayer-3d/scripts/record-ownership-session.mjs +98 -0
  301. package/kits/multiplayer-3d/scripts/test-eligible-claims.mjs +167 -0
  302. package/kits/multiplayer-3d/scripts/test-own-name-tag.mjs +247 -0
  303. package/kits/physics-2d/CLAUDE.md +9 -0
  304. package/kits/physics-2d/castle.json +2 -2
  305. package/kits/physics-2d/engine/scene.js +288 -1
  306. package/kits/physics-2d/engine/skyKey.js +129 -0
  307. package/kits/physics-2d/engine/stillClock.js +106 -0
  308. package/kits/physics-2d/engine/updateBudget.js +21 -0
  309. package/kits/physics-2d/scripts/test-skip-unchanged.mjs +370 -1
  310. package/kits/physics-3d/CLAUDE.md +1391 -0
  311. package/kits/physics-3d/behaviors/Player.jsx +921 -3
  312. package/kits/physics-3d/castle.json +3 -3
  313. package/kits/physics-3d/docs/pxmodel-format.md +12 -0
  314. package/kits/physics-3d/engine3d/Scene3DPlayer.jsx +98 -10
  315. package/kits/physics-3d/engine3d/actornode.js +86 -0
  316. package/kits/physics-3d/engine3d/animationBudget.js +23 -0
  317. package/kits/physics-3d/engine3d/editorWorld.js +6 -0
  318. package/kits/physics-3d/engine3d/emptynodes.js +232 -0
  319. package/kits/physics-3d/engine3d/framecap.js +96 -0
  320. package/kits/physics-3d/engine3d/gait.js +118 -0
  321. package/kits/physics-3d/engine3d/kithooks.js +47 -0
  322. package/kits/physics-3d/engine3d/look.js +507 -48
  323. package/kits/physics-3d/engine3d/lookbake.js +208 -0
  324. package/kits/physics-3d/engine3d/materialpresets.js +2 -0
  325. package/kits/physics-3d/engine3d/motion.js +39 -10
  326. package/kits/physics-3d/engine3d/nearshadows.js +37 -19
  327. package/kits/physics-3d/engine3d/optimize.js +58 -5
  328. package/kits/physics-3d/engine3d/orbitinput.js +755 -0
  329. package/kits/physics-3d/engine3d/orbitlens.js +48 -0
  330. package/kits/physics-3d/engine3d/ownmodel.js +213 -0
  331. package/kits/physics-3d/engine3d/pxmodel.js +42 -11
  332. package/kits/physics-3d/engine3d/shadowfade.js +882 -0
  333. package/kits/physics-3d/engine3d/sky.js +547 -0
  334. package/kits/physics-3d/engine3d/skylife.js +306 -0
  335. package/kits/physics-3d/engine3d/unchanged.js +11 -0
  336. package/kits/physics-3d/engine3d/viewrange.js +52 -0
  337. package/kits/physics-3d/engine3d/world3d.js +870 -87
  338. package/kits/physics-3d/main.jsx +5 -0
  339. package/kits/physics-3d/package.json +1 -1
  340. package/kits/physics-3d/scripts/Player.before-orbit.mjs +310 -0
  341. package/kits/physics-3d/scripts/Scene3DPlayer.before-orbit.jsx +337 -0
  342. package/kits/physics-3d/scripts/look-baseline.mjs +155 -0
  343. package/kits/physics-3d/scripts/test-camera-follow.mjs +12 -2
  344. package/kits/physics-3d/scripts/test-camera-orbit.mjs +4588 -0
  345. package/kits/physics-3d/scripts/test-drop-empty-parts.mjs +787 -0
  346. package/kits/physics-3d/scripts/test-frame-cap-bob.mjs +435 -0
  347. package/kits/physics-3d/scripts/test-frame-cap.mjs +452 -0
  348. package/kits/physics-3d/scripts/test-gait.mjs +387 -0
  349. package/kits/physics-3d/scripts/test-look-bake.mjs +1056 -0
  350. package/kits/physics-3d/scripts/test-model-parse-cache.mjs +92 -0
  351. package/kits/physics-3d/scripts/test-near-shadows.mjs +3 -0
  352. package/kits/physics-3d/scripts/test-orbit-shadow-fade.mjs +1920 -0
  353. package/kits/physics-3d/scripts/test-orbit-shift-lock.mjs +545 -0
  354. package/kits/physics-3d/scripts/test-orbit-shot.mjs +583 -0
  355. package/kits/physics-3d/scripts/test-orbit-view.mjs +230 -0
  356. package/kits/physics-3d/scripts/test-skip-empty-nodes.mjs +845 -0
  357. package/kits/physics-3d/scripts/test-sky.mjs +2470 -0
  358. package/kits/physics-3d/scripts/test-static-actors.mjs +13 -1
  359. package/kits/physics-3d/scripts/test-static-passes.mjs +1 -1
  360. package/kits/physics-3d/scripts/test-static-scenery.mjs +2171 -0
  361. package/kits/physics-3d/scripts/test-steady-hover.mjs +635 -0
  362. package/kits/physics-3d/scripts/test-view-range.mjs +260 -0
  363. package/kits/physics-3d/systems/physics3d.js +474 -12
  364. package/kits/real-time/castle.json +2 -2
  365. package/kits/real-time/code/server/gameHooks.js +7 -0
  366. package/kits/real-time/code/server/session.js +97 -19
  367. package/package.json +2 -2
  368. package/dist/shell/assets/index-Cf61cNfT.css +0 -1
@@ -0,0 +1,882 @@
1
+ // The far edge of the near shadows, for a scene that sets both
2
+ // `"engine": { "shadows": { "distance": n } }` and the orbit camera key
3
+ // (`"camera": { "follow": "rigid", "control": "orbit" }`).
4
+ //
5
+ // On a frame the near-shadow mode draws (nearshadows.js) the sun's shadow is
6
+ // drawn into the mode's own map, a box round the view out to `reach` (the
7
+ // distance it could fit, measured along the way the camera looks). Past the
8
+ // box's edge nothing is shadowed: three.js finds the point outside the map
9
+ // and leaves it lit. Under a fixed camera looking down that edge is off
10
+ // screen; under the orbit camera, which the player can tilt to the
11
+ // horizontal, it is a hard line across the ground. So under both keys the
12
+ // sun's shadow strength fades out over the last quarter of the reach: whole
13
+ // up to FADE_FROM of it, gone at it, eased between (smoothstep), to full sun,
14
+ // which is what lies beyond.
15
+ //
16
+ // How, without touching anything the keyless world draws: world-local
17
+ // material variants, put on the meshes only for the keyed world's own render
18
+ // call, and never a change to a material.
19
+ //
20
+ // - What gets a variant: a material whose class is exactly one of three's
21
+ // own lit ones (standard, physical, lambert, phong, toon, by identity: a
22
+ // subclass is never faded, so no other constructor ever runs) and whose
23
+ // `onBeforeCompile`, `customProgramCacheKey` and `onBeforeRender` are
24
+ // three's own or the kit's (kithooks.js: look.js, optimize.js,
25
+ // materialpresets.js, none of which writes anything of the material's),
26
+ // alone on a mesh (a mesh with an array of materials is not faded: the
27
+ // player's models have none, optimize.js splits per-face art into one
28
+ // mesh per face) that receives shadows and whose own `onBeforeRender`
29
+ // and `onAfterRender` are three's (they are handed the material
30
+ // drawn). And a frame is faded only when the scene has no
31
+ // `onBeforeRender` of its own (it runs inside the render while the
32
+ // variants are on). Anything else is drawn as it is, unfaded: no deck
33
+ // code ever runs on, or is handed, a variant, so whatever it writes
34
+ // lands where it would without the key.
35
+ // - A variant is a separate material object to the renderer that reads
36
+ // and writes the original's properties. Its prototype, shared by the
37
+ // material class, has a forwarding accessor (`this[ORIGINAL][key]`,
38
+ // read and written) for every own property a material of the class has
39
+ // and every accessor of the class (enumerable or not, symbols
40
+ // included); below that, in place of the class's prototype, a proxy
41
+ // that answers any other key from the original (a property only this
42
+ // original has, a method) and writes any other key on it, and gives
43
+ // that key a forwarder of its own so it does not come back (read on
44
+ // anything but a variant, the shared prototype itself included, both
45
+ // fall through to the class's prototype). So every
46
+ // read is the original's, live, and every write through a variant
47
+ // (deck code inside the frame that reaches one: a callback installed
48
+ // mid-frame, handed the material three listed; an LOD's `update`;
49
+ // three's own side flip and version bumps) lands on the original as it
50
+ // would without the key. A property defined on the variant itself is
51
+ // put on the original after the frame (and before the next), and one
52
+ // that cannot then be taken off the variant drops it. Its events are
53
+ // the original's: what is added, removed, asked about or dispatched
54
+ // through it, and its `dispose`, act on the original, except the
55
+ // renderer's own dispose listener for the variant's program, known by
56
+ // its function (learned from the renderer itself, by compiling a
57
+ // private material once): that one is held by the variant (and added to the original as well, where
58
+ // the renderer drawing it without the key would have it), and the
59
+ // variant's program is let go of through it, when the variant is
60
+ // disposed or dropped, or when a dispose listener has gone from the
61
+ // original (it was disposed). The variant's own: an `id` (three sorts,
62
+ // binds and keeps programs apart by it), that listener, and the two
63
+ // shader hooks, held as they were when it was made: `onBeforeCompile`
64
+ // runs the held hook then adds the fade, and `customProgramCacheKey` is
65
+ // the held key's plus `|near-shadow-fade`; written, they write the
66
+ // original's. The original is never written but by what is written
67
+ // through the variant, as without the key.
68
+ // - Before each faded frame a variant is drawn in: if the original no
69
+ // longer has exactly the hooks it holds, or is no longer one to fade,
70
+ // the variant is dropped (and a new one made if it still is). A hook the
71
+ // original is given inside the frame, after the variants went on, is
72
+ // never run for the variant: it compiles with the held hook, under a key
73
+ // no other material has, and is dropped on the next frame.
74
+ // - Each keyed world keeps its own variants (`world.shadowFade`), made the
75
+ // first time a material is drawn faded and kept while it is in the scene.
76
+ // A variant not drawn for SWEEP_EVERY
77
+ // faded frames is disposed. Every one is disposed on a scene load (the
78
+ // physics system's `reset`), an editor's frame, any frame of a scene
79
+ // without both keys and `disposeWorld`; one dropped from inside a render
80
+ // is disposed once the outermost `renderWorld` is over.
81
+ // - `begin`, called by the near-shadow mode right before its one render
82
+ // call of a frame it draws, puts each variant on its mesh and puts a
83
+ // hook on the world renderer's `shadowMap.render` (an own property of
84
+ // three's WebGLShadowMap, which the renderer calls through the object):
85
+ // three's `render` calls it on every render after listing the draws
86
+ // (reading `mesh.material`) and before drawing any, whatever the shadow
87
+ // settings, and the hook puts the originals back. `end`, when the frame
88
+ // gives the sun its own shadow back (nearshadows.js `giveBack`, whatever
89
+ // the render did), puts the originals back too and takes the hook off.
90
+ // A hook a deck replaced meanwhile is left replaced; ours, if the
91
+ // deck's still calls it, has let go of the fade and only calls the pass
92
+ // it had. A mesh whose material was changed meanwhile keeps the new
93
+ // one. The hook goes on before any variant does, and a pass that cannot
94
+ // take it (read-only) draws the frame unfaded. Putting back is total
95
+ // (`putBack`): each mesh on its own, a material a deck made read-only
96
+ // but configurable redefined with the deck's attributes and the
97
+ // original's value, an accessor a deck put there with a setter given
98
+ // the original through it once (its getter never called), and anything
99
+ // else held (`stuck`) and tried at every frame boundary; such a mesh
100
+ // keeps its variant, which reads and writes the original, until then.
101
+ // A variant the fade has let go of (dropped, or the fade itself gone)
102
+ // is inert: it keys and compiles as its original, with no fade.
103
+ //
104
+ // So no material and nothing of another world is written by the fade, and
105
+ // whatever deck code writes through a variant lands where it would without
106
+ // the key; a keyless world sharing a material compiles the same program
107
+ // from it, draws the same draws and sees the same writes. The keyed frame's
108
+ // own draws use the variants, and every render after its list of draws sees
109
+ // the originals. What is left, inside the keyed world's own frame: a
110
+ // variant is another object, so deck code that reaches one (an LOD's
111
+ // `update`, a callback installed mid-frame, a sort comparator) and compares
112
+ // it by identity with the original, or asks it `key in`, or deletes a
113
+ // property through it, does not see the original; and a property defined
114
+ // on a variant reaches the original after the frame rather than at once.
115
+ // The eligibility rule above keeps the scene's, the faded meshes' and the
116
+ // faded materials' own callbacks off variants all the same. A hook or
117
+ // callback that changes in place an object the original holds by reference
118
+ // (its colour, a texture, its defines) changes it for every world, as it
119
+ // would without the key.
120
+ //
121
+ // The patch multiplies the strength the sun's shadow is drawn with (three's
122
+ // `shadowIntensity`, in lights_fragment_begin) by the fade, for the first
123
+ // directional light that casts. `begin` checks, in the same walk, that the
124
+ // sun is that light as the renderer will order them (the first casting
125
+ // directional light in the scene, visible and on the camera's layers, which
126
+ // three's light sort keeps first); if not, the frame is not faded.
127
+ import * as THREE from 'three';
128
+ import { knownCompileHook, knownProgramKey, knownDrawCallback } from './kithooks';
129
+
130
+ // The share of the reach the shadow is whole to.
131
+ export const FADE_FROM = 0.75;
132
+
133
+ // Faded frames a variant is kept without being drawn.
134
+ const SWEEP_EVERY = 60;
135
+
136
+ const INCLUDE = '#include <lights_fragment_begin>';
137
+ const TARGET = 'directionalLightShadow.shadowIntensity, directionalLightShadow.shadowBias';
138
+ const DECLARE = `uniform vec2 nearShadowFade;
139
+ float nearShadowFadeAt( const in float depth ) {
140
+ return 1.0 - smoothstep( nearShadowFade.x, nearShadowFade.y, depth );
141
+ }
142
+ `;
143
+ let lights = null;
144
+ function fadedLights() {
145
+ return (lights ??= THREE.ShaderChunk.lights_fragment_begin.replace(
146
+ TARGET,
147
+ 'directionalLightShadow.shadowIntensity * ( UNROLLED_LOOP_INDEX == 0 ? nearShadowFadeAt( vViewPosition.z ) : 1.0 ), directionalLightShadow.shadowBias'
148
+ ));
149
+ }
150
+ // A three.js whose chunk no longer has the line: nothing is faded.
151
+ const patchable = () => fadedLights() !== THREE.ShaderChunk.lights_fragment_begin;
152
+
153
+ export const PROGRAM_KEY_SUFFIX = '|near-shadow-fade';
154
+
155
+ // The source change, on a variant's shader. A shader with no lights is left
156
+ // as it is.
157
+ export function patchShadowFade(shader, uniform) {
158
+ if (!shader.fragmentShader.includes(INCLUDE) || shader.fragmentShader.includes('nearShadowFadeAt')) return;
159
+ shader.uniforms.nearShadowFade = uniform;
160
+ shader.fragmentShader = DECLARE + shader.fragmentShader.replace(INCLUDE, fadedLights());
161
+ }
162
+
163
+ // three's own lit material classes, by identity: a material is lit here
164
+ // only when its prototype is exactly one of theirs, so no other class (and
165
+ // no other constructor) is ever involved in making a variant.
166
+ const LIT_CLASSES = new Map(
167
+ [THREE.MeshStandardMaterial, THREE.MeshPhysicalMaterial, THREE.MeshLambertMaterial, THREE.MeshPhongMaterial, THREE.MeshToonMaterial].map(
168
+ (Class) => [Class.prototype, Class]
169
+ )
170
+ );
171
+
172
+ function lit(material) {
173
+ return material !== null && typeof material === 'object' && LIT_CLASSES.has(Object.getPrototypeOf(material));
174
+ }
175
+
176
+ const own = (object, key) => Object.prototype.hasOwnProperty.call(object, key);
177
+
178
+ // Whether a material gets a variant: lit, and its shader hook, key function
179
+ // and draw callback are three's own or the kit's (kithooks.js), none of which
180
+ // writes anything of the material's. One with a deck's own is drawn as it
181
+ // is, unfaded.
182
+ function fadeable(material) {
183
+ return (
184
+ lit(material) &&
185
+ knownCompileHook(dataValue(material, 'onBeforeCompile')) &&
186
+ knownProgramKey(dataValue(material, 'customProgramCacheKey')) &&
187
+ knownDrawCallback(dataValue(material, 'onBeforeRender')) &&
188
+ dataValue(material, '_listeners') !== ACCESSOR
189
+ );
190
+ }
191
+
192
+ // What an object has for `key` along its prototype chain, read from the
193
+ // property descriptors so no getter is ever called: the value of a data
194
+ // property, ACCESSOR for an accessor (a deck's getter; such a material, or
195
+ // mesh, is never faded), undefined for none. The fade's own reads of an
196
+ // original (its hooks, its listeners) and of a mesh (its material, whether
197
+ // it receives shadows) go through this, so they call nothing a deck
198
+ // defined.
199
+ const ACCESSOR = Symbol('accessor');
200
+
201
+ // The same, for a property the fade will assign: ACCESSOR too when an
202
+ // assignment would not land (read-only, or inherited by an object that
203
+ // cannot take an own property).
204
+ function writableValue(object, key) {
205
+ for (let level = object; level !== null && level !== undefined; level = Object.getPrototypeOf(level)) {
206
+ const descriptor = Reflect.getOwnPropertyDescriptor(level, key);
207
+ if (!descriptor) continue;
208
+ if (!('value' in descriptor) || descriptor.writable !== true) return ACCESSOR;
209
+ if (level !== object && !Object.isExtensible(object)) return ACCESSOR;
210
+ return descriptor.value;
211
+ }
212
+ return ACCESSOR;
213
+ }
214
+
215
+ // A mesh's own `material`, if it is one the fade can swap and swap back to
216
+ // exactly what it was: an own data property, writable and configurable.
217
+ // ACCESSOR otherwise (an inherited material too: the swap would leave the
218
+ // mesh an own property in place of the inherited one).
219
+ function ownMaterial(mesh) {
220
+ const descriptor = Reflect.getOwnPropertyDescriptor(mesh, 'material');
221
+ if (!descriptor || !('value' in descriptor) || descriptor.writable !== true || descriptor.configurable !== true) return ACCESSOR;
222
+ return descriptor.value;
223
+ }
224
+
225
+ function dataValue(object, key) {
226
+ for (let level = object; level !== null && level !== undefined; level = Object.getPrototypeOf(level)) {
227
+ const descriptor = Reflect.getOwnPropertyDescriptor(level, key);
228
+ if (descriptor) return 'value' in descriptor ? descriptor.value : ACCESSOR;
229
+ }
230
+ return undefined;
231
+ }
232
+
233
+ // Whether a mesh's own callbacks are three's: they are handed the material
234
+ // being drawn, which on a faded frame is the variant.
235
+ const OBJECT_BEFORE = THREE.Object3D.prototype.onBeforeRender;
236
+ const OBJECT_AFTER = THREE.Object3D.prototype.onAfterRender;
237
+ function plainObject(object, protos) {
238
+ const hasOwn = Object.prototype.hasOwnProperty;
239
+ if (hasOwn.call(object, 'onBeforeRender') || hasOwn.call(object, 'onAfterRender') || !protos) {
240
+ return dataValue(object, 'onBeforeRender') === OBJECT_BEFORE && dataValue(object, 'onAfterRender') === OBJECT_AFTER;
241
+ }
242
+ // Neither is the object's own: what its prototype has, looked at once a
243
+ // frame for each prototype (`protos`).
244
+ const proto = Object.getPrototypeOf(object);
245
+ let plain = protos.get(proto);
246
+ if (plain === undefined) {
247
+ plain = dataValue(proto, 'onBeforeRender') === OBJECT_BEFORE && dataValue(proto, 'onAfterRender') === OBJECT_AFTER;
248
+ protos.set(proto, plain);
249
+ }
250
+ return plain;
251
+ }
252
+
253
+ // A variant's original, and its own state (`events`), on the variant.
254
+ const ORIGINAL = Symbol('nearShadowFadeOriginal');
255
+ const STATE = Symbol('nearShadowFadeState');
256
+
257
+ // What a variant has of its own; every other property, read or written,
258
+ // is the original's.
259
+ const OWN = new Set([ORIGINAL, STATE, 'id', 'onBeforeCompile', 'customProgramCacheKey']);
260
+
261
+ // The accessor that reads and writes the original's `key` (`this` is the
262
+ // variant): one pair of functions per key, shared by every class.
263
+ const forwardingFunctions = new Map();
264
+
265
+ function forwarding(key, enumerable) {
266
+ let pair = forwardingFunctions.get(key);
267
+ if (!pair) {
268
+ pair = {
269
+ get() {
270
+ const original = this[ORIGINAL];
271
+ return original ? original[key] : Reflect.get(classProtoBelow(this), key, this);
272
+ },
273
+ set(value) {
274
+ const original = this[ORIGINAL];
275
+ if (original) original[key] = value;
276
+ else Reflect.set(classProtoBelow(this), key, value, this);
277
+ },
278
+ };
279
+ forwardingFunctions.set(key, pair);
280
+ }
281
+ return { get: pair.get, set: pair.set, enumerable, configurable: true };
282
+ }
283
+
284
+ // The class's own prototype under a shared prototype (`this` not a variant:
285
+ // the shared prototype read or written directly), for the forwarders and
286
+ // the proxy to fall through to.
287
+ const classProtos = new WeakMap();
288
+
289
+ function classProtoBelow(object) {
290
+ for (let level = object; level; level = Object.getPrototypeOf(level)) {
291
+ const proto = classProtos.get(level);
292
+ if (proto) return proto;
293
+ }
294
+ return Object.prototype;
295
+ }
296
+
297
+ // The renderer's own dispose listener (three's `onMaterialDispose`, one
298
+ // function per renderer), learned from the renderer itself: it is compiled
299
+ // a private material no one else can reach (`renderer.compile`, outside any
300
+ // render) and the one listener it adds to it is the renderer's; the private
301
+ // material's program is set up as for a first draw and the material then
302
+ // disposed, which lets its program go. Null when the renderer cannot do
303
+ // that: no frame of it is faded.
304
+ const rendererListeners = new WeakMap();
305
+
306
+ function disposeListenerOf(renderer) {
307
+ if (rendererListeners.has(renderer)) return rendererListeners.get(renderer);
308
+ let listener = null;
309
+ if (typeof renderer?.compile === 'function') {
310
+ const probe = new THREE.MeshBasicMaterial();
311
+ const scene = new THREE.Scene();
312
+ scene.add(new THREE.Mesh(new THREE.BufferGeometry(), probe));
313
+ try {
314
+ renderer.compile(scene, new THREE.PerspectiveCamera());
315
+ const added = probe._listeners?.dispose;
316
+ if (added?.length === 1 && typeof added[0] === 'function') listener = added[0];
317
+ // compile only links: three delays diagnostics, shader cleanup and
318
+ // uniform/attribute discovery until getUniforms. Finish that while
319
+ // the probe is alive. Otherwise Chromium's queued native completion
320
+ // readback can query its deleted program (and program 0 for metadata),
321
+ // producing twelve glGetProgramiv warnings on the first faded frame.
322
+ // This also blocks on metadata with checkShaderErrors disabled, and
323
+ // does not draw, bind a program or change any render state.
324
+ renderer.properties?.get(probe).currentProgram?.getUniforms();
325
+ } finally {
326
+ probe.dispose();
327
+ }
328
+ }
329
+ rendererListeners.set(renderer, listener);
330
+ return listener;
331
+ }
332
+
333
+ // A variant's events are its original's, as without the key: what deck code
334
+ // adds, removes, asks about or dispatches through a variant, and its
335
+ // `dispose`, act on the original. The one thing kept apart is the
336
+ // renderer's own dispose listener for the variant's program, known by its
337
+ // function (`disposeListenerOf`, never by when it is added): it is held by
338
+ // the variant (`state.renderer`) and also added to the original, where the
339
+ // renderer drawing the original without the key would have it, and the
340
+ // variant's program is released through it (`releaseVariant`).
341
+ const EVENT_METHODS = {
342
+ addEventListener(type, listener) {
343
+ const state = this[STATE];
344
+ if (type === 'dispose' && listener === state.known) state.renderer = listener;
345
+ this[ORIGINAL].addEventListener(type, listener);
346
+ },
347
+ removeEventListener(type, listener) {
348
+ const state = this[STATE];
349
+ // The renderer letting go of the variant's program: the variant's alone.
350
+ if (state.releasing && type === 'dispose' && listener === state.renderer) {
351
+ state.renderer = null;
352
+ return;
353
+ }
354
+ this[ORIGINAL].removeEventListener(type, listener);
355
+ },
356
+ hasEventListener(type, listener) {
357
+ return this[ORIGINAL].hasEventListener(type, listener);
358
+ },
359
+ dispatchEvent(event) {
360
+ this[ORIGINAL].dispatchEvent(event);
361
+ if (event.type === 'dispose') releaseVariant(this);
362
+ },
363
+ dispose() {
364
+ this[ORIGINAL].dispose();
365
+ releaseVariant(this);
366
+ },
367
+ };
368
+
369
+ // The variant's program let go of in the renderer that drew it, through the
370
+ // renderer's own listener, with nothing of the original's touched.
371
+ function releaseVariant(variant) {
372
+ const state = variant[STATE];
373
+ const listener = state.renderer;
374
+ if (!listener) return;
375
+ state.releasing = true;
376
+ try {
377
+ listener.call(variant, { type: 'dispose', target: variant });
378
+ } finally {
379
+ state.releasing = false;
380
+ state.renderer = null;
381
+ }
382
+ }
383
+
384
+ // Per material class, shared by its variants: a prototype with a
385
+ // forwarding accessor for every own property a material of the class has
386
+ // (what its constructor makes and what the first original of the class
387
+ // had, enumerable or not, symbols included), and the event methods above;
388
+ // so the properties three reads every draw go through one shared function
389
+ // each. Below it, in place of the class's own prototype, a proxy of that
390
+ // prototype that answers for the original: anything else read through a
391
+ // variant (a property only this original has, hidden or a symbol; the
392
+ // class's methods and accessors) is read from the original, and anything
393
+ // else written through it (a property the original never had; the class's
394
+ // setters, `needsUpdate`, `clearcoat`, ...) is written on the original, as
395
+ // without the key.
396
+ const forwarders = new Map();
397
+
398
+ // Read once through it, a key gets a forwarder of its own on the shared
399
+ // prototype (`shared`), so three's every-draw reads of what a class does
400
+ // not have (`isShaderMaterial`, ...) and of its methods do not come back.
401
+ function originalBelow(proto, shared) {
402
+ return new Proxy(proto, {
403
+ // `receiver` is a variant only when ORIGINAL is its own: read that way,
404
+ // the check never comes back through this proxy (the shared prototype
405
+ // itself, or anything else under it, falls through to the class).
406
+ get(target, key, receiver) {
407
+ const original = ownOriginal(receiver);
408
+ if (!original) return Reflect.get(target, key, receiver);
409
+ if (!Object.prototype.hasOwnProperty.call(shared, key)) Object.defineProperty(shared, key, forwarding(key, false));
410
+ return Reflect.get(original, key, original);
411
+ },
412
+ set(target, key, value, receiver) {
413
+ const original = ownOriginal(receiver);
414
+ return original ? Reflect.set(original, key, value, original) : Reflect.set(target, key, value, receiver);
415
+ },
416
+ });
417
+ }
418
+
419
+ function ownOriginal(object) {
420
+ return object !== null && typeof object === 'object' && Object.prototype.hasOwnProperty.call(object, ORIGINAL) ? object[ORIGINAL] : undefined;
421
+ }
422
+
423
+ function ownKeysWith(object, into) {
424
+ for (const key of Reflect.ownKeys(object)) {
425
+ if (!into.has(key)) into.set(key, Reflect.getOwnPropertyDescriptor(object, key).enumerable === true);
426
+ }
427
+ }
428
+
429
+ function forwarderFor(original) {
430
+ const proto = Object.getPrototypeOf(original);
431
+ let entry = forwarders.get(proto);
432
+ if (!entry) {
433
+ // The class is one of three's own (`lit`): a bare one of it, made with
434
+ // its own constructor (which touches nothing else), and the original's
435
+ // own keys.
436
+ const keys = new Map();
437
+ ownKeysWith(original, keys);
438
+ ownKeysWith(new (LIT_CLASSES.get(proto))(), keys);
439
+ // The class's own accessors too (`alphaTest`, `transmission`, ...),
440
+ // read every draw: forwarded, they run on the original.
441
+ for (let level = proto; level && level !== Object.prototype; level = Object.getPrototypeOf(level)) {
442
+ for (const key of Reflect.ownKeys(level)) {
443
+ const descriptor = Reflect.getOwnPropertyDescriptor(level, key);
444
+ if (!keys.has(key) && (descriptor.get || descriptor.set)) keys.set(key, false);
445
+ }
446
+ }
447
+ const shared = {};
448
+ Object.setPrototypeOf(shared, originalBelow(proto, shared));
449
+ classProtos.set(shared, proto);
450
+ for (const [key, enumerable] of keys) if (!OWN.has(key)) Object.defineProperty(shared, key, forwarding(key, enumerable));
451
+ for (const [name, method] of Object.entries(EVENT_METHODS)) Object.defineProperty(shared, name, { value: method, configurable: true, writable: true });
452
+ entry = { shared, keys };
453
+ forwarders.set(proto, entry);
454
+ }
455
+ return entry;
456
+ }
457
+
458
+ // A world-local view of `original`. The hooks checked when it is made
459
+ // (`fadeable`) are held while active: `current` says whether the original
460
+ // still has exactly them. Once inert, it follows the original's live hooks.
461
+ function makeVariant(original, uniform, rendererListener, properties) {
462
+ const { shared } = forwarderFor(original);
463
+ const variant = Object.create(shared);
464
+ const hook = dataValue(original, 'onBeforeCompile');
465
+ const key = dataValue(original, 'customProgramCacheKey');
466
+ const callback = dataValue(original, 'onBeforeRender');
467
+ const current = () =>
468
+ dataValue(original, 'onBeforeCompile') === hook &&
469
+ dataValue(original, 'customProgramCacheKey') === key &&
470
+ dataValue(original, 'onBeforeRender') === callback;
471
+ // `inert`: the variant is no longer the fade's (dropped, or the fade
472
+ // gone) but may be left on a mesh a deck would not give back: it then
473
+ // compiles and keys exactly as its original, with no fade.
474
+ const state = { known: rendererListener, renderer: null, releasing: false, inert: false };
475
+ // The held hook on the variant's shader, then the fade. A hook the
476
+ // original has been given since (inside the frame, before this compile)
477
+ // is never run.
478
+ function onBeforeCompile(shader, renderer) {
479
+ // Inert: exactly what three does when it compiles the original itself.
480
+ if (state.inert) return original.onBeforeCompile(shader, renderer);
481
+ hook.call(this, shader, renderer);
482
+ patchShadowFade(shader, uniform);
483
+ }
484
+ // The original's key plus the fade's, while it has the hooks held. Once
485
+ // it has others, a key no other material has, so the program compiled
486
+ // with the held hook is this variant's alone (it is dropped next frame).
487
+ const stale = `${PROGRAM_KEY_SUFFIX}|stale|${THREE.MathUtils.generateUUID()}`;
488
+ const customProgramCacheKey = () => {
489
+ if (state.inert) return original.customProgramCacheKey();
490
+ if (!current()) return stale;
491
+ return key.call(original) + PROGRAM_KEY_SUFFIX;
492
+ };
493
+ Object.defineProperties(variant, {
494
+ [ORIGINAL]: { value: original },
495
+ [STATE]: { value: state },
496
+ // A fresh number from three's own counter (the renderer sorts and binds
497
+ // by it, and keeps the variant's program apart by it).
498
+ id: { value: new THREE.Material().id },
499
+ // Read: the held ones. Written: the original's, as without the key (and
500
+ // the variant is then not current).
501
+ onBeforeCompile: {
502
+ get: () => onBeforeCompile,
503
+ set: (value) => {
504
+ original.onBeforeCompile = value;
505
+ },
506
+ },
507
+ customProgramCacheKey: {
508
+ get: () => customProgramCacheKey,
509
+ set: (value) => {
510
+ original.customProgramCacheKey = value;
511
+ },
512
+ },
513
+ });
514
+ // The original's dispose listeners when the variant was last brought in
515
+ // step: one gone (the original disposed, by anyone, and a renderer let go
516
+ // of it) releases the variant's program too. And, for a renderer that
517
+ // also draws the original itself (a keyed deck drawing another scene with
518
+ // the world's renderer), the renderer's own entry for the original
519
+ // (`properties`, read only if it is there, never made): a dispose removes
520
+ // it, and a draw after makes a new one, so a dispose that a redraw has
521
+ // already answered (its listener back in the same array) is seen too.
522
+ let listeners = null;
523
+ let listenerCount = 0;
524
+ let entryOfOriginal = null;
525
+ // Before each faded frame it is drawn in, and after each: anything
526
+ // defined on the variant itself (a property defined, or a forwarder
527
+ // redefined) is put on the original as it was put on the variant and
528
+ // taken off the variant. False when one cannot be taken off: the variant
529
+ // is then dropped.
530
+ function sync() {
531
+ const held = dataValue(original, '_listeners');
532
+ const disposeListeners = held !== ACCESSOR && held !== null && typeof held === 'object' ? dataValue(held, 'dispose') : undefined;
533
+ if (disposeListeners === ACCESSOR) return false;
534
+ const entry = properties?.has(original) ? properties.get(original) : null;
535
+ if ((listeners && disposeListeners !== listeners) || (disposeListeners?.length ?? 0) < listenerCount || (entryOfOriginal && entry !== entryOfOriginal)) releaseVariant(variant);
536
+ listeners = disposeListeners;
537
+ listenerCount = disposeListeners?.length ?? 0;
538
+ entryOfOriginal = entry;
539
+ for (const name of Reflect.ownKeys(variant)) {
540
+ if (OWN.has(name)) continue;
541
+ Reflect.defineProperty(original, name, Reflect.getOwnPropertyDescriptor(variant, name));
542
+ if (!Reflect.deleteProperty(variant, name)) return false;
543
+ }
544
+ return true;
545
+ }
546
+ sync();
547
+ return { variant, sync, current, seen: 0 };
548
+ }
549
+
550
+ // The material a variant fades, or undefined (for the tests).
551
+ export function shadowFadeOriginal(material) {
552
+ return material?.[ORIGINAL];
553
+ }
554
+
555
+ // The shadow pass hook of one frame (`begin`): the originals back, then the
556
+ // pass it had. Once its frame is over (`fade` null) it is only the pass it
557
+ // had, so one a deck kept hold of (a replacement that calls it) holds
558
+ // nothing of the fade's and does nothing of it.
559
+ // A mesh's own material back in place of the variant it was given, if it
560
+ // still has the variant. A plain assignment, or, for a property a deck made
561
+ // read-only meanwhile but left configurable, the same property redefined
562
+ // with the deck's attributes and the original's value. False when it cannot
563
+ // be (read-only and not configurable): the mesh keeps the variant, which
564
+ // reads and writes its original, until it can.
565
+ function putBack(mesh, put, had) {
566
+ const descriptor = Reflect.getOwnPropertyDescriptor(mesh, 'material');
567
+ // Gone from the mesh (deleted, or inherited now): not ours to touch.
568
+ if (!descriptor) return true;
569
+ if ('value' in descriptor) {
570
+ if (descriptor.value !== put) return true;
571
+ if (Reflect.set(mesh, 'material', had) && Reflect.getOwnPropertyDescriptor(mesh, 'material')?.value === had) return true;
572
+ if (descriptor.configurable !== true) return false;
573
+ return Reflect.defineProperty(mesh, 'material', { ...descriptor, value: had }) && Reflect.getOwnPropertyDescriptor(mesh, 'material')?.value === had;
574
+ }
575
+ // An accessor a deck put there during the render: what it holds cannot be
576
+ // read without calling its getter, which the fade never does. With a
577
+ // setter, the original is set through it once, the value the deck's own
578
+ // code would have had there without the key; without one, the mesh is
579
+ // held.
580
+ if (typeof descriptor.set !== 'function') return false;
581
+ // A deck setter that throws is the deck's own: the mesh is held, and
582
+ // the frame and every other mesh go on.
583
+ try {
584
+ Reflect.set(mesh, 'material', had);
585
+ } catch {
586
+ return false;
587
+ }
588
+ return true;
589
+ }
590
+
591
+ function passHook(had) {
592
+ const hook = {
593
+ fade: null,
594
+ had,
595
+ render(lights, scene, camera) {
596
+ hook.fade?.restore();
597
+ return had.call(this, lights, scene, camera);
598
+ },
599
+ };
600
+ return hook;
601
+ }
602
+
603
+ // Lets go of each variant's program, every one even if a listener throws;
604
+ // the first throw goes on after. The originals are not disposed.
605
+ function disposeAll(variants) {
606
+ let failure = null;
607
+ for (const variant of variants) {
608
+ try {
609
+ releaseVariant(variant);
610
+ } catch (err) {
611
+ failure ??= err;
612
+ }
613
+ }
614
+ if (failure) throw failure;
615
+ }
616
+
617
+ class ShadowFade {
618
+ constructor(world) {
619
+ this.world = world;
620
+ this.uniform = { value: new THREE.Vector2() };
621
+ // Original material -> its variant and when it was last drawn.
622
+ this.variants = new Map();
623
+ // The meshes given a variant this frame, what they had and what they were
624
+ // given, the first `count` of each.
625
+ this.meshes = [];
626
+ this.had = [];
627
+ this.put = [];
628
+ this.count = 0;
629
+ this.frame = 0;
630
+ this.swept = 0;
631
+ this.depth = 0;
632
+ this.dropped = false;
633
+ // Variants dropped since the last settle (`variantFor`), to dispose.
634
+ this.dead = [];
635
+ this.synced = 0;
636
+ this.listener = null;
637
+ // Per frame: whether each prototype met has three's own object
638
+ // callbacks (`plainObject`).
639
+ this.protos = new Map();
640
+ // Per frame: the materials met that are not to be faded.
641
+ this.refused = new Set();
642
+ this.firstSun = null;
643
+ this.visit = (object) => this.swapIn(object);
644
+ // The shadow pass hook of the frame now drawn (`begin`, `end`), on the
645
+ // renderer's shadow map, and whether that map had the function as its
646
+ // own.
647
+ this.pass = null;
648
+ this.passOwn = false;
649
+ this.hook = null;
650
+ // Meshes that could not be given their material back (`restore`).
651
+ this.stuck = [];
652
+ }
653
+
654
+ // Right before the render call of a frame the mode draws: the frame fades
655
+ // over the last quarter of `reach`. The variants go on the meshes now, and
656
+ // come off when three starts the shadow pass of a render on this world's
657
+ // renderer (`shadowMap.render`, which three calls on every render after it
658
+ // has listed the draws and before it draws any, whatever the shadow
659
+ // settings); `end` puts the originals back and takes the hook off.
660
+ begin(reach, sun) {
661
+ this.end();
662
+ const pass = this.world.renderer?.shadowMap;
663
+ // The pass must take the hook (a plain, writable function, read from
664
+ // its descriptor): a pass a deck has made read-only draws the frame
665
+ // unfaded.
666
+ const had = pass ? writableValue(pass, 'render') : ACCESSOR;
667
+ if (this.dropped || typeof had !== 'function') return;
668
+ // The scene's own `onBeforeRender` runs inside the render before the
669
+ // draws are listed, while the variants are on the meshes: a scene with
670
+ // one of its own is not faded.
671
+ if (!plainObject(this.world.scene)) return;
672
+ // The renderer's own dispose listener, known before any variant meets
673
+ // the renderer (learned once per renderer, outside any render).
674
+ this.listener ??= disposeListenerOf(this.world.renderer);
675
+ if (!this.listener) return;
676
+ this.frame += 1;
677
+ this.uniform.value.set(FADE_FROM * reach, reach);
678
+ // The hook goes on first, and the variants only once it is on, so no
679
+ // variant is ever on a mesh without the hook that takes it off.
680
+ if (!this.hookPass(pass, had)) return;
681
+ this.firstSun = null;
682
+ this.protos.clear();
683
+ this.refused.clear();
684
+ this.world.scene.traverseVisible(this.visit);
685
+ if (this.firstSun !== sun) this.end();
686
+ }
687
+
688
+ // Our hook on the pass (`had` its own function); false when it did not
689
+ // take.
690
+ hookPass(pass, had) {
691
+ if (this.hook?.had !== had) this.hook = passHook(had);
692
+ const passOwn = own(pass, 'render');
693
+ if (!Reflect.set(pass, 'render', this.hook.render) || dataValue(pass, 'render') !== this.hook.render) return false;
694
+ this.hook.fade = this;
695
+ this.pass = pass;
696
+ this.passOwn = passOwn;
697
+ return true;
698
+ }
699
+
700
+ // The frame is over (nearshadows.js `giveBack`, whatever the render did):
701
+ // the originals back and the hook off. A hook a deck has replaced meanwhile
702
+ // stays replaced, and the one it may still call is inert. Assignments only.
703
+ end() {
704
+ this.restore();
705
+ const { pass, hook } = this;
706
+ if (hook) hook.fade = null;
707
+ // Our hook back off the pass, if it is still there and can be taken off
708
+ // (one that cannot stays, inert: it only calls the pass it had). A hook
709
+ // a deck put over it is left.
710
+ if (pass && hook && dataValue(pass, 'render') === hook.render) {
711
+ if (this.passOwn) {
712
+ if (!Reflect.set(pass, 'render', hook.had)) {
713
+ // Made read-only during the frame but still configurable: put the
714
+ // pass back with the deck's attributes and the function it had.
715
+ const descriptor = Reflect.getOwnPropertyDescriptor(pass, 'render');
716
+ if (descriptor?.configurable && 'value' in descriptor) {
717
+ Reflect.defineProperty(pass, 'render', { ...descriptor, value: hook.had });
718
+ }
719
+ }
720
+ } else {
721
+ Reflect.deleteProperty(pass, 'render');
722
+ }
723
+ } else if (pass) {
724
+ this.hook = null;
725
+ }
726
+ this.pass = null;
727
+ }
728
+
729
+ swapIn(object) {
730
+ if (object.isMesh === true) {
731
+ if (dataValue(object, 'receiveShadow') !== true || !plainObject(object, this.protos)) return;
732
+ // The one property the fade writes on a mesh: only the mesh's own,
733
+ // writable and configurable, else the mesh is drawn as it is.
734
+ const material = ownMaterial(object);
735
+ if (material === ACCESSOR) return;
736
+ // A mesh with an array of materials is drawn as it is: a variant array
737
+ // would be another array, and a write into it would not reach the
738
+ // mesh's own.
739
+ if (Array.isArray(material)) return;
740
+ const put = this.variantFor(material);
741
+ if (!put) return;
742
+ if (!Reflect.set(object, 'material', put)) return;
743
+ const i = this.count++;
744
+ this.meshes[i] = object;
745
+ this.had[i] = material;
746
+ this.put[i] = put;
747
+ } else if (this.firstSun === null && object.isDirectionalLight === true && object.castShadow === true && object.layers.test(this.world.camera.layers)) {
748
+ this.firstSun = object;
749
+ }
750
+ }
751
+
752
+ variantFor(material) {
753
+ let entry = this.variants.get(material);
754
+ // Looked at once a frame for each material.
755
+ if (entry?.seen === this.frame) return entry.variant;
756
+ if (this.refused.has(material)) return null;
757
+ const eligible = fadeable(material);
758
+ // One whose original is no longer one to fade, has other hooks than it
759
+ // holds, or cannot be brought back in step, is dropped (and a new one
760
+ // made if the original is still one to fade).
761
+ if (entry && entry.seen !== this.frame && (!eligible || !entry.current() || !entry.sync())) {
762
+ this.variants.delete(material);
763
+ this.dead.push(entry.variant);
764
+ entry.variant[STATE].inert = true;
765
+ entry = null;
766
+ }
767
+ if (!eligible) {
768
+ this.refused.add(material);
769
+ return null;
770
+ }
771
+ if (!entry) {
772
+ entry = makeVariant(material, this.uniform, this.listener, this.world.renderer?.properties);
773
+ this.variants.set(material, entry);
774
+ }
775
+ entry.seen = this.frame;
776
+ return entry.variant;
777
+ }
778
+
779
+ // The originals back on every mesh that still has what it was given.
780
+ // Assignments only.
781
+ restore() {
782
+ // Each mesh on its own: the bookkeeping is right after every one, and
783
+ // one that cannot be given its material back does not stop the others.
784
+ for (let i = this.count - 1; i >= 0; i--) {
785
+ const mesh = this.meshes[i];
786
+ const put = this.put[i];
787
+ const had = this.had[i];
788
+ this.meshes[i] = null;
789
+ this.had[i] = null;
790
+ this.put[i] = null;
791
+ this.count = i;
792
+ if (!putBack(mesh, put, had)) this.stuck.push({ mesh, put, had });
793
+ }
794
+ // Those that could not be before: tried again at every frame boundary.
795
+ if (this.stuck.length > 0) {
796
+ const stuck = this.stuck;
797
+ this.stuck = [];
798
+ for (const left of stuck) if (!putBack(left.mesh, left.put, left.had)) this.stuck.push(left);
799
+ }
800
+ }
801
+
802
+ // After the outermost `renderWorld`: what landed on the variants drawn
803
+ // this frame goes to their originals (`sync`), what was dropped is
804
+ // disposed, and now and then the variants not drawn for a while.
805
+ settle() {
806
+ if (!this.dropped && this.synced !== this.frame) {
807
+ this.synced = this.frame;
808
+ for (const [material, entry] of this.variants) {
809
+ if (entry.seen !== this.frame || entry.sync()) continue;
810
+ this.variants.delete(material);
811
+ this.dead.push(entry.variant);
812
+ entry.variant[STATE].inert = true;
813
+ }
814
+ }
815
+ const dead = this.dead;
816
+ if (dead.length > 0) {
817
+ this.dead = [];
818
+ disposeAll(dead);
819
+ }
820
+ if (this.dropped) {
821
+ this.disposeVariants(() => true, this.stuck.map((left) => left.put));
822
+ return;
823
+ }
824
+ if (this.frame - this.swept < SWEEP_EVERY) return;
825
+ this.swept = this.frame;
826
+ const stale = this.frame - SWEEP_EVERY;
827
+ this.disposeVariants((entry) => entry.seen <= stale);
828
+ }
829
+
830
+ disposeVariants(gone, held = []) {
831
+ const variants = new Set(held);
832
+ for (const [material, entry] of this.variants) {
833
+ if (!gone(entry)) continue;
834
+ this.variants.delete(material);
835
+ variants.add(entry.variant);
836
+ }
837
+ for (const variant of variants) variant[STATE].inert = true;
838
+ disposeAll(variants);
839
+ }
840
+ }
841
+
842
+ // A frame of a scene with the orbit key: the world's fade, if the scene also
843
+ // asked for near shadows, else null.
844
+ export function holdShadowFade(world, runtime) {
845
+ if (!(runtime?.nearShadowDistance > 0) || !patchable()) return null;
846
+ world.shadowFade ??= new ShadowFade(world);
847
+ return world.shadowFade;
848
+ }
849
+
850
+ // The outermost `renderWorld` of a faded frame, round the near-shadow mode's
851
+ // call (`draw(fade)`).
852
+ export function renderShadowFaded(fade, draw) {
853
+ fade.depth += 1;
854
+ try {
855
+ draw(fade);
856
+ } finally {
857
+ fade.depth -= 1;
858
+ if (fade.depth === 0) fade.settle();
859
+ }
860
+ }
861
+
862
+ // A scene load, an editor's frame, a frame of a scene without both keys,
863
+ // `disposeWorld`: the originals back and every variant disposed (after the
864
+ // render, if inside one).
865
+ export function dropShadowFade(world) {
866
+ const fade = world?.shadowFade;
867
+ if (!fade) return;
868
+ world.shadowFade = null;
869
+ fade.end();
870
+ fade.dropped = true;
871
+ // A variant a mesh still holds (`stuck`) draws as its original from now
872
+ // on; its program is let go of below and made again from the original's
873
+ // key.
874
+ for (const entry of fade.variants.values()) entry.variant[STATE].inert = true;
875
+ for (const left of fade.stuck) left.put[STATE].inert = true;
876
+ if (fade.depth === 0) fade.settle();
877
+ }
878
+
879
+ // For the tests: a world's variant of a material, if it has one.
880
+ export function shadowFadeVariant(world, material) {
881
+ return world.shadowFade?.variants.get(material)?.variant;
882
+ }