threedog 0.5.0 → 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 (511) hide show
  1. package/CHANGELOG.md +81 -1
  2. package/README.md +21 -13
  3. package/core/Assets.js +23 -12
  4. package/core/Audio.js +1 -1
  5. package/core/Baked.js +14 -23
  6. package/core/Budget.js +2 -2
  7. package/core/DebugOverrides.js +33 -18
  8. package/core/Describe.js +10 -11
  9. package/core/Engine.js +50 -46
  10. package/core/I18n.js +2 -2
  11. package/core/Input.js +11 -5
  12. package/core/Inspect.js +6 -3
  13. package/core/InspectMaterials.js +2 -3
  14. package/core/InspectPerf.js +11 -4
  15. package/core/MaterialInfo.js +7 -5
  16. package/core/Rays.js +7 -2
  17. package/core/Stream.js +2 -3
  18. package/core/UrlState.js +49 -9
  19. package/core/Views.js +1 -0
  20. package/core/adaptive.js +2 -2
  21. package/core/assetEntry.js +5 -2
  22. package/core/assetLoad.js +13 -15
  23. package/core/assetManifest.js +6 -3
  24. package/core/assetReport.js +4 -4
  25. package/core/batchGroups.js +9 -1
  26. package/core/catalog.js +16 -3
  27. package/core/chunkStream.js +1 -1
  28. package/core/copies.js +1 -1
  29. package/core/debugView.js +4 -4
  30. package/core/describeRender.js +104 -28
  31. package/core/describeText.js +6 -6
  32. package/core/errorCodes.js +72 -45
  33. package/core/errorHints.js +4 -1
  34. package/core/errors.js +4 -4
  35. package/core/events.generated.js +1 -0
  36. package/core/gameLights.js +9 -2
  37. package/core/lifecycle.js +2 -4
  38. package/core/lightWatch.js +5 -3
  39. package/core/loop.js +18 -11
  40. package/core/math.js +7 -0
  41. package/core/moduleSlots.js +32 -7
  42. package/core/moduleTypes.js +20 -7
  43. package/core/modules.js +7 -7
  44. package/core/namedModel.js +8 -4
  45. package/core/owned.js +33 -30
  46. package/core/page.js +43 -10
  47. package/core/pictureSlots.js +33 -0
  48. package/core/pictureWriters.js +343 -0
  49. package/core/plugins.js +64 -96
  50. package/core/sceneDelta.js +12 -4
  51. package/core/sceneMethods.js +2 -9
  52. package/core/services.js +1 -1
  53. package/core/shadowTypes.js +1 -1
  54. package/core/shadows.js +405 -0
  55. package/core/sharedPoint.js +5 -19
  56. package/core/stackFrames.js +16 -5
  57. package/core/stacks.js +6 -6
  58. package/core/startup.js +9 -11
  59. package/core/thingExplain.js +1 -1
  60. package/core/things.js +1 -1
  61. package/core/tier.js +1 -1
  62. package/core/util.js +1 -1
  63. package/core/validate.js +1 -1
  64. package/core/view.js +43 -0
  65. package/docs/AI.md +62 -58
  66. package/docs/API.md +15 -15
  67. package/docs/CODEMAP.md +36 -54
  68. package/docs/COMMANDS.md +28 -25
  69. package/docs/CONVENTIONS.md +36 -30
  70. package/docs/MIGRATING-0.4.md +93 -1
  71. package/docs/MIGRATING-0.6.md +126 -0
  72. package/docs/MIGRATING-0.7.md +39 -0
  73. package/docs/THREE.md +24 -22
  74. package/docs/api/assets.md +5 -3
  75. package/docs/api/audio.md +2 -2
  76. package/docs/api/budget.md +4 -0
  77. package/docs/api/characters.md +3 -1
  78. package/docs/api/collision.md +2 -2
  79. package/docs/api/engine.md +47 -20
  80. package/docs/api/events.md +2 -1
  81. package/docs/api/grids.md +8 -1
  82. package/docs/api/ground.md +1 -1
  83. package/docs/api/hud.md +3 -0
  84. package/docs/api/input.md +3 -2
  85. package/docs/api/interactions.md +3 -1
  86. package/docs/api/inventory.md +1 -2
  87. package/docs/api/level.md +0 -1
  88. package/docs/api/lightPool.md +1 -1
  89. package/docs/api/occlusion.md +5 -0
  90. package/docs/api/particles.md +6 -3
  91. package/docs/api/pickups.md +6 -0
  92. package/docs/api/post.md +5 -3
  93. package/docs/api/sky.md +12 -10
  94. package/docs/api/terrain.md +12 -6
  95. package/docs/api/terrainDraw.md +12 -0
  96. package/docs/api/tools.md +8 -7
  97. package/docs/api/triggers.md +2 -0
  98. package/docs/api/vegetation.md +7 -2
  99. package/docs/api/vegetationDraw.md +10 -0
  100. package/docs/api/water.md +6 -2
  101. package/docs/api/weather.md +5 -0
  102. package/docs/api/weatherDraw.md +10 -0
  103. package/docs/api/world.md +1 -1
  104. package/docs/recipes/01-new-game.md +19 -23
  105. package/docs/recipes/03-items-pickups.md +25 -4
  106. package/docs/recipes/05-props-bake.md +53 -37
  107. package/docs/recipes/06-clock-events.md +3 -3
  108. package/docs/recipes/07-flow-scenes-pause.md +12 -3
  109. package/docs/recipes/08-weather-sky.md +45 -57
  110. package/docs/recipes/09-materials-lights-models.md +185 -0
  111. package/docs/recipes/10-blender-level.md +2 -2
  112. package/docs/recipes/11-vehicles.md +3 -3
  113. package/docs/recipes/13-animation.md +13 -7
  114. package/docs/recipes/14-custom-module.md +7 -5
  115. package/docs/recipes/15-perf.md +3 -3
  116. package/docs/recipes/16-dev-bridge.md +22 -12
  117. package/docs/recipes/18-world.md +10 -8
  118. package/docs/recipes/19-shooting-picking.md +7 -5
  119. package/docs/recipes/20-content-actions-save.md +6 -2
  120. package/docs/recipes/21-camera.md +8 -3
  121. package/docs/recipes/23-ops.md +1 -1
  122. package/docs/recipes/26-ui-parts.md +35 -11
  123. package/docs/recipes/28-terrain.md +12 -12
  124. package/docs/recipes/29-grid.md +14 -5
  125. package/docs/recipes/30-templates.md +8 -3
  126. package/docs/recipes/31-audio.md +4 -4
  127. package/docs/recipes/32-action-game.md +18 -4
  128. package/docs/recipes/34-environment-lighting.md +102 -38
  129. package/docs/recipes/35-picture-from-reference.md +147 -0
  130. package/docs/recipes/36-sky-ground-plants.md +158 -0
  131. package/docs/recipes/{37-terrain-look-hooks.md → 37-terrain-hooks.md} +9 -13
  132. package/docs/recipes/38-environment-hooks.md +67 -0
  133. package/docs/recipes/40-scene.md +106 -27
  134. package/docs/recipes/42-terrain-shape.md +1 -1
  135. package/docs/recipes/{43-shader-patches-surface-looks.md → 43-shader-patches-surfaces.md} +66 -77
  136. package/docs/recipes/44-water-weather-hooks.md +191 -0
  137. package/docs/recipes/45-cascaded-shadows.md +121 -23
  138. package/docs/recipes/46-blender-art-route.md +17 -20
  139. package/docs/recipes/47-island-snow-backdrop.md +10 -12
  140. package/docs/recipes/49-game-module-contract.md +1 -1
  141. package/docs/recipes/50-own-renderer.md +3 -3
  142. package/docs/recipes/52-around-the-picture.md +181 -0
  143. package/docs/recipes/53-ground-up-close.md +12 -12
  144. package/docs/recipes/54-three-idioms.md +18 -18
  145. package/docs/recipes/55-mods-time-wrap.md +17 -5
  146. package/docs/recipes/56-webgpu-tsl.md +66 -23
  147. package/docs/recipes/57-control.md +39 -0
  148. package/docs/recipes/58-input.md +3 -0
  149. package/docs/recipes/59-depth-post-effects.md +5 -5
  150. package/docs/recipes/60-judge-the-picture.md +39 -24
  151. package/docs/recipes/61-deploy.md +27 -0
  152. package/docs/recipes/62-picture-code.md +244 -0
  153. package/docs/recipes/63-third-person-start.md +77 -0
  154. package/docs/recipes/64-particles.md +35 -0
  155. package/examples/action/.threedog/content-index.json +1 -1
  156. package/examples/action/GAMEMAP.md +7 -5
  157. package/examples/action/main.js +5 -4
  158. package/examples/action/models.js +12 -14
  159. package/examples/action/picture/index.js +13 -0
  160. package/examples/action/picture/lights.js +35 -0
  161. package/examples/action/picture/materials.js +8 -0
  162. package/examples/action/picture/palette.js +33 -0
  163. package/examples/plain-picture/.threedog/content-index.json +1 -0
  164. package/examples/plain-picture/FINDINGS.md +53 -0
  165. package/examples/plain-picture/GAMEMAP.md +90 -0
  166. package/examples/plain-picture/main.js +48 -0
  167. package/examples/plain-picture/picture/index.js +21 -0
  168. package/examples/plain-picture/picture/lights.js +54 -0
  169. package/examples/plain-picture/picture/materials.js +24 -0
  170. package/examples/plain-picture/picture/palette.js +16 -0
  171. package/examples/plain-picture/picture/sky.js +37 -0
  172. package/examples/pullable.json +31 -9
  173. package/examples/rc2d/GAMEMAP.md +8 -3
  174. package/examples/rc2d/README.md +11 -6
  175. package/examples/rc2d/main.js +25 -33
  176. package/examples/rc2d/picture/index.js +18 -0
  177. package/examples/rc2d/picture/lights.js +11 -0
  178. package/examples/rc2d/picture/materials.js +18 -0
  179. package/examples/rc2d/picture/palette.js +15 -0
  180. package/examples/rc2d/picture/post.js +11 -0
  181. package/examples/rc2d/rc2d.js +18 -28
  182. package/examples/sheepisland/.threedog/content-index.json +1 -1
  183. package/examples/sheepisland/GAMEMAP.md +13 -10
  184. package/examples/sheepisland/assets.js +2 -2
  185. package/examples/sheepisland/content/templates.js +1 -1
  186. package/examples/sheepisland/content/vegetation.js +3 -1
  187. package/examples/sheepisland/main.js +15 -9
  188. package/examples/sheepisland/{style/models.js → models.js} +13 -24
  189. package/examples/sheepisland/picture/index.js +20 -0
  190. package/examples/sheepisland/picture/lights.js +34 -0
  191. package/examples/sheepisland/picture/materials.js +61 -0
  192. package/examples/sheepisland/picture/palette.js +60 -0
  193. package/examples/sheepisland/picture/sky.js +20 -0
  194. package/examples/sheepisland/views.js +1 -1
  195. package/examples/styletest/.threedog/content-index.json +1 -1
  196. package/examples/styletest/GAMEMAP.md +15 -11
  197. package/examples/styletest/main.js +20 -30
  198. package/examples/styletest/models.js +29 -29
  199. package/examples/styletest/params.js +1 -4
  200. package/examples/styletest/picture/env.js +38 -0
  201. package/examples/styletest/picture/index.js +16 -0
  202. package/examples/styletest/picture/lights.js +9 -0
  203. package/examples/styletest/picture/materials.js +28 -0
  204. package/examples/styletest/picture/palette.js +22 -0
  205. package/examples/techniques/contact-shadows/README.md +22 -23
  206. package/examples/techniques/contact-shadows/contactShadows.js +32 -16
  207. package/examples/techniques/contact-shadows/main.js +20 -18
  208. package/examples/techniques/contact-shadows/picture/index.js +14 -0
  209. package/examples/techniques/contact-shadows/picture/lights.js +34 -0
  210. package/examples/techniques/contact-shadows/picture/materials.js +12 -0
  211. package/examples/techniques/contact-shadows/picture/palette.js +17 -0
  212. package/examples/techniques/day-night-grade/README.md +13 -16
  213. package/examples/techniques/day-night-grade/main.js +15 -25
  214. package/examples/techniques/day-night-grade/picture/index.js +20 -0
  215. package/examples/techniques/day-night-grade/picture/lights.js +45 -0
  216. package/examples/techniques/day-night-grade/picture/materials.js +15 -0
  217. package/examples/techniques/day-night-grade/picture/palette.js +59 -0
  218. package/examples/techniques/day-night-grade/picture/post.js +25 -0
  219. package/examples/techniques/day-night-grade/picture/sky.js +33 -0
  220. package/examples/techniques/interior-light/README.md +16 -13
  221. package/examples/techniques/interior-light/interiorLight.js +19 -17
  222. package/examples/techniques/interior-light/main.js +30 -67
  223. package/examples/techniques/interior-light/picture/index.js +24 -0
  224. package/examples/techniques/interior-light/picture/lights.js +52 -0
  225. package/examples/techniques/interior-light/picture/materials.js +13 -0
  226. package/examples/techniques/interior-light/picture/palette.js +32 -0
  227. package/examples/techniques/interior-light/picture/sky.js +40 -0
  228. package/examples/techniques/modular-building/README.md +4 -3
  229. package/examples/techniques/modular-building/kit.js +2 -2
  230. package/examples/techniques/modular-building/main.js +9 -17
  231. package/examples/techniques/modular-building/picture/index.js +17 -0
  232. package/examples/techniques/modular-building/picture/lights.js +26 -0
  233. package/examples/techniques/modular-building/picture/materials.js +13 -0
  234. package/examples/techniques/modular-building/picture/palette.js +23 -0
  235. package/examples/techniques/night-lights/README.md +15 -14
  236. package/examples/techniques/night-lights/main.js +20 -43
  237. package/examples/techniques/night-lights/nightLights.js +25 -21
  238. package/examples/techniques/night-lights/picture/index.js +18 -0
  239. package/examples/techniques/night-lights/picture/lights.js +44 -0
  240. package/examples/techniques/night-lights/picture/materials.js +15 -0
  241. package/examples/techniques/night-lights/picture/palette.js +34 -0
  242. package/examples/techniques/night-lights/picture/post.js +9 -0
  243. package/examples/techniques/night-lights/picture/sky.js +36 -0
  244. package/examples/techniques/outlines-and-rim/README.md +2 -2
  245. package/examples/techniques/outlines-and-rim/main.js +15 -38
  246. package/examples/techniques/outlines-and-rim/outlines.js +3 -3
  247. package/examples/techniques/outlines-and-rim/picture/index.js +16 -0
  248. package/examples/techniques/outlines-and-rim/picture/lights.js +18 -0
  249. package/examples/techniques/outlines-and-rim/picture/materials.js +11 -0
  250. package/examples/techniques/outlines-and-rim/picture/palette.js +16 -0
  251. package/examples/techniques/outlines-and-rim/picture/post.js +10 -0
  252. package/examples/techniques/procedural-animation/README.md +5 -3
  253. package/examples/techniques/procedural-animation/main.js +9 -7
  254. package/examples/techniques/procedural-animation/picture/index.js +15 -0
  255. package/examples/techniques/procedural-animation/picture/lights.js +35 -0
  256. package/examples/techniques/procedural-animation/picture/materials.js +10 -0
  257. package/examples/techniques/procedural-animation/picture/palette.js +16 -0
  258. package/examples/techniques/procedural-animation/picture/sky.js +31 -0
  259. package/examples/techniques/procedural-animation/proceduralAnim.js +1 -1
  260. package/examples/techniques/procedural-surface/README.md +2 -0
  261. package/examples/techniques/procedural-surface/main.js +17 -55
  262. package/examples/techniques/procedural-surface/picture/index.js +14 -0
  263. package/examples/techniques/procedural-surface/picture/lights.js +18 -0
  264. package/examples/techniques/procedural-surface/picture/materials.js +34 -0
  265. package/examples/techniques/procedural-surface/picture/palette.js +15 -0
  266. package/examples/techniques/stylized-shading/README.md +13 -14
  267. package/examples/techniques/stylized-shading/main.js +11 -42
  268. package/examples/techniques/stylized-shading/picture/index.js +14 -0
  269. package/examples/techniques/stylized-shading/picture/lights.js +23 -0
  270. package/examples/techniques/stylized-shading/picture/materials.js +18 -0
  271. package/examples/techniques/stylized-shading/picture/palette.js +19 -0
  272. package/examples/techniques/stylized-shading/shading.js +2 -2
  273. package/examples/techniques/sun-and-key-light/README.md +20 -22
  274. package/examples/techniques/sun-and-key-light/keyLight.js +47 -29
  275. package/examples/techniques/sun-and-key-light/main.js +19 -63
  276. package/examples/techniques/sun-and-key-light/picture/index.js +13 -0
  277. package/examples/techniques/sun-and-key-light/picture/lights.js +33 -0
  278. package/examples/techniques/sun-and-key-light/picture/materials.js +9 -0
  279. package/examples/techniques/sun-and-key-light/picture/palette.js +29 -0
  280. package/examples/techniques/sun-and-key-light/picture/sky.js +16 -0
  281. package/examples/techniques/three-looks/README.md +21 -0
  282. package/examples/techniques/three-looks/main.js +58 -0
  283. package/examples/techniques/three-looks/picture/cartoon.js +23 -0
  284. package/examples/techniques/three-looks/picture/cinematic.js +27 -0
  285. package/examples/techniques/three-looks/picture/comic.js +27 -0
  286. package/examples/techniques/three-looks/picture/index.js +14 -0
  287. package/examples/techniques/three-looks/picture/sun.js +23 -0
  288. package/examples/techniques/wet-road/README.md +7 -4
  289. package/examples/techniques/wet-road/main.js +18 -38
  290. package/examples/techniques/wet-road/picture/index.js +17 -0
  291. package/examples/techniques/wet-road/picture/lights.js +57 -0
  292. package/examples/techniques/wet-road/picture/materials.js +16 -0
  293. package/examples/techniques/wet-road/picture/palette.js +24 -0
  294. package/examples/techniques/wet-road/picture/sky.js +46 -0
  295. package/examples/webgpu-tsl/GAMEMAP.md +9 -4
  296. package/examples/webgpu-tsl/README.md +13 -10
  297. package/examples/webgpu-tsl/main.js +30 -7
  298. package/examples/webgpu-tsl/picture/index.js +18 -0
  299. package/examples/webgpu-tsl/picture/lights.js +25 -0
  300. package/examples/webgpu-tsl/picture/materials.js +36 -0
  301. package/examples/webgpu-tsl/picture/palette.js +23 -0
  302. package/examples/webgpu-tsl/picture/post.js +16 -0
  303. package/examples/webgpu-tsl/picture/sky.js +30 -0
  304. package/examples/world/.threedog/content-index.json +1 -1
  305. package/examples/world/GAMEMAP.md +15 -12
  306. package/examples/world/art/humanoid.js +9 -13
  307. package/examples/world/art/index.js +2 -2
  308. package/examples/world/art/models.js +3 -4
  309. package/examples/world/art/shapes.js +2 -2
  310. package/examples/world/assets.js +2 -2
  311. package/examples/world/content/audio.js +1 -1
  312. package/examples/world/content/items.js +1 -1
  313. package/examples/world/content/props.js +1 -1
  314. package/examples/world/content/templates.js +1 -1
  315. package/examples/world/content/vegetation.js +1 -1
  316. package/examples/world/main.js +10 -10
  317. package/examples/world/models.js +17 -7
  318. package/examples/world/picture/index.js +20 -0
  319. package/examples/world/picture/lights.js +41 -0
  320. package/examples/world/picture/materials.js +34 -0
  321. package/examples/world/picture/palette.js +74 -0
  322. package/examples/world/picture/post.js +9 -0
  323. package/examples/world/picture/sky.js +39 -0
  324. package/index.d.ts +7 -16
  325. package/index.js +7 -7
  326. package/llms.txt +15 -13
  327. package/modules/ambience/Ambience.js +1 -1
  328. package/modules/calendar/Calendar.js +1 -1
  329. package/modules/calendar/index.js +1 -0
  330. package/modules/camera/FirstPersonCamera.js +1 -1
  331. package/modules/camera/FixedCamera.js +2 -2
  332. package/modules/camera/RtsCamera.js +1 -1
  333. package/modules/camera/common.js +7 -5
  334. package/modules/characters/Character.js +15 -10
  335. package/modules/characters/NPC.js +5 -5
  336. package/modules/characters/assetModel.js +5 -4
  337. package/modules/characters/index.js +28 -3
  338. package/modules/characters/movers/common.js +6 -1
  339. package/modules/characters/movers/manual.js +3 -0
  340. package/modules/characters/movers/support.js +25 -11
  341. package/modules/characters/npcSim.js +5 -1
  342. package/modules/characters/proceduralRig.js +18 -4
  343. package/modules/characters/useModel.js +2 -2
  344. package/modules/collision/Collision.js +4 -2
  345. package/modules/content/index.js +8 -0
  346. package/modules/content/models.js +56 -11
  347. package/modules/content/props.js +5 -3
  348. package/modules/content/rules.js +12 -4
  349. package/modules/content/templates.js +5 -3
  350. package/modules/gameplay/Interactions.js +91 -16
  351. package/modules/gameplay/Inventory.js +3 -3
  352. package/modules/gameplay/dialogueBox.js +1 -1
  353. package/modules/gameplay/dialogueBubbles.js +1 -1
  354. package/modules/gameplay/interactionPrompt.js +1 -1
  355. package/modules/gameplay/interactions.contract.js +1 -1
  356. package/modules/gameplay/inventoryOps.js +2 -2
  357. package/modules/gameplay/pickups.js +62 -24
  358. package/modules/gameplay/questLog.js +2 -2
  359. package/modules/grid/Layer.js +13 -0
  360. package/modules/grid/index.js +40 -11
  361. package/modules/grid/preview.js +24 -15
  362. package/modules/grid/tiles.js +15 -19
  363. package/modules/hlod/index.js +3 -2
  364. package/modules/hud/index.js +13 -0
  365. package/modules/hud/parts.js +9 -7
  366. package/modules/level/index.js +5 -4
  367. package/modules/lights/index.js +12 -8
  368. package/modules/music/Music.js +1 -1
  369. package/modules/particles/Particles.js +41 -20
  370. package/modules/particles/effect.js +21 -6
  371. package/modules/photo/Photo.js +11 -10
  372. package/modules/settings/Settings.js +7 -7
  373. package/modules/settings/apply.js +5 -5
  374. package/modules/settings/sections.js +2 -2
  375. package/modules/settings/strings.js +1 -1
  376. package/modules/sky/Sky.js +104 -89
  377. package/modules/sky/clouds.js +16 -17
  378. package/modules/sky/dome.js +100 -0
  379. package/modules/sky/domeLayers.js +5 -5
  380. package/modules/sky/environment.js +45 -169
  381. package/modules/sky/fog.js +5 -180
  382. package/modules/sky/frame.js +26 -90
  383. package/modules/sky/season.js +3 -3
  384. package/modules/sky/shader.js +35 -29
  385. package/modules/sky/skyModule.js +25 -22
  386. package/modules/sky/tint.js +9 -11
  387. package/modules/terrain/ChunkManager.js +29 -12
  388. package/modules/terrain/draw.js +154 -0
  389. package/modules/terrain/index.js +44 -66
  390. package/modules/terrain/terrainDefaults.js +2 -2
  391. package/modules/terrain/terrainEdit.js +1 -1
  392. package/modules/terrain/terrainGen.js +2 -2
  393. package/modules/triggers/index.js +10 -6
  394. package/modules/vegetation/Vegetation.js +58 -17
  395. package/modules/vegetation/draw.js +191 -0
  396. package/modules/vegetation/groundChunks.js +1 -1
  397. package/modules/vegetation/index.js +10 -10
  398. package/modules/water/Water.js +135 -88
  399. package/modules/water/surface.js +53 -53
  400. package/modules/weather/Weather.js +52 -17
  401. package/modules/weather/particles.js +13 -5
  402. package/modules/weather/shaders.js +1 -1
  403. package/modules/weather/weatherDraw.js +141 -0
  404. package/modules/world/World.js +2 -2
  405. package/modules/world/views.js +1 -1
  406. package/package.json +3 -2
  407. package/presets.js +2 -2
  408. package/render/PostFX.js +199 -41
  409. package/render/backend.js +52 -6
  410. package/render/cascades.js +195 -0
  411. package/render/cascades.tsl.js +96 -0
  412. package/render/daylight.js +39 -0
  413. package/render/detail.js +5 -5
  414. package/render/effects.js +41 -26
  415. package/render/fx/cel.tsl.js +42 -0
  416. package/render/fx/index.js +7 -5
  417. package/render/fx/patchNodes.tsl.js +1 -1
  418. package/render/fx/postWebGPU.tsl.js +30 -7
  419. package/render/fx/sky.tsl.js +19 -92
  420. package/render/fx/water.tsl.js +1 -1
  421. package/render/layers.js +14 -0
  422. package/render/lighting.js +6 -5
  423. package/render/lightmap.js +142 -0
  424. package/render/lights.js +23 -201
  425. package/render/materials.js +44 -160
  426. package/render/neutral.js +23 -0
  427. package/render/occlusion.js +184 -0
  428. package/render/ownFrame.js +1 -1
  429. package/render/patches.js +34 -29
  430. package/render/postWebGL.js +11 -3
  431. package/render/probes.js +179 -122
  432. package/render/slots.js +4 -4
  433. package/render/terms.js +53 -101
  434. package/render/uniformDecl.js +1 -1
  435. package/types/core.d.ts +333 -296
  436. package/types/gameplay.d.ts +65 -26
  437. package/types/generated/core/Audio.d.ts +1 -1
  438. package/types/generated/core/DebugOverrides.d.ts +14 -6
  439. package/types/generated/core/Input.d.ts +9 -2
  440. package/types/generated/core/page.d.ts +9 -2
  441. package/types/generated/modules/grid/Layer.d.ts +8 -0
  442. package/types/generated/modules/grid/index.d.ts +29 -5
  443. package/types/generated/modules/particles/Particles.d.ts +13 -6
  444. package/types/generated/modules/sky/Sky.d.ts +85 -18
  445. package/types/generated/modules/sky/skyModule.d.ts +1 -1
  446. package/types/generated/modules/water/Water.d.ts +50 -3
  447. package/types/generated/modules/weather/Weather.d.ts +33 -3
  448. package/types/generated/modules/weather/weatherDraw.d.ts +11 -0
  449. package/types/generated/modules/world/World.d.ts +2 -2
  450. package/types/generated/render/PostFX.d.ts +53 -10
  451. package/types/generated/render/lighting.d.ts +29 -0
  452. package/types/generated/render/materials.d.ts +19 -44
  453. package/types/generated/render/occlusion.d.ts +13 -0
  454. package/types/physics.d.ts +5 -3
  455. package/types/tsl.d.ts +8 -0
  456. package/types/world.d.ts +376 -57
  457. package/ui/Focus.js +12 -2
  458. package/ui/LoadingScreen.js +2 -2
  459. package/ui/Parts.js +8 -4
  460. package/ui/UI.js +10 -10
  461. package/ui/cursor.js +1 -1
  462. package/ui/theme.js +33 -27
  463. package/ui/touchPart.js +1 -1
  464. package/core/look/coreParts.js +0 -219
  465. package/core/look/part.js +0 -253
  466. package/core/look/points.js +0 -255
  467. package/core/look/report.js +0 -96
  468. package/core/look/shadowPart.js +0 -63
  469. package/core/look/state.js +0 -45
  470. package/core/look/write.js +0 -70
  471. package/core/styleParts.js +0 -114
  472. package/docs/api/style.md +0 -136
  473. package/docs/api/terrainLook.md +0 -10
  474. package/docs/api/vegetationLook.md +0 -10
  475. package/docs/api/weatherLook.md +0 -10
  476. package/docs/recipes/09-style.md +0 -218
  477. package/docs/recipes/35-style-from-reference.md +0 -153
  478. package/docs/recipes/36-sky-ground-plant-looks.md +0 -118
  479. package/docs/recipes/38-environment-look-hooks.md +0 -99
  480. package/docs/recipes/41-thing-registry.md +0 -97
  481. package/docs/recipes/44-water-weather-look-hooks.md +0 -144
  482. package/docs/recipes/48-make-material.md +0 -75
  483. package/docs/recipes/52-look.md +0 -282
  484. package/examples/action/look.js +0 -5
  485. package/examples/sheepisland/style/index.js +0 -79
  486. package/examples/styletest/look.js +0 -11
  487. package/examples/techniques/day-night-grade/dayNightGrade.js +0 -76
  488. package/examples/webgpu-tsl/tslScene.js +0 -75
  489. package/examples/world/style/index.js +0 -61
  490. package/examples/world/style/plugins/10-bloom.js +0 -5
  491. package/modules/sky/cascades.js +0 -186
  492. package/modules/sky/cascades.tsl.js +0 -82
  493. package/modules/sky/look.js +0 -54
  494. package/modules/sky/lookFog.js +0 -76
  495. package/modules/sky/lookShadow.js +0 -62
  496. package/modules/sky/lookSky.js +0 -169
  497. package/modules/sky/shadow.js +0 -98
  498. package/modules/terrain/look.js +0 -83
  499. package/modules/terrain/lookGround.js +0 -66
  500. package/modules/vegetation/look.js +0 -119
  501. package/modules/vegetation/lookWind.js +0 -98
  502. package/modules/water/look.js +0 -91
  503. package/modules/water/surfaceDefaults.js +0 -3
  504. package/modules/weather/look.js +0 -74
  505. package/modules/weather/weatherLook.js +0 -135
  506. package/render/lookGlow.js +0 -139
  507. package/render/lookSurface.js +0 -224
  508. package/render/postLook.js +0 -164
  509. package/render/surface.js +0 -105
  510. package/types/generated/modules/weather/weatherLook.d.ts +0 -12
  511. package/types/style.d.ts +0 -671
