threedog 0.2.1 → 0.5.0

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 (593) hide show
  1. package/CHANGELOG.md +290 -0
  2. package/README.md +9 -8
  3. package/core/Animator.js +15 -12
  4. package/core/Assets.js +71 -16
  5. package/core/Audio.js +45 -38
  6. package/core/Baked.js +32 -8
  7. package/core/Batching.js +102 -15
  8. package/core/Budget.js +39 -18
  9. package/core/Clock.js +40 -80
  10. package/core/DebugOverrides.js +45 -10
  11. package/core/Describe.js +104 -43
  12. package/core/Engine.js +183 -59
  13. package/core/Events.js +4 -1
  14. package/core/I18n.js +25 -13
  15. package/core/Input.js +168 -79
  16. package/core/Inspect.js +2 -2
  17. package/core/InspectMaterials.js +85 -0
  18. package/core/InspectPerf.js +61 -0
  19. package/core/InspectScene.js +13 -2
  20. package/core/Loading.js +2 -1
  21. package/core/Ops.js +50 -9
  22. package/core/Random.js +9 -2
  23. package/core/Rays.js +5 -5
  24. package/core/SaveStore.js +41 -9
  25. package/core/Saves.js +71 -15
  26. package/core/Stream.js +14 -10
  27. package/core/Threads.js +26 -7
  28. package/core/UrlState.js +25 -9
  29. package/core/Views.js +9 -6
  30. package/core/actor.js +1 -1
  31. package/core/adaptive.js +93 -0
  32. package/core/assetEntry.js +22 -5
  33. package/core/assetHot.js +176 -0
  34. package/core/assetLoad.js +38 -11
  35. package/core/assetManifest.js +48 -9
  36. package/core/assetReport.js +1 -1
  37. package/core/audioRules.js +13 -11
  38. package/core/audioVoice.js +69 -42
  39. package/core/canvasBox.js +1 -2
  40. package/core/catalog.js +5 -2
  41. package/core/cells.js +62 -0
  42. package/core/checks.js +10 -8
  43. package/core/chunkStream.js +87 -0
  44. package/core/control.js +149 -0
  45. package/core/copies.js +32 -0
  46. package/core/dayTime.js +12 -6
  47. package/core/debugView.js +82 -15
  48. package/core/describeRender.js +37 -17
  49. package/core/describeText.js +41 -21
  50. package/core/engineCamera.js +32 -0
  51. package/core/errorCodes.js +438 -19
  52. package/core/errorHints.js +37 -141
  53. package/core/errors.js +31 -8
  54. package/core/events.generated.js +12 -7
  55. package/core/gameLights.js +62 -0
  56. package/core/grayMap.js +20 -0
  57. package/core/grid.js +14 -13
  58. package/core/gridLoad.js +2 -1
  59. package/core/ground.js +6 -4
  60. package/core/headlessLoad.js +55 -0
  61. package/core/holds.js +78 -50
  62. package/core/inputContext.js +11 -7
  63. package/core/inputDefaults.js +24 -21
  64. package/core/inputPad.js +1 -1
  65. package/core/inputPointer.js +1 -1
  66. package/core/inputTouch.js +2 -2
  67. package/core/lifecycle.js +48 -12
  68. package/core/lightWatch.js +6 -4
  69. package/core/look/coreParts.js +137 -30
  70. package/core/look/part.js +175 -95
  71. package/core/look/points.js +255 -0
  72. package/core/look/report.js +34 -17
  73. package/core/look/shadowPart.js +63 -0
  74. package/core/look/state.js +45 -0
  75. package/core/look/write.js +12 -28
  76. package/core/loop.js +100 -41
  77. package/core/math.js +11 -3
  78. package/core/moduleSlots.js +75 -0
  79. package/core/moduleTypes.js +73 -0
  80. package/core/modules.js +145 -27
  81. package/core/movement.js +2 -2
  82. package/core/namedModel.js +70 -0
  83. package/core/opsCheck.js +3 -3
  84. package/core/opsFail.js +15 -6
  85. package/core/opsGuard.js +4 -4
  86. package/core/opsRun.js +4 -3
  87. package/core/owned.js +168 -0
  88. package/core/page.js +2 -1
  89. package/core/params.js +30 -8
  90. package/core/plugins.js +277 -0
  91. package/core/saveApply.js +99 -17
  92. package/core/saveFiles.js +8 -6
  93. package/core/saveValues.js +46 -0
  94. package/core/saveVersions.js +20 -3
  95. package/core/scatter.js +48 -19
  96. package/core/sceneDelta.js +203 -0
  97. package/core/sceneMethods.js +35 -30
  98. package/core/screenToGround.js +1 -1
  99. package/core/services.js +27 -11
  100. package/core/session.js +28 -25
  101. package/core/shaderCost.js +78 -0
  102. package/core/shadowTypes.js +2 -1
  103. package/core/sharedPoint.js +317 -0
  104. package/core/skip.js +36 -28
  105. package/core/stackFrames.js +34 -0
  106. package/core/stacks.js +166 -0
  107. package/core/startup.js +153 -11
  108. package/core/streamSources.js +0 -11
  109. package/core/styleParts.js +63 -47
  110. package/core/targeting.js +1 -1
  111. package/core/textureBytes.js +20 -0
  112. package/core/thingExplain.js +2 -2
  113. package/core/thingOps.js +29 -6
  114. package/core/things.js +21 -11
  115. package/core/threadPure.js +7 -3
  116. package/core/threePatches.js +47 -0
  117. package/core/tier.js +21 -10
  118. package/core/time.js +236 -0
  119. package/core/uiClock.js +25 -0
  120. package/core/util.js +7 -1
  121. package/core/validate.js +65 -86
  122. package/core/wrap.js +106 -0
  123. package/docs/AI.md +87 -100
  124. package/docs/API.md +32 -28
  125. package/docs/CODEMAP.md +220 -147
  126. package/docs/COMMANDS.md +56 -35
  127. package/docs/CONVENTIONS.md +90 -28
  128. package/docs/MIGRATING-0.3.md +248 -0
  129. package/docs/MIGRATING-0.4.md +228 -0
  130. package/docs/MIGRATING-0.5.md +68 -0
  131. package/docs/PERF.md +35 -7
  132. package/docs/THREE.md +79 -27
  133. package/docs/api/ambience.md +0 -1
  134. package/docs/api/assets.md +11 -9
  135. package/docs/api/audio.md +10 -14
  136. package/docs/api/calendar.md +41 -0
  137. package/docs/api/cameraRig.md +16 -5
  138. package/docs/api/characters.md +38 -23
  139. package/docs/api/clock.md +6 -5
  140. package/docs/api/collision.md +9 -6
  141. package/docs/api/content.md +6 -4
  142. package/docs/api/dialogue.md +9 -9
  143. package/docs/api/engine.md +63 -60
  144. package/docs/api/events.md +19 -14
  145. package/docs/api/grids.md +3 -3
  146. package/docs/api/ground.md +1 -1
  147. package/docs/api/hud.md +2 -2
  148. package/docs/api/i18n.md +1 -1
  149. package/docs/api/input.md +11 -6
  150. package/docs/api/interactions.md +7 -5
  151. package/docs/api/inventory.md +17 -16
  152. package/docs/api/level.md +0 -1
  153. package/docs/api/lightPool.md +3 -1
  154. package/docs/api/music.md +1 -1
  155. package/docs/api/nav.md +3 -0
  156. package/docs/api/openWorld.md +2 -1
  157. package/docs/api/particles.md +2 -2
  158. package/docs/api/photo.md +0 -2
  159. package/docs/api/pickups.md +15 -0
  160. package/docs/api/post.md +5 -4
  161. package/docs/api/quests.md +2 -3
  162. package/docs/api/rigid.md +16 -14
  163. package/docs/api/saves.md +6 -4
  164. package/docs/api/settings.md +2 -3
  165. package/docs/api/sky.md +11 -44
  166. package/docs/api/stream.md +1 -1
  167. package/docs/api/style.md +28 -26
  168. package/docs/api/terrain.md +9 -6
  169. package/docs/api/terrainLook.md +10 -0
  170. package/docs/api/timeline.md +3 -4
  171. package/docs/api/tools.md +29 -13
  172. package/docs/api/triggers.md +5 -10
  173. package/docs/api/vegetation.md +11 -8
  174. package/docs/api/vegetationLook.md +10 -0
  175. package/docs/api/water.md +4 -20
  176. package/docs/api/weather.md +5 -15
  177. package/docs/api/weatherLook.md +10 -0
  178. package/docs/api/world.md +2 -1
  179. package/docs/recipes/01-new-game.md +48 -34
  180. package/docs/recipes/02-npc.md +24 -7
  181. package/docs/recipes/03-items-pickups.md +35 -21
  182. package/docs/recipes/04-quests.md +12 -5
  183. package/docs/recipes/05-props-bake.md +151 -15
  184. package/docs/recipes/06-clock-events.md +100 -52
  185. package/docs/recipes/07-flow-scenes-pause.md +40 -9
  186. package/docs/recipes/08-weather-sky.md +145 -36
  187. package/docs/recipes/{09-style-pack.md → 09-style.md} +101 -52
  188. package/docs/recipes/10-blender-level.md +14 -2
  189. package/docs/recipes/11-vehicles.md +64 -7
  190. package/docs/recipes/13-animation.md +14 -6
  191. package/docs/recipes/14-custom-module.md +61 -29
  192. package/docs/recipes/15-perf.md +30 -9
  193. package/docs/recipes/16-dev-bridge.md +61 -37
  194. package/docs/recipes/18-world.md +16 -6
  195. package/docs/recipes/19-shooting-picking.md +6 -6
  196. package/docs/recipes/20-content-actions-save.md +55 -17
  197. package/docs/recipes/21-camera.md +49 -16
  198. package/docs/recipes/22-shop-gold.md +3 -3
  199. package/docs/recipes/23-ops.md +16 -10
  200. package/docs/recipes/26-ui-parts.md +61 -12
  201. package/docs/recipes/27-net.md +11 -5
  202. package/docs/recipes/28-terrain.md +34 -1
  203. package/docs/recipes/29-grid.md +34 -16
  204. package/docs/recipes/30-templates.md +64 -12
  205. package/docs/recipes/31-audio.md +20 -12
  206. package/docs/recipes/32-action-game.md +35 -2
  207. package/docs/recipes/34-environment-lighting.md +26 -3
  208. package/docs/recipes/35-style-from-reference.md +88 -19
  209. package/docs/recipes/36-sky-ground-plant-looks.md +18 -18
  210. package/docs/recipes/37-terrain-look-hooks.md +3 -2
  211. package/docs/recipes/38-environment-look-hooks.md +37 -26
  212. package/docs/recipes/40-scene.md +16 -13
  213. package/docs/recipes/41-thing-registry.md +11 -7
  214. package/docs/recipes/42-terrain-shape.md +2 -2
  215. package/docs/recipes/43-shader-patches-surface-looks.md +158 -76
  216. package/docs/recipes/44-water-weather-look-hooks.md +26 -17
  217. package/docs/recipes/45-cascaded-shadows.md +16 -7
  218. package/docs/recipes/47-island-snow-backdrop.md +7 -7
  219. package/docs/recipes/48-make-material.md +11 -6
  220. package/docs/recipes/49-game-module-contract.md +30 -17
  221. package/docs/recipes/50-own-renderer.md +2 -2
  222. package/docs/recipes/51-capability-catalog.md +3 -3
  223. package/docs/recipes/52-look.md +197 -61
  224. package/docs/recipes/53-ground-up-close.md +1 -1
  225. package/docs/recipes/54-three-idioms.md +88 -0
  226. package/docs/recipes/55-mods-time-wrap.md +112 -0
  227. package/docs/recipes/56-webgpu-tsl.md +260 -0
  228. package/docs/recipes/57-control.md +65 -0
  229. package/docs/recipes/58-input.md +57 -0
  230. package/docs/recipes/59-depth-post-effects.md +83 -0
  231. package/docs/recipes/60-judge-the-picture.md +55 -0
  232. package/examples/action/.threedog/content-index.json +1 -0
  233. package/examples/action/GAMEMAP.md +17 -13
  234. package/examples/action/assets.js +8 -0
  235. package/examples/action/content/actions.js +1 -1
  236. package/examples/action/content/audio.js +5 -5
  237. package/examples/action/look.js +1 -1
  238. package/examples/action/main.js +10 -14
  239. package/examples/action/models.js +24 -23
  240. package/examples/pullable.json +111 -0
  241. package/examples/rc2d/.threedog/content-index.json +1 -0
  242. package/examples/rc2d/GAMEMAP.md +83 -0
  243. package/examples/rc2d/README.md +52 -0
  244. package/examples/rc2d/main.js +63 -0
  245. package/examples/rc2d/rc2d.js +197 -0
  246. package/examples/rc2d/shaders.js +144 -0
  247. package/examples/sheepisland/.threedog/content-index.json +1 -0
  248. package/examples/sheepisland/GAMEMAP.md +13 -11
  249. package/examples/sheepisland/assets.js +5 -0
  250. package/examples/sheepisland/main.js +17 -12
  251. package/examples/sheepisland/style/index.js +6 -6
  252. package/examples/styletest/.threedog/content-index.json +1 -0
  253. package/examples/styletest/GAMEMAP.md +15 -16
  254. package/examples/styletest/assets.js +6 -0
  255. package/examples/styletest/content/templates.js +1 -1
  256. package/examples/styletest/content/vegetation.js +1 -1
  257. package/examples/styletest/look.js +1 -1
  258. package/examples/styletest/main.js +17 -16
  259. package/examples/styletest/models.js +1 -1
  260. package/examples/styletest/params.js +3 -3
  261. package/examples/techniques/contact-shadows/README.md +92 -0
  262. package/examples/techniques/contact-shadows/contactShadows.js +51 -0
  263. package/examples/techniques/contact-shadows/main.js +53 -0
  264. package/examples/techniques/day-night-grade/README.md +86 -0
  265. package/examples/techniques/day-night-grade/dayNightGrade.js +76 -0
  266. package/examples/techniques/day-night-grade/main.js +51 -0
  267. package/examples/techniques/interior-light/README.md +103 -0
  268. package/examples/techniques/interior-light/interiorLight.js +45 -0
  269. package/examples/techniques/interior-light/main.js +124 -0
  270. package/examples/techniques/modular-building/README.md +125 -0
  271. package/examples/techniques/modular-building/kit.js +426 -0
  272. package/examples/techniques/modular-building/main.js +47 -0
  273. package/examples/techniques/night-lights/README.md +94 -0
  274. package/examples/techniques/night-lights/main.js +70 -0
  275. package/examples/techniques/night-lights/nightLights.js +91 -0
  276. package/examples/techniques/outlines-and-rim/README.md +59 -0
  277. package/examples/techniques/outlines-and-rim/main.js +84 -0
  278. package/examples/techniques/outlines-and-rim/outlines.js +64 -0
  279. package/examples/techniques/procedural-animation/README.md +77 -0
  280. package/examples/techniques/procedural-animation/creature.js +44 -0
  281. package/examples/techniques/procedural-animation/main.js +45 -0
  282. package/examples/techniques/procedural-animation/proceduralAnim.js +118 -0
  283. package/examples/techniques/procedural-surface/README.md +67 -0
  284. package/examples/techniques/procedural-surface/main.js +103 -0
  285. package/examples/techniques/procedural-surface/surface.js +120 -0
  286. package/examples/techniques/stylized-shading/README.md +75 -0
  287. package/examples/techniques/stylized-shading/main.js +93 -0
  288. package/examples/techniques/stylized-shading/shading.js +131 -0
  289. package/examples/techniques/sun-and-key-light/README.md +135 -0
  290. package/examples/techniques/sun-and-key-light/keyLight.js +53 -0
  291. package/examples/techniques/sun-and-key-light/main.js +95 -0
  292. package/examples/techniques/wet-road/README.md +99 -0
  293. package/examples/techniques/wet-road/main.js +68 -0
  294. package/examples/techniques/wet-road/wetRoad.js +209 -0
  295. package/examples/webgpu-tsl/.threedog/content-index.json +1 -0
  296. package/examples/webgpu-tsl/GAMEMAP.md +84 -0
  297. package/examples/webgpu-tsl/README.md +42 -0
  298. package/examples/webgpu-tsl/main.js +21 -0
  299. package/examples/webgpu-tsl/tslScene.js +75 -0
  300. package/examples/world/.threedog/content-index.json +1 -0
  301. package/examples/world/GAMEMAP.md +22 -15
  302. package/examples/world/art/humanoid.js +42 -0
  303. package/examples/world/art/index.js +11 -0
  304. package/examples/world/art/models.js +58 -0
  305. package/examples/world/art/shapes.js +9 -0
  306. package/examples/world/assets.js +6 -0
  307. package/examples/world/content/audio.js +5 -5
  308. package/examples/world/content/npcs.js +1 -1
  309. package/examples/world/content/props.js +1 -1
  310. package/examples/world/content/rules.js +1 -1
  311. package/examples/world/content/templates.js +1 -1
  312. package/examples/world/content/vegetation.js +1 -1
  313. package/examples/world/main.js +17 -13
  314. package/examples/world/style/index.js +61 -0
  315. package/examples/world/style/plugins/10-bloom.js +5 -0
  316. package/index.d.ts +44 -45
  317. package/index.js +26 -22
  318. package/llms.txt +11 -4
  319. package/modules/ambience/Ambience.js +34 -23
  320. package/modules/calendar/Calendar.js +163 -0
  321. package/modules/calendar/contract.js +21 -0
  322. package/modules/calendar/index.js +81 -0
  323. package/modules/{sky → calendar}/schedule.js +17 -16
  324. package/modules/calendar/seasons.js +55 -0
  325. package/modules/camera/FirstPersonCamera.js +6 -15
  326. package/modules/camera/FixedCamera.js +33 -15
  327. package/modules/camera/FollowCamera.js +90 -16
  328. package/modules/camera/RtsCamera.js +18 -17
  329. package/modules/camera/common.js +70 -19
  330. package/modules/camera/contract.js +17 -0
  331. package/modules/camera/projection.js +11 -24
  332. package/modules/characters/Character.js +177 -42
  333. package/modules/characters/NPC.js +87 -36
  334. package/modules/characters/assetModel.js +4 -13
  335. package/modules/characters/behaviors.js +2 -1
  336. package/modules/characters/contract.js +13 -0
  337. package/modules/characters/footsteps.js +1 -1
  338. package/modules/characters/index.js +87 -49
  339. package/modules/characters/moveHelpers.js +4 -5
  340. package/modules/characters/movers/common.js +97 -19
  341. package/modules/characters/movers/manual.js +20 -0
  342. package/modules/characters/movers/rapier.js +19 -16
  343. package/modules/characters/movers/simple.js +14 -11
  344. package/modules/characters/movers/support.js +172 -0
  345. package/modules/characters/npcAvoid.js +47 -42
  346. package/modules/characters/npcSim.js +12 -8
  347. package/modules/characters/npcView.js +44 -16
  348. package/modules/characters/pathAsync.js +7 -3
  349. package/modules/characters/playerStrings.js +4 -3
  350. package/modules/characters/proceduralRig.js +2 -1
  351. package/modules/characters/useModel.js +1 -1
  352. package/modules/characters/vehicle.contract.js +21 -0
  353. package/modules/characters/walkTo.js +21 -7
  354. package/modules/collision/Collision.js +65 -25
  355. package/modules/collision/grid.js +8 -6
  356. package/modules/content/explain.js +2 -2
  357. package/modules/content/fields.js +102 -0
  358. package/modules/content/ids.js +8 -6
  359. package/modules/content/index.js +96 -27
  360. package/modules/content/loader.js +45 -33
  361. package/modules/content/models.js +11 -40
  362. package/modules/content/packs.js +6 -5
  363. package/modules/content/parts.js +41 -0
  364. package/modules/content/props.js +16 -9
  365. package/modules/content/rules.js +58 -0
  366. package/modules/content/stages.js +80 -0
  367. package/modules/content/templates.js +121 -30
  368. package/modules/debug/DebugPanel.js +24 -17
  369. package/modules/flow/contract.js +16 -0
  370. package/modules/flow/index.js +21 -15
  371. package/modules/gameplay/Container.js +41 -32
  372. package/modules/gameplay/Containers.js +30 -15
  373. package/modules/gameplay/Dialogue.js +113 -55
  374. package/modules/gameplay/Interactions.js +193 -67
  375. package/modules/gameplay/Inventory.js +69 -73
  376. package/modules/gameplay/Quests.js +75 -39
  377. package/modules/gameplay/boxOps.js +3 -3
  378. package/modules/gameplay/dialogue.contract.js +21 -0
  379. package/modules/gameplay/dialogueBox.js +4 -2
  380. package/modules/gameplay/dialogueBubbles.js +116 -0
  381. package/modules/gameplay/interactionPrompt.js +4 -1
  382. package/modules/gameplay/interactions.contract.js +16 -0
  383. package/modules/gameplay/inventory.contract.js +23 -0
  384. package/modules/gameplay/inventoryOps.js +18 -16
  385. package/modules/gameplay/pickups.js +268 -85
  386. package/modules/gameplay/questLog.js +4 -4
  387. package/modules/gameplay/quests.contract.js +17 -0
  388. package/modules/grid/Grid.js +92 -50
  389. package/modules/grid/Layer.js +14 -13
  390. package/modules/grid/index.js +60 -36
  391. package/modules/grid/ops.js +20 -18
  392. package/modules/grid/preview.js +15 -16
  393. package/modules/grid/strings.js +12 -0
  394. package/modules/grid/tiles.js +6 -5
  395. package/modules/hlod/index.js +51 -19
  396. package/modules/hud/index.js +35 -20
  397. package/modules/hud/parts.js +20 -7
  398. package/modules/level/heightfield.js +3 -1
  399. package/modules/level/index.js +45 -20
  400. package/modules/level/nodes.js +4 -3
  401. package/modules/lights/index.js +42 -18
  402. package/modules/music/Music.js +38 -21
  403. package/modules/music/rules.js +1 -1
  404. package/modules/nav/index.js +48 -42
  405. package/modules/net/Client.js +13 -6
  406. package/modules/net/Host.js +19 -5
  407. package/modules/net/Net.js +2 -1
  408. package/modules/net/index.js +13 -8
  409. package/modules/net/sync.js +9 -8
  410. package/modules/net/transports.js +77 -27
  411. package/modules/particles/Particles.js +23 -14
  412. package/modules/particles/contract.js +17 -0
  413. package/modules/particles/effect.js +39 -11
  414. package/modules/photo/Photo.js +53 -36
  415. package/modules/rigid/RigidBodies.js +194 -32
  416. package/modules/rigid/Vehicles.js +18 -15
  417. package/modules/rigid/sync.js +62 -17
  418. package/modules/settings/Settings.js +57 -52
  419. package/modules/settings/apply.js +38 -3
  420. package/modules/settings/sections.js +5 -3
  421. package/modules/settings/strings.js +3 -2
  422. package/modules/settings/view.js +5 -3
  423. package/modules/sky/Sky.js +88 -176
  424. package/modules/sky/cascades.js +86 -25
  425. package/modules/sky/cascades.tsl.js +82 -0
  426. package/modules/sky/clouds.js +34 -19
  427. package/modules/sky/domeLayers.js +30 -15
  428. package/modules/sky/environment.js +83 -13
  429. package/modules/sky/fog.js +140 -95
  430. package/modules/sky/frame.js +112 -44
  431. package/modules/sky/look.js +22 -19
  432. package/modules/sky/lookFog.js +48 -52
  433. package/modules/sky/lookShadow.js +53 -41
  434. package/modules/sky/lookSky.js +82 -21
  435. package/modules/sky/season.js +21 -0
  436. package/modules/sky/shader.js +41 -19
  437. package/modules/sky/shadow.js +44 -20
  438. package/modules/sky/skyModule.js +45 -33
  439. package/modules/sky/tint.js +61 -0
  440. package/modules/terrain/ChunkManager.js +175 -108
  441. package/modules/terrain/chunkJob.js +1 -1
  442. package/modules/terrain/chunkQueries.js +6 -4
  443. package/modules/terrain/index.js +105 -50
  444. package/modules/terrain/look.js +83 -0
  445. package/modules/terrain/lookGround.js +21 -16
  446. package/modules/terrain/terrainDefaults.js +1 -1
  447. package/modules/terrain/terrainEdit.js +18 -20
  448. package/modules/terrain/terrainGen.js +5 -3
  449. package/modules/terrain/terrainIsland.js +5 -4
  450. package/modules/terrain/terrainShape.js +12 -12
  451. package/modules/terrain/terrainShapeMain.js +8 -7
  452. package/modules/terrain/terrainTexture.js +21 -19
  453. package/modules/timeline/Timeline.js +95 -43
  454. package/modules/timeline/Tween.js +18 -10
  455. package/modules/timeline/letterbox.js +2 -2
  456. package/modules/timeline/shots.js +5 -1
  457. package/modules/triggers/index.js +107 -76
  458. package/modules/vegetation/Vegetation.js +186 -110
  459. package/modules/vegetation/densityMap.js +62 -0
  460. package/modules/vegetation/groundChunks.js +52 -58
  461. package/modules/vegetation/index.js +32 -11
  462. package/modules/vegetation/look.js +119 -0
  463. package/modules/vegetation/lookWind.js +71 -10
  464. package/modules/vegetation/modelGeometry.js +3 -2
  465. package/modules/vegetation/nearFade.js +8 -2
  466. package/modules/water/Water.js +65 -27
  467. package/modules/water/look.js +29 -18
  468. package/modules/water/surface.js +64 -25
  469. package/modules/water/surfaceDefaults.js +3 -0
  470. package/modules/weather/Weather.js +62 -104
  471. package/modules/weather/look.js +1 -2
  472. package/modules/weather/particles.js +24 -9
  473. package/modules/weather/strings.js +2 -2
  474. package/modules/weather/weatherLook.js +135 -0
  475. package/modules/world/World.js +24 -7
  476. package/modules/world/explain.js +1 -1
  477. package/modules/world/index.js +18 -4
  478. package/modules/world/persist.js +62 -2
  479. package/modules/world/pos.js +3 -2
  480. package/modules/world/records.js +8 -6
  481. package/modules/world/sim.js +60 -20
  482. package/modules/world/simFar.js +4 -3
  483. package/modules/world/state.js +3 -2
  484. package/modules/world/thingSource.js +1 -1
  485. package/modules/world/views.js +7 -5
  486. package/package.json +7 -3
  487. package/presets.js +41 -27
  488. package/render/PostFX.js +145 -120
  489. package/render/backend.js +440 -0
  490. package/render/detail.js +120 -39
  491. package/render/effects.js +130 -38
  492. package/render/engineUniforms.js +10 -1
  493. package/render/fuse.js +5 -4
  494. package/render/fx/cloudShadow.tsl.js +16 -0
  495. package/render/fx/detail.tsl.js +66 -0
  496. package/render/fx/index.js +26 -0
  497. package/render/fx/lighting.tsl.js +28 -0
  498. package/render/fx/nodeErrors.tsl.js +68 -0
  499. package/render/fx/noise.tsl.js +12 -0
  500. package/render/fx/objectOrigin.tsl.js +42 -0
  501. package/render/fx/particles.tsl.js +139 -0
  502. package/render/fx/patchNodes.tsl.js +403 -0
  503. package/render/fx/postWebGPU.tsl.js +158 -0
  504. package/render/fx/season.tsl.js +13 -0
  505. package/render/fx/sky.tsl.js +260 -0
  506. package/render/fx/uniforms.tsl.js +67 -0
  507. package/render/fx/water.tsl.js +53 -0
  508. package/render/fx/wind.tsl.js +22 -0
  509. package/render/fxNumbers.js +36 -0
  510. package/render/hotGlsl.js +24 -0
  511. package/render/lighting.js +66 -14
  512. package/render/lights.js +223 -4
  513. package/render/lookGlow.js +139 -0
  514. package/render/lookSurface.js +99 -36
  515. package/render/materials.js +71 -52
  516. package/render/modeling.js +73 -0
  517. package/render/ownFrame.js +76 -0
  518. package/render/patches.js +311 -34
  519. package/render/postLook.js +124 -46
  520. package/render/postWebGL.js +197 -0
  521. package/render/probes.js +231 -0
  522. package/render/shaderErrors.js +154 -0
  523. package/render/slots.js +165 -0
  524. package/render/surface.js +53 -17
  525. package/render/terms.js +211 -0
  526. package/render/uniformDecl.js +3 -2
  527. package/types/common.d.ts +2 -2
  528. package/types/core.d.ts +614 -240
  529. package/types/gameplay.d.ts +482 -312
  530. package/types/generated/core/Audio.d.ts +26 -42
  531. package/types/generated/core/Batching.d.ts +6 -2
  532. package/types/generated/core/Budget.d.ts +2 -8
  533. package/types/generated/core/DebugOverrides.d.ts +44 -3
  534. package/types/generated/core/I18n.d.ts +2 -2
  535. package/types/generated/core/Input.d.ts +102 -31
  536. package/types/generated/core/Ops.d.ts +31 -0
  537. package/types/generated/core/Random.d.ts +2 -2
  538. package/types/generated/core/Saves.d.ts +10 -2
  539. package/types/generated/core/Stream.d.ts +1 -1
  540. package/types/generated/core/Threads.d.ts +4 -1
  541. package/types/generated/core/Views.d.ts +0 -1
  542. package/types/generated/core/audioVoice.d.ts +17 -9
  543. package/types/generated/core/control.d.ts +55 -0
  544. package/types/generated/core/session.d.ts +2 -1
  545. package/types/generated/modules/ambience/Ambience.d.ts +0 -9
  546. package/types/generated/modules/calendar/Calendar.d.ts +84 -0
  547. package/types/generated/modules/calendar/index.d.ts +10 -0
  548. package/types/generated/modules/debug/DebugPanel.d.ts +2 -2
  549. package/types/generated/modules/grid/Grid.d.ts +61 -25
  550. package/types/generated/modules/grid/Layer.d.ts +5 -5
  551. package/types/generated/modules/grid/index.d.ts +29 -17
  552. package/types/generated/modules/music/Music.d.ts +5 -3
  553. package/types/generated/modules/net/index.d.ts +3 -3
  554. package/types/generated/modules/net/transports.d.ts +12 -3
  555. package/types/generated/modules/particles/Particles.d.ts +1 -1
  556. package/types/generated/modules/sky/Sky.d.ts +12 -104
  557. package/types/generated/modules/sky/skyModule.d.ts +1 -1
  558. package/types/generated/modules/timeline/Timeline.d.ts +8 -6
  559. package/types/generated/modules/weather/Weather.d.ts +17 -18
  560. package/types/generated/modules/weather/weatherLook.d.ts +12 -0
  561. package/types/generated/modules/world/World.d.ts +18 -3
  562. package/types/generated/render/PostFX.d.ts +67 -30
  563. package/types/generated/render/materials.d.ts +26 -20
  564. package/types/generated/render/modeling.d.ts +30 -0
  565. package/types/generated/render/patches.d.ts +16 -2
  566. package/types/generated/ui/UI.d.ts +6 -3
  567. package/types/nav.d.ts +6 -0
  568. package/types/physics.d.ts +137 -44
  569. package/types/style.d.ts +109 -81
  570. package/types/tsl.d.ts +86 -0
  571. package/types/world.d.ts +158 -110
  572. package/ui/Focus.js +25 -14
  573. package/ui/LoadingScreen.js +1 -1
  574. package/ui/Parts.js +27 -11
  575. package/ui/UI.js +20 -10
  576. package/ui/cursor.js +115 -0
  577. package/ui/defaults.js +15 -11
  578. package/ui/theme.js +9 -6
  579. package/ui/touchPart.js +1 -1
  580. package/core/claims.js +0 -51
  581. package/core/look/layers.js +0 -136
  582. package/core/renamed.js +0 -22
  583. package/docs/api/entities.md +0 -61
  584. package/examples/styletest/overrides.js +0 -33
  585. package/examples/styletest/panel.js +0 -135
  586. package/modules/entities/Entities.js +0 -243
  587. package/modules/entities/build.js +0 -25
  588. package/modules/entities/index.js +0 -4
  589. package/modules/sky/calendar.js +0 -80
  590. package/modules/sky/layers.js +0 -69
  591. package/modules/terrain/chunkLevels.js +0 -40
  592. package/modules/terrain/mountTerrain.js +0 -32
  593. package/modules/world/cells.js +0 -42
