threedog 0.7.0 → 0.8.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 (544) hide show
  1. package/CHANGELOG.md +196 -1
  2. package/README.md +6 -9
  3. package/core/{Animator.js → assets/Animator.js} +28 -18
  4. package/core/{Assets.js → assets/Assets.js} +98 -54
  5. package/core/{Baked.js → assets/Baked.js} +4 -4
  6. package/core/assets/assetEntry.js +237 -0
  7. package/core/{assetHot.js → assets/assetHot.js} +7 -6
  8. package/core/{assetLoad.js → assets/assetLoad.js} +55 -16
  9. package/core/assets/assetLod.js +113 -0
  10. package/core/{assetManifest.js → assets/assetManifest.js} +19 -11
  11. package/core/{assetReport.js → assets/assetReport.js} +6 -12
  12. package/core/{namedModel.js → assets/namedModel.js} +17 -13
  13. package/core/{Audio.js → audio/Audio.js} +26 -23
  14. package/core/{audioRules.js → audio/audioRules.js} +8 -5
  15. package/core/{audioVoice.js → audio/audioVoice.js} +17 -30
  16. package/core/{Events.js → base/Events.js} +3 -3
  17. package/core/{I18n.js → base/I18n.js} +10 -10
  18. package/core/{Random.js → base/Random.js} +5 -4
  19. package/core/{easing.js → base/easing.js} +1 -1
  20. package/core/{errorCodes.js → base/errorCodes.js} +98 -110
  21. package/core/{errorHints.js → base/errorHints.js} +5 -9
  22. package/core/{errors.js → base/errors.js} +6 -6
  23. package/core/{events.generated.js → base/events.generated.js} +1 -0
  24. package/core/{math.js → base/math.js} +15 -4
  25. package/core/{params.js → base/params.js} +3 -4
  26. package/core/base/util.js +32 -0
  27. package/core/{validate.js → base/validate.js} +28 -7
  28. package/core/{Engine.js → engine/Engine.js} +80 -77
  29. package/core/{Loading.js → engine/Loading.js} +4 -2
  30. package/core/{copies.js → engine/copies.js} +4 -4
  31. package/core/{headlessLoad.js → engine/headlessLoad.js} +4 -4
  32. package/core/{lifecycle.js → engine/lifecycle.js} +14 -12
  33. package/core/{loop.js → engine/loop.js} +68 -47
  34. package/core/{moduleSlots.js → engine/moduleSlots.js} +6 -6
  35. package/core/{moduleTypes.js → engine/moduleTypes.js} +13 -14
  36. package/core/{modules.js → engine/modules.js} +14 -11
  37. package/core/{page.js → engine/page.js} +36 -23
  38. package/core/{plugins.js → engine/plugins.js} +6 -9
  39. package/core/{services.js → engine/services.js} +47 -40
  40. package/core/{startup.js → engine/startup.js} +26 -24
  41. package/core/{Input.js → input/Input.js} +30 -22
  42. package/core/{inputContext.js → input/inputContext.js} +14 -5
  43. package/core/{inputPad.js → input/inputPad.js} +1 -1
  44. package/core/{inputPointer.js → input/inputPointer.js} +8 -2
  45. package/core/{inputTouch.js → input/inputTouch.js} +1 -1
  46. package/core/{Budget.js → inspect/Budget.js} +21 -20
  47. package/core/{DebugOverrides.js → inspect/DebugOverrides.js} +30 -16
  48. package/core/{Describe.js → inspect/Describe.js} +26 -22
  49. package/core/{Inspect.js → inspect/Inspect.js} +9 -7
  50. package/core/{InspectMaterials.js → inspect/InspectMaterials.js} +7 -8
  51. package/core/{InspectPerf.js → inspect/InspectPerf.js} +19 -13
  52. package/core/{InspectScene.js → inspect/InspectScene.js} +8 -5
  53. package/core/{MaterialInfo.js → inspect/MaterialInfo.js} +3 -2
  54. package/core/{UrlState.js → inspect/UrlState.js} +52 -20
  55. package/core/{catalog.js → inspect/catalog.js} +3 -3
  56. package/core/{checks.js → inspect/checks.js} +7 -5
  57. package/core/{debugView.js → inspect/debugView.js} +4 -4
  58. package/core/{describeRender.js → inspect/describeRender.js} +21 -20
  59. package/core/{describeText.js → inspect/describeText.js} +11 -6
  60. package/core/inspect/geometryWatch.js +38 -0
  61. package/core/{lightWatch.js → inspect/lightWatch.js} +8 -8
  62. package/core/{pictureWriters.js → inspect/pictureWriters.js} +15 -16
  63. package/core/{shaderCost.js → inspect/shaderCost.js} +1 -1
  64. package/core/{Ops.js → ops/Ops.js} +16 -11
  65. package/core/{control.js → ops/control.js} +13 -12
  66. package/core/ops/holds.js +139 -0
  67. package/core/{opsAtomic.js → ops/opsAtomic.js} +2 -1
  68. package/core/{opsCheck.js → ops/opsCheck.js} +4 -5
  69. package/core/{opsGuard.js → ops/opsGuard.js} +3 -3
  70. package/core/{opsRun.js → ops/opsRun.js} +7 -4
  71. package/core/{owned.js → ops/owned.js} +14 -17
  72. package/core/{sharedPoint.js → ops/sharedPoint.js} +8 -8
  73. package/core/{stackFrames.js → ops/stackFrames.js} +2 -2
  74. package/core/{stacks.js → ops/stacks.js} +12 -14
  75. package/core/{wrap.js → ops/wrap.js} +8 -6
  76. package/core/{SaveStore.js → saves/SaveStore.js} +1 -1
  77. package/core/{Saves.js → saves/Saves.js} +58 -36
  78. package/core/{saveApply.js → saves/saveApply.js} +13 -9
  79. package/core/{saveFiles.js → saves/saveFiles.js} +17 -15
  80. package/core/{saveValues.js → saves/saveValues.js} +20 -3
  81. package/core/{saveVersions.js → saves/saveVersions.js} +4 -17
  82. package/core/{session.js → saves/session.js} +31 -17
  83. package/core/{Rays.js → scene/Rays.js} +23 -25
  84. package/core/{Views.js → scene/Views.js} +14 -6
  85. package/core/{adaptive.js → scene/adaptive.js} +15 -8
  86. package/core/{canvasBox.js → scene/canvasBox.js} +2 -3
  87. package/core/{engineCamera.js → scene/engineCamera.js} +1 -1
  88. package/core/{gameLights.js → scene/gameLights.js} +17 -7
  89. package/core/{grayMap.js → scene/grayMap.js} +8 -2
  90. package/core/{grid.js → scene/grid.js} +4 -4
  91. package/core/{gridLoad.js → scene/gridLoad.js} +1 -1
  92. package/core/{ground.js → scene/ground.js} +9 -9
  93. package/core/{movement.js → scene/movement.js} +3 -3
  94. package/core/{pictureSlots.js → scene/pictureSlots.js} +2 -2
  95. package/core/{sceneDelta.js → scene/sceneDelta.js} +2 -2
  96. package/core/{sceneMethods.js → scene/sceneMethods.js} +36 -13
  97. package/core/scene/stepPoses.js +55 -0
  98. package/core/{targeting.js → scene/targeting.js} +4 -5
  99. package/core/{thingExplain.js → scene/thingExplain.js} +6 -5
  100. package/core/{thingOps.js → scene/thingOps.js} +4 -3
  101. package/core/{things.js → scene/things.js} +18 -5
  102. package/core/{threePatches.js → scene/threePatches.js} +1 -1
  103. package/core/{view.js → scene/view.js} +17 -16
  104. package/core/{Batching.js → stream/Batching.js} +85 -27
  105. package/core/{Stream.js → stream/Stream.js} +60 -30
  106. package/core/{batchGroups.js → stream/batchGroups.js} +10 -9
  107. package/core/{chunkStream.js → stream/chunkStream.js} +40 -0
  108. package/core/{scatter.js → stream/scatter.js} +2 -2
  109. package/core/{streamSources.js → stream/streamSources.js} +12 -7
  110. package/core/{Commits.js → threads/Commits.js} +6 -5
  111. package/core/{Jobs.js → threads/Jobs.js} +12 -10
  112. package/core/{Threads.js → threads/Threads.js} +16 -16
  113. package/core/{threadPure.js → threads/threadPure.js} +7 -5
  114. package/core/{threadServe.js → threads/threadServe.js} +3 -3
  115. package/core/{Clock.js → time/Clock.js} +17 -10
  116. package/core/{dayTime.js → time/dayTime.js} +5 -1
  117. package/core/{skip.js → time/skip.js} +18 -17
  118. package/core/{time.js → time/time.js} +18 -12
  119. package/core/{uiClock.js → time/uiClock.js} +4 -4
  120. package/docs/AI.md +22 -19
  121. package/docs/API.md +24 -23
  122. package/docs/CODEMAP.md +76 -183
  123. package/docs/COMMANDS.md +66 -58
  124. package/docs/CONVENTIONS.md +54 -14
  125. package/docs/MIGRATING-0.8.md +169 -0
  126. package/docs/THREE.md +78 -29
  127. package/docs/api/assets.md +12 -13
  128. package/docs/api/audio.md +6 -5
  129. package/docs/api/budget.md +4 -4
  130. package/docs/api/calendar.md +10 -11
  131. package/docs/api/cameraRig.md +29 -29
  132. package/docs/api/characters.md +23 -18
  133. package/docs/api/clock.md +1 -1
  134. package/docs/api/collision.md +12 -9
  135. package/docs/api/content.md +6 -6
  136. package/docs/api/dialogue.md +3 -3
  137. package/docs/api/engine.md +45 -43
  138. package/docs/api/events.md +3 -2
  139. package/docs/api/flow.md +1 -1
  140. package/docs/api/grids.md +1 -1
  141. package/docs/api/ground.md +4 -5
  142. package/docs/api/hud.md +2 -2
  143. package/docs/api/i18n.md +6 -6
  144. package/docs/api/input.md +7 -7
  145. package/docs/api/interactions.md +4 -4
  146. package/docs/api/inventory.md +9 -9
  147. package/docs/api/jobs.md +5 -5
  148. package/docs/api/level.md +7 -7
  149. package/docs/api/lightPool.md +1 -6
  150. package/docs/api/music.md +3 -3
  151. package/docs/api/nav.md +10 -10
  152. package/docs/api/openWorld.md +1 -1
  153. package/docs/api/particles.md +2 -2
  154. package/docs/api/photo.md +4 -2
  155. package/docs/api/pickups.md +2 -2
  156. package/docs/api/post.md +20 -3
  157. package/docs/api/quests.md +3 -3
  158. package/docs/api/rays.md +5 -7
  159. package/docs/api/rigid.md +27 -18
  160. package/docs/api/saves.md +8 -7
  161. package/docs/api/settings.md +9 -8
  162. package/docs/api/sky.md +8 -7
  163. package/docs/api/stream.md +7 -7
  164. package/docs/api/swarm.md +34 -0
  165. package/docs/api/terrain.md +7 -8
  166. package/docs/api/terrainDraw.md +1 -1
  167. package/docs/api/timeline.md +2 -2
  168. package/docs/api/tools.md +25 -22
  169. package/docs/api/vars.md +1 -1
  170. package/docs/api/vegetation.md +4 -3
  171. package/docs/api/vegetationDraw.md +1 -1
  172. package/docs/api/water.md +1 -1
  173. package/docs/api/weather.md +9 -9
  174. package/docs/api/weatherDraw.md +3 -3
  175. package/docs/api/world.md +5 -5
  176. package/docs/recipes/01-new-game.md +25 -40
  177. package/docs/recipes/02-npc.md +19 -20
  178. package/docs/recipes/03-items-pickups.md +27 -43
  179. package/docs/recipes/04-quests.md +3 -4
  180. package/docs/recipes/05-props-bake.md +29 -39
  181. package/docs/recipes/06-clock-events.md +27 -18
  182. package/docs/recipes/07-flow-scenes-pause.md +12 -17
  183. package/docs/recipes/08-weather-sky.md +88 -34
  184. package/docs/recipes/09-materials-lights-models.md +22 -36
  185. package/docs/recipes/10-blender-level.md +6 -7
  186. package/docs/recipes/11-vehicles.md +30 -22
  187. package/docs/recipes/12-navmesh.md +9 -8
  188. package/docs/recipes/13-animation.md +39 -17
  189. package/docs/recipes/14-custom-module.md +14 -13
  190. package/docs/recipes/15-perf.md +49 -26
  191. package/docs/recipes/16-dev-bridge.md +32 -52
  192. package/docs/recipes/18-world.md +36 -23
  193. package/docs/recipes/19-shooting-picking.md +4 -2
  194. package/docs/recipes/20-content-actions-save.md +30 -35
  195. package/docs/recipes/21-camera.md +33 -5
  196. package/docs/recipes/22-shop-gold.md +0 -4
  197. package/docs/recipes/23-ops.md +10 -10
  198. package/docs/recipes/26-ui-parts.md +22 -12
  199. package/docs/recipes/27-net.md +0 -2
  200. package/docs/recipes/28-terrain.md +117 -21
  201. package/docs/recipes/29-grid.md +2 -2
  202. package/docs/recipes/30-templates.md +55 -19
  203. package/docs/recipes/31-audio.md +5 -5
  204. package/docs/recipes/32-action-game.md +15 -6
  205. package/docs/recipes/33-stream-sources.md +2 -2
  206. package/docs/recipes/34-environment-lighting.md +67 -19
  207. package/docs/recipes/35-picture-from-reference.md +10 -17
  208. package/docs/recipes/36-ground-plants.md +105 -0
  209. package/docs/recipes/40-scene.md +7 -11
  210. package/docs/recipes/43-shader-patches-surfaces.md +20 -41
  211. package/docs/recipes/44-water-weather-hooks.md +48 -43
  212. package/docs/recipes/45-cascaded-shadows.md +39 -17
  213. package/docs/recipes/46-blender-art-route.md +7 -8
  214. package/docs/recipes/49-game-module-contract.md +5 -5
  215. package/docs/recipes/50-own-renderer.md +2 -2
  216. package/docs/recipes/52-around-the-picture.md +54 -98
  217. package/docs/recipes/53-ground-up-close.md +61 -24
  218. package/docs/recipes/55-mods-time-wrap.md +12 -16
  219. package/docs/recipes/56-webgpu-tsl.md +16 -14
  220. package/docs/recipes/57-control.md +9 -2
  221. package/docs/recipes/58-input.md +2 -2
  222. package/docs/recipes/59-depth-post-effects.md +1 -1
  223. package/docs/recipes/60-judge-the-picture.md +29 -24
  224. package/docs/recipes/62-picture-code.md +49 -17
  225. package/docs/recipes/63-third-person-start.md +17 -4
  226. package/docs/recipes/64-particles.md +1 -1
  227. package/docs/recipes/65-swarm.md +140 -0
  228. package/examples/action/.threedog/content-index.json +1 -1
  229. package/examples/action/GAMEMAP.md +2 -2
  230. package/examples/{sheepisland → perf-island}/.threedog/content-index.json +1 -1
  231. package/examples/{sheepisland → perf-island}/GAMEMAP.md +12 -11
  232. package/examples/{sheepisland → perf-island}/assets.js +1 -1
  233. package/examples/{sheepisland → perf-island}/main.js +8 -8
  234. package/examples/{sheepisland → perf-island}/models.js +1 -1
  235. package/examples/{sheepisland → perf-island}/params.js +1 -1
  236. package/examples/{sheepisland → perf-island}/picture/index.js +1 -1
  237. package/examples/{sheepisland → perf-island}/picture/palette.js +2 -2
  238. package/examples/{sheepisland → perf-island}/views.js +1 -1
  239. package/examples/plain-picture/GAMEMAP.md +1 -0
  240. package/examples/rc2d/GAMEMAP.md +3 -2
  241. package/examples/rc2d/main.js +1 -3
  242. package/examples/styletest/.threedog/content-index.json +1 -1
  243. package/examples/styletest/GAMEMAP.md +3 -2
  244. package/examples/techniques/contact-shadows/contactShadows.js +1 -1
  245. package/examples/techniques/contact-shadows/main.js +1 -1
  246. package/examples/techniques/interior-light/main.js +1 -1
  247. package/examples/techniques/modular-building/README.md +7 -6
  248. package/examples/techniques/outlines-and-rim/picture/post.js +1 -1
  249. package/examples/techniques/procedural-animation/README.md +1 -1
  250. package/examples/webgpu-tsl/GAMEMAP.md +1 -0
  251. package/examples/world/.threedog/content-index.json +1 -1
  252. package/examples/world/GAMEMAP.md +4 -3
  253. package/examples/world/picture/sky.js +1 -1
  254. package/index.d.ts +8 -7
  255. package/index.js +31 -31
  256. package/llms.txt +9 -13
  257. package/modules/ambience/Ambience.js +11 -8
  258. package/modules/calendar/Calendar.js +19 -19
  259. package/modules/calendar/index.js +13 -13
  260. package/modules/calendar/schedule.js +11 -6
  261. package/modules/calendar/seasons.js +7 -7
  262. package/modules/camera/FirstPersonCamera.js +2 -2
  263. package/modules/camera/FixedCamera.js +14 -15
  264. package/modules/camera/FollowCamera.js +4 -4
  265. package/modules/camera/RtsCamera.js +11 -11
  266. package/modules/camera/common.js +48 -24
  267. package/modules/camera/contract.js +1 -0
  268. package/modules/camera/projection.js +2 -2
  269. package/modules/characters/Character.js +33 -30
  270. package/modules/characters/NPC.js +27 -19
  271. package/modules/characters/assetModel.js +14 -10
  272. package/modules/characters/astar.js +7 -5
  273. package/modules/characters/behaviors.js +4 -5
  274. package/modules/characters/contract.js +2 -2
  275. package/modules/characters/index.js +168 -214
  276. package/modules/characters/moveHelpers.js +14 -3
  277. package/modules/characters/movers/common.js +16 -14
  278. package/modules/characters/movers/manual.js +1 -1
  279. package/modules/characters/movers/rapier.js +7 -6
  280. package/modules/characters/movers/simple.js +3 -3
  281. package/modules/characters/movers/support.js +8 -8
  282. package/modules/characters/npcAvoid.js +4 -4
  283. package/modules/characters/npcSim.js +23 -17
  284. package/modules/characters/npcView.js +6 -5
  285. package/modules/characters/pathAsync.js +12 -10
  286. package/modules/characters/pathWorker.js +1 -1
  287. package/modules/characters/player.js +108 -0
  288. package/modules/characters/playerStrings.js +2 -2
  289. package/modules/characters/proceduralRig.js +2 -2
  290. package/modules/characters/useModel.js +3 -3
  291. package/modules/characters/walkTo.js +11 -17
  292. package/modules/collision/Collision.js +26 -20
  293. package/modules/collision/contract.js +15 -0
  294. package/modules/content/explain.js +1 -2
  295. package/modules/content/fields.js +24 -3
  296. package/modules/content/ids.js +2 -2
  297. package/modules/content/index.js +61 -99
  298. package/modules/content/loader.js +14 -17
  299. package/modules/content/markers.js +25 -0
  300. package/modules/content/models.js +10 -7
  301. package/modules/content/packs.js +3 -3
  302. package/modules/content/parts.js +2 -2
  303. package/modules/content/places.js +35 -0
  304. package/modules/content/props.js +17 -14
  305. package/modules/content/rules.js +25 -21
  306. package/modules/content/stages.js +7 -7
  307. package/modules/content/templates.js +20 -20
  308. package/modules/content/words.js +34 -0
  309. package/modules/debug/DebugPanel.js +10 -43
  310. package/modules/flow/contract.js +1 -1
  311. package/modules/flow/index.js +11 -7
  312. package/modules/gameplay/Container.js +12 -11
  313. package/modules/gameplay/Containers.js +14 -5
  314. package/modules/gameplay/Dialogue.js +13 -11
  315. package/modules/gameplay/Interactions.js +37 -24
  316. package/modules/gameplay/Inventory.js +8 -9
  317. package/modules/gameplay/Quests.js +16 -9
  318. package/modules/gameplay/boxOps.js +1 -1
  319. package/modules/gameplay/dialogueBubbles.js +3 -4
  320. package/modules/gameplay/inventoryOps.js +0 -1
  321. package/modules/gameplay/pickups.js +22 -23
  322. package/modules/gameplay/questSave.js +3 -3
  323. package/modules/grid/Grid.js +1 -1
  324. package/modules/grid/Layer.js +4 -4
  325. package/modules/grid/index.js +7 -8
  326. package/modules/grid/preview.js +7 -7
  327. package/modules/grid/tiles.js +6 -6
  328. package/modules/hlod/index.js +37 -55
  329. package/modules/hud/index.js +17 -11
  330. package/modules/hud/parts.js +4 -4
  331. package/modules/level/contract.js +16 -0
  332. package/modules/level/heightfield.js +12 -10
  333. package/modules/level/index.js +47 -152
  334. package/modules/level/load.js +79 -0
  335. package/modules/level/meshes.js +65 -0
  336. package/modules/level/nodes.js +6 -7
  337. package/modules/lights/index.js +28 -181
  338. package/modules/lights/slots.js +93 -0
  339. package/modules/lights/sources.js +88 -0
  340. package/modules/music/Music.js +30 -26
  341. package/modules/music/rules.js +1 -1
  342. package/modules/nav/contract.js +14 -0
  343. package/modules/nav/index.js +11 -10
  344. package/modules/net/Client.js +7 -7
  345. package/modules/net/Host.js +7 -7
  346. package/modules/net/Net.js +1 -1
  347. package/modules/net/index.js +5 -4
  348. package/modules/net/sync.js +2 -2
  349. package/modules/net/transports.js +2 -2
  350. package/modules/particles/Particles.js +11 -8
  351. package/modules/particles/effect.js +7 -5
  352. package/modules/particles/shaders.js +2 -1
  353. package/modules/photo/Photo.js +15 -17
  354. package/modules/rigid/RigidBodies.js +66 -43
  355. package/modules/rigid/Vehicles.js +2 -3
  356. package/modules/rigid/ragdoll.js +287 -0
  357. package/modules/rigid/ragdollBones.js +159 -0
  358. package/modules/rigid/sync.js +6 -6
  359. package/modules/settings/Settings.js +108 -39
  360. package/modules/settings/apply.js +6 -6
  361. package/modules/settings/sections.js +16 -14
  362. package/modules/settings/view.js +3 -3
  363. package/modules/sky/Sky.js +29 -22
  364. package/modules/sky/clouds.js +7 -7
  365. package/modules/sky/dome.js +33 -20
  366. package/modules/sky/domeLayers.js +9 -9
  367. package/modules/sky/environment.js +6 -6
  368. package/modules/sky/frame.js +19 -10
  369. package/modules/sky/season.js +7 -1
  370. package/modules/sky/shader.js +24 -25
  371. package/modules/sky/skyModule.js +14 -27
  372. package/modules/sky/tint.js +1 -1
  373. package/modules/swarm/Swarm.js +346 -0
  374. package/modules/swarm/draw.js +68 -0
  375. package/modules/swarm/grid.js +63 -0
  376. package/modules/terrain/ChunkManager.js +70 -52
  377. package/modules/terrain/chunkQueries.js +7 -24
  378. package/modules/terrain/chunkWorker.js +1 -1
  379. package/modules/terrain/draw.js +14 -11
  380. package/modules/terrain/index.js +28 -34
  381. package/modules/terrain/terrainColorMap.js +6 -3
  382. package/modules/terrain/terrainDefaults.js +2 -1
  383. package/modules/terrain/terrainEdit.js +32 -27
  384. package/modules/terrain/terrainGen.js +17 -12
  385. package/modules/terrain/terrainIsland.js +2 -2
  386. package/modules/terrain/terrainMesh.js +7 -6
  387. package/modules/terrain/terrainShape.js +8 -10
  388. package/modules/terrain/terrainShapeMain.js +3 -3
  389. package/modules/timeline/Timeline.js +27 -19
  390. package/modules/timeline/Tween.js +6 -6
  391. package/modules/timeline/shots.js +5 -7
  392. package/modules/triggers/index.js +9 -9
  393. package/modules/vegetation/Vegetation.js +30 -23
  394. package/modules/vegetation/densityMap.js +4 -7
  395. package/modules/vegetation/draw.js +23 -18
  396. package/modules/vegetation/groundChunks.js +11 -31
  397. package/modules/vegetation/index.js +8 -6
  398. package/modules/vegetation/modelGeometry.js +3 -6
  399. package/modules/vegetation/nearFade.js +1 -1
  400. package/modules/water/Water.js +16 -16
  401. package/modules/water/surface.js +3 -3
  402. package/modules/weather/Weather.js +41 -30
  403. package/modules/weather/particles.js +8 -8
  404. package/modules/weather/weatherDraw.js +22 -18
  405. package/modules/world/World.js +8 -8
  406. package/modules/world/explain.js +1 -1
  407. package/modules/world/index.js +18 -12
  408. package/modules/world/persist.js +73 -25
  409. package/modules/world/pos.js +4 -4
  410. package/modules/world/records.js +28 -8
  411. package/modules/world/sim.js +4 -4
  412. package/modules/world/state.js +12 -5
  413. package/modules/world/thingSource.js +4 -2
  414. package/modules/world/views.js +11 -9
  415. package/package.json +42 -7
  416. package/presets.js +1 -3
  417. package/render/PostFX.js +39 -27
  418. package/render/backend.js +26 -32
  419. package/render/cascades.js +17 -12
  420. package/render/cascades.tsl.js +5 -8
  421. package/render/daylight.js +20 -3
  422. package/render/detail.js +8 -7
  423. package/render/effects.js +28 -25
  424. package/render/fuse.js +3 -3
  425. package/render/fx/cloudShadow.tsl.js +3 -3
  426. package/render/fx/detail.tsl.js +1 -1
  427. package/render/fx/index.js +5 -10
  428. package/render/fx/library.tsl.js +11 -0
  429. package/render/fx/lighting.tsl.js +2 -1
  430. package/render/fx/nodeErrors.tsl.js +2 -2
  431. package/render/fx/objectOrigin.tsl.js +1 -2
  432. package/render/fx/patchNodes.tsl.js +5 -15
  433. package/render/fx/postWebGPU.tsl.js +27 -23
  434. package/render/fx/sky.tsl.js +15 -17
  435. package/render/fx/uniforms.tsl.js +4 -4
  436. package/render/fx/wind.tsl.js +1 -1
  437. package/render/fxNumbers.js +3 -0
  438. package/render/layers.js +1 -1
  439. package/render/lighting.js +5 -3
  440. package/render/lightmap.js +20 -20
  441. package/render/lights.js +6 -6
  442. package/render/lit.js +24 -0
  443. package/render/materials.js +8 -7
  444. package/render/mergeCommon.js +22 -0
  445. package/render/modeling.js +2 -2
  446. package/render/occlusion.js +20 -4
  447. package/render/ownFrame.js +4 -4
  448. package/render/patches.js +13 -13
  449. package/render/postWebGL.js +12 -10
  450. package/render/probes.js +16 -19
  451. package/render/recordSweep.js +117 -0
  452. package/render/shaderErrors.js +10 -9
  453. package/render/shadowCache.tsl.js +107 -0
  454. package/render/shadowTypes.js +9 -0
  455. package/{core → render}/shadows.js +57 -11
  456. package/render/slots.js +7 -7
  457. package/render/terms.js +22 -24
  458. package/render/uniformDecl.js +1 -1
  459. package/types/core.d.ts +200 -153
  460. package/types/gameplay.d.ts +163 -91
  461. package/types/generated/core/{Audio.d.ts → audio/Audio.d.ts} +22 -7
  462. package/types/generated/core/{audioVoice.d.ts → audio/audioVoice.d.ts} +2 -2
  463. package/types/generated/core/{Events.d.ts → base/Events.d.ts} +4 -4
  464. package/types/generated/core/{I18n.d.ts → base/I18n.d.ts} +7 -15
  465. package/types/generated/core/{Random.d.ts → base/Random.d.ts} +4 -3
  466. package/types/generated/core/{page.d.ts → engine/page.d.ts} +8 -9
  467. package/types/generated/core/{Input.d.ts → input/Input.d.ts} +12 -8
  468. package/types/generated/core/{Budget.d.ts → inspect/Budget.d.ts} +36 -26
  469. package/types/generated/core/{DebugOverrides.d.ts → inspect/DebugOverrides.d.ts} +2 -2
  470. package/types/generated/core/{Ops.d.ts → ops/Ops.d.ts} +4 -3
  471. package/types/generated/core/{control.d.ts → ops/control.d.ts} +4 -10
  472. package/types/generated/core/{Saves.d.ts → saves/Saves.d.ts} +37 -13
  473. package/types/generated/core/{session.d.ts → saves/session.d.ts} +2 -1
  474. package/types/generated/core/{Rays.d.ts → scene/Rays.d.ts} +8 -27
  475. package/types/generated/core/{Views.d.ts → scene/Views.d.ts} +1 -1
  476. package/types/generated/core/{Batching.d.ts → stream/Batching.d.ts} +7 -7
  477. package/types/generated/core/{Stream.d.ts → stream/Stream.d.ts} +18 -16
  478. package/types/generated/core/{Commits.d.ts → threads/Commits.d.ts} +1 -1
  479. package/types/generated/core/{Jobs.d.ts → threads/Jobs.d.ts} +6 -6
  480. package/types/generated/core/{Threads.d.ts → threads/Threads.d.ts} +4 -4
  481. package/types/generated/modules/calendar/Calendar.d.ts +10 -11
  482. package/types/generated/modules/debug/DebugPanel.d.ts +1 -9
  483. package/types/generated/modules/grid/Layer.d.ts +1 -1
  484. package/types/generated/modules/grid/index.d.ts +3 -3
  485. package/types/generated/modules/music/Music.d.ts +7 -5
  486. package/types/generated/modules/particles/Particles.d.ts +1 -1
  487. package/types/generated/modules/sky/Sky.d.ts +21 -10
  488. package/types/generated/modules/swarm/Swarm.d.ts +80 -0
  489. package/types/generated/modules/timeline/Timeline.d.ts +2 -1
  490. package/types/generated/modules/water/Water.d.ts +1 -0
  491. package/types/generated/modules/weather/Weather.d.ts +16 -12
  492. package/types/generated/modules/weather/weatherDraw.d.ts +3 -3
  493. package/types/generated/modules/world/World.d.ts +5 -3
  494. package/types/generated/render/PostFX.d.ts +17 -11
  495. package/types/generated/render/materials.d.ts +1 -0
  496. package/types/generated/render/occlusion.d.ts +9 -0
  497. package/types/generated/render/patches.d.ts +1 -0
  498. package/types/generated/ui/Focus.d.ts +1 -0
  499. package/types/generated/ui/UI.d.ts +1 -0
  500. package/types/nav.d.ts +11 -11
  501. package/types/physics.d.ts +99 -70
  502. package/types/tsl.d.ts +37 -4
  503. package/types/world.d.ts +38 -71
  504. package/ui/Focus.js +10 -9
  505. package/ui/Parts.js +4 -3
  506. package/ui/UI.js +7 -7
  507. package/ui/defaults.js +3 -2
  508. package/ui/theme.js +5 -3
  509. package/core/assetEntry.js +0 -109
  510. package/core/holds.js +0 -134
  511. package/core/shadowTypes.js +0 -16
  512. package/core/util.js +0 -17
  513. package/docs/MIGRATING-0.3.md +0 -248
  514. package/docs/MIGRATING-0.4.md +0 -320
  515. package/docs/MIGRATING-0.5.md +0 -68
  516. package/docs/MIGRATING-0.6.md +0 -126
  517. package/docs/PERF.md +0 -245
  518. package/docs/recipes/36-sky-ground-plants.md +0 -158
  519. package/docs/recipes/37-terrain-hooks.md +0 -115
  520. package/docs/recipes/38-environment-hooks.md +0 -67
  521. package/docs/recipes/42-terrain-shape.md +0 -64
  522. package/docs/recipes/47-island-snow-backdrop.md +0 -43
  523. package/docs/recipes/54-three-idioms.md +0 -88
  524. package/examples/plain-picture/FINDINGS.md +0 -53
  525. /package/core/{Vars.js → base/Vars.js} +0 -0
  526. /package/core/{mulberry32.js → base/mulberry32.js} +0 -0
  527. /package/core/{inputDefaults.js → input/inputDefaults.js} +0 -0
  528. /package/core/{textureBytes.js → inspect/textureBytes.js} +0 -0
  529. /package/core/{actor.js → ops/actor.js} +0 -0
  530. /package/core/{opsFail.js → ops/opsFail.js} +0 -0
  531. /package/core/{opsHistory.js → ops/opsHistory.js} +0 -0
  532. /package/core/{saveIds.js → saves/saveIds.js} +0 -0
  533. /package/core/{screenToGround.js → scene/screenToGround.js} +0 -0
  534. /package/core/{tier.js → scene/tier.js} +0 -0
  535. /package/core/{cells.js → stream/cells.js} +0 -0
  536. /package/core/{streamCells.js → stream/streamCells.js} +0 -0
  537. /package/core/{pureWorker.js → threads/pureWorker.js} +0 -0
  538. /package/examples/{sheepisland → perf-island}/content/index.js +0 -0
  539. /package/examples/{sheepisland → perf-island}/content/props.js +0 -0
  540. /package/examples/{sheepisland → perf-island}/content/templates.js +0 -0
  541. /package/examples/{sheepisland → perf-island}/content/vegetation.js +0 -0
  542. /package/examples/{sheepisland → perf-island}/picture/lights.js +0 -0
  543. /package/examples/{sheepisland → perf-island}/picture/materials.js +0 -0
  544. /package/examples/{sheepisland → perf-island}/picture/sky.js +0 -0