@@ -0,0 +1,45 @@
1
+ // @ts-check
2
+ // Sun or moon, ambient, exposure and the game's lamps, all from the keyframes. level() is the lamps' one number; the materials read it too.
3
+ import { THREE } from 'threedog';
4
+ import { COLORS, EXPOSURE, LAMPS, sample, skyColor, skyNumber } from './palette.js';
5
+
6
+ /** @param {any} engine */
7
+ export function createLights(engine) {
8
+ const { scene, renderer } = engine;
9
+ const hemi = new THREE.HemisphereLight(0xffffff, 0x4a4a4a, 1);
10
+ const sun = new THREE.DirectionalLight(0xffffff, 1);
11
+ sun.castShadow = true;
12
+ sun.shadow.mapSize.setScalar(engine.tier === 'low' ? 1024 : 2048);
13
+ Object.assign(sun.shadow.camera, { left: -24, right: 24, top: 24, bottom: -24, near: 1, far: 120 });
14
+ sun.shadow.normalBias = 0.04;
15
+ scene.add(hemi, sun, sun.target);
16
+
17
+ const lamps = /** @type {THREE.PointLight[]} */ ([]);
18
+ /** @param {number} x @param {number} y @param {number} z */
19
+ function addLamp(x, y, z) {
20
+ const l = new THREE.PointLight(COLORS.lamp, 0, 9, 1.6); // distance limits its cost to the street
21
+ l.position.set(x, y, z); scene.add(l); lamps.push(l);
22
+ }
23
+
24
+ let level = 0;
25
+ const smooth = (/** @type {number} */ x) => x * x * (3 - 2 * x);
26
+ const dir = new THREE.Vector3();
27
+ return {
28
+ sun, hemi, addLamp, level: () => level,
29
+ /** @param {any} s engine.uniforms */
30
+ update(s) {
31
+ const t = s.uTimeOfDay.value, d = s.uSunDir.value;
32
+ // below the horizon the same light is the moon, across the sky from where the sun went; it fades through 0 at the swap so shadows never jump
33
+ dir.set(d.y < 0 ? -d.x : d.x, Math.abs(d.y), d.z).normalize();
34
+ sun.position.copy(dir).multiplyScalar(60).add(sun.target.position);
35
+ sun.intensity = skyNumber(4, t) * Math.min(1, Math.abs(d.y) / 0.08);
36
+ skyColor(sun.color, 3, t);
37
+ skyColor(hemi.color, 5, t);
38
+ hemi.intensity = skyNumber(6, t);
39
+ renderer.toneMappingExposure = sample(EXPOSURE, t)[0];
40
+ level = 1 - smooth(Math.min(1, Math.max(0, (s.uDaylight.value - LAMPS.onBelow) / (LAMPS.offAbove - LAMPS.onBelow))));
41
+ // intensity only, never visible = false: a light count change recompiles every lit material (a stall at dusk)
42
+ for (const l of lamps) l.intensity = LAMPS.light * level;
43
+ },
44
+ };
45
+ }
@@ -0,0 +1,15 @@
1
+ // @ts-check
2
+ // Materials by name. Windows and lamp heads glow by the same level as the lamps: a lit window and a lit street must agree.
3
+ import { THREE } from 'threedog';
4
+ import { COLORS, LAMPS } from './palette.js';
5
+
6
+ export function createMaterials() {
7
+ const flat = (/** @type {number} */ color) => new THREE.MeshStandardMaterial({ color, roughness: 0.9 });
8
+ const glass = new THREE.MeshStandardMaterial({ color: COLORS.dark, emissive: COLORS.window, emissiveIntensity: 0, roughness: 0.9 });
9
+ const head = new THREE.MeshStandardMaterial({ color: COLORS.dark, emissive: COLORS.lampHead, emissiveIntensity: 0, roughness: 0.9 });
10
+ return {
11
+ mat: { ground: flat(COLORS.ground), wall: flat(COLORS.wall), roof: flat(COLORS.roof), figure: flat(COLORS.figure), crate: flat(COLORS.crate), glass, head },
12
+ /** @param {number} level the lamps' level, 0 off .. 1 on */
13
+ update(level) { glass.emissiveIntensity = LAMPS.window * level; head.emissiveIntensity = LAMPS.head * level; },
14
+ };
15
+ }
@@ -0,0 +1,59 @@
1
+ // @ts-check
2
+ // Day-night palette: one set of keyframes over the day for sky, light, exposure, grade and lamps. Numbers are a neutral starting point on a gray
3
+ // scene; the colors of your game come from its own picture, not from here.
4
+ import { THREE } from 'threedog';
5
+
6
+ /** Materials and lamp colors of the demo street, by name */
7
+ export const COLORS = { ground: 0x8e8e8e, wall: 0xb2b2b2, roof: 0x7a7a7a, figure: 0xa0a0a0, crate: 0x9c9c9c, dark: 0x202020, window: 0xffc27a, lampHead: 0xffd29a, lamp: 0xffc88a };
8
+ /** Sky keyframes: [time 0-1, zenith, horizon (= fog color), sun color (the moon at night), sun intensity, ambient color, ambient intensity] */
9
+ export const SKY = [
10
+ [0.00, 0x0d1626, 0x1f2c44, 0x8fa4d4, 0.35, 0x2f3147, 0.26], // night: the "sun" row is moonlight, blue and weak; ambient keeps a floor
11
+ [0.21, 0x172036, 0x3a4058, 0x9aa8d4, 0.35, 0x2f3147, 0.26],
12
+ [0.26, 0x55688c, 0xd09c7c, 0xffb27c, 1.2, 0x8088a8, 0.6], // sunrise: warm sun, horizon warm, zenith still cool (the contrast IS the picture)
13
+ [0.33, 0x6d92c2, 0xc6d2dc, 0xfff0dc, 2.4, 0xa0b6d4, 0.9],
14
+ [0.50, 0x6e98ce, 0xcfdae4, 0xfffaf2, 3.0, 0xa8bdd9, 1.1], // noon: sun 3 + ambient 1.1 ~ pi: mid-tone albedos show at their own value
15
+ [0.68, 0x6a8ebe, 0xd4d0c6, 0xfff0d8, 2.6, 0xa4b8d4, 1.0],
16
+ [0.73, 0x4c5c86, 0xdc9c6c, 0xffa264, 1.6, 0x7c88ac, 0.7], // dusk: warmth in the LIGHT color, ambient drops so shadows turn cool by contrast
17
+ [0.78, 0x222e4c, 0x5c5068, 0x9aa6d4, 0.35, 0x3c4060, 0.32], // blue hour: the moon row again, a little more ambient than deep night
18
+ [1.00, 0x0d1626, 0x1f2c44, 0x8fa4d4, 0.35, 0x2f3147, 0.26],
19
+ ];
20
+ /** Exposure over the day [time, exposure]: lifted at night so the moonlit scene reads, but never back to day brightness */
21
+ 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]];
22
+ /** Color grade over the day [time, temperature (-1 cool..1 warm), saturation, contrast] */
23
+ export const GRADE = [
24
+ [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
25
+ [0.22, -0.3, 0.8, 1.04],
26
+ [0.28, 0.35, 0.95, 1.0],
27
+ [0.40, 0, 1, 1],
28
+ [0.65, 0, 1, 1],
29
+ [0.73, 0.5, 0.9, 1.04], // dusk: warm, and saturation DOWN as warmth goes up, or oranges clip into neon
30
+ [0.79, -0.25, 0.78, 1.05],
31
+ [1.00, -0.45, 0.72, 1.06],
32
+ ];
33
+ /** Lamps: fully on below this daylight (0 night .. 1 noon), fading in above it. Daylight 0.35 is about 16:40: lamps come on before dark, as people switch them on */
34
+ export const LAMPS = { onBelow: 0.02, offAbove: 0.35, light: 14, window: 2.2, head: 3 };
35
+ export const TONE_MAPPING = THREE.AgXToneMapping;
36
+
37
+ /** The two keyframe rows around t and how far t is between them; rows are sorted by t and the table wraps at 1 @param {number[][]} rows @param {number} t */
38
+ function around(rows, t) {
39
+ let i = rows.findIndex(r => r[0] > t);
40
+ if (i <= 0) i = i === 0 ? 0 : rows.length - 1;
41
+ const a = rows[Math.max(0, i - 1)], b = rows[i], k = b[0] === a[0] ? 0 : (t - a[0]) / (b[0] - a[0]);
42
+ return { a, b, k: Math.min(1, Math.max(0, k)) };
43
+ }
44
+ /** Plain numbers at t @param {number[][]} rows @param {number} t */
45
+ export function sample(rows, t) {
46
+ const { a, b, k } = around(rows, t);
47
+ return a.slice(1).map((v, j) => v + (b[j + 1] - v) * k);
48
+ }
49
+ const ca = new THREE.Color(), cb = new THREE.Color();
50
+ /** One color column of SKY at t, blended as colors (hex numbers must not be interpolated as numbers) @param {THREE.Color} out @param {number} col @param {number} t */
51
+ export function skyColor(out, col, t) {
52
+ const { a, b, k } = around(SKY, t);
53
+ return out.copy(ca.set(a[col])).lerp(cb.set(b[col]), k);
54
+ }
55
+ /** One number column of SKY at t @param {number} col @param {number} t */
56
+ export function skyNumber(col, t) {
57
+ const { a, b, k } = around(SKY, t);
58
+ return a[col] + (b[col] - a[col]) * k;
59
+ }
@@ -0,0 +1,25 @@
1
+ // @ts-check
2
+ // The color grade: one plain ShaderPass whose uniforms the keyframes write directly, so nothing sits between the table and the shader.
3
+ import { ShaderPass } from 'three/addons/postprocessing/ShaderPass.js';
4
+ import { GRADE, sample } from './palette.js';
5
+
6
+ /** @param {any} engine */
7
+ export function createPost(engine) {
8
+ const pass = new ShaderPass({
9
+ uniforms: { tDiffuse: { value: null }, uTemp: { value: 0 }, uSat: { value: 1 }, uContrast: { value: 1 } },
10
+ vertexShader: 'varying vec2 vUv; void main(){ vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); }',
11
+ fragmentShader: `uniform sampler2D tDiffuse; uniform float uTemp; uniform float uSat; uniform float uContrast; varying vec2 vUv;
12
+ void main(){ vec4 c = texture2D(tDiffuse, vUv);
13
+ c.rgb += vec3(uTemp * 0.06, 0.0, -uTemp * 0.06);
14
+ c.rgb = clamp((c.rgb - 0.5) * uContrast + 0.5, 0.0, 1.0);
15
+ c.rgb = mix(vec3(dot(c.rgb, vec3(0.2126, 0.7152, 0.0722))), c.rgb, uSat);
16
+ gl_FragColor = c; }`,
17
+ });
18
+ const u = pass.uniforms, handle = engine.post.add(pass, { name: 'grade', slot: 'ldr' }); // 'ldr': after the tone curve, on display colors
19
+ return {
20
+ grade: () => ({ temperature: u.uTemp.value, saturation: u.uSat.value, contrast: u.uContrast.value }),
21
+ remove: () => handle.remove(),
22
+ /** @param {any} s engine.uniforms */
23
+ update(s) { [u.uTemp.value, u.uSat.value, u.uContrast.value] = sample(GRADE, s.uTimeOfDay.value); },
24
+ };
25
+ }
@@ -0,0 +1,33 @@
1
+ // @ts-check
2
+ // The sky is the game's: a gradient sphere from the keyframes, with fog in the horizon color. The engine's dome is hidden.
3
+ import { THREE } from 'threedog';
4
+ import { skyColor } from './palette.js';
5
+
6
+ /** @param {any} engine */
7
+ export function createSky(engine) {
8
+ const { scene } = engine;
9
+ scene.fog = new THREE.Fog(0x000000, 180, 600);
10
+ const skyMat = new THREE.ShaderMaterial({
11
+ side: THREE.BackSide, depthWrite: false, fog: false,
12
+ uniforms: { uTop: { value: new THREE.Color() }, uHorizon: { value: new THREE.Color() } },
13
+ vertexShader: 'varying vec3 vDir; void main(){ vDir = normalize(position); gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); }',
14
+ // tonemapping_fragment: the sky takes the same curve and exposure as the street, so a dusk horizon and a dusk wall stay in one picture
15
+ fragmentShader: `uniform vec3 uTop; uniform vec3 uHorizon; varying vec3 vDir;
16
+ void main(){ gl_FragColor = vec4(mix(uHorizon, uTop, smoothstep(-0.05, 0.45, vDir.y)), 1.0);
17
+ #include <tonemapping_fragment>
18
+ #include <colorspace_fragment>
19
+ }`,
20
+ });
21
+ const dome = new THREE.Mesh(new THREE.SphereGeometry(400, 32, 16), skyMat);
22
+ dome.renderOrder = -1;
23
+ scene.add(dome);
24
+ if (engine.sky) engine.sky.dome.visible = false;
25
+ return {
26
+ /** @param {any} s engine.uniforms */
27
+ update(s) {
28
+ const t = s.uTimeOfDay.value, u = skyMat.uniforms;
29
+ skyColor(u.uTop.value, 1, t); skyColor(u.uHorizon.value, 2, t);
30
+ /** @type {THREE.Fog} */ (scene.fog).color.copy(u.uHorizon.value);
31
+ },
32
+ };
33
+ }
@@ -13,8 +13,8 @@ Run `npm run dev` and open `/interior-light.html`. The engine never imports this
13
13
  | `&area=0` | no window area light |
14
14
  | `&leak=1` | keep the outdoor ambient indoors (shows the problem the zones solve) |
15
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).
16
+ The whole picture is plain three.js in `picture/`; every value of it sits in `picture/palette.js` (`SUN`, `MOON`, `AMBIENT`, `WINDOW_LIGHT`, `BOUNCE_LIGHT`,
17
+ `EXPOSURE`, `LAMPS`, `SHADOW`, `SKY`). `main.js` keeps the shapes (`ROOM`, `WINDOW`, `DOOR`, `VIEWS`). Related: `sun-and-key-light` (the sun itself), `contact-shadows`, `night-lights` (lights in an outdoor night).
18
18
 
