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,173 @@
1
+ // Shared GLSL pieces: the header with the uniform blocks every program sees, and noise from an integer hash.
2
+ // The Frame block holds what the renderer fills (view, light, shadow map) plus slots that the renderer's owner fills
3
+ // (`Renderer.frameSlots`), declared through `shaderHeader`. The slot positions inside the block are part of the layout.
4
+ import { PALETTE_CAPACITY } from '../core/palette.js';
5
+ /** Size of the Frame block in floats (std140, all vec4 and mat4). */
6
+ export function frameBlockFloats(slots) {
7
+ return 16 * 2 + 4 * (5 + slots.afterLight + slots.afterView);
8
+ }
9
+ /**
10
+ * Program header: version, precision, the Frame block with the given slots and the Palette block. In a depth mode
11
+ * other than `'standard'` it also defines `WH_DEPTH_REVERSED` or `WH_DEPTH_LOGARITHMIC` (to 1), for the rare shader
12
+ * that reads depth values of the main target itself.
13
+ */
14
+ export function shaderHeader(slots = { afterLight: '', afterView: '' }, depth = 'standard', extra = {}) {
15
+ const mode = depth === 'reversed' ? '#define WH_DEPTH_REVERSED 1\n' : depth === 'logarithmic' ? '#define WH_DEPTH_LOGARITHMIC 1\n' : '';
16
+ return /* glsl */ `#version 300 es
17
+ ${mode}${extra.defines ?? ''}precision highp float;
18
+ precision highp int;
19
+ precision highp sampler2DShadow;
20
+ precision highp usampler2D;
21
+
22
+ layout(std140) uniform Frame {
23
+ mat4 u_viewProj;
24
+ mat4 u_lightViewProj;
25
+ vec4 u_sunDir; // xyz direction to the main directional light, w used part of the shadow map (0–1)
26
+ ${slots.afterLight} vec4 u_view; // x metres per screen pixel, y metres per shadow map texel, z shadow softening radius in texels, w 1/shadow map size
27
+ vec4 u_camRight; // xyz screen right, w pixel growth: metres per pixel gained per metre of depth (0 orthographic)
28
+ vec4 u_camUp; // xyz screen up, w logarithmic depth scale 1/log2(1 + far) (0: depth is not logarithmic)
29
+ vec4 u_camForward; // xyz view direction, w depth at which u_view.x applies
30
+ ${slots.afterView}${extra.frameTail ?? ''}};
31
+
32
+ layout(std140) uniform Palette {
33
+ vec4 u_palette[${PALETTE_CAPACITY}];
34
+ };
35
+ `;
36
+ }
37
+ /**
38
+ * Local up, shader side. Appended to every program header, so any shader may ask which way is up at a point of render
39
+ * space instead of assuming +y:
40
+ *
41
+ * - `localUp(p)`: unit up at `p`.
42
+ * - `localHeight(p)`: height of `p` above the base surface.
43
+ * - `localFrame(p)`: three axes at `p` as the columns of a matrix; the middle one is up, the other two span the
44
+ * horizon. A point given along x, up and z of an upright object is `p + localFrame(p) * q`.
45
+ *
46
+ * By default up is +y, the height is `p.y` and the frame is the identity. A renderer made with `radialUp` defines
47
+ * `WH_LOCAL_UP` and fills `u_upCenter`, the last member of the Frame block: up then points away from that point, and
48
+ * the frame is the world's axes turned by the shortest rotation that takes +y to up (`upFrame` of base is the same
49
+ * frame on the CPU).
50
+ */
51
+ export const LOCAL_UP_DEFINE = '#define WH_LOCAL_UP 1\n';
52
+ export const LOCAL_UP_MEMBER = ' vec4 u_upCenter; // xyz the point up points away from, in render space, w radius of the base surface\n';
53
+ /** Floats `LOCAL_UP_MEMBER` adds to the end of the Frame block. */
54
+ export const LOCAL_UP_FLOATS = 4;
55
+ export const LOCAL_UP_GLSL = /* glsl */ `
56
+ #ifdef WH_LOCAL_UP
57
+ vec3 localUp(vec3 p) { return normalize(p - u_upCenter.xyz); }
58
+ float localHeight(vec3 p) { return length(p - u_upCenter.xyz) - u_upCenter.w; }
59
+ mat3 localFrame(vec3 p) {
60
+ vec3 u = localUp(p);
61
+ // Opposite to +y the shortest rotation is not defined: half a turn about x.
62
+ if (u.y < -0.99999) return mat3(1.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, -1.0);
63
+ // k = 1 / (1 + u.y). Below the horizon it is taken as (1 - u.y) / (u.x * u.x + u.z * u.z), the same number for a
64
+ // unit vector: 1 + u.y loses its digits near -y, and the frame would be sheared by a percent there.
65
+ float h = u.x * u.x + u.z * u.z;
66
+ float k = u.y > 0.0 || h == 0.0 ? 1.0 / (1.0 + u.y) : (1.0 - u.y) / h;
67
+ return mat3(1.0 - u.x * u.x * k, -u.x, -u.x * u.z * k, u.x, u.y, u.z, -u.x * u.z * k, -u.z, 1.0 - u.z * u.z * k);
68
+ }
69
+ #else
70
+ vec3 localUp(vec3 p) { return vec3(0.0, 1.0, 0.0); }
71
+ float localHeight(vec3 p) { return p.y; }
72
+ mat3 localFrame(vec3 p) { return mat3(1.0); }
73
+ #endif
74
+ `;
75
+ /**
76
+ * Logarithmic depth, fragment side. A fragment shader drawn into the main target gets the prelude right after the
77
+ * header and the epilogue at its end (`Renderer.resolve` does both): the shader's own `main` becomes
78
+ * `whMaterialMain`, and the real `main` calls it and then writes depth from the distance along the view, which the
79
+ * rasteriser carries in `gl_FragCoord.w`. No varying is involved, so any vertex shader fits. The polygon offset of the
80
+ * draw state arrives as `u_whDepthBias` (factor, units), because the pipeline's own offset does not reach a depth the
81
+ * shader writes.
82
+ */
83
+ export const LOG_DEPTH_PRELUDE = /* glsl */ `uniform vec2 u_whDepthBias;
84
+ #define main whMaterialMain
85
+ `;
86
+ export const LOG_DEPTH_EPILOGUE = /* glsl */ `
87
+ #undef main
88
+ void main() {
89
+ whMaterialMain();
90
+ // Orthographic views and programs outside the 3D scene (u_camUp.w = 0) keep the depth of the pipeline.
91
+ float d = u_camUp.w > 0.0 ? log2(1.0 + max(1.0 / gl_FragCoord.w, 0.0)) * u_camUp.w : gl_FragCoord.z;
92
+ float slope = max(abs(dFdx(d)), abs(dFdy(d)));
93
+ // 1.2e-7: the smallest step a 32-bit float depth resolves near 1.
94
+ gl_FragDepth = clamp(d + u_whDepthBias.x * slope + u_whDepthBias.y * 1.2e-7, 0.0, 1.0);
95
+ }
96
+ `;
97
+ /**
98
+ * Pixel size functions, appended to every program header (after the Frame block, which they read):
99
+ * `metresPerPixelAt(p)` (metres one screen pixel covers at a world point), `detailVisible(cellM)` and
100
+ * `detailVisibleAt(cellM, p)` (1 while a detail of that size is still sharp, 0 where it should vanish). They were
101
+ * part of `<noise>` before; `<noise>` keeps them for sources without the header, guarded by `WH_PIXEL_SIZE`.
102
+ */
103
+ export const PIXEL_SIZE_GLSL = /* glsl */ `
104
+ #ifndef WH_PIXEL_SIZE
105
+ #define WH_PIXEL_SIZE 1
106
+ float detailVisible(float cellM) {
107
+ return 1.0 - smoothstep(0.5, 0.9, u_view.x / cellM);
108
+ }
109
+ float metresPerPixelAt(vec3 worldPos) {
110
+ return max(u_view.x + u_camRight.w * (dot(worldPos, u_camForward.xyz) - u_camForward.w), 1e-4);
111
+ }
112
+ float detailVisibleAt(float cellM, vec3 worldPos) {
113
+ return 1.0 - smoothstep(0.5, 0.9, metresPerPixelAt(worldPos) / cellM);
114
+ }
115
+ #endif
116
+ `;
117
+ export const NOISE = /* glsl */ `
118
+ // Noise from an integer hash. Never from the sine function: it gives different results on different GPUs.
119
+ uint hashU(uint x) {
120
+ x ^= x >> 16; x *= 0x7feb352dU;
121
+ x ^= x >> 15; x *= 0x846ca68bU;
122
+ x ^= x >> 16;
123
+ return x;
124
+ }
125
+ float hash1(uint x) { return float(hashU(x)) * (1.0 / 4294967296.0); }
126
+ float hash2(ivec2 p, uint seed) {
127
+ return float(hashU(uint(p.x) * 0x9e3779b1U ^ hashU(uint(p.y) + seed * 0x85ebca6bU))) * (1.0 / 4294967296.0);
128
+ }
129
+ float hash3(ivec3 p, uint seed) {
130
+ return float(hashU(uint(p.x) * 0x9e3779b1U ^ hashU(uint(p.y) * 0xc2b2ae35U ^ hashU(uint(p.z) + seed)))) * (1.0 / 4294967296.0);
131
+ }
132
+ // Smooth value noise, 0–1.
133
+ float vnoise(vec2 p, uint seed) {
134
+ vec2 f = floor(p);
135
+ ivec2 i = ivec2(f);
136
+ vec2 t = p - f;
137
+ t = t * t * (3.0 - 2.0 * t);
138
+ float a = hash2(i, seed), b = hash2(i + ivec2(1, 0), seed);
139
+ float c = hash2(i + ivec2(0, 1), seed), d = hash2(i + ivec2(1, 1), seed);
140
+ return mix(mix(a, b, t.x), mix(c, d, t.x), t.y);
141
+ }
142
+ float vnoise3(vec3 p, uint seed) {
143
+ vec3 f = floor(p);
144
+ ivec3 i = ivec3(f);
145
+ vec3 t = p - f;
146
+ t = t * t * (3.0 - 2.0 * t);
147
+ float a = mix(hash3(i, seed), hash3(i + ivec3(1, 0, 0), seed), t.x);
148
+ float b = mix(hash3(i + ivec3(0, 1, 0), seed), hash3(i + ivec3(1, 1, 0), seed), t.x);
149
+ float c = mix(hash3(i + ivec3(0, 0, 1), seed), hash3(i + ivec3(1, 0, 1), seed), t.x);
150
+ float d = mix(hash3(i + ivec3(0, 1, 1), seed), hash3(i + ivec3(1, 1, 1), seed), t.x);
151
+ return mix(mix(a, b, t.y), mix(c, d, t.y), t.z);
152
+ }
153
+ // The pixel size functions (detailVisible, metresPerPixelAt, detailVisibleAt) come with the header now
154
+ // (PIXEL_SIZE_GLSL); a source that includes noise without the header still gets them here.
155
+ #ifndef WH_PIXEL_SIZE
156
+ #define WH_PIXEL_SIZE 1
157
+ // How visible a detail of size cellM metres still is: 1 = sharp, 0 = the pattern should vanish.
158
+ // Applies to smooth noise: fully visible from two pixels per cell, around one pixel not at all,
159
+ // because it would fight with the screen grid and flicker when panning. Patterns with a sharp edge
160
+ // (grids, stripes) have a stricter rule of their own.
161
+ float detailVisible(float cellM) {
162
+ return 1.0 - smoothstep(0.5, 0.9, u_view.x / cellM);
163
+ }
164
+ // Size of one screen pixel at a world point, in metres. Constant for an orthographic view, grows with depth in perspective.
165
+ float metresPerPixelAt(vec3 worldPos) {
166
+ return max(u_view.x + u_camRight.w * (dot(worldPos, u_camForward.xyz) - u_camForward.w), 1e-4);
167
+ }
168
+ // detailVisible at a world point: the same rule with the pixel size there, for perspective views.
169
+ float detailVisibleAt(float cellM, vec3 worldPos) {
170
+ return 1.0 - smoothstep(0.5, 0.9, metresPerPixelAt(worldPos) / cellM);
171
+ }
172
+ #endif
173
+ `;
@@ -0,0 +1,26 @@
1
+ export { NOISE, PIXEL_SIZE_GLSL, frameBlockFloats, shaderHeader } from './glsl.ts';
2
+ export type { FrameSlotSizes, FrameSlotsGlsl, HeaderExtra } from './glsl.ts';
3
+ export { DEPTH_FS, DEPTH_VS, SHADOW_AREAS_MAX, SHADOW_HORIZON_UNIT } from './shadow-glsl.ts';
4
+ export { InstancedLayer, Material, MeshLayer, Renderer, SHADOW_MAP_UNIT, cullInstances } from './renderer.ts';
5
+ export { PieceLayer } from './pieces.ts';
6
+ export type { MeshPiece, PieceLayerOptions } from './pieces.ts';
7
+ export { POINT_LIGHTS_BINDING, POINT_LIGHTS_PER_DRAW, PointLightSet, pointLightsGlsl } from './point-lights.ts';
8
+ export type { PointLight, PointLightSetOptions } from './point-lights.ts';
9
+ export { INSTANCE_SET_FLOATS, INSTANCE_SET_GLSL, INSTANCE_SET_LAYOUT, InstanceSetLayer, instanceSet, instanceSetAttribs, pushInstance, writeInstance } from './instance-sets.ts';
10
+ export type { InstanceSet, InstanceSetLayerOptions } from './instance-sets.ts';
11
+ export { ParticleLayer } from './particles.ts';
12
+ export type { ParticleCounts, ParticleLayerOptions, ParticleSource } from './particles.ts';
13
+ export { InstanceCellLayer, cellDetail } from './cells.ts';
14
+ export type { CellDetail, InstanceCellGroup, InstanceCellLayerOptions } from './cells.ts';
15
+ export type { DrawContext, FrameStats, InstanceBatch, InstanceBounds, InstancedLayerOptions, Layer, MeshLayerOptions, PassName, RenderProbe, RendererOptions, SceneBounds, ShadowHorizon, ShadowMode, ShadowOptions, } from './renderer.ts';
16
+ export { DUST_LIFE_S, TransientLayer } from './transient.ts';
17
+ export type { TransientBatch, TransientStats } from './transient.ts';
18
+ export { RIG_POSE_GLSL, RigLayer } from './rig.ts';
19
+ export type { RigCounts, RigLayerOptions } from './rig.ts';
20
+ export { BoneInstances } from './bone-instances.ts';
21
+ export type { BonePart } from './bone-instances.ts';
22
+ export { POINT_VALUE_ATTRIBS, POINT_VALUE_MATERIAL, RASTER_VALUE_MATERIAL, SHAPE_VALUE_MATERIAL, VALUE_GLSL } from './value-glsl.ts';
23
+ export { POINT_VALUE_LAYOUT, PointValueLayer, RasterValueLayer, ShapeValueLayer, ValueSteps } from './value-layers.ts';
24
+ export type { PointValueOptions, RasterValueOptions, ShapeValueOptions, ValueGrid, ValueSheetTile, ValueStyle } from './value-layers.ts';
25
+ export { NEAR_INSTANCE_GLSL, NearInstanceLayer } from './near-instances.ts';
26
+ export type { NearInstanceLayerOptions, NearKindDraw, NearPart, NearWind } from './near-instances.ts';
@@ -0,0 +1,16 @@
1
+ // Layer 5, render: the renderer (passes, shadow map, layers), materials, texture bakes, shared GLSL pieces, the
2
+ // shadow pass shaders, culled mesh pieces, instances in cells with levels of detail and instanced rigs.
3
+ export { NOISE, PIXEL_SIZE_GLSL, frameBlockFloats, shaderHeader } from './glsl.js';
4
+ export { DEPTH_FS, DEPTH_VS, SHADOW_AREAS_MAX, SHADOW_HORIZON_UNIT } from './shadow-glsl.js';
5
+ export { InstancedLayer, Material, MeshLayer, Renderer, SHADOW_MAP_UNIT, cullInstances } from './renderer.js';
6
+ export { PieceLayer } from './pieces.js';
7
+ export { POINT_LIGHTS_BINDING, POINT_LIGHTS_PER_DRAW, PointLightSet, pointLightsGlsl } from './point-lights.js';
8
+ export { INSTANCE_SET_FLOATS, INSTANCE_SET_GLSL, INSTANCE_SET_LAYOUT, InstanceSetLayer, instanceSet, instanceSetAttribs, pushInstance, writeInstance } from './instance-sets.js';
9
+ export { ParticleLayer } from './particles.js';
10
+ export { InstanceCellLayer, cellDetail } from './cells.js';
11
+ export { DUST_LIFE_S, TransientLayer } from './transient.js';
12
+ export { RIG_POSE_GLSL, RigLayer } from './rig.js';
13
+ export { BoneInstances } from './bone-instances.js';
14
+ export { POINT_VALUE_ATTRIBS, POINT_VALUE_MATERIAL, RASTER_VALUE_MATERIAL, SHAPE_VALUE_MATERIAL, VALUE_GLSL } from './value-glsl.js';
15
+ export { POINT_VALUE_LAYOUT, PointValueLayer, RasterValueLayer, ShapeValueLayer, ValueSteps } from './value-layers.js';
16
+ export { NEAR_INSTANCE_GLSL, NearInstanceLayer } from './near-instances.js';
@@ -0,0 +1,97 @@
1
+ import type { MeshData, VertexLayout } from '../base/data.ts';
2
+ import type { PointLightSet } from './point-lights.ts';
3
+ import { MeshLayer } from './renderer.ts';
4
+ import type { DrawContext, MeshLayerOptions, Renderer } from './renderer.ts';
5
+ /** Floats per instance record. */
6
+ export declare const INSTANCE_SET_FLOATS = 16;
7
+ /** Per-instance layout: three vec4 of matrix columns, then the free vec4. */
8
+ export declare const INSTANCE_SET_LAYOUT: VertexLayout;
9
+ /**
10
+ * Attribute locations of the instance record, starting at `first` (after the mesh's own attributes), for the
11
+ * material's `attribs`.
12
+ */
13
+ export declare function instanceSetAttribs(first: number): Record<'i_m0' | 'i_m1' | 'i_m2' | 'i_extra', number>;
14
+ /** Vertex shader include: the instance attributes and the transform of a point and a normal. */
15
+ export declare const INSTANCE_SET_GLSL = "\nin vec4 i_m0;\nin vec4 i_m1;\nin vec4 i_m2;\nin vec4 i_extra;\nmat3 instanceLinear() { return mat3(i_m0.xyz, vec3(i_m0.w, i_m1.xy), vec3(i_m1.zw, i_m2.x)); }\nvec3 instancePoint(vec3 p) { return instanceLinear() * p + i_m2.yzw; }\nvec3 instanceNormal(vec3 n) {\n mat3 m = instanceLinear();\n // Cofactor matrix: the inverse transpose up to the determinant (normalised here, sign kept for mirrored copies).\n mat3 c = mat3(cross(m[1], m[2]), cross(m[2], m[0]), cross(m[0], m[1]));\n return normalize(c * n) * sign(dot(m[0], cross(m[1], m[2])));\n}\n";
16
+ /**
17
+ * One mesh and its instance records. Plain data owned by the caller; the layer reads it. Use few sets: each is a
18
+ * draw call.
19
+ */
20
+ export interface InstanceSet {
21
+ /** For statistics and debugging. */
22
+ readonly name?: string;
23
+ /** The mesh in its own local coordinates. */
24
+ readonly mesh: MeshData;
25
+ readonly capacity: number;
26
+ /** capacity × `INSTANCE_SET_FLOATS`. */
27
+ readonly data: Float32Array;
28
+ /** Records in use (the first `count`). */
29
+ count: number;
30
+ /** Set after changing `data` or `count`; the layer uploads the set and clears it. */
31
+ dirty: boolean;
32
+ /** Drawn into the shadow map. */
33
+ castShadow: boolean;
34
+ /** Drawn at all. A change counts as a change of the shadow. */
35
+ visible: boolean;
36
+ }
37
+ /** A new, empty instance set. */
38
+ export declare function instanceSet(mesh: MeshData, capacity: number, o?: {
39
+ name?: string;
40
+ castShadow?: boolean;
41
+ visible?: boolean;
42
+ }): InstanceSet;
43
+ /**
44
+ * Writes record `index`: `m` holds 12 numbers, the linear part column by column (9) then the translation (3); then the
45
+ * four free numbers. Does not change `count` or `dirty`.
46
+ */
47
+ export declare function writeInstance(set: InstanceSet, index: number, m: ArrayLike<number>, a?: number, b?: number, c?: number, d?: number): void;
48
+ /** Appends a record (`count` grows, `dirty` is set); returns its index, or −1 when the set is full. */
49
+ export declare function pushInstance(set: InstanceSet, m: ArrayLike<number>, a?: number, b?: number, c?: number, d?: number): number;
50
+ export interface InstanceSetLayerOptions extends Omit<MeshLayerOptions, 'mesh'> {
51
+ /** Point lights: every draw sees the whole selection of the set (`PointLightSet.bindAll`). Default none. */
52
+ lights?: PointLightSet | null;
53
+ }
54
+ /**
55
+ * Instance sets drawn with one material: one instanced draw per visible set in the colour pass and, for sets that
56
+ * cast shadows, in the shadow pass. Not culled (sets are few). The material declares the instance attributes
57
+ * (`INSTANCE_SET_GLSL`, `instanceSetAttribs`).
58
+ */
59
+ export declare class InstanceSetLayer extends MeshLayer {
60
+ /** Its casters move: drawn over the kept static shadow map. */
61
+ readonly shadowCasters: 'dynamic';
62
+ /** Point lights for the colour pass. */
63
+ lights: PointLightSet | null;
64
+ /** Triangles of all visible instances after the last upload. */
65
+ triangles: number;
66
+ private readonly entries;
67
+ private readonly renderer;
68
+ private readonly device;
69
+ /** A caster was added, removed, moved, shown or hidden since the last shadow pass. */
70
+ private shadowChanged;
71
+ private readonly hook;
72
+ constructor(renderer: Renderer, o: InstanceSetLayerOptions);
73
+ /** The sets in the order they were added. */
74
+ get sets(): readonly InstanceSet[];
75
+ /**
76
+ * Adds a set (uploads its buffer, unless another layer of the same renderer already holds the set: then the two
77
+ * share it). Empty meshes and sets without capacity are ignored.
78
+ */
79
+ add(set: InstanceSet): void;
80
+ /** Takes a set out and deletes its GPU objects (the instance buffer once no layer holds the set). Returns whether it was there. */
81
+ remove(set: InstanceSet): boolean;
82
+ /** Stops the per-frame upload hook (call when the layer is dropped for good). */
83
+ dispose(): void;
84
+ /** Uploads the dirty sets. Runs at the start of every frame; call it yourself only to upload earlier. */
85
+ upload(): void;
86
+ /**
87
+ * True when a shadow-casting set changed since the layer was last drawn into the shadow map. Always false for a
88
+ * layer that does not draw in the shadow pass, and for one none of whose sets ever cast shadows.
89
+ */
90
+ shadowDirty(): boolean;
91
+ /**
92
+ * Sets are not culled: everything visible is kept. Returns the records of the visible sets, so the renderer's
93
+ * `instancesVisible` counts them with the instances of the culled layers.
94
+ */
95
+ cull(): number;
96
+ draw(ctx: DrawContext): void;
97
+ }
@@ -0,0 +1,241 @@
1
+ // Instance sets: one mesh drawn many times, each copy with its own affine transform (3 × 4) and four free numbers
2
+ // (typically a tint and one more value), in one instanced draw per set. They cast shadows. For anything that moves
3
+ // in its own way (parts posed by the owner's code, spinning or swinging pieces) or for repeated props; `RigLayer`
4
+ // covers the fixed set of rig motions, `InstancedLayer` many static copies with culling.
5
+ //
6
+ // The owner writes a set's `data` (`writeInstance`, `pushInstance`) and `count`, and sets `dirty`; the layer uploads
7
+ // dirty sets at the start of the next frame (`Renderer.beforeFrame`) with `Device.writeBuffer` over the set's own
8
+ // array, so nothing is allocated per frame. The layer is a dynamic shadow caster: `shadowDirty()` tells the renderer
9
+ // whether a set that casts shadows changed since the layer was last drawn into the shadow map (never, when the
10
+ // layer has no shadow pass or none of its sets casts shadows).
11
+ //
12
+ // One set may be drawn by several layers (for example one material for the surface and another for an outline):
13
+ // layers of one renderer share the set's GPU instance buffer. Whichever layer sees `dirty` first uploads it once and
14
+ // clears it; a version number tells the other layers that the records changed.
15
+ //
16
+ // Record layout (`INSTANCE_SET_FLOATS` = 16 floats): the 3 × 4 matrix as its four columns (linear part column by
17
+ // column, then the translation: 12 floats), then the four free numbers. The vertex include `INSTANCE_SET_GLSL`
18
+ // declares the attributes `i_m0`, `i_m1`, `i_m2`, `i_extra` and gives `instanceLinear()`, `instancePoint(p)`,
19
+ // `instanceNormal(n)`.
20
+ import { MeshLayer } from './renderer.js';
21
+ /** Floats per instance record. */
22
+ export const INSTANCE_SET_FLOATS = 16;
23
+ /** Per-instance layout: three vec4 of matrix columns, then the free vec4. */
24
+ export const INSTANCE_SET_LAYOUT = {
25
+ stride: INSTANCE_SET_FLOATS * 4,
26
+ attribs: [
27
+ { name: 'i_m0', size: 4, type: 'f32', offset: 0 },
28
+ { name: 'i_m1', size: 4, type: 'f32', offset: 16 },
29
+ { name: 'i_m2', size: 4, type: 'f32', offset: 32 },
30
+ { name: 'i_extra', size: 4, type: 'f32', offset: 48 },
31
+ ],
32
+ };
33
+ /**
34
+ * Attribute locations of the instance record, starting at `first` (after the mesh's own attributes), for the
35
+ * material's `attribs`.
36
+ */
37
+ export function instanceSetAttribs(first) {
38
+ return { i_m0: first, i_m1: first + 1, i_m2: first + 2, i_extra: first + 3 };
39
+ }
40
+ /** Vertex shader include: the instance attributes and the transform of a point and a normal. */
41
+ export const INSTANCE_SET_GLSL = /* glsl */ `
42
+ in vec4 i_m0;
43
+ in vec4 i_m1;
44
+ in vec4 i_m2;
45
+ in vec4 i_extra;
46
+ mat3 instanceLinear() { return mat3(i_m0.xyz, vec3(i_m0.w, i_m1.xy), vec3(i_m1.zw, i_m2.x)); }
47
+ vec3 instancePoint(vec3 p) { return instanceLinear() * p + i_m2.yzw; }
48
+ vec3 instanceNormal(vec3 n) {
49
+ mat3 m = instanceLinear();
50
+ // Cofactor matrix: the inverse transpose up to the determinant (normalised here, sign kept for mirrored copies).
51
+ mat3 c = mat3(cross(m[1], m[2]), cross(m[2], m[0]), cross(m[0], m[1]));
52
+ return normalize(c * n) * sign(dot(m[0], cross(m[1], m[2])));
53
+ }
54
+ `;
55
+ /** A new, empty instance set. */
56
+ export function instanceSet(mesh, capacity, o = {}) {
57
+ const cap = Math.max(0, Math.floor(capacity));
58
+ const set = {
59
+ mesh,
60
+ capacity: cap,
61
+ data: new Float32Array(cap * INSTANCE_SET_FLOATS),
62
+ count: 0,
63
+ dirty: true,
64
+ castShadow: o.castShadow ?? true,
65
+ visible: o.visible ?? true,
66
+ };
67
+ return o.name === undefined ? set : { ...set, name: o.name };
68
+ }
69
+ /**
70
+ * Writes record `index`: `m` holds 12 numbers, the linear part column by column (9) then the translation (3); then the
71
+ * four free numbers. Does not change `count` or `dirty`.
72
+ */
73
+ export function writeInstance(set, index, m, a = 1, b = 1, c = 1, d = 0) {
74
+ const o = index * INSTANCE_SET_FLOATS;
75
+ const out = set.data;
76
+ for (let k = 0; k < 12; k++)
77
+ out[o + k] = m[k];
78
+ out[o + 12] = a;
79
+ out[o + 13] = b;
80
+ out[o + 14] = c;
81
+ out[o + 15] = d;
82
+ }
83
+ /** Appends a record (`count` grows, `dirty` is set); returns its index, or −1 when the set is full. */
84
+ export function pushInstance(set, m, a = 1, b = 1, c = 1, d = 0) {
85
+ if (set.count >= set.capacity)
86
+ return -1;
87
+ const i = set.count++;
88
+ writeInstance(set, i, m, a, b, c, d);
89
+ set.dirty = true;
90
+ return i;
91
+ }
92
+ /** Set → its shared GPU buffer. A set added to layers of another device gets a buffer of its own there. */
93
+ const sharedOf = new WeakMap();
94
+ /**
95
+ * Instance sets drawn with one material: one instanced draw per visible set in the colour pass and, for sets that
96
+ * cast shadows, in the shadow pass. Not culled (sets are few). The material declares the instance attributes
97
+ * (`INSTANCE_SET_GLSL`, `instanceSetAttribs`).
98
+ */
99
+ export class InstanceSetLayer extends MeshLayer {
100
+ /** Its casters move: drawn over the kept static shadow map. */
101
+ shadowCasters = 'dynamic';
102
+ /** Point lights for the colour pass. */
103
+ lights;
104
+ /** Triangles of all visible instances after the last upload. */
105
+ triangles = 0;
106
+ entries = [];
107
+ renderer;
108
+ device;
109
+ /** A caster was added, removed, moved, shown or hidden since the last shadow pass. */
110
+ shadowChanged = false;
111
+ hook;
112
+ constructor(renderer, o) {
113
+ super(o);
114
+ this.renderer = renderer;
115
+ this.device = renderer.device;
116
+ this.lights = o.lights ?? null;
117
+ this.hook = () => this.upload();
118
+ renderer.beforeFrame.push(this.hook);
119
+ }
120
+ /** The sets in the order they were added. */
121
+ get sets() {
122
+ const out = [];
123
+ for (const e of this.entries)
124
+ out.push(e.set);
125
+ return out;
126
+ }
127
+ /**
128
+ * Adds a set (uploads its buffer, unless another layer of the same renderer already holds the set: then the two
129
+ * share it). Empty meshes and sets without capacity are ignored.
130
+ */
131
+ add(set) {
132
+ if (set.mesh.count === 0 || set.capacity === 0)
133
+ return;
134
+ let shared = sharedOf.get(set);
135
+ if (!shared || shared.device !== this.device) {
136
+ const instances = this.renderer.createInstances({ layout: INSTANCE_SET_LAYOUT, data: set.data });
137
+ const count = Math.min(set.count, set.capacity);
138
+ shared = { device: this.device, instances, users: 0, version: 0, count };
139
+ set.dirty = false;
140
+ if (!sharedOf.has(set))
141
+ sharedOf.set(set, shared);
142
+ }
143
+ shared.users++;
144
+ const mesh = this.renderer.createMesh(set.mesh, this.material, shared.instances);
145
+ mesh.instances = shared.count;
146
+ this.entries.push({ set, mesh, shared, version: shared.version, shown: set.visible });
147
+ if (set.castShadow)
148
+ this.shadowChanged = true;
149
+ }
150
+ /** Takes a set out and deletes its GPU objects (the instance buffer once no layer holds the set). Returns whether it was there. */
151
+ remove(set) {
152
+ const i = this.entries.findIndex((e) => e.set === set);
153
+ if (i < 0)
154
+ return false;
155
+ const e = this.entries[i];
156
+ this.entries.splice(i, 1);
157
+ this.device.deleteMesh(e.mesh);
158
+ if (--e.shared.users <= 0) {
159
+ this.device.deleteInstances(e.shared.instances);
160
+ if (sharedOf.get(set) === e.shared)
161
+ sharedOf.delete(set);
162
+ }
163
+ if (set.castShadow)
164
+ this.shadowChanged = true;
165
+ return true;
166
+ }
167
+ /** Stops the per-frame upload hook (call when the layer is dropped for good). */
168
+ dispose() {
169
+ const i = this.renderer.beforeFrame.indexOf(this.hook);
170
+ if (i >= 0)
171
+ this.renderer.beforeFrame.splice(i, 1);
172
+ }
173
+ /** Uploads the dirty sets. Runs at the start of every frame; call it yourself only to upload earlier. */
174
+ upload() {
175
+ let tris = 0;
176
+ for (const e of this.entries) {
177
+ const s = e.set;
178
+ const sh = e.shared;
179
+ if (s.dirty) {
180
+ const n = Math.min(s.count, s.capacity);
181
+ this.device.writeBuffer(sh.instances.buffer, s.data, n * INSTANCE_SET_FLOATS);
182
+ sh.count = n;
183
+ sh.version++;
184
+ s.dirty = false;
185
+ }
186
+ if (e.version !== sh.version) {
187
+ e.version = sh.version;
188
+ e.mesh.instances = sh.count;
189
+ if (s.castShadow && s.visible)
190
+ this.shadowChanged = true;
191
+ }
192
+ if (s.visible !== e.shown) {
193
+ e.shown = s.visible;
194
+ if (s.castShadow)
195
+ this.shadowChanged = true;
196
+ }
197
+ if (s.visible)
198
+ tris += e.mesh.triangles * e.mesh.instances;
199
+ }
200
+ this.triangles = tris;
201
+ }
202
+ /**
203
+ * True when a shadow-casting set changed since the layer was last drawn into the shadow map. Always false for a
204
+ * layer that does not draw in the shadow pass, and for one none of whose sets ever cast shadows.
205
+ */
206
+ shadowDirty() {
207
+ return this.shadowChanged && this.passes.has('shadow');
208
+ }
209
+ /**
210
+ * Sets are not culled: everything visible is kept. Returns the records of the visible sets, so the renderer's
211
+ * `instancesVisible` counts them with the instances of the culled layers.
212
+ */
213
+ cull() {
214
+ let n = 0;
215
+ for (const e of this.entries)
216
+ if (e.set.visible)
217
+ n += e.mesh.instances;
218
+ return n;
219
+ }
220
+ draw(ctx) {
221
+ const shadow = ctx.pass === 'shadow';
222
+ if (shadow)
223
+ this.shadowChanged = false;
224
+ let program = null;
225
+ for (const e of this.entries) {
226
+ const s = e.set;
227
+ // A mesh with 0 instances would be drawn once without instancing: skip empty sets.
228
+ if (!s.visible || e.mesh.instances <= 0 || (shadow && !s.castShadow))
229
+ continue;
230
+ if (!program) {
231
+ program = this.begin(ctx);
232
+ if (!program)
233
+ return;
234
+ if (ctx.pass === 'color' && this.lights)
235
+ this.lights.bindAll(program);
236
+ }
237
+ ctx.device.draw(e.mesh);
238
+ ctx.count(e.mesh.triangles * e.mesh.instances);
239
+ }
240
+ }
241
+ }
@@ -0,0 +1,75 @@
1
+ import type { MeshData } from '../base/data.ts';
2
+ import type { Vec3 } from '../base/math.ts';
3
+ import type { NearFade } from '../gen-core/near-scatter.ts';
4
+ import type { Device } from '../gpu/index.ts';
5
+ import { MeshLayer } from './renderer.ts';
6
+ import type { DrawContext, MeshLayerOptions } from './renderer.ts';
7
+ /** One kind: its unit shape (base y = 0, top y = 1), its fade, its ceiling and the tip shift per metre of height at wind 1. */
8
+ export interface NearKindDraw {
9
+ mesh: MeshData;
10
+ fade: NearFade;
11
+ maxInstances: number;
12
+ bendM: number;
13
+ }
14
+ /** One kind of one set: the anchor (a world point in doubles), a sphere round it that holds every record, the records. */
15
+ export interface NearPart {
16
+ anchor: Vec3;
17
+ radiusM: number;
18
+ /** Records of one kind in the order of `nearScatter` (highest order first). */
19
+ data: Float32Array;
20
+ }
21
+ /** The wind: unit direction along the ground in the frame of the anchors, strength 0–1, time in seconds. */
22
+ export interface NearWind {
23
+ dir: Vec3;
24
+ strength: number;
25
+ timeS: number;
26
+ /** Share of the two travelling waves, 0–1 (default 1); 0 leaves the steady lean alone. */
27
+ waves?: number;
28
+ }
29
+ export interface NearInstanceLayerOptions extends Omit<MeshLayerOptions, 'mesh'> {
30
+ kinds: readonly NearKindDraw[];
31
+ /** Metres the eye may move before the buffers are rebuilt. Default 2. */
32
+ stepM?: number;
33
+ /** World position of the render origin (doubles), asked at every draw. Default the world origin. */
34
+ origin?: () => readonly [number, number, number];
35
+ }
36
+ /** Instances of several kinds round the eye; see the header of this file. */
37
+ export declare class NearInstanceLayer extends MeshLayer {
38
+ wind: NearWind;
39
+ readonly stats: {
40
+ parts: number;
41
+ held: number;
42
+ drawn: number;
43
+ clipped: number;
44
+ rebuilds: number;
45
+ bytes: number;
46
+ };
47
+ private readonly device;
48
+ private readonly kinds;
49
+ private readonly parts;
50
+ private readonly stepM;
51
+ private readonly origin;
52
+ private dirty;
53
+ /** Anchor of the layer: the eye of the last rebuild. */
54
+ private anchor;
55
+ private eye;
56
+ constructor(device: Device, o: NearInstanceLayerOptions);
57
+ private static id;
58
+ /** Adds, replaces or (with null) removes one kind of one set. */
59
+ put(key: string, kind: number, part: NearPart | null): void;
60
+ has(key: string, kind: number): boolean;
61
+ /** Once per frame: the eye in world doubles and a gain 0–1 per kind that scales its reach. */
62
+ view(eye: Vec3, gains: readonly number[]): void;
63
+ draw(ctx: DrawContext): void;
64
+ dispose(): void;
65
+ }
66
+ /**
67
+ * Vertex include for the app's material (after `#include <header>`). Declares the instance attributes `i_base`,
68
+ * `i_shape`, `i_color` and the uniforms the layer sets per draw (`u_nearOffset`: layer anchor − render origin;
69
+ * `u_nearEye`: the eye in render space; `u_nearFade`: gain · reach, band, soft; `u_nearWind`: xyz = direction ·
70
+ * strength · bendM, w = time; `u_nearWave`: phases of the anchor per wave and the share of the waves). Gives
71
+ * `nearBase()` (the base in render space), `nearScale()` (0–1, the fade), `nearAxes(base)` (the instance's frame:
72
+ * upright along `localFrame`, turned about up) and `nearPoint(unitPos)`: the vertex in render space, scaled by the
73
+ * fade and bent by the wind.
74
+ */
75
+ export declare const NEAR_INSTANCE_GLSL: string;