wallhammer-web 0.0.0-stage → 0.4.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 (408) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +88 -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,333 @@
1
+ // Value layers: numbers shown over a scene through a colour ramp, in three shapes. `RasterValueLayer` lays a grid of
2
+ // values on a sheet over the ground, `PointValueLayer` draws discs, `ShapeValueLayer` tints prisms by a table row.
3
+ // All three take their values as 16-bit words (`quantizeValues`), one array per time step, and blend two steps.
4
+ // What the numbers mean, where they come from and how a legend looks is the caller's business.
5
+ import { valueTableSize, wordTexture } from '../gen-tex/index.js';
6
+ import { PieceLayer } from './pieces.js';
7
+ import { MeshLayer } from './renderer.js';
8
+ import { POINT_VALUE_MATERIAL, RASTER_VALUE_MATERIAL, SHAPE_VALUE_MATERIAL } from './value-glsl.js';
9
+ /** Programs are compiled once per renderer and shared by every value layer on it. */
10
+ const MATERIALS = new WeakMap();
11
+ function materialOf(renderer, kind) {
12
+ let set = MATERIALS.get(renderer);
13
+ if (!set) {
14
+ set = {};
15
+ MATERIALS.set(renderer, set);
16
+ }
17
+ let m = set[kind];
18
+ if (!m) {
19
+ m = renderer.material(kind === 'raster' ? RASTER_VALUE_MATERIAL : kind === 'points' ? POINT_VALUE_MATERIAL : SHAPE_VALUE_MATERIAL);
20
+ set[kind] = m;
21
+ }
22
+ return m;
23
+ }
24
+ const clamp01 = (v) => Math.min(1, Math.max(0, v));
25
+ /**
26
+ * The two time steps a value layer shows, as textures of `width` × `height` words. A step is recognised by its array:
27
+ * showing an array that is already loaded costs nothing, so walking through steps in order uploads one texture per
28
+ * step. Pass a new array when the values of a step change.
29
+ */
30
+ export class ValueSteps {
31
+ width;
32
+ height;
33
+ /** Blend between the two shown steps, 0–1. */
34
+ mix = 0;
35
+ device;
36
+ held = [
37
+ { words: null, texture: null },
38
+ { words: null, texture: null },
39
+ ];
40
+ second = 0;
41
+ constructor(device, width, height) {
42
+ this.device = device;
43
+ this.width = width;
44
+ this.height = height;
45
+ }
46
+ /** False until the first `show`. */
47
+ get ready() {
48
+ return this.held[0].texture !== null;
49
+ }
50
+ /** Texture of the first shown step (null before the first `show`). */
51
+ get a() {
52
+ return this.held[0].texture;
53
+ }
54
+ /** Texture of the second shown step; the first one again when only one step is shown. */
55
+ get b() {
56
+ return this.held[this.second].texture;
57
+ }
58
+ /** Shows step `a`, or a blend from `a` to `b` by `mix` (0–1). Each array holds `width` × `height` words, row by row. */
59
+ show(a, b = a, mix = 0) {
60
+ if (this.held[0].words !== a) {
61
+ if (this.held[1].words === a)
62
+ this.held.reverse();
63
+ else
64
+ this.load(0, a);
65
+ }
66
+ if (b === a)
67
+ this.second = 0;
68
+ else {
69
+ if (this.held[1].words !== b)
70
+ this.load(1, b);
71
+ this.second = 1;
72
+ }
73
+ this.mix = b === a ? 0 : clamp01(mix);
74
+ }
75
+ load(slot, words) {
76
+ const h = this.held[slot];
77
+ if (h.texture)
78
+ this.device.deleteTexture(h.texture);
79
+ h.texture = this.device.createTexture(wordTexture(words, this.width, this.height));
80
+ h.words = words;
81
+ }
82
+ dispose() {
83
+ for (const h of this.held) {
84
+ if (h.texture)
85
+ this.device.deleteTexture(h.texture);
86
+ h.texture = null;
87
+ h.words = null;
88
+ }
89
+ this.second = 0;
90
+ }
91
+ }
92
+ /**
93
+ * A grid of values on a sheet over the ground. The sheet (tiles from `drapeTiles`) only carries the surface; which
94
+ * cell a fragment shows follows from its position, so the grid and the sheet need not match in size or density.
95
+ * Outside the grid and in cells without a value nothing is drawn. Draws in the `color` and `data` passes, blended,
96
+ * without writing depth; register it after the layers it should lie on.
97
+ */
98
+ export class RasterValueLayer extends PieceLayer {
99
+ steps;
100
+ grid;
101
+ renderer;
102
+ rampTexture;
103
+ cellTexture;
104
+ lookup;
105
+ outline;
106
+ opacity;
107
+ range;
108
+ smooth;
109
+ constructor(renderer, o) {
110
+ super({ material: materialOf(renderer, 'raster'), passes: ['color', 'data'] });
111
+ this.renderer = renderer;
112
+ this.grid = o.grid;
113
+ this.lookup = o.lookup !== undefined;
114
+ this.outline = o.lookup?.outline ?? 0.35;
115
+ this.opacity = o.opacity ?? 0.75;
116
+ this.range = o.range ?? [0, 1];
117
+ this.smooth = o.smooth ?? true;
118
+ const table = o.lookup ? valueTableSize(o.lookup.rows) : { width: o.grid.cols, height: o.grid.rows };
119
+ this.steps = new ValueSteps(renderer.device, table.width, table.height);
120
+ this.rampTexture = renderer.createTexture(o.ramp);
121
+ this.cellTexture = o.lookup ? renderer.createTexture(wordTexture(o.lookup.cells, o.grid.cols, o.grid.rows)) : renderer.device.pixelTexture([0, 0, 0, 0]);
122
+ this.sync();
123
+ }
124
+ /** Uploads sheet tiles and adds them as pieces. */
125
+ addTiles(tiles) {
126
+ for (const t of tiles)
127
+ this.add({ mesh: this.renderer.createMesh(t.mesh, this.material), min: t.min, max: t.max });
128
+ }
129
+ /** Shows one time step, or a blend of two (see `ValueSteps.show`). */
130
+ show(a, b, mix) {
131
+ this.steps.show(a, b, mix);
132
+ this.sync();
133
+ }
134
+ setStyle(s) {
135
+ if (s.opacity !== undefined)
136
+ this.opacity = s.opacity;
137
+ if (s.range !== undefined)
138
+ this.range = s.range;
139
+ if (s.smooth !== undefined)
140
+ this.smooth = s.smooth;
141
+ this.sync();
142
+ }
143
+ /** Replaces the colour ramp. */
144
+ setRamp(ramp) {
145
+ this.renderer.device.deleteTexture(this.rampTexture);
146
+ this.rampTexture = this.renderer.createTexture(ramp);
147
+ this.sync();
148
+ }
149
+ sync() {
150
+ const g = this.grid;
151
+ this.textures = { u_valuesA: this.steps.a, u_valuesB: this.steps.b, u_ramp: this.rampTexture, u_cells: this.cellTexture };
152
+ this.uniforms = {
153
+ u_grid: { vec4: [g.x0, g.z0, 1 / g.cellX, 1 / g.cellZ] },
154
+ u_size: { vec4: [g.cols, g.rows, this.steps.width, 0] },
155
+ u_style: { vec4: [this.steps.mix, clamp01(this.opacity), this.smooth ? 1 : 0, this.lookup ? 1 : 0] },
156
+ u_range: { vec4: [this.range[0], this.range[1], this.outline, 0] },
157
+ };
158
+ }
159
+ draw(ctx) {
160
+ if (!this.steps.ready)
161
+ return;
162
+ super.draw(ctx);
163
+ }
164
+ /** Frees the sheet, the ramp and the step textures. The caller takes the layer out of the renderer. */
165
+ dispose() {
166
+ for (const p of [...this.pieces]) {
167
+ this.remove(p);
168
+ this.renderer.device.deleteMesh(p.mesh);
169
+ }
170
+ this.steps.dispose();
171
+ this.renderer.device.deleteTexture(this.rampTexture);
172
+ this.renderer.device.deleteTexture(this.cellTexture);
173
+ }
174
+ }
175
+ const QUAD_LAYOUT = { stride: 8, attribs: [{ name: 'a_corner', size: 2, type: 'f32', offset: 0 }] };
176
+ const QUAD = {
177
+ layout: QUAD_LAYOUT,
178
+ vertices: new Float32Array([-1, -1, 1, -1, 1, 1, -1, 1]),
179
+ indices: new Uint16Array([0, 1, 2, 0, 2, 3]),
180
+ count: 6,
181
+ };
182
+ /** One point per instance: centre x, y, z and radius in metres (`a_point`). */
183
+ export const POINT_VALUE_LAYOUT = { stride: 16, attribs: [{ name: 'a_point', size: 4, type: 'f32', offset: 0 }] };
184
+ /**
185
+ * Discs that face the view, coloured (and optionally sized) by a value per point; one instanced draw call. A time
186
+ * step is a table of one word per point (laid out by `valueTableSize`); points without a value are not drawn.
187
+ */
188
+ export class PointValueLayer extends MeshLayer {
189
+ steps;
190
+ count;
191
+ renderer;
192
+ instances;
193
+ rampTexture;
194
+ opacity;
195
+ range;
196
+ sizeByValue;
197
+ minRadiusPx;
198
+ radiusScale;
199
+ constructor(renderer, o) {
200
+ // A layer's state for a pass replaces the material's, so the tested variant repeats the rest of it.
201
+ const tested = { ...POINT_VALUE_MATERIAL.state?.color, depthTest: true, depthFunc: 'lequal' };
202
+ super({ material: materialOf(renderer, 'points'), passes: ['color', 'data'], state: o.depthTest ? { color: tested, data: tested } : {} });
203
+ this.renderer = renderer;
204
+ this.count = Math.floor(o.points.length / 4);
205
+ this.opacity = o.opacity ?? 0.9;
206
+ this.range = o.range ?? [0, 1];
207
+ this.sizeByValue = o.sizeByValue ?? 0;
208
+ this.minRadiusPx = o.minRadiusPx ?? 2;
209
+ this.radiusScale = o.radiusScale ?? 1;
210
+ const table = valueTableSize(this.count);
211
+ this.steps = new ValueSteps(renderer.device, table.width, table.height);
212
+ this.rampTexture = renderer.createTexture(o.ramp);
213
+ if (this.count > 0) {
214
+ const data = { layout: POINT_VALUE_LAYOUT, data: o.points.subarray(0, this.count * 4) };
215
+ this.instances = renderer.createInstances(data);
216
+ this.mesh = renderer.createMesh(QUAD, this.material, this.instances);
217
+ }
218
+ else
219
+ this.instances = null;
220
+ this.sync();
221
+ }
222
+ /** Shows one time step, or a blend of two (see `ValueSteps.show`). */
223
+ show(a, b, mix) {
224
+ this.steps.show(a, b, mix);
225
+ this.sync();
226
+ }
227
+ setStyle(s) {
228
+ if (s.opacity !== undefined)
229
+ this.opacity = s.opacity;
230
+ if (s.range !== undefined)
231
+ this.range = s.range;
232
+ if (s.sizeByValue !== undefined)
233
+ this.sizeByValue = s.sizeByValue;
234
+ if (s.minRadiusPx !== undefined)
235
+ this.minRadiusPx = s.minRadiusPx;
236
+ if (s.radiusScale !== undefined)
237
+ this.radiusScale = s.radiusScale;
238
+ this.sync();
239
+ }
240
+ sync() {
241
+ this.textures = { u_valuesA: this.steps.a, u_valuesB: this.steps.b, u_ramp: this.rampTexture };
242
+ this.uniforms = {
243
+ u_style: { vec4: [this.steps.mix, clamp01(this.opacity), this.steps.width, clamp01(this.sizeByValue)] },
244
+ u_range: { vec4: [this.range[0], this.range[1], this.minRadiusPx, this.radiusScale] },
245
+ };
246
+ }
247
+ draw(ctx) {
248
+ const mesh = this.mesh;
249
+ if (!mesh || mesh.instances === 0 || !this.steps.ready)
250
+ return;
251
+ if (!this.begin(ctx))
252
+ return;
253
+ ctx.device.draw(mesh);
254
+ ctx.count(mesh.triangles * mesh.instances);
255
+ }
256
+ /** Frees the points, the ramp and the step textures. The caller takes the layer out of the renderer. */
257
+ dispose() {
258
+ if (this.mesh)
259
+ this.renderer.device.deleteMesh(this.mesh);
260
+ this.mesh = null;
261
+ if (this.instances)
262
+ this.renderer.device.deleteInstances(this.instances);
263
+ this.steps.dispose();
264
+ this.renderer.device.deleteTexture(this.rampTexture);
265
+ }
266
+ }
267
+ /**
268
+ * Prisms tinted by a value per table row (`prismMesh`: every prism carries its row). A time step is a table of one
269
+ * word per row (laid out by `valueTableSize`). Blended over what is already drawn, with depth, so register it after
270
+ * the layers it wraps. Meshes are added as pieces and culled per box.
271
+ */
272
+ export class ShapeValueLayer extends PieceLayer {
273
+ steps;
274
+ renderer;
275
+ rampTexture;
276
+ opacity;
277
+ range;
278
+ neutral;
279
+ constructor(renderer, o) {
280
+ super({ material: materialOf(renderer, 'shapes'), passes: ['color', 'data'] });
281
+ this.renderer = renderer;
282
+ this.opacity = o.opacity ?? 0.9;
283
+ this.range = o.range ?? [0, 1];
284
+ this.neutral = o.neutral ?? null;
285
+ const table = valueTableSize(o.rows);
286
+ this.steps = new ValueSteps(renderer.device, table.width, table.height);
287
+ this.rampTexture = renderer.createTexture(o.ramp);
288
+ this.sync();
289
+ }
290
+ /** Uploads a prism mesh and adds it as one piece with its box. */
291
+ addShapes(shapes) {
292
+ if (shapes.mesh.count === 0)
293
+ return;
294
+ this.add({ mesh: this.renderer.createMesh(shapes.mesh, this.material), min: shapes.min, max: shapes.max });
295
+ }
296
+ /** Shows one time step, or a blend of two (see `ValueSteps.show`). */
297
+ show(a, b, mix) {
298
+ this.steps.show(a, b, mix);
299
+ this.sync();
300
+ }
301
+ setStyle(s) {
302
+ if (s.opacity !== undefined)
303
+ this.opacity = s.opacity;
304
+ if (s.range !== undefined)
305
+ this.range = s.range;
306
+ if (s.neutral !== undefined)
307
+ this.neutral = s.neutral;
308
+ this.sync();
309
+ }
310
+ sync() {
311
+ const n = this.neutral;
312
+ this.textures = { u_valuesA: this.steps.a, u_valuesB: this.steps.b, u_ramp: this.rampTexture };
313
+ this.uniforms = {
314
+ u_style: { vec4: [this.steps.mix, clamp01(this.opacity), this.steps.width, n ? 1 : 0] },
315
+ u_range: { vec4: [this.range[0], this.range[1], 0, 0] },
316
+ u_neutral: { vec4: [n?.[0] ?? 0, n?.[1] ?? 0, n?.[2] ?? 0, 1] },
317
+ };
318
+ }
319
+ draw(ctx) {
320
+ if (!this.steps.ready)
321
+ return;
322
+ super.draw(ctx);
323
+ }
324
+ /** Frees the prisms, the ramp and the step textures. The caller takes the layer out of the renderer. */
325
+ dispose() {
326
+ for (const p of [...this.pieces]) {
327
+ this.remove(p);
328
+ this.renderer.device.deleteMesh(p.mesh);
329
+ }
330
+ this.steps.dispose();
331
+ this.renderer.device.deleteTexture(this.rampTexture);
332
+ }
333
+ }
@@ -0,0 +1,37 @@
1
+ import type { View } from '../base/view.ts';
2
+ import type { Device } from '../gpu/index.ts';
3
+ import type { DrawContext, Layer, Renderer } from '../render/index.ts';
4
+ import type { AtmosphereSlot } from '../env/index.ts';
5
+ /** Size of the sky table in texels: columns round the up of the eye, rows in two halves below and above the horizon. */
6
+ export declare const ATMOSPHERE_SKY_TABLE: {
7
+ readonly width: 64;
8
+ readonly height: 128;
9
+ };
10
+ /** Fills the sky table: one direction per texel, integrated as `atmosphereSky` does. Needs `SKY_VS`. */
11
+ export declare const ATMOSPHERE_TABLE_FS: string;
12
+ export declare const ATMOSPHERE_FS: string;
13
+ export declare class AtmosphereLayer implements Layer {
14
+ /** Name in profiles (`Layer.name`). */
15
+ readonly name: string;
16
+ readonly slot: AtmosphereSlot;
17
+ private readonly renderer;
18
+ private readonly program;
19
+ private readonly tables;
20
+ /** The sky table and the program that fills it, or null where float textures cannot be drawn into. */
21
+ private readonly sky;
22
+ private readonly skyProgram;
23
+ /** What the sky table was last filled from, or null when it has to be filled whatever the frame holds. */
24
+ private filledFrom;
25
+ /** The slot must be part of the renderer's Frame block (and its `EnvironmentLink`), so `<atmosphere>` is the real one. */
26
+ constructor(renderer: Renderer, slot: AtmosphereSlot);
27
+ /** True when the sky is read from the sky table in this frame, false when every pixel integrates its own ray. */
28
+ get usesSkyTable(): boolean;
29
+ /** Fills the sky table when what it was filled from has changed. */
30
+ prepare(device: Device, view: View): void;
31
+ /**
32
+ * Compares what the sky table depends on with what it was filled from, and remembers the new values: the
33
+ * environment's part of the Frame block (the slot's members among them) and the centre of up.
34
+ */
35
+ private frameChanged;
36
+ draw(ctx: DrawContext): void;
37
+ }
@@ -0,0 +1,304 @@
1
+ // Atmosphere layer: uploads the tables of the atmosphere slot, keeps them bound for every layer drawn after it, and
2
+ // draws the air behind the scene: one triangle across the screen, first in the colour pass, with the stars and the
3
+ // sun's disc behind the air. Only perspective views get it. Every frame it hands the view's eye and its height above
4
+ // the ground sphere to the slot, which carries them into the Frame block for the air in front of the scene
5
+ // (`atmosphereAir`, `atmosphereApply`) and for the light on surfaces (`atmosphereLight`).
6
+ //
7
+ // The sky table. The light along a view ray of the sky is not integrated per pixel: before the passes of a frame
8
+ // (`prepare`) the layer fills a small table on the graphics card with that integral for directions spread over the
9
+ // whole sky of the eye, and every pixel reads it. The table is filled again only when the environment's part of the
10
+ // Frame block or the centre of up has changed. Its three textures (16-bit float) hold what `atmSegmentParts` of the
11
+ // include `<atmosphere>` gives: the light of molecules with the light scattered more than once, the light of
12
+ // particles before its phase value (the pixel multiplies that in, so the glow round the sun stays sharp), and the
13
+ // share of light from behind the air that passes.
14
+ // - Columns: the cosine of the angle, round the up of the eye, between the direction and the sun. Seen from one
15
+ // place the air depends on that angle through its cosine alone, so the columns are evenly spaced in it.
16
+ // - Rows, in two halves that never blend into each other, so the horizon stays a line: the lower half holds the
17
+ // directions that end on the ground sphere, from its horizon down to straight down; the upper half the others,
18
+ // from the horizon up to straight up, or to the rim of the shell for an eye above the air. Both by the square
19
+ // root of the angle from the horizon: the rows crowd there, where the light changes fastest.
20
+ // Where the graphics card cannot draw into float textures, and in the slot's reference quality
21
+ // (`AtmosphereParams.reference`), every pixel integrates its own ray as `atmosphereSky` does.
22
+ import { ATMOSPHERE_TABLE_UNIT } from '../env/index.js';
23
+ import { SKY_VS } from './sky.js';
24
+ /** Size of the sky table in texels: columns round the up of the eye, rows in two halves below and above the horizon. */
25
+ export const ATMOSPHERE_SKY_TABLE = { width: 64, height: 128 };
26
+ const SKY_W = ATMOSPHERE_SKY_TABLE.width.toFixed(1);
27
+ const SKY_HALF = (ATMOSPHERE_SKY_TABLE.height / 2).toFixed(1);
28
+ /** First texture unit the three textures of the sky table are bound to while the sky is drawn. */
29
+ const SKY_UNIT = 1;
30
+ /** What the sky table is laid out by, shared by the program that fills it and the one that reads it. Needs `<atmosphere>`. */
31
+ const SKY_TABLE_GLSL = /* glsl */ `
32
+ const float ATM_PI = 3.14159265;
33
+ struct AtmSkyFrame {
34
+ vec3 ro; // the eye relative to the centre of the sphere
35
+ vec3 up; // unit up at the eye
36
+ vec3 toSun; // unit and level at the eye, toward the sun
37
+ vec3 side; // unit and level, square to both
38
+ float r0; // distance of the eye from the centre
39
+ float h; // height of the eye above the ground sphere
40
+ float horizon; // angle from straight up to the horizon of the ground sphere
41
+ float top; // angle from straight up to the first direction with air: 0 inside the shell, its rim from above
42
+ };
43
+ AtmSkyFrame atmSkyFrame() {
44
+ AtmSkyFrame f;
45
+ f.ro = atmEye(f.h);
46
+ float Rg = u_atmShell.x;
47
+ float Rt = u_atmShell.y;
48
+ f.r0 = Rg + f.h;
49
+ f.up = f.ro / f.r0;
50
+ vec3 s = u_atmSun.xyz - f.up * dot(u_atmSun.xyz, f.up);
51
+ // With the sun straight above or below the sky is the same all the way round: any level direction serves.
52
+ if (dot(s, s) < 1e-8) s = cross(f.up, abs(f.up.y) < 0.9 ? vec3(0.0, 1.0, 0.0) : vec3(1.0, 0.0, 0.0));
53
+ f.toSun = normalize(s);
54
+ f.side = cross(f.up, f.toSun);
55
+ // Both angles from lengths that stay exact a metre above the ground and a metre above the air.
56
+ f.horizon = 0.5 * ATM_PI + atan(sqrt(f.h * (2.0 * Rg + f.h)), Rg);
57
+ f.top = f.r0 > Rt ? ATM_PI - atan(Rt, sqrt((f.r0 - Rt) * (f.r0 + Rt))) : 0.0;
58
+ return f;
59
+ }
60
+ `;
61
+ /** Fills the sky table: one direction per texel, integrated as `atmosphereSky` does. Needs `SKY_VS`. */
62
+ export const ATMOSPHERE_TABLE_FS = /* glsl */ `#include <header>
63
+ #include <atmosphere>
64
+ ${SKY_TABLE_GLSL}
65
+ layout(location = 0) out vec4 o_base;
66
+ layout(location = 1) out vec4 o_mie;
67
+ layout(location = 2) out vec4 o_through;
68
+ void main() {
69
+ AtmSkyFrame f = atmSkyFrame();
70
+ vec2 cell = floor(gl_FragCoord.xy);
71
+ bool down = cell.y < ${SKY_HALF};
72
+ float c = (down ? cell.y : cell.y - ${SKY_HALF}) / (${SKY_HALF} - 1.0);
73
+ float theta = down ? f.horizon + c * c * (ATM_PI - f.horizon) : f.horizon - c * c * (f.horizon - f.top);
74
+ float cosPhi = cell.x / (${SKY_W} - 1.0) * 2.0 - 1.0;
75
+ float mu = cos(theta);
76
+ vec3 dir = f.up * mu + (f.toSun * cosPhi + f.side * sqrt(max(1.0 - cosPhi * cosPhi, 0.0))) * sin(theta);
77
+ // The piece of the ray inside the shell; the half of the table says whether it ends on the ground, so the two
78
+ // rows at the horizon hold the two sides of that line.
79
+ float Rt = u_atmShell.y;
80
+ float b = f.r0 * mu;
81
+ float sq = sqrt(max(b * b - (f.r0 - Rt) * (f.r0 + Rt), 0.0));
82
+ float t0 = max(-b - sq, 0.0);
83
+ float t1 = -b + sq;
84
+ if (down) t1 = min(t1, -b - sqrt(max(b * b - f.h * (2.0 * u_atmShell.x + f.h), 0.0)));
85
+ vec3 mie = vec3(0.0);
86
+ vec3 through = vec3(1.0);
87
+ vec3 base = vec3(0.0);
88
+ if (u_atmSun.w > 0.0 && t1 > t0) base = atmSegmentParts(f.ro + dir * t0, dir, t1 - t0, ATM_SKY_STEPS, mie, through);
89
+ o_base = vec4(base, 1.0);
90
+ o_mie = vec4(mie, 1.0);
91
+ o_through = vec4(through, 1.0);
92
+ }`;
93
+ export const ATMOSPHERE_FS = /* glsl */ `#include <header>
94
+ #include <noise>
95
+ #include <atmosphere>
96
+ ${SKY_TABLE_GLSL}
97
+ uniform highp sampler2D u_atmSkyBase; // sky table: light of molecules and light scattered more than once
98
+ uniform highp sampler2D u_atmSkyMie; // sky table: light of particles before its phase value
99
+ uniform highp sampler2D u_atmSkyThrough; // sky table: share of light from behind the air that passes
100
+ uniform float u_atmMarch; // 1: every pixel integrates its own ray, 0: the sky table is read
101
+ uniform vec3 u_rayCenter; // view ray through the screen centre, scaled so that its depth along the view is 1
102
+ uniform vec3 u_rayRight; // change of the ray from the centre to the right edge
103
+ uniform vec3 u_rayUp; // change of the ray from the centre to the top edge
104
+ uniform vec2 u_screen; // drawing surface in physical pixels
105
+ out vec4 o_color;
106
+
107
+ // Stars: at most one per cell of a cube-map grid over the directions, from an integer hash. Fixed in the world and
108
+ // never narrower than a pixel.
109
+ float atmStars(vec3 dir, float pixelAngle) {
110
+ vec3 a = abs(dir);
111
+ vec2 uv;
112
+ uint face;
113
+ float major;
114
+ if (a.x >= a.y && a.x >= a.z) { uv = dir.zy / a.x; face = dir.x > 0.0 ? 0u : 1u; major = a.x; }
115
+ else if (a.y >= a.z) { uv = dir.xz / a.y; face = dir.y > 0.0 ? 2u : 3u; major = a.y; }
116
+ else { uv = dir.xy / a.z; face = dir.z > 0.0 ? 4u : 5u; major = a.z; }
117
+ const float CELLS = 160.0;
118
+ vec2 g = (uv * 0.5 + 0.5) * CELLS;
119
+ vec2 cf = floor(g);
120
+ ivec2 cell = ivec2(cf);
121
+ uint seed = 400u + face * 8u;
122
+ if (hash2(cell, seed) > 0.3) return 0.0;
123
+ vec2 at = 0.2 + 0.6 * vec2(hash2(cell, seed + 1u), hash2(cell, seed + 2u));
124
+ float m = hash2(cell, seed + 3u);
125
+ float bright = 0.12 + 0.88 * m * m * m * m;
126
+ float px = pixelAngle * CELLS * 0.5 / major;
127
+ float r = max(0.8 * px, 0.01);
128
+ vec2 off = g - cf - at;
129
+ return bright * exp(-dot(off, off) / (r * r));
130
+ }
131
+
132
+ // The air along a view ray from the eye, read from the sky table: what atmosphereSky gives.
133
+ vec3 atmSkyRead(vec3 dir, out vec3 through, out float ground) {
134
+ through = vec3(1.0);
135
+ ground = 0.0;
136
+ if (u_atmSun.w <= 0.0) return vec3(0.0);
137
+ AtmSkyFrame f = atmSkyFrame();
138
+ float Rt = u_atmShell.y;
139
+ float mu = dot(dir, f.up);
140
+ float b = f.r0 * mu;
141
+ float ct = (f.r0 - Rt) * (f.r0 + Rt);
142
+ // No air along the ray: it passes the shell by, or the shell lies behind an eye above it.
143
+ if (b * b - ct <= 0.0 || (ct > 0.0 && b >= 0.0)) return vec3(0.0);
144
+ bool down = b < 0.0 && b * b - f.h * (2.0 * u_atmShell.x + f.h) > 0.0;
145
+ ground = down ? 1.0 : 0.0;
146
+ float theta = acos(clamp(mu, -1.0, 1.0));
147
+ float x = down ? (theta - f.horizon) / (ATM_PI - f.horizon) : (f.horizon - theta) / (f.horizon - f.top);
148
+ float row = sqrt(clamp(x, 0.0, 1.0)) * (${SKY_HALF} - 1.0) + (down ? 0.0 : ${SKY_HALF});
149
+ vec3 level = dir - f.up * mu;
150
+ float l = length(level);
151
+ float cosPhi = l > 1e-5 ? dot(level, f.toSun) / l : 1.0;
152
+ vec2 at = (vec2((cosPhi * 0.5 + 0.5) * (${SKY_W} - 1.0), row) + 0.5) / vec2(${SKY_W}, 2.0 * ${SKY_HALF});
153
+ through = textureLod(u_atmSkyThrough, at, 0.0).rgb;
154
+ return textureLod(u_atmSkyBase, at, 0.0).rgb + textureLod(u_atmSkyMie, at, 0.0).rgb * atmPhaseMie(dot(dir, u_atmSun.xyz));
155
+ }
156
+
157
+ void main() {
158
+ vec2 s = gl_FragCoord.xy / u_screen * 2.0 - 1.0;
159
+ vec3 dir = normalize(u_rayCenter + u_rayRight * s.x + u_rayUp * s.y);
160
+ float pa = max(length(fwidth(dir)), 1e-5);
161
+ vec3 through;
162
+ float ground;
163
+ vec3 light;
164
+ if (u_atmMarch > 0.5) light = atmosphereSky(dir, through, ground);
165
+ else light = atmSkyRead(dir, through, ground);
166
+ vec3 c = atmDisplay(light);
167
+ // Behind the air, where the ground sphere is not in the way: the stars, lost in a bright sky, and the sun's disc,
168
+ // so bright that only a long way through the air gives it a colour.
169
+ float behind = 1.0 - ground;
170
+ float dark = 1.0 - max(c.r, max(c.g, c.b));
171
+ dark *= dark;
172
+ c += vec3(0.88, 0.92, 1.0) * through * (atmStars(dir, pa) * u_atmTune.w * dark * dark * behind);
173
+ float rs = u_atmShell.w;
174
+ float disc = (1.0 - smoothstep(rs - pa, rs + pa, length(dir - u_atmSun.xyz))) * behind;
175
+ c += (1.0 - c) * (1.0 - exp(-through * 30.0)) * disc;
176
+ o_color = vec4(c, 1.0);
177
+ }`;
178
+ export class AtmosphereLayer {
179
+ /** Name in profiles (`Layer.name`). */
180
+ name = 'atmosphere';
181
+ slot;
182
+ renderer;
183
+ program;
184
+ tables;
185
+ /** The sky table and the program that fills it, or null where float textures cannot be drawn into. */
186
+ sky;
187
+ skyProgram;
188
+ /** What the sky table was last filled from, or null when it has to be filled whatever the frame holds. */
189
+ filledFrom = null;
190
+ /** The slot must be part of the renderer's Frame block (and its `EnvironmentLink`), so `<atmosphere>` is the real one. */
191
+ constructor(renderer, slot) {
192
+ this.slot = slot;
193
+ this.renderer = renderer;
194
+ this.tables = renderer.device.createTexture(slot.tables.texture);
195
+ // Every program that declares the sampler reads it from the same unit.
196
+ renderer.device.fixSampler('u_atmTables', ATMOSPHERE_TABLE_UNIT);
197
+ this.program = renderer.device.program('atmosphere', SKY_VS, renderer.resolve(ATMOSPHERE_FS));
198
+ this.sky = renderer.device.colorTarget(ATMOSPHERE_SKY_TABLE.width, ATMOSPHERE_SKY_TABLE.height, 3, 'rgba16f');
199
+ this.skyProgram = this.sky ? renderer.device.program('atmosphere sky table', SKY_VS, renderer.resolve(ATMOSPHERE_TABLE_FS, 'other')) : null;
200
+ // A fresh context holds an empty table.
201
+ renderer.device.onRestored(() => (this.filledFrom = null));
202
+ // Before the environment is packed, so the eye of this frame's view goes into this frame's block.
203
+ renderer.beforeFrame.unshift((view) => {
204
+ const eye = viewEye(view);
205
+ const p = slot.params;
206
+ p.eye = eye;
207
+ if (!eye)
208
+ return;
209
+ const c = renderer.upCenter();
210
+ p.eyeHeightM = c ? Math.hypot(eye[0] - c[0], eye[1] - c[1], eye[2] - c[2]) - slot.medium.radiusM : eye[1] - p.groundY;
211
+ });
212
+ }
213
+ /** True when the sky is read from the sky table in this frame, false when every pixel integrates its own ray. */
214
+ get usesSkyTable() {
215
+ return this.sky !== null && !this.slot.params.reference;
216
+ }
217
+ /** Fills the sky table when what it was filled from has changed. */
218
+ prepare(device, view) {
219
+ const p = this.slot.params;
220
+ if (!this.sky || !this.skyProgram)
221
+ return;
222
+ if (!this.usesSkyTable || view.pixelGrowth <= 0 || !(p.exposure > 0)) {
223
+ this.filledFrom = null;
224
+ return;
225
+ }
226
+ if (!this.frameChanged())
227
+ return;
228
+ device.texture(ATMOSPHERE_TABLE_UNIT, this.tables);
229
+ device.texture(ATMOSPHERE_TABLE_UNIT - 1, null);
230
+ device.fill(this.sky, this.skyProgram);
231
+ }
232
+ /**
233
+ * Compares what the sky table depends on with what it was filled from, and remembers the new values: the
234
+ * environment's part of the Frame block (the slot's members among them) and the centre of up.
235
+ */
236
+ frameChanged() {
237
+ const { afterLight, afterView } = this.renderer.frameSlots;
238
+ const c = this.renderer.upCenter() ?? [0, 0, 0];
239
+ const n = afterLight.length + afterView.length + 3;
240
+ let last = this.filledFrom;
241
+ let changed = false;
242
+ if (!last || last.length !== n) {
243
+ last = this.filledFrom = new Float32Array(n);
244
+ changed = true;
245
+ }
246
+ let at = 0;
247
+ for (const part of [afterLight, afterView, c]) {
248
+ for (let i = 0; i < part.length; i++, at++) {
249
+ const v = Math.fround(part[i]);
250
+ if (last[at] !== v) {
251
+ last[at] = v;
252
+ changed = true;
253
+ }
254
+ }
255
+ }
256
+ return changed;
257
+ }
258
+ draw(ctx) {
259
+ if (ctx.pass !== 'color')
260
+ return;
261
+ const view = ctx.view;
262
+ // The tables stay on their unit for the layers that follow; the unit selected afterwards is a spare one, so a
263
+ // texture made later in the frame does not land on the tables' unit.
264
+ ctx.device.texture(ATMOSPHERE_TABLE_UNIT, this.tables);
265
+ ctx.device.texture(ATMOSPHERE_TABLE_UNIT - 1, null);
266
+ if (view.pixelGrowth <= 0)
267
+ return;
268
+ const f = view.axes().forward;
269
+ // Corner rays: left-bottom, left-top, right-bottom, right-top. Scaled to depth 1 they interpolate linearly across the screen.
270
+ const [lb, lt, rb] = view.cornerRays().map((r) => {
271
+ const k = 1 / (r.dir[0] * f[0] + r.dir[1] * f[1] + r.dir[2] * f[2]);
272
+ return [r.dir[0] * k, r.dir[1] * k, r.dir[2] * k];
273
+ });
274
+ const right = [(rb[0] - lb[0]) / 2, (rb[1] - lb[1]) / 2, (rb[2] - lb[2]) / 2];
275
+ const up = [(lt[0] - lb[0]) / 2, (lt[1] - lb[1]) / 2, (lt[2] - lb[2]) / 2];
276
+ const center = [lb[0] + right[0] + up[0], lb[1] + right[1] + up[1], lb[2] + right[2] + up[2]];
277
+ ctx.device.setState({ depthTest: false, depthWrite: false, cullBack: false });
278
+ ctx.device.use(this.program);
279
+ this.program.set('u_rayCenter', { vec3: center });
280
+ this.program.set('u_rayRight', { vec3: right });
281
+ this.program.set('u_rayUp', { vec3: up });
282
+ this.program.set('u_screen', { vec2: [view.width, view.height] });
283
+ // The sky table on three units of its own for this draw. Without it the samplers still need textures of their
284
+ // kind: the slot's tables stand in, unread.
285
+ const sky = this.usesSkyTable ? this.sky : null;
286
+ const samplers = ['u_atmSkyBase', 'u_atmSkyMie', 'u_atmSkyThrough'];
287
+ samplers.forEach((name, i) => {
288
+ ctx.device.texture(SKY_UNIT + i, sky ? sky.textures[i] : this.tables);
289
+ this.program.int(name, SKY_UNIT + i);
290
+ });
291
+ this.program.float('u_atmMarch', sky ? 0 : 1);
292
+ ctx.device.drawFullscreen();
293
+ samplers.forEach((_, i) => ctx.device.texture(SKY_UNIT + i, null));
294
+ ctx.device.texture(ATMOSPHERE_TABLE_UNIT - 1, null);
295
+ ctx.count(1);
296
+ }
297
+ }
298
+ /** The eye of a perspective view (where its corner rays start), or null for an orthographic one. */
299
+ function viewEye(view) {
300
+ if (view.pixelGrowth <= 0)
301
+ return null;
302
+ const o = view.cornerRays()[0].origin;
303
+ return [o[0], o[1], o[2]];
304
+ }