19
19
  ## The principle
20
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
@@ -60,9 +60,10 @@ window dark but still readable if the game needs it readable. Check from the cam
60
60
 
61
61
  ## The engine helper (optional convenience)
62
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.
63
+ - `exposureZones(engine, { hemi, isInside, exposure, ambient, ease })`: changes exposure and the leaking outdoor ambient together, eased, and only
64
+ when the zone changes. It writes `hemi.intensity`, `scene.environmentIntensity` and `renderer.toneMappingExposure`; call its `update(dt)` from
65
+ `engine.onFrame`, and `update()` to jump to the zone's values at once. The page uses a box around the room and `EXPOSURE.day.outside` / `.inside`,
66
+ eased over `EXPOSURE.ease` seconds.
66
67
  - `windowLight(engine, { position, lookAt, width, height })`: a `RectAreaLight` with its one-time uniforms init done.
67
68
 
68
69
  | fill | what it gives | cost |
@@ -78,9 +79,9 @@ Zones vs measured exposure: a zone is cheap and predictable, and the designer de
78
79
  luminance, ease toward a target) adapts to anything but needs a downsample and readback every few frames, can pump when bright objects enter
79
80
  the view, and still needs limits. A zone also carries the ambient change, which measured exposure cannot. Limit of zones: they follow the