package/render/patches.js CHANGED
@@ -1,11 +1,16 @@
1
- // Named shader patches: one way to add shader code to any three.js material (WebGLRenderer only).
2
- // A patch says where its code goes (after or before a `#include <chunk>` anchor), which uniforms it needs and what makes its
3
- // program unique (cacheKey). Patches stack: the material's own onBeforeCompile runs first, then every patch in the order added,
1
+ // Named shader patches. One way to add shader code to any three.js material: GLSL on WebGLRenderer, the patch's tsl on NodeMaterials (WebGPURenderer).
2
+ // A patch says where its code goes (a semantic slot from render/slots.js: 'position', 'color', 'lighting'...; or, as the expert exit,
3
+ // a raw `#include <chunk>` anchor with before / after / replace), which uniforms it needs and what makes its
4
+ // program unique (cacheKey). Patches stack: the material's own onBeforeCompile runs first (also one the game assigns after the engine
5
+ // patched it: the instance accessor keeps both, see patchMaterial), then every patch in the order added,
4
6
  // and the program cache key joins all of them, so two patches never overwrite each other and differently-parameterised
5
7
  // materials never share a program. A missing anchor is reported when the patch is added, not discovered as a silent no-op.
6
- // GLSL lives under `glsl`; a WebGPU node version would be a separate field once a WebGPU path exists (WebGPURenderer does not
7
- // run onBeforeCompile). Engine uniforms (engine.uniforms: uTime, uWind, uCloud...) are resolved per renderer when the program
8
- // compiles, so one material works under any engine and two engines on a page each drive their own time and weather.
8
+ // GLSL lives under `glsl`; the TSL version under `tsl`: (ctx) => node per slot, applied by render/fx/patchNodes.tsl.js (loaded with threedog/tsl)
9
+ // on a NodeMaterial, and on the node twin WebGPURenderer builds of a classic material (dressNodeTwins below). So one patched MeshLambertMaterial
10
+ // draws with its GLSL on WebGLRenderer and with its tsl on WebGPURenderer: the engine's own materials stay classic, which is what lets
11
+ // fallback: 'webgl' (decided in start(), after they were made) keep them. A patch without tsl throws on a NodeMaterial and is named by
12
+ // start() on WebGPURenderer (render/backend.js webglOnlyOn). Engine uniforms (engine.uniforms: uTime, uWind, uCloud...) are resolved per
13
+ // renderer when the program compiles, so one material works under any engine and two engines on a page each drive their own time and weather.
9
14
  // A patch that moves vertices (shadow: true) owns its shadow: meshes using the material get matching depth and distance
