threedog 0.2.1 → 0.7.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 (725) hide show
  1. package/CHANGELOG.md +371 -1
  2. package/README.md +21 -12
  3. package/core/Animator.js +15 -12
  4. package/core/Assets.js +92 -26
  5. package/core/Audio.js +45 -38
  6. package/core/Baked.js +30 -15
  7. package/core/Batching.js +102 -15
  8. package/core/Budget.js +40 -19
  9. package/core/Clock.js +40 -80
  10. package/core/DebugOverrides.js +75 -25
  11. package/core/Describe.js +105 -45
  12. package/core/Engine.js +203 -75
  13. package/core/Events.js +4 -1
  14. package/core/I18n.js +25 -13
  15. package/core/Input.js +175 -80
  16. package/core/Inspect.js +8 -5
  17. package/core/InspectMaterials.js +84 -0
  18. package/core/InspectPerf.js +68 -0
  19. package/core/InspectScene.js +13 -2
  20. package/core/Loading.js +2 -1
  21. package/core/MaterialInfo.js +7 -5
  22. package/core/Ops.js +50 -9
  23. package/core/Random.js +9 -2
  24. package/core/Rays.js +12 -7
  25. package/core/SaveStore.js +41 -9
  26. package/core/Saves.js +71 -15
  27. package/core/Stream.js +13 -10
  28. package/core/Threads.js +26 -7
  29. package/core/UrlState.js +69 -13
  30. package/core/Views.js +10 -6
  31. package/core/actor.js +1 -1
  32. package/core/adaptive.js +93 -0
  33. package/core/assetEntry.js +27 -7
  34. package/core/assetHot.js +176 -0
  35. package/core/assetLoad.js +44 -19
  36. package/core/assetManifest.js +53 -11
  37. package/core/assetReport.js +5 -5
  38. package/core/audioRules.js +13 -11
  39. package/core/audioVoice.js +69 -42
  40. package/core/batchGroups.js +9 -1
  41. package/core/canvasBox.js +1 -2
  42. package/core/catalog.js +21 -5
  43. package/core/cells.js +62 -0
  44. package/core/checks.js +10 -8
  45. package/core/chunkStream.js +87 -0
  46. package/core/control.js +149 -0
  47. package/core/copies.js +32 -0
  48. package/core/dayTime.js +12 -6
  49. package/core/debugView.js +85 -18
  50. package/core/describeRender.js +117 -21
  51. package/core/describeText.js +41 -21
  52. package/core/engineCamera.js +32 -0
  53. package/core/errorCodes.js +478 -32
  54. package/core/errorHints.js +41 -142
  55. package/core/errors.js +31 -8
  56. package/core/events.generated.js +13 -7
  57. package/core/gameLights.js +69 -0
  58. package/core/grayMap.js +20 -0
  59. package/core/grid.js +14 -13
  60. package/core/gridLoad.js +2 -1
  61. package/core/ground.js +6 -4
  62. package/core/headlessLoad.js +55 -0
  63. package/core/holds.js +78 -50
  64. package/core/inputContext.js +11 -7
  65. package/core/inputDefaults.js +24 -21
  66. package/core/inputPad.js +1 -1
  67. package/core/inputPointer.js +1 -1
  68. package/core/inputTouch.js +2 -2
  69. package/core/lifecycle.js +46 -12
  70. package/core/lightWatch.js +9 -5
  71. package/core/loop.js +109 -43
  72. package/core/math.js +18 -3
  73. package/core/moduleSlots.js +100 -0
  74. package/core/moduleTypes.js +86 -0
  75. package/core/modules.js +147 -29
  76. package/core/movement.js +2 -2
  77. package/core/namedModel.js +74 -0
  78. package/core/opsCheck.js +3 -3
  79. package/core/opsFail.js +15 -6
  80. package/core/opsGuard.js +4 -4
  81. package/core/opsRun.js +4 -3
  82. package/core/owned.js +171 -0
  83. package/core/page.js +44 -10
  84. package/core/params.js +30 -8
  85. package/core/pictureSlots.js +33 -0
  86. package/core/pictureWriters.js +343 -0
  87. package/core/plugins.js +245 -0
  88. package/core/saveApply.js +99 -17
  89. package/core/saveFiles.js +8 -6
  90. package/core/saveValues.js +46 -0
  91. package/core/saveVersions.js +20 -3
  92. package/core/scatter.js +48 -19
  93. package/core/sceneDelta.js +211 -0
  94. package/core/sceneMethods.js +31 -33
  95. package/core/screenToGround.js +1 -1
  96. package/core/services.js +27 -11
  97. package/core/session.js +28 -25
  98. package/core/shaderCost.js +78 -0
  99. package/core/shadowTypes.js +3 -2
  100. package/core/shadows.js +405 -0
  101. package/core/sharedPoint.js +303 -0
  102. package/core/skip.js +36 -28
  103. package/core/stackFrames.js +45 -0
  104. package/core/stacks.js +166 -0
  105. package/core/startup.js +153 -13
  106. package/core/streamSources.js +0 -11
  107. package/core/targeting.js +1 -1
  108. package/core/textureBytes.js +20 -0
  109. package/core/thingExplain.js +3 -3
  110. package/core/thingOps.js +29 -6
  111. package/core/things.js +22 -12
  112. package/core/threadPure.js +7 -3
  113. package/core/threePatches.js +47 -0
  114. package/core/tier.js +21 -10
  115. package/core/time.js +236 -0
  116. package/core/uiClock.js +25 -0
  117. package/core/util.js +8 -2
  118. package/core/validate.js +66 -87
  119. package/core/view.js +43 -0
  120. package/core/wrap.js +106 -0
  121. package/docs/AI.md +95 -104
  122. package/docs/API.md +35 -31
  123. package/docs/CODEMAP.md +231 -176
  124. package/docs/COMMANDS.md +63 -39
  125. package/docs/CONVENTIONS.md +99 -31
  126. package/docs/MIGRATING-0.3.md +248 -0
  127. package/docs/MIGRATING-0.4.md +320 -0
  128. package/docs/MIGRATING-0.5.md +68 -0
  129. package/docs/MIGRATING-0.6.md +126 -0
  130. package/docs/MIGRATING-0.7.md +39 -0
  131. package/docs/PERF.md +35 -7
  132. package/docs/THREE.md +82 -28
  133. package/docs/api/ambience.md +0 -1
  134. package/docs/api/assets.md +15 -11
  135. package/docs/api/audio.md +10 -14
  136. package/docs/api/budget.md +4 -0
  137. package/docs/api/calendar.md +41 -0
  138. package/docs/api/cameraRig.md +16 -5
  139. package/docs/api/characters.md +41 -24
  140. package/docs/api/clock.md +6 -5
  141. package/docs/api/collision.md +10 -7
  142. package/docs/api/content.md +6 -4
  143. package/docs/api/dialogue.md +9 -9
  144. package/docs/api/engine.md +95 -65
  145. package/docs/api/events.md +20 -14
  146. package/docs/api/grids.md +10 -3
  147. package/docs/api/ground.md +1 -1
  148. package/docs/api/hud.md +5 -2
  149. package/docs/api/i18n.md +1 -1
  150. package/docs/api/input.md +13 -7
  151. package/docs/api/interactions.md +10 -6
  152. package/docs/api/inventory.md +17 -17
  153. package/docs/api/level.md +0 -2
  154. package/docs/api/lightPool.md +3 -1
  155. package/docs/api/music.md +1 -1
  156. package/docs/api/nav.md +3 -0
  157. package/docs/api/occlusion.md +5 -0
  158. package/docs/api/openWorld.md +2 -1
  159. package/docs/api/particles.md +6 -3
  160. package/docs/api/photo.md +0 -2
  161. package/docs/api/pickups.md +21 -0
  162. package/docs/api/post.md +8 -5
  163. package/docs/api/quests.md +2 -3
  164. package/docs/api/rigid.md +16 -14
  165. package/docs/api/saves.md +6 -4
  166. package/docs/api/settings.md +2 -3
  167. package/docs/api/sky.md +18 -49
  168. package/docs/api/stream.md +1 -1
  169. package/docs/api/terrain.md +16 -7
  170. package/docs/api/terrainDraw.md +12 -0
  171. package/docs/api/timeline.md +3 -4
  172. package/docs/api/tools.md +32 -15
  173. package/docs/api/triggers.md +7 -10
  174. package/docs/api/vegetation.md +17 -9
  175. package/docs/api/vegetationDraw.md +10 -0
  176. package/docs/api/water.md +9 -21
  177. package/docs/api/weather.md +10 -15
  178. package/docs/api/weatherDraw.md +10 -0
  179. package/docs/api/world.md +3 -2
  180. package/docs/recipes/01-new-game.md +52 -42
  181. package/docs/recipes/02-npc.md +24 -7
  182. package/docs/recipes/03-items-pickups.md +57 -22
  183. package/docs/recipes/04-quests.md +12 -5
  184. package/docs/recipes/05-props-bake.md +174 -22
  185. package/docs/recipes/06-clock-events.md +100 -52
  186. package/docs/recipes/07-flow-scenes-pause.md +50 -10
  187. package/docs/recipes/08-weather-sky.md +134 -37
  188. package/docs/recipes/09-materials-lights-models.md +185 -0
  189. package/docs/recipes/10-blender-level.md +16 -4
  190. package/docs/recipes/11-vehicles.md +67 -10
  191. package/docs/recipes/13-animation.md +23 -9
  192. package/docs/recipes/14-custom-module.md +63 -29
  193. package/docs/recipes/15-perf.md +31 -10
  194. package/docs/recipes/16-dev-bridge.md +74 -40
  195. package/docs/recipes/18-world.md +26 -14
  196. package/docs/recipes/19-shooting-picking.md +12 -10
  197. package/docs/recipes/20-content-actions-save.md +60 -18
  198. package/docs/recipes/21-camera.md +55 -17
  199. package/docs/recipes/22-shop-gold.md +3 -3
  200. package/docs/recipes/23-ops.md +17 -11
  201. package/docs/recipes/26-ui-parts.md +86 -13
  202. package/docs/recipes/27-net.md +11 -5
  203. package/docs/recipes/28-terrain.md +36 -3
  204. package/docs/recipes/29-grid.md +46 -19
  205. package/docs/recipes/30-templates.md +72 -15
  206. package/docs/recipes/31-audio.md +20 -12
  207. package/docs/recipes/32-action-game.md +51 -4
  208. package/docs/recipes/34-environment-lighting.md +108 -21
  209. package/docs/recipes/35-picture-from-reference.md +147 -0
  210. package/docs/recipes/36-sky-ground-plants.md +158 -0
  211. package/docs/recipes/{37-terrain-look-hooks.md → 37-terrain-hooks.md} +9 -12
  212. package/docs/recipes/38-environment-hooks.md +67 -0
  213. package/docs/recipes/40-scene.md +114 -32
  214. package/docs/recipes/42-terrain-shape.md +2 -2
  215. package/docs/recipes/43-shader-patches-surfaces.md +206 -0
  216. package/docs/recipes/44-water-weather-hooks.md +191 -0
  217. package/docs/recipes/45-cascaded-shadows.md +130 -23
  218. package/docs/recipes/46-blender-art-route.md +17 -20
  219. package/docs/recipes/47-island-snow-backdrop.md +13 -15
  220. package/docs/recipes/49-game-module-contract.md +30 -17
  221. package/docs/recipes/50-own-renderer.md +4 -4
  222. package/docs/recipes/51-capability-catalog.md +3 -3
  223. package/docs/recipes/52-around-the-picture.md +181 -0
  224. package/docs/recipes/53-ground-up-close.md +12 -12
  225. package/docs/recipes/54-three-idioms.md +88 -0
  226. package/docs/recipes/55-mods-time-wrap.md +124 -0
  227. package/docs/recipes/56-webgpu-tsl.md +303 -0
  228. package/docs/recipes/57-control.md +104 -0
  229. package/docs/recipes/58-input.md +60 -0
  230. package/docs/recipes/59-depth-post-effects.md +83 -0
  231. package/docs/recipes/60-judge-the-picture.md +70 -0
  232. package/docs/recipes/61-deploy.md +27 -0
  233. package/docs/recipes/62-picture-code.md +244 -0
  234. package/docs/recipes/63-third-person-start.md +77 -0
  235. package/docs/recipes/64-particles.md +35 -0
  236. package/examples/action/.threedog/content-index.json +1 -0
  237. package/examples/action/GAMEMAP.md +20 -14
  238. package/examples/action/assets.js +8 -0
  239. package/examples/action/content/actions.js +1 -1
  240. package/examples/action/content/audio.js +5 -5
  241. package/examples/action/main.js +12 -15
  242. package/examples/action/models.js +23 -24
  243. package/examples/action/picture/index.js +13 -0
  244. package/examples/action/picture/lights.js +35 -0
  245. package/examples/action/picture/materials.js +8 -0
  246. package/examples/action/picture/palette.js +33 -0
  247. package/examples/plain-picture/.threedog/content-index.json +1 -0
  248. package/examples/plain-picture/FINDINGS.md +53 -0
  249. package/examples/plain-picture/GAMEMAP.md +90 -0
  250. package/examples/plain-picture/main.js +48 -0
  251. package/examples/plain-picture/picture/index.js +21 -0
  252. package/examples/plain-picture/picture/lights.js +54 -0
  253. package/examples/plain-picture/picture/materials.js +24 -0
  254. package/examples/plain-picture/picture/palette.js +16 -0
  255. package/examples/plain-picture/picture/sky.js +37 -0
  256. package/examples/pullable.json +133 -0
  257. package/examples/rc2d/.threedog/content-index.json +1 -0
  258. package/examples/rc2d/GAMEMAP.md +88 -0
  259. package/examples/rc2d/README.md +57 -0
  260. package/examples/rc2d/main.js +55 -0
  261. package/examples/rc2d/picture/index.js +18 -0
  262. package/examples/rc2d/picture/lights.js +11 -0
  263. package/examples/rc2d/picture/materials.js +18 -0
  264. package/examples/rc2d/picture/palette.js +15 -0
  265. package/examples/rc2d/picture/post.js +11 -0
  266. package/examples/rc2d/rc2d.js +187 -0
  267. package/examples/rc2d/shaders.js +144 -0
  268. package/examples/sheepisland/.threedog/content-index.json +1 -0
  269. package/examples/sheepisland/GAMEMAP.md +22 -17
  270. package/examples/sheepisland/assets.js +5 -0
  271. package/examples/sheepisland/content/templates.js +1 -1
  272. package/examples/sheepisland/content/vegetation.js +3 -1
  273. package/examples/sheepisland/main.js +29 -18
  274. package/examples/sheepisland/{style/models.js → models.js} +13 -24
  275. package/examples/sheepisland/picture/index.js +20 -0
  276. package/examples/sheepisland/picture/lights.js +34 -0
  277. package/examples/sheepisland/picture/materials.js +61 -0
  278. package/examples/sheepisland/picture/palette.js +60 -0
  279. package/examples/sheepisland/picture/sky.js +20 -0
  280. package/examples/sheepisland/views.js +1 -1
  281. package/examples/styletest/.threedog/content-index.json +1 -0
  282. package/examples/styletest/GAMEMAP.md +25 -22
  283. package/examples/styletest/assets.js +6 -0
  284. package/examples/styletest/content/templates.js +1 -1
  285. package/examples/styletest/content/vegetation.js +1 -1
  286. package/examples/styletest/main.js +25 -34
  287. package/examples/styletest/models.js +30 -30
  288. package/examples/styletest/params.js +2 -5
  289. package/examples/styletest/picture/env.js +38 -0
  290. package/examples/styletest/picture/index.js +16 -0
  291. package/examples/styletest/picture/lights.js +9 -0
  292. package/examples/styletest/picture/materials.js +28 -0
  293. package/examples/styletest/picture/palette.js +22 -0
  294. package/examples/techniques/contact-shadows/README.md +91 -0
  295. package/examples/techniques/contact-shadows/contactShadows.js +67 -0
  296. package/examples/techniques/contact-shadows/main.js +55 -0
  297. package/examples/techniques/contact-shadows/picture/index.js +14 -0
  298. package/examples/techniques/contact-shadows/picture/lights.js +34 -0
  299. package/examples/techniques/contact-shadows/picture/materials.js +12 -0
  300. package/examples/techniques/contact-shadows/picture/palette.js +17 -0
  301. package/examples/techniques/day-night-grade/README.md +83 -0
  302. package/examples/techniques/day-night-grade/main.js +41 -0
  303. package/examples/techniques/day-night-grade/picture/index.js +20 -0
  304. package/examples/techniques/day-night-grade/picture/lights.js +45 -0
  305. package/examples/techniques/day-night-grade/picture/materials.js +15 -0
  306. package/examples/techniques/day-night-grade/picture/palette.js +59 -0
  307. package/examples/techniques/day-night-grade/picture/post.js +25 -0
  308. package/examples/techniques/day-night-grade/picture/sky.js +33 -0
  309. package/examples/techniques/interior-light/README.md +106 -0
  310. package/examples/techniques/interior-light/interiorLight.js +47 -0
  311. package/examples/techniques/interior-light/main.js +87 -0
  312. package/examples/techniques/interior-light/picture/index.js +24 -0
  313. package/examples/techniques/interior-light/picture/lights.js +52 -0
  314. package/examples/techniques/interior-light/picture/materials.js +13 -0
  315. package/examples/techniques/interior-light/picture/palette.js +32 -0
  316. package/examples/techniques/interior-light/picture/sky.js +40 -0
  317. package/examples/techniques/modular-building/README.md +126 -0
  318. package/examples/techniques/modular-building/kit.js +426 -0
  319. package/examples/techniques/modular-building/main.js +39 -0
  320. package/examples/techniques/modular-building/picture/index.js +17 -0
  321. package/examples/techniques/modular-building/picture/lights.js +26 -0
  322. package/examples/techniques/modular-building/picture/materials.js +13 -0
  323. package/examples/techniques/modular-building/picture/palette.js +23 -0
  324. package/examples/techniques/night-lights/README.md +95 -0
  325. package/examples/techniques/night-lights/main.js +47 -0
  326. package/examples/techniques/night-lights/nightLights.js +95 -0
  327. package/examples/techniques/night-lights/picture/index.js +18 -0
  328. package/examples/techniques/night-lights/picture/lights.js +44 -0
  329. package/examples/techniques/night-lights/picture/materials.js +15 -0
  330. package/examples/techniques/night-lights/picture/palette.js +34 -0
  331. package/examples/techniques/night-lights/picture/post.js +9 -0
  332. package/examples/techniques/night-lights/picture/sky.js +36 -0
  333. package/examples/techniques/outlines-and-rim/README.md +59 -0
  334. package/examples/techniques/outlines-and-rim/main.js +61 -0
  335. package/examples/techniques/outlines-and-rim/outlines.js +64 -0
  336. package/examples/techniques/outlines-and-rim/picture/index.js +16 -0
  337. package/examples/techniques/outlines-and-rim/picture/lights.js +18 -0
  338. package/examples/techniques/outlines-and-rim/picture/materials.js +11 -0
  339. package/examples/techniques/outlines-and-rim/picture/palette.js +16 -0
  340. package/examples/techniques/outlines-and-rim/picture/post.js +10 -0
  341. package/examples/techniques/procedural-animation/README.md +79 -0
  342. package/examples/techniques/procedural-animation/creature.js +44 -0
  343. package/examples/techniques/procedural-animation/main.js +47 -0
  344. package/examples/techniques/procedural-animation/picture/index.js +15 -0
  345. package/examples/techniques/procedural-animation/picture/lights.js +35 -0
  346. package/examples/techniques/procedural-animation/picture/materials.js +10 -0
  347. package/examples/techniques/procedural-animation/picture/palette.js +16 -0
  348. package/examples/techniques/procedural-animation/picture/sky.js +31 -0
  349. package/examples/techniques/procedural-animation/proceduralAnim.js +118 -0
  350. package/examples/techniques/procedural-surface/README.md +69 -0
  351. package/examples/techniques/procedural-surface/main.js +65 -0
  352. package/examples/techniques/procedural-surface/picture/index.js +14 -0
  353. package/examples/techniques/procedural-surface/picture/lights.js +18 -0
  354. package/examples/techniques/procedural-surface/picture/materials.js +34 -0
  355. package/examples/techniques/procedural-surface/picture/palette.js +15 -0
  356. package/examples/techniques/procedural-surface/surface.js +120 -0
  357. package/examples/techniques/stylized-shading/README.md +74 -0
  358. package/examples/techniques/stylized-shading/main.js +62 -0
  359. package/examples/techniques/stylized-shading/picture/index.js +14 -0
  360. package/examples/techniques/stylized-shading/picture/lights.js +23 -0
  361. package/examples/techniques/stylized-shading/picture/materials.js +18 -0
  362. package/examples/techniques/stylized-shading/picture/palette.js +19 -0
  363. package/examples/techniques/stylized-shading/shading.js +131 -0
  364. package/examples/techniques/sun-and-key-light/README.md +133 -0
  365. package/examples/techniques/sun-and-key-light/keyLight.js +71 -0
  366. package/examples/techniques/sun-and-key-light/main.js +51 -0
  367. package/examples/techniques/sun-and-key-light/picture/index.js +13 -0
  368. package/examples/techniques/sun-and-key-light/picture/lights.js +33 -0
  369. package/examples/techniques/sun-and-key-light/picture/materials.js +9 -0
  370. package/examples/techniques/sun-and-key-light/picture/palette.js +29 -0
  371. package/examples/techniques/sun-and-key-light/picture/sky.js +16 -0
  372. package/examples/techniques/three-looks/README.md +21 -0
  373. package/examples/techniques/three-looks/main.js +58 -0
  374. package/examples/techniques/three-looks/picture/cartoon.js +23 -0
  375. package/examples/techniques/three-looks/picture/cinematic.js +27 -0
  376. package/examples/techniques/three-looks/picture/comic.js +27 -0
  377. package/examples/techniques/three-looks/picture/index.js +14 -0
  378. package/examples/techniques/three-looks/picture/sun.js +23 -0
  379. package/examples/techniques/wet-road/README.md +102 -0
  380. package/examples/techniques/wet-road/main.js +48 -0
  381. package/examples/techniques/wet-road/picture/index.js +17 -0
  382. package/examples/techniques/wet-road/picture/lights.js +57 -0
  383. package/examples/techniques/wet-road/picture/materials.js +16 -0
  384. package/examples/techniques/wet-road/picture/palette.js +24 -0
  385. package/examples/techniques/wet-road/picture/sky.js +46 -0
  386. package/examples/techniques/wet-road/wetRoad.js +209 -0
  387. package/examples/webgpu-tsl/.threedog/content-index.json +1 -0
  388. package/examples/webgpu-tsl/GAMEMAP.md +89 -0
  389. package/examples/webgpu-tsl/README.md +45 -0
  390. package/examples/webgpu-tsl/main.js +44 -0
  391. package/examples/webgpu-tsl/picture/index.js +18 -0
  392. package/examples/webgpu-tsl/picture/lights.js +25 -0
  393. package/examples/webgpu-tsl/picture/materials.js +36 -0
  394. package/examples/webgpu-tsl/picture/palette.js +23 -0
  395. package/examples/webgpu-tsl/picture/post.js +16 -0
  396. package/examples/webgpu-tsl/picture/sky.js +30 -0
  397. package/examples/world/.threedog/content-index.json +1 -0
  398. package/examples/world/GAMEMAP.md +31 -21
  399. package/examples/world/art/humanoid.js +38 -0
  400. package/examples/world/art/index.js +11 -0
  401. package/examples/world/art/models.js +57 -0
  402. package/examples/world/art/shapes.js +9 -0
  403. package/examples/world/assets.js +6 -0
  404. package/examples/world/content/audio.js +5 -5
  405. package/examples/world/content/items.js +1 -1
  406. package/examples/world/content/npcs.js +1 -1
  407. package/examples/world/content/props.js +1 -1
  408. package/examples/world/content/rules.js +1 -1
  409. package/examples/world/content/templates.js +1 -1
  410. package/examples/world/content/vegetation.js +1 -1
  411. package/examples/world/main.js +22 -18
  412. package/examples/world/models.js +17 -7
  413. package/examples/world/picture/index.js +20 -0
  414. package/examples/world/picture/lights.js +41 -0
  415. package/examples/world/picture/materials.js +34 -0
  416. package/examples/world/picture/palette.js +74 -0
  417. package/examples/world/picture/post.js +9 -0
  418. package/examples/world/picture/sky.js +39 -0
  419. package/index.d.ts +44 -54
  420. package/index.js +29 -25
  421. package/llms.txt +24 -15
  422. package/modules/ambience/Ambience.js +34 -23
  423. package/modules/calendar/Calendar.js +163 -0
  424. package/modules/calendar/contract.js +21 -0
  425. package/modules/calendar/index.js +82 -0
  426. package/modules/{sky → calendar}/schedule.js +17 -16
  427. package/modules/calendar/seasons.js +55 -0
  428. package/modules/camera/FirstPersonCamera.js +6 -15
  429. package/modules/camera/FixedCamera.js +33 -15
  430. package/modules/camera/FollowCamera.js +90 -16
  431. package/modules/camera/RtsCamera.js +18 -17
  432. package/modules/camera/common.js +73 -20
  433. package/modules/camera/contract.js +17 -0
  434. package/modules/camera/projection.js +11 -24
  435. package/modules/characters/Character.js +188 -48
  436. package/modules/characters/NPC.js +92 -41
  437. package/modules/characters/assetModel.js +8 -16
  438. package/modules/characters/behaviors.js +2 -1
  439. package/modules/characters/contract.js +13 -0
  440. package/modules/characters/footsteps.js +1 -1
  441. package/modules/characters/index.js +112 -49
  442. package/modules/characters/moveHelpers.js +4 -5
  443. package/modules/characters/movers/common.js +102 -19
  444. package/modules/characters/movers/manual.js +23 -0
  445. package/modules/characters/movers/rapier.js +19 -16
  446. package/modules/characters/movers/simple.js +14 -11
  447. package/modules/characters/movers/support.js +186 -0
  448. package/modules/characters/npcAvoid.js +47 -42
  449. package/modules/characters/npcSim.js +17 -9
  450. package/modules/characters/npcView.js +44 -16
  451. package/modules/characters/pathAsync.js +7 -3
  452. package/modules/characters/playerStrings.js +4 -3
  453. package/modules/characters/proceduralRig.js +19 -4
  454. package/modules/characters/useModel.js +2 -2
  455. package/modules/characters/vehicle.contract.js +21 -0
  456. package/modules/characters/walkTo.js +21 -7
  457. package/modules/collision/Collision.js +69 -27
  458. package/modules/collision/grid.js +8 -6
  459. package/modules/content/explain.js +2 -2
  460. package/modules/content/fields.js +102 -0
  461. package/modules/content/ids.js +8 -6
  462. package/modules/content/index.js +104 -27
  463. package/modules/content/loader.js +45 -33
  464. package/modules/content/models.js +58 -42
  465. package/modules/content/packs.js +6 -5
  466. package/modules/content/parts.js +41 -0
  467. package/modules/content/props.js +20 -11
  468. package/modules/content/rules.js +66 -0
  469. package/modules/content/stages.js +80 -0
  470. package/modules/content/templates.js +123 -30
  471. package/modules/debug/DebugPanel.js +24 -17
  472. package/modules/flow/contract.js +16 -0
  473. package/modules/flow/index.js +21 -15
  474. package/modules/gameplay/Container.js +41 -32
  475. package/modules/gameplay/Containers.js +30 -15
  476. package/modules/gameplay/Dialogue.js +113 -55
  477. package/modules/gameplay/Interactions.js +273 -72
  478. package/modules/gameplay/Inventory.js +72 -76
  479. package/modules/gameplay/Quests.js +75 -39
  480. package/modules/gameplay/boxOps.js +3 -3
  481. package/modules/gameplay/dialogue.contract.js +21 -0
  482. package/modules/gameplay/dialogueBox.js +4 -2
  483. package/modules/gameplay/dialogueBubbles.js +116 -0
  484. package/modules/gameplay/interactionPrompt.js +4 -1
  485. package/modules/gameplay/interactions.contract.js +16 -0
  486. package/modules/gameplay/inventory.contract.js +23 -0
  487. package/modules/gameplay/inventoryOps.js +19 -17
  488. package/modules/gameplay/pickups.js +306 -85
  489. package/modules/gameplay/questLog.js +6 -6
  490. package/modules/gameplay/quests.contract.js +17 -0
  491. package/modules/grid/Grid.js +92 -50
  492. package/modules/grid/Layer.js +27 -13
  493. package/modules/grid/index.js +96 -43
  494. package/modules/grid/ops.js +20 -18
  495. package/modules/grid/preview.js +37 -29
  496. package/modules/grid/strings.js +12 -0
  497. package/modules/grid/tiles.js +19 -22
  498. package/modules/hlod/index.js +52 -19
  499. package/modules/hud/index.js +48 -20
  500. package/modules/hud/parts.js +28 -13
  501. package/modules/level/heightfield.js +3 -1
  502. package/modules/level/index.js +49 -23
  503. package/modules/level/nodes.js +4 -3
  504. package/modules/lights/index.js +47 -19
  505. package/modules/music/Music.js +38 -21
  506. package/modules/music/rules.js +1 -1
  507. package/modules/nav/index.js +48 -42
  508. package/modules/net/Client.js +13 -6
  509. package/modules/net/Host.js +19 -5
  510. package/modules/net/Net.js +2 -1
  511. package/modules/net/index.js +13 -8
  512. package/modules/net/sync.js +9 -8
  513. package/modules/net/transports.js +77 -27
  514. package/modules/particles/Particles.js +58 -28
  515. package/modules/particles/contract.js +17 -0
  516. package/modules/particles/effect.js +54 -11
  517. package/modules/photo/Photo.js +56 -38
  518. package/modules/rigid/RigidBodies.js +194 -32
  519. package/modules/rigid/Vehicles.js +18 -15
  520. package/modules/rigid/sync.js +62 -17
  521. package/modules/settings/Settings.js +60 -55
  522. package/modules/settings/apply.js +38 -3
  523. package/modules/settings/sections.js +5 -3
  524. package/modules/settings/strings.js +3 -2
  525. package/modules/settings/view.js +5 -3
  526. package/modules/sky/Sky.js +132 -205
  527. package/modules/sky/clouds.js +37 -23
  528. package/modules/sky/dome.js +100 -0
  529. package/modules/sky/domeLayers.js +30 -15
  530. package/modules/sky/environment.js +45 -99
  531. package/modules/sky/fog.js +9 -139
  532. package/modules/sky/frame.js +60 -56
  533. package/modules/sky/season.js +21 -0
  534. package/modules/sky/shader.js +63 -35
  535. package/modules/sky/skyModule.js +52 -37
  536. package/modules/sky/tint.js +59 -0
  537. package/modules/terrain/ChunkManager.js +194 -110
  538. package/modules/terrain/chunkJob.js +1 -1
  539. package/modules/terrain/chunkQueries.js +6 -4
  540. package/modules/terrain/draw.js +154 -0
  541. package/modules/terrain/index.js +87 -54
  542. package/modules/terrain/terrainDefaults.js +2 -2
  543. package/modules/terrain/terrainEdit.js +18 -20
  544. package/modules/terrain/terrainGen.js +6 -4
  545. package/modules/terrain/terrainIsland.js +5 -4
  546. package/modules/terrain/terrainShape.js +12 -12
  547. package/modules/terrain/terrainShapeMain.js +8 -7
  548. package/modules/terrain/terrainTexture.js +21 -19
  549. package/modules/timeline/Timeline.js +95 -43
  550. package/modules/timeline/Tween.js +18 -10
  551. package/modules/timeline/letterbox.js +2 -2
  552. package/modules/timeline/shots.js +5 -1
  553. package/modules/triggers/index.js +111 -76
  554. package/modules/vegetation/Vegetation.js +229 -112
  555. package/modules/vegetation/densityMap.js +62 -0
  556. package/modules/vegetation/draw.js +191 -0
  557. package/modules/vegetation/groundChunks.js +52 -58
  558. package/modules/vegetation/index.js +32 -11
  559. package/modules/vegetation/modelGeometry.js +3 -2
  560. package/modules/vegetation/nearFade.js +8 -2
  561. package/modules/water/Water.js +157 -72
  562. package/modules/water/surface.js +86 -47
  563. package/modules/weather/Weather.js +96 -103
  564. package/modules/weather/particles.js +37 -14
  565. package/modules/weather/shaders.js +1 -1
  566. package/modules/weather/strings.js +2 -2
  567. package/modules/weather/weatherDraw.js +141 -0
  568. package/modules/world/World.js +26 -9
  569. package/modules/world/explain.js +1 -1
  570. package/modules/world/index.js +18 -4
  571. package/modules/world/persist.js +62 -2
  572. package/modules/world/pos.js +3 -2
  573. package/modules/world/records.js +8 -6
  574. package/modules/world/sim.js +60 -20
  575. package/modules/world/simFar.js +4 -3
  576. package/modules/world/state.js +3 -2
  577. package/modules/world/thingSource.js +1 -1
  578. package/modules/world/views.js +8 -6
  579. package/package.json +8 -3
  580. package/presets.js +43 -29
  581. package/render/PostFX.js +318 -135
  582. package/render/backend.js +486 -0
  583. package/render/cascades.js +195 -0
  584. package/render/cascades.tsl.js +96 -0
  585. package/render/daylight.js +39 -0
  586. package/render/detail.js +122 -41
  587. package/render/effects.js +155 -48
  588. package/render/engineUniforms.js +10 -1
  589. package/render/fuse.js +5 -4
  590. package/render/fx/cel.tsl.js +42 -0
  591. package/render/fx/cloudShadow.tsl.js +16 -0
  592. package/render/fx/detail.tsl.js +66 -0
  593. package/render/fx/index.js +28 -0
  594. package/render/fx/lighting.tsl.js +28 -0
  595. package/render/fx/nodeErrors.tsl.js +68 -0
  596. package/render/fx/noise.tsl.js +12 -0
  597. package/render/fx/objectOrigin.tsl.js +42 -0
  598. package/render/fx/particles.tsl.js +139 -0
  599. package/render/fx/patchNodes.tsl.js +403 -0
  600. package/render/fx/postWebGPU.tsl.js +181 -0
  601. package/render/fx/season.tsl.js +13 -0
  602. package/render/fx/sky.tsl.js +187 -0
  603. package/render/fx/uniforms.tsl.js +67 -0
  604. package/render/fx/water.tsl.js +53 -0
  605. package/render/fx/wind.tsl.js +22 -0
  606. package/render/fxNumbers.js +36 -0
  607. package/render/hotGlsl.js +24 -0
  608. package/render/layers.js +14 -0
  609. package/render/lighting.js +71 -18
  610. package/render/lightmap.js +142 -0
  611. package/render/lights.js +47 -6
  612. package/render/materials.js +55 -152
  613. package/render/modeling.js +73 -0
  614. package/render/neutral.js +23 -0
  615. package/render/occlusion.js +184 -0
  616. package/render/ownFrame.js +76 -0
  617. package/render/patches.js +317 -35
  618. package/render/postWebGL.js +205 -0
  619. package/render/probes.js +288 -0
  620. package/render/shaderErrors.js +154 -0
  621. package/render/slots.js +165 -0
  622. package/render/terms.js +163 -0
  623. package/render/uniformDecl.js +4 -3
  624. package/types/common.d.ts +2 -2
  625. package/types/core.d.ts +757 -346
  626. package/types/gameplay.d.ts +539 -330
  627. package/types/generated/core/Audio.d.ts +26 -42
  628. package/types/generated/core/Batching.d.ts +6 -2
  629. package/types/generated/core/Budget.d.ts +2 -8
  630. package/types/generated/core/DebugOverrides.d.ts +58 -9
  631. package/types/generated/core/I18n.d.ts +2 -2
  632. package/types/generated/core/Input.d.ts +110 -32
  633. package/types/generated/core/Ops.d.ts +31 -0
  634. package/types/generated/core/Random.d.ts +2 -2
  635. package/types/generated/core/Saves.d.ts +10 -2
  636. package/types/generated/core/Stream.d.ts +1 -1
  637. package/types/generated/core/Threads.d.ts +4 -1
  638. package/types/generated/core/Views.d.ts +0 -1
  639. package/types/generated/core/audioVoice.d.ts +17 -9
  640. package/types/generated/core/control.d.ts +55 -0
  641. package/types/generated/core/page.d.ts +9 -2
  642. package/types/generated/core/session.d.ts +2 -1
  643. package/types/generated/modules/ambience/Ambience.d.ts +0 -9
  644. package/types/generated/modules/calendar/Calendar.d.ts +84 -0
  645. package/types/generated/modules/calendar/index.d.ts +10 -0
  646. package/types/generated/modules/debug/DebugPanel.d.ts +2 -2
  647. package/types/generated/modules/grid/Grid.d.ts +61 -25
  648. package/types/generated/modules/grid/Layer.d.ts +13 -5
  649. package/types/generated/modules/grid/index.d.ts +56 -20
  650. package/types/generated/modules/music/Music.d.ts +5 -3
  651. package/types/generated/modules/net/index.d.ts +3 -3
  652. package/types/generated/modules/net/transports.d.ts +12 -3
  653. package/types/generated/modules/particles/Particles.d.ts +13 -6
  654. package/types/generated/modules/sky/Sky.d.ts +85 -110
  655. package/types/generated/modules/sky/skyModule.d.ts +1 -1
  656. package/types/generated/modules/timeline/Timeline.d.ts +8 -6
  657. package/types/generated/modules/water/Water.d.ts +50 -3
  658. package/types/generated/modules/weather/Weather.d.ts +44 -15
  659. package/types/generated/modules/weather/weatherDraw.d.ts +11 -0
  660. package/types/generated/modules/world/World.d.ts +20 -5
  661. package/types/generated/render/PostFX.d.ts +109 -29
  662. package/types/generated/render/lighting.d.ts +29 -0
  663. package/types/generated/render/materials.d.ts +31 -50
  664. package/types/generated/render/modeling.d.ts +30 -0
  665. package/types/generated/render/occlusion.d.ts +13 -0
  666. package/types/generated/render/patches.d.ts +16 -2
  667. package/types/generated/ui/UI.d.ts +6 -3
  668. package/types/nav.d.ts +6 -0
  669. package/types/physics.d.ts +140 -45
  670. package/types/tsl.d.ts +94 -0
  671. package/types/world.d.ts +489 -122
  672. package/ui/Focus.js +35 -14
  673. package/ui/LoadingScreen.js +3 -3
  674. package/ui/Parts.js +35 -15
  675. package/ui/UI.js +27 -17
  676. package/ui/cursor.js +115 -0
  677. package/ui/defaults.js +15 -11
  678. package/ui/theme.js +36 -27
  679. package/ui/touchPart.js +2 -2
  680. package/core/claims.js +0 -51
  681. package/core/look/coreParts.js +0 -112
  682. package/core/look/layers.js +0 -136
  683. package/core/look/part.js +0 -173
  684. package/core/look/report.js +0 -79
  685. package/core/look/write.js +0 -86
  686. package/core/renamed.js +0 -22
  687. package/core/styleParts.js +0 -98
  688. package/docs/api/entities.md +0 -61
  689. package/docs/api/style.md +0 -134
  690. package/docs/recipes/09-style-pack.md +0 -169
  691. package/docs/recipes/35-style-from-reference.md +0 -84
  692. package/docs/recipes/36-sky-ground-plant-looks.md +0 -118
  693. package/docs/recipes/38-environment-look-hooks.md +0 -88
  694. package/docs/recipes/41-thing-registry.md +0 -93
  695. package/docs/recipes/43-shader-patches-surface-looks.md +0 -135
  696. package/docs/recipes/44-water-weather-look-hooks.md +0 -135
  697. package/docs/recipes/48-make-material.md +0 -70
  698. package/docs/recipes/52-look.md +0 -146
  699. package/examples/action/look.js +0 -5
  700. package/examples/sheepisland/style/index.js +0 -79
  701. package/examples/styletest/look.js +0 -11
  702. package/examples/styletest/overrides.js +0 -33
  703. package/examples/styletest/panel.js +0 -135
  704. package/modules/entities/Entities.js +0 -243
  705. package/modules/entities/build.js +0 -25
  706. package/modules/entities/index.js +0 -4
  707. package/modules/sky/calendar.js +0 -80
  708. package/modules/sky/cascades.js +0 -125
  709. package/modules/sky/layers.js +0 -69
  710. package/modules/sky/look.js +0 -51
  711. package/modules/sky/lookFog.js +0 -80
  712. package/modules/sky/lookShadow.js +0 -50
  713. package/modules/sky/lookSky.js +0 -108
  714. package/modules/sky/shadow.js +0 -74
  715. package/modules/terrain/chunkLevels.js +0 -40
  716. package/modules/terrain/lookGround.js +0 -61
  717. package/modules/terrain/mountTerrain.js +0 -32
  718. package/modules/vegetation/lookWind.js +0 -37
  719. package/modules/water/look.js +0 -80
  720. package/modules/weather/look.js +0 -75
  721. package/modules/world/cells.js +0 -42
  722. package/render/lookSurface.js +0 -161
  723. package/render/postLook.js +0 -86
  724. package/render/surface.js +0 -69
  725. package/types/style.d.ts +0 -643