80
81
  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
+ their own fill (a probe grid over each part, or lightmaps).
82
83
 
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
+ Night (`?night=1`): a very low moon (`MOON`), the indoor zone's exposure at 1, two warm PointLights (physical falloff,
84
85
  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
86
  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
 
@@ -88,16 +89,18 @@ Rules (correctness):
88
89
  - `RectAreaLight` needs `RectAreaLightUniformsLib.init()` once, lights only `MeshStandardMaterial` / `MeshPhysicalMaterial`, and **casts no
89
90
  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
91
  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
92
+ - The sun through a window needs every wall, the ceiling and the window frame to `castShadow`, and a shadow box that covers the room. `picture/lights.js`
93
+ aims its own `DirectionalLight` and writes `shadow.camera` to fit the room. Thin walls want a small `normalBias` (here 1 cm) or light leaks along
94
+ their inner edge.
95
+ - `engine.bakeProbes({ bounds, spacing })` bakes the scene as it is at that moment: set the indoor ambient and the lights first, and
95
96
  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.
97
+ the bake also saw (recipe 34). Every pixel in the room takes its own light; outside `bounds` there is none,
98
+ so a building seen from outside needs its own light.
98
99
  - The bake renders after start; this page tracks it in `engine.loading` so screenshots and tests wait for it.