10
15
  // materials automatically (see the customDepthMaterial / customDistanceMaterial defaults below), callers never set them.
11
16
  // material.clone() does not carry patches (three's Material.copy skips onBeforeCompile): copy them with copyPatches(from, to).
@@ -23,17 +28,35 @@
23
28
  // the material's own key + '|threedog:' + name=cacheKey of every patch in order; one name + cacheKey producing
24
29
  // two different GLSL texts throws, since those materials would silently share one program.
25
30
  import * as THREE from 'three';
26
- import { closest } from '../core/validate.js';
31
+ import { closest, DEV, warnOnce } from '../core/validate.js';
32
+ import { EFFECTS, PROVIDED_BY } from './effects.js';
33
+ import { noteCopy, VERSION } from '../core/copies.js';
34
+ import { notePatch, patchOn } from '../core/threePatches.js';
27
35
  import { sameUniform, resolveUniform } from './engineUniforms.js';
36
+ import { SLOTS, SLOT_NAMES, toAnchors } from './slots.js';
37
+ import { codedError, coded } from '../core/errorHints.js';
38
+ import { webgpuPart, webgpuPartOrNull, addWebGPURule } from './backend.js';
39
+ import { engineUniformName } from './engineUniforms.js';
40
+ import { copyTerms } from './terms.js'; // a cycle (terms.js patches materials); only called at run time, never while loading
41
+
28
42
  /** @import { Material } from '../types/common' */