@@ -0,0 +1,124 @@
1
+ # Shared values, exclusive slots, time channels and wrapping module methods
2
+
3
+ A shared value is something several owners change: the game, a module, an effect. Name it, say who you are, say how you change it:
4
+
5
+ ```js
6
+ const slow = engine.mod('time.enemies', { owner: 'bullet-time', mul: 0.35, for: 2 }); // bullet time; slow.remove() ends it early
7
+ engine.mod('time.world', { owner: 'hitstop', set: 0, layer: 'system', for: 0.06 }); // decide on a higher layer: hides everything below
8
+
9
+ const storm = () => ({ name: 'storm', type: 'rules', install(e, ctx) {
10
+ ctx.mod('time.world', { mul: 0.8, layer: 'effect' }); // inside a module: owned by it, gone when the module goes
11
+ } });
12
+
13
+ const myBag = () => ({ name: 'myBag', type: 'rules', provides: ['inventory'], install(e) { /* ... */ } }); // only one may hold 'inventory'
14
+ engine.use(myBag(), { replaces: 'inventory' }); // swap the engine's part: the old one leaves with all its changes
15
+ const shop = () => ({ name: 'shop', type: 'rules', requires: ['inventory'] }); // depend on the slot, not on a module name
16
+
17
+ engine.describe().shared['time.world']; // { value: 0.8, writer: 'default', how: 'default 1 × storm 0.8', also: [{ owner: 'storm', ... }] }
18
+ ```
19
+
20
+ What to remember:
21
+ - **Layers combine by themselves.** The game sets a base value, effects (weather, sunset, bullet time) multiply on top, the player's settings cap
22
+ it. You never pick a layer by hand unless you want to: game code and game modules write `'game'` (the default); effects pass `layer: 'effect'`.
23
+ - **`mul` / `add` to cooperate, `set` to decide.** Any number of owners can multiply and add. Two owners `set` the same value on the same layer is
24
+ an error in development that names both lines and gives three copyable fixes; copy one. Release builds let the later write win and warn once.
25
+ - **`replaces` to swap an engine part** that `provides` a slot; a second provider without it is the error `shared-taken`.
26
+ - Every handle ends with `h.remove()` (`engine.mod`, `ctx.mod`, `input.gate`, `wrap`, `freeze`, `takeCamera`); it drops only your own entry.
27
+
28
+ Only things several parties commonly write are shared values: time speeds and the calendar rate, pause / freeze / camera holders, move stats,
29
+ input gates; shadow on / quality and post parameters are on them too. Your own objects,
30
+ materials and textures are plain three.js: never registered, never a conflict. Which tool to reach for: `../CONVENTIONS.md` "Customization ladder".
31
+
32
+ ```js
33
+ engine.mod('time.player', { owner: 'haste', mul: 1.2, for: 5 }); // haste on the player
34
+ const menu = engine.freeze('pause-menu'); /* ... */ menu.remove(); // pause menu: the picture, UI and ambient uTime (wind, water, clouds) keep running; { ambient: true } stops those too
35
+ engine.input.gate('jump', { owner: 'stun', for: 1.5 }); // no jumping while stunned
36
+ engine.wrap(engine.inventory, 'add', (next, id, n = 1, o) => // a weight rule on top of the engine's inventory
37
+ carried() + weightOf(id) * n > 30 ? false : next(id, n, o), { owner: 'weight' });
38
+ ```
39
+
40
+ ## The rules in full: `engine.mod(point, { owner, set | add | mul | clamp, layer?, for?, id? })`
41
+
42
+ | Layer (low to high) | Who writes it |
43
+ |---|---|
44
+ | `engine` | the engine's own defaults and drivers |
45
+ | `game` (default) | game code, game modules |
46
+ | `effect` | temporary effects: weather, sunset fade, bullet time, haste |
47
+ | `settings` | the player's settings menu: only `clamp` / `mul` (a value point: `set: false`), so it can turn things down or off, never on |
48
+ | `system` | cutscenes, menus, photo tools (also "lock this number": a `set` here hides every layer below) |
49
+ | `debug` | debug overrides and panels |
50
+
51
+ - Inside a layer: `set`, then every `add`, then every `mul`, then every `clamp` (`{ min, max }`); the result is clamped to the point's own min / max.
52
+ - One entry per owner; the same owner writing again replaces its own entry (never a conflict). Only the owner ends it: `remove()`, or `for` seconds.
53
+ - Nothing remembers an old value: every read recomputes from the base and the entries still there, so effects end in any order without fighting.
54
+ - Development builds print one console line when a value the game `set` is shown as another number because effects changed it ("'game' set
55
+ sun 3, shown 1.8 = game 3 × weather 0.6 ... to lock the number: ..."). That is the rule working, not a bug.
56
+ - `describe().shared` has one row per point with entries: `value`, `writer` (whose value the rest acts on; `'default'` when nobody set it),
57
+ `layer`, `at` (the code line, development builds), `how` (`'cave 180 × storm 0.6'`), `also` (everyone else; `hidden: true` when a set above hides
58
+ theirs). `engine.stacks.of('storm')` lists everything one owner changes. Per-record points read `'move.speed[player]'`.
59
+ - A point holds a value and writes no three.js by itself: whoever reads it writes three.js. A value the engine writes on its own shows in
60
+ `describe().owned`, and a direct three.js write takes it (the row says `writer: 'three (engine yielded)'`).
61
+ - Base values are yours: `player.speed = 6` is always fine; modifiers go through `engine.mod('move.speed', { owner, id: 'player', mul })`.
62
+ - Your own point: `engine.stacks.define('damageTaken', { per: true, min: 0 })`, then `engine.stacks.get('damageTaken').value(enemyId)`;
63
+ `kind: 'value'` holds a color, a string or a flag (set only), `kind: 'one'` is an exclusive slot (what `provides` makes).
64
+ - Saved: `engine` to `effect` entries. Not saved: `settings` (the settings module re-applies the player's choices), `system`, `debug`.
65
+
66
+ ## Picture points follow the same rule
67
+ `shadows.enabled`, `post.<name>.enabled`, `sky.tint`, `sky.flash` and `ground.cover` are shared points like the rest: the game sets a base value, effects multiply on top, the player's settings cap it.
68
+ `sky.tint`, `ground.cover` (blend by amount) and `sky.flash` (adds) are written with `engine.mod`.
69
+ ```js
70
+ const hold = engine.mod('shadows.enabled', { owner: 'cutscene', layer: 'system', set: false }); // decides over the game and the menu
71
+ engine.describe().shared['shadows.enabled']; // who decided and who is hidden
72
+ hold.remove(); // the handle ends yours
73
+ ```
74
+ - The exact number whatever else is stacked: set on `{ layer: 'system' }`, or write the three.js value.
75
+ - The settings menu only restricts (`shadows` off is `shadows.enabled` set false on the settings layer); a game's `enabled: true` does not
76
+ switch them back on, and a cutscene's `false` is not undone by the menu.
77
+ - A direct three.js write (`engine.lights.hemi.intensity = 0.2`) still works, and describe tags it `three`.
78
+
79
+ ## Time channels
80
+
81
+ - `engine.onFixed(dt => moveEnemies(dt), { channel: 'enemies' })`: dt is that channel's scaled dt. Without `channel` dt is real (1/60): cooldowns
82
+ and UI timers are not slowed by bullet time by accident.
83
+ - `engine.characters.createPlayer` drives the player on channel `'player'`; `new Character(engine, { channel })` picks one for other characters.
84
+ - `engine.world.simulate('slime', { near, far, channel: 'enemies' })`: near and far dt follow the channel too (NPCs: their own channel).
85
+ - `engine.clock.rate` is the calendar speed (fast-forward a day), separate from the channels.
86
+ - One time source: `engine.time.tick` is the only counter; `engine.time.now('real' | 'ambient' | 'calendar' | channel)` are read-only views of it
87
+ (recipe 06 "The time table"). Passing time is `engine.skip({ seconds })`: every view moves as if stepped, timers fire in order, far NPCs and
88
+ the calendar catch up. Writing `engine.clock.now` throws (it is a view).
89
+ - Timers: `engine.clock.after(s, fnOrEvent, data, { channel })` / `clock.every(s, fn, { channel })` on any view (default `'calendar'`; `'real'`
90
+ ignores bullet time; `'enemies'` slows with it). One queue: `remove()` takes effect at once, even later in the same step.
91
+ - `character.velocity` / `character.vy` are not stacks: a write is an impulse the mover integrates from (no owner, nothing to hand back). Lasting
92
+ knockback is `character.push(vx, vz, seconds)`; full takeover is `character.mover = 'manual'`.
93
+
94
+ ## Fixed-step order: `onFixed(fn, { phase })`
95
+
96
+ `'early'` (default) runs before every module (the player has not moved yet this step); `'late'` runs after all modules and before the clock (hit
97
+ checks that need this step's player position). The order never depends on which line registered first.
98
+
99
+ ## Wrapping a module method: `engine.wrap(target, method, (next, ...args) => ...)`
100
+
101
+ - Before: do something, then `return next(...args)`. After: `const r = next(...args); ...; return r`. Around: change the args, or skip `next`.
102
+ - Several wrappers compose: the last one wrapped runs first. `handle.remove()` takes one out; the method is restored when the last goes.
103
+ - Every caller of that method goes through it (the game, the module's own ops, other modules). It wraps one method: a rule for every way items
104
+ enter the bag wraps `add` and `transfer`.
105
+ - Only public methods (the module's `contract.js` `api` list); `_private` names throw. `describe().wraps` lists `{ 'inventory.add': [owners] }`.
106
+
107
+ ## Replacing a module
108
+
109
+ Two modules that each want to be "the" one (a sky, a tone-mapping stage, an inventory) declare an exclusive slot: `provides: ['inventory']`.
110
+ Other modules `requires: ['inventory']` and work with whichever holds it. `engine.use(myInventory(), { replaces: 'inventory' })` unloads the
111
+ holder (its tracked handles, its `ctx.mod` entries, its `dispose`) before the new install, and puts it back if that fails.
112
+
113
+ Each replaceable module has a contract (`modules/gameplay/inventory.contract.js`, `interactions.contract.js`, `dialogue.contract.js`, `quests.contract.js`, `modules/flow/contract.js`,
114
+ `modules/characters/contract.js`, `modules/calendar/contract.js`, `modules/camera/contract.js`; object contract `modules/characters/vehicle.contract.js`): the api, fields, ops, events
115
+ and save keys other code relies on, and nothing more (the engine's callers use only that surface). `runContract(contract, () => myInventory(), { check })`
116
+ from `@threedog/tools/contract` checks a replacement (in a game installed from npm too); `tests/contract/fakeInventory.js` in the engine's
117
+ repository (not shipped) is a whole replacement in ~40 lines; `fakeDialogue.js`, `fakeQuests.js`, `fakeCamera.js` and `fakeVehicle.js`
118
+ are the same for their contracts, and the engine's NPCs, timeline, content loader, hud and characters run on them (test `tests/specs/ui-nav.mjs`).
119
+ A camera module's `prepare` hook: `runContract(contract, () => myCamera(), { check, prepare: e => { e.camera = ... } })` adds what it needs to the
120
+ small engine. A vehicle is checked as an object: `runContract(vehicleContract, () => myBoat, { check })`.
121
+
122
+ ## Who holds what
123
+
124
+ `describe().holds`: pause / freeze / camera / debugView, `input` (pushed contexts with owners), `gates`. Every other shared value and slot: `describe().shared`.
@@ -0,0 +1,303 @@
1
+ # WebGPU and your own TSL materials
2
+
3
+ **In this recipe**: WebGPU or WebGL · Turning it on, fallbacks · A minimal TSL material · The engine's values as nodes · The engine's shader
4
+ functions · Patches with a `tsl` field (lighting hooks) · Sky and fog hooks · Post effects as nodes · A consistent style · Mistakes, each with the fix · GLSL to TSL
5
+
6
+ On the WebGPU route a game draws with three.js node materials (`MeshStandardNodeMaterial` ...) and writes shaders in TSL, three's shading
7
+ language in JavaScript. Learn TSL from three itself: the [TSL guide](https://github.com/mrdoob/three.js/wiki/Three.js-Shading-Language) and
8
+ the [WebGPU examples](https://threejs.org/examples/?q=webgpu) are written for exactly this renderer. The engine adds nothing you must use:
9
+ its wind, cloud shadows, season tint and the rest are ordinary TSL functions in `threedog/tsl`, called where you want them. A whole small
10
+ scene written this way: `threedog examples pull webgpu-tsl`.
11
+
12
+ Rules (correctness, not taste): `import 'threedog/tsl'` comes first in `main.js`; GLSL does not run here (`ShaderMaterial`, `RawShaderMaterial`,
13
+ `onBeforeCompile`, three.js `Pass` objects, GLSL-only patches and hooks), and each one fails by name instead of drawing nothing.
14
+
15
+ ## WebGPU or WebGL
16
+ `threedog new-game` and `threedog init` start a game on WebGPU with TSL (`main.js` imports `threedog/tsl` first and calls `createEngine({ renderer: 'webgpu' })`). `--webgl` starts it on GLSL instead. `createEngine` called without `renderer` still builds a WebGL renderer. When the browser has no WebGPU the game keeps running on three's WebGL2 backend, and `engine.describe().renderer` says why. How to decide:
17
+ - WebGPU + TSL (the default for a new game): the picture is built from TSL, you write shaders in JavaScript and compose them as functions, or start from a three.js WebGPU example, and effects that exist only as node materials are open to you.
18
+ - `--webgl` + GLSL: the game already has GLSL (ShaderMaterials, `onBeforeCompile`, a three.js `Pass`), or must run best on modest hardware where plain WebGL draws a frame fastest, or start-up time matters more than the shading style. Recipe 43 is the GLSL route for the same tasks.
19
+ - Check on the hardware you care about: `threedog inspect perf` on both routes, same scene. Measured in 2026-11 on one scene (terrain, 3000 trees, shadows, bloom): on an integrated GPU WebGPU took about 1.5 times as long as WebGL and WebGPURenderer's WebGL2 backend about 1.7 times; the first WebGPU frame came 0.6 to 1.3 s later (shader compilation).
20
+
21
+ ## Turning it on
22
+ ```js
23
+ import 'threedog/tsl'; // first line: brings three/webgpu and the node versions of the engine's sky, post chain and patches
24
+ import { createEngine } from 'threedog';
25
+
26
+ const engine = createEngine({ canvas: document.getElementById('view'), renderer: 'webgpu' });
27
+ await engine.start(); // start() initializes the renderer, so createEngine stays synchronous
28
+ console.log(engine.describe().renderer); // { requested: 'webgpu', backend: 'webgpu' | 'webgl2' | 'webgl', reason }
29
+ ```
30
+ Your own `new WebGPURenderer(...)` as `renderer` goes the same way (the engine configures only a renderer it built, recipe 50). When the
31
+ browser gives no WebGPU, `fallback` decides, and the console says once which one happened (`[threedog] WebGPU unavailable (...)`):
32
+ - `'webgl2'` (default): the same WebGPURenderer on its WebGL2 backend. Your TSL keeps working; frames are slower than plain WebGL.
33
+ - `'webgl'`: a `WebGLRenderer` instead. Faster, but node materials cannot draw there (`start()` fails when the scene has one). The engine's
34
+ own materials (terrain, vegetation, water) are plain three.js materials with both a GLSL and a tsl form of each patch, so they
35
+ draw on either renderer with the same picture. What engine modules built before `start()` is rebuilt for WebGLRenderer there: the post chain,
36
+ the sky dome (with the `engine.sky.clouds` / `layer` hooks set so far), fog, cascaded shadows, particles, rain and snow. Your own objects are not:
37
+ build node materials after `start()` when `engine.describe().renderer.backend !== 'webgl'`.
38
+ - `'error'`: `start()` rejects; the game shows its own message.
39
+
40
+ ## A minimal TSL material
41
+ ```js
42
+ import * as THREE from 'three';
43
+ import { MeshStandardNodeMaterial } from 'three/webgpu';
44
+ import { color, positionWorld, sin } from 'three/tsl';
45
+
46
+ const mat = new MeshStandardNodeMaterial({ roughness: 0.5 });
47
+ mat.colorNode = color(0x4080ff).mul(sin(positionWorld.y.mul(4)).mul(0.25).add(0.75)); // bands by world height
48
+ engine.add(new THREE.Mesh(new THREE.TorusKnotGeometry(0.6, 0.2, 128, 16), mat));
49
+ ```
50
+ A node material is lit, shadowed and fogged by three like its plain twin; you replace only the inputs you set (`colorNode`, `positionNode`,
51
+ `normalNode`, `emissiveNode`, `roughnessNode`, `opacityNode` ...).
52
+
53
+ ## Node names games use
54
+ Everything is imported from `three/tsl`; a name you do not find here is almost always there too (`Object.keys(await import('three/tsl'))` lists them, `node_modules/three/src/nodes/` has the source).
55
+ - Values: `float`, `vec2`, `vec3`, `vec4`, `color`, `uniform`, `texture`, `attribute`, `varying`.
56
+ - Where am I: `positionLocal`, `positionWorld`, `positionGeometry`, `normalLocal`, `normalWorld`, `normalView`, `uv`, `screenUV`, `cameraPosition`, `instanceIndex`, `vertexColor`, `time`.
57
+ - Math: `sin`, `cos`, `abs`, `fract`, `floor`, `pow`, `length`, `dot`, `normalize`, `min`, `max`, `clamp`, `mix`, `step`, `smoothstep`, `oneMinus`, `select`; chain the operators as methods (`.add`, `.mul`, `.sub`, `.div`).
58
+ - Noise: `mx_noise_float`, `mx_fractal_noise_float` (MaterialX, also `_vec3`).
59
+ - Your own function and branches: `Fn`, `If`, `Loop`.
60
+
61
+ ## The engine's values as nodes: `uniforms(engine)`
62
+ `uniforms(engine)` returns `engine.uniforms` as TSL nodes with the same names: `uTime`, `uWind`, `uCloud`, `uSunDir`, `uSunColor`, `uDaylight`
63
+ (0 at night, 1 at noon), `uTimeOfDay`, `uFoliageTint`, `uGroundTint` ... (the list: `uniforms` in [the engine API](../api/engine.md)). The node reads the
64
+ engine's value each render, so weather, the calendar and the sky move your material with no per-frame code.
65
+ ```js
66
+ import * as THREE from 'three';
67
+ import { MeshStandardNodeMaterial } from 'three/webgpu';
68
+ import { color, float, sin, smoothstep, uniform } from 'three/tsl';
69
+ import { uniforms } from 'threedog/tsl';
70
+
71
+ const u = uniforms(engine);
72
+ const glow = uniform(3); // a value of your own: change glow.value from game code (an op, a quest step)
73
+ const lantern = new MeshStandardNodeMaterial({ color: 0x3a2a18 });
74
+ const night = smoothstep(0.3, 0.9, float(1).sub(u.uDaylight));
75
+ lantern.emissiveNode = color(0xffa040).mul(night.mul(glow)).mul(sin(u.uTime.mul(7)).mul(0.08).add(0.92)); // lights up at dusk, flickers
76
+ engine.add(new THREE.Mesh(new THREE.OctahedronGeometry(0.3), lantern));
77
+ ```
78
+
79
+ ## The engine's shader functions: `threedog/tsl`
80
+ Each is a TSL `Fn` with the same arguments and meaning as its GLSL twin in recipe 43, reading the same `engine.uniforms`; full signatures are
81
+ in the package's `types/tsl.d.ts`. Call them on nodes, put the result where it belongs:
82
+
83
+ | Function | Returns | Typical place |
84
+ |---|---|---|
85
+ | `wind(positionGeometry, objectOrigin(), amplitude)` | object-space offset (vec3) | `positionNode = positionLocal.add(...)`: grass, leaves, flags, hanging cloth |
86
+ | `objectOrigin()` | world origin of the object or instance (vec3) | a per-object phase or random seed |
87
+ | `cloudShadow(positionWorld)` | 0-1 light factor under drifting clouds | multiply into the color or the direct light |
88
+ | `season(color, 0 \| 1)` | the color moved by the calendar's tint (0 foliage, 1 ground) | `colorNode` |
89
+ | `detailNoise(p, kind, octaves)` / `detailBump(...)` | gray noise + height / a bumped normal | breaking up flat color; `normalNode` |
90
+ | `wrapDiffuse`, `shadowTint`, `rim` | light to add | inside a lighting hook (next section) |
91
+ | `celLighting` | a lighting model | `mat.setupLightingModel = celLighting` on a `MeshToonNodeMaterial` ("A consistent style" below) |
92
+ | `waterWave`, `waterSurface(spec)`, `waterFresnel` | wave height / surface color / fresnel blend | a water material of your own |
93
+
94
+ ```js
95
+ import * as THREE from 'three';
96
+ import { MeshLambertNodeMaterial } from 'three/webgpu';
97
+ import { color, float, mix, positionGeometry, positionLocal, positionWorld, uv } from 'three/tsl';
98
+ import { wind, objectOrigin, cloudShadow, season } from 'threedog/tsl';
99
+
100
+ const flag = new MeshLambertNodeMaterial({ side: THREE.DoubleSide });
101
+ flag.colorNode = season(mix(color(0x803020), color(0xd06040), uv().x), 0).mul(cloudShadow(positionWorld));
102
+ flag.positionNode = positionLocal.add(wind(positionGeometry, objectOrigin(), float(0.4))); // its shadow sways too
103
+ const mesh = new THREE.Mesh(new THREE.PlaneGeometry(1.2, 0.8, 12, 8).translate(0.6, 2.2, 0), flag);
104
+ mesh.castShadow = true;
105
+ engine.add(mesh);
106
+ ```
107
+ three's shadow pass draws with the material's `positionNode`, so whatever moves vertices there moves their shadow: no depth material to keep
108
+ in step. With an `InstancedMesh` whose instances are rotated or scaled, the offset must turn with each instance; the patch form below does that.
109
+
110
+ ## Patches with a `tsl` field (optional convenience)
111
+ `applyEffects(mat, { wind, season, clouds, patches })` (recipe 43) works on node materials: the engine's own patches carry a TSL version, and a
112
+ patch of yours gives `tsl: (ctx) => node` beside or instead of `glsl`. Same slots, same order and conflict rules; `ctx` holds the slot's value
113
+ so far (`ctx.color`, `ctx.position`, `ctx.normal` ...), `ctx.tsl` (three/tsl) and `ctx.fx` (the `threedog/tsl` functions). `ctx.position`
114
+ is object space before instancing, as in GLSL, so an instanced mesh's offset turns with each instance by itself. Return the new value. A patch that has only `glsl` stops with `patch 'x' has no tsl version`.
115
+ ```js
116
+ import * as THREE from 'three';
117
+ import { MeshLambertNodeMaterial } from 'three/webgpu';
118
+ import { applyEffects } from 'threedog';
119
+
120
+ const leaves = new MeshLambertNodeMaterial({ color: 0x88aa55 });
121
+ applyEffects(leaves, { wind: 0.3, season: 'foliage', patches: [
122
+ // darker toward the ground: ctx.color is the vec4 surface color with the map and vertex colors applied
123
+ { name: 'heightShade', slot: 'color', tsl: ({ color, tsl }) => tsl.vec4(color.rgb.mul(tsl.positionWorld.y.mul(0.25).add(0.5).clamp(0, 1)), color.a) },
124
+ ] });
125
+ engine.add(new THREE.Mesh(new THREE.BoxGeometry(0.6, 3, 0.6, 1, 10, 1).translate(0, 1.5, 0), leaves));
126
+ ```
127
+ The `lighting` slot edits light after three summed it: `tsl: (ctx) => ({ directDiffuse, directSpecular, indirectDiffuse, indirectSpecular })`
128
+ (any of the four, as nodes). For what GLSL reads in `lightsPars` (the sun's direction, its shadow alone), give hooks instead: `start(ctx)` runs
129
+ before the lights, `light(light, ctx)` may return a new color node for one light before three lights the surface with it (the
130
+ engine's cloud shadow dims the sun this way; return nothing to leave a light alone), `direct(light, ctx)` once per light (`light.direction`, `light.color` with the shadow in it, `light.shadow` 1 lit / 0 shadowed
131
+ or null, `light.directional`, `light.light`; what it adds to `ctx.directDiffuse` ... joins the summed light at the patch's place in the
132
+ list, as on WebGL), `after(ctx)` at the end; `ctx.state` is shared between them. The `if` in `direct` is
133
+ plain JavaScript run while the shader is built (which light this is), not a per-pixel branch.
134
+ ```js
135
+ import * as THREE from 'three';
136
+ import { MeshStandardNodeMaterial } from 'three/webgpu';
137
+ import { applyEffects } from 'threedog';
138
+
139
+ const coolShade = { name: 'coolShade', slot: 'lighting', tsl: {
140
+ start: c => { c.state.lit = c.tsl.float(1).toVar('coolShadeLit'); },
141
+ direct: (light, c) => { if (light.directional && light.shadow) c.state.lit.assign(light.shadow); },
142
+ after: c => ({ indirectDiffuse: c.indirectDiffuse.mul(c.tsl.mix(c.tsl.vec3(0.8, 0.9, 1.25), c.tsl.vec3(1), c.state.lit)) }), // bluer fill in shadow
143
+ } };
144
+ const rock = applyEffects(new MeshStandardNodeMaterial({ color: 0x9a9080 }), { patches: [coolShade] });
145
+ engine.add(new THREE.Mesh(new THREE.DodecahedronGeometry(1), rock));
146
+ ```
147
+
148
+ ## Half-transparent surfaces and fog differ from plain WebGL
149
+ WebGL straight to the canvas tone-maps and encodes each material first, then applies fog and blends in display values. WebGPU, and WebGL with any `engine.post` effect on, fog and blend in linear and tone-map once. Those two match pixel for pixel, opaque unfogged surfaces match on all three, and half-transparent or fogged ones shift on the straight WebGL path, more in dim scenes. So on WebGL turning the first post effect on can move those surfaces a little. This is how three.js's renderers work, not a fault to fix in your scene.
150
+
151
+ ## Sky and fog hooks
152
+ The engine's sky dome has a node version; nothing changes in how you set it (recipe 08). The GLSL hooks (`engine.sky.clouds`, `engine.sky.layer`
153
+ clouds and layers) take `{ glsl, tsl }` on this route: see 52's [tsl forms](52-around-the-picture.md#tsl-forms).
154
+ A sky of your own: three's `SkyMesh` (`three/addons/objects/SkyMesh.js`) through `engine.sky.replace`.
155
+
156
+ ## Post effects as nodes
157
+ On WebGPURenderer the post chain is one three.js `RenderPipeline` (`engine.post.object()` returns `{ pipeline, scenePass }`). The slots, order
158
+ and ownership rules of recipe 52 are unchanged; an effect gives `node: (input, ctx) => color` instead of `create`, and a fused snippet gives
159
+ `fuse.tsl: (color, uv, ctx) => color` beside its `fuse.glsl`. `ctx.p` holds a live uniform per number or color param (so `handle.set()` does
160
+ not rebuild), `ctx.tsl` is three/tsl, `ctx.scenePass` the scene pass (depth: `ctx.scenePass.getTextureNode('depth')`). The effects of
161
+ `@threedog/post-effects` run on both routes as they are. An effect that reads depth, with a GLSL pass and a TSL node in one object
162
+ (volumetric fog, god rays): [recipe 59](59-depth-post-effects.md).
163
+ ```js
164
+ engine.post.add({ name: 'warmGrade', slot: 'ldr', params: { amount: 0.3 },
165
+ node: (input, { tsl, p }) => tsl.vec4(tsl.mix(input.rgb, input.rgb.mul(tsl.vec3(1.05, 0.95, 0.8)), p.amount), input.a) });
166
+ engine.post.get('warmGrade').set({ amount: 0.5 }); // a uniform write, no rebuild
167
+ ```
168
+ `ldr` runs after tone mapping (display colors 0-1); something that should glow or bleed belongs in `hdr`. An effect with only `create`
169
+ (a three.js `Pass`) stops with its name.
170
+
171
+ ## A consistent style
172
+ A style reads as one when every surface answers the same few questions the same way. Decide these once, in one place of `picture/`, and let materials share the answers; a
173
+ look built material by material drifts. Working example: `examples/techniques/three-looks/` (page `/technique-looks.html`), the same models three ways.
174
+
175
+ | Question | Where it lives | With |
176
+ |---|---|---|
177
+ | Where does light turn into shade, how hard, what color is the shade | the lighting model; `colorNode` runs before lighting and sees no light | three's PBR with `wrapDiffuse` / `shadowTint` in a hook; `MeshToonNodeMaterial` + `gradientMap` (`rampTexture`); `celLighting` for colored bands |
178
+ | Do shapes have lines | `hdr`, before the tone curve | `outline()`, or three's `toonOutlinePass` |
179
+ | What moves | `positionNode`; the shadow follows, `castShadowPositionNode` only when it should differ | `wind()`, any node |
180
+ | What fills the air | the scene | `scene.fog`, or `scene.fogNode = fog(color, factor)` |
181
+ | What does the tone curve do | the renderer, once | `renderer.toneMapping`; `NoToneMapping` when the colors you wrote are the colors you want |
182
+ | What is the finished picture's color | `ldr`: display colors after the tone curve, never tone-mapped again | `lut()`, `paletteLut()`, your own node; `grayscale`, `posterize`, `saturation`, `hue` from three/tsl |
183
+
184
+ ```js
185
+ import { MeshToonNodeMaterial } from 'three/webgpu';
186
+ import { rampTexture } from 'threedog';
187
+ import { celLighting } from 'threedog/tsl';
188
+ const ramp = rampTexture([0x7a84c8, 0xd0d4f0, 0xffffff]); // example colors: the light in each band, dark to light, from the game's palette; one ramp for the whole game
189
+ const mat = new MeshToonNodeMaterial({ color: 0xe08050, gradientMap: ramp });
190
+ mat.setupLightingModel = celLighting; // colored bands; a cast shadow reads the dark end of the ramp, so it is the same flat color as the side turned away
191
+ ```
192
+ three's toon alone is enough for gray bands: it reads only the red channel of `gradientMap`, and a cast shadow drops to the ambient light. Choose it when that is the picture you
193
+ want; `celLighting` when shade has a color or cast shadows belong to the bands. The ramp's width sets where bands fall, the left half being the side turned away.
194
+
195
+ **Same tools, different pictures**: a paper cut-out is two bands and no line; ink wash is a gray palette through `paletteLut` with soft fog; a neon night is a dark ramp with `bloom` and
196
+ emissive lines; a storybook is soft ramp edges (`rampTexture(ramp, soften)`) with a warm grade; a retro handheld is a four-color `paletteLut` with `posterize` before it.
197
+
198
+ **How to judge it**, at the game's real camera (`threedog shot /your-page --gpu`):
199
+ - Look at the picture in gray. Do the shapes still separate? Color cannot rescue forms that have the same value.
200
+ - Cover the object you just changed. If the rest of the frame tells you the same story about light, the style is holding; if one material looks lit by another sun, its answers differ.
201
+ - Is every shade in the frame leaning the same way? A cast shadow and a turned-away side that disagree read as two renderers.
202
+ - A flat look with soft or stair-stepped shadow edges: the shadow map is showing. Raise its resolution for the area in view, or soften the ramp a little.
203
+ - Count what the picture really holds, not what you meant: most common colors, saturation, light-dark spread (`window.techTest.measure` in the example is one way). A palette look with
204
+ many stray colors has something running after the `lut`; grain and vignette belong before it there.
205
+ - Put an effect in the stage that matches what it reads. A grade written for finished colors looks wrong before the tone curve, and a glow after it stops glowing.
206
+
207
+ ## Mistakes, each with the fix
208
+ Each block shows the mistake as a comment and the working line under it.
209
+ ```js
210
+ import * as THREE from 'three';
211
+ import { MeshBasicNodeMaterial } from 'three/webgpu';
212
+ import { color, mix, uv } from 'three/tsl';
213
+
214
+ // wrong: new THREE.ShaderMaterial({ fragmentShader }) or mat.onBeforeCompile = fn; WebGPURenderer never runs either (start() names the mesh)
215
+ const sign = new MeshBasicNodeMaterial();
216
+ sign.colorNode = mix(color(0x203060), color(0xf0c080), uv().y);
217
+ engine.add(new THREE.Mesh(new THREE.PlaneGeometry(2, 1), sign));
218
+ ```
219
+ An sRGB picture as `scene.background` is tone-mapped on both routes and with or without post effects (the engine's frame matches
220
+ WebGPURenderer, which tone-maps the whole frame once). Plain three.js's WebGLRenderer leaves an sRGB background out of tone mapping, so a
221
+ `renderer.render(scene, camera)` of your own on WebGL shows it brighter than the engine's frame.
222
+ ```js
223
+ import * as THREE from 'three';
224
+ import { MeshLambertNodeMaterial } from 'three/webgpu';
225
+ import { float, positionGeometry, positionLocal } from 'three/tsl';
226
+ import { wind, objectOrigin } from 'threedog/tsl';
227
+
228
+ const grass = new MeshLambertNodeMaterial({ color: 0x6a8a3a });
229
+ // wrong: wind(positionLocal, ...); positionLocal is after the instance transform, so every blade sways by its height in the field
230
+ grass.positionNode = positionLocal.add(wind(positionGeometry, objectOrigin(), float(0.3)));
231
+ engine.add(new THREE.InstancedMesh(new THREE.PlaneGeometry(0.05, 0.4, 1, 3).translate(0, 0.2, 0), grass, 10));
232
+ ```
233
+ ```js
234
+ import * as THREE from 'three';
235
+ import { MeshStandardNodeMaterial } from 'three/webgpu';
236
+ import { color, select, uv, vec3 } from 'three/tsl';
237
+ import { uniforms } from 'threedog/tsl';
238
+
239
+ const u = uniforms(engine), mat = new MeshStandardNodeMaterial();
240
+ // wrong: color(0xff0000).add(uv()); vec3 + vec2 gives no error, a channel is dropped or padded silently. Make the types match:
241
+ mat.colorNode = color(0xff0000).mul(0.5).add(vec3(uv(), 0).mul(0.5));
242
+ // wrong: u.uDaylight.value < 0.5 ? a : b, or u.uTime * 2; JavaScript runs once while the shader is built (and a node times a number is NaN)
243
+ mat.emissiveNode = select(u.uDaylight.lessThan(0.5), color(0xffaa44), color(0x000000)).mul(u.uTime.mul(2).sin().mul(0.5).add(0.5));
244
+ engine.add(new THREE.Mesh(new THREE.SphereGeometry(0.5), mat));
245
+ ```
246
+ ```js
247
+ import * as THREE from 'three';
248
+ import { MeshBasicNodeMaterial } from 'three/webgpu';
249
+ import { Fn, Loop, float, uniform, uv, vec3 } from 'three/tsl';
250
+
251
+ // wrong: uniform(new THREE.Vector3()).onRenderUpdate(({ value }) => value.copy(p)); the callback is handed the frame, not the value: it RETURNS the new value
252
+ const camHeight = uniform(0).onRenderUpdate(() => engine.camera.position.y);
253
+ const bands = new MeshBasicNodeMaterial();
254
+ // wrong: Loop(...) (or If, or x.addAssign) at the top level, outside an Fn: nothing holds the statement, three drops it with NO error and
255
+ // the sum stays 0. The engine warns in dev for a patch's or post effect's code; for your own material nothing tells you
256
+ bands.colorNode = Fn(() => {
257
+ const sum = float(0).toVar();
258
+ Loop(4, ({ i }) => { sum.addAssign(uv().x.mul(float(i).add(1)).fract().mul(0.25)); });
259
+ return vec3(sum).mul(camHeight.mul(0.1).clamp(0.5, 1));
260
+ })();
261
+ engine.add(new THREE.Mesh(new THREE.PlaneGeometry(2, 1), bands));
262
+ ```
263
+ ```js
264
+ import * as THREE from 'three';
265
+ import { RenderTarget } from 'three/webgpu';
266
+
267
+ // wrong: your own requestAnimationFrame loop calling engine.renderer.render(...); the engine runs the only loop and its post chain
268
+ const minimap = new RenderTarget(256, 256), top = new THREE.OrthographicCamera(-20, 20, 20, -20, 0.1, 100);
269
+ top.position.set(0, 50, 0); top.lookAt(0, 0, 0);
270
+ engine.onFrame(() => engine.renderOnce(minimap, top)); // one extra picture per frame; a fresh node frame each call
271
+ ```
272
+ ```js
273
+ import { RenderPipeline } from 'three/webgpu';
274
+
275
+ // wrong: new PostProcessing(renderer) from older three examples (three 0.187 names it RenderPipeline), and a second pipeline beside the engine's
276
+ console.log(engine.post.object()?.pipeline instanceof RenderPipeline); // the engine's own, once its chain is built; add effects with engine.post.add
277
+ ```
278
+ Also: three's `uniform` takes a value, not a `{ value }` object, so passing it `engine.uniforms.uWind` throws; use `uniforms(engine).uWind`.
279
+ A type mismatch never errors: look at the result (`engine.debug.view('lighting')`, `threedog shot`).
280
+ Many materials, many programs: WebGPURenderer builds one program per distinct set of patch uniform objects. Materials given the same
281
+ `detail` / `lighting` options through `applyEffects` share theirs, so a hundred stones build one; a patch you write once and pass
282
+ to every material shares too, a fresh patch object per material builds one program each.
283
+
284
+ ## GLSL to TSL
285
+ | GLSL | TSL (`three/tsl`) |
286
+ |---|---|
287
+ | `float x = 1.0;` / `vec3(1.0, 0.5, 0.0)` | `float(1)` / `vec3(1, 0.5, 0)`, `color(0xff8000)` for a hex color |
288
+ | `a + b`, `a * b`, `-a`, `1.0 - a` | `a.add(b)`, `a.mul(b)`, `a.negate()`, `a.oneMinus()` |
289
+ | `x += y;` | `x.addAssign(y)` (x made with `.toVar()`) |
290
+ | `mix(a, b, t)`, `smoothstep(e0, e1, x)`, `clamp(x, 0.0, 1.0)` | `mix(a, b, t)`, `smoothstep(e0, e1, x)`, `x.clamp(0, 1)` |
291
+ | `uniform float uAmount;` | `const uAmount = uniform(0.5)`, changed by `uAmount.value = 1` |
292
+ | the engine's `uTime`, `uWind`, `uDaylight` | `uniforms(engine).uTime` ... (the clock every engine effect reads, not three's own `time`) |
293
+ | `varying vec3 vPos;` | built in: `positionWorld`, `normalView`...; your own: `node.toVarying()` |
294
+ | `vec4 c = texture2D(map, vUv);` | `const c = texture(map, uv())` |
295
+ | `gl_FragCoord.xy` | `screenCoordinate.xy` (pixels), `screenUV` (0-1) |
296
+ | `position`, `normal`, `uv` attributes | `positionGeometry`, `normalGeometry`, `const st = uv()` |
297
+ | moving vertices (`transformed`, `gl_Position`) | `material.positionNode` (object space), `vertexNode` (clip space) |
298
+ | `gl_FragColor = ...` | `colorNode` (lit surface color), `emissiveNode`, `outputNode` (final color) |
299
+ | `if (c) {...} else {...}` | `const v = select(c, a, b)`; inside `Fn`, `If` / `Else` blocks |
300
+ | `for (int i = 0; i < 4; i++)` | `Loop` with a count and a callback, inside `Fn` |
301
+ | `float f(vec3 p) {...}` | `const f = Fn(([p]) => ...)` |
302
+ | `discard;` | `Discard` inside `Fn`, or `alphaTest` with `opacityNode` |
303
+ | `#include <threedog_wind>`, `threedogWind(...)` | `import { wind } from 'threedog/tsl'`, `wind(...)` |
@@ -0,0 +1,104 @@
1
+ # Who steers a character: engine.control
2
+
3
+ Every character (the player, an NPC, a hand-built `Character`) has one control slot. One owner is in charge of it at a time, and only that
4
+ owner moves the character. Take it, steer, give it back:
5
+
6
+ ```js
7
+ // a quest walks the guard to the gate; its schedule waits and carries on afterwards
8
+ const escort = engine.control.take(guard, { owner: 'escort', layer: 'script', step: (dt, ch) => ({ x: 0, z: 1 }) });
9
+ escort.remove();
10
+
11
+ // a roll: the keys pause for 0.4 s, then come back by themselves
12
+ const roll = engine.control.take(engine.player, { owner: 'roll', layer: 'script', step: () => ({ x: 0, z: 1, run: true }) });
13
+ engine.clock.after(0.4, () => roll.remove());
14
+
15
+ // no step: move it from your own code; move() does nothing while someone above holds it
16
+ const h = engine.control.take(drone, { owner: 'ai', layer: 'script' });
17
+ engine.onFixed(dt => h.move(dir.x, dir.z, false, dt));
18
+
19
+ engine.control.of(engine.player); // { owner: 'timeline', layer: 'cutscene', waiting: [{ owner: 'walkTo', layer: 'script' }, { owner: 'player-input', layer: 'base' }] }
20
+ engine.describe().control; // the same for every held character, by id
21
+ ```
22
+
23
+ What to remember:
24
+ - **Layers, lowest first**: `base` (the character's own driver: the player's keys, an NPC's behaviour) < `script` (walkTo, quest scripts, AI
25
+ orders; the default) < `cutscene` (timeline) < `net` (another machine drives it). The highest holder is in charge.
26
+ - **A higher holder pauses the lower one, it does not cancel it.** When the cutscene ends, the walk it interrupted goes on; when the walk
27
+ arrives, the keys work again. Nobody has to remember what to restore.
28
+ - **One owner per layer.** Two different owners on the same layer is `[control-conflict]` in development, naming both: end the old one first
29
+ (`h.remove()`), or take another layer. The same owner taking again just replaces its own hold.
30
+ - **A held character moves only through its holder.** `character.move()` from anywhere else is `[control-bypass]` in development: the bug where
31
+ keys, a walk and a cutscene all pushed one body in the same step and the last one won.
32
+ - **`step(dt, ch)`** returns `{ x, z, run }` (world direction, length 0-1 scales the speed), `null` (stand still: gravity and the idle pose go on)
33
+ or `false` (do not move this step: you placed it yourself). `dt` is the character's time channel, so bullet time slows it too.
34
+ - **Inside a module** use `ctx.control.take(ch, { ... })`: the owner defaults to the module, and everything it holds is handed back when the
35
+ module goes (unloaded, rolled back, replaced).
36
+
37
+ ## Who takes what
38
+
39
+ | Driver | Layer | Owner | Notes |
40
+ |---|---|---|---|
41
+ | `createPlayer({ control })` | base | `'player-input'` | `control` is `'camera'` or `(player, input, dt) => ({ x, z, run })`; jump and driving live here too |
42
+ | NPC behaviour (schedule, wander, your own) | base | `'npcs'` | paused near and far while anything above holds the NPC; kept by NPC id, so it survives the model being rebuilt |
43
+ | `character.walkTo(target)` | script | `'walkTo'` | paused under a cutscene (no progress, no stuck clock), resumes after |
44
+ | `timeline.play(steps, { actors })` | cutscene | `'timeline'` | `actors` defaults to `[engine.player]`; `[]` drives nobody; a `path` step adds its character. While the player is an actor, the `'cutscene'` input context is up |
45
+ | `net()` | net | `'net'` | a client holds every character the host drives; the host holds each peer's avatar. Taking one is `[control-net]`: change it with an op the host runs |
46
+
47
+ Two cutscenes running at once are one owner (`'timeline'`), counted per character, so they never conflict; the slot goes back when the last
48
+ one using that character ends.
49
+
50
+ ## Control is not the body
51
+
52
+ **Control** decides where to go. **The mover** (`character.mover = 'simple' | 'rapier' | 'manual'`) decides how the body gets there: integration,
53
+ collision, gravity. They never mix: a cutscene can take a character whose mover is `'rapier'`, and a `'manual'` mover can be steered by the
54
+ player's input. `mover: 'manual'` is for a game that writes the position itself; it still needs someone in control to call `move()` each
55
+ step (a `control` function returning `null` does).
56
+
57
+ ## Moved by script: ride a boat, a lift, an elevator, a ladder
58
+
59
+ Move the player by script in one of three ways. Pick by what the player may still do.
60
+
61
+ | Case | Use | The player |
62
+ |---|---|---|
63
+ | A floor that moves: lift, elevator, raft, moving platform | `characters.support(deck)` (recipe 32, Platforms) | keeps walking and jumping, is carried |
64
+ | A ride on a route: boat, cable car, being carried up stairs | `engine.control.take` with `step: () => false` | steers nothing, gravity is off, your code places it |
65
+ | Climbing: ladder, rope, wall | `mover: 'manual'` plus the `states` option | moves on the axis you write, the climb clip plays |
66
+
67
+ ```js
68
+ // a ride: keys and gravity stop while the hold lasts; the seat is a point in the boat's own space
69
+ const seat = new THREE.Vector3(0, 0.6, 0);
70
+ const ride = engine.control.take(engine.player, { owner: 'boat', layer: 'script', step: (dt, ch) => {
71
+ boat.updateWorldMatrix(true, false);
72
+ boat.localToWorld(ch.root.position.copy(seat));
73
+ ch.syncRecord();
74
+ return false; // placed by hand this step
75
+ } });
76
+ ```
77
+ To lock the player's input during a scripted ride, one line: `const lock = engine.input.push('ride');` and `lock.remove()` at the end. `engine.characters.support(deckMesh)` works for a boat on water: pass the deck mesh, and the player stands on it and rides it wherever it moves, with no ground needed under it.
78
+ Gravity comes back the moment the hold ends, so end a ride where the character can stand: `player.placeAt(pier)` then `ride.remove()`, both in a
79
+ fixed step or an op. The camera and saves follow the position as usual.
80
+
81
+ ```js
82
+ // a ladder (the landing point must be ground or a registered platform, engine.characters.support; anywhere else it drops to what is below): createPlayer({ clips: { climb: 'Climb' }, states: () => climbing ? 'climb' : null }), then call this when the player uses the ladder
83
+ let climbing = false;
84
+ function climb(player, top) { // top: { x, y, z }, where it steps off, on ground or a registered platform
85
+ climbing = true; player.mover = 'manual'; // no gravity, the position is yours
86
+ const h = engine.control.take(player, { owner: 'ladder', layer: 'script', step: (dt, ch) => {
87
+ const up = (engine.input.down('moveForward') ? 1 : 0) - (engine.input.down('moveBackward') ? 1 : 0);
88
+ ch.root.position.y += up * 2 * dt;
89
+ if (ch.root.position.y < top.y) return null; // null: the mover runs, so states picks climb
90
+ climbing = false; ch.mover = 'simple'; h.remove(); ch.placeAt(top);
91
+ return false;
92
+ } });
93
+ }
94
+ ```
95
+
96
+ ## Vehicles
97
+
98
+ Getting into a vehicle does not change who controls the character: the player's input (`'player-input'`) drives the car while seated. A
99
+ cutscene that takes the player while driving stops the steering; the rider stays on its seat.
100
+
101
+ ## Saves
102
+
103
+ Holds are not saved: they are running code (a walk, a cutscene, a quest step), and that code takes control again after a load. A walk is
104
+ cancelled by a load (`walkTo` resolves `'cancelled'`).
@@ -0,0 +1,60 @@
1
+ # Input: modules declare actions, the game binds keys
2
+
3
+ Three roles, one rule each:
4
+ - **A module declares** an action and the context it is read in, never a key: `ctx.input.action('choice1', { context: 'dialog' })`.
5
+ - **The game binds** keys: `createEngine({ bindings })`, `engine.input.bind(...)`, or a ready map from `presets.bindings`.
6
+ - **The player rebinds** in the settings menu, which calls `engine.input.rebind` and saves it.
7
+
8
+ Only the basics have default keys, the one default preset: WASD / arrows move (`moveForward` ...), `confirm` (Space / Enter / pad A),
9
+ `cancel` (Esc / pad B) and the UI cursor (`uiUp` ...). Every other key in your game is a line you wrote.
10
+
11
+ ## 1. Bind keys (the game)
12
+ ```js
13
+ import { createEngine, presets } from 'threedog';
14
+ const engine = createEngine({ canvas, bindings: {
15
+ attack: ['KeyJ', 'Mouse0', 'PadX'], // keyboard, mouse buttons (Mouse0 / 1 / 2) and gamepad buttons go in one list
16
+ ...presets.bindings.dialogNumbers, // 1-9 pick dialogue choices
17
+ ...presets.bindings.debug, // F3 debug panel, F4 trigger wireframes
18
+ } });
19
+ engine.input.bind('dodge', ['ShiftLeft']); // later, in code; returns a handle, remove() takes the keys away
20
+ engine.input.bind(presets.bindings.rtsRotate); // a whole map at once: rotateLeft Q, rotateRight R
21
+ engine.input.action('brake', { context: 'vehicle', label: 'Brake', keys: ['KeyB'] }); // your own action with a context and a label for the menu
22
+ engine.onFixed(() => { if (engine.input.pressed('attack')) swing(); }); // gameplay keys are read in the fixed step
23
+ ```
24
+ `presets.bindings`: `walk` (run / jump), `interact`, `inventory`, `quests`, `photo`, `gridRotate` (rotate), `brake`, `dialogNumbers` (choice1..9),
25
+ `rtsRotate` (rotateLeft / rotateRight), `cutsceneSkip` (skip: Backspace; cancel always skips), `debug` (debugPanel F3, triggersDebug F4). Spread one
26
+ and override a key like any object: `{ ...presets.bindings.rtsRotate, rotateLeft: ['KeyZ'] }`. No module takes a keys option: a module only declares
27
+ its actions (gone with the module), the game binds them; `[]` is deliberately no key.
28
+
29
+ ## 2. Contexts: the top one wins
30
+ Every action belongs to one context. The stack starts with `walk`; a module or the game pushes others: `vehicle` (pass: unrelated `walk` actions
31
+ keep working), `dialog`, `menu`, `cutscene` (opaque: nothing below reads). The top context wins a key both bind.
32
+ ```js
33
+ const car = engine.input.push('vehicle', { pass: true, owner: 'car' }); // brake (context vehicle) reads now; car.remove() pops it
34
+ engine.describe().input // { stack: ['walk', 'vehicle(owner: car)'], unbound: [], rebound: { attack: ['KeyK'] } }
35
+ ```
36
+ Details of switching (next fixed step, no stuck keys, focus loss): recipe 07 §2b.
37
+
38
+ ## 3. In a module: declare, read, leave
39
+ ```js
40
+ const sprint = () => ({ name: 'sprint', type: 'rules', install(e, ctx) {
41
+ ctx.input.action('sprint', { label: 'sprint.action' }); // no keys: the game binds 'sprint'
42
+ ctx.input.action('sprintHud', { label: 'sprint.hud', optional: true }); // optional: reading it unbound is silent
43
+ }, fixed(dt, e) { if (e.input.down('sprint')) boost(e.player); } });
44
+ ```
45
+ Everything a module declares or binds through `ctx.input` leaves with it (rollback, `replaces`, dispose).
46
+
47
+ ## 4. What the engine tells you
48
+ - **`input-unbound`** (dev, once per action): a module needs an action nobody bound. The message names the action, its context and the bind line.
49
+ `engine.input.isUnbound('interact')` asks the same thing: true for no key at all, false for any key, an optional action or `[]` (no key on purpose).
50
+ - **`interaction-no-key`** (dev throws, gameplay freezes): interactions were added to an action with no key. Rule R51 finds it in `check`, the smoke test fails on it,
51
+ `describe` calls the thing `no key bound` and `interaction.use` is refused with `noKey`.
52
+ - **`input-bound-twice`** (dev throws; release: the later wins): two owners bind one action, e.g. the game's `createEngine({ bindings })` and a
53
+ module's `ctx.input.bind` for the same action. Keep one. The same owner binding again only replaces its keys.
54
+ - **`input-action-context`**: two owners declare one action in different contexts. Rename one, or use the same context to share it.
55
+ - `describe().shared['input.keys[attack]']` says who bound the keys now in effect (writer `game`, `settings` for the player's choice, or a module).
56
+
57
+ ## 5. Rebinding (the player)
58
+ `engine.input.rebind('attack', ['KeyK'])` is the player's choice, on top of the game's keys; `rebind('attack', null)` gives the game's back.
59
+ `engine.input.rebound()` lists them (the settings menu saves exactly that and restores it at start, even before the module declaring an action is
60
+ installed). The settings menu's key page, its reset button and its save all go through these two calls.