99
100
  - Do not name page URL parameters `time` or `view` (or the other engine scenario keys: `weather`, `pos`, `season`, `state`, `save`, `wait`,
100
101
  `cutscene`): `?time=night` reaches the sky as a clock time, turns into NaN and blacks out the frame.
101
102
 
102
103
  Sources: written for this repository, no code copied. Background: three.js `RectAreaLight` and `LightProbe` docs and examples (0.187), general
103
104
  architectural-visualization and game-lighting writing on window light, bounce and eye adaptation.
105
+
106
+ This example is written in GLSL: it is the WebGL route (`threedog new-game --webgl`). A game on the default WebGPU route writes the same ideas in TSL (recipe 56).
@@ -7,26 +7,28 @@ import { RectAreaLightUniformsLib } from 'three/addons/lights/RectAreaLightUnifo
7
7
 
8
8
  /**
9
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
10
+ * intensities switch at once (they light through walls) and the exposure eases over `ease` seconds (an eye adapting). Presentation only: call
11
+ * update(dt) from engine.onFrame; update() with no dt jumps straight to the zone's values. Writes hemi.intensity, scene.environmentIntensity and
12
+ * renderer.toneMappingExposure, nothing else.
13
+ * @param {{ camera: THREE.Camera, scene: THREE.Scene, renderer: THREE.WebGLRenderer }} engine
14
+ * @param {{ hemi: THREE.HemisphereLight, isInside: (p: THREE.Vector3) => boolean, exposure: { inside: number, outside: number }, ambient: { inside: { hemi: number, env: number }, outside: { hemi: number, env: number } }, ease?: number }} o
14
15
  */