29
43
 
30
44
  /**
31
45
  * @typedef {{ before?: string, after?: string, replace?: string } | string} PatchCode code for one anchor (a plain string goes after the include;
32
46
  * replace swaps the include itself for new code, one patch per anchor)
47
+ * @typedef {'position' | 'shadowPosition' | 'clipPosition' | 'map' | 'color' | 'normal' | 'roughness' | 'emissive' | 'lightsPars' | 'lightingModel' | 'lighting' | 'output' | 'fogPars' | 'fog'} PatchSlot
33
48
  * @typedef {{ name: string, cacheKey?: string | number, uniforms?: Record<string, { value: any }>, shadow?: boolean,
34
- * needs?: { data?: string[], patches?: string[] }, conflicts?: string[],
35
- * glsl?: { vertex?: Record<string, PatchCode>, fragment?: Record<string, PatchCode> } }} ShaderPatch
36
- * glsl.vertex / glsl.fragment: anchor chunk name -> code; the key `pars` is declarations placed at the top of the shader.
49
+ * needs?: { data?: string[], patches?: string[] }, conflicts?: string[], slot?: PatchSlot, pars?: string | { vertex?: string, fragment?: string },
50
+ * glsl?: string | { [slot in PatchSlot]?: string } | { vertex?: Record<string, PatchCode>, fragment?: Record<string, PatchCode> },
51
+ * tsl?: ((ctx: any) => any) | { [slot in PatchSlot]?: any } }} ShaderPatch
52
+ * tsl (NodeMaterial / WebGPURenderer): (ctx) => node for `slot`, or { slot: fn }; ctx[slot] is the value so far, ctx.fx the threedog/tsl
53
+ * functions (ctx.fx.patchUniform(u) = a { value } uniform of the patch as a node), ctx.tsl three/tsl.
54
+ * The lighting slot returns { directDiffuse?, directSpecular?, indirectDiffuse?, indirectSpecular? }, or takes
55
+ * hooks { start?(ctx), light?(light, ctx) => color, direct?(light, ctx), after?(ctx) } (light = { direction, color, shadow, directional, light };
56
+ * light() returns that one light's new color before three uses it; ctx.state is shared by the hooks).
57
+ * slot + glsl string: that slot's code (render/slots.js lists the slots, where each goes and what is in scope); glsl { slot: code }:
58
+ * several slots; pars: declarations at the top of every stage the patch touches (or { vertex, fragment }).
59
+ * Raw-anchor exit: glsl.vertex / glsl.fragment: three.js chunk name -> code; the key `pars` is declarations placed at the top of the shader.
37
60
  * shadow: the patch moves vertices, so the depth and point-light distance passes run its vertex code too.
38
61
  * needs.data: program inputs read, e.g. ['instancing']; needs.patches: patches it builds on, e.g. ['world'] (the vThreedogWorld varying).
39
62
  * conflicts: patch names it cannot be combined with. cacheKey: changes whenever the GLSL text changes (see the header).
@@ -54,7 +77,10 @@ function missing(m, p) {
54
77
  for (const stage of ['vertex', 'fragment']) {
55
78
  const src = sourceOf(m, stage);
56
79
  if (src == null) return null;
57
- for (const k in p.glsl?.[stage] || {}) if (k !== 'pars' && !src.includes(`#include <${k}>`)) out.push(`${stage} <${k}>`);
80
+ for (const k in p.glsl?.[stage] || {}) if (k !== 'pars' && !src.includes(`#include <${k}>`)) {
81
+ const slot = SLOT_NAMES.find(n => SLOTS[n].stage === stage && SLOTS[n].anchor === k);
82
+ out.push(`${stage} <${k}>` + (slot ? ` (slot '${slot}')` : ''));
83
+ }
58
84
  }
59
85
  return out;
60
86
  }
@@ -73,13 +99,14 @@ function clash(list, p) {
73
99
  for (const q of others) if (p.conflicts?.includes(q.name) || q.conflicts?.includes(p.name))
74
100
  return `patches '${q.name}' and '${p.name}' cannot be combined on one material; keep one of them, or use two materials`;
75
101
  for (const n of p.needs?.patches || []) if (!others.some(q => q.name === n))
76
- return `patch '${p.name}' needs patch '${n}' on the material first; put '${n}' before it in the patches list (applyEffects adds the built-in 'world' itself when a patch needs it), or fix why '${n}' was skipped`;
102
+ return `patch '${p.name}' needs patch '${n}' on the material first; put '${n}' before it in the patches list (the built-in 'world' is added by the engine), or fix why '${n}' was skipped`;
77
103
  for (const q of others) for (const u in p.uniforms || {}) if (q.uniforms?.[u] && !sameUniform(q.uniforms[u], p.uniforms[u]))
78
104
  return `patches '${q.name}' and '${p.name}' both bring a uniform named '${u}'; rename one (prefix it with the patch name), or share engine.uniforms.${u}`;
79
105
  const mine = replaced(p);
80
106
  for (const q of others) if (replaced(q).some(k => mine.includes(k)))
81
107
  return `patches '${q.name}' and '${p.name}' both replace one chunk; keep one, or add code before / after it`;
82
- const key = p.name + '=' + (p.cacheKey ?? ''), code = JSON.stringify(p.glsl ?? {});
108
+ // strip slots.js's marker comments before comparing: the same code written with slots or anchors differs only by slot= in the marker and generates the same program, so it is not "same cacheKey, different code"
109
+ const key = p.name + '=' + (p.cacheKey ?? ''), code = JSON.stringify(p.glsl ?? {}).replace(/\/\/ threedog:(begin|end)[^\\"]*/g, '');
83
110
  if (CODE.has(key) && CODE.get(key) !== code)