@@ -8,9 +8,9 @@ The engine's three outward concepts are **Scene** (what stands in the world, whe
8
8
  sounds, fonts: `05-props-bake.md` §3b) and **Picture** (how it looks: `09-materials-lights-models.md`). Content packs belong to Scene: they are the data form of it.
9
9
  Patches, ops and world records are the scene's inner machinery; read them when you need them (`23-ops.md`, `18-world.md`).
10
10
 
11
- Every root you pass to `engine.add` is registered: an id, where it was added (`main.js:50`, dev builds only), who manages it and how much a save
11
+ Every root added with `engine.add` or `engine.scene.add` is registered: an id, where it was added (`main.js:50`, dev builds only), who manages it and how much a save
12
12
  keeps. World records (template instances, props, pickups, NPCs, level nodes) share the same id space and the same query, so one question works for a
13
- plain three.js mesh and for a template instance alike. `engine.scene.add` adds without registering: the plain three.js way in, still legal.
13
+ plain three.js mesh and for a template instance alike. Only `engine.add(obj, { id })` gives it a stable id.
14
14
 
15
15
  ## 1. Two doors into one scene
16
16
  A scene object comes in either of two ways, and both end up in the same scene:
@@ -45,7 +45,7 @@ export default [{ id: 'windmill-hill', template: 'windmill', x: 20, z: -10 }];
45
45
  // main.js: a code-built model registered by name