15
- export function exposureZones(engine, { isInside, exposure, ambient, ease = 1.2, owner = 'zone' }) {
16
+ export function exposureZones(engine, { hemi, isInside, exposure, ambient, ease = 1.2 }) {
16
17
  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 }); }
18
+ return {
19
+ zone,
20
+ update(dt = Infinity) {
21
+ const name = isInside(engine.camera.position) ? 'inside' : 'outside';
22
+ if (name !== zone.name) {
23
+ zone.name = name;
24
+ zone.target = exposure[name];
25
+ hemi.intensity = ambient[name].hemi;
26
+ engine.scene.environmentIntensity = ambient[name].env;
27
+ }
28
+ zone.exposure = zone.exposure ? zone.exposure + (zone.target - zone.exposure) * Math.min(1, dt / (ease / 3)) : zone.target;
29
+ engine.renderer.toneMappingExposure = zone.exposure;
30
+ },
27
31
  };
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
32
  }
31
33
 
32
34
  /**
@@ -4,22 +4,15 @@
4
4
  // window), filling the shadowed side with bounce, the exposure change between outside and inside, and lamps at night.
5
5
  // (time and view are the engine's own URL scenario keys, see core/UrlState.js: page params use other names)
6
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 }.
7
+ // Test page for tests/specs/techniques-light.mjs: window.interiorTest = { THREE, ready, points, picture }. Shapes only here; the whole picture is picture/.
8
8
  import { createEngine, sky, fixedCamera, THREE } from 'threedog';
9
- import { exposureZones, windowLight } from './interiorLight.js';
9
+ import { createPicture } from './picture/index.js';
10
10
 
11
11
  // ---- parameters: change these ----------------------------------------------------------------------------------------------------------
12
12
  const ROOM = { min: [-4, 0, -3], max: [4, 3, 3], wall: 0.2 };
13
13
  const WINDOW = { z: [-1.1, 1.1], y: [0.8, 2.4] }; // opening in the west wall (x = -4)
14
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 }];
15
+ const LAMPS = /** @type {{ at: number[], kind: 'main' | 'side' }[]} */ ([{ at: [2.6, 1.55, -2.2], kind: 'main' }, { at: [-2.8, 1.0, 2.2], kind: 'side' }]);
23
16
  const VIEWS = {
24
17
  inside: { position: [3.4, 1.65, 2.4], lookAt: [-3, 0.8, -0.6] },
25
18
  doorway: { position: [1.95, 1.65, 5.2], lookAt: [0.6, 1.1, -3] },
@@ -31,40 +24,32 @@ const url = new URLSearchParams(location.search);
31
24
  const night = url.get('night') === '1', fill = url.get('fill') ?? 'probes', view = /** @type {keyof typeof VIEWS} */ (url.get('cam') ?? 'inside');
32
25
  if (!VIEWS[view]) throw new Error(`?cam=${view}: use one of ${Object.keys(VIEWS).join(', ')}`);
33
26
 
34
- const engine = createEngine({ canvas: /** @type {HTMLCanvasElement} */ (document.getElementById('view')), ui: { loading: false }, saveKey: 'threedog-interior' });
27
+ const engine = createEngine({ canvas: /** @type {HTMLCanvasElement} */ (document.getElementById('view')), lights: false, ui: { loading: false }, saveKey: 'threedog-interior' });
35
28
  engine.use(sky({ time: night ? '22:30' : '15:00' }));
36
29
  engine.use(fixedCamera({ shots: VIEWS, start: view }));
37
30
 
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' });
31
+ const [x0, , z0] = ROOM.min, [x1, h, z1] = ROOM.max, t = ROOM.wall, windowY = (WINDOW.y[0] + WINDOW.y[1]) / 2;
32
+ // the camera inside this box gets indoor ambient and indoor exposure, eased
33
+ const inside = (/** @type {THREE.Vector3} */ p) => p.x > x0 && p.x < x1 && p.z > z0 && p.z < z1 + 0.5 && p.y < h;
34
+ const picture = createPicture(engine, { night, fill, isInside: inside, leak: url.get('leak') === '1',
35
+ // the window as a light: a RectAreaLight in the opening, facing in
36
+ windowOpening: url.get('area') === '0' ? null : { position: [x0 - 0.02, windowY, 0], lookAt: [0, windowY, 0], width: WINDOW.z[1] - WINDOW.z[0], height: WINDOW.y[1] - WINDOW.y[0] } });
37
+ const { mat } = picture;
50
38
 
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) => {
39
+ // ---- the neutral gray room ----
40
+ const box = (/** @type {number[]} */ min, /** @type {number[]} */ max, m = mat.wall) => {
55
41
  const mesh = new THREE.Mesh(new THREE.BoxGeometry(max[0] - min[0], max[1] - min[1], max[2] - min[2]), m);
56
42
  mesh.position.set((min[0] + max[0]) / 2, (min[1] + max[1]) / 2, (min[2] + max[2]) / 2);
57
43
  mesh.castShadow = mesh.receiveShadow = true;
58
44
  engine.scene.add(mesh);
59
45
  return mesh;
60
46
  };
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));
47
+ const ground = new THREE.Mesh(new THREE.PlaneGeometry(120, 120), mat.ground);
63
48
  ground.rotation.x = -Math.PI / 2;
