threedog 0.2.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (593) hide show
  1. package/CHANGELOG.md +290 -0
  2. package/README.md +9 -8
  3. package/core/Animator.js +15 -12
  4. package/core/Assets.js +71 -16
  5. package/core/Audio.js +45 -38
  6. package/core/Baked.js +32 -8
  7. package/core/Batching.js +102 -15
  8. package/core/Budget.js +39 -18
  9. package/core/Clock.js +40 -80
  10. package/core/DebugOverrides.js +45 -10
  11. package/core/Describe.js +104 -43
  12. package/core/Engine.js +183 -59
  13. package/core/Events.js +4 -1
  14. package/core/I18n.js +25 -13
  15. package/core/Input.js +168 -79
  16. package/core/Inspect.js +2 -2
  17. package/core/InspectMaterials.js +85 -0
  18. package/core/InspectPerf.js +61 -0
  19. package/core/InspectScene.js +13 -2
  20. package/core/Loading.js +2 -1
  21. package/core/Ops.js +50 -9
  22. package/core/Random.js +9 -2
  23. package/core/Rays.js +5 -5
  24. package/core/SaveStore.js +41 -9
  25. package/core/Saves.js +71 -15
  26. package/core/Stream.js +14 -10
  27. package/core/Threads.js +26 -7
  28. package/core/UrlState.js +25 -9
  29. package/core/Views.js +9 -6
  30. package/core/actor.js +1 -1
  31. package/core/adaptive.js +93 -0
  32. package/core/assetEntry.js +22 -5
  33. package/core/assetHot.js +176 -0
  34. package/core/assetLoad.js +38 -11
  35. package/core/assetManifest.js +48 -9
  36. package/core/assetReport.js +1 -1
  37. package/core/audioRules.js +13 -11
  38. package/core/audioVoice.js +69 -42
  39. package/core/canvasBox.js +1 -2
  40. package/core/catalog.js +5 -2
  41. package/core/cells.js +62 -0
  42. package/core/checks.js +10 -8
  43. package/core/chunkStream.js +87 -0
  44. package/core/control.js +149 -0
  45. package/core/copies.js +32 -0
  46. package/core/dayTime.js +12 -6
  47. package/core/debugView.js +82 -15
  48. package/core/describeRender.js +37 -17
  49. package/core/describeText.js +41 -21
  50. package/core/engineCamera.js +32 -0
  51. package/core/errorCodes.js +438 -19
  52. package/core/errorHints.js +37 -141
  53. package/core/errors.js +31 -8
  54. package/core/events.generated.js +12 -7
  55. package/core/gameLights.js +62 -0
  56. package/core/grayMap.js +20 -0
  57. package/core/grid.js +14 -13
  58. package/core/gridLoad.js +2 -1
  59. package/core/ground.js +6 -4
  60. package/core/headlessLoad.js +55 -0
  61. package/core/holds.js +78 -50
  62. package/core/inputContext.js +11 -7
  63. package/core/inputDefaults.js +24 -21
  64. package/core/inputPad.js +1 -1
  65. package/core/inputPointer.js +1 -1
  66. package/core/inputTouch.js +2 -2
  67. package/core/lifecycle.js +48 -12
  68. package/core/lightWatch.js +6 -4
  69. package/core/look/coreParts.js +137 -30
  70. package/core/look/part.js +175 -95
  71. package/core/look/points.js +255 -0
  72. package/core/look/report.js +34 -17
  73. package/core/look/shadowPart.js +63 -0
  74. package/core/look/state.js +45 -0
  75. package/core/look/write.js +12 -28
  76. package/core/loop.js +100 -41
  77. package/core/math.js +11 -3
  78. package/core/moduleSlots.js +75 -0
  79. package/core/moduleTypes.js +73 -0
  80. package/core/modules.js +145 -27
  81. package/core/movement.js +2 -2
  82. package/core/namedModel.js +70 -0
  83. package/core/opsCheck.js +3 -3
  84. package/core/opsFail.js +15 -6
  85. package/core/opsGuard.js +4 -4
  86. package/core/opsRun.js +4 -3
  87. package/core/owned.js +168 -0
  88. package/core/page.js +2 -1
  89. package/core/params.js +30 -8
  90. package/core/plugins.js +277 -0
  91. package/core/saveApply.js +99 -17
  92. package/core/saveFiles.js +8 -6
  93. package/core/saveValues.js +46 -0
  94. package/core/saveVersions.js +20 -3
  95. package/core/scatter.js +48 -19
  96. package/core/sceneDelta.js +203 -0
  97. package/core/sceneMethods.js +35 -30
  98. package/core/screenToGround.js +1 -1
  99. package/core/services.js +27 -11
  100. package/core/session.js +28 -25
  101. package/core/shaderCost.js +78 -0
  102. package/core/shadowTypes.js +2 -1
  103. package/core/sharedPoint.js +317 -0
  104. package/core/skip.js +36 -28
  105. package/core/stackFrames.js +34 -0
  106. package/core/stacks.js +166 -0
  107. package/core/startup.js +153 -11
  108. package/core/streamSources.js +0 -11
  109. package/core/styleParts.js +63 -47
  110. package/core/targeting.js +1 -1
  111. package/core/textureBytes.js +20 -0
  112. package/core/thingExplain.js +2 -2
  113. package/core/thingOps.js +29 -6
  114. package/core/things.js +21 -11
  115. package/core/threadPure.js +7 -3
  116. package/core/threePatches.js +47 -0
  117. package/core/tier.js +21 -10
  118. package/core/time.js +236 -0
  119. package/core/uiClock.js +25 -0
  120. package/core/util.js +7 -1
  121. package/core/validate.js +65 -86
  122. package/core/wrap.js +106 -0
  123. package/docs/AI.md +87 -100
  124. package/docs/API.md +32 -28
  125. package/docs/CODEMAP.md +220 -147
  126. package/docs/COMMANDS.md +56 -35
  127. package/docs/CONVENTIONS.md +90 -28
  128. package/docs/MIGRATING-0.3.md +248 -0
  129. package/docs/MIGRATING-0.4.md +228 -0
  130. package/docs/MIGRATING-0.5.md +68 -0
  131. package/docs/PERF.md +35 -7
  132. package/docs/THREE.md +79 -27
  133. package/docs/api/ambience.md +0 -1
  134. package/docs/api/assets.md +11 -9
  135. package/docs/api/audio.md +10 -14
  136. package/docs/api/calendar.md +41 -0
  137. package/docs/api/cameraRig.md +16 -5
  138. package/docs/api/characters.md +38 -23
  139. package/docs/api/clock.md +6 -5
  140. package/docs/api/collision.md +9 -6
  141. package/docs/api/content.md +6 -4
  142. package/docs/api/dialogue.md +9 -9
  143. package/docs/api/engine.md +63 -60
  144. package/docs/api/events.md +19 -14
  145. package/docs/api/grids.md +3 -3
  146. package/docs/api/ground.md +1 -1
  147. package/docs/api/hud.md +2 -2
  148. package/docs/api/i18n.md +1 -1
  149. package/docs/api/input.md +11 -6
  150. package/docs/api/interactions.md +7 -5
  151. package/docs/api/inventory.md +17 -16
  152. package/docs/api/level.md +0 -1
  153. package/docs/api/lightPool.md +3 -1
  154. package/docs/api/music.md +1 -1
  155. package/docs/api/nav.md +3 -0
  156. package/docs/api/openWorld.md +2 -1
  157. package/docs/api/particles.md +2 -2
  158. package/docs/api/photo.md +0 -2
  159. package/docs/api/pickups.md +15 -0
  160. package/docs/api/post.md +5 -4
  161. package/docs/api/quests.md +2 -3
  162. package/docs/api/rigid.md +16 -14
  163. package/docs/api/saves.md +6 -4
  164. package/docs/api/settings.md +2 -3
  165. package/docs/api/sky.md +11 -44
  166. package/docs/api/stream.md +1 -1
  167. package/docs/api/style.md +28 -26
  168. package/docs/api/terrain.md +9 -6
  169. package/docs/api/terrainLook.md +10 -0
  170. package/docs/api/timeline.md +3 -4
  171. package/docs/api/tools.md +29 -13
  172. package/docs/api/triggers.md +5 -10
  173. package/docs/api/vegetation.md +11 -8
  174. package/docs/api/vegetationLook.md +10 -0
  175. package/docs/api/water.md +4 -20
  176. package/docs/api/weather.md +5 -15
  177. package/docs/api/weatherLook.md +10 -0
  178. package/docs/api/world.md +2 -1
  179. package/docs/recipes/01-new-game.md +48 -34
  180. package/docs/recipes/02-npc.md +24 -7
  181. package/docs/recipes/03-items-pickups.md +35 -21
  182. package/docs/recipes/04-quests.md +12 -5
  183. package/docs/recipes/05-props-bake.md +151 -15
  184. package/docs/recipes/06-clock-events.md +100 -52
  185. package/docs/recipes/07-flow-scenes-pause.md +40 -9
  186. package/docs/recipes/08-weather-sky.md +145 -36
  187. package/docs/recipes/{09-style-pack.md → 09-style.md} +101 -52
  188. package/docs/recipes/10-blender-level.md +14 -2
  189. package/docs/recipes/11-vehicles.md +64 -7
  190. package/docs/recipes/13-animation.md +14 -6
  191. package/docs/recipes/14-custom-module.md +61 -29
  192. package/docs/recipes/15-perf.md +30 -9
  193. package/docs/recipes/16-dev-bridge.md +61 -37
  194. package/docs/recipes/18-world.md +16 -6
  195. package/docs/recipes/19-shooting-picking.md +6 -6
  196. package/docs/recipes/20-content-actions-save.md +55 -17
  197. package/docs/recipes/21-camera.md +49 -16
  198. package/docs/recipes/22-shop-gold.md +3 -3
  199. package/docs/recipes/23-ops.md +16 -10
  200. package/docs/recipes/26-ui-parts.md +61 -12
  201. package/docs/recipes/27-net.md +11 -5
  202. package/docs/recipes/28-terrain.md +34 -1
  203. package/docs/recipes/29-grid.md +34 -16
  204. package/docs/recipes/30-templates.md +64 -12
  205. package/docs/recipes/31-audio.md +20 -12
  206. package/docs/recipes/32-action-game.md +35 -2
  207. package/docs/recipes/34-environment-lighting.md +26 -3
  208. package/docs/recipes/35-style-from-reference.md +88 -19
  209. package/docs/recipes/36-sky-ground-plant-looks.md +18 -18
  210. package/docs/recipes/37-terrain-look-hooks.md +3 -2
  211. package/docs/recipes/38-environment-look-hooks.md +37 -26
  212. package/docs/recipes/40-scene.md +16 -13
  213. package/docs/recipes/41-thing-registry.md +11 -7
  214. package/docs/recipes/42-terrain-shape.md +2 -2
  215. package/docs/recipes/43-shader-patches-surface-looks.md +158 -76
  216. package/docs/recipes/44-water-weather-look-hooks.md +26 -17
  217. package/docs/recipes/45-cascaded-shadows.md +16 -7
  218. package/docs/recipes/47-island-snow-backdrop.md +7 -7
  219. package/docs/recipes/48-make-material.md +11 -6
  220. package/docs/recipes/49-game-module-contract.md +30 -17
  221. package/docs/recipes/50-own-renderer.md +2 -2
  222. package/docs/recipes/51-capability-catalog.md +3 -3
  223. package/docs/recipes/52-look.md +197 -61
  224. package/docs/recipes/53-ground-up-close.md +1 -1
  225. package/docs/recipes/54-three-idioms.md +88 -0
  226. package/docs/recipes/55-mods-time-wrap.md +112 -0
  227. package/docs/recipes/56-webgpu-tsl.md +260 -0
  228. package/docs/recipes/57-control.md +65 -0
  229. package/docs/recipes/58-input.md +57 -0
  230. package/docs/recipes/59-depth-post-effects.md +83 -0
  231. package/docs/recipes/60-judge-the-picture.md +55 -0
  232. package/examples/action/.threedog/content-index.json +1 -0
  233. package/examples/action/GAMEMAP.md +17 -13
  234. package/examples/action/assets.js +8 -0
  235. package/examples/action/content/actions.js +1 -1
  236. package/examples/action/content/audio.js +5 -5
  237. package/examples/action/look.js +1 -1
  238. package/examples/action/main.js +10 -14
  239. package/examples/action/models.js +24 -23
  240. package/examples/pullable.json +111 -0
  241. package/examples/rc2d/.threedog/content-index.json +1 -0
  242. package/examples/rc2d/GAMEMAP.md +83 -0
  243. package/examples/rc2d/README.md +52 -0
  244. package/examples/rc2d/main.js +63 -0
  245. package/examples/rc2d/rc2d.js +197 -0
  246. package/examples/rc2d/shaders.js +144 -0
  247. package/examples/sheepisland/.threedog/content-index.json +1 -0
  248. package/examples/sheepisland/GAMEMAP.md +13 -11
  249. package/examples/sheepisland/assets.js +5 -0
  250. package/examples/sheepisland/main.js +17 -12
  251. package/examples/sheepisland/style/index.js +6 -6
  252. package/examples/styletest/.threedog/content-index.json +1 -0
  253. package/examples/styletest/GAMEMAP.md +15 -16
  254. package/examples/styletest/assets.js +6 -0
  255. package/examples/styletest/content/templates.js +1 -1
  256. package/examples/styletest/content/vegetation.js +1 -1
  257. package/examples/styletest/look.js +1 -1
  258. package/examples/styletest/main.js +17 -16
  259. package/examples/styletest/models.js +1 -1
  260. package/examples/styletest/params.js +3 -3
  261. package/examples/techniques/contact-shadows/README.md +92 -0
  262. package/examples/techniques/contact-shadows/contactShadows.js +51 -0
  263. package/examples/techniques/contact-shadows/main.js +53 -0
  264. package/examples/techniques/day-night-grade/README.md +86 -0
  265. package/examples/techniques/day-night-grade/dayNightGrade.js +76 -0
  266. package/examples/techniques/day-night-grade/main.js +51 -0
  267. package/examples/techniques/interior-light/README.md +103 -0
  268. package/examples/techniques/interior-light/interiorLight.js +45 -0
  269. package/examples/techniques/interior-light/main.js +124 -0
  270. package/examples/techniques/modular-building/README.md +125 -0
  271. package/examples/techniques/modular-building/kit.js +426 -0
  272. package/examples/techniques/modular-building/main.js +47 -0
  273. package/examples/techniques/night-lights/README.md +94 -0
  274. package/examples/techniques/night-lights/main.js +70 -0
  275. package/examples/techniques/night-lights/nightLights.js +91 -0
  276. package/examples/techniques/outlines-and-rim/README.md +59 -0
  277. package/examples/techniques/outlines-and-rim/main.js +84 -0
  278. package/examples/techniques/outlines-and-rim/outlines.js +64 -0
  279. package/examples/techniques/procedural-animation/README.md +77 -0
  280. package/examples/techniques/procedural-animation/creature.js +44 -0
  281. package/examples/techniques/procedural-animation/main.js +45 -0
  282. package/examples/techniques/procedural-animation/proceduralAnim.js +118 -0
  283. package/examples/techniques/procedural-surface/README.md +67 -0
  284. package/examples/techniques/procedural-surface/main.js +103 -0
  285. package/examples/techniques/procedural-surface/surface.js +120 -0
  286. package/examples/techniques/stylized-shading/README.md +75 -0
  287. package/examples/techniques/stylized-shading/main.js +93 -0
  288. package/examples/techniques/stylized-shading/shading.js +131 -0
  289. package/examples/techniques/sun-and-key-light/README.md +135 -0
  290. package/examples/techniques/sun-and-key-light/keyLight.js +53 -0
  291. package/examples/techniques/sun-and-key-light/main.js +95 -0
  292. package/examples/techniques/wet-road/README.md +99 -0
  293. package/examples/techniques/wet-road/main.js +68 -0
  294. package/examples/techniques/wet-road/wetRoad.js +209 -0
  295. package/examples/webgpu-tsl/.threedog/content-index.json +1 -0
  296. package/examples/webgpu-tsl/GAMEMAP.md +84 -0
  297. package/examples/webgpu-tsl/README.md +42 -0
  298. package/examples/webgpu-tsl/main.js +21 -0
  299. package/examples/webgpu-tsl/tslScene.js +75 -0
  300. package/examples/world/.threedog/content-index.json +1 -0
  301. package/examples/world/GAMEMAP.md +22 -15
  302. package/examples/world/art/humanoid.js +42 -0
  303. package/examples/world/art/index.js +11 -0
  304. package/examples/world/art/models.js +58 -0
  305. package/examples/world/art/shapes.js +9 -0
  306. package/examples/world/assets.js +6 -0
  307. package/examples/world/content/audio.js +5 -5
  308. package/examples/world/content/npcs.js +1 -1
  309. package/examples/world/content/props.js +1 -1
  310. package/examples/world/content/rules.js +1 -1
  311. package/examples/world/content/templates.js +1 -1
  312. package/examples/world/content/vegetation.js +1 -1
  313. package/examples/world/main.js +17 -13
  314. package/examples/world/style/index.js +61 -0
  315. package/examples/world/style/plugins/10-bloom.js +5 -0
  316. package/index.d.ts +44 -45
  317. package/index.js +26 -22
  318. package/llms.txt +11 -4
  319. package/modules/ambience/Ambience.js +34 -23
  320. package/modules/calendar/Calendar.js +163 -0
  321. package/modules/calendar/contract.js +21 -0
  322. package/modules/calendar/index.js +81 -0
  323. package/modules/{sky → calendar}/schedule.js +17 -16
  324. package/modules/calendar/seasons.js +55 -0
  325. package/modules/camera/FirstPersonCamera.js +6 -15
  326. package/modules/camera/FixedCamera.js +33 -15
  327. package/modules/camera/FollowCamera.js +90 -16
  328. package/modules/camera/RtsCamera.js +18 -17
  329. package/modules/camera/common.js +70 -19
  330. package/modules/camera/contract.js +17 -0
  331. package/modules/camera/projection.js +11 -24
  332. package/modules/characters/Character.js +177 -42
  333. package/modules/characters/NPC.js +87 -36
  334. package/modules/characters/assetModel.js +4 -13
  335. package/modules/characters/behaviors.js +2 -1
  336. package/modules/characters/contract.js +13 -0
  337. package/modules/characters/footsteps.js +1 -1
  338. package/modules/characters/index.js +87 -49
  339. package/modules/characters/moveHelpers.js +4 -5
  340. package/modules/characters/movers/common.js +97 -19
  341. package/modules/characters/movers/manual.js +20 -0
  342. package/modules/characters/movers/rapier.js +19 -16
  343. package/modules/characters/movers/simple.js +14 -11
  344. package/modules/characters/movers/support.js +172 -0
  345. package/modules/characters/npcAvoid.js +47 -42
  346. package/modules/characters/npcSim.js +12 -8
  347. package/modules/characters/npcView.js +44 -16
  348. package/modules/characters/pathAsync.js +7 -3
  349. package/modules/characters/playerStrings.js +4 -3
  350. package/modules/characters/proceduralRig.js +2 -1
  351. package/modules/characters/useModel.js +1 -1
  352. package/modules/characters/vehicle.contract.js +21 -0
  353. package/modules/characters/walkTo.js +21 -7
  354. package/modules/collision/Collision.js +65 -25
  355. package/modules/collision/grid.js +8 -6
  356. package/modules/content/explain.js +2 -2
  357. package/modules/content/fields.js +102 -0
  358. package/modules/content/ids.js +8 -6
  359. package/modules/content/index.js +96 -27
  360. package/modules/content/loader.js +45 -33
  361. package/modules/content/models.js +11 -40
  362. package/modules/content/packs.js +6 -5
  363. package/modules/content/parts.js +41 -0
  364. package/modules/content/props.js +16 -9
  365. package/modules/content/rules.js +58 -0
  366. package/modules/content/stages.js +80 -0
  367. package/modules/content/templates.js +121 -30
  368. package/modules/debug/DebugPanel.js +24 -17
  369. package/modules/flow/contract.js +16 -0
  370. package/modules/flow/index.js +21 -15
  371. package/modules/gameplay/Container.js +41 -32
  372. package/modules/gameplay/Containers.js +30 -15
  373. package/modules/gameplay/Dialogue.js +113 -55
  374. package/modules/gameplay/Interactions.js +193 -67
  375. package/modules/gameplay/Inventory.js +69 -73
  376. package/modules/gameplay/Quests.js +75 -39
  377. package/modules/gameplay/boxOps.js +3 -3
  378. package/modules/gameplay/dialogue.contract.js +21 -0
  379. package/modules/gameplay/dialogueBox.js +4 -2
  380. package/modules/gameplay/dialogueBubbles.js +116 -0
  381. package/modules/gameplay/interactionPrompt.js +4 -1
  382. package/modules/gameplay/interactions.contract.js +16 -0
  383. package/modules/gameplay/inventory.contract.js +23 -0
  384. package/modules/gameplay/inventoryOps.js +18 -16
  385. package/modules/gameplay/pickups.js +268 -85
  386. package/modules/gameplay/questLog.js +4 -4
  387. package/modules/gameplay/quests.contract.js +17 -0
  388. package/modules/grid/Grid.js +92 -50
  389. package/modules/grid/Layer.js +14 -13
  390. package/modules/grid/index.js +60 -36
  391. package/modules/grid/ops.js +20 -18
  392. package/modules/grid/preview.js +15 -16
  393. package/modules/grid/strings.js +12 -0
  394. package/modules/grid/tiles.js +6 -5
  395. package/modules/hlod/index.js +51 -19
  396. package/modules/hud/index.js +35 -20
  397. package/modules/hud/parts.js +20 -7
  398. package/modules/level/heightfield.js +3 -1
  399. package/modules/level/index.js +45 -20
  400. package/modules/level/nodes.js +4 -3
  401. package/modules/lights/index.js +42 -18
  402. package/modules/music/Music.js +38 -21
  403. package/modules/music/rules.js +1 -1
  404. package/modules/nav/index.js +48 -42
  405. package/modules/net/Client.js +13 -6
  406. package/modules/net/Host.js +19 -5
  407. package/modules/net/Net.js +2 -1
  408. package/modules/net/index.js +13 -8
  409. package/modules/net/sync.js +9 -8
  410. package/modules/net/transports.js +77 -27
  411. package/modules/particles/Particles.js +23 -14
  412. package/modules/particles/contract.js +17 -0
  413. package/modules/particles/effect.js +39 -11
  414. package/modules/photo/Photo.js +53 -36
  415. package/modules/rigid/RigidBodies.js +194 -32
  416. package/modules/rigid/Vehicles.js +18 -15
  417. package/modules/rigid/sync.js +62 -17
  418. package/modules/settings/Settings.js +57 -52
  419. package/modules/settings/apply.js +38 -3
  420. package/modules/settings/sections.js +5 -3
  421. package/modules/settings/strings.js +3 -2
  422. package/modules/settings/view.js +5 -3
  423. package/modules/sky/Sky.js +88 -176
  424. package/modules/sky/cascades.js +86 -25
  425. package/modules/sky/cascades.tsl.js +82 -0
  426. package/modules/sky/clouds.js +34 -19
  427. package/modules/sky/domeLayers.js +30 -15
  428. package/modules/sky/environment.js +83 -13
  429. package/modules/sky/fog.js +140 -95
  430. package/modules/sky/frame.js +112 -44
  431. package/modules/sky/look.js +22 -19
  432. package/modules/sky/lookFog.js +48 -52
  433. package/modules/sky/lookShadow.js +53 -41
  434. package/modules/sky/lookSky.js +82 -21
  435. package/modules/sky/season.js +21 -0
  436. package/modules/sky/shader.js +41 -19
  437. package/modules/sky/shadow.js +44 -20
  438. package/modules/sky/skyModule.js +45 -33
  439. package/modules/sky/tint.js +61 -0
  440. package/modules/terrain/ChunkManager.js +175 -108
  441. package/modules/terrain/chunkJob.js +1 -1
  442. package/modules/terrain/chunkQueries.js +6 -4
  443. package/modules/terrain/index.js +105 -50
  444. package/modules/terrain/look.js +83 -0
  445. package/modules/terrain/lookGround.js +21 -16
  446. package/modules/terrain/terrainDefaults.js +1 -1
  447. package/modules/terrain/terrainEdit.js +18 -20
  448. package/modules/terrain/terrainGen.js +5 -3
  449. package/modules/terrain/terrainIsland.js +5 -4
  450. package/modules/terrain/terrainShape.js +12 -12
  451. package/modules/terrain/terrainShapeMain.js +8 -7
  452. package/modules/terrain/terrainTexture.js +21 -19
  453. package/modules/timeline/Timeline.js +95 -43
  454. package/modules/timeline/Tween.js +18 -10
  455. package/modules/timeline/letterbox.js +2 -2
  456. package/modules/timeline/shots.js +5 -1
  457. package/modules/triggers/index.js +107 -76
  458. package/modules/vegetation/Vegetation.js +186 -110
  459. package/modules/vegetation/densityMap.js +62 -0
  460. package/modules/vegetation/groundChunks.js +52 -58
  461. package/modules/vegetation/index.js +32 -11
  462. package/modules/vegetation/look.js +119 -0
  463. package/modules/vegetation/lookWind.js +71 -10
  464. package/modules/vegetation/modelGeometry.js +3 -2
  465. package/modules/vegetation/nearFade.js +8 -2
  466. package/modules/water/Water.js +65 -27
  467. package/modules/water/look.js +29 -18
  468. package/modules/water/surface.js +64 -25
  469. package/modules/water/surfaceDefaults.js +3 -0
  470. package/modules/weather/Weather.js +62 -104
  471. package/modules/weather/look.js +1 -2
  472. package/modules/weather/particles.js +24 -9
  473. package/modules/weather/strings.js +2 -2
  474. package/modules/weather/weatherLook.js +135 -0
  475. package/modules/world/World.js +24 -7
  476. package/modules/world/explain.js +1 -1
  477. package/modules/world/index.js +18 -4
  478. package/modules/world/persist.js +62 -2
  479. package/modules/world/pos.js +3 -2
  480. package/modules/world/records.js +8 -6
  481. package/modules/world/sim.js +60 -20
  482. package/modules/world/simFar.js +4 -3
  483. package/modules/world/state.js +3 -2
  484. package/modules/world/thingSource.js +1 -1
  485. package/modules/world/views.js +7 -5
  486. package/package.json +7 -3
  487. package/presets.js +41 -27
  488. package/render/PostFX.js +145 -120
  489. package/render/backend.js +440 -0
  490. package/render/detail.js +120 -39
  491. package/render/effects.js +130 -38
  492. package/render/engineUniforms.js +10 -1
  493. package/render/fuse.js +5 -4
  494. package/render/fx/cloudShadow.tsl.js +16 -0
  495. package/render/fx/detail.tsl.js +66 -0
  496. package/render/fx/index.js +26 -0
  497. package/render/fx/lighting.tsl.js +28 -0
  498. package/render/fx/nodeErrors.tsl.js +68 -0
  499. package/render/fx/noise.tsl.js +12 -0
  500. package/render/fx/objectOrigin.tsl.js +42 -0
  501. package/render/fx/particles.tsl.js +139 -0
  502. package/render/fx/patchNodes.tsl.js +403 -0
  503. package/render/fx/postWebGPU.tsl.js +158 -0
  504. package/render/fx/season.tsl.js +13 -0
  505. package/render/fx/sky.tsl.js +260 -0
  506. package/render/fx/uniforms.tsl.js +67 -0
  507. package/render/fx/water.tsl.js +53 -0
  508. package/render/fx/wind.tsl.js +22 -0
  509. package/render/fxNumbers.js +36 -0
  510. package/render/hotGlsl.js +24 -0
  511. package/render/lighting.js +66 -14
  512. package/render/lights.js +223 -4
  513. package/render/lookGlow.js +139 -0
  514. package/render/lookSurface.js +99 -36
  515. package/render/materials.js +71 -52
  516. package/render/modeling.js +73 -0
  517. package/render/ownFrame.js +76 -0
  518. package/render/patches.js +311 -34
  519. package/render/postLook.js +124 -46
  520. package/render/postWebGL.js +197 -0
  521. package/render/probes.js +231 -0
  522. package/render/shaderErrors.js +154 -0
  523. package/render/slots.js +165 -0
  524. package/render/surface.js +53 -17
  525. package/render/terms.js +211 -0
  526. package/render/uniformDecl.js +3 -2
  527. package/types/common.d.ts +2 -2
  528. package/types/core.d.ts +614 -240
  529. package/types/gameplay.d.ts +482 -312
  530. package/types/generated/core/Audio.d.ts +26 -42
  531. package/types/generated/core/Batching.d.ts +6 -2
  532. package/types/generated/core/Budget.d.ts +2 -8
  533. package/types/generated/core/DebugOverrides.d.ts +44 -3
  534. package/types/generated/core/I18n.d.ts +2 -2
  535. package/types/generated/core/Input.d.ts +102 -31
  536. package/types/generated/core/Ops.d.ts +31 -0
  537. package/types/generated/core/Random.d.ts +2 -2
  538. package/types/generated/core/Saves.d.ts +10 -2
  539. package/types/generated/core/Stream.d.ts +1 -1
  540. package/types/generated/core/Threads.d.ts +4 -1
  541. package/types/generated/core/Views.d.ts +0 -1
  542. package/types/generated/core/audioVoice.d.ts +17 -9
  543. package/types/generated/core/control.d.ts +55 -0
  544. package/types/generated/core/session.d.ts +2 -1
  545. package/types/generated/modules/ambience/Ambience.d.ts +0 -9
  546. package/types/generated/modules/calendar/Calendar.d.ts +84 -0
  547. package/types/generated/modules/calendar/index.d.ts +10 -0
  548. package/types/generated/modules/debug/DebugPanel.d.ts +2 -2
  549. package/types/generated/modules/grid/Grid.d.ts +61 -25
  550. package/types/generated/modules/grid/Layer.d.ts +5 -5
  551. package/types/generated/modules/grid/index.d.ts +29 -17
  552. package/types/generated/modules/music/Music.d.ts +5 -3
  553. package/types/generated/modules/net/index.d.ts +3 -3
  554. package/types/generated/modules/net/transports.d.ts +12 -3
  555. package/types/generated/modules/particles/Particles.d.ts +1 -1
  556. package/types/generated/modules/sky/Sky.d.ts +12 -104
  557. package/types/generated/modules/sky/skyModule.d.ts +1 -1
  558. package/types/generated/modules/timeline/Timeline.d.ts +8 -6
  559. package/types/generated/modules/weather/Weather.d.ts +17 -18
  560. package/types/generated/modules/weather/weatherLook.d.ts +12 -0
  561. package/types/generated/modules/world/World.d.ts +18 -3
  562. package/types/generated/render/PostFX.d.ts +67 -30
  563. package/types/generated/render/materials.d.ts +26 -20
  564. package/types/generated/render/modeling.d.ts +30 -0
  565. package/types/generated/render/patches.d.ts +16 -2
  566. package/types/generated/ui/UI.d.ts +6 -3
  567. package/types/nav.d.ts +6 -0
  568. package/types/physics.d.ts +137 -44
  569. package/types/style.d.ts +109 -81
  570. package/types/tsl.d.ts +86 -0
  571. package/types/world.d.ts +158 -110
  572. package/ui/Focus.js +25 -14
  573. package/ui/LoadingScreen.js +1 -1
  574. package/ui/Parts.js +27 -11
  575. package/ui/UI.js +20 -10
  576. package/ui/cursor.js +115 -0
  577. package/ui/defaults.js +15 -11
  578. package/ui/theme.js +9 -6
  579. package/ui/touchPart.js +1 -1
  580. package/core/claims.js +0 -51
  581. package/core/look/layers.js +0 -136
  582. package/core/renamed.js +0 -22
  583. package/docs/api/entities.md +0 -61
  584. package/examples/styletest/overrides.js +0 -33
  585. package/examples/styletest/panel.js +0 -135
  586. package/modules/entities/Entities.js +0 -243
  587. package/modules/entities/build.js +0 -25
  588. package/modules/entities/index.js +0 -4
  589. package/modules/sky/calendar.js +0 -80
  590. package/modules/sky/layers.js +0 -69
  591. package/modules/terrain/chunkLevels.js +0 -40
  592. package/modules/terrain/mountTerrain.js +0 -32
  593. package/modules/world/cells.js +0 -42