46
46
  engine.assets.manifest({ models: { windmill: buildWindmill } });
47
47
  ```
48
- Route B gives the object an id, a source line (`engine.world.origin('windmill-hill')`), a save record and a place a future editor can write back to.
48
+ Route B gives the object an id, a source line (`engine.world.origin('windmill-hill')`), a save record and a place the editor (F2, recipe 30 section 7) writes back to.
49
49
  Route A gives you less ceremony and full three.js control.
50
50
 
51
51
  ## 2. What goes in content, what stays three.js
@@ -56,8 +56,7 @@ Route A gives you less ceremony and full three.js control.
56
56
  | Many copies of one definition | A one-off with custom geometry or material logic |
57
57
  | Has a state worth reading with `world.get(id)` | Pure presentation |
58
58
 
59
- The rule check (R33) is deliberately narrow. It flags only the calls that would run again after a load and so duplicate saved data: `world.add`,
60
- `engine.content.spawn`, `pickups.spawn` and `npcs.add` at startup. `engine.add`, `collision.*`, `rigid.*`, `interactions.add` and the
59
+ The rule check R33 flags only calls that would run again after a load and so duplicate saved data (recipe 30 section 6). `engine.add`, `collision.*`, `rigid.*`, `interactions.add` and the
61
60
  like are fine at startup.
62
61
 
63
62
  ## 3. Which state a save keeps
@@ -123,16 +122,14 @@ write a template, add a placement with an id. Delete the `engine.add` line. Noth
123
122
  op are listed by `saves.save()` and in `describe().scene.unsaved`.
124
123
  - `engine.add` of a world model throws (`thing-owned`): world adds, places and tears down its models.
125
124
  - Code that moves a `positionFrom: 'record'` world model outside its present `update` is warned once and listed in `describe().scene.conflicts`.
126
- - Rule R33 (`npm run check`) flags the static half: startup calls that create things saved whole, and code writing `world.view()` models.
127
- - Cost: a release build pays two table writes per `engine.add` (about 2 µs per add in `tests/specs/things.mjs`); dev builds also take a short stack
128
- for `at`, at most 2000 per session (`describe().scene.atCapped` after that).
125
+ - Dev builds take a short stack for `at`, at most 2000 per session (`describe().scene.atCapped` after that).
129
126
 
130
127
  ## 7. Why is this mesh this colour? (material provenance)
131
128
 
132
129
  `engine.inspect.scene()` / `threedog scene` list, for each mesh, one `material:` line and the renderer line (tone mapping, exposure; the same text as `describe().render`). Diagnostics only: nothing about the picture changes.
133
130
 
134
131
  ```