64
49
  ground.position.y = -0.01;
65
50
  ground.receiveShadow = true;
66
51
  engine.scene.add(ground);
67
- box([x0, -0.01, z0], [x1, 0.02, z1], floorMat); // the floor
52
+ box([x0, -0.01, z0], [x1, 0.02, z1], mat.floor); // the floor
68
53
  box([x0 - t, h, z0 - t], [x1 + t, h + t, z1 + t]); // the ceiling
69
54
  box([x0 - t, 0, z0 - t], [x1 + t, h, z0]); // north wall
70
55
  box([x1, 0, z0], [x1 + t, h, z1]); // east wall
@@ -74,51 +59,29 @@ box([x0 - t, 0, WINDOW.z[0]], [x0, WINDOW.y[0], WINDOW.z[1]]); box([x0 - t, WIND
74
59
  // south wall with the door
75
60
  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
61
  // 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
- }
62
+ box([-1.2, 0, -0.5], [0.4, 0.75, 0.5], mat.table); box([3.2, 0, -1.4], [3.95, 1.9, 0.6], mat.cabinet); box([-3.9, 0, 1.4], [-2.2, 0.8, 2.9], mat.sofa);
84
63
 
85
- // ---- night: lamps. The bulb hides inside a shade; the light lands on surfaces, the visible emitter stays dim (no glare without bloom) ----
64
+ // ---- night: lamps. The bulb hides inside a shade; the light lands on surfaces, the visible emitter stays dim ----
86
65
  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);
66
+ for (const { at, kind } of LAMPS) {
67
+ picture.addLamp(kind, at);
68
+ const shade = new THREE.Mesh(new THREE.CylinderGeometry(0.16, 0.26, 0.28, 24, 1, true), mat.shade[kind]);
69
+ shade.position.set(at[0], at[1], at[2]);
70
+ const stand = new THREE.Mesh(new THREE.CylinderGeometry(0.02, 0.02, at[1] - 0.14, 8), mat.stand);
71
+ stand.position.set(at[0], (at[1] - 0.14) / 2, at[2]);
72
+ engine.scene.add(shade, stand);
98
73
  }
99
74
  }
100
75
 
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 ----
76
+ // ---- fill: bounce baked into light probes; ?fill=light is one cheap point light in picture/lights.js ----
108
77
  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
- }
78
+ picture.snapZone(); // bake with indoor ambient, as the room is meant to be seen
79
+ if (fill !== 'probes') return;
80
+ // bounds = the room's air
81
+ 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 });
119
82
  }
120
83
  // spots the spec reads: the floor along the north wall and the north wall's lower part, both out of the sun patch
121
84
  const points = { corner: [-0.5, 0.03, -2.5], northWall: [1.5, 0.6, -2.98] };
122
85
  // the bake renders cubes between frames, so it runs after start; tracked in engine.loading so screenshots and tests wait for it
123
86
  const ready = engine.start().then(() => engine.loading.track('fill', setupFill));
