wallhammer-web 0.0.0-stage → 0.4.1

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 (408) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +92 -2
  3. package/dist/base/data.d.ts +67 -0
  4. package/dist/base/data.js +10 -0
  5. package/dist/base/destructible.d.ts +167 -0
  6. package/dist/base/destructible.js +47 -0
  7. package/dist/base/env-slot.d.ts +101 -0
  8. package/dist/base/env-slot.js +8 -0
  9. package/dist/base/frames.d.ts +138 -0
  10. package/dist/base/frames.js +459 -0
  11. package/dist/base/index.d.ts +21 -0
  12. package/dist/base/index.js +11 -0
  13. package/dist/base/mat34.d.ts +46 -0
  14. package/dist/base/mat34.js +173 -0
  15. package/dist/base/material.d.ts +87 -0
  16. package/dist/base/material.js +6 -0
  17. package/dist/base/math.d.ts +27 -0
  18. package/dist/base/math.js +60 -0
  19. package/dist/base/progress.d.ts +38 -0
  20. package/dist/base/progress.js +81 -0
  21. package/dist/base/rig.d.ts +177 -0
  22. package/dist/base/rig.js +97 -0
  23. package/dist/base/trig.d.ts +29 -0
  24. package/dist/base/trig.js +169 -0
  25. package/dist/base/up.d.ts +26 -0
  26. package/dist/base/up.js +40 -0
  27. package/dist/base/view.d.ts +128 -0
  28. package/dist/base/view.js +136 -0
  29. package/dist/camera/camera.d.ts +99 -0
  30. package/dist/camera/camera.js +228 -0
  31. package/dist/camera/follow.d.ts +120 -0
  32. package/dist/camera/follow.js +320 -0
  33. package/dist/camera/free.d.ts +153 -0
  34. package/dist/camera/free.js +359 -0
  35. package/dist/camera/index.d.ts +10 -0
  36. package/dist/camera/index.js +6 -0
  37. package/dist/camera/orbit.d.ts +93 -0
  38. package/dist/camera/orbit.js +261 -0
  39. package/dist/camera/path.d.ts +48 -0
  40. package/dist/camera/path.js +179 -0
  41. package/dist/core/ballistic.d.ts +66 -0
  42. package/dist/core/ballistic.js +104 -0
  43. package/dist/core/bodies.d.ts +77 -0
  44. package/dist/core/bodies.js +125 -0
  45. package/dist/core/clock.d.ts +37 -0
  46. package/dist/core/clock.js +87 -0
  47. package/dist/core/cover-fade.d.ts +14 -0
  48. package/dist/core/cover-fade.js +38 -0
  49. package/dist/core/index.d.ts +13 -0
  50. package/dist/core/index.js +8 -0
  51. package/dist/core/kepler.d.ts +74 -0
  52. package/dist/core/kepler.js +108 -0
  53. package/dist/core/level-fade.d.ts +66 -0
  54. package/dist/core/level-fade.js +123 -0
  55. package/dist/core/palette.d.ts +15 -0
  56. package/dist/core/palette.js +47 -0
  57. package/dist/env/atmosphere.d.ts +129 -0
  58. package/dist/env/atmosphere.js +707 -0
  59. package/dist/env/cloud-field.d.ts +44 -0
  60. package/dist/env/cloud-field.js +111 -0
  61. package/dist/env/environment.d.ts +217 -0
  62. package/dist/env/environment.js +441 -0
  63. package/dist/env/frame-block.d.ts +29 -0
  64. package/dist/env/frame-block.js +152 -0
  65. package/dist/env/glsl.d.ts +35 -0
  66. package/dist/env/glsl.js +246 -0
  67. package/dist/env/index.d.ts +13 -0
  68. package/dist/env/index.js +9 -0
  69. package/dist/env/sky.d.ts +48 -0
  70. package/dist/env/sky.js +227 -0
  71. package/dist/env/sun.d.ts +31 -0
  72. package/dist/env/sun.js +42 -0
  73. package/dist/gen-arch/build.d.ts +17 -0
  74. package/dist/gen-arch/build.js +414 -0
  75. package/dist/gen-arch/damage/damaged.d.ts +44 -0
  76. package/dist/gen-arch/damage/damaged.js +1119 -0
  77. package/dist/gen-arch/damage/fall-material.d.ts +10 -0
  78. package/dist/gen-arch/damage/fall-material.js +47 -0
  79. package/dist/gen-arch/damage/fall.d.ts +78 -0
  80. package/dist/gen-arch/damage/fall.js +797 -0
  81. package/dist/gen-arch/damage/rubble.d.ts +22 -0
  82. package/dist/gen-arch/damage/rubble.js +188 -0
  83. package/dist/gen-arch/index.d.ts +25 -0
  84. package/dist/gen-arch/index.js +16 -0
  85. package/dist/gen-arch/interior/interior-plan.d.ts +114 -0
  86. package/dist/gen-arch/interior/interior-plan.js +615 -0
  87. package/dist/gen-arch/interior/interior.d.ts +97 -0
  88. package/dist/gen-arch/interior/interior.js +966 -0
  89. package/dist/gen-arch/structure/building-shader.d.ts +25 -0
  90. package/dist/gen-arch/structure/building-shader.js +946 -0
  91. package/dist/gen-arch/structure/buildings.d.ts +248 -0
  92. package/dist/gen-arch/structure/buildings.js +294 -0
  93. package/dist/gen-arch/structure/dims.d.ts +77 -0
  94. package/dist/gen-arch/structure/dims.js +39 -0
  95. package/dist/gen-arch/structure/distant.d.ts +62 -0
  96. package/dist/gen-arch/structure/distant.js +316 -0
  97. package/dist/gen-arch/structure/env-slots.d.ts +18 -0
  98. package/dist/gen-arch/structure/env-slots.js +47 -0
  99. package/dist/gen-arch/structure/facade.d.ts +193 -0
  100. package/dist/gen-arch/structure/facade.js +159 -0
  101. package/dist/gen-arch/structure/parts.d.ts +31 -0
  102. package/dist/gen-arch/structure/parts.js +69 -0
  103. package/dist/gen-arch/structure/roof.d.ts +76 -0
  104. package/dist/gen-arch/structure/roof.js +568 -0
  105. package/dist/gen-arch/structure/structure-material.d.ts +23 -0
  106. package/dist/gen-arch/structure/structure-material.js +55 -0
  107. package/dist/gen-arch/structure/writer.d.ts +94 -0
  108. package/dist/gen-arch/structure/writer.js +195 -0
  109. package/dist/gen-core/cell.d.ts +138 -0
  110. package/dist/gen-core/cell.js +150 -0
  111. package/dist/gen-core/cube-sphere.d.ts +57 -0
  112. package/dist/gen-core/cube-sphere.js +159 -0
  113. package/dist/gen-core/field.d.ts +55 -0
  114. package/dist/gen-core/field.js +154 -0
  115. package/dist/gen-core/geometry.d.ts +31 -0
  116. package/dist/gen-core/geometry.js +231 -0
  117. package/dist/gen-core/hash.d.ts +18 -0
  118. package/dist/gen-core/hash.js +58 -0
  119. package/dist/gen-core/height-field.d.ts +33 -0
  120. package/dist/gen-core/height-field.js +137 -0
  121. package/dist/gen-core/index.d.ts +22 -0
  122. package/dist/gen-core/index.js +17 -0
  123. package/dist/gen-core/instance-cells.d.ts +37 -0
  124. package/dist/gen-core/instance-cells.js +72 -0
  125. package/dist/gen-core/near-scatter.d.ts +56 -0
  126. package/dist/gen-core/near-scatter.js +120 -0
  127. package/dist/gen-core/noise.d.ts +23 -0
  128. package/dist/gen-core/noise.js +72 -0
  129. package/dist/gen-core/occupancy.d.ts +21 -0
  130. package/dist/gen-core/occupancy.js +104 -0
  131. package/dist/gen-core/sphere-patch.d.ts +138 -0
  132. package/dist/gen-core/sphere-patch.js +544 -0
  133. package/dist/gen-core/strip-surface.d.ts +34 -0
  134. package/dist/gen-core/strip-surface.js +246 -0
  135. package/dist/gen-core/strip.d.ts +102 -0
  136. package/dist/gen-core/strip.js +817 -0
  137. package/dist/gen-ground/env-slots.d.ts +48 -0
  138. package/dist/gen-ground/env-slots.js +78 -0
  139. package/dist/gen-ground/ground.d.ts +78 -0
  140. package/dist/gen-ground/ground.js +208 -0
  141. package/dist/gen-ground/index.d.ts +9 -0
  142. package/dist/gen-ground/index.js +7 -0
  143. package/dist/gen-ground/markings.d.ts +46 -0
  144. package/dist/gen-ground/markings.js +124 -0
  145. package/dist/gen-ground/materials.d.ts +18 -0
  146. package/dist/gen-ground/materials.js +22 -0
  147. package/dist/gen-ground/terrain-shader.d.ts +5 -0
  148. package/dist/gen-ground/terrain-shader.js +151 -0
  149. package/dist/gen-mesh/bare-shapes.d.ts +194 -0
  150. package/dist/gen-mesh/bare-shapes.js +596 -0
  151. package/dist/gen-mesh/bisect-surface.d.ts +47 -0
  152. package/dist/gen-mesh/bisect-surface.js +261 -0
  153. package/dist/gen-mesh/box.d.ts +18 -0
  154. package/dist/gen-mesh/box.js +2 -0
  155. package/dist/gen-mesh/cell-morph-mesh.d.ts +52 -0
  156. package/dist/gen-mesh/cell-morph-mesh.js +258 -0
  157. package/dist/gen-mesh/cell-surface-mesh.d.ts +32 -0
  158. package/dist/gen-mesh/cell-surface-mesh.js +140 -0
  159. package/dist/gen-mesh/chunk-mesh.d.ts +25 -0
  160. package/dist/gen-mesh/chunk-mesh.js +112 -0
  161. package/dist/gen-mesh/drape-mesh.d.ts +36 -0
  162. package/dist/gen-mesh/drape-mesh.js +98 -0
  163. package/dist/gen-mesh/height-field-mesh.d.ts +14 -0
  164. package/dist/gen-mesh/height-field-mesh.js +98 -0
  165. package/dist/gen-mesh/index.d.ts +26 -0
  166. package/dist/gen-mesh/index.js +15 -0
  167. package/dist/gen-mesh/line-mesh.d.ts +25 -0
  168. package/dist/gen-mesh/line-mesh.js +62 -0
  169. package/dist/gen-mesh/mesh-builder.d.ts +115 -0
  170. package/dist/gen-mesh/mesh-builder.js +315 -0
  171. package/dist/gen-mesh/packed-mesh.d.ts +53 -0
  172. package/dist/gen-mesh/packed-mesh.js +205 -0
  173. package/dist/gen-mesh/prism-mesh.d.ts +33 -0
  174. package/dist/gen-mesh/prism-mesh.js +176 -0
  175. package/dist/gen-mesh/rig-mesh.d.ts +9 -0
  176. package/dist/gen-mesh/rig-mesh.js +135 -0
  177. package/dist/gen-mesh/strip-mesh.d.ts +45 -0
  178. package/dist/gen-mesh/strip-mesh.js +258 -0
  179. package/dist/gen-plan/drain.d.ts +58 -0
  180. package/dist/gen-plan/drain.js +270 -0
  181. package/dist/gen-plan/grid.d.ts +37 -0
  182. package/dist/gen-plan/grid.js +182 -0
  183. package/dist/gen-plan/horizon.d.ts +50 -0
  184. package/dist/gen-plan/horizon.js +185 -0
  185. package/dist/gen-plan/index.d.ts +15 -0
  186. package/dist/gen-plan/index.js +13 -0
  187. package/dist/gen-plan/line-tiles.d.ts +35 -0
  188. package/dist/gen-plan/line-tiles.js +190 -0
  189. package/dist/gen-plan/lines.d.ts +31 -0
  190. package/dist/gen-plan/lines.js +183 -0
  191. package/dist/gen-plan/plots.d.ts +40 -0
  192. package/dist/gen-plan/plots.js +282 -0
  193. package/dist/gen-plan/route.d.ts +55 -0
  194. package/dist/gen-plan/route.js +297 -0
  195. package/dist/gen-plan/sites.d.ts +32 -0
  196. package/dist/gen-plan/sites.js +209 -0
  197. package/dist/gen-tex/code-stamp.d.ts +31 -0
  198. package/dist/gen-tex/code-stamp.js +137 -0
  199. package/dist/gen-tex/index.d.ts +7 -0
  200. package/dist/gen-tex/index.js +5 -0
  201. package/dist/gen-tex/light-horizon.d.ts +47 -0
  202. package/dist/gen-tex/light-horizon.js +180 -0
  203. package/dist/gen-tex/textures.d.ts +8 -0
  204. package/dist/gen-tex/textures.js +49 -0
  205. package/dist/gen-tex/value-textures.d.ts +46 -0
  206. package/dist/gen-tex/value-textures.js +102 -0
  207. package/dist/gen-vegetation/env-slots.d.ts +20 -0
  208. package/dist/gen-vegetation/env-slots.js +29 -0
  209. package/dist/gen-vegetation/index.d.ts +8 -0
  210. package/dist/gen-vegetation/index.js +7 -0
  211. package/dist/gen-vegetation/materials.d.ts +7 -0
  212. package/dist/gen-vegetation/materials.js +17 -0
  213. package/dist/gen-vegetation/planting.d.ts +170 -0
  214. package/dist/gen-vegetation/planting.js +363 -0
  215. package/dist/gen-vegetation/tree-shader.d.ts +23 -0
  216. package/dist/gen-vegetation/tree-shader.js +229 -0
  217. package/dist/gen-vegetation/trees.d.ts +59 -0
  218. package/dist/gen-vegetation/trees.js +329 -0
  219. package/dist/gpu/device.d.ts +286 -0
  220. package/dist/gpu/device.js +1157 -0
  221. package/dist/gpu/gl.d.ts +134 -0
  222. package/dist/gpu/gl.js +276 -0
  223. package/dist/gpu/index.d.ts +4 -0
  224. package/dist/gpu/index.js +3 -0
  225. package/dist/index.d.ts +11 -0
  226. package/dist/index.js +13 -0
  227. package/dist/motion/graph.d.ts +244 -0
  228. package/dist/motion/graph.js +739 -0
  229. package/dist/motion/index.d.ts +8 -0
  230. package/dist/motion/index.js +8 -0
  231. package/dist/motion/pose-ops.d.ts +30 -0
  232. package/dist/motion/pose-ops.js +232 -0
  233. package/dist/motion/reach.d.ts +45 -0
  234. package/dist/motion/reach.js +264 -0
  235. package/dist/motion/skeleton.d.ts +49 -0
  236. package/dist/motion/skeleton.js +99 -0
  237. package/dist/platform/byte-store.d.ts +21 -0
  238. package/dist/platform/byte-store.js +137 -0
  239. package/dist/platform/canvas.d.ts +29 -0
  240. package/dist/platform/canvas.js +76 -0
  241. package/dist/platform/context-loss.d.ts +8 -0
  242. package/dist/platform/context-loss.js +17 -0
  243. package/dist/platform/free-input.d.ts +51 -0
  244. package/dist/platform/free-input.js +100 -0
  245. package/dist/platform/index.d.ts +18 -0
  246. package/dist/platform/index.js +10 -0
  247. package/dist/platform/input.d.ts +48 -0
  248. package/dist/platform/input.js +117 -0
  249. package/dist/platform/keys.d.ts +12 -0
  250. package/dist/platform/keys.js +29 -0
  251. package/dist/platform/loop.d.ts +41 -0
  252. package/dist/platform/loop.js +84 -0
  253. package/dist/platform/walk-input.d.ts +74 -0
  254. package/dist/platform/walk-input.js +226 -0
  255. package/dist/platform/worker.d.ts +51 -0
  256. package/dist/platform/worker.js +170 -0
  257. package/dist/render/bone-instances.d.ts +24 -0
  258. package/dist/render/bone-instances.js +74 -0
  259. package/dist/render/cells.d.ts +70 -0
  260. package/dist/render/cells.js +144 -0
  261. package/dist/render/glsl.d.ts +66 -0
  262. package/dist/render/glsl.js +173 -0
  263. package/dist/render/index.d.ts +26 -0
  264. package/dist/render/index.js +16 -0
  265. package/dist/render/instance-sets.d.ts +97 -0
  266. package/dist/render/instance-sets.js +241 -0
  267. package/dist/render/near-instances.d.ts +75 -0
  268. package/dist/render/near-instances.js +219 -0
  269. package/dist/render/particles.d.ts +63 -0
  270. package/dist/render/particles.js +215 -0
  271. package/dist/render/pieces.d.ts +57 -0
  272. package/dist/render/pieces.js +151 -0
  273. package/dist/render/point-lights.d.ts +97 -0
  274. package/dist/render/point-lights.js +243 -0
  275. package/dist/render/renderer.d.ts +447 -0
  276. package/dist/render/renderer.js +1100 -0
  277. package/dist/render/rig.d.ts +74 -0
  278. package/dist/render/rig.js +576 -0
  279. package/dist/render/shadow-areas.d.ts +49 -0
  280. package/dist/render/shadow-areas.js +125 -0
  281. package/dist/render/shadow-glsl.d.ts +12 -0
  282. package/dist/render/shadow-glsl.js +146 -0
  283. package/dist/render/transient.d.ts +61 -0
  284. package/dist/render/transient.js +213 -0
  285. package/dist/render/value-glsl.d.ts +17 -0
  286. package/dist/render/value-glsl.js +208 -0
  287. package/dist/render/value-layers.d.ts +180 -0
  288. package/dist/render/value-layers.js +333 -0
  289. package/dist/runtime/atmosphere.d.ts +37 -0
  290. package/dist/runtime/atmosphere.js +304 -0
  291. package/dist/runtime/cloud-shell.d.ts +29 -0
  292. package/dist/runtime/cloud-shell.js +158 -0
  293. package/dist/runtime/engine.d.ts +155 -0
  294. package/dist/runtime/engine.js +305 -0
  295. package/dist/runtime/environment-link.d.ts +22 -0
  296. package/dist/runtime/environment-link.js +49 -0
  297. package/dist/runtime/index.d.ts +13 -0
  298. package/dist/runtime/index.js +12 -0
  299. package/dist/runtime/precipitation-glsl.d.ts +2 -0
  300. package/dist/runtime/precipitation-glsl.js +70 -0
  301. package/dist/runtime/precipitation.d.ts +11 -0
  302. package/dist/runtime/precipitation.js +78 -0
  303. package/dist/runtime/sky.d.ts +30 -0
  304. package/dist/runtime/sky.js +114 -0
  305. package/dist/runtime/streaming.d.ts +245 -0
  306. package/dist/runtime/streaming.js +602 -0
  307. package/dist/sim/agenda.d.ts +167 -0
  308. package/dist/sim/agenda.js +425 -0
  309. package/dist/sim/bytes.d.ts +34 -0
  310. package/dist/sim/bytes.js +125 -0
  311. package/dist/sim/destruction/apply.d.ts +59 -0
  312. package/dist/sim/destruction/apply.js +243 -0
  313. package/dist/sim/destruction/cell-states.d.ts +54 -0
  314. package/dist/sim/destruction/cell-states.js +198 -0
  315. package/dist/sim/destruction/index.d.ts +11 -0
  316. package/dist/sim/destruction/index.js +10 -0
  317. package/dist/sim/destruction/int-geometry.d.ts +17 -0
  318. package/dist/sim/destruction/int-geometry.js +131 -0
  319. package/dist/sim/destruction/pick.d.ts +13 -0
  320. package/dist/sim/destruction/pick.js +59 -0
  321. package/dist/sim/destruction/rubble.d.ts +8 -0
  322. package/dist/sim/destruction/rubble.js +159 -0
  323. package/dist/sim/destruction/state.d.ts +15 -0
  324. package/dist/sim/destruction/state.js +83 -0
  325. package/dist/sim/destruction/structure.d.ts +30 -0
  326. package/dist/sim/destruction/structure.js +139 -0
  327. package/dist/sim/events.d.ts +37 -0
  328. package/dist/sim/events.js +106 -0
  329. package/dist/sim/fixed.d.ts +21 -0
  330. package/dist/sim/fixed.js +64 -0
  331. package/dist/sim/flow-probe.d.ts +58 -0
  332. package/dist/sim/flow-probe.js +158 -0
  333. package/dist/sim/flow.d.ts +257 -0
  334. package/dist/sim/flow.js +967 -0
  335. package/dist/sim/index.d.ts +28 -0
  336. package/dist/sim/index.js +20 -0
  337. package/dist/sim/interlock.d.ts +47 -0
  338. package/dist/sim/interlock.js +252 -0
  339. package/dist/sim/movers.d.ts +228 -0
  340. package/dist/sim/movers.js +1083 -0
  341. package/dist/sim/nav.d.ts +72 -0
  342. package/dist/sim/nav.js +292 -0
  343. package/dist/sim/paths.d.ts +80 -0
  344. package/dist/sim/paths.js +197 -0
  345. package/dist/sim/regions.d.ts +87 -0
  346. package/dist/sim/regions.js +517 -0
  347. package/dist/sim/simulation.d.ts +114 -0
  348. package/dist/sim/simulation.js +291 -0
  349. package/dist/sim/states.d.ts +23 -0
  350. package/dist/sim/states.js +54 -0
  351. package/dist/sim/table.d.ts +49 -0
  352. package/dist/sim/table.js +126 -0
  353. package/dist/sim/zones.d.ts +11 -0
  354. package/dist/sim/zones.js +196 -0
  355. package/dist/tools/collision-lines.d.ts +12 -0
  356. package/dist/tools/collision-lines.js +50 -0
  357. package/dist/tools/data-view-glsl.d.ts +3 -0
  358. package/dist/tools/data-view-glsl.js +63 -0
  359. package/dist/tools/data-view-layers.d.ts +20 -0
  360. package/dist/tools/data-view-layers.js +60 -0
  361. package/dist/tools/debug-view.d.ts +30 -0
  362. package/dist/tools/debug-view.js +4 -0
  363. package/dist/tools/index.d.ts +10 -0
  364. package/dist/tools/index.js +7 -0
  365. package/dist/tools/mesh-checks.d.ts +67 -0
  366. package/dist/tools/mesh-checks.js +188 -0
  367. package/dist/tools/profile.d.ts +85 -0
  368. package/dist/tools/profile.js +173 -0
  369. package/dist/world/cell-cache.d.ts +32 -0
  370. package/dist/world/cell-cache.js +60 -0
  371. package/dist/world/cell-format.d.ts +25 -0
  372. package/dist/world/cell-format.js +232 -0
  373. package/dist/world/collide.d.ts +192 -0
  374. package/dist/world/collide.js +822 -0
  375. package/dist/world/index.d.ts +16 -0
  376. package/dist/world/index.js +12 -0
  377. package/dist/world/mesh-solids.d.ts +48 -0
  378. package/dist/world/mesh-solids.js +169 -0
  379. package/dist/world/pick.d.ts +34 -0
  380. package/dist/world/pick.js +177 -0
  381. package/dist/world/providers.d.ts +66 -0
  382. package/dist/world/providers.js +83 -0
  383. package/dist/world/sphere.d.ts +69 -0
  384. package/dist/world/sphere.js +271 -0
  385. package/dist/world/streaming.d.ts +288 -0
  386. package/dist/world/streaming.js +801 -0
  387. package/dist/world/walker.d.ts +145 -0
  388. package/dist/world/walker.js +502 -0
  389. package/docs/architecture.md +111 -0
  390. package/docs/cameras.md +302 -0
  391. package/docs/concepts.md +123 -0
  392. package/docs/destruction.md +127 -0
  393. package/docs/frames.md +199 -0
  394. package/docs/generators.md +575 -0
  395. package/docs/getting-started.md +123 -0
  396. package/docs/headless.md +50 -0
  397. package/docs/motion.md +160 -0
  398. package/docs/planet.md +152 -0
  399. package/docs/recipes.md +521 -0
  400. package/docs/reference.md +364 -0
  401. package/docs/rendering.md +823 -0
  402. package/docs/rigs.md +203 -0
  403. package/docs/simulation.md +235 -0
  404. package/docs/streaming.md +590 -0
  405. package/docs/system.md +166 -0
  406. package/docs/tools.md +175 -0
  407. package/docs/walking.md +262 -0
  408. package/package.json +131 -3