135
- renderer: ACES (game) exposure 1.1 (game) shadows soft (engine)
132
+ renderer: aces (game) exposure 1.1 shadows pcf
136
133
  ✓ roof [Mesh] (4, 3, 0) r=2 · MeshLambertMaterial/roof
137
134
  material: roof from game · MeshLambertMaterial #3b5168 · map x color #3b5168 (vertexColors off) · map (base colour)
138
135
  note: map already carries colour and color #3b5168 is not white: the result is map x color (fine when it is a tint)
@@ -152,7 +149,7 @@ Plain three.js is always allowed (`engine.scene`, `engine.renderer`, `engine.cam
152
149
  | your own `EffectComposer` / render call to the canvas | the engine yields the frame (`[own-frame]`), so `engine.post` effects, adaptive resolution, photo mode, `describe().render.post`; keep them with `engine.post.add(pass)` instead |
153
150
  | `renderer.toneMappingExposure = x`, `scene.fog = ...` | the debug layer's restore (`engine.debug.override` gives the old value back) |
154
151
 
155
- The engine adds nothing to your materials. The cloud shadow is a signal (`engine.uniforms.uCloudShadow`, `uCloudScale`, `uCloudSpeed`) and a function: `applyEffects(mat)` gives a lit three.js material the engine's term, so it darkens under the drifting clouds like the engine's ground, on WebGL and WebGPU; your own shader calls `threedogCloudShadow(worldPos)` (recipe 43). Clouds dim the sun only (directional lights, every cascade included): a `PointLight` street lamp or a torch is never shaded by them. At 0 a uniform branch skips the cloud noise, and one program per material type stays. Snow and season cover work the same way: `applyEffects(mat, { season: 'ground' })` (`'foliage'` for leaves).
152
+ The engine adds nothing to your materials. The cloud shadow is a signal (`engine.uniforms.uCloudShadow`, `uCloudShadowScale`, `uCloudShadowSpeed`) and a function: `applyEffects(mat)` gives a lit three.js material the engine's term, so it darkens under the drifting clouds like the engine's ground, on WebGL and WebGPU; your own shader calls `threedogCloudShadow(worldPos)` (recipe 43). Clouds dim the sun only (directional lights, every cascade included): a `PointLight` street lamp or a torch is never shaded by them. At 0 a uniform branch skips the cloud noise, and one program per material type stays. Snow and season cover work the same way: `applyEffects(mat, { season: 'ground' })` (`'foliage'` for leaves).
156
153
  Keep the integrations by going through the engine: `applyEffects(mat, { wind, season, detail, lighting, patches })` on your material, `engine.post.add(pass | { name, fuse: { glsl }, slot })` for a screen effect.
157
154
 
158
155
  ## 9. One id, everything: explain() and the three kinds of change
@@ -187,4 +184,3 @@ file no longer has the value you saw (someone else changed it). `content add pro
187
184
  `--file content/props/town.js` picks the part of a folder kind) and `content remove <id>` deletes one. Fields are the content file's names
188
185
  (`x`, `z`, `y`, `rot`, `scale`, `template`, `place`, `model`), not the record's `position`. It refuses, and says why with the line, for an entry built
189
186
  by code (loop, spread, computed value), an id matching several entries (`--in props`), or an add / remove that would shift entries whose id is their position. A placed entry wins over a template or npc of the same name.