124
- Object.assign(window, { engine, interiorTest: { THREE, ready, points, zone, fill, night } });
87
+ Object.assign(window, { engine, interiorTest: { THREE, ready, points, picture, fill, night } });
@@ -0,0 +1,24 @@
1
+ // @ts-check
2
+ // The picture of the interior-light demo: wiring only. Each part writes plain three.js.
3
+ import { exposureZones } from '../interiorLight.js';
4
+ import { AMBIENT, EXPOSURE, TONE_MAPPING } from './palette.js';
5
+ import { createLights } from './lights.js';
6
+ import { createMaterials } from './materials.js';
7
+ import { createSky } from './sky.js';
8
+
9
+ /**
10
+ * Returns { mat, sun, hemi, addLamp, zone, snapZone() }. snapZone(): the zone's values at once, without easing, for the bake.
11
+ * @param {any} engine
12
+ * @param {Parameters<typeof createLights>[1] & { isInside: (p: any) => boolean, leak: boolean }} o
13
+ */
14
+ export function createPicture(engine, { isInside, leak, ...o }) {
15
+ engine.renderer.toneMapping = TONE_MAPPING;
16
+ const sky = createSky(engine), lights = createLights(engine, o), { mat } = createMaterials();
17
+ const zones = exposureZones(engine, { hemi: lights.hemi, isInside, exposure: EXPOSURE[o.night ? 'night' : 'day'], ease: EXPOSURE.ease,
18
+ ambient: leak ? { inside: AMBIENT.outside, outside: AMBIENT.outside } : AMBIENT });
19
+ // onRender, not onFrame: onFrame runs before the sky module, so it would read last frame's signals
20
+ engine.onRender(() => { sky.update(engine.uniforms); lights.update(engine.uniforms); });
21
+ // the zones read no signals but must count every second, and a frame that is not drawn still passes time: onFrame sees those, onRender does not
22
+ engine.onFrame((/** @type {number} */ dt) => zones.update(dt));
23
+ return { mat, sun: lights.sun, hemi: lights.hemi, addLamp: lights.addLamp, zone: zones.zone, snapZone: () => zones.update() };
24
+ }
@@ -0,0 +1,52 @@
1
+ // @ts-check
2
+ // The room's lights: sun or moon aimed by hand with its shadow box fitted to the room, the hemisphere, the window as an area light, the bounce
3
+ // stand-in and the lamps. The sun stays fixed all day: this page is about the room, not the clock; only the hemisphere's colors read the signals.
4
+ import { THREE } from 'threedog';
5
+ import { windowLight } from '../interiorLight.js';
6
+ import { BOUNCE_LIGHT, HEMI, LAMPS, LAMP_SHADOW, MOON, SHADOW, SUN, WINDOW_LIGHT } from './palette.js';
7
+
8
+ /**
9
+ * @param {any} engine
10
+ * @param {{ night: boolean, fill: string, windowOpening: { position: number[], lookAt: number[], width: number, height: number } | null }} o
11
+ */
12
+ export function createLights(engine, { night, fill, windowOpening }) {
13
+ const { scene, renderer } = engine, key = night ? MOON : SUN;
14
+ const hemi = new THREE.HemisphereLight(), sun = new THREE.DirectionalLight(key.color, key.intensity);
15
+ const az = THREE.MathUtils.degToRad(key.azimuth), el = THREE.MathUtils.degToRad(key.elevation);
16
+ sun.position.set(Math.sin(az) * Math.cos(el), Math.sin(el), -Math.cos(az) * Math.cos(el)).multiplyScalar(SHADOW.distance);
17
+ sun.castShadow = true;
18
+ renderer.shadowMap.type = SHADOW.type;
19
+ sun.shadow.mapSize.setScalar(SHADOW.mapSize[engine.tier === 'low' ? 'low' : 'high']);
20
+ const { extent: e, distance } = SHADOW;
21
+ Object.assign(sun.shadow.camera, { left: -e, right: e, top: e, bottom: -e, near: 1, far: distance * 2 });
22
+ // three adds bias in the shadow camera's 0-1 depth, i.e. a world offset of bias x (far - near)
23
+ Object.assign(sun.shadow, { normalBias: SHADOW.normalBias, bias: -SHADOW.biasCm / 100 / (distance * 2 - 1), radius: SHADOW.radius });
24
+ scene.add(hemi, sun, sun.target);
25
+
26
+ if (windowOpening && !night) windowLight(engine, { ...windowOpening, ...WINDOW_LIGHT });
27
+ if (fill === 'light' && !night) {
28
+ const { color, intensity, distance: reach, decay, at } = BOUNCE_LIGHT, bounce = new THREE.PointLight(color, intensity, reach, decay);
29
+ bounce.position.set(at[0], at[1], at[2]);
30
+ scene.add(bounce);
31
+ }
32
+
33
+ /** The light of one lamp; the bulb hides inside the shade main.js puts around it @param {keyof typeof LAMPS} kind @param {number[]} at */
34
+ function addLamp(kind, at) {
35
+ const l = LAMPS[kind], p = new THREE.PointLight(l.color, l.intensity, 0, 2);
36
+ p.position.set(at[0], at[1], at[2]);
37
+ p.castShadow = l.shadow;
38
+ if (l.shadow) { p.shadow.mapSize.setScalar(LAMP_SHADOW.mapSize); p.shadow.bias = LAMP_SHADOW.bias; p.shadow.radius = LAMP_SHADOW.radius; }
39
+ scene.add(p);
40
+ }
41
+
42
+ const day = { sky: new THREE.Color(HEMI.day.sky), ground: new THREE.Color(HEMI.day.ground) }, dark = { sky: new THREE.Color(HEMI.night.sky), ground: new THREE.Color(HEMI.night.ground) };
43
+ return {
44
+ sun, hemi, addLamp,
45
+ /** @param {any} s engine.uniforms */
46
+ update(s) {
47
+ const light = s.uDaylight.value;
48
+ hemi.color.copy(dark.sky).lerp(day.sky, light);
49
+ hemi.groundColor.copy(dark.ground).lerp(day.ground, light);
50
+ },
51
+ };
52
+ }
@@ -0,0 +1,13 @@
1
+ // @ts-check
2
+ // Materials by name. Standard everywhere, since a RectAreaLight lights nothing else. Each lamp's shade glows in its own light's color.
3
+ import { THREE } from 'threedog';
4
+ import { COLORS, LAMPS, ROUGHNESS } from './palette.js';
5
+
6
+ export function createMaterials() {
7
+ const flat = (/** @type {number} */ color, roughness = ROUGHNESS.surface) => new THREE.MeshStandardMaterial({ color, roughness });
8
+ const shade = (/** @type {{ color: string, glow: number }} */ l) => new THREE.MeshStandardMaterial({ color: COLORS.shade, emissive: l.color, emissiveIntensity: l.glow, side: THREE.DoubleSide, roughness: ROUGHNESS.shade });
9
+ return {
10
+ mat: { wall: flat(COLORS.wall), floor: flat(COLORS.floor), ground: flat(COLORS.ground, ROUGHNESS.ground), table: flat(COLORS.table), cabinet: flat(COLORS.cabinet),
11
+ sofa: flat(COLORS.sofa), stand: flat(COLORS.stand), shade: { main: shade(LAMPS.main), side: shade(LAMPS.side) } },
12
+ };
13
+ }
@@ -0,0 +1,32 @@
1
+ // @ts-check
2
+ // Interior light palette: every value of the picture. Neutral numbers on a gray room; the colors of your game come from its own picture.
3
+ import { THREE } from 'threedog';
4
+
5
+ /** Surfaces of the room, by name */
6
+ export const COLORS = { wall: 0xb5b5b5, floor: 0x8f8f8f, ground: 0x8a8a8a, table: 0xa0a0a0, cabinet: 0x9c9c9c, sofa: 0xa6a6a6, stand: 0x777777, shade: 0xe8dccb };
7
+ export const ROUGHNESS = { surface: 0.9, ground: 0.95, shade: 1 };
8
+ /** azimuth: compass degrees from north (-Z), -90 = from the west. By night the same light is a weak moon */
9
+ export const SUN = { elevation: 32, azimuth: -90 + 12, intensity: 3.2, color: '#fff0dc' };
10
+ export const MOON = { elevation: 40, azimuth: -90 + 40, intensity: 0.12, color: '#8fa4d4' };
11
+ // the walls stop the sun but not these two, so indoors the ambient is turned down and the fill comes from bounce
12
+ export const AMBIENT = { outside: { hemi: 1.0, env: 0.3 }, inside: { hemi: 0.06, env: 0.04 } };
13
+ /** Hemisphere colors, blended by daylight */
14
+ export const HEMI = { day: { sky: '#b9b9b9', ground: '#4a4a4a' }, night: { sky: '#212121', ground: '#3a3a3a' } };
15
+ /** RectAreaLight: the sky seen through the window (nits-like units, Standard / Physical only) */
16
+ export const WINDOW_LIGHT = { color: '#cfe0ff', intensity: 2.2 };
17
+ /** ?fill=light: one unshadowed point light just above the sun patch, standing in for its bounce */
18
+ export const BOUNCE_LIGHT = { color: '#ffe2c0', intensity: 1.2, distance: 7, decay: 1, at: [-1.6, 0.4, 0.2] };
19
+ /** Exposure per zone, eased over `ease` seconds when the camera crosses the doorway (an eye adapting) */
20
+ export const EXPOSURE = { day: { outside: 1, inside: 1.9 }, night: { outside: 1.6, inside: 1 }, ease: 1.2 };
21
+ /** intensity in candela, physical falloff; a point light shadow is six renders, so only the light that needs it gets one. glow: the shade stays dim (no glare without bloom) */
22
+ export const LAMPS = {
23
+ main: { intensity: 5, color: '#ffc58a', shadow: true, glow: 0.35 },
24
+ side: { intensity: 2.5, color: '#ffb874', shadow: false, glow: 0.35 },
25
+ };
26
+ export const LAMP_SHADOW = { mapSize: 512, bias: -0.002, radius: 4 };
27
+ /** Fitted to the room: 8 m wide at 2048 px is 0.6 cm texels. Thin walls need a small normalBias, or light leaks along their inner edge; biasCm is the depth bias in centimeters */
28
+ export const SHADOW = { extent: 9, distance: 160, mapSize: { high: 2048, low: 1024 }, normalBias: 0.01, biasCm: 1, radius: 2, type: THREE.PCFShadowMap };
29
+ /** Sky colors by daylight; horizon is also the fog color */
30
+ export const SKY = { day: { top: '#d4d4d4', horizon: '#d4d4d4' }, night: { top: '#20242c', horizon: '#2a2a2a' } };
31
+ export const FOG = { near: 180, far: 600 };
32
+ export const TONE_MAPPING = THREE.AgXToneMapping;
@@ -0,0 +1,40 @@
1
+ // @ts-check
2
+ // The sky is the game's: a gradient sphere and fog in its horizon color, blended by daylight. The engine's dome is hidden. The environment is
3
+ // three's RoomEnvironment: soft fill with some direction, no asset; the zones write its strength.
4
+ import { THREE } from 'threedog';
5
+ import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
6
+ import { FOG, SKY } from './palette.js';
7
+
8
+ /** @param {any} engine */
9
+ export function createSky(engine) {
10
+ const { scene, renderer } = engine;
11
+ scene.fog = new THREE.Fog(SKY.day.horizon, FOG.near, FOG.far);
12
+ const pmrem = new THREE.PMREMGenerator(renderer);
13
+ scene.environment = pmrem.fromScene(new RoomEnvironment(), 0.04).texture;
14
+ pmrem.dispose();
15
+ const skyMat = new THREE.ShaderMaterial({
16
+ side: THREE.BackSide, depthWrite: false, fog: false,
17
+ uniforms: { uTop: { value: new THREE.Color() }, uHorizon: { value: new THREE.Color() } },
18
+ vertexShader: 'varying vec3 vDir; void main(){ vDir = normalize(position); gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); }',
19
+ // tonemapping_fragment: the sky takes the room's curve and exposure, so the window runs bright indoors and calms down outside, as the zones intend
20
+ fragmentShader: `uniform vec3 uTop; uniform vec3 uHorizon; varying vec3 vDir;
21
+ void main(){ gl_FragColor = vec4(mix(uHorizon, uTop, smoothstep(-0.05, 0.45, vDir.y)), 1.0);
22
+ #include <tonemapping_fragment>
23
+ #include <colorspace_fragment>
24
+ }`,
25
+ });
26
+ const dome = new THREE.Mesh(new THREE.SphereGeometry(400, 32, 16), skyMat);
27
+ dome.renderOrder = -1;
28
+ scene.add(dome);
29
+ if (engine.sky) engine.sky.dome.visible = false;
30
+ const day = { top: new THREE.Color(SKY.day.top), horizon: new THREE.Color(SKY.day.horizon) }, night = { top: new THREE.Color(SKY.night.top), horizon: new THREE.Color(SKY.night.horizon) };
31
+ return {
32
+ /** @param {any} s engine.uniforms */
33
+ update(s) {
34
+ const light = s.uDaylight.value, u = skyMat.uniforms;
35
+ u.uTop.value.copy(night.top).lerp(day.top, light);
36
+ u.uHorizon.value.copy(night.horizon).lerp(day.horizon, light);
37
+ /** @type {THREE.Fog} */ (scene.fog).color.copy(u.uHorizon.value);
38
+ },
39
+ };
40
+ }