@@ -0,0 +1,707 @@
1
+ // Air as a shell around a sphere, as an environment slot: light scattered by molecules (Rayleigh) and by particles
2
+ // (Mie), an absorbing layer, and light scattered more than once. One model serves an eye on the ground, in flight
3
+ // and outside the shell, so nothing switches on the way up: the sky, the haze in front of distant geometry, the rim
4
+ // of the shell seen from outside and the line between day and night all come from the same integral along the view
5
+ // ray. The method follows Hillaire 2020 ("A Scalable and Production Ready Sky and Atmosphere Rendering Technique").
6
+ //
7
+ // That integral is never taken per pixel unless asked for (`reference`): the atmosphere layer of runtime keeps it
8
+ // for the sky in a small table over the directions from the eye, and a material takes it at its vertices
9
+ // (`atmosphereAir`) and lets the rasteriser carry the result to its fragments.
10
+ //
11
+ // Three small tables are computed once, on the CPU, when the slot is made (`atmosphereTables`), and packed into one
12
+ // 160 × 32 texture; no file is involved:
13
+ // - how much sunlight reaches a point from a direction above its horizon (128 × 32),
14
+ // - light scattered more than once, by height and sun height (32 × 16),
15
+ // - the sky's light on a level surface, by height and sun height (32 × 16).
16
+ //
17
+ // The sphere is the renderer's `radialUp` sphere when there is one. Without it (up is +y) the ground sphere touches
18
+ // the plane `groundY` straight under the eye, which is exact enough for a map tens of kilometres across.
19
+ //
20
+ // The slot's GLSL is registered as the include `<atmosphere>` (it needs only `<header>` before it, in a vertex shader
21
+ // as in a fragment shader). Without the slot `EnvironmentLink` registers `ATMOSPHERE_FALLBACK` under the same name, so
22
+ // a material may call `atmosphereAir` and `atmosphereApply` in any engine and ask `#ifdef WH_ATMOSPHERE` before it
23
+ // uses the rest.
24
+ /** Air of the Earth (coefficients as in Hillaire 2020). */
25
+ export const DEFAULT_ATMOSPHERE_MEDIUM = {
26
+ radiusM: 6_360_000,
27
+ heightM: 100_000,
28
+ rayleigh: [5.802e-6, 13.558e-6, 33.1e-6],
29
+ rayleighScaleM: 8000,
30
+ mieScattering: 3.996e-6,
31
+ mieAbsorption: 4.4e-6,
32
+ mieScaleM: 1200,
33
+ mieG: 0.8,
34
+ absorption: [0.65e-6, 1.881e-6, 0.085e-6],
35
+ absorptionCenterM: 25_000,
36
+ absorptionHalfWidthM: 15_000,
37
+ groundAlbedo: 0.3,
38
+ };
39
+ export const DEFAULT_ATMOSPHERE = {
40
+ ...DEFAULT_ATMOSPHERE_MEDIUM,
41
+ exposure: 30,
42
+ sunLight: [1.45, 1.38, 1.3],
43
+ skyLightGain: 2.5,
44
+ nightLight: [0.02, 0.025, 0.04],
45
+ sunDiscRad: 0.012,
46
+ stars: 1,
47
+ groundY: 0,
48
+ skySteps: 24,
49
+ airSteps: 8,
50
+ reference: false,
51
+ };
52
+ /**
53
+ * The default air fitted to a sphere of another size: every height is scaled to a shell `heightM` thick and every
54
+ * density the other way, so the light that comes straight down is the same as through the default air.
55
+ */
56
+ export function atmosphereFor(radiusM, heightM) {
57
+ const d = DEFAULT_ATMOSPHERE_MEDIUM;
58
+ const k = heightM / d.heightM;
59
+ return {
60
+ radiusM,
61
+ heightM,
62
+ rayleigh: [d.rayleigh[0] / k, d.rayleigh[1] / k, d.rayleigh[2] / k],
63
+ rayleighScaleM: d.rayleighScaleM * k,
64
+ mieScattering: d.mieScattering / k,
65
+ mieAbsorption: d.mieAbsorption / k,
66
+ mieScaleM: d.mieScaleM * k,
67
+ mieG: d.mieG,
68
+ absorption: [d.absorption[0] / k, d.absorption[1] / k, d.absorption[2] / k],
69
+ absorptionCenterM: d.absorptionCenterM * k,
70
+ absorptionHalfWidthM: d.absorptionHalfWidthM * k,
71
+ groundAlbedo: d.groundAlbedo,
72
+ };
73
+ }
74
+ // --- Tables ----------------------------------------------------------------------------------------------------------
75
+ /** Texture unit the atmosphere layer keeps the tables on during the colour pass (sampler `u_atmTables`). */
76
+ export const ATMOSPHERE_TABLE_UNIT = 14;
77
+ /** Where each table lies in the texture, in texels: x, y, width, height. */
78
+ export const ATMOSPHERE_TABLE_LAYOUT = {
79
+ width: 160,
80
+ height: 32,
81
+ transmittance: [0, 0, 128, 32],
82
+ multiScatter: [128, 0, 32, 16],
83
+ skyLight: [128, 16, 32, 16],
84
+ };
85
+ const TW = ATMOSPHERE_TABLE_LAYOUT.transmittance[2];
86
+ const TH = ATMOSPHERE_TABLE_LAYOUT.transmittance[3];
87
+ const MW = ATMOSPHERE_TABLE_LAYOUT.multiScatter[2];
88
+ const MH = ATMOSPHERE_TABLE_LAYOUT.multiScatter[3];
89
+ const FOUR_PI = 4 * Math.PI;
90
+ /** Cosine range over which the sun sinks behind the horizon of the ground sphere (the GLSL uses the same). */
91
+ const HORIZON_SOFT = 0.01;
92
+ /** Largest exponent of a density: keeps the smallest density a normal 32-bit float, as in the shader. */
93
+ const MAX_EXPONENT = 60;
94
+ /** Mean of an exponential over a segment from its two end values (exact when the height changes evenly). */
95
+ function expMean(a, b) {
96
+ const d = a - b;
97
+ return Math.abs(d) > 1e-4 * (a + b) ? d / Math.log(a / b) : 0.5 * (a + b);
98
+ }
99
+ /** Reads a table of rgb triples at (x, y) in 0–1 with texel centres at the ends, bilinear. */
100
+ function readTable(table, w, h, x, y, out) {
101
+ const fx = Math.min(1, Math.max(0, x)) * (w - 1);
102
+ const fy = Math.min(1, Math.max(0, y)) * (h - 1);
103
+ const x0 = Math.min(w - 2, Math.floor(fx));
104
+ const y0 = Math.min(h - 2, Math.floor(fy));
105
+ const tx = fx - x0;
106
+ const ty = fy - y0;
107
+ const a = (y0 * w + x0) * 3;
108
+ const b = a + 3;
109
+ const c = a + w * 3;
110
+ const d = c + 3;
111
+ for (let ch = 0; ch < 3; ch++) {
112
+ const top = table[a + ch] + (table[b + ch] - table[a + ch]) * tx;
113
+ const bottom = table[c + ch] + (table[d + ch] - table[c + ch]) * tx;
114
+ out[ch] = top + (bottom - top) * ty;
115
+ }
116
+ }
117
+ /** Densities at a height: molecules, particles, the absorbing layer. */
118
+ function densities(air, h, out) {
119
+ const m = air.m;
120
+ const hh = Math.max(0, h);
121
+ out[0] = Math.exp(-Math.min(hh / m.rayleighScaleM, MAX_EXPONENT));
122
+ out[1] = Math.exp(-Math.min(hh / m.mieScaleM, MAX_EXPONENT));
123
+ out[2] = Math.max(0, 1 - Math.abs(h - m.absorptionCenterM) / m.absorptionHalfWidthM);
124
+ }
125
+ /** Share of sunlight that reaches a point `r` from the centre when the sun stands at cosine `mu` from straight up. */
126
+ function sunAt(air, r, mu, out) {
127
+ const { Rg, Rt, Hh } = air;
128
+ const rr = Math.min(Rt, Math.max(Rg, r));
129
+ const rho = Math.sqrt(Math.max(0, rr * rr - Rg * Rg));
130
+ const muH = -rho / rr;
131
+ const vis = Math.min(1, Math.max(0, (mu - muH) / HORIZON_SOFT + 0.5));
132
+ if (vis <= 0) {
133
+ out[0] = out[1] = out[2] = 0;
134
+ return;
135
+ }
136
+ const muC = Math.max(mu, muH);
137
+ const d = Math.max(0, -rr * muC + Math.sqrt(Math.max(0, rr * rr * (muC * muC - 1) + Rt * Rt)));
138
+ const dMin = Rt - rr;
139
+ const dMax = rho + Hh;
140
+ readTable(air.transmittance, TW, TH, (d - dMin) / Math.max(dMax - dMin, 1e-3), rho / Hh, out);
141
+ out[0] *= vis;
142
+ out[1] *= vis;
143
+ out[2] *= vis;
144
+ }
145
+ /** Distance along a ray from radius `r` with cosine `mu` from straight up: to the ground when it hits it, else to the top. */
146
+ function rayLength(air, r, mu) {
147
+ const { Rg, Rt } = air;
148
+ const dg = r * r * (mu * mu - 1) + Rg * Rg;
149
+ if (mu < 0 && dg >= 0)
150
+ return { length: Math.max(0, -r * mu - Math.sqrt(dg)), ground: true };
151
+ return { length: Math.max(0, -r * mu + Math.sqrt(Math.max(0, r * r * (mu * mu - 1) + Rt * Rt))), ground: false };
152
+ }
153
+ function transmittanceTable(air) {
154
+ const { m, Rg, Rt, Hh } = air;
155
+ const out = new Float64Array(TW * TH * 3);
156
+ const STEPS = 40;
157
+ const a = [0, 0, 0];
158
+ const b = [0, 0, 0];
159
+ for (let j = 0; j < TH; j++) {
160
+ const rho = (Hh * j) / (TH - 1);
161
+ const r = Math.sqrt(rho * rho + Rg * Rg);
162
+ const dMin = Rt - r;
163
+ const dMax = rho + Hh;
164
+ for (let i = 0; i < TW; i++) {
165
+ const d = dMin + ((dMax - dMin) * i) / (TW - 1);
166
+ const mu = d > 0 ? Math.min(1, Math.max(-1, (Hh * Hh - rho * rho - d * d) / (2 * r * d))) : 1;
167
+ // Optical depth along the ray to the top of the air.
168
+ let depthR = 0;
169
+ let depthM = 0;
170
+ let depthO = 0;
171
+ const dt = d / STEPS;
172
+ densities(air, r - Rg, a);
173
+ for (let k = 1; k <= STEPS; k++) {
174
+ const t = dt * k;
175
+ densities(air, Math.sqrt(r * r + t * t + 2 * r * mu * t) - Rg, b);
176
+ depthR += expMean(a[0], b[0]) * dt;
177
+ depthM += expMean(a[1], b[1]) * dt;
178
+ depthO += 0.5 * (a[2] + b[2]) * dt;
179
+ a[0] = b[0];
180
+ a[1] = b[1];
181
+ a[2] = b[2];
182
+ }
183
+ const at = (j * TW + i) * 3;
184
+ for (let ch = 0; ch < 3; ch++)
185
+ out[at + ch] = Math.exp(-(m.rayleigh[ch] * depthR + air.mieExtinction * depthM + m.absorption[ch] * depthO));
186
+ }
187
+ }
188
+ return out;
189
+ }
190
+ /**
191
+ * Integrates along a ray from the point (0, r, 0) in the direction `w`, with the sun in the direction `s` (both unit
192
+ * vectors), to the ground or to the top of the air. Adds to `light` what is scattered toward the start (sunlight
193
+ * with the phase values `phaseM` for particles and `phaseR` for molecules, plus `multi`, the light scattered more than
194
+ * once, when given, plus what lit ground at the end of the ray sends back) and to `share` the share of light the air
195
+ * along the ray scatters at all.
196
+ */
197
+ function marchRay(air, r, w, s, steps, phaseM, phaseR, multi, light, share) {
198
+ const { m, Rg } = air;
199
+ const { length, ground } = rayLength(air, r, w[1]);
200
+ const through = [1, 1, 1];
201
+ const dA = [0, 0, 0];
202
+ const dB = [0, 0, 0];
203
+ const sunA = [0, 0, 0];
204
+ const sunB = [0, 0, 0];
205
+ const msA = [0, 0, 0];
206
+ const msB = [0, 0, 0];
207
+ densities(air, r - Rg, dA);
208
+ sunAt(air, r, s[1], sunA);
209
+ if (multi)
210
+ multi(r - Rg, s[1], msA);
211
+ const dt = length / steps;
212
+ let px = 0;
213
+ let py = r;
214
+ let pz = 0;
215
+ for (let k = 1; k <= steps && dt > 0; k++) {
216
+ const t = dt * k;
217
+ px = w[0] * t;
218
+ py = r + w[1] * t;
219
+ pz = w[2] * t;
220
+ const rk = Math.sqrt(px * px + py * py + pz * pz);
221
+ const mu = (px * s[0] + py * s[1] + pz * s[2]) / rk;
222
+ densities(air, rk - Rg, dB);
223
+ sunAt(air, rk, mu, sunB);
224
+ if (multi)
225
+ multi(rk - Rg, mu, msB);
226
+ const rhoR = expMean(dA[0], dB[0]);
227
+ const rhoM = expMean(dA[1], dB[1]);
228
+ const rhoO = 0.5 * (dA[2] + dB[2]);
229
+ for (let ch = 0; ch < 3; ch++) {
230
+ const scatR = m.rayleigh[ch] * rhoR;
231
+ const scatM = m.mieScattering * rhoM;
232
+ const ext = scatR + air.mieExtinction * rhoM + m.absorption[ch] * rhoO;
233
+ const stepT = Math.exp(-ext * dt);
234
+ const part = ext * dt > 1e-6 ? (1 - stepT) / ext : dt;
235
+ const src = 0.5 * (sunA[ch] + sunB[ch]) * (scatR * phaseR + scatM * phaseM) + 0.5 * (msA[ch] + msB[ch]) * (scatR + scatM);
236
+ light[ch] = light[ch] + through[ch] * src * part;
237
+ share[ch] = share[ch] + through[ch] * (scatR + scatM) * part;
238
+ through[ch] = through[ch] * stepT;
239
+ }
240
+ for (let ch = 0; ch < 3; ch++) {
241
+ dA[ch] = dB[ch];
242
+ sunA[ch] = sunB[ch];
243
+ msA[ch] = msB[ch];
244
+ }
245
+ }
246
+ if (ground && length > 0) {
247
+ // The lit ground at the end of the ray sends its share back.
248
+ const mu = (px * s[0] + py * s[1] + pz * s[2]) / Rg;
249
+ sunAt(air, Rg, mu, sunB);
250
+ const k = (Math.max(0, mu) * m.groundAlbedo) / Math.PI;
251
+ for (let ch = 0; ch < 3; ch++)
252
+ light[ch] = light[ch] + through[ch] * sunB[ch] * k;
253
+ }
254
+ }
255
+ /** Height of row `j` of a table of `rows` rows over the shell: a little inside both of its ends. */
256
+ function rowRadius(air, j, rows) {
257
+ const off = air.m.heightM * 1e-4;
258
+ return air.Rg + off + ((air.m.heightM - 2 * off) * j) / (rows - 1);
259
+ }
260
+ /** Light scattered more than once, per unit of scattering, by sun height (columns, cosine −1…1) and height (rows). */
261
+ function multiScatterTable(air) {
262
+ const out = new Float64Array(MW * MH * 3);
263
+ const DIRS = 8;
264
+ const dirs = [];
265
+ for (let a = 0; a < DIRS; a++) {
266
+ for (let b = 0; b < DIRS; b++) {
267
+ const phi = (2 * Math.PI * (a + 0.5)) / DIRS;
268
+ const cos = 1 - (2 * (b + 0.5)) / DIRS;
269
+ const sin = Math.sqrt(Math.max(0, 1 - cos * cos));
270
+ dirs.push([sin * Math.cos(phi), cos, sin * Math.sin(phi)]);
271
+ }
272
+ }
273
+ for (let j = 0; j < MH; j++) {
274
+ const r = rowRadius(air, j, MH);
275
+ for (let i = 0; i < MW; i++) {
276
+ const mu = -1 + (2 * i) / (MW - 1);
277
+ const s = [Math.sqrt(Math.max(0, 1 - mu * mu)), mu, 0];
278
+ const light = [0, 0, 0];
279
+ const share = [0, 0, 0];
280
+ // Second order, with an even phase for both kinds of scattering.
281
+ for (const w of dirs)
282
+ marchRay(air, r, w, s, 20, 1 / FOUR_PI, 1 / FOUR_PI, null, light, share);
283
+ const at = (j * MW + i) * 3;
284
+ // Every further order repeats the same transfer: a geometric series.
285
+ for (let ch = 0; ch < 3; ch++)
286
+ out[at + ch] = light[ch] / dirs.length / (1 - Math.min(0.99, share[ch] / dirs.length));
287
+ }
288
+ }
289
+ return out;
290
+ }
291
+ /** The sky's light on a level surface (sun above the air = 1), by sun height (columns) and height (rows). */
292
+ function skyLightTable(air, multi) {
293
+ const out = new Float64Array(MW * MH * 3);
294
+ const AROUND = 8;
295
+ const DOWN = 4;
296
+ const height = air.m.heightM;
297
+ const readMulti = (h, mu, o) => readTable(multi, MW, MH, mu * 0.5 + 0.5, h / height, o);
298
+ const dirs = [];
299
+ for (let a = 0; a < AROUND; a++) {
300
+ for (let b = 0; b < DOWN; b++) {
301
+ // Spread by the cosine to the surface, so the mean over the directions times pi is the light on it.
302
+ const phi = (2 * Math.PI * (a + 0.5)) / AROUND;
303
+ const cos = Math.sqrt(1 - (b + 0.5) / DOWN);
304
+ const sin = Math.sqrt(Math.max(0, 1 - cos * cos));
305
+ dirs.push([sin * Math.cos(phi), cos, sin * Math.sin(phi)]);
306
+ }
307
+ }
308
+ const unused = [0, 0, 0];
309
+ for (let j = 0; j < MH; j++) {
310
+ const r = rowRadius(air, j, MH);
311
+ for (let i = 0; i < MW; i++) {
312
+ const mu = -1 + (2 * i) / (MW - 1);
313
+ const s = [Math.sqrt(Math.max(0, 1 - mu * mu)), mu, 0];
314
+ const light = [0, 0, 0];
315
+ for (const w of dirs) {
316
+ const cosT = w[0] * s[0] + w[1] * s[1] + w[2] * s[2];
317
+ // Particles scatter evenly here: these few directions cannot resolve their forward peak.
318
+ marchRay(air, r, w, s, 16, 1 / FOUR_PI, (3 / (16 * Math.PI)) * (1 + cosT * cosT), readMulti, light, unused);
319
+ }
320
+ const at = (j * MW + i) * 3;
321
+ for (let ch = 0; ch < 3; ch++)
322
+ out[at + ch] = (Math.PI * light[ch]) / dirs.length;
323
+ }
324
+ }
325
+ return out;
326
+ }
327
+ /** Pure: the three tables of a medium, packed into one texture. Takes tens of milliseconds. */
328
+ export function atmosphereTables(m) {
329
+ if (!(m.radiusM > 0 && m.heightM > 0))
330
+ throw new Error('atmosphereTables: radiusM and heightM must be positive');
331
+ const Rg = m.radiusM;
332
+ const Rt = m.radiusM + m.heightM;
333
+ const air = { m, Rg, Rt, Hh: Math.sqrt(Rt * Rt - Rg * Rg), mieExtinction: m.mieScattering + m.mieAbsorption, transmittance: new Float64Array(0) };
334
+ air.transmittance = transmittanceTable(air);
335
+ const multi = multiScatterTable(air);
336
+ const sky = skyLightTable(air, multi);
337
+ const peak = (t) => t.reduce((a, v) => Math.max(a, v), 1e-9);
338
+ const multiScatterScale = peak(multi);
339
+ const skyLightScale = peak(sky);
340
+ const L = ATMOSPHERE_TABLE_LAYOUT;
341
+ const data = new Uint8Array(L.width * L.height * 4);
342
+ const put = (table, region, scale) => {
343
+ const [x0, y0, w, h] = region;
344
+ for (let j = 0; j < h; j++) {
345
+ for (let i = 0; i < w; i++) {
346
+ const at = ((y0 + j) * L.width + x0 + i) * 4;
347
+ for (let ch = 0; ch < 3; ch++)
348
+ data[at + ch] = Math.round(255 * Math.sqrt(Math.min(1, Math.max(0, table[(j * w + i) * 3 + ch] / scale))));
349
+ data[at + 3] = 255;
350
+ }
351
+ }
352
+ };
353
+ put(air.transmittance, L.transmittance, 1);
354
+ put(multi, L.multiScatter, multiScatterScale);
355
+ put(sky, L.skyLight, skyLightScale);
356
+ return { texture: { width: L.width, height: L.height, format: 'rgba8', data, filter: 'linear' }, multiScatterScale, skyLightScale };
357
+ }
358
+ /** Pure: the slot's values from the generic environment, the params, and the medium with its tables. */
359
+ export function computeAtmosphere(E, p, m, tables) {
360
+ return {
361
+ sunDir: E.sunDir,
362
+ exposure: p.eye ? Math.max(0, p.exposure) : 0,
363
+ eye: p.eye ?? [0, 0, 0],
364
+ eyeHeightM: p.eye ? p.eyeHeightM : 0,
365
+ radiusM: m.radiusM,
366
+ topM: m.radiusM + m.heightM,
367
+ groundY: p.groundY,
368
+ sunDiscRad: p.sunDiscRad,
369
+ rayleigh: m.rayleigh,
370
+ invRayleighScale: 1 / m.rayleighScaleM,
371
+ mieScattering: m.mieScattering,
372
+ mieExtinction: m.mieScattering + m.mieAbsorption,
373
+ invMieScale: 1 / m.mieScaleM,
374
+ mieG: m.mieG,
375
+ absorption: m.absorption,
376
+ absorptionCenterM: m.absorptionCenterM,
377
+ invAbsorptionHalfWidth: 1 / m.absorptionHalfWidthM,
378
+ multiScatterScale: tables.multiScatterScale,
379
+ skyLightScale: tables.skyLightScale,
380
+ stars: p.stars,
381
+ sunLight: p.sunLight,
382
+ skyLightGain: p.skyLightGain,
383
+ nightLight: p.nightLight,
384
+ reference: p.reference ? 1 : 0,
385
+ };
386
+ }
387
+ /** Largest number of steps the GLSL integrates along one ray. */
388
+ const MAX_STEPS = 48;
389
+ const steps = (n) => Math.min(MAX_STEPS, Math.max(2, Math.round(n)));
390
+ const f = (n) => n.toFixed(1);
391
+ /** GLSL of the slot for the given step counts. Needs `<header>` before it. */
392
+ export function atmosphereGlsl(skySteps, airSteps) {
393
+ const L = ATMOSPHERE_TABLE_LAYOUT;
394
+ const region = (r) => `vec2(${f(r[0])}, ${f(r[1])}), vec2(${f(r[2])}, ${f(r[3])})`;
395
+ return /* glsl */ `
396
+ #define WH_ATMOSPHERE 1
397
+ #define ATM_SKY_STEPS ${steps(skySteps)}
398
+ #define ATM_AIR_STEPS ${steps(airSteps)}
399
+ uniform highp sampler2D u_atmTables;
400
+
401
+ // Centre of the ground sphere in render space. Where up is +y the sphere touches the ground straight under the eye.
402
+ vec3 atmCenter() {
403
+ #ifdef WH_LOCAL_UP
404
+ return u_upCenter.xyz;
405
+ #else
406
+ return vec3(u_atmEye.x, u_atmShell.z - u_atmShell.x, u_atmEye.z);
407
+ #endif
408
+ }
409
+
410
+ // One of the tables at a place given in 0–1 with texel centres at both ends; the stored square root is undone.
411
+ vec3 atmTable(vec2 at, vec2 origin, vec2 size) {
412
+ vec2 uv = (origin + 0.5 + clamp(at, 0.0, 1.0) * (size - 1.0)) / vec2(${f(L.width)}, ${f(L.height)});
413
+ vec3 v = textureLod(u_atmTables, uv, 0.0).rgb;
414
+ return v * v;
415
+ }
416
+
417
+ // Densities at a height above the ground sphere: x molecules, y particles, z the absorbing layer.
418
+ vec3 atmDensity(float h) {
419
+ float hh = max(h, 0.0);
420
+ return vec3(exp(-min(hh * u_atmRayleigh.w, ${f(MAX_EXPONENT)})), exp(-min(hh * u_atmMie.z, ${f(MAX_EXPONENT)})), max(0.0, 1.0 - abs(h - u_atmAbsorb.w) * u_atmTune.x));
421
+ }
422
+
423
+ // Mean of an exponential over a segment from its two end values.
424
+ float atmMean(float a, float b) {
425
+ float d = a - b;
426
+ return abs(d) > 1e-4 * (a + b) ? d / log(a / b) : 0.5 * (a + b);
427
+ }
428
+
429
+ // Share of the sun's light that reaches a point at the distance r from the centre, where the sun stands at the
430
+ // cosine mu from straight up. Behind the horizon of the ground sphere it is 0: that is the line between day and night.
431
+ vec3 atmSunAt(float r, float mu) {
432
+ float Rg = u_atmShell.x;
433
+ float Rt = u_atmShell.y;
434
+ r = clamp(r, Rg, Rt);
435
+ float rho = sqrt(max(r * r - Rg * Rg, 0.0));
436
+ float muH = -rho / r;
437
+ float vis = clamp((mu - muH) * ${f(1 / HORIZON_SOFT)} + 0.5, 0.0, 1.0);
438
+ if (vis <= 0.0) return vec3(0.0);
439
+ mu = max(mu, muH);
440
+ float hh = sqrt(Rt * Rt - Rg * Rg);
441
+ float d = max(-r * mu + sqrt(max(r * r * (mu * mu - 1.0) + Rt * Rt, 0.0)), 0.0);
442
+ float dMin = Rt - r;
443
+ float dMax = rho + hh;
444
+ return atmTable(vec2((d - dMin) / max(dMax - dMin, 1e-3), rho / hh), ${region(L.transmittance)}) * vis;
445
+ }
446
+
447
+ // Light scattered more than once at a height, per unit of scattering.
448
+ vec3 atmMultiAt(float h, float mu) {
449
+ return atmTable(vec2(mu * 0.5 + 0.5, h / (u_atmShell.y - u_atmShell.x)), ${region(L.multiScatter)}) * u_atmTune.y;
450
+ }
451
+
452
+ // The sky's light on a level surface at a height.
453
+ vec3 atmSkyLightAt(float h, float mu) {
454
+ return atmTable(vec2(mu * 0.5 + 0.5, h / (u_atmShell.y - u_atmShell.x)), ${region(L.skyLight)}) * u_atmTune.z;
455
+ }
456
+
457
+ // Share of the sunlight particles scatter that goes on at the cosine cosT between the ray and the sun.
458
+ float atmPhaseMie(float cosT) {
459
+ float g = u_atmMie.w;
460
+ return 0.0795775 * (1.0 - g * g) / pow(max(1.0 + g * g - 2.0 * g * cosT, 1e-4), 1.5);
461
+ }
462
+
463
+ // Light the air scatters toward the start of a piece of a ray (the sun above the air is 1), in two parts, and
464
+ // through: the share of light from behind the piece that passes it. Returned: what molecules scatter of the sun and
465
+ // the light scattered more than once. mie: what particles scatter of the sun, before their phase value
466
+ // (atmPhaseMie), which is constant along a ray and far from even across the sky: kept apart, a coarse table of it
467
+ // still gives a sharp glow round the sun. ro is the start relative to the centre of the sphere, rd the unit
468
+ // direction, len the length of the piece inside the shell of air.
469
+ vec3 atmSegmentParts(vec3 ro, vec3 rd, float len, int count, out vec3 mie, out vec3 through) {
470
+ through = vec3(1.0);
471
+ mie = vec3(0.0);
472
+ vec3 light = vec3(0.0);
473
+ if (len <= 0.0) return light;
474
+ float Rg = u_atmShell.x;
475
+ vec3 sunDir = u_atmSun.xyz;
476
+ float cosT = dot(rd, sunDir);
477
+ float phaseR = 0.0596831 * (1.0 + cosT * cosT);
478
+ float dt = len / float(count);
479
+ float r = max(length(ro), 1.0);
480
+ float mu = dot(ro, sunDir) / r;
481
+ vec3 dA = atmDensity(r - Rg);
482
+ vec3 sunA = atmSunAt(r, mu);
483
+ vec3 msA = atmMultiAt(r - Rg, mu);
484
+ for (int i = 1; i <= ${MAX_STEPS}; i++) {
485
+ if (i > count) break;
486
+ vec3 p = ro + rd * (dt * float(i));
487
+ r = max(length(p), 1.0);
488
+ mu = dot(p, sunDir) / r;
489
+ vec3 dB = atmDensity(r - Rg);
490
+ vec3 sunB = atmSunAt(r, mu);
491
+ vec3 msB = atmMultiAt(r - Rg, mu);
492
+ float rhoR = atmMean(dA.x, dB.x);
493
+ float rhoM = atmMean(dA.y, dB.y);
494
+ float rhoO = 0.5 * (dA.z + dB.z);
495
+ vec3 scatR = u_atmRayleigh.rgb * rhoR;
496
+ float scatM = u_atmMie.x * rhoM;
497
+ vec3 ext = scatR + vec3(u_atmMie.y * rhoM) + u_atmAbsorb.rgb * rhoO;
498
+ vec3 sun = 0.5 * (sunA + sunB);
499
+ vec3 x = ext * dt;
500
+ vec3 stepT = exp(-x);
501
+ // The piece's own share, exact for an even source; for very thin air the series, which does not cancel.
502
+ vec3 part = through * mix(dt * (1.0 - 0.5 * x), (1.0 - stepT) / max(ext, vec3(1e-20)), step(vec3(1e-3), x));
503
+ light += part * (sun * scatR * phaseR + 0.5 * (msA + msB) * (scatR + vec3(scatM)));
504
+ mie += part * sun * scatM;
505
+ through *= stepT;
506
+ dA = dB;
507
+ sunA = sunB;
508
+ msA = msB;
509
+ }
510
+ return light;
511
+ }
512
+
513
+ // The same as one light: both parts, the particles' with their phase value.
514
+ vec3 atmSegment(vec3 ro, vec3 rd, float len, int count, out vec3 through) {
515
+ vec3 mie;
516
+ vec3 light = atmSegmentParts(ro, rd, len, count, mie, through);
517
+ return light + mie * atmPhaseMie(dot(rd, u_atmSun.xyz));
518
+ }
519
+
520
+ // Scattered light as a screen-space colour.
521
+ vec3 atmDisplay(vec3 light) {
522
+ return 1.0 - exp(-light * u_atmSun.w);
523
+ }
524
+
525
+ // The eye relative to the centre of the sphere. Its distance comes from the height computed on the CPU, which stays
526
+ // exact a metre above a sphere thousands of kilometres across.
527
+ vec3 atmEye(out float h) {
528
+ h = max(u_atmEye.w, 0.5);
529
+ vec3 e = u_atmEye.xyz - atmCenter();
530
+ return e * ((u_atmShell.x + h) / max(length(e), 1.0));
531
+ }
532
+
533
+ // The air along a view ray from the eye: the scattered light (the sun above the air is 1), through: the share of
534
+ // what lies behind the air that still shows, ground: 1 when the ray ends on the ground sphere.
535
+ vec3 atmosphereSky(vec3 dir, out vec3 through, out float ground) {
536
+ through = vec3(1.0);
537
+ ground = 0.0;
538
+ if (u_atmSun.w <= 0.0) return vec3(0.0);
539
+ float Rg = u_atmShell.x;
540
+ float Rt = u_atmShell.y;
541
+ float h;
542
+ vec3 ro = atmEye(h);
543
+ float r0 = Rg + h;
544
+ float b = dot(ro, dir);
545
+ // r0² − Rt² and r0² − Rg², from the heights.
546
+ float ct = (r0 - Rt) * (r0 + Rt);
547
+ float cg = h * (2.0 * Rg + h);
548
+ float dTop = b * b - ct;
549
+ if (dTop <= 0.0) return vec3(0.0);
550
+ float sq = sqrt(dTop);
551
+ float t0 = max(-b - sq, 0.0);
552
+ float t1 = -b + sq;
553
+ float dGround = b * b - cg;
554
+ if (b < 0.0 && dGround > 0.0) {
555
+ t1 = min(t1, -b - sqrt(dGround));
556
+ ground = 1.0;
557
+ }
558
+ if (t1 <= t0) return vec3(0.0);
559
+ return atmSegment(ro + dir * t0, dir, t1 - t0, ATM_SKY_STEPS, through);
560
+ }
561
+
562
+ // The air between the eye and a point of the scene: the light it scatters toward the eye (the sun above the air is
563
+ // 1), and through: the share of the point's own colour that passes.
564
+ vec3 atmAirTo(vec3 worldPos, out vec3 through) {
565
+ through = vec3(1.0);
566
+ vec3 d = worldPos - u_atmEye.xyz;
567
+ float len = length(d);
568
+ if (len < 1e-3) return vec3(0.0);
569
+ vec3 dir = d / len;
570
+ float Rt = u_atmShell.y;
571
+ float h;
572
+ vec3 ro = atmEye(h);
573
+ float r0 = u_atmShell.x + h;
574
+ float b = dot(ro, dir);
575
+ float dTop = b * b - (r0 - Rt) * (r0 + Rt);
576
+ if (dTop <= 0.0) return vec3(0.0);
577
+ float sq = sqrt(dTop);
578
+ float t0 = max(-b - sq, 0.0);
579
+ float t1 = min(-b + sq, len);
580
+ if (t1 <= t0) return vec3(0.0);
581
+ return atmSegment(ro + dir * t0, dir, t1 - t0, ATM_AIR_STEPS, through);
582
+ }
583
+
584
+ // The air between the eye and a point of the scene, integrated here and now: screen-space colour in, screen-space
585
+ // colour out. Identity in a view without an eye. In a fragment shader this is the integral per pixel; a material
586
+ // with vertices close enough together takes it there instead (atmosphereAir) and applies the result below.
587
+ vec3 atmosphereApply(vec3 display, vec3 worldPos) {
588
+ if (u_atmSun.w <= 0.0) return display;
589
+ vec3 through;
590
+ vec3 air = atmAirTo(worldPos, through);
591
+ return display * through + atmDisplay(air);
592
+ }
593
+
594
+ // For a vertex shader: the air between the eye and the vertex, as the two values the fragment shader hands to
595
+ // atmosphereApply below. Both change slowly over a surface, so the rasteriser may carry them across a triangle.
596
+ // Returned: the scattered light, through: the share of the surface's colour that passes. In the reference quality
597
+ // nothing is integrated here.
598
+ vec3 atmosphereAir(vec3 worldPos, out vec3 through) {
599
+ through = vec3(1.0);
600
+ if (u_atmSun.w <= 0.0 || u_atmNight.w > 0.5) return vec3(0.0);
601
+ return atmAirTo(worldPos, through);
602
+ }
603
+
604
+ // The air in front of a fragment from what atmosphereAir gave at the vertices: screen-space colour in, screen-space
605
+ // colour out. In the reference quality the fragment integrates its own view ray instead.
606
+ vec3 atmosphereApply(vec3 display, vec3 worldPos, vec3 air, vec3 through) {
607
+ if (u_atmSun.w <= 0.0) return display;
608
+ if (u_atmNight.w > 0.5) return atmosphereApply(display, worldPos);
609
+ return display * through + atmDisplay(air);
610
+ }
611
+
612
+ // Colour of the direct sun at a point, as a multiple of the surface colour: white above the air, red through a long
613
+ // way of it, nothing on the night side.
614
+ vec3 atmosphereSun(vec3 worldPos) {
615
+ vec3 p = worldPos - atmCenter();
616
+ float r = max(length(p), 1.0);
617
+ return u_atmLight.rgb * atmSunAt(r, dot(p, u_atmSun.xyz) / r);
618
+ }
619
+
620
+ // Light on a surface as a multiple of its colour, in the place of lightOn: the sun through the air over the point,
621
+ // the sky's light there and what remains at night. sun is the share of direct light (shadows, clouds), ao the share
622
+ // of the surroundings' light.
623
+ vec3 atmosphereLight(vec3 worldPos, vec3 n, float sun, float ao) {
624
+ vec3 p = worldPos - atmCenter();
625
+ float r = max(length(p), 1.0);
626
+ vec3 up = p / r;
627
+ vec3 s = u_atmSun.xyz;
628
+ float mu = dot(up, s);
629
+ vec3 direct = atmSunAt(r, mu) * (max(dot(n, s), 0.0) * sun);
630
+ vec3 sky = atmSkyLightAt(r - u_atmShell.x, mu) * (u_atmLight.w * (0.5 + 0.5 * dot(n, up)));
631
+ return u_atmLight.rgb * (direct + sky * ao) + u_atmNight.rgb * ao;
632
+ }
633
+ `;
634
+ }
635
+ /** The functions a material may call without asking: no air, the colour passes unchanged. Registered as `<atmosphere>` when there is no atmosphere slot. */
636
+ export const ATMOSPHERE_FALLBACK = /* glsl */ `
637
+ vec3 atmosphereApply(vec3 display, vec3 worldPos) { return display; }
638
+ vec3 atmosphereAir(vec3 worldPos, out vec3 through) { through = vec3(1.0); return vec3(0.0); }
639
+ vec3 atmosphereApply(vec3 display, vec3 worldPos, vec3 air, vec3 through) { return display; }
640
+ `;
641
+ /**
642
+ * The atmosphere slot. Pass it in the engine's slots together with an `AtmosphereLayer` of runtime, or use
643
+ * `createEngine({ atmosphere })`, which does both. The medium part of the params is read once, here.
644
+ */
645
+ export function atmosphereSlot(params = {}) {
646
+ const p = { ...DEFAULT_ATMOSPHERE, eye: null, eyeHeightM: 0, ...params };
647
+ const medium = {
648
+ radiusM: p.radiusM,
649
+ heightM: p.heightM,
650
+ rayleigh: [p.rayleigh[0], p.rayleigh[1], p.rayleigh[2]],
651
+ rayleighScaleM: p.rayleighScaleM,
652
+ mieScattering: p.mieScattering,
653
+ mieAbsorption: p.mieAbsorption,
654
+ mieScaleM: p.mieScaleM,
655
+ mieG: p.mieG,
656
+ absorption: [p.absorption[0], p.absorption[1], p.absorption[2]],
657
+ absorptionCenterM: p.absorptionCenterM,
658
+ absorptionHalfWidthM: p.absorptionHalfWidthM,
659
+ groundAlbedo: p.groundAlbedo,
660
+ };
661
+ const tables = atmosphereTables(medium);
662
+ return {
663
+ name: 'atmosphere',
664
+ params: p,
665
+ medium,
666
+ tables,
667
+ fields: [
668
+ { member: 'u_atmSun', order: 1100, lanes: 'xyzw', doc: 'xyz true direction to the sun, w exposure of the scattered light (0: no air in this view)' },
669
+ { member: 'u_atmEye', order: 1110, lanes: 'xyzw', doc: 'xyz eye, w its height above the ground sphere' },
670
+ { member: 'u_atmShell', order: 1120, lanes: 'xyzw', doc: 'x radius of the ground sphere, y radius of the top of the air, z ground height where up is +y, w radius of the sun disc in radians' },
671
+ { member: 'u_atmRayleigh', order: 1130, lanes: 'xyzw', doc: 'rgb scattering by molecules per metre at the ground, w 1 / its scale height' },
672
+ { member: 'u_atmMie', order: 1140, lanes: 'xyzw', doc: 'x scattering by particles per metre at the ground, y their extinction, z 1 / their scale height, w forward share' },
673
+ { member: 'u_atmAbsorb', order: 1150, lanes: 'xyzw', doc: 'rgb absorption per metre in the middle of the absorbing layer, w height of its middle' },
674
+ { member: 'u_atmTune', order: 1160, lanes: 'xyzw', doc: 'x 1 / half-width of the absorbing layer, y, z scales of the tables of light scattered more than once and of sky light, w star visibility' },
675
+ { member: 'u_atmLight', order: 1170, lanes: 'xyzw', doc: 'rgb the sun above the air as light on surfaces, w gain of the sky light on surfaces' },
676
+ { member: 'u_atmNight', order: 1180, lanes: 'xyzw', doc: 'rgb light on surfaces without the sun, w 1: the reference quality, every pixel integrates its own view ray' },
677
+ ],
678
+ compute: (E, q) => computeAtmosphere(E, q, medium, tables),
679
+ lanes: (v) => [
680
+ ...v.sunDir,
681
+ v.exposure,
682
+ ...v.eye,
683
+ v.eyeHeightM,
684
+ v.radiusM,
685
+ v.topM,
686
+ v.groundY,
687
+ v.sunDiscRad,
688
+ ...v.rayleigh,
689
+ v.invRayleighScale,
690
+ v.mieScattering,
691
+ v.mieExtinction,
692
+ v.invMieScale,
693
+ v.mieG,
694
+ ...v.absorption,
695
+ v.absorptionCenterM,
696
+ v.invAbsorptionHalfWidth,
697
+ v.multiScatterScale,
698
+ v.skyLightScale,
699
+ v.stars,
700
+ ...v.sunLight,
701
+ v.skyLightGain,
702
+ ...v.nightLight,
703
+ v.reference,
704
+ ],
705
+ glsl: atmosphereGlsl(p.skySteps, p.airSteps),
706
+ };
707
+ }