84
111
  return `patch '${p.name}' produced different GLSL under the same cacheKey '${p.cacheKey ?? ''}'; put every value that changes the code into cacheKey`;
85
112
  CODE.set(key, code);
@@ -87,13 +114,33 @@ function clash(list, p) {
87
114
  }
88
115
 
89
116
  const LISTS = new WeakMap(); // material -> its patches (not userData: that gets JSON-cloned and exported to glTF)
117
+
118
+ // Shared declarations appear once per stage. Patches may share engine.uniforms (uTime ...) and the threedog_* chunks, and each patch's pars is
119
+ // written to stand alone, so two patches on one material each declare `uniform float uTime;` (twice is a GLSL redefinition: the program fails).
120
+ // Done here, in the one place that joins the pars, so no patch has to guard its own lines: threedog_* includes are expanded once (a second
121
+ // include of the same chunk is dropped), then a uniform name already declared by the material's own shader or an earlier patch is dropped.
122
+ const DECL = /\buniform\s+(?:(?:lowp|mediump|highp)\s+)?\w+\s+(\w+)\s*(?:\[[^\]]*\])?\s*;/g;
123
+ const OWN_INC = /#include\s+<(threedog_\w+)>/g;
124
+ function sharedOnce(pars, src) {
125
+ const expander = (seen = new Set()) => function expand(code) {
126
+ return code.replace(OWN_INC, (all, name) => {
127
+ if (seen.has(name)) return '';
128
+ seen.add(name);
129
+ const chunk = THREE.ShaderChunk[name];
130
+ return chunk == null ? all : expand(chunk);
131
+ });
132
+ };
133
+ // the material's own source is only scanned (three resolves it), with its own chunk set: a statement chunk included inside main() must not
134
+ // hide a global one from the pars
135
+ const uniforms = new Set(Array.from(expander()(src).matchAll(DECL), m => m[1])), expand = expander();
136
+ return pars.map(code => expand(code).replace(DECL, (all, name) => uniforms.has(name) ? '' : (uniforms.add(name), all)));
137
+ }
90
138
  const PROTO_COMPILE = THREE.Material.prototype.onBeforeCompile;
91
- const warned = new Set();
92
139
 