@@ -0,0 +1,76 @@
1
+ // @ts-check
2
+ // Day-night lighting and color grade: one set of keyframes over the day for the sky (sun / moon, hemisphere, fog), exposure, a post color grade,
3
+ // and the game's own lamps. Technique example (README.md next to this file); copy and adapt: the numbers are a neutral starting point, the
4
+ // colors of your game come from its DESIGN.md look, not from here.
5
+ import { lut } from '@threedog/post-effects';
6
+
7
+ /** Sky keyframes: [time 0-1, zenith, horizon (= fog color), sun color (the moon at night), sun intensity, ambient intensity] */
8
+ export const SKY = [
9
+ [0.00, 0x0d1626, 0x1f2c44, 0x8fa4d4, 0.35, 0.26], // night: the "sun" row is moonlight, blue and weak; ambient keeps a floor
10
+ [0.21, 0x172036, 0x3a4058, 0x9aa8d4, 0.35, 0.26],
11
+ [0.26, 0x55688c, 0xd09c7c, 0xffb27c, 1.2, 0.6], // sunrise: warm sun, horizon warm, zenith still cool (the contrast IS the look)
12
+ [0.33, 0x6d92c2, 0xc6d2dc, 0xfff0dc, 2.4, 0.9],
13
+ [0.50, 0x6e98ce, 0xcfdae4, 0xfffaf2, 3.0, 1.1], // noon: sun 3 + hemi 1.1 ~ pi: mid-tone albedos show at their own value
14
+ [0.68, 0x6a8ebe, 0xd4d0c6, 0xfff0d8, 2.6, 1.0],
15
+ [0.73, 0x4c5c86, 0xdc9c6c, 0xffa264, 1.6, 0.7], // dusk: warmth in the LIGHT color, ambient drops so shadows turn cool by contrast
16
+ [0.78, 0x222e4c, 0x5c5068, 0x9aa6d4, 0.35, 0.32], // blue hour: the moon row again, a little more ambient than deep night
17
+ [1.00, 0x0d1626, 0x1f2c44, 0x8fa4d4, 0.35, 0.26],
18
+ ];
19
+ /** Exposure over the day [time, exposure]: lifted at night so the moonlit scene reads, but never back to day brightness */
20
+ export const EXPOSURE = [[0, 1.15], [0.22, 1.15], [0.3, 1.0], [0.7, 1.0], [0.77, 1.1], [1, 1.15]];
21
+ /** Color grade over the day [time, temperature (-1 cool..1 warm), saturation, contrast] (the lut() post effect's params) */
22
+ export const GRADE = [
23
+ [0.00, -0.45, 0.72, 1.06], // night: cooler and less saturated (eyes lose color in the dark), a touch of contrast keeps edges
24
+ [0.22, -0.3, 0.8, 1.04],
25
+ [0.28, 0.35, 0.95, 1.0],
26
+ [0.40, 0, 1, 1],
27
+ [0.65, 0, 1, 1],
28
+ [0.73, 0.5, 0.9, 1.04], // dusk: warm, and saturation DOWN as warmth goes up, or oranges clip into neon
29
+ [0.79, -0.25, 0.78, 1.05],
30
+ [1.00, -0.45, 0.72, 1.06],
31
+ ];
32
+ /** Lamps: fully on below this daylight (0 night .. 1 noon), fading in above it */
33
+ export const LAMPS = { onBelow: 0.02, offAbove: 0.35 }; // daylight 0.35 is about 16:40: windows and lamps come on before dark, as people switch them on
34
+
35
+ /**
36
+ * The style fragment that carries the keyframes: createEngine({ style: [yourStyle, dayNightStyle()] }) or merge the keys into your own.
37
+ * The grade is a look plugin of this fragment (the shape a plugins/ file exports), so it reaches the chain through look.post.add like any effect
38
+ */
39
+ export function dayNightStyle() {
40
+ return { name: 'dayNight', sky: { keyframes: SKY, moonColor: 0xc8d4f0 }, exposure: EXPOSURE, plugins: { 'lut.js': { default: lut({ enabled: true }) } } };
41
+ }
42
+
43
+ /** Linear interpolation over [[t, ...values]] rows sorted by t, wrapping at 1 */
44
+ export function sample(rows, t) {
45
+ let i = rows.findIndex(r => r[0] > t);
46
+ if (i <= 0) i = i === 0 ? 0 : rows.length - 1;
47
+ const a = rows[Math.max(0, i - 1)], b = rows[i], k = b[0] === a[0] ? 0 : (t - a[0]) / (b[0] - a[0]);
48
+ return a.slice(1).map((v, j) => v + (b[j + 1] - v) * Math.min(1, Math.max(0, k)));
49
+ }
50
+
51
+ /**
52
+ * Drive the grade and the game's lamps from engine.calendar every frame. Lamps: [{ light?: THREE.Light, material?: { emissiveIntensity },
53
+ * intensity, emissive }]: full values at night. The grade is written through look.post (owner 'day-night'), only when it moved, so
54
+ * describe() shows who set it and the fused post shader is not refreshed every frame (the same owner overwrites its keys). Returns { remove(), level() }.
55
+ * @param {import('threedog').Engine} engine
56
+ * @param {{ lamps?: { light?: any, material?: any, intensity?: number, emissive?: number }[] }} [o]
57
+ */
58
+ export function followDayNight(engine, { lamps = [] } = {}) {
59
+ let last = null, level = 0, post = null;
60
+ const smooth = (/** @type {number} */ x) => x * x * (3 - 2 * x);
61
+ const frame = engine.onFrame(() => {
62
+ const cal = /** @type {any} */ (engine).calendar, t = cal.timeOfDay;
63
+ const [temperature, saturation, contrast] = sample(GRADE, t);
64
+ if (!last || Math.abs(last[0] - temperature) + Math.abs(last[1] - saturation) + Math.abs(last[2] - contrast) > 0.004) {
65
+ last = [temperature, saturation, contrast];
66
+ post = engine.look.post.set({ 'lut.temperature': temperature, 'lut.saturation': saturation, 'lut.contrast': contrast }, { owner: 'day-night' });
67
+ }
68
+ level = 1 - smooth(Math.min(1, Math.max(0, (cal.daylight - LAMPS.onBelow) / (LAMPS.offAbove - LAMPS.onBelow))));
69
+ for (const l of lamps) {
70
+ // intensity only, never visible = false: a light count change recompiles every lit material (a stall at dusk)
71
+ if (l.light) l.light.intensity = (l.intensity ?? 1) * level;
72
+ if (l.material) l.material.emissiveIntensity = (l.emissive ?? 1) * level;
73
+ }
74
+ });
75
+ return { remove() { frame.remove(); post?.remove(); }, level: () => level };
76
+ }
@@ -0,0 +1,51 @@
1
+ // @ts-check
2
+ // Demo page for the day-night-grade technique (page /day-night-grade.html). A neutral gray street corner: three houses with a lit window each,
3
+ // two lamp posts with a point light, a figure-sized capsule. The day runs in 4 minutes; ?time=17:30 (the engine reads it) starts at a time.
4
+ // Test page for tests/specs/day-night-grade.mjs: window.dayNightTest = { THREE, follow, lamps }.
5
+ import { createEngine, calendar, sky, fixedCamera, makeMaterial, THREE } from 'threedog';
6
+ import { dayNightStyle, followDayNight } from './dayNightGrade.js';
7
+
8
+ const engine = createEngine({ canvas: /** @type {HTMLCanvasElement} */ (document.getElementById('view')), style: dayNightStyle(),
9
+ ui: { loading: false }, saveKey: 'threedog-day-night' });
10
+ engine.use(calendar({ time: '12:00', dayLength: 240 }));
11
+ engine.use(sky());
12
+ engine.use(fixedCamera({ position: [9, 4.2, 11], lookAt: [0, 1.2, 0] }));
13
+
14
+ const ground = new THREE.Mesh(new THREE.PlaneGeometry(80, 80), makeMaterial('terrain', { color: 0x8e8e8e }));
15
+ ground.rotation.x = -Math.PI / 2;
16
+ ground.receiveShadow = true;
17
+ engine.scene.add(ground);
18
+ const add = (/** @type {THREE.BufferGeometry} */ g, /** @type {THREE.Material} */ m, x = 0, y = 0, z = 0) => {
19
+ const mesh = new THREE.Mesh(g, m);
20
+ mesh.position.set(x, y, z);
21
+ mesh.castShadow = mesh.receiveShadow = true;
22
+ engine.scene.add(mesh);
23
+ return mesh;
24
+ };
25
+ const wall = makeMaterial('prop', { color: 0xb2b2b2 }), roof = makeMaterial('prop', { color: 0x7a7a7a });
26
+ // warm window glass: the color is the lamp's, its strength follows the night (followDayNight writes emissiveIntensity)
27
+ const glass = makeMaterial('prop', { color: 0x202020, emissive: 0xffc27a, emissiveIntensity: 0 });
28
+ const lamps = [];
29
+ for (const [x, z, w] of [[-5, -4, 4], [1, -6, 5], [6, -2, 3.5]]) {
30
+ add(new THREE.BoxGeometry(w, 3, 4), wall, x, 1.5, z);
31
+ add(new THREE.ConeGeometry(w * 0.8, 1.4, 4).rotateY(Math.PI / 4), roof, x, 3.7, z);
32
+ add(new THREE.BoxGeometry(0.9, 0.8, 0.05), glass, x, 1.6, z + 2.01);
33
+ }
34
+ lamps.push({ material: glass, emissive: 2.2 });
35
+ // lamp posts: an emissive head (what you see) plus a point light (what it lights); both follow the night together
36
+ const head = makeMaterial('prop', { color: 0x202020, emissive: 0xffd29a, emissiveIntensity: 0 });
37
+ for (const [x, z] of [[-1.5, 1.5], [4.5, 2.5]]) {
38
+ add(new THREE.CylinderGeometry(0.06, 0.08, 3.2, 10), roof, x, 1.6, z);
39
+ add(new THREE.SphereGeometry(0.18, 16, 12), head, x, 3.3, z).castShadow = false;
40
+ const light = new THREE.PointLight(0xffc88a, 0, 9, 1.6); // intensity written by followDayNight; distance limits its cost to the street
41
+ light.position.set(x, 3.1, z);
42
+ engine.scene.add(light);
43
+ lamps.push({ light, intensity: 14 });
44
+ }
45
+ lamps.push({ material: head, emissive: 3 });
46
+ add(new THREE.CapsuleGeometry(0.28, 1.1, 8, 16), makeMaterial('prop', { color: 0xa0a0a0 }), 1.5, 0.83, 2.2);
47
+ add(new THREE.BoxGeometry(0.8, 0.8, 0.8), makeMaterial('prop', { color: 0x9c9c9c }), -3, 0.4, 2.5);
48
+
49
+ const follow = followDayNight(engine, { lamps });
50
+ Object.assign(window, { engine, dayNightTest: { THREE, follow, lamps } });
51
+ engine.start();
@@ -0,0 +1,103 @@
1
+ Interior light: sun through a window with shadows, the window as an area light, faked bounce with light probes or a fill light, exposure from outside to inside, and lamps at night without glare.
2
+
3
+ # Interior light (technique example)
4
+
5
+ A neutral gray room (8 x 6 m, 3 m high, a window in the west wall, a door in the south wall), lit from outside by day and by lamps at night.
6
+ Run `npm run dev` and open `/interior-light.html`. The engine never imports this folder.
7
+
8
+ | URL | What changes |
9
+ |---|---|
10
+ | `?cam=inside \| doorway \| outside` | the camera view (the zone, and so exposure and ambient, follows the camera) |
11
+ | `&fill=probes \| light \| none` | bounce baked into light probes (default), one cheap point light, or nothing |
12
+ | `&night=1` | night: lamps instead of the sun |
13
+ | `&area=0` | no window area light |
14
+ | `&leak=1` | keep the outdoor ambient indoors (shows the problem the zones solve) |
15
+
16
+ All values sit in the parameter block at the top of `main.js` (`ROOM`, `WINDOW`, `SUN`, `AMBIENT`, `WINDOW_LIGHT`, `BOUNCE_LIGHT`, `EXPOSURE`,
17
+ `LAMPS`, `VIEWS`). Related: `sun-and-key-light` (the sun itself), `contact-shadows`, `night-lights` (lights in an outdoor night).
18
+
19
+ ## The principle
20
+ **A room is lit from its openings.** Daylight enters in two parts: the sun (a hard patch on the floor, the brightest thing in the room) and
21
+ the sky seen through the window (a soft, cool light that falls off with distance from the window). Everything else is bounce: the sun patch
22
+ lights the ceiling and the walls around it. A room without bounce looks like a cave with one spotlight; a room where the bounce is as bright
23
+ as the patch looks flat.
24
+
25
+ **Outdoor ambient leaks.** Hemisphere and environment light reach every surface regardless of walls, so indoors they light the room as if
26
+ it had no roof. Turn them down inside and let the bounce do the fill. `?leak=1` shows the room with the outdoor values: evenly gray, the sun
27
+ patch barely stands out.
28
+
29
+ **Exposure is the eye.** Real interiors are many times darker than the street; the eye adapts, a camera changes exposure. Games compress the
30
+ range: inside gets a higher exposure, eased over about a second when the player crosses the threshold. Looking in from outside, the room usually
31
+ reads darker than the street (and from inside the window brighter than the room), which gives the space its depth; if your game wants a flat,
32
+ evenly lit look (a diorama, a cutaway), keep one exposure on purpose.
33
+
34
+ **Night: light the surfaces, not the bulb.** A lamp is a pool of light on a table, a wall, a floor. Hide the bulb in a shade, keep the visible
35
+ emitter dim, and let the light do the work; several small warm lights read cozier than one bright one. Glare and clipped white come from
36
+ bright visible emitters plus bloom; without bloom they just look like flat white shapes.
37
+
38
+ How to judge it in a shot: the sun patch is the brightest area, the wall opposite the window clearly dimmer, and the corner furthest from the
39
+ window dark but still readable if the game needs it readable. Check from the cameras the game really uses, including the doorway.
40
+
41
+ ## Plain three.js
42
+ - Sun patch: a `DirectionalLight` with `castShadow`, every wall / ceiling / frame `castShadow`, and `shadow.camera` fitted to the room
43
+ ([DirectionalLightShadow](https://threejs.org/docs/#api/en/lights/shadows/DirectionalLightShadow)).
44
+ - Window sky light: [RectAreaLight](https://threejs.org/docs/#api/en/lights/RectAreaLight) with `RectAreaLightUniformsLib.init()`
45
+ ([webgl_lights_rectarealight](https://threejs.org/examples/#webgl_lights_rectarealight)).
46
+ - Bounce: a [LightProbe](https://threejs.org/docs/#api/en/lights/LightProbe) filled by `LightProbeGenerator.fromCubeRenderTarget` after a
47
+ `CubeCamera` render inside the room; or a weak unshadowed `PointLight` above the patch; or a lightmap (`material.lightMap`, needs a second UV set).
48
+ - Exposure: ease `renderer.toneMappingExposure` and the `HemisphereLight` intensity together when the camera enters a box.
49
+ - Lamps: `PointLight` with `decay: 2`, intensity in candela ([PointLight](https://threejs.org/docs/#api/en/lights/PointLight)), a low
50
+ `emissiveIntensity` on the shade.
51
+
52
+ ## Style routes
53
+ - **Realistic**: probes or lightmaps for the bounce, a strong sun patch, exposure zones with a noticeable change. Costs a bake.
54
+ - **Stylized / cozy**: warmer bounce (`BOUNCE_LIGHT.color`), softer contrast between patch and room, more lamps at night, gentle exposure
55
+ change. Costs a few more lights.
56
+ - **Horror / stealth**: little or no fill, a hard window patch, slow exposure adaptation so the player is blind for a moment at the door. Cheapest.
57
+ - **Top-down or isometric**: the ceiling is usually hidden; the window patch and lamp pools carry the room. For 2D-like spreading light that
58
+ stops at walls see `examples/rc2d`.
59
+ - **Baked (static rooms)**: lightmaps from Blender give per-surface bounce at no run-time cost when the room does not change (recipe 46).
60
+
61
+ ## The engine helper (optional convenience)
62
+ `threedog examples pull interior-light` copies `interiorLight.js` and this README into the game; the probes are `engine.bakeProbes` itself.
63
+ - `exposureZones(engine, { isInside, exposure, ambient, ease })`: changes exposure and the leaking outdoor ambient together, eased, and only
64
+ when the zone changes, with `engine.look.tone.set({ exposure }, { owner: 'zone' })` in `onFrame` (presentation). The page uses a box around
65
+ the room and `EXPOSURE.day.outside` / `.inside`, eased over `EXPOSURE.ease` seconds.
66
+ - `windowLight(engine, { position, lookAt, width, height })`: a `RectAreaLight` with its one-time uniforms init done.
67
+
68
+ | fill | what it gives | cost |
69
+ |---|---|---|
70
+ | `probes` | bounce from the actual sun patch and walls, colored by them, direction-dependent | a bake at load (6 cube renders per probe; `probes.bakeMs` says how long); at run time one LightProbe, about free |
71
+ | `light` | one unshadowed PointLight above the sun patch, standing in for its bounce | one more light in every lit shader; no bake. Simple and easy to tune, but it is a point, so it shows as a hot spot if too strong |
72
+ | `none` | the room as the direct lights leave it | nothing |
73
+
74
+ Measured in the test (linear light, the floor along the north wall and the wall's lower part, out of the sun patch): probes make them clearly
75
+ brighter than no fill (the test requires more than 1.15x).
76
+
77
+ Zones vs measured exposure: a zone is cheap and predictable, and the designer decides the values. Measured auto exposure (average the frame's
78
+ luminance, ease toward a target) adapts to anything but needs a downsample and readback every few frames, can pump when bright objects enter
79
+ the view, and still needs limits. A zone also carries the ambient change, which measured exposure cannot. Limit of zones: they follow the
80
+ camera, so looking into the room from outside it still gets outdoor ambient (`?cam=doorway`). When that view matters, give indoor surfaces
81
+ their own fill (probes sampled per object with `probes.sample(pos)`, or lightmaps).
82
+
83
+ Night (`?night=1`): the sky at night (its moonlight stays very low), the indoor zone's exposure at 1, two warm PointLights (physical falloff,
84
+ decay 2, intensity in candela) inside shades with an emissive of 0.35; one casts a 512 px shadow (a point light shadow is six renders: give it
85
+ only to the light that needs it). With bloom (`@threedog/post-effects`), keep the emitter's `emissiveIntensity` low or it blooms across the room.
86
+
87
+ Rules (correctness):
88
+ - `RectAreaLight` needs `RectAreaLightUniformsLib.init()` once, lights only `MeshStandardMaterial` / `MeshPhysicalMaterial`, and **casts no
89
+ shadow**: it lights through furniture and walls on its front side. Use it for the window's soft sky light or a screen, and keep the
90
+ shadowed part to the sun (a DirectionalLight) or a SpotLight.
91
+ - The sun through a window needs every wall, the ceiling and the window frame to `castShadow`, and a shadow box that covers the room. With
92
+ the sky module: `look.lights.manual('sun.direction')` to aim it, `look.shadow.set({ follow: false, extent })` to fit the box. Thin walls
93
+ want a small `normalBias` (here 1 cm) or light leaks along their inner edge.
94
+ - `engine.bakeProbes({ bounds, spacing, follow })` bakes the scene as it is at that moment: set the indoor ambient and the lights first, and
95
+ bake again (or ship one bake per state) if the lighting changes a lot (day / night). Probes add to the hemisphere and environment, which
96
+ the bake also saw (recipe 34). It is one spherical harmonic for the whole scene at a time, interpolated at the `follow` point: right for
97
+ the room you are in, wrong for a building seen from far away.
98
+ - The bake renders after start; this page tracks it in `engine.loading` so screenshots and tests wait for it.
99
+ - Do not name page URL parameters `time` or `view` (or the other engine scenario keys: `weather`, `pos`, `season`, `state`, `save`, `wait`,
100
+ `cutscene`): `?time=night` reaches the sky as a clock time, turns into NaN and blacks out the frame.
101
+
102
+ Sources: written for this repository, no code copied. Background: three.js `RectAreaLight` and `LightProbe` docs and examples (0.187), general
103
+ architectural-visualization and game-lighting writing on window light, bounce and eye adaptation.
@@ -0,0 +1,45 @@
1
+ // @ts-check
2
+ // Interior light helpers (technique example, README.md next to this file). Imports only three and its RectAreaLight addon.
3
+ // Why helpers: the zone switch has to change exposure and the leaking outdoor ambient together, eased, and only when the zone changes;
4
+ // the window light needs a one-time uniforms init. Both are easy to get half right by hand.
5
+ import * as THREE from 'three';
6
+ import { RectAreaLightUniformsLib } from 'three/addons/lights/RectAreaLightUniformsLib.js';
7
+
8
+ /**
9
+ * Exposure and ambient by zone, following the camera. `isInside(cameraPosition)` decides the zone; on a change the hemisphere / environment
10
+ * intensities switch at once (they light through walls) and the exposure eases over `ease` seconds (an eye adapting). Presentation only: it
11
+ * runs in onFrame and writes owner layers of look.lights / look.tone (remove() on the handle gives the style values back).
12
+ * @param {any} engine
13
+ * @param {{ isInside: (p: THREE.Vector3) => boolean, exposure: { inside: number, outside: number }, ambient: { inside: { hemi: number, env: number }, outside: { hemi: number, env: number } }, ease?: number, owner?: string }} o
14
+ */
15
+ export function exposureZones(engine, { isInside, exposure, ambient, ease = 1.2, owner = 'zone' }) {
16
+ const zone = { name: '', exposure: 0, target: 0 };
17
+ /** @param {boolean} immediate @param {number} dt */
18
+ const apply = (immediate, dt = 0) => {
19
+ const name = isInside(engine.camera.position) ? 'inside' : 'outside';
20
+ if (name !== zone.name) {
21
+ zone.name = name;
22
+ zone.target = exposure[name];
23
+ engine.look.lights.set({ hemi: { intensity: ambient[name].hemi }, env: { intensity: ambient[name].env } }, { owner });
24
+ }
25
+ const next = immediate || !zone.exposure ? zone.target : zone.exposure + (zone.target - zone.exposure) * Math.min(1, dt / (ease / 3));
26
+ if (Math.abs(next - zone.exposure) > 1e-4) { zone.exposure = next; engine.look.tone.set({ exposure: next }, { owner }); }
27
+ };
28
+ const h = engine.onFrame((/** @type {number} */ dt) => apply(false, dt));
29
+ return { zone, apply, remove() { h?.remove?.(); engine.look.lights.set({ hemi: null, env: null }, { owner }); engine.look.tone.set({ exposure: null }, { owner }); } };
30
+ }
31
+
32
+ /**
33
+ * The sky seen through a window as a RectAreaLight in the opening, facing `lookAt`. Soft and window-shaped, but it casts no shadow and lights
34
+ * only MeshStandardMaterial / MeshPhysicalMaterial.
35
+ * @param {any} engine
36
+ * @param {{ position: number[], lookAt: number[], width: number, height: number, color?: THREE.ColorRepresentation, intensity?: number }} o
37
+ */
38
+ export function windowLight(engine, { position, lookAt, width, height, color = '#cfe0ff', intensity = 2 }) {
39
+ RectAreaLightUniformsLib.init(); // once per page is enough; calling it again is harmless
40
+ const area = new THREE.RectAreaLight(color, intensity, width, height);
41
+ area.position.set(position[0], position[1], position[2]);
42
+ area.lookAt(lookAt[0], lookAt[1], lookAt[2]);
43
+ engine.scene.add(area);
44
+ return area;
45
+ }
@@ -0,0 +1,124 @@
1
+ // @ts-check
2
+ // Technique example: interior light (page /interior-light.html, README.md next to this file). A neutral gray room (8 x 6 m, 3 m high) with a
3
+ // window in the west wall and a door in the south wall. One skill: lighting a room from outside (sun through the opening, sky light through the
4
+ // window), filling the shadowed side with bounce, the exposure change between outside and inside, and lamps at night.
5
+ // (time and view are the engine's own URL scenario keys, see core/UrlState.js: page params use other names)
6
+ // URL: ?night=1 ?cam=inside|doorway|outside ?fill=probes|light|none ?area=0 (no window area light) ?leak=1 (keep outdoor ambient indoors)
7
+ // Test page for tests/specs/techniques-light.mjs: window.interiorTest = { THREE, ready, points, zone }.
8
+ import { createEngine, sky, fixedCamera, THREE } from 'threedog';
9
+ import { exposureZones, windowLight } from './interiorLight.js';
10
+
11
+ // ---- parameters: change these ----------------------------------------------------------------------------------------------------------
12
+ const ROOM = { min: [-4, 0, -3], max: [4, 3, 3], wall: 0.2 };
13
+ const WINDOW = { z: [-1.1, 1.1], y: [0.8, 2.4] }; // opening in the west wall (x = -4)
14
+ const DOOR = { x: [1.4, 2.5], y: [0, 2.2] }; // opening in the south wall (z = +3)
15
+ const SUN = { elevation: 32, azimuth: -90 + 12, intensity: 3.2, color: '#fff0dc' }; // azimuth: compass degrees from north (-Z), -90 = from the west
16
+ // outdoor ambient (hemisphere + room environment) is not blocked by walls: indoors it is turned down and the fill comes from bounce instead
17
+ const AMBIENT = { outside: { hemi: 1.0, env: 0.3 }, inside: { hemi: 0.06, env: 0.04 } };
18
+ const WINDOW_LIGHT = { color: '#cfe0ff', intensity: 2.2 }; // RectAreaLight: the sky seen through the window (nits-like units, Standard / Physical only)
19
+ const BOUNCE_LIGHT = { color: '#ffe2c0', intensity: 1.2, distance: 7 }; // ?fill=light: one unshadowed point light standing in for the sun patch's bounce
20
+ // exposure per zone, eased over EASE seconds when the camera crosses the doorway (an eye adapting)
21
+ const EXPOSURE = { day: { outside: 1, inside: 1.9 }, night: { outside: 1.6, inside: 1 }, ease: 1.2 };
22
+ const LAMPS = [{ at: [2.6, 1.55, -2.2], intensity: 5, color: '#ffc58a', shadow: true }, { at: [-2.8, 1.0, 2.2], intensity: 2.5, color: '#ffb874', shadow: false }];
23
+ const VIEWS = {
24
+ inside: { position: [3.4, 1.65, 2.4], lookAt: [-3, 0.8, -0.6] },
25
+ doorway: { position: [1.95, 1.65, 5.2], lookAt: [0.6, 1.1, -3] },
26
+ outside: { position: [6, 1.7, 12], lookAt: [0, 1.2, 0] },
27
+ };
28
+ // ------------------------------------------------------------------------------------------------------------------------------------------
29
+
30
+ const url = new URLSearchParams(location.search);
31
+ const night = url.get('night') === '1', fill = url.get('fill') ?? 'probes', view = /** @type {keyof typeof VIEWS} */ (url.get('cam') ?? 'inside');
32
+ if (!VIEWS[view]) throw new Error(`?cam=${view}: use one of ${Object.keys(VIEWS).join(', ')}`);
33
+
34
+ const engine = createEngine({ canvas: /** @type {HTMLCanvasElement} */ (document.getElementById('view')), ui: { loading: false }, saveKey: 'threedog-interior' });
35
+ engine.use(sky({ time: night ? '22:30' : '15:00' }));
36
+ engine.use(fixedCamera({ shots: VIEWS, start: view }));
37
+
38
+ // ---- the sun through the window: aimed by hand, shadow box fitted to the room (8 m wide: 0.6 cm texels at 2048) ----
39
+ const sun = engine.lights.sun;
40
+ if (!night) {
41
+ engine.look.lights.manual('sun.direction');
42
+ const az = THREE.MathUtils.degToRad(SUN.azimuth), el = THREE.MathUtils.degToRad(SUN.elevation);
43
+ const dir = new THREE.Vector3(Math.sin(az) * Math.cos(el), Math.sin(el), -Math.cos(az) * Math.cos(el));
44
+ sun.target.position.set(0, 0, 0);
45
+ sun.position.copy(dir).multiplyScalar(160);
46
+ engine.look.lights.set({ sun: { intensity: SUN.intensity, color: SUN.color } }, { owner: 'technique' });
47
+ }
48
+ // thin walls need a small normal bias, or light leaks along the wall's inner edge; the bias is ~1 cm (shadow depth 0-1 x the camera's depth range)
49
+ engine.look.shadow.set({ follow: false, extent: 9, normalBias: 0.01, bias: -0.01 / (sun.shadow.camera.far - sun.shadow.camera.near), type: 'pcf', radius: 2 }, { owner: 'technique' });
50
+
51
+ // ---- the neutral gray room (plain three.js) ----
52
+ const mat = (/** @type {number} */ c, roughness = 0.9) => new THREE.MeshStandardMaterial({ color: c, roughness });
53
+ const wallMat = mat(0xb5b5b5), floorMat = mat(0x8f8f8f);
54
+ const box = (/** @type {number[]} */ min, /** @type {number[]} */ max, m = wallMat) => {
55
+ const mesh = new THREE.Mesh(new THREE.BoxGeometry(max[0] - min[0], max[1] - min[1], max[2] - min[2]), m);
56
+ mesh.position.set((min[0] + max[0]) / 2, (min[1] + max[1]) / 2, (min[2] + max[2]) / 2);
57
+ mesh.castShadow = mesh.receiveShadow = true;
58
+ engine.scene.add(mesh);
59
+ return mesh;
60
+ };
61
+ const [x0, , z0] = ROOM.min, [x1, h, z1] = ROOM.max, t = ROOM.wall;
62
+ const ground = new THREE.Mesh(new THREE.PlaneGeometry(120, 120), mat(0x8a8a8a, 0.95));
63
+ ground.rotation.x = -Math.PI / 2;
64
+ ground.position.y = -0.01;
65
+ ground.receiveShadow = true;
66
+ engine.scene.add(ground);
67
+ box([x0, -0.01, z0], [x1, 0.02, z1], floorMat); // the floor
68
+ box([x0 - t, h, z0 - t], [x1 + t, h + t, z1 + t]); // the ceiling
69
+ box([x0 - t, 0, z0 - t], [x1 + t, h, z0]); // north wall
70
+ box([x1, 0, z0], [x1 + t, h, z1]); // east wall
71
+ // west wall with the window: four pieces around the opening
72
+ box([x0 - t, 0, z0], [x0, h, WINDOW.z[0]]); box([x0 - t, 0, WINDOW.z[1]], [x0, h, z1]);
73
+ box([x0 - t, 0, WINDOW.z[0]], [x0, WINDOW.y[0], WINDOW.z[1]]); box([x0 - t, WINDOW.y[1], WINDOW.z[0]], [x0, h, WINDOW.z[1]]);
74
+ // south wall with the door
75
+ box([x0 - t, 0, z1], [DOOR.x[0], h, z1 + t]); box([DOOR.x[1], 0, z1], [x1 + t, h, z1 + t]); box([DOOR.x[0], DOOR.y[1], z1], [DOOR.x[1], h, z1 + t]);
76
+ // furniture-sized blocks: a table, a cabinet against the east wall, a sofa-sized block in the corner
77
+ box([-1.2, 0, -0.5], [0.4, 0.75, 0.5], mat(0xa0a0a0)); box([3.2, 0, -1.4], [3.95, 1.9, 0.6], mat(0x9c9c9c)); box([-3.9, 0, 1.4], [-2.2, 0.8, 2.9], mat(0xa6a6a6));
78
+
79
+ // ---- the window as a light: a RectAreaLight in the opening, facing in. Soft and shaped like the window, but it casts no shadow ----
80
+ if (url.get('area') !== '0' && !night) {
81
+ windowLight(engine, { position: [x0 - 0.02, (WINDOW.y[0] + WINDOW.y[1]) / 2, 0], lookAt: [0, (WINDOW.y[0] + WINDOW.y[1]) / 2, 0],
82
+ width: WINDOW.z[1] - WINDOW.z[0], height: WINDOW.y[1] - WINDOW.y[0], ...WINDOW_LIGHT });
83
+ }
84
+
85
+ // ---- night: lamps. The bulb hides inside a shade; the light lands on surfaces, the visible emitter stays dim (no glare without bloom) ----
86
+ if (night) {
87
+ for (const l of LAMPS) {
88
+ const p = new THREE.PointLight(l.color, l.intensity, 0, 2); // physical falloff (decay 2); intensity in candela
89
+ p.position.set(l.at[0], l.at[1], l.at[2]);
90
+ p.castShadow = l.shadow;
91
+ if (l.shadow) { p.shadow.mapSize.set(512, 512); p.shadow.bias = -0.002; p.shadow.radius = 4; }
92
+ const shade = new THREE.Mesh(new THREE.CylinderGeometry(0.16, 0.26, 0.28, 24, 1, true),
93
+ new THREE.MeshStandardMaterial({ color: 0xe8dccb, emissive: l.color, emissiveIntensity: 0.35, side: THREE.DoubleSide, roughness: 1 }));
94
+ shade.position.copy(p.position);
95
+ const stand = new THREE.Mesh(new THREE.CylinderGeometry(0.02, 0.02, l.at[1] - 0.14, 8), mat(0x777777));
96
+ stand.position.set(l.at[0], (l.at[1] - 0.14) / 2, l.at[2]);
97
+ engine.scene.add(p, shade, stand);
98
+ }
99
+ }
100
+
101
+ // ---- zones: the camera inside the room gets indoor ambient and indoor exposure, eased. Presentation, so it runs per frame ----
102
+ const inside = (/** @type {THREE.Vector3} */ p) => p.x > x0 && p.x < x1 && p.z > z0 && p.z < z1 + 0.5 && p.y < h;
103
+ const leak = url.get('leak') === '1';
104
+ const { zone, apply: applyZone } = exposureZones(engine, { isInside: inside, exposure: EXPOSURE[night ? 'night' : 'day'], ease: EXPOSURE.ease,
105
+ ambient: leak ? { inside: AMBIENT.outside, outside: AMBIENT.outside } : AMBIENT });
106
+
107
+ // ---- fill: bounce baked into light probes, one cheap point light, or nothing ----
108
+ async function setupFill() {
109
+ applyZone(true); // bake with indoor ambient, as the room is meant to be seen
110
+ if (fill === 'probes') {
111
+ // bounds = the room's air; the probe light follows a fixed point in the room (one SH for the whole scene at a time)
112
+ const center = new THREE.Vector3(0, 1.2, 0);
113
+ await engine.bakeProbes({ bounds: { min: [x0 + 0.5, 0.4, z0 + 0.5], max: [x1 - 0.5, h - 0.4, z1 - 0.5] }, spacing: 2.5, follow: () => center });
114
+ } else if (fill === 'light' && !night) {
115
+ const bounce = new THREE.PointLight(BOUNCE_LIGHT.color, BOUNCE_LIGHT.intensity, BOUNCE_LIGHT.distance, 1);
116
+ bounce.position.set(-1.6, 0.4, 0.2); // just above where the sun patch lands
117
+ engine.scene.add(bounce);
118
+ }
119
+ }
120
+ // spots the spec reads: the floor along the north wall and the north wall's lower part, both out of the sun patch
121
+ const points = { corner: [-0.5, 0.03, -2.5], northWall: [1.5, 0.6, -2.98] };
122
+ // the bake renders cubes between frames, so it runs after start; tracked in engine.loading so screenshots and tests wait for it
123
+ const ready = engine.start().then(() => engine.loading.track('fill', setupFill));
124
+ Object.assign(window, { engine, interiorTest: { THREE, ready, points, zone, fill, night } });
@@ -0,0 +1,125 @@
1
+ Modular buildings on a grid: parameterized wall / window / door / roof / trim modules, an assembler for buildings and seeded streets, any style.
2
+
3
+ # Modular buildings (technique example)
4
+
5
+ A kit of building pieces made in code, snapped to a grid and assembled into buildings and whole streets, with the colours and materials left to
6
+ the game. The pieces are plain on purpose: the same kit draws a cozy town, a gray block-out or a cold sci-fi row by swapping one set of
7
+ materials and one set of trim profiles. Run it with `npm run dev` and open `/modular-building.html` (`?set=gray|warm|cool`, `seed=1`,
8
+ `count=10`, `view=street|close`, `batch=0|merge`). The engine never imports this folder; copy it and change anything.
9
+
10
+ ## The principle
11
+ A town reads from far to near: skyline first, then the rhythm of openings, then trim, then small detail. A grid makes every piece fit every
12
+ slot, and a few repeated pieces are both cheap to draw and what makes a street look built rather than noisy. These are ways to judge your own
13
+ pieces, not fixed recipes; the numbers in `kit.js` are this example's current params, picked for a street seen from a few metres to a few dozen.
14
+
15
+ 1. **Block out first: grid and silhouettes.** Decide the cell (one wall module wide) and the storey height from the player's size: block out
16
+ with the player model (or a capsule of its size) in view, or a door that looks fine alone is too short next to the player. Then place
17
+ plain boxes for whole buildings and look at the street from the game's camera. Height, width and roof shape are what read from far away;
18
+ if the skyline is dull, no window detail will save it.
19
+ 2. **Then modules.** Replace blank cells with windows and doors. Every module of one kind shares its outline with its neighbours (same cell
20
+ width, same storey height, outer face on the same plane), so any module fits any slot.
21
+ 3. **Then trim and detail, big to small.** Big: roof shape, floor count, overhangs. Medium: cornices and floor bands, pilasters on corners,
22
+ awnings, signs. Small: sills, mullions, planters. Each level should still read at the distance where the player sees it; detail smaller
23
+ than a few pixels on screen costs triangles and turns into noise or shimmer.
24
+ 4. **One texture language.** Pick one way surfaces get their pattern (flat colour, vertex AO, one kind of canvas mask) and use it everywhere.
25
+ 5. **Change style by swapping, not rewriting.** A style is a set of materials plus trim cross-sections plus window variants.
26
+ 6. **Variation from a few choices, not continuous noise.** Pick each trait from a short list (floors, width, roof, window variant, trim
27
+ profile, door position, shop front) with a seeded random, and mirror upper floors around the middle so facades look designed. A few
28
+ variants keep geometry shared while no two neighbours repeat; a random size per window makes every window unique (noise, and nothing
29
+ batches). A street of one window reads as copy-paste.
30
+ 7. **Facades follow the inside.** A door on a side wall facing another building, or windows where an interior wall would be: if the game
31
+ has interiors, derive facades from the floor plan.
32
+
33
+ How to judge it: a shot at the game's real camera, not a close-up. Does the skyline vary? Do neighbours differ? Does any detail shimmer or
34
+ vanish at that distance? Does anything flicker at seams when the camera moves?
35
+
36
+ ## Plain three.js
37
+ - Pieces: `BoxGeometry`, `RoundedBoxGeometry` (addon), and trim strips as an `ExtrudeGeometry` of a `Shape` cross-section
38
+ ([ExtrudeGeometry](https://threejs.org/docs/#api/en/geometries/ExtrudeGeometry)). Each module a `Group` placed at integer cell offsets.
39
+ - Many copies: [InstancedMesh](https://threejs.org/docs/#api/en/objects/InstancedMesh) per repeated piece
40
+ ([webgl_instancing_performance](https://threejs.org/examples/#webgl_instancing_performance)), or
41
+ [BatchedMesh](https://threejs.org/docs/#api/en/objects/BatchedMesh) for mixed geometries sharing a material
42
+ ([webgl_mesh_batch](https://threejs.org/examples/#webgl_mesh_batch)).
43
+ - Merging: `mergeGeometries` from [BufferGeometryUtils](https://threejs.org/docs/#examples/en/utils/BufferGeometryUtils) gives one draw per
44
+ material, at the cost of culling and per-piece edits.
45
+ - Seams: avoid coplanar faces; if one is unavoidable, `material.polygonOffset` is the last resort, not the plan.
46
+
47
+ ## Style routes
48
+ - **Gray block-out**: boxes and `.` cells only; judges silhouette and scale before any art. Cheapest.
49
+ - **Cozy / storybook**: soft cove trims, rounded edges, warm slot materials, awnings and planters. More trim strips (one-off geometry).
50
+ - **Cold sci-fi / brutalist**: hard stepped trims, flat roofs, faintly emissive frames. Fewer pieces, more repetition.
51
+ - **Low-poly flat**: flat colour per slot, vertex AO for depth, no textures. Very cheap.
52
+ - **Hand-painted / textured**: one texture set per slot from a DCC tool, the kit only for layout. Texture memory instead of geometry.
53
+ - **Merged hero block**: a fixed set piece merged once for the fewest draws, when it never changes.
54
+
55
+ ## The engine helper (optional convenience)
56
+ `kit.js` is the kit: modules, assembler, manifest entries; it imports only `three` and `three/addons`. `main.js` is the demo page; it builds the
57
+ same seed three times (gray block-out, warm with soft cove trims, cool with hard stepped trims and faintly emissive frames; the two coloured
58
+ sets are demo values, not advice). In the kit, the block-out stage is `facade` strings of `.` (blank walls) and `roof`. A style is the slot
59
+ materials (`wall`, `trim`, `roof`, `frame`, `glass`, `accent`, `awning`, `sign`) plus `profiles` plus `windows`. The only textures are gray
60
+ masks (awning stripes, sign text) tinted by the slot material's colour, so a material swap recolours them too. `street` mirrors upper floors
61
+ and picks traits with a seeded `rng`.
62
+
63
+ ```js
64
+ import { createModularKit, PROFILES, WINDOWS } from './kit.js';
65
+ const kit = createModularKit({
66
+ slots: { wall, trim, roof, frame, glass, accent, awning, sign }, // the game's materials; only wall is required, the rest fall back
67
+ cell: 2, storey: 3, wall: 0.2, // grid cell, floor height, wall thickness (metres)
68
+ profiles: PROFILES, windows: WINDOWS, // trim cross-sections and window variants: swap them per style
69
+ ao: { bake: bakeVertexAO, samples: 24 }, // optional: threedog's bakeVertexAO per module (slots then need vertexColors)
70
+ });
71
+ engine.assets.manifest(kit.pack); // mb-wall, mb-window, mb-door, mb-corner, mb-trim, mb-gable, mb-flatRoof, mb-awning,
72
+ // mb-sign, mb-chimney, mb-building, mb-street; all { build, params }
73
+ // content/props.js: { model: ['mb-street', { seed: 7, count: 12 }], x: 0, z: 0 } or { model: ['mb-building', { floors: 3 }], ... }
74
+ const b = kit.building({ width: 3, depth: 2, floors: 2, front: 'SDS/WWW', back: 'W.W', roof: 'gable', trim: 'cove', sign: 'BAKERY' });
75
+ const s = kit.street({ seed: 7, count: 10, gap: 1 });
76
+ s.userData.batch = true; engine.scene.add(s); // opt in to engine.batching (recipe 05, 2b)
77
+ engine.scene.add(kit.merge(s)); // or: one mesh per material (see costs)
78
+ kit.modules.window({ width: 1.1, height: 1.5, sill: 0.8, mullions: 2, planter: true }); // any single module as a Group
79
+ ```
80
+ - Each module is a `Group` with one mesh per slot (pieces of the same slot are merged), identity transforms, geometry shared by every copy
81
+ with the same params. `userData.module = { type, side, floor, i }` names each placed module; the wall slab mesh is named `wall`.
82
+ - Because every piece is a registered `{ build, params }` entry, `threedog gallery` shows each module on a turntable, saving `kit.js` swaps
83
+ the pieces in a running game, and `?panel=1` drags the params (`threedog tune write` writes them back).
84
+ - `kit.dispose()` frees the geometries, textures and textured materials it made; the slot materials are the game's.
85
+
86
+ Costs (seed 1, 10 buildings, 960x540, `npm run shot`, 2026-11-02). Draw calls as `describe().perf` counts them (the shadow pass included),
87
+ about 74k triangles in every case:
88
+
89
+ | | draw calls |
90
+ |---|---|
91
+ | plain meshes (`?batch=0`) | 1377 |
92
+ | `engine.batching` (`userData.batch = true`, the default page) | 308 |
93
+ | `kit.merge` (`?batch=merge`) | 24 |
94
+
95
+ - `engine.batching` instances meshes that repeat at least 8 times in a 64 m cell: windows, walls, corners. What stays single is one-off
96
+ geometry: each building's roof and trim strips (their length is the building's), signs. Fewer distinct sizes = fewer draws.
97
+ - `kit.merge` gives one draw per material, but the whole merged street is drawn whenever any of it is on screen, and single pieces can no
98
+ longer be moved or swapped. Merge per block (a few buildings) in a large town so culling still works.
99
+ - Rounded edges use `RoundedBoxGeometry` with one segment (108 triangles per piece instead of 300 with two): a chamfer with smooth normals
100
+ catches the same highlight, and sills / pilasters / copings repeat hundreds of times.
101
+ - This kit's 4 cm mullions are fine up close and vanish or shimmer a few dozen metres away: size each level of detail for your camera.
102
+
103
+ Rules (correctness):
104
+ - **Every facade module spans exactly one cell and one storey, with its outer face at module z = 0**, details in front (+z), the slab behind.
105
+ A module that breaks this leaves gaps or overlaps that the assembler cannot fix.
106
+ - **Building origins land on whole cells** (`street` places each building at an integer cell offset). Off-grid origins break the snapping
107
+ the spec checks.
108
+ - **A facade row has one letter per footprint cell** (`W` window, `S` shop window with awning, `D` door, `.` blank); rows are separated by `/`,
109
+ ground floor first, and the last row repeats upward. A wrong length or a door above the ground floor throws, naming the side.
110
+ - **No two visible faces share a plane.** Two faces in one plane flicker when the camera moves; never rely on "same colour, nobody will
111
+ notice". Where pieces meet, either one covers the seam (corner pilasters cover the wall ends; front / back trims run past the corners,
112
+ side trims stop at the walls) or they are offset by a few millimetres (the sill top sits above the reveal floor).
113
+ - **Generators get every input from `params`.** The slot materials are live objects, so the kit's manifest entries are `cache: false`
114
+ (the dev cache would otherwise keep a model built with old materials). To bake a street, make the slots `makeMaterial` roles inside the
115
+ generator instead (recipe 05, section 2a) and drop `cache: false`.
116
+
117
+ Limits:
118
+ - Footprints are rectangles of cells (`width` x `depth`); an L-shaped building needs `building` extended to take a cell list (two buildings
119
+ with `gap: 0` meet wall to wall, and their corner pilasters then overlap).
120
+ - Gable ridges run along the building's width; hip roofs, dormers and balconies are not in the kit (each is one more module).
121
+ - The test is `npm test -- "modular building"` (tests/specs/modular-building.mjs): determinism per seed, grid snapping without gaps,
122
+ shared geometry, batching under 400 draw calls, params changing shape, the optional AO pass.
123
+
124
+ Sources: written for this example; no code copied. Background: the grid-snapped kit of reusable pieces is common modular level design practice
125
+ (Joel Burgess, "Skyrim's Modular Approach to Level Design", GDC 2013); `RoundedBoxGeometry` and `mergeGeometries` are three.js addons.