190
- It finds the file through `.threedog/content-index.json` (written by the content check; rebuilt when an id is not where it says), so a write reads only that one file however many there are.
@@ -11,11 +11,8 @@ const mat = new THREE.ShaderMaterial({ uniforms: { ...engine.uniforms }, vertexS
11
11
  applyEffects(mat2, { patches: [{ name: 'tint', slot: 'color', glsl: 'diffuseColor.rgb *= 0.9;' }] }); // 2. patch a slot
12
12
  ```
13
13
 
14
- **1. Your own material or shader** works exactly as in three.js (`new THREE.MeshStandardMaterial`, `ShaderMaterial`, `onBeforeCompile`).
15
- **2. Patch a slot**: a named piece of GLSL at a named place (`color`, `normal`, `lighting`, `output`...) on any material,
16
- built-in or yours. **3. Engine GLSL functions** (`#include <threedog_wind>`, `<threedog_detail>`, `<threedog_lighting>`...) are tools you call inside 1 and 2:
17
- a noise, a wrapped diffuse term, a fresnel edge. None of them is a picture. A picture is what your game's `picture/` code combines: its colors,
18
- textures, light model and post. The default surface is flat (`lambert` plus vertex colors) until you write one.
14
+ A patch is a named piece of GLSL at a named place on any material, built-in or yours. The engine GLSL functions are tools you call inside your shader or a patch; none of them is a picture. A picture is what your
15
+ game's `picture/` code combines.
19
16
 
20
17
  ## Your own material
21
18
  Any `new THREE.*Material` is fine (THREE.md), and the engine patches nothing of yours: call `applyEffects(material, { wind, clouds, season, detail, patches })` for the
@@ -35,14 +32,14 @@ Write a **named patch** that names a **slot** (where the code goes) and add it w
35
32
  The full form, with every field:
36
33
  ```js fragment
37
34
  { name: 'fuzz', // one name per effect; the same name again replaces it
38
- slot: 'color', // where the code goes (table below)
35
+ slot: 'color',
39
36
  glsl: 'diffuseColor.rgb *= 1.0 - uFuzz * 0.5;',
40
37
  pars: 'uniform float uFuzz;', // declarations at the top of every stage the patch touches (or { vertex, fragment })
41
- cacheKey: 'v1', // changes whenever the GLSL text changes, never for uniform values
38
+ cacheKey: 'v1',
42
39
  uniforms: { uFuzz: { value: 0.3 } }, // numbers live here, so materials with different values share a program
43
- needs: { patches: ['world'], data: ['vertexColors'] }, // 'world' gives `varying vec3 vThreedogWorld`; data = program inputs you read
44
- conflicts: ['wind'], // patches it cannot share a material with (checked when added)
45
- shadow: false } // true = the patch MOVES vertices, so its vertex code also runs in the shadow passes (wind does)
40
+ needs: { patches: ['world'], data: ['vertexColors'] },
41
+ conflicts: ['wind'],
42
+ shadow: false }
46
43
  ```
47
44
  Slots (the one table is `render/slots.js`; the engine's own wind, clouds, season, detail, lighting and cascades use the same names, and on WebGPU
48
45
  the same names map to node properties through a patch's `tsl` field, recipe 56). An unknown slot throws with this list.
@@ -60,7 +57,7 @@ the same names map to node properties through a patch's `tsl` field, recipe 56).
60
57
  | `emissive` | after `emissivemap_fragment` | `emissiveNode` | totalEmissiveRadiance (vec3, write), diffuseColor | emitted light after the emissive map |
61
58
  | `lightsPars` | after `shadowmap_pars_fragment` | WebGL only | declarations; three's light structs, getShadow() and directionalLights[] are already declared | functions that call three's light / shadow helpers (pars goes before them); WebGPU: TSL has no declaration area: the light's direction and shadow come to the lighting slot's direct(light, ctx) hook |
62
59
  | `lightingModel` | replace `lights_fragment_begin` | WebGL only | geometryPosition, geometryNormal, material, reflectedLight; your code replaces three's light loop | replace how lights are summed (one patch per material may take it); WebGPU: use the lighting slot's hooks { start, light, direct, after }, or a NodeMaterial with your own setupLightingModel |
63
- | `lighting` | after `lights_fragment_end` | inside `setupLightingModel` | reflectedLight.directDiffuse / directSpecular / indirectDiffuse (write), normal, diffuseColor, vViewPosition | after lights are summed: cloud shadows, wrap, shadow tint, rim; WebGPU: hooks { start, light, direct, after }: light(light, ctx) returns a new color for one light before three lights the surface with it (what GLSL does by wrapping three's light info in lightsPars), direct(light, ctx) sees each light in three's loop and adds to its own sums, which join the summed light at the patch's place in the list, then its after() runs: the same order as WebGL |
60
+ | `lighting` | after `lights_fragment_end` | inside `setupLightingModel` | reflectedLight.directDiffuse / directSpecular / indirectDiffuse (write), normal, diffuseColor, vViewPosition | after lights are summed: wrap, shadow tint, rim; WebGPU: hooks { start, light, direct, after }: light(light, ctx) returns a new color for one light before three lights the surface with it (what GLSL does by wrapping three's light info in lightsPars), direct(light, ctx) sees each light in three's loop and adds to its own sums, which join the summed light at the patch's place in the list, then its after() runs: the same order as WebGL |
64
61
  | `output` | before `opaque_fragment` | inside `setupOutput` | outgoingLight (vec3, write), diffuseColor.a | the final lit color, before fog and tone mapping |
65
62
  | `fogPars` | after `fog_pars_fragment` | WebGL only | declarations; fogColor and fogNear / fogFar or fogDensity are already declared (inside #ifdef USE_FOG) | fog functions that read three's fog uniforms (wrap them in #ifdef USE_FOG); WebGPU: fog on WebGPU is scene.fogNode, set by the game |
66
63
  | `fog` | replace `fog_fragment` | WebGL only | gl_FragColor (write, tone mapped already), fogColor, vFogDepth, fogNear / fogFar or fogDensity (inside #ifdef USE_FOG) | replace how fog mixes into the final color (one patch per material may take it); WebGPU: fog on WebGPU is scene.fogNode, set by the game |
@@ -76,9 +73,7 @@ cacheKey so the old program is not reused) without reloading the page, and a com
76
73
  `shader error in file shaders/fuzz.glsl:4, patch 'fuzz' (slot color)`. Text copied into your own `onBeforeCompile` closure cannot be found: the page
77
74
  reloads instead. Builds get the plain text. Plain Vite equivalent: `import.meta.hot.accept('./fuzz.glsl?raw', m => { mat.fragmentShader = m.default; mat.needsUpdate = true; })`.
78
75
  `MeshBasicMaterial` has no lights, so no `normal` / `lighting` slots; `lambert` has no `roughness`. A slot the material lacks throws when the patch is added.
79
- `vThreedogWorld` is the world position (instancing and batching included); it comes from the engine's `world` patch. A patch whose GLSL reads
80
- `vThreedogWorld` gets `world` added in front of it even without `needs` (a one-time dev note asks for the `needs: { patches: ['world'] }` line, which
81
- says the same thing in the patch itself). Meshes with several patches stack them in one program per distinct combination.
76
+ `vThreedogWorld` is the world position (instancing and batching included); it comes from the engine's `world` patch, added in front of a patch whose GLSL reads it even without `needs` (a one-time dev note asks for the line). Meshes with several patches stack them in one program per distinct combination.
82
77
 
83
78
  The `normal` slot also fixes a three.js rule that surprises card and grass art: with `side: THREE.DoubleSide` three flips the normal of every back
84
79
  face (`faceDirection`), so a blade or card whose normals all point up shades dark from the side you see its back. Keep the authored normal by
@@ -90,9 +85,9 @@ Patches on one material must agree. Each patch declares what it relies on, and a
90
85
  | Field | Meaning | Checked |
91
86
  |---|---|---|
92
87
  | `needs.data` | Program inputs the code reads: `'instancing'`, `'instancingColor'`, `'vertexColors'`, `'skinning'`, `'batching'` | when the program compiles (console.error once per material) |
93
- | `needs.patches` | Patches it builds on, e.g. `['world']` for the `vThreedogWorld` varying (world position; the engine adds `world` itself when a patch needs it or its code reads `vThreedogWorld`) | when added |
88
+ | `needs.patches` | Patches it builds on, e.g. `['world']` for the `vThreedogWorld` varying | when added |
94
89
  | `conflicts` | Patch names it cannot share a material with | when added, both ways |
95
- | `uniforms` | A uniform name belongs to one patch; share `engine.uniforms` (`uTime`, `uWind`, `uCloud`...) instead of making your own `uTime` | when added |
90
+ | `uniforms` | A uniform name belongs to one patch; share `engine.uniforms` (`uTime`, `uWind`, `uWeatherCloud`...) instead of making your own `uTime` | when added |
96
91
  | `shadow` | Render steps: without it the color pass only; `true` also runs its vertex code in the depth (sun / spot shadows) and distance (point-light shadows) passes | - |
97
92
  | `cacheKey` | Changes whenever the GLSL text changes (defines, branches), never for uniform values; the program key joins every patch's `name=cacheKey` in order | same name + cacheKey with different code throws. On WebGPU the program also follows the identity of each `tsl` function, so make it once and reuse it: one written fresh per material costs a program per material |
98
93
  ```js
@@ -100,21 +95,9 @@ applyEffects(mat, { wind: 0.03, season: 'foliage', patches: [{ name: 'ripple', n
100
95
  slot: 'color', glsl: 'diffuseColor.rgb *= 0.9 + 0.1 * sin(vThreedogWorld.x);' }] }); // throws: 'wind' and 'ripple' cannot be combined
101
96
  ```
102
97
 
103
- **Exit: raw three.js anchors** (WebGL only, plain three.js knowledge). Where no slot fits, `glsl: { vertex|fragment: { <chunk>: code } }` puts
104
- code after `#include <chunk>` (a value may be `{ before, after, replace }`; the key `pars` is declarations at the top of that stage). A missing chunk
105
- throws when the patch is added, not as a silent no-op. Chunk names in three **0.187** (read from `three/src/renderers/shaders`), in main-body order:
106
-
107
- | stage | chunk | what is in scope after it |
108
- |---|---|---|
109
- | vertex | `begin_vertex` | `transformed` (object space; wind moves it here) |
110
- | vertex | `project_vertex` | `mvPosition`, `gl_Position` |
111
- | vertex | `worldpos_vertex` | `worldPosition` (only when shadows, env maps or transmission need it) |
112
- | fragment | `map_fragment` `color_fragment` | `diffuseColor` (rgb after the map, then the vertex colors) |
113
- | fragment | `roughnessmap_fragment` `metalnessmap_fragment` | `roughnessFactor`, `metalnessFactor` (standard / physical) |
114
- | fragment | `normal_fragment_begin` `normal_fragment_maps` | `normal` (view space), `faceDirection`; `vViewPosition` = minus the view-space position |
115
- | fragment | `emissivemap_fragment` | `totalEmissiveRadiance` |
116
- | fragment | `lights_fragment_begin` ... `lights_fragment_end` | `reflectedLight.directDiffuse / indirectDiffuse / directSpecular / indirectSpecular`; after `_end` they are final |
117
- | fragment | `opaque_fragment` | `outgoingLight` and `gl_FragColor`; (called `output_fragment` before r154) |
98
+ **Exit: raw three.js anchors** (WebGL only). Where no slot fits, `glsl: { vertex|fragment: { <chunk>: code } }` puts code after `#include <chunk>` (a value may be `{ before, after, replace }`; the key `pars` is
99
+ declarations at the top of that stage). A missing chunk throws when the patch is added, not as a silent no-op. Chunk names are three's, in `three/src/renderers/shaders`; the slot table above gives the common ones and
100
+ what is in scope. `worldPosition` exists after `worldpos_vertex` only when shadows, env maps or transmission need it.
118
101
 
119
102
  ### Engine effects in your own ShaderMaterial
120
103
  ```js
@@ -125,12 +108,10 @@ The engine never patches a ShaderMaterial of yours; call its effects as function
125
108
 
126
109
  | include | function |
127
110
  |---|---|
128
- | `#include <threedog_uniforms>` | declares every engine uniform: `uTime`, `uWind`, `uWindFreq`, `uWindAmp`, `uCloud`, `uCloudShadow`, `uCloudScale`, `uCloudSpeed`, `uFoliageTint`, `uGroundTint`, `uFoliageMix`, `uGroundMix`, `uCover` (snow / ash cover amount alone: x foliage, y ground), `uSunDir`, `uSunColor`, `uDaylight`, `uTimeOfDay` (include once, in each stage that uses them) |
111
+ | `#include <threedog_uniforms>` | declares every name on `engine.uniforms`, for example `uTime`, `uWind`, `uWeatherCloud`, `uRain`, `uFoliageTint`, `uGroundCover` (snow / ash cover amount alone: x foliage, y ground), `uSunDir`, `uDaylight`, `uNight`, `uSkyHorizon` (include once, in each stage that uses them; do not declare one of these yourself as well) |
129
112
  | `#include <threedog_wind>` | `vec3 threedogWind(vec3 position, vec3 origin, float amplitude)`: object-space sway offset, higher vertices sway more; `origin` is the object's world origin (its phase) |
130
113
  | `#include <threedog_cloudShadow>` | `float threedogCloudShadow(vec3 worldPos)`: light factor 0-1 under the drifting clouds |
131
114
  | `#include <threedog_season>` | `vec3 threedogSeason(vec3 color, int kind)`: season tint, kind 0 foliage, 1 ground |
132
-
133
- To change the cloud shadow or the season tint on the engine's own materials (not only in your shader), replace the term: `engine.terms.cloudShadow({ glsl, tsl })` / `engine.terms.season(kind => ({ glsl, tsl }))`, or `false` to drop it (recipe 52).
134
115
  | `#include <threedog_detail>` | `vec4 threedogDetailNoise(vec2 p, int kind, float octaves)`: the detail noises, kind 0 grain, 1 paper, 2 stroke, 3 dabs, returns (gray rgb, height 0-1); `vec3 threedogDetailBump(vec3 surfPos, vec3 surfNorm, float h, float k, float faceDir)`: screen-space bump. No uniforms. `p` is yours: world xz for a pattern fixed to the ground (it shrinks with distance), `gl_FragCoord.xy / 3.0` for one the same size all over the screen (3-pixel cells; device pixels, so it follows the pixel ratio) |
135
116
  | `#include <threedog_lighting>` | `vec3 threedogWrapDiffuse(float nl, float wrap, vec3 lightColor, float shadow, vec3 albedo)` (light to add), `vec3 threedogShadowTint(vec3 ambient, vec3 tint, float strength, float soft, float nl, float shadow)` (the new ambient), `vec3 threedogRim(vec3 normal, vec3 viewDir, vec3 color, float strength, float power, float shadowSide, float nl)` (light to add); nl = dot(normal, sunDir), shadow 1 lit / 0 shadowed. No uniforms |
136
117
 
@@ -139,18 +120,16 @@ To change the cloud shadow or the season tint on the engine's own materials (not
139
120
  #include <threedog_wind>
140
121
  void main() { vec3 p = position + threedogWind(position, (modelMatrix * vec4(0, 0, 0, 1)).xyz, 0.05); gl_Position = projectionMatrix * modelViewMatrix * vec4(p, 1.0); }
141
122
  ```
142
- The `detail` effect below calls these same functions, so a value tuned on a material reads the same in your shader. To give a material another cloud shadow or
143
- season tint, leave the engine's out (`clouds: false`, no `season`) and write your own patch with these functions. Combining tools into a picture is per game: write it in its `picture/` code.
144
- The pixel and program-count guarantees are in `tests/specs/surface.mjs`.
123
+ The `detail` effect below calls these same functions, so a value tuned on a material reads the same in your shader. To give one material another cloud shadow or
124
+ season tint, leave the engine's out (`clouds: false`, no `season`) and write your own patch with these functions; on every engine material at once: `engine.shaderTerms` (recipe 52).
145
125
 
146
126
  ## Ready-made effects: `detail`, light shaping, seasons
147
- Each one puts a function above on a built-in material, with its numbers as uniforms. They are building blocks with fixed shapes (four noise kinds, one tint model),
148
- not finished pictures: combine them with your own colors, textures and patches, and when one does not give what you want, write the shader yourself and call the same function there.
127
+ Each one puts a function above on a built-in material, with its numbers as uniforms. They are building blocks with fixed shapes, not finished pictures: when one does not give what you want, write the shader yourself and call the same function there.
149
128
 
150
129
  **`detail`**: `applyEffects(material, { detail: { noise: 'paper', strength: 0.4 } })`. A noise pattern, a texture or your own GLSL laid over the surface in world space (needs no UVs). Every number is a uniform, so
151
- 100 materials with different `scale` / `strength` / `direction` compile **one** program on WebGL (tested: 8 variants, +0 programs).
130
+ materials with different `scale` / `strength` / `direction` compile **one** program on WebGL.
152
131
  WebGPURenderer keys its program by the patch's uniform objects instead: materials with the same `detail` options share one
153
- patch object, so they share one program there too (tested: 8 materials, 1 build); different numbers build one each. Sharing
132
+ patch object, so they share one program there too; different numbers build one each. Sharing
154
133
  means one set of values: a uniform changed on one of them changes all that share those options.
155
134
 
156
135
  | key | meaning |
@@ -1,4 +1,6 @@
1
- # Water and rain/snow: depth color, foam, reflections, particles, lightning
1
+ # Water and rain/snow: depth color, foam, reflections, particles, snow cover, lightning
2
+
3
+ **In this recipe**: water look and material, foam and depth, a reflecting mirror (WebGL addon and WebGPU node), weather particles, snow that settles, lightning.
2
4
 
3
5
  ```js
4
6
  engine.use(water({ level: 2 }));
@@ -10,20 +12,14 @@ engine.water.normalMap('water-n', { scale: 0.4, repeat: 0.05, speed: [0.01, 0.00
10
12
  ```
11
13
 
12
14
  For an AI that wants water or weather particles to look a way the defaults do not. `water()` takes the structure only (`sea`, `level`, `flat`, `size`,
13
- `segments`); the water is neutral gray until your `picture/` code writes it. Lights, fog and the sky are in `38-environment-hooks.md`. Water that uses
14
- none of `depthColor`, `foam`, `surface` keeps the plain shader and pays nothing (`tests/specs/appearance-water.mjs`). Four exits: a material write, a
15
+ `segments`); the water is neutral gray until your `picture/` code writes it. Lights and fog are in `34-environment-lighting.md`, the sky in `08-weather-sky.md`. Water that uses
16
+ none of `depthColor`, `foam`, `surface` keeps the plain shader and pays nothing. Four exits: a material write, a
15
17
  uniform, a GLSL function you replace, or your own material.
16
18
 
17
19
  ## Water: material and uniforms
18
20
  `engine.water.material` is shared by every water mesh and is a `MeshLambertMaterial` by default: color, opacity and anything else three.js has on it are
19
21
  plain writes. Lambert has no highlight; for `shininess` / `specular` set your own Phong material (below). `engine.water.uniforms` are `{ value }` objects:
20
-
21
- | Uniform | Default | What it does |
22
- |---|---|---|
23
- | `uWave` | 1 | multiplies the wave function (0 = flat) |
24
- | `uWaveScale` | 1 | world position scale fed to the wave function |
25
- | `uWaveSpeed` | 1 | time scale fed to the wave function |
26
- | `uFresnel` | 0.5 | grazing angles blend toward the fog color, or the sky's horizon with no fog (0 = off) |
22
+ `uWave` multiplies the wave function (0 = flat), `uWaveScale` scales the world position fed to it, `uWaveSpeed` the time; all three default to 1. `uFresnel` (default 0.5, 0 = off) blends grazing angles toward the fog color, or the sky's horizon with no fog.
27
23
 
28
24
  ## Water: depth color and crest foam
29
25
  ```js fragment
@@ -31,22 +27,17 @@ engine.water.shader({ depthColor: { shallow: 0x55ddcc, deep: 0x082040 } });
31
27
  engine.water.shader({ depthColor: { shallow: 0x7fe0d0, deep: 0x0a3050, by: 'wave', curve: 1.5 } }); // shallow on the crests, deep in the troughs
32
28
  engine.water.shader({ foam: { color: 0xffffff, start: 0.55, end: 0.85, opacity: 1 } }); // white on the wave tops; foam: true = these defaults
33
29
  ```
34
- - `shader()` decides which GLSL exists, so it patches the engine's material again (one recompile; it stays the same material). Keys left out keep what an
35
- earlier call gave, `null` takes one back out. It refreshes `engine.water.patches` and `engine.water.uniforms`; `uWave`, `uWaveScale`, `uWaveSpeed`,
36
- `uFresnel` stay the same objects.
30
+ - `shader()` decides which GLSL exists, so each call is one recompile: a scene change, not a frame. What it keeps and refreshes: `../api/water.md`.
37
31
  - `depthColor` replaces the material color (opacity stays). `'view'` is a fresnel-style blend by the view angle, `'wave'` blends by the wave height; neither
38
32
  needs a depth texture. `curve` bends the blend (above 1 keeps more of the shallow color). A color left out is a neutral gray (light for
39
33
  `shallow`, dark for `deep`): the engine picks no water color, so set both.
40
34
  - `foam` uses the same wave height as the vertices: `start` / `end` are fractions of the peak height (`waveMax` of the `wave` function, default 0.29) where
41
- foam begins and is full. Foam is opaque even on transparent water. All numbers are uniforms (`engine.water.uniforms.uFoamStart.value`; also
42
- `uFoamColor`, `uFoamEnd`, `uFoamOpacity`, `uWaterShallow`, `uWaterDeep`, `uWaterCurve`).
43
- - **Shore foam is not built in.** An accurate foam line where water meets land needs the distance to the ground under each pixel: a depth
44
- pre-pass (a `DepthTexture` render target of the scene without water) or a terrain height lookup, both cost a full extra pass or a texture
45
- read per pixel. Do it yourself with your own material: render the scene's depth to a target before the water draws
46
- (`material.onBeforeRender`), pass it as a uniform, and in your fragment compare the scene depth with this fragment's depth, both turned into meters with three's
47
- `perspectiveDepthToViewZ` (`#include <packing>`); foam
48
- where the difference is under about a meter. A cheap fake: `engine.addWaterBody` lakes have known radii, so a surface function can foam the
35
+ foam begins and is full. Foam is opaque even on transparent water. All numbers are uniforms (`engine.water.uniforms.uFoamStart.value`).
36
+ - **Shore foam is not built in.** A foam line where water meets land needs the distance to the ground under each pixel, so an extra depth pass or a height lookup per pixel.
37
+ Do it with your own material: render the scene's depth to a target before the water draws (`material.onBeforeRender`), pass it as a uniform, and compare it with this fragment's depth, both in meters through three's
38
+ `perspectiveDepthToViewZ` (`#include <packing>`). A cheap fake: `engine.addWaterBody` lakes have known radii, so a surface function can foam the
49
39
  ring `length(worldPos.xz - center) > radius - 1.0` (pass the center and radius as `uniforms`).
40
+ - A look that changes with real depth (tint, opacity) reads depth as the water level minus `engine.ground.height(x, z)`: once per vertex of a subdivided plane, or baked into an attribute a material reads.
50
41
 
51
42
  ## Water: replace the wave or the surface function (GLSL)
52
43
  ```js
@@ -71,9 +62,7 @@ engine.water.shader({
71
62
  - Your GLSL has no WebGPU form (the GLSL route is for `--webgl` games): on `renderer: 'webgpu'` start() names the patch. The built-in wave, depth color and foam run on both renderers.
72
63
 
73
64
  ## Water: normal map
74
- `engine.water.normalMap(name, { scale, repeat, speed })` loads the texture by asset name onto the engine's material: `scale` is its strength, `repeat` tiles
75
- per meter (rewrites the meshes' UVs once), `speed` the scroll `[x, z]` per second. Keys left out keep their value; the same name again only changes those;
76
- `null` takes it off. It returns the texture, also readable as `engine.water.normalTexture`. A material of your own takes that texture itself
65
+ `engine.water.normalMap(name, { scale, repeat, speed })` goes on the engine's material only (keys in `../api/water.md`). A material of your own takes the texture itself
77
66
  (`myMaterial.normalMap = engine.water.normalTexture`); the tiling and the scroll run whatever material draws.
78
67
 
79
68
  ## Water: your own material (reflections, refraction)
@@ -86,7 +75,8 @@ Setting `engine.water.material` swaps it on every water mesh, now and later, and
86
75
  stay (pick from the list by `name` for only some: `waves`, `waterSurface`, `fresnel`; `waterSurface` needs `waves` before it). After a later `shader()` call
87
76
  patch your material again; the engine only rebuilds its own. Your material reads `engine.water.uniforms` like the built-in one, and the engine still
88
77
  builds the meshes, snaps the sea to the focus and scrolls the normal map. Reflections and refraction are not built in (each one renders the scene a
89
- second time). three's `Reflector` addon works through this exit; the engine water is pre-rotated geometry, so the mirror gets the mesh's
78
+ second time). **The `Reflector` addon below is WebGL only**: on the default WebGPU renderer its `ShaderMaterial` stops the page, so use the WebGPU route
79
+ further down. On WebGL, three's `Reflector` addon works through this exit; the engine water is pre-rotated geometry, so the mirror gets the mesh's
90
80
  matrix turned into the XY plane it expects:
91
81
 
92
82
  ```js
@@ -105,12 +95,26 @@ m.onBeforeRender = (renderer, scene, camera, geometry, mesh) => {
105
95
  };
106
96
  engine.water.material = m;
107
97
  ```
98
+ **WebGPU route** (default renderer): three's `reflector` node (`three/tsl`) draws the scene into a texture the material samples by screen position, so the mesh
99
+ geometry does not matter. The mirror is the XY plane of `target` (normal +Z): turn it flat, put it at the water level. Plain three.js, not run in this repo's tests.
100
+
101
+ ```js
102
+ import * as THREE from 'three/webgpu';
103
+ import { reflector, mix, color } from 'three/tsl';
104
+
105
+ const mirror = reflector({ resolutionScale: 0.5 }); // half-size render target
106
+ mirror.target.rotateX(-Math.PI / 2); // XY plane -> flat, normal up
107
+ mirror.target.position.y = 0; // the water level
108
+ engine.scene.add(mirror.target); // must be in the scene
109
+ const m = new THREE.MeshBasicNodeMaterial();
110
+ m.colorNode = mix(color(0x2a5a7a), mirror, 0.6); // 0 = water color, 1 = pure reflection
111
+ engine.water.material = m;
112
+ ```
108
113
  - Cost: the whole scene drawn once more per frame per water mesh at `textureWidth` x `textureHeight` (shadows are not re-rendered). Use 256-512
109
114
  on phones; on `engine.tier === 'low'` keep the default material instead.
110
115
  - One mirror plane: the material is shared, so water bodies at other levels need `water({ sea: false })` or their own mirror. The Reflector's
111
116
  shader has no waves or fog; for both, copy its `fragmentShader` into a `MeshPhongMaterial` patch that samples `tDiffuse` with `textureMatrix`.
112
117
  - three's `Refractor` addon (next to `Reflector`) reads `matrixWorld` as an XY plane too, so the same adapter applies (not covered by a test).
113
- - Tested: `tests/specs/appearance-water.mjs` renders this exact example and finds the reflected box in the mirror texture.
114
118
 
115
119
  ## Water: your own meshes (three's `Water` addon, your own geometry)
116
120
  The water module's material is yours to swap, its meshes are not. For water drawn entirely your way, leave `water()` out and draw it in plain
@@ -129,10 +133,7 @@ weather({ types,
129
133
  // snow: { texture: 'flake' }, // a sprite image replaces the shape
130
134
  });
131
135
  ```
132
- - `shape`: `'line'` (rain default; leans with the wind by `slant`, `length` meters) or a sprite: `'round'` (snow default), `'soft'`, `'square'`
133
- (`size` pixels at 3 m, capped at `maxSize`; drifts `sway` meters). `speed` is the fall in m/s.
134
- - `density: amount => share`: how many particles show at a weather amount 0..1 (rain `a => min(1, a * 1.2)`, snow `() => 1`); opacity still
135
- follows the amount. `count` per system (default `weather({ particles })`, halved on low-tier devices). Coverage is `weather({ area })`.
136
+ The keys and defaults are `WeatherParticles` in `types/world.d.ts`. `density` decides how many particles show at a weather amount; opacity follows the amount as well. Coverage is `weather({ area })`.
136
137
 
137
138
  At run time each system is in `engine.weather.particles`, and every number is a plain write, live on both renderers:
138
139
  ```js
@@ -157,9 +158,19 @@ void main(){
157
158
  }` } } });
158
159
  ```
159
160
  `glsl.vertex` / `glsl.fragment` are the whole shader text; copy `modules/weather/shaders.js` (`lineVert lineFrag pointVert pointFrag`) as the
160
- start. In scope: `uCam uSize uFallOff uOpacity uColor`; lines add `uWindDir uLen` and the attribute `aEnd` (0 head, 1 tail); sprites add
161
- `uPx uMaxPx uSway uSwayAmp uMap uUseMap`. Your `uniforms` are declared for you; declare the built-in ones you use, as the copies do. Keep
162
- the two `#include` lines so the color matches three's materials with and without post-processing. WebGPURenderer does not compile GLSL text: `engine.weather.define(field, { create })` with a node material there.
161
+ start (what is in scope: `glsl` in `WeatherParticles`; the attribute `aEnd` is 0 at the head, 1 at the tail). Your `uniforms` are declared for you; declare the built-in ones you use, as the copies do. Keep
162
+ the two `#include` lines so the color matches three's materials with and without post-processing. WebGPURenderer does not compile GLSL text: `engine.weather.defineField(field, { create })` with a node material there.
163
+
164
+ ## Snow that settles: `weather({ snow: { cover } })`
165
+ ```js
166
+ weather({ types, snow: { size: 18, cover: { foliage: 0xf2f4f6, ground: 0xeef0f2, strength: 0.8 } } });
167
+ ```
168
+ - While the weather's `snow` amount rises (any type with `snow > 0`), the foliage / ground season tint moves `strength` of the way toward
169
+ these colors, and melts back as it falls. It reuses the season patch uniforms (`uFoliageTint/Mix`, `uGroundTint/Mix`): vegetation, terrain
170
+ and any material with `applyEffects(m, { season: 'ground' })` follow; no extra shader pass. It needs `sky()`, which holds the point the cover goes through.
171
+ - Over a `mode: 'mix'` season the two blend into one mix; a `'multiply'` season gives way while snow lies (use `'mix'` for smooth fades).
172
+ - Leave a part out to keep its season color (`cover: { foliage: 0xffffff }` whitens trees only). Leave `cover` out and snow is particles only.
173
+ - Ash, frost or anything else: `engine.mod('ground.cover', { owner: 'ash', value: { ground: 0x555555, amount: 0.6 } })` (one entry per owner, the handle's `remove()` ends it).
163
174
 
164
175
  ## Lightning picture
165
176
  ```js
@@ -169,23 +180,17 @@ engine.events.on('weather:lightning', () => spawnBolt()); // when it st
169
180
  ```
170
181
 
171
182
  ## Particles of the game's own weather
172
- A field of the game's own weather table (`ash`, `petals`, `sparks`) gets particles with `engine.weather.define(field, spec)`; the amount is
183
+ A field of the game's own weather table (`ash`, `petals`, `sparks`) gets particles with `engine.weather.defineField(field, spec)`; the amount is
173
184
  `engine.weather.current[field]`, so they fade in and out with the weather. The spec takes the rain / snow keys above (default: drifting
174
185
  round sprites), or `create` for a system of your own:
175
186
 
176
187
  ```js
177
188
  engine.use(sky(), weather({ types: { clear: {}, ashfall: { ash: 1, grey: 0.5, sun: 0.6 } } }));
178
- engine.weather.define('ash', { shape: 'soft', color: 0x555555, size: 14, speed: 0.8, sway: 3 });
179
- engine.weather.define('rain', { create: (engine, { area, count }) => { // the same call replaces rain or snow
189
+ engine.weather.defineField('ash', { shape: 'soft', color: 0x555555, size: 14, speed: 0.8, sway: 3 });
190
+ engine.weather.defineField('rain', { create: (engine, { area, count }) => { // the same call replaces rain or snow
180
191
  const drops = new THREE.InstancedMesh(new THREE.SphereGeometry(0.03), new THREE.MeshBasicMaterial({ color: 0x99bbff }), count);
181
192
  return { object: drops, update(amount, { camera, time, wind }) { /* move the instances; shown while amount > 0.02 */ } };
182
193
  } });
183
194
  ```
184
- A field no type has throws with the table's fields. `define(field, null)` gives rain and snow back and removes any other field. Rain and snow are built
195
+ A field no type has throws with the table's fields. `defineField(field, null)` gives rain and snow back and removes any other field. Rain and snow are built
185
196
  only when a type rains or snows, so a desert pays nothing.
186
-
187
- The weather's effect on the picture stays on signals: `engine.uniforms.uWeatherLight`, `uWeatherFog`, `uRain`, `uSnow`, `uWind`, `uCloud`. The game's own light,
188
- fog and wet-road code reads them; the weather writes no light and no fog.
189
-
190
-
191
- A look that changes with depth (tint, opacity, foam) reads depth as the water level minus `engine.ground.height(x, z)`: once per vertex of a subdivided plane, or baked into an attribute a material reads.
@@ -1,4 +1,4 @@
1
- # Shadows: cascades, box, budget, the switch
1
+ # Shadows: cascades, box, budget, cache, the switch
2
2
 
3
3
  **In this recipe**: `engine.shadows` works on a light you hand it and leaves every other light alone: cascades, a box that follows, an update budget, the master
4
4
  switch. Filter, bias, softness and map size are plain three.js (last section).
@@ -24,7 +24,7 @@ The engine sun takes the same entry: `engine.shadows.cascades(engine.lights.sun,
24
24
 
25
25
  | field | default | meaning |
26
26
  |---|---|---|
27
- | `count` | 3 | 1 = one map that follows the camera (`maxFar` each way); 2-4 = cascades |
27
+ | `count` | 3 | 1 = one map (see the rules below); 2-4 = cascades |
28
28
  | `maxFar` | 120 | meters from the camera they cover; no sun shadow beyond |
29
29
  | `split` | `'practical'` | where borders fall: `'practical'` / `'uniform'` / `'logarithmic'` / `(count, near, far, out) => void` |
30
30
  | `lightMargin` | 100 | meters behind each cascade box toward the light still caught as casters |
@@ -48,7 +48,6 @@ follow the camera frustum and snap to whole shadow pixels themselves, so edges d
48
48
  `lights_fragment_begin` (it would clash with `csm`, reported when added).
49
49
  - three's `ShaderChunk` is never changed (the addon's constructor does that globally; the engine swaps the chunk per material instead).
50
50
  - Under `engine.shadows.budget` cascades move only on frames whose maps re-render (or when the camera jumps), so lights and maps agree.
51
- - `engine.shadows.mapSize(sun, n)` resizes every cascade's map, on both renderers.
52
51
 
53
52
  On WebGPURenderer (recipe 56) the same call is three's
54
53
  `CSMShadowNode` on `light.shadow.shadowNode`: one light, so no material is patched, nothing is listed in `describe().owned`,
@@ -57,32 +56,28 @@ lights). The cascade shadow cameras reach `lightMargin + 2 * maxFar` meters, as
57
56
 
58
57
  ## Costs and stalls
59
58
  - Each cascade draws the shadow casters once more (each culls to its own box; near ones see little). `engine.budget.byTag()` counts a
60
- caster once per shadow map (each cascade, on both renderers), an upper bound. In the styletest acceptance test (software renderer, relative only) 3 cascades
61
- went from 1 to 3 shadow passes and roughly doubled frame time; do not expect frame rate to improve.
59
+ caster once per shadow map (each cascade, on both renderers), an upper bound. Do not expect frame rate to improve.
62
60
  - Switching cascades on, off or changing any cascade field recompiles every lit material once (a stall), and each material keeps the
63
61
  old program until it is disposed (three would otherwise reuse a stale program whose uniforms no longer update). Do it on a settings
64
62
  screen, not during play.
65
63
  - Ladder for weaker devices: `count: 3` -> `2` -> `1` (one map); before dropping a cascade try a smaller `maxFar` or `mapSize`.
66
- Low-tier devices get 1 unless you ask: `{ count: 3, low: { count: 2 } }`.
67
64
 
68
65
  ## Rules of the entry
69
- `engine.shadows.cascades(light, options)` takes `count`, `maxFar`, `split`, `fade`, `lightMargin` (the table above) and `low: { count }`, with and without a
70
- sky, on both renderers (WebGPU needs `import 'threedog/tsl'`, or the call says so).
66
+ It works with and without a sky, on both renderers (WebGPU needs `import 'threedog/tsl'`, or the call says so).
71
67
  - The light must be a DirectionalLight, a direct child of `engine.scene`, with `castShadow = true`; anything else throws at the call.
72
- - Color, intensity, `visible`, `castShadow` and `light.shadow` values stay yours and are read every frame: `mapSize`, `bias`, `normalBias`,
73
- `radius` and `blurSamples` written on the light reach every cascade. The cascades only place the light. On WebGL, while they are on,
68
+ - Color, intensity, `visible`, `castShadow` and `light.shadow` values stay yours and are read every frame. The cascades only place the light. On WebGL, while they are on,
74
69
  `light.position` and `light.target` hold that placement: writes to `light.position` or a new `light.target` still set the direction, and
75
70
  `remove()` puts both back as you left them, with the shadow camera box. A `light.target` you assigned (your player, say) is never moved.
76
- - `engine.tier === 'low'` gets one map unless you pass `low: { count: 2 }`. That map follows the camera and covers `maxFar` each way, so it is blurrier than cascades but never stops at the
77
- origin; `handle.csm` is three's CSM object, null while there is one map. Like cascades it places the light, so `engine.shadows.box` on that light is refused.
71
+ - `engine.tier === 'low'` gets one map unless you pass `low: { count: 2 }`. On a game's light that map follows the camera and covers `maxFar` each way, so it is blurrier than cascades but never stops at the
72
+ origin; like cascades it places the light, so `engine.shadows.box` on that light is refused. On `engine.lights.sun` the one map stays the engine's own box around the focus, with its size. `handle.csm` is null while there is one map.
78
73
  - An engine draws one set of cascades: a second handle throws. A box on the same light throws too, except the engine's own box on `engine.lights.sun`, which waits
79
74
  until `remove()`.
80
- - Materials work as described above (the `csm` patch, `engine.shadows.include(mat)`, `describe().owned`). Never call `CSM.setupMaterial` yourself.
81
75
  - Another shadow-casting DirectionalLight next to cascades breaks the cascade order on WebGL (three sorts shadow casters first): one warning
82
76
  names it; keep extra lights shadowless.
83
77
 
84
78
  ## A box that follows
85
- One shadow map stays sharp when it covers little. `box` keeps that little around the action:
79
+ One shadow map stays sharp when it covers little: one texel covers `2 x extent / mapSize` meters (the engine sun's 30 m on a 2048 map is about 3 cm, twice that on the 1024 map of low tier). A box much bigger than
80
+ what the camera sees blurs the shadows of small things; a box smaller than it cuts shadows off. A depth bias scales with the texel, so re-check `bias` / `normalBias` after changing either. `box` keeps that little around the action:
86
81
 
87
82
  ```js
88
83
  const sun = new THREE.DirectionalLight('#fff', 3);
@@ -106,6 +101,34 @@ box.remove(); // position, target and the shadow camera come back
106
101
  - The engine's placeholder sun under `sky()` has a box already, following `engine.focus`, 30 m each way. `engine.shadows.box(engine.lights.sun, { follow: false, extent: 9 })` changes that one
107
102
  box and `remove()` restores it. Writing `engine.lights.sun.target.position` yourself takes the box's center over, with one warning; `{ follow: 'focus' }` gives it back.
108
103
 
104
+ ## Keep what does not move
105
+
106
+ ```js
107
+ const kept = engine.shadows.cache(sun, { static: mesh => mesh !== cars.mesh }); // a test on each mesh; or an Object3D, or a list of them
108
+ kept.redraw(); // a static object moved, hid or left
109
+ kept.set({ static: town }); // say again what is static
110
+ kept.remove(); // three's own shadow node again
111
+ ```
112
+
113
+ Off until you call it, and only on the light you hand in. Static casters are drawn once into a second depth map. Each shadow frame copies that map into the light's own
114
+ shadow map and draws the rest on top, so no material or shadow filter changes. Whatever you did not name static keeps redrawing: forgetting one costs time, never a
115
+ wrong shadow. A static object added later (a streamed block) casts at once, drawn with the moving things until the next redraw. A batch counts as static when all its originals do, by a subtree or by a test; one moving original keeps the whole batch
116
+ redrawing.
117
+
118
+ **You must call `redraw()`** when a static object moves, hides or is removed: its old shadow stays until then. The engine does not watch for this, with one exception: an
119
+ `InstancedMesh` whose instances changed, or that left the scene (a batch too), redraws the kept map by itself.
120
+
121
+ It does not help when:
122
+ - **The sun moves.** Any change to the light or its box redraws everything, on top of the copy, so a day cycle costs more than plain shadows. Under a box the map holds
123
+ still until the center has left a quarter of the box; the shadows reach that much less far on the side the camera moves toward, and each step is one full redraw.
124
+ - **Wind or vertex animation moves a "static" object.** Its kept shadow does not sway. Leave those objects out of `static`.
125
+ - **Terrain detail changes.** A chunk that swaps its detail leaves the old shape in the kept map until `redraw()`. Leave it out, or call `redraw()` on the swap.
126
+ - **Few things cast.** The copy and the second map (mapSize x mapSize x 4 bytes, and float depth on the light's own map) are a fixed price. Frame time has not been measured yet.
127
+
128
+ WebGPU backend only, one plain map only: with cascades it is refused, and three's `CSMShadowNode` gives no way in. On WebGLRenderer and on WebGPURenderer's WebGL2 backend
129
+ the light draws everything, with one warning: the same picture, no saving. A budget on the same light still sets how often the moving things redraw.
130
+ Listed in `describe().threePatches` as `shadow-cache`.
131
+
109
132
  ## An update budget
110
133
  A shadow map of a still scene need not redraw every frame:
111
134
 
@@ -149,9 +172,8 @@ sun.shadow.bias = -0.0001; sun.shadow.normalBias = 0.03;
149
172
  sun.shadow.mapSize.setScalar(2048); // before the first frame; later also sun.shadow.map?.dispose(); sun.shadow.map = null;
150
173
  ```
151
174
 
152
- Changing the type later, measured against three 0.187's source:
153
- - WebGLRenderer does the work itself: `WebGLShadowMap.render` sees the new type, sets `needsUpdate` on every material in the scene and rebuilds the maps of the lights it draws that frame. You write
154
- only `shadowMap.type`. Two limits: it happens on the next frame that draws a shadow, and only for lights that draw on that frame. A light under `engine.shadows.budget` is redrawn
175
+ Changing the type later:
176
+ - WebGLRenderer does the work itself on the next frame that draws a shadow, only for lights that draw on that frame. A light under `engine.shadows.budget` is redrawn
155
177
  by the budget at once. A light of your own with `shadow.autoUpdate = false` needs `light.shadow.needsUpdate = true` in the same frame, or it keeps its old map.
156
178
  - WebGPURenderer reads the type when it builds a light's shadow node. Set it before the first frame; there is no switch at run time.
157
179
 
@@ -21,7 +21,7 @@ File > Export > glTF 2.0, into `art/models/` of the game:
21
21
  | Transform > +Y Up | on | three.js is Y-up; the exporter converts from Blender's Z-up |
22
22
  | Mesh > Apply Modifiers | on | the game gets the shape you see |
23
23
  | Mesh > UVs, Normals, Vertex Colors | on if used | missing data cannot be added later |
24
- | Material > Images | Automatic (embedded in the .glb) | `threedog assets` recompresses them anyway |
24
+ | Material > Images | Automatic (embedded in the .glb) | `threedog assets optimize` recompresses them anyway |
25
25
  | Animation > Actions / NLA tracks | on for animated models | each action becomes one clip; name it as the game will (`Idle`, `Walk`, `Slash`) |
26
26
  | Limit to > Visible Objects | off for levels | hidden `col_` boxes must be exported |
27
27
 
@@ -30,10 +30,10 @@ Do not join meshes or decimate a level: node names are its interface.
30
30
  ## 3. Check, then compress
31
31
  ```
32
32
  threedog assets check art/models/town.glb # inspect only: spawns, col_ colliders, trigger_ zones, template / id nodes, material suspicions
33
- threedog assets # compress art/models/*.glb into public/optimized/ (add -w my-game for a game)
33
+ threedog assets optimize # compress art/models/*.glb into public/optimized/ (add -w my-game for a game)
34
34
  ```
35
35
  `assets check` also lists materials, textures with the color space three gives them (by slot), normal maps, clips, `col_` colliders and template / id nodes.
36
- After loading, `engine.assets.report('hero')` (or `.reportText`; pass a copy from `model()` to check what the game shows) lists the same from the scene and
36
+ After loading, `engine.assets.report('hero')` (pass a copy from `model()` to check what the game shows) lists the same from the scene and
37
37
  flags what the file has but the scene lost: `normal-map-lost`, `material-replaced` (your code took over), `clip-lost`, `clip-unmapped` (an animation
38
38
  state names a clip the file lacks), `node-lost`, and wrong color spaces. The engine never recomputes normals or drops a normal map on a GLB; only code
39
39
  that swaps materials as in section 5 does.
@@ -44,7 +44,7 @@ colliders, template / id nodes, spawn / trigger_ / npc_ / marker_ points; warnin
44
44
  | Step | What it does | When it matters |
45
45
  |---|---|---|
46
46
  | Meshopt | compresses geometry; the decoder ships with the engine | always on; smaller download, no visual change |
47
- | KTX2 | textures stay compressed on the GPU too, and the download shrinks | always on for a model with textures; needs the `ktx` tool (KTX-Software): without it that model fails, the command says what to install, and the game's `assets.js` stays as it was |
47
+ | KTX2 | textures stay compressed on the GPU too, and the download shrinks | always on for a model with textures; encoded by the `ktx` tool when it is on PATH, else by the WebAssembly encoder of `ktx2-encoder`, which comes with the tools; if neither runs, that model fails, the command says why, and the game's `assets.js` stays as it was |
48
48
 
49
49
  It needs `@gltf-transform/cli` (`npm i -D @gltf-transform/cli`). Unchanged files are skipped; `--force` redoes everything.
50
50
 
@@ -61,7 +61,6 @@ Views can also be content: `content/views.js` = `{ gate: { position: [10, 4, 12]
61
61
  ambient and hemisphere light hidden so shadows read black, `'normals'` the normals as color (a normal map shows here); `'off'` restores everything.
62
62
  A view is temporary and owner-scoped (`{ owner }`; another owner needs `force: true` to clear it), never saved, and `describe().render` / `describe render`
63
63
  start with a DEBUG VIEW line while one is on, so a screenshot of clay is not taken for the real picture. Meshes added after the call are not covered: call it again.
64
- It needs the debug module (`engine.use(debug())`).
65
64
 
66
65
  ## 4. Use it
67
66
  Register the compressed file in the asset pack and refer to it by name everywhere else:
@@ -90,7 +89,7 @@ This replaces the painted material, so the texture is gone unless yours sets `ma
90
89
 
91
90
  ## 6. Smallest copyable flow
92
91
  1. Blender: a cube named `well`, UV unwrapped, base color baked to a texture, export `art/models/well.glb` with the settings above.
93
- 2. `threedog assets check art/models/well.glb` (no errors), then `threedog assets`.
92
+ 2. `threedog assets check art/models/well.glb` (no errors), then `threedog assets optimize`.
94
93
  3. Add `well: '/models/well.glb'` to `engine.assets.manifest({ models })`.
95
94
  4. `engine.add(await engine.assets.model('well'))`, then `threedog shot /your-page.html --describe-only` or open the page.
96
95
 
@@ -100,11 +99,11 @@ This replaces the painted material, so the texture is gone unless yours sets `ma
100
99
  | Model too dark, or textures washed out/white | color space: a color map used as data or a data map as color (`assets check` reports `looks like data` / `looks like color`) | color maps (base color, emissive) are sRGB; normal, AO, roughness are Non-Color in Blender; one image per use |
101
100
  | Texture shows one flat color | no UV set on that mesh (`texture-without-uv`) | unwrap in Blender, export again |
102
101
  | Emissive never glows | emissive texture with black emissive color (`emissive-hidden`) | set the emissive color to white |
103
- | Node names lost (`spawn`, `col_` not found) | meshes joined before export | keep objects separate; `threedog assets` never flattens or joins; run `assets check` to see what the level finds |
102
+ | Node names lost (`spawn`, `col_` not found) | meshes joined before export | keep objects separate; `threedog assets optimize` never flattens or joins; run `assets check` to see what the level finds |
104
103
  | Custom properties missing (`template`, `id`) | "Custom Properties" off at export | tick it and export again |
105
104
  | Collision does nothing | `col_` object not exported, or no `collision()` module | export hidden objects; `engine.use(level(...), collision())`; test `engine.collision.walkable({ x, z })` |
106
105
  | Painted material replaced by a flat look | game code swapped it (section 5) | remove the swap, or give your material a `map` |
107
- | File too big | large textures or many materials | textures at 2048 or less, share atlases, `threedog assets`; sizes are in the `assets` output and `.threedog/optimize-report.json` |
106
+ | File too big | large textures or many materials | textures at 2048 or less, share atlases, `threedog assets optimize`; sizes are in the `assets` output and `.threedog/optimize-report.json` |
108
107
  | Clip not found | action name differs from the manifest `animation` mapping | rename the action in Blender or change `clip:` |
109
108
 
110
109
  See also: `10-blender-level.md` (level names and markers), `43-shader-patches-surfaces.md` (your own material, shader patches),