93
140
  function apply(shader, list, m, renderer) {
94
141
  for (const p of list) for (const d of p.needs?.data || []) if (!shader[d]) {
95
- const tag = `${m.uuid}:${p.name}:${d}`;
96
- if (!warned.has(tag)) { warned.add(tag); console.error(`[threedog] patch '${p.name}' needs '${d}', which this mesh does not provide (e.g. 'instancing' needs an InstancedMesh, 'vertexColors' needs material.vertexColors and a color attribute)`); }
142
+ // keyed per material + patch: the same text for another mesh is another place to fix
143
+ warnOnce(coded('shader-patch', `[threedog] patch '${p.name}' needs '${d}', which this mesh does not provide (e.g. 'instancing' needs an InstancedMesh, 'vertexColors' needs material.vertexColors and a color attribute)`), `patch-data\0${m.uuid}:${p.name}:${d}`);
97
144
  }
98
145
  for (const stage of ['vertex', 'fragment']) {
99
146
  const key = stage + 'Shader', pars = [], at = new Map();
@@ -110,13 +157,12 @@ function apply(shader, list, m, renderer) {
110
157
  for (const [k, { before, after, replace }] of at) {
111
158
  const inc = `#include <${k}>`;
112
159
  if (!src.includes(inc)) {
113
- const tag = `${m.type}:${k}`;
114
- if (!warned.has(tag)) { warned.add(tag); console.error(`[threedog] shader patch anchor ${inc} not found in ${m.type}; add it to your shader or #include the threedog_* chunks by hand (see render/patches.js)`); }
160
+ warnOnce(coded('shader-patch', `[threedog] shader patch anchor ${inc} not found in ${m.type}; add it to your shader or #include the threedog_* chunks by hand (see render/patches.js)`));
115
161
  continue;
116
162
  }
117
163
  src = src.replace(inc, () => [...before, replace ?? inc, ...after].join('\n'));
118
164
  }
119
- shader[key] = pars.join('\n') + '\n' + src;
165
+ shader[key] = sharedOnce(pars, src).join('\n') + '\n' + src;
120
166
  }
121
167
  }
122
168
 
@@ -128,9 +174,9 @@ function apply(shader, list, m, renderer) {
128
174
  * @example
129
175
  * // reads the world position varying of 'clouds', and must not share a material with the engine's 'wind'
130
176
  * patchMaterial(mat, [{ name: 'ripple', needs: { patches: ['clouds'] }, conflicts: ['wind'], shadow: true, ... }]);
131
- * patchMaterial(mat, [{ name: 'snap', cacheKey: 64, uniforms: { uGrid: { value: 64 } }, glsl: {
132
- * vertex: { pars: 'uniform float uGrid;', project_vertex: 'gl_Position.xy = floor(gl_Position.xy / gl_Position.w * uGrid) / uGrid * gl_Position.w;' } } }]);
133
- * @internal games and style packs go through applyEffects(material, { patches })
177
+ * patchMaterial(mat, [{ name: 'snap', slot: 'clipPosition', cacheKey: 64, uniforms: { uGrid: { value: 64 } }, pars: 'uniform float uGrid;',
178
+ * glsl: 'gl_Position.xy = floor(gl_Position.xy / gl_Position.w * uGrid) / uGrid * gl_Position.w;' }]);
179
+ * @internal games and styles go through applyEffects(material, { patches })
134
180
  * @param {Material} material
135
181
  * @param {ShaderPatch[]} patches
136
182
  * @param {{ strict?: boolean }} [opts]
@@ -139,36 +185,173 @@ function apply(shader, list, m, renderer) {
139
185
  export function patchMaterial(material, patches, { strict = true } = {}) {
140
186
  const m = /** @type {any} */ (material);
141
187
  let list = LISTS.get(m);
188
+ // NodeMaterials take no onBeforeCompile / GLSL program key (three keys them by their nodes): the list alone, turned into nodes below
189
+ if (!list && m.isNodeMaterial) LISTS.set(m, list = []);
142
190
  if (!list) {
143
191
  LISTS.set(m, list = []);
144
- const prev = m.onBeforeCompile;
145
- const prevKey = Object.hasOwn(m, 'customProgramCacheKey') ? m.customProgramCacheKey.bind(m) : prev !== PROTO_COMPILE ? () => prev.toString() : () => '';
146
- m.onBeforeCompile = function (shader, renderer) { if (prev !== PROTO_COMPILE) prev.call(this, shader, renderer); apply(shader, list, m, renderer); };
192
+ // the game's own onBeforeCompile / customProgramCacheKey: those there before patching and those assigned after patching are all kept here and run first (user decision 5, 2026-11-02).
193
+ // Why an instance accessor instead of a warning: before, a game assigning `m.onBeforeCompile = fn` later silently overwrote the wrapper, losing wind / cloud shadow / season;
194
+ // with a warning an AI could only copy it into the chained form, an extra step for nothing. Now assignment replaces only "the game's part", engine patches still follow, and both apply.
195
+ let game = m.onBeforeCompile, gameKey = Object.hasOwn(m, 'customProgramCacheKey') ? m.customProgramCacheKey : null;
196
+ const own = game !== PROTO_COMPILE;
197
+ const wrapper = function (shader, renderer) { if (game && game !== PROTO_COMPILE) game.call(this, shader, renderer); apply(shader, list, m, renderer); };
198
+ // the wrapper marks itself as the engine's and hands out the game's part: the WebGPU route names only the game's GLSL hook (render/backend.js)
199
+ wrapper[Symbol.for('threedog.patchWrapper')] = () => game;
200
+ const key = () => (gameKey ? gameKey.call(m) : game && game !== PROTO_COMPILE ? game.toString() : '') + '|threedog:' + list.map(p => p.name + '=' + (p.cacheKey ?? '')).join(',');
201
+ if (!patchOn('material-compile-accessor')) { m.onBeforeCompile = wrapper; m.customProgramCacheKey = key; } // three's plain properties: a later assignment replaces both
202
+ else {
203
+ Object.defineProperty(m, 'onBeforeCompile', { configurable: true, enumerable: own, get: () => wrapper, set(fn) { game = fn; m.needsUpdate = true; } });
204
+ Object.defineProperty(m, 'customProgramCacheKey', { configurable: true, enumerable: false, get: () => key, set(fn) { gameKey = fn; m.needsUpdate = true; } });
205
+ }
147
206
  m.addEventListener('dispose', () => dropShadow(m));
148
- m.customProgramCacheKey = () => prevKey() + '|threedog:' + list.map(p => p.name + '=' + (p.cacheKey ?? '')).join(',');
149
207
  }
150
- for (const p of patches) {
208
+ // slot form is converted to anchor form at the entry (one internal representation), so the later conflict / shadow / cacheKey checks are the same for both forms; derived ones (copyPatches / shadow material) are already converted
209
+ const before = list.slice();
210
+ for (const p of deriving ? patches : withProviders(patches, list).map(toAnchors)) {
211
+ // a GLSL-only patch on a NodeMaterial throws even when not strict: skipping it would draw the material without its look, silently
212
+ if (m.isNodeMaterial && !deriving && p.tsl == null) throw codedError('shader-patch', `[threedog] patch '${p.name}' has no tsl version; add tsl: (ctx) => ..., or keep this material on WebGLRenderer (${m.type} is a NodeMaterial)`);
213
+ const nodeGap = m.isNodeMaterial && !deriving ? nodePatches(m).gaps(m, p) : '';
214
+ if (nodeGap) {
215
+ if (strict) throw codedError('shader-patch', `[threedog] ${nodeGap}`);
216
+ warnOnce(coded('shader-patch', `[threedog] ${nodeGap}; skipped`));
217
+ continue;
218
+ }
151
219
  const bad = deriving ? '' : clash(list, p);
152
220
  if (bad) {
153
- if (strict) throw new Error(`[threedog] ${bad}`);
154
- console.error(`[threedog] ${bad}; patch '${p.name}' skipped`);
221
+ if (strict) throw codedError('shader-patch', `[threedog] ${bad}`);
222
+ warnOnce(coded('shader-patch', `[threedog] ${bad}; patch '${p.name}' skipped`));
155
223
  continue;
156
224
  }
157
225
  const gaps = missing(m, p);
158
226
  if (gaps?.length) {
159
227
  const msg = `patch '${p.name}' needs ${gaps.join(', ')}, which ${m.type} does not have`;
160
- if (strict) throw new Error(msg + '; use a material that has these chunks, or #include the threedog_* chunks in your own shader');
161
- if (!warned.has(msg)) { warned.add(msg); console.warn('[threedog] ' + msg + '; skipped'); }
228
+ if (strict) throw codedError('shader-patch', msg + '; use a material that has these chunks, or #include the threedog_* chunks in your own shader');
229
+ warnOnce(coded('shader-patch', '[threedog] ' + msg + '; skipped'));
162
230
  continue;
163
231
  }
164
232
  const i = list.findIndex(q => q.name === p.name);
165
233
  if (i >= 0) list[i] = p; else list.push(p);
166
234
  }
235
+ if (m.isNodeMaterial) try { nodePatches(m).apply(m, list); } catch (e) { list.splice(0, list.length, ...before); throw e; }
236
+ else changedClassic(m);
167
237
  dropShadow(m);
168
238
  m.needsUpdate = true;
169
239
  return m;
170
240
  }
171
241
 
242
+ /** The TSL side of a classic material: its nodes follow the list (rebuilt on the next read) when threedog/tsl is loaded; WebGL games skip it */
243
+ function changedClassic(m) {
244
+ webgpuPartOrNull('nodePatches')?.invalidate(m, () => LISTS.get(m) ?? []);
245
+ if (!Object.hasOwn(m, NODE_KEY)) Object.defineProperty(m, NODE_KEY, { configurable: true, enumerable: true, get: () => nodeKey(m) });
246
+ }
247
+
248
+ // WebGPURenderer keys a classic material's program by its customProgramCacheKey (the patch names and cacheKeys above) plus its enumerable
249
+ // properties, and shares one node build between materials whose keys match. The build holds the patch's own uniform nodes (render/fx:
250
+ // one node per { value } object), so two materials with the same patches but their own uniform objects would draw with the first one's
251
+ // values. This enumerable property adds those objects' identities to the WebGPU key only (WebGLRenderer never reads it, and there every
252
+ // material binds its own uniforms, so sharing a program is right). Materials that share the uniform objects (one material, or patches
253
+ // made once and reused) still share one program.
254
+ const NODE_KEY = 'threedogNodeKey';
255
+ const IDS = new WeakMap();
256
+ let nextId = 0;
257
+ const idOf = o => { let i = IDS.get(o); if (i === undefined) IDS.set(o, i = ++nextId); return i; };
258
+ function nodeKey(m) {
259
+ const ids = [];
260
+ for (const p of LISTS.get(m) ?? []) for (const u in p.uniforms ?? {}) if (!engineUniformName(p.uniforms[u])) ids.push(idOf(p.uniforms[u]));
261
+ return ids.join('.');
262
+ }
263
+
264
+ /**
265
+ * @internal One patch object per (owner, kind, options): materials made from the same spec get the same patch, so its uniform objects and
266
+ * with them threedogNodeKey are the same and WebGPURenderer builds one program for all of them (8 same-role materials: 8 builds -> 1).
267
+ * WebGL already shares the program (its key is the code); there this also saves the uniform objects. Options that hold something whose
268
+ * identity matters (a function, the game's own uniforms objects, any non-plain object but a Color / Vector / Texture) are not shared:
269
+ * make() runs every time. Shared means one set of values: change a patch uniform on one material and every material of that spec follows,
270
+ * which is what a spec means; give a material its own options to tune it alone.
271
+ * @template T @param {object | null} owner the engine (null: a page-wide table) @param {string} kind @param {any} opts @param {() => T} make @returns {T}
272
+ */
273
+ export function sharedPatch(owner, kind, opts, make) {
274
+ const key = optionsKey(opts);
275
+ if (key == null) return make();
276
+ const o = owner ?? SHARED_PAGE;
277
+ let t = SHARED.get(o);
278
+ if (!t) SHARED.set(o, t = new Map());
279
+ const k = kind + ':' + key;
280
+ let p = t.get(k);
281
+ if (!p) t.set(k, p = make());
282
+ return p;
283
+ }
284
+ const SHARED = new WeakMap(), SHARED_PAGE = {};
285
+ /** A text key for plain options, or null when they hold something that cannot be keyed by value @param {any} v */
286
+ function optionsKey(v) {
287
+ let ok = true;
288
+ const s = JSON.stringify(v, (k, x) => {
289
+ if (!ok || x == null || typeof x !== 'object') { if (typeof x === 'function') ok = false; return x; }
290
+ if (k === 'uniforms') { ok = false; return null; }
291
+ if (x.isTexture) return 'tex#' + idOf(x);
292
+ if (x.isColor) return 'c#' + x.getHexString();
293
+ if (x.isVector2 || x.isVector3 || x.isVector4) return x.toArray();
294
+ if (Array.isArray(x) || Object.getPrototypeOf(x) === Object.prototype) return x;
295
+ ok = false; return null;
296
+ });
297
+ return ok ? s : null;
298
+ }
299
+
300
+ /**
301
+ * @internal core/Engine.js, once per WebGPURenderer: the node twin three makes of a classic material (NodeLibrary.fromMaterial, run for each
302
+ * program it builds) gets the material's patches as nodes. Why this one call: it is where three turns MeshLambertMaterial into
303
+ * MeshLambertNodeMaterial, copying the plain properties and nothing of onBeforeCompile, so without it every engine material lost wind,
304
+ * season, cloud shadows, detail and lighting on WebGPU without a word. Unpatched twins pass through too: a Lambert / Phong twin gets
305
+ * scene.environment's diffuse light, which three's WebGLRenderer gives the classic material and its node twin does not (patchNodes.tsl.js).
306
+ * @param {any} r
307
+ */
308
+ export function dressNodeTwins(r) {
309
+ const lib = r?.library;
310
+ if (!lib || lib._threedogTwins) return;
311
+ const from = lib.fromMaterial.bind(lib);
312
+ lib.fromMaterial = material => {
313
+ const twin = from(material);
314
+ if (!twin || twin === material) return twin;
315
+ const list = LISTS.get(material) ?? [];
316
+ // every twin goes through dress (Lambert / Phong get scene.environment's diffuse light as on WebGL); only a patched one needs threedog/tsl
317
+ const part = list.length ? nodePatches(material) : webgpuPartOrNull('nodePatches');
318
+ return part ? part.dress(twin, material, list) : twin;
319
+ };
320
+ lib._threedogTwins = true;
321
+ }
322
+
323
+ // start() on WebGPURenderer, and each object added later: a classic material whose patches cannot all run there (a GLSL-only patch, a slot
324
+ // with no node place) is named instead of drawing without its look
325
+ addWebGPURule(m => {
326
+ const list = !m.isNodeMaterial ? LISTS.get(m) : null;
327
+ return list?.length ? webgpuPartOrNull('nodePatches')?.problem(m, list) ?? null : null;
328
+ });
329
+
330
+ /** The TSL side (render/fx/patchNodes.tsl.js registers it when threedog/tsl loads; a global, so this file never imports three/tsl) */
331
+ function nodePatches(m) {
332
+ return webgpuPart('nodePatches', `patches on ${m.type}`);
333
+ }
334
+
335
+ // A patch read a varying that an engine patch brings (vThreedogWorld comes from 'world') without writing needs; it used to fail compiling with undeclared identifier,
336
+ // with no hint to write needs: { patches: ['world'] }. There is a single provider and adding it changes nothing else visible, so it is added automatically before the patch (dev warns once when needs was not written),
337
+ // and also when needs was written: applyEffects / look.materials.add / style entries all pass through here, so none has to remember to add 'world'.
338
+ const reads = (p, name) => new RegExp(`\\b${name}\\b`).test(JSON.stringify([p.glsl ?? '', p.pars ?? '']));
339
+ /** patches with every engine patch they read from (by needs.patches or by the varyings their code uses) inserted before them, once */
340
+ function withProviders(patches, list) {
341
+ const out = [], have = n => list.some(q => q.name === n) || out.some(q => q.name === n);
342
+ for (const p of patches) {
343
+ for (const [varying, prov] of Object.entries(PROVIDED_BY)) {
344
+ if (p.name === prov || have(prov)) continue;
345
+ const declared = !!p.needs?.patches?.includes(prov);
346
+ if (!declared && !reads(p, varying)) continue;
347
+ out.push(EFFECTS[prov]());
348
+ if (DEV && !declared) warnOnce(`[threedog] patch '${p.name}' reads ${varying}: the engine added the '${prov}' patch that provides it. Say so in the patch with needs: { patches: ['${prov}'] } (recipes/43)`);
349
+ }
350
+ out.push(p);
351
+ }
352
+ return out;
353
+ }
354
+
172
355
  /**
173
356
  * Names of the patches on a material (in order), e.g. ['clouds', 'tint', 'wind']
174
357
  * @param {Material} material
@@ -176,6 +359,48 @@ export function patchMaterial(material, patches, { strict = true } = {}) {
176
359
  */
177
360
  export function materialPatches(material) { return (LISTS.get(material) || []).map(p => p.name); }
178
361
 
362
+ /**
363
+ * The patches on a material with the slots (or raw anchors) their code goes into, read from the threedog:begin markers (engine.inspect.materials)
364
+ * @internal
365
+ * @param {Material} material
366
+ * @returns {{ name: string, slots: string[] }[]}
367
+ */
368
+ export function materialPatchSlots(material) {
369
+ return (LISTS.get(material) || []).map(p => ({ name: p.name, slots: [...new Set([...JSON.stringify(p.glsl ?? {}).matchAll(/threedog:begin patch=\S+ slot=([\w.-]+)/g)].map(m => m[1]))] }));
370
+ }
371
+
372
+ let hotRev = 0;
373
+ /**
374
+ * Shader hot reload (render/hotGlsl.js): swap the GLSL text `old` for `neu` inside the patches of these materials. A changed patch gets a new cacheKey
375
+ * (else three.js would reuse the program compiled from the old text), and every material carrying it recompiles. Returns the materials changed.
376
+ * @internal
377
+ * @param {Material[]} materials @param {string} old @param {string} neu
378
+ * @returns {Material[]}
379
+ */
380
+ export function replacePatchCode(materials, old, neu) {
381
+ const changed = new Set(), seen = new Set();
382
+ // only walk shapes the patch code is known to use: glsl / pars / shadowVertex are strings, or plain objects (slots, vertex / fragment, chunk name -> string or { before, after, replace }),
383
+ // at most three levels. Why not recurse into every object field: today toAnchors already turned glsl into a new object holding only strings, but if the internal representation changes (e.g. uniforms brought in),
384
+ // an Object3D / camera (parent <-> children cycle) would overflow the stack on hot reload, and also trigger getters. Non-plain objects (three class instances) and uniforms keys are never entered
385
+ const swap = (o, k, depth = 0) => {
386
+ const v = o[k];
387
+ if (typeof v === 'string') { if (!v.includes(old)) return false; o[k] = v.replaceAll(old, neu); return true; }
388
+ if (depth >= 3 || !v || typeof v !== 'object' || Object.getPrototypeOf(v) !== Object.prototype) return false;
389
+ let hit = false;
390
+ for (const kk of Object.keys(v)) if (kk !== 'uniforms' && swap(v, kk, depth + 1)) hit = true;
391
+ return hit;
392
+ };
393
+ for (const m of materials) for (const p of LISTS.get(m) || []) {
394
+ if (seen.has(p)) continue;
395
+ seen.add(p);
396
+ const hit = ['glsl', 'pars', 'shadowVertex'].map(k => swap(p, k)).some(Boolean);
397
+ if (hit) { p.cacheKey = `${String(p.cacheKey ?? '').replace(/~hot\d+$/, '')}~hot${++hotRev}`; changed.add(p); }
398
+ }
399
+ const out = materials.filter(m => (LISTS.get(m) || []).some(p => changed.has(p)));
400
+ for (const m of out) { dropShadow(m); m.needsUpdate = true; if (!(/** @type {any} */ (m).isNodeMaterial)) changedClassic(m); }
401
+ return out;
402
+ }
403
+
179
404
  /**
180
405
  * Take the patch named `name` off a material (no-op when absent); returns the material.
181
406
  * @internal sky cascades
@@ -185,7 +410,10 @@ export function materialPatches(material) { return (LISTS.get(material) || []).m
185
410
  */
186
411
  export function unpatchMaterial(material, name) {
187
412
  const list = LISTS.get(material), i = list ? list.findIndex(p => p.name === name) : -1;
188
- if (i >= 0) { list.splice(i, 1); dropShadow(material); material.needsUpdate = true; }
413
+ if (i >= 0) {
414
+ list.splice(i, 1); dropShadow(material); material.needsUpdate = true;
415
+ if (/** @type {any} */ (material).isNodeMaterial) nodePatches(material).apply(material, list); else changedClassic(material);
416
+ }
189
417
  return material;
190
418
  }
191
419
 
@@ -200,10 +428,43 @@ export function unpatchMaterial(material, name) {
200
428
  export function copyPatches(from, to) {
201
429
  const list = LISTS.get(from);
202
430
  if (!list?.length || from === to) return to;
431
+ copyTerms(from, to); // the copy carries the cloud / season terms, so a later look.cloudShadow / look.season replace reaches it too
432
+ deriving = true; // already checked on `from`
433
+ try { return patchMaterial(to, list, { strict: false }); } finally { deriving = false; }
434
+ }
435
+
436
+ /**
437
+ * Give `to` the patches of `from` except the names in `skip` and those `to` already has (look.materials.rebuild: the new material keeps the wind /
438
+ * cloud shadow a module added after makeMaterial, while the look.materials patches come from the current layers). Returns `to`.
439
+ * @internal
440
+ * @param {Material} from @param {Material} to @param {Set<string>} skip
441
+ * @returns {Material}
442
+ */
443
+ export function carryPatches(from, to, skip) {
444
+ const have = new Set(materialPatches(to)), list = (LISTS.get(from) || []).filter(p => !skip.has(p.name) && !have.has(p.name));
445
+ copyTerms(from, to);
446
+ if (!list.length) return to;
203
447
  deriving = true; // already checked on `from`
204
448
  try { return patchMaterial(to, list, { strict: false }); } finally { deriving = false; }
205
449
  }
206
450
 
451
+ // ---------- which materials the engine made ----------
452
+ // Why record: automatic effects (season, cloud shadow, wind, cascaded-shadow list) should land only on materials the engine made (user decision 3, 2026-11-02); a game `new` material that wants effects must call
453
+ // applyEffects to opt in. WeakSet, not userData: userData is carried by clone and written into the file on glTF export, so a game material cloned from an engine one would be mistaken for it.
454
+ const MADE = new WeakSet();
455
+ /**
456
+ * Record a material as engine-made (makeMaterial, terrain, vegetation clones, water); returns it.
457
+ * @internal
458
+ * @template {Material} M @param {M} m @returns {M}
459
+ */
460
+ export function markEngineMade(m) { MADE.add(m); return m; }
461
+ /**
462
+ * Whether the engine made this material (see markEngineMade); a game's `new THREE.*Material` is false.
463
+ * @internal
464
+ * @param {Material} m @returns {boolean}
465
+ */
466
+ export function isEngineMade(m) { return MADE.has(m); }
467
+
207
468
  // ---------- shadows follow vertex-moving patches ----------
208
469
  // Depth / distance materials carrying the material's vertex-moving patches; undefined when nothing moves. Cached per material
209
470
  // and kind; patchMaterial and material.dispose() drop (and dispose) the cached ones, so patches added later are picked up.
@@ -226,7 +487,9 @@ function shadowMaterial(material, kind) {
226
487
  let byKind = shadowMats.get(material);
227
488
  if (!byKind) shadowMats.set(material, byKind = {});
228
489
  if (kind in byKind) return byKind[kind];
229
- const moving = LISTS.get(material).filter(p => p.shadow).map(p => ({ ...p, needs: { data: p.needs?.data }, conflicts: undefined, glsl: { vertex: p.glsl?.vertex } }));
490
+ // shadowPosition slot code goes only into the shadow material, replacing the same patch's position code (use when the shadow must differ from the color pass)
491
+ const moving = LISTS.get(material).filter(p => p.shadow).map(p => ({ ...p, needs: { data: p.needs?.data }, conflicts: undefined,
492
+ glsl: { vertex: p.shadowVertex ? { ...p.glsl?.vertex, ...p.shadowVertex } : p.glsl?.vertex } }));
230
493
  // three copies side, alphaTest, map and clipping from the drawn material onto the depth material on every draw
231
494
  const base = kind === 'depth' ? new THREE.MeshDepthMaterial() : new THREE.MeshDistanceMaterial();
232
495
  deriving = true; // vertex-only copies of patches already checked on the source material
@@ -237,6 +500,19 @@ function shadowMaterial(material, kind) {
237
500
  // however the mesh was built (game code, level load, instancing, a swapped material); an explicit assignment still wins. It must be
238
501
  // on Mesh.prototype before any mesh exists (Object3D's constructor assigns both, which would otherwise make own properties).
239
502
  // Explicit values live under a global symbol, so a second copy of this file (hot reload) redefining the accessor loses none.
503
+ // A second copy of the ENGINE (another path) still breaks the first copy's moving shadows (its shadowMaterial is no longer the one
504
+ // asked), so noteCopy says so once, with both paths and how to dedupe (core/copies.js).
505
+ { const w = noteCopy({ version: VERSION, url: import.meta.url }); if (w) warnOnce(w); }
506
+ {
507
+ /** @type {[string, PropertyDescriptor | undefined][]} */
508
+ const before = ['customDepthMaterial', 'customDistanceMaterial'].map(p => [p, Object.getOwnPropertyDescriptor(THREE.Mesh.prototype, p)]);
509
+ notePatch('shadow-accessors', 'Mesh.prototype.customDepthMaterial / customDistanceMaterial: an unassigned one returns the moving shadow of a wind-patched material',
510
+ () => { for (const [p, d] of before) { if (d) Object.defineProperty(THREE.Mesh.prototype, p, d); else delete THREE.Mesh.prototype[p]; } },
511
+ { lost: 'meshes with wind or another vertex-moving patch cast a still shadow' });
512
+ }
513
+ notePatch('material-compile-accessor', "onBeforeCompile / customProgramCacheKey on each material the engine patches (cloud shadow on plain lit materials, wind, fog): an own get / set, so the game's own hook assigned before or after still runs, then the engine's",
514
+ () => {}, // off: materials patched from now on get three's plain properties (patchMaterial asks patchOn)
515
+ { scope: 'patched materials', lost: "on materials patched from now on, assigning material.onBeforeCompile replaces the engine's patches (wind, cloud shadow, fog) instead of running before them" });
240
516
  for (const [prop, kind] of [['customDepthMaterial', 'depth'], ['customDistanceMaterial', 'distance']]) {
241
517
  const own = Symbol.for('threedog.' + prop);
242
518
  Object.defineProperty(THREE.Mesh.prototype, prop, {
@@ -245,7 +521,8 @@ for (const [prop, kind] of [['customDepthMaterial', 'depth'], ['customDistanceMa
245
521
  if (Object.hasOwn(this, own)) return this[own];
246
522
  // multi-material mesh (trunk + leaves): one shadow material serves every group, so take the first one that moves
247
523
  const m = Array.isArray(this.material) ? this.material.find(moves) : this.material;
248
- return moves(m) ? shadowMaterial(m, kind) : undefined;
524
+ // NodeMaterials: three's shadow pass reads material.positionNode itself (the tsl position chain), so no depth / distance material
525
+ return moves(m) && !m.isNodeMaterial ? shadowMaterial(m, kind) : undefined;
249
526
  },
250
527
  set(v) { if (v === undefined) delete this[own]; else this[own] = v; }, // Object3D's constructor assigns undefined
251
528
  });