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,74 @@
1
+ import type { MaterialDesc } from '../base/material.ts';
2
+ import type { RigDesc } from '../base/rig.ts';
3
+ import type { DrawContext, Layer, Renderer } from './renderer.ts';
4
+ /**
5
+ * Vertex include of rig posing (registered as `<rig-pose>` by `RigLayer`; write it after `<header>` and
6
+ * `<noise>`): the part attributes and instance record (`RIG_ATTRIBS`), the uniforms `RigLayer` sets (`u_rig`,
7
+ * `u_rig2`, with `WH_RIG_EXT` also `u_rig3`) and two functions. `bool rigPose(out vec3 pos, out vec3 nrm)`: the
8
+ * world position and unit normal of the vertex in the pose of its instance; false when the instance lacks the
9
+ * part's variant (collapse the vertex). `vec3 rigColor()`: the part's colour times its instance colour slot.
10
+ */
11
+ export declare const RIG_POSE_GLSL = "\nin vec3 a_pos;\nin vec3 a_normal;\nin vec3 a_pivot;\nin vec4 a_motion; // kind, amount, segment, colour slot (\u22121 fixed colour)\nin vec4 a_color; // rgb, finish\nin vec2 a_extra; // gait phase, variant bit (\u22121 always)\nin vec4 i_p0;\nin vec4 i_dir;\nin vec4 i_p1;\nin vec4 i_move; // distance m, speed m/s, turn rate rad/s, seed\nin vec4 i_look; // state, height scale, width scale, variant bits\nin vec4 i_colors;\nuniform vec4 u_rig; // x stride m (0: rolls instead of walking), y steering base m, z roll gain, w roll limit rad\nuniform vec4 u_rig2; // x bob m, y animate 0\u20131, z lamps 0\u20131, w leg length m\nconst float TAU = 6.2831853;\nmat3 rotX(float a) { float c = cos(a), s = sin(a); return mat3(1.0, 0.0, 0.0, 0.0, c, s, 0.0, -s, c); }\nmat3 rotY(float a) { float c = cos(a), s = sin(a); return mat3(c, 0.0, -s, 0.0, 1.0, 0.0, s, 0.0, c); }\nmat3 rotZ(float a) { float c = cos(a), s = sin(a); return mat3(c, s, 0.0, -s, c, 0.0, 0.0, 0.0, 1.0); }\n\n\nvec3 toWorld(vec3 q, float seg, out vec3 fwd, out vec3 right) {\n vec4 p = seg < 0.5 ? i_p0 : i_p1;\n vec2 d = seg < 0.5 ? i_dir.xy : i_dir.zw;\n#ifdef WH_LOCAL_UP\n // Up is radial: the heading (x, z) is meant in the local frame of the anchor, and heights go along its up.\n mat3 lf = localFrame(p.xyz);\n fwd = lf * vec3(d.x, 0.0, d.y);\n right = lf * vec3(-d.y, 0.0, d.x);\n return p.xyz + right * q.x + lf[1] * (q.y + q.z * p.w) + fwd * q.z;\n#else\n fwd = vec3(d.x, 0.0, d.y);\n right = vec3(-d.y, 0.0, d.x);\n return p.xyz + right * q.x + vec3(0.0, q.y + q.z * p.w, 0.0) + fwd * q.z;\n#endif\n}\n\n#ifdef WH_RIG_EXT\nuniform vec4 u_rig3; // x gait (0 pair, 1 free), y dip m, z w the speeds the gait fades in between (m/s at scale 1)\n#endif\nvec3 unpackRgb(float c) {\n float r = floor(c / 65536.0);\n float g = floor((c - r * 65536.0) / 256.0);\n return vec3(r, g, c - r * 65536.0 - g * 256.0) / 255.0;\n}\nbool rigPose(out vec3 pos, out vec3 nrm) {\n // A variant part the instance does not have collapses outside the clip volume.\n if (a_extra.y >= 0.0 && mod(floor(i_look.w / exp2(a_extra.y)), 2.0) < 0.5) {\n return false;\n }\n float kind = a_motion.x;\n float amount = a_motion.y;\n float anim = u_rig2.y;\n float dist = i_move.x;\n float speed = i_move.y;\n float seed = i_move.w;\n float t = u_time.x;\n vec3 q = a_pos - a_pivot;\n vec3 n = a_normal;\n mat3 r = mat3(1.0);\n float dy = 0.0;\n float roll = 0.0;\n bool sprung = true;\n if (u_rig.x > 0.0) {\n // Gait. Phase in cycles from the distance travelled, so the stance foot moves exactly backward at the speed\n // of travel and stays still on the ground; the swing foot comes forward on a smooth step.\n float walk = smoothstep(0.15, 0.9, speed) * anim;\n float phase = dist / (u_rig.x * i_look.y) + seed;\n float legL = u_rig2.w;\n // The leg on the ground: its foot offset falls linearly from +1/4 to \u22121/4 of the stride.\n float us = (0.25 - 0.5 * fract(2.0 * phase)) * u_rig.x * walk;\n dy = legL * (cos(asin(clamp(us / legL, -0.9, 0.9))) - 1.0);\n roll = 0.035 * sin(TAU * phase) * walk + 0.02 * sin(t * 0.8 + seed * TAU) * (1.0 - walk) * anim;\n float f = fract(phase + a_extra.x);\n float u = f < 0.5 ? 0.25 - f : -0.25 + 0.5 * smoothstep(0.0, 1.0, (f - 0.5) * 2.0);\n if (kind == 3.0) {\n float lift = f < 0.5 ? 0.0 : sin(3.14159 * (f - 0.5) * 2.0);\n q.y *= 1.0 - 0.22 * lift * walk;\n r = rotX(-asin(clamp(u * u_rig.x * walk / amount, -0.9, 0.9)));\n } else if (kind == 4.0) {\n r = rotX(-amount * 4.0 * u * walk);\n }\n } else {\n // Rolling: wheels spin with the distance, the steering pair turns toward the curve, the body leans out of it\n // and bobs a little on the surface.\n float k = i_move.z / max(speed, 0.5);\n roll = clamp(u_rig.z * speed * i_move.z, -u_rig.w, u_rig.w) * anim;\n dy = u_rig2.x * (vnoise(vec2(dist * 0.9, seed * 97.0), 7u) - 0.5) * 2.0 * min(speed / 4.0, 1.0) * anim;\n if (kind == 1.0 || kind == 2.0) {\n sprung = false;\n r = rotX(dist / amount * anim);\n if (kind == 2.0) r = rotY(clamp(atan(u_rig.y * k), -0.6, 0.6) * anim) * r;\n }\n }\n#ifdef WH_RIG_EXT\n // Extended pose. g: the gait phase in cycles (time when the rig rolls); walkX: how much it walks, 0\u20131.\n float g = u_rig.x > 0.0 ? dist / (u_rig.x * i_look.y) + seed : 0.5 * t + seed;\n float walkX = u_rig.x > 0.0 ? smoothstep(0.15, 0.9, speed) * anim : 0.0;\n if (u_rig.x > 0.0 && u_rig3.x > 0.5) {\n // Free gait: every limb plants its own end point. Stance (f < 0.5): the end point lies on the ground and moves\n // backward at the speed of travel; swing: it comes forward on a smooth step, lifted. The limb is aimed at it and\n // stretched along its own length. The body dips by u_rig3.y twice per cycle and keeps the sway.\n walkX = smoothstep(u_rig3.z * i_look.y, u_rig3.w * i_look.y, speed) * anim;\n dy = -u_rig3.y * walkX * (0.5 + 0.5 * cos(2.0 * TAU * g));\n roll = 0.035 * sin(TAU * g) * walkX + 0.02 * sin(t * 0.8 + seed * TAU) * (1.0 - walkX) * anim;\n q = a_pos - a_pivot;\n r = mat3(1.0);\n float fx = fract(g + a_extra.x);\n float ux = fx < 0.5 ? 0.25 - fx : -0.25 + 0.5 * smoothstep(0.0, 1.0, (fx - 0.5) * 2.0);\n if (kind == 3.0) {\n float lift = fx < 0.5 ? 0.0 : sin(3.14159 * (fx - 0.5) * 2.0);\n // From the pivot (lowered by the dip) to the end point; at rest straight down by the limb's length.\n vec2 e = vec2(ux * u_rig.x * walkX, mix(-amount, 0.22 * amount * lift - (a_pivot.y + dy), walkX));\n q.y *= length(e) / amount;\n r = rotX(-atan(e.x, -e.y));\n } else if (kind == 4.0) {\n r = rotX(-amount * 4.0 * ux * walkX);\n }\n }\n if (kind > 6.5) {\n // A link of a chain (kind 7, 8, 9: about x, y, z): turns about the root by amount \u00D7 its distance from the root \u00D7\n // a wave that lags along the chain; zero at the root, so the chain never leaves the body and links stay joined.\n float dr = length(a_pos - a_pivot);\n float wave = walkX * sin(TAU * (g - a_extra.x * dr)) + (1.0 - walkX) * 0.4 * sin(TAU * (0.15 * t + seed - a_extra.x * dr)) * anim;\n float th = amount * dr * wave;\n r = kind < 7.5 ? rotX(th) : kind < 8.5 ? rotY(th) : rotZ(th);\n }\n#endif\n if (kind == 5.0) {\n float still = 1.0 - smoothstep(0.1, 0.6, speed);\n r = rotY(amount * sin(t * 0.45 + seed * 40.0) * still * anim);\n } else if (kind == 6.0) {\n float wa = (vnoise(vec2(t * 1.7 + seed * 50.0, dist * 0.6), 3u) - 0.5) * 2.0;\n float wb = (vnoise(vec2(t * 1.3 + seed * 20.0, dist * 0.4), 5u) - 0.5) * 2.0;\n r = rotY(amount * wb * anim) * rotX(amount * wa * anim);\n }\n q = r * q + a_pivot;\n n = r * n;\n if (sprung) {\n // Roll about a point half a metre above the ground.\n vec3 c = vec3(0.0, 0.5, 0.0);\n q = rotZ(roll) * (q - c) + c;\n n = rotZ(roll) * n;\n q.y += dy;\n }\n q *= vec3(i_look.y * i_look.z, i_look.y, i_look.y);\n vec3 fwd, right;\n pos = toWorld(q, a_motion.z, fwd, right);\n#ifdef WH_LOCAL_UP\n nrm = normalize(right * n.x + cross(right, fwd) * n.y + fwd * n.z);\n#else\n nrm = normalize(right * n.x + vec3(0.0, n.y, 0.0) + fwd * n.z);\n#endif\n return true;\n}\nvec3 rigColor() {\n vec3 c = a_color.rgb;\n if (a_motion.w >= 0.0) {\n int s = int(a_motion.w + 0.5);\n c *= unpackRgb(s == 0 ? i_colors.x : s == 1 ? i_colors.y : s == 2 ? i_colors.z : i_colors.w);\n }\n return c;\n}\n";
12
+ export interface RigLayerOptions {
13
+ /**
14
+ * The game's material for the bodies instead of the engine's: attributes `RIG_ATTRIBS`, a vertex shader over the
15
+ * include `<rig-pose>` (after `<header>` and `<noise>`; `rigPose`, `rigColor`), any fragment shader. The layer sets
16
+ * `u_rig`, `u_rig2` (`u_rig3` for extended kinds) and `u_shadowMap`. A `shadow` pass of it is used when
17
+ * `castShadow` is on. Default: the engine's material, drawn exactly as before this option existed.
18
+ */
19
+ material?: MaterialDesc;
20
+ /** Draw the posed bodies into the shadow map (a dynamic caster, every instance, near detail). Default false. */
21
+ castShadow?: boolean;
22
+ /** Soft contact shadows under the rigs. Default true. */
23
+ contactShadows?: boolean;
24
+ }
25
+ export interface RigCounts {
26
+ near: number;
27
+ far: number;
28
+ hidden: number;
29
+ }
30
+ export declare class RigLayer implements Layer {
31
+ /** Lamps and lit interiors, 0 by day to 1 at night; the app sets it from the light. */
32
+ lamps: number;
33
+ /** Off draws nothing. */
34
+ enabled: boolean;
35
+ /** Counts of the last drawn frame, all kinds together. */
36
+ readonly counts: RigCounts;
37
+ /** `'dynamic'` with `castShadow`; unset otherwise (a layer that casts nothing, as before the option existed). */
38
+ readonly shadowCasters?: 'dynamic';
39
+ private readonly kinds;
40
+ private readonly bodyDesc;
41
+ private readonly body;
42
+ private readonly contact;
43
+ /** The program of the extended pose, made by the first kind that needs it. */
44
+ private bodyExt;
45
+ private readonly renderer;
46
+ private readonly castShadow;
47
+ private readonly contactShadows;
48
+ private depth;
49
+ private depthExt;
50
+ /** Records changed since the layer was last drawn into the shadow map. */
51
+ private shadowChanged;
52
+ constructor(renderer: Renderer, o?: RigLayerOptions);
53
+ /** Adds a kind of rig that can be drawn up to `capacity` times; returns its index for `set`. */
54
+ addKind(desc: RigDesc, capacity: number): number;
55
+ /** Removes every kind and deletes its GPU objects. Kind indices returned by `addKind` are invalid afterwards. */
56
+ clear(): void;
57
+ /** The instance records of a kind for this frame (`RIG_INSTANCE_FLOATS` each); the layer culls them when drawing. */
58
+ set(kind: number, data: Float32Array, count: number): void;
59
+ /** The buffer `set` copies from, to fill in place: write records, then call `commit`. */
60
+ staging(kind: number): Float32Array;
61
+ commit(kind: number, count: number): void;
62
+ /**
63
+ * With `castShadow`: true when records were set or committed since the layer was last drawn into the shadow map.
64
+ * The pose also moves with the frame time; set the records every frame for moving shadows.
65
+ */
66
+ shadowDirty(): boolean;
67
+ draw(ctx: DrawContext): void;
68
+ /** The shadow pass: every instance of every kind, posed at near detail. */
69
+ private drawShadow;
70
+ /** The material of the shadow pass: the game's when it has a `shadow` pass, else the engine's depth of the pose. */
71
+ private depthDesc;
72
+ /** Culls every kind against the view and sorts the rest into the near and far buffers, then uploads them. */
73
+ private split;
74
+ }
@@ -0,0 +1,576 @@
1
+ // Instanced rigs: every kind is one mesh of rigid parts, posed in the vertex shader from the instance record (see
2
+ // `RIG_INSTANCE_LAYOUT`). Gait, rolling, steering, body roll, bob and idle sway are closed-form functions of the
3
+ // distance travelled, speed, turn rate and the animation time, so nothing is stored between frames and a jump in
4
+ // time poses correctly at once. Every frame the layer culls the instances against the view and splits them by
5
+ // their size on screen: near (all parts, animated), far (fewer parts, still) and hidden. Per kind and level of
6
+ // detail that is one draw for the bodies and one for a soft contact shadow. Kinds with a free gait or chains of
7
+ // links (`rigExtended`) are drawn by a second program, the same text with `WH_RIG_EXT` defined, made when the first
8
+ // such kind is added; every other kind keeps the first program as it was.
9
+ //
10
+ // The posing is its own vertex include (`RIG_POSE_GLSL`, registered as `<rig-pose>`), so a game can draw the bodies
11
+ // with its own material (`RigLayerOptions.material`); without one the layer draws exactly as before. By default rigs
12
+ // do not enter the shadow map (redrawing it costs time), they receive its shadows and stand on their contact shadow;
13
+ // `castShadow` draws them into it as a dynamic caster. The instance field `state` is not read by any shader.
14
+ import { RIG_ATTRIBS, RIG_INSTANCE_FLOATS, RIG_INSTANCE_LAYOUT, rigExtended } from '../base/rig.js';
15
+ import { rigMesh, rigShadowMesh } from '../gen-mesh/rig-mesh.js';
16
+ import { SHADOW_MAP_UNIT } from './renderer.js';
17
+ import { DEPTH_FS } from './shadow-glsl.js';
18
+ const INPUTS = /* glsl */ `
19
+ in vec3 a_pos;
20
+ in vec3 a_normal;
21
+ in vec3 a_pivot;
22
+ in vec4 a_motion; // kind, amount, segment, colour slot (−1 fixed colour)
23
+ in vec4 a_color; // rgb, finish
24
+ in vec2 a_extra; // gait phase, variant bit (−1 always)
25
+ in vec4 i_p0;
26
+ in vec4 i_dir;
27
+ in vec4 i_p1;
28
+ in vec4 i_move; // distance m, speed m/s, turn rate rad/s, seed
29
+ in vec4 i_look; // state, height scale, width scale, variant bits
30
+ in vec4 i_colors;
31
+ uniform vec4 u_rig; // x stride m (0: rolls instead of walking), y steering base m, z roll gain, w roll limit rad
32
+ uniform vec4 u_rig2; // x bob m, y animate 0–1, z lamps 0–1, w leg length m
33
+ const float TAU = 6.2831853;
34
+ mat3 rotX(float a) { float c = cos(a), s = sin(a); return mat3(1.0, 0.0, 0.0, 0.0, c, s, 0.0, -s, c); }
35
+ mat3 rotY(float a) { float c = cos(a), s = sin(a); return mat3(c, 0.0, -s, 0.0, 1.0, 0.0, s, 0.0, c); }
36
+ mat3 rotZ(float a) { float c = cos(a), s = sin(a); return mat3(c, s, 0.0, -s, c, 0.0, 0.0, 0.0, 1.0); }
37
+ `;
38
+ /** Places a segment-local point (already posed and scaled) in the world on the segment's anchor and heading. */
39
+ const FRAME = /* glsl */ `
40
+ vec3 toWorld(vec3 q, float seg, out vec3 fwd, out vec3 right) {
41
+ vec4 p = seg < 0.5 ? i_p0 : i_p1;
42
+ vec2 d = seg < 0.5 ? i_dir.xy : i_dir.zw;
43
+ #ifdef WH_LOCAL_UP
44
+ // Up is radial: the heading (x, z) is meant in the local frame of the anchor, and heights go along its up.
45
+ mat3 lf = localFrame(p.xyz);
46
+ fwd = lf * vec3(d.x, 0.0, d.y);
47
+ right = lf * vec3(-d.y, 0.0, d.x);
48
+ return p.xyz + right * q.x + lf[1] * (q.y + q.z * p.w) + fwd * q.z;
49
+ #else
50
+ fwd = vec3(d.x, 0.0, d.y);
51
+ right = vec3(-d.y, 0.0, d.x);
52
+ return p.xyz + right * q.x + vec3(0.0, q.y + q.z * p.w, 0.0) + fwd * q.z;
53
+ #endif
54
+ }
55
+ `;
56
+ /**
57
+ * Vertex include of rig posing (registered as `<rig-pose>` by `RigLayer`; write it after `<header>` and
58
+ * `<noise>`): the part attributes and instance record (`RIG_ATTRIBS`), the uniforms `RigLayer` sets (`u_rig`,
59
+ * `u_rig2`, with `WH_RIG_EXT` also `u_rig3`) and two functions. `bool rigPose(out vec3 pos, out vec3 nrm)`: the
60
+ * world position and unit normal of the vertex in the pose of its instance; false when the instance lacks the
61
+ * part's variant (collapse the vertex). `vec3 rigColor()`: the part's colour times its instance colour slot.
62
+ */
63
+ export const RIG_POSE_GLSL = /* glsl */ `${INPUTS}
64
+ ${FRAME}
65
+ #ifdef WH_RIG_EXT
66
+ uniform vec4 u_rig3; // x gait (0 pair, 1 free), y dip m, z w the speeds the gait fades in between (m/s at scale 1)
67
+ #endif
68
+ vec3 unpackRgb(float c) {
69
+ float r = floor(c / 65536.0);
70
+ float g = floor((c - r * 65536.0) / 256.0);
71
+ return vec3(r, g, c - r * 65536.0 - g * 256.0) / 255.0;
72
+ }
73
+ bool rigPose(out vec3 pos, out vec3 nrm) {
74
+ // A variant part the instance does not have collapses outside the clip volume.
75
+ if (a_extra.y >= 0.0 && mod(floor(i_look.w / exp2(a_extra.y)), 2.0) < 0.5) {
76
+ return false;
77
+ }
78
+ float kind = a_motion.x;
79
+ float amount = a_motion.y;
80
+ float anim = u_rig2.y;
81
+ float dist = i_move.x;
82
+ float speed = i_move.y;
83
+ float seed = i_move.w;
84
+ float t = u_time.x;
85
+ vec3 q = a_pos - a_pivot;
86
+ vec3 n = a_normal;
87
+ mat3 r = mat3(1.0);
88
+ float dy = 0.0;
89
+ float roll = 0.0;
90
+ bool sprung = true;
91
+ if (u_rig.x > 0.0) {
92
+ // Gait. Phase in cycles from the distance travelled, so the stance foot moves exactly backward at the speed
93
+ // of travel and stays still on the ground; the swing foot comes forward on a smooth step.
94
+ float walk = smoothstep(0.15, 0.9, speed) * anim;
95
+ float phase = dist / (u_rig.x * i_look.y) + seed;
96
+ float legL = u_rig2.w;
97
+ // The leg on the ground: its foot offset falls linearly from +1/4 to −1/4 of the stride.
98
+ float us = (0.25 - 0.5 * fract(2.0 * phase)) * u_rig.x * walk;
99
+ dy = legL * (cos(asin(clamp(us / legL, -0.9, 0.9))) - 1.0);
100
+ roll = 0.035 * sin(TAU * phase) * walk + 0.02 * sin(t * 0.8 + seed * TAU) * (1.0 - walk) * anim;
101
+ float f = fract(phase + a_extra.x);
102
+ float u = f < 0.5 ? 0.25 - f : -0.25 + 0.5 * smoothstep(0.0, 1.0, (f - 0.5) * 2.0);
103
+ if (kind == 3.0) {
104
+ float lift = f < 0.5 ? 0.0 : sin(3.14159 * (f - 0.5) * 2.0);
105
+ q.y *= 1.0 - 0.22 * lift * walk;
106
+ r = rotX(-asin(clamp(u * u_rig.x * walk / amount, -0.9, 0.9)));
107
+ } else if (kind == 4.0) {
108
+ r = rotX(-amount * 4.0 * u * walk);
109
+ }
110
+ } else {
111
+ // Rolling: wheels spin with the distance, the steering pair turns toward the curve, the body leans out of it
112
+ // and bobs a little on the surface.
113
+ float k = i_move.z / max(speed, 0.5);
114
+ roll = clamp(u_rig.z * speed * i_move.z, -u_rig.w, u_rig.w) * anim;
115
+ dy = u_rig2.x * (vnoise(vec2(dist * 0.9, seed * 97.0), 7u) - 0.5) * 2.0 * min(speed / 4.0, 1.0) * anim;
116
+ if (kind == 1.0 || kind == 2.0) {
117
+ sprung = false;
118
+ r = rotX(dist / amount * anim);
119
+ if (kind == 2.0) r = rotY(clamp(atan(u_rig.y * k), -0.6, 0.6) * anim) * r;
120
+ }
121
+ }
122
+ #ifdef WH_RIG_EXT
123
+ // Extended pose. g: the gait phase in cycles (time when the rig rolls); walkX: how much it walks, 0–1.
124
+ float g = u_rig.x > 0.0 ? dist / (u_rig.x * i_look.y) + seed : 0.5 * t + seed;
125
+ float walkX = u_rig.x > 0.0 ? smoothstep(0.15, 0.9, speed) * anim : 0.0;
126
+ if (u_rig.x > 0.0 && u_rig3.x > 0.5) {
127
+ // Free gait: every limb plants its own end point. Stance (f < 0.5): the end point lies on the ground and moves
128
+ // backward at the speed of travel; swing: it comes forward on a smooth step, lifted. The limb is aimed at it and
129
+ // stretched along its own length. The body dips by u_rig3.y twice per cycle and keeps the sway.
130
+ walkX = smoothstep(u_rig3.z * i_look.y, u_rig3.w * i_look.y, speed) * anim;
131
+ dy = -u_rig3.y * walkX * (0.5 + 0.5 * cos(2.0 * TAU * g));
132
+ roll = 0.035 * sin(TAU * g) * walkX + 0.02 * sin(t * 0.8 + seed * TAU) * (1.0 - walkX) * anim;
133
+ q = a_pos - a_pivot;
134
+ r = mat3(1.0);
135
+ float fx = fract(g + a_extra.x);
136
+ float ux = fx < 0.5 ? 0.25 - fx : -0.25 + 0.5 * smoothstep(0.0, 1.0, (fx - 0.5) * 2.0);
137
+ if (kind == 3.0) {
138
+ float lift = fx < 0.5 ? 0.0 : sin(3.14159 * (fx - 0.5) * 2.0);
139
+ // From the pivot (lowered by the dip) to the end point; at rest straight down by the limb's length.
140
+ vec2 e = vec2(ux * u_rig.x * walkX, mix(-amount, 0.22 * amount * lift - (a_pivot.y + dy), walkX));
141
+ q.y *= length(e) / amount;
142
+ r = rotX(-atan(e.x, -e.y));
143
+ } else if (kind == 4.0) {
144
+ r = rotX(-amount * 4.0 * ux * walkX);
145
+ }
146
+ }
147
+ if (kind > 6.5) {
148
+ // A link of a chain (kind 7, 8, 9: about x, y, z): turns about the root by amount × its distance from the root ×
149
+ // a wave that lags along the chain; zero at the root, so the chain never leaves the body and links stay joined.
150
+ float dr = length(a_pos - a_pivot);
151
+ float wave = walkX * sin(TAU * (g - a_extra.x * dr)) + (1.0 - walkX) * 0.4 * sin(TAU * (0.15 * t + seed - a_extra.x * dr)) * anim;
152
+ float th = amount * dr * wave;
153
+ r = kind < 7.5 ? rotX(th) : kind < 8.5 ? rotY(th) : rotZ(th);
154
+ }
155
+ #endif
156
+ if (kind == 5.0) {
157
+ float still = 1.0 - smoothstep(0.1, 0.6, speed);
158
+ r = rotY(amount * sin(t * 0.45 + seed * 40.0) * still * anim);
159
+ } else if (kind == 6.0) {
160
+ float wa = (vnoise(vec2(t * 1.7 + seed * 50.0, dist * 0.6), 3u) - 0.5) * 2.0;
161
+ float wb = (vnoise(vec2(t * 1.3 + seed * 20.0, dist * 0.4), 5u) - 0.5) * 2.0;
162
+ r = rotY(amount * wb * anim) * rotX(amount * wa * anim);
163
+ }
164
+ q = r * q + a_pivot;
165
+ n = r * n;
166
+ if (sprung) {
167
+ // Roll about a point half a metre above the ground.
168
+ vec3 c = vec3(0.0, 0.5, 0.0);
169
+ q = rotZ(roll) * (q - c) + c;
170
+ n = rotZ(roll) * n;
171
+ q.y += dy;
172
+ }
173
+ q *= vec3(i_look.y * i_look.z, i_look.y, i_look.y);
174
+ vec3 fwd, right;
175
+ pos = toWorld(q, a_motion.z, fwd, right);
176
+ #ifdef WH_LOCAL_UP
177
+ nrm = normalize(right * n.x + cross(right, fwd) * n.y + fwd * n.z);
178
+ #else
179
+ nrm = normalize(right * n.x + vec3(0.0, n.y, 0.0) + fwd * n.z);
180
+ #endif
181
+ return true;
182
+ }
183
+ vec3 rigColor() {
184
+ vec3 c = a_color.rgb;
185
+ if (a_motion.w >= 0.0) {
186
+ int s = int(a_motion.w + 0.5);
187
+ c *= unpackRgb(s == 0 ? i_colors.x : s == 1 ? i_colors.y : s == 2 ? i_colors.z : i_colors.w);
188
+ }
189
+ return c;
190
+ }
191
+ `;
192
+ const BODY_VS = /* glsl */ `#include <header>
193
+ #include <noise>
194
+ ${RIG_POSE_GLSL}
195
+ out vec3 v_world;
196
+ out vec3 v_normal;
197
+ out vec3 v_color;
198
+ out float v_finish;
199
+ void main() {
200
+ vec3 pos, nrm;
201
+ if (!rigPose(pos, nrm)) {
202
+ gl_Position = vec4(2.0, 2.0, 2.0, 1.0);
203
+ return;
204
+ }
205
+ v_world = pos;
206
+ v_normal = nrm;
207
+ v_color = rigColor();
208
+ v_finish = a_color.a;
209
+ gl_Position = u_viewProj * vec4(v_world, 1.0);
210
+ }`;
211
+ /** Depth of posed bodies for the shadow map (`RigLayerOptions.castShadow` without a shadow pass of the game). */
212
+ const DEPTH_POSE_VS = /* glsl */ `#include <header>
213
+ #include <noise>
214
+ ${RIG_POSE_GLSL}
215
+ uniform mat4 u_lightRender;
216
+ void main() {
217
+ vec3 pos, nrm;
218
+ if (!rigPose(pos, nrm)) {
219
+ gl_Position = vec4(2.0, 2.0, 2.0, 1.0);
220
+ return;
221
+ }
222
+ gl_Position = u_lightRender * vec4(pos, 1.0);
223
+ }`;
224
+ const BODY_FS = /* glsl */ `#include <header>
225
+ #include <noise>
226
+ #include <lighting>
227
+ uniform vec4 u_rig2;
228
+ in vec3 v_world;
229
+ in vec3 v_normal;
230
+ in vec3 v_color;
231
+ in float v_finish;
232
+ out vec4 o_color;
233
+ void main() {
234
+ vec3 n = normalize(v_normal);
235
+ float sun = sunVisibility(v_world, n, 0.6);
236
+ #ifdef WH_LOCAL_UP
237
+ float nUp = dot(n, localUp(v_world));
238
+ #else
239
+ float nUp = n.y;
240
+ #endif
241
+ float lamps = u_rig2.z;
242
+ vec3 col = v_color;
243
+ int finish = int(v_finish + 0.5);
244
+ vec3 c;
245
+ if (finish == 1 || finish == 4) {
246
+ // Glass: dark, with a faint sky sheen on upward faces; a lit interior glows at night.
247
+ c = toDisplay(col * u_warmTint.rgb * lightOn(n, sun, 1.0) * 0.45 + u_skyFill.rgb * 0.18 * (0.4 + 0.6 * max(nUp, 0.0)));
248
+ if (finish == 4) c = mix(c, vec3(0.95, 0.86, 0.62), 0.75 * lamps);
249
+ } else {
250
+ if (finish == 5) {
251
+ // Paving: darker joints of a 0.5 m grid in world space, on upward faces.
252
+ vec2 g = abs(fract(v_world.xz * 2.0) - 0.5);
253
+ col *= 1.0 - 0.22 * smoothstep(0.43, 0.48, max(g.x, g.y)) * max(nUp, 0.0);
254
+ }
255
+ c = toDisplay(col * u_warmTint.rgb * lightOn(n, sun, 1.0));
256
+ if (finish == 2) c = mix(c, vec3(1.0, 0.96, 0.84) * 1.15, 0.2 + 0.8 * lamps);
257
+ else if (finish == 3) c = mix(c, vec3(0.95, 0.12, 0.08), 0.3 + 0.6 * lamps);
258
+ }
259
+ o_color = vec4(applyFog(c, v_world), 1.0);
260
+ }`;
261
+ const SHADOW_VS = /* glsl */ `#include <header>
262
+ ${INPUTS}
263
+ ${FRAME}
264
+ out vec2 v_local;
265
+ void main() {
266
+ v_local = a_pos.xz;
267
+ float s = i_look.y;
268
+ // Half size and forward offset come in the pivot; the quad leans a little away from a low sun.
269
+ vec3 q = vec3(a_pos.x * a_pivot.x * s * i_look.z, 0.04, a_pos.z * a_pivot.y * s + a_pivot.z * s);
270
+ vec3 fwd, right;
271
+ vec3 w = toWorld(q, a_motion.z, fwd, right);
272
+ #ifdef WH_LOCAL_UP
273
+ // The same lean along the local horizon.
274
+ vec3 up = localUp(w);
275
+ float high = dot(u_sunDir.xyz, up);
276
+ vec3 away = -(u_sunDir.xyz - up * high) / max(high, 0.35);
277
+ w += away * 0.15 * s * clamp(high * 4.0, 0.0, 1.0);
278
+ #else
279
+ vec2 away = -u_sunDir.xz / max(u_sunDir.y, 0.35);
280
+ w.xz += away * 0.15 * s * clamp(u_sunDir.y * 4.0, 0.0, 1.0);
281
+ #endif
282
+ gl_Position = u_viewProj * vec4(w, 1.0);
283
+ }`;
284
+ const SHADOW_FS = /* glsl */ `#include <header>
285
+ in vec2 v_local;
286
+ out vec4 o_color;
287
+ void main() {
288
+ vec2 a = abs(v_local);
289
+ float d = pow(pow(a.x, 4.0) + pow(a.y, 4.0), 0.25);
290
+ o_color = vec4(0.0, 0.0, 0.0, 0.42 * (1.0 - smoothstep(0.45, 1.0, d)));
291
+ }`;
292
+ // The device merges a partial state into the current one, so the body states every field it relies on.
293
+ const BODY_MATERIAL = {
294
+ name: 'rig',
295
+ attribs: RIG_ATTRIBS,
296
+ passes: { color: { vs: BODY_VS, fs: BODY_FS } },
297
+ state: { color: { depthTest: true, depthWrite: true, cullBack: true, blend: null, polygonOffset: null } },
298
+ };
299
+ const CONTACT_MATERIAL = {
300
+ name: 'rig-contact',
301
+ attribs: RIG_ATTRIBS,
302
+ passes: { color: { vs: SHADOW_VS, fs: SHADOW_FS } },
303
+ state: { color: { depthWrite: false, blend: { src: 'src-alpha', dst: 'one-minus-src-alpha' }, polygonOffset: [-1, -2] } },
304
+ };
305
+ const DEPTH_MATERIAL = {
306
+ name: 'rig-depth',
307
+ attribs: RIG_ATTRIBS,
308
+ passes: { shadow: { vs: DEPTH_POSE_VS, fs: DEPTH_FS } },
309
+ };
310
+ /** The same description with the extended pose (`WH_RIG_EXT` defined after the header line of every pass). */
311
+ function extended(desc) {
312
+ const passes = {};
313
+ for (const [pass, src] of Object.entries(desc.passes)) {
314
+ passes[pass] = src === undefined || src === 'depth' ? src : { vs: src.vs.replace('#include <header>\n', '#include <header>\n#define WH_RIG_EXT 1\n'), fs: src.fs };
315
+ }
316
+ return { ...desc, name: `${desc.name}-ext`, passes };
317
+ }
318
+ export class RigLayer {
319
+ /** Lamps and lit interiors, 0 by day to 1 at night; the app sets it from the light. */
320
+ lamps = 0;
321
+ /** Off draws nothing. */
322
+ enabled = true;
323
+ /** Counts of the last drawn frame, all kinds together. */
324
+ counts = { near: 0, far: 0, hidden: 0 };
325
+ /** `'dynamic'` with `castShadow`; unset otherwise (a layer that casts nothing, as before the option existed). */
326
+ shadowCasters;
327
+ kinds = [];
328
+ bodyDesc;
329
+ body;
330
+ contact;
331
+ /** The program of the extended pose, made by the first kind that needs it. */
332
+ bodyExt = null;
333
+ renderer;
334
+ castShadow;
335
+ contactShadows;
336
+ depth = null;
337
+ depthExt = null;
338
+ /** Records changed since the layer was last drawn into the shadow map. */
339
+ shadowChanged = false;
340
+ constructor(renderer, o = {}) {
341
+ this.renderer = renderer;
342
+ if (!renderer.includes.has('rig-pose'))
343
+ renderer.includes.set('rig-pose', RIG_POSE_GLSL);
344
+ this.bodyDesc = o.material ?? BODY_MATERIAL;
345
+ this.body = renderer.material(this.bodyDesc);
346
+ this.contact = renderer.material(CONTACT_MATERIAL);
347
+ this.castShadow = o.castShadow ?? false;
348
+ this.contactShadows = o.contactShadows ?? true;
349
+ if (this.castShadow) {
350
+ this.shadowCasters = 'dynamic';
351
+ this.depth = renderer.material(this.depthDesc());
352
+ }
353
+ }
354
+ /** Adds a kind of rig that can be drawn up to `capacity` times; returns its index for `set`. */
355
+ addKind(desc, capacity) {
356
+ const ext = rigExtended(desc);
357
+ if (ext && !this.bodyExt)
358
+ this.bodyExt = this.renderer.material(extended(this.bodyDesc));
359
+ if (ext && this.castShadow && !this.depthExt)
360
+ this.depthExt = this.renderer.material(extended(this.depthDesc()));
361
+ const bodyMaterial = ext ? this.bodyExt : this.body;
362
+ const lod = (far) => {
363
+ const data = new Float32Array(capacity * RIG_INSTANCE_FLOATS);
364
+ const buffer = this.renderer.createInstances({ layout: RIG_INSTANCE_LAYOUT, data });
365
+ return { data, buffer, body: this.renderer.createMesh(rigMesh(desc, far), bodyMaterial, buffer), contact: this.renderer.createMesh(rigShadowMesh(desc), this.contact, buffer), count: 0 };
366
+ };
367
+ const legs = desc.parts.find((p) => p.motion === 'limb');
368
+ const source = new Float32Array(capacity * RIG_INSTANCE_FLOATS);
369
+ let shade = null;
370
+ if (this.castShadow) {
371
+ const buffer = this.renderer.createInstances({ layout: RIG_INSTANCE_LAYOUT, data: source });
372
+ shade = { buffer, body: this.renderer.createMesh(rigMesh(desc, false), ext ? this.depthExt : this.depth, buffer) };
373
+ }
374
+ this.kinds.push({
375
+ desc,
376
+ capacity,
377
+ source,
378
+ count: 0,
379
+ near: lod(false),
380
+ far: lod(true),
381
+ shade,
382
+ rig: [desc.stride ?? 0, desc.steerBase ?? 0, desc.roll ?? 0, desc.rollLimit ?? 0],
383
+ rig2: [desc.bob ?? 0, 1, 0, legs?.amount ?? 1],
384
+ ext,
385
+ rig3: desc.gait === 'free' ? [1, desc.dip ?? 0, desc.walkSpeed?.[0] ?? 0.15, desc.walkSpeed?.[1] ?? 0.9] : [0, 0, 0.15, 0.9],
386
+ });
387
+ return this.kinds.length - 1;
388
+ }
389
+ /** Removes every kind and deletes its GPU objects. Kind indices returned by `addKind` are invalid afterwards. */
390
+ clear() {
391
+ const device = this.renderer.device;
392
+ for (const k of this.kinds) {
393
+ for (const lod of [k.near, k.far]) {
394
+ device.deleteMesh(lod.body);
395
+ device.deleteMesh(lod.contact);
396
+ device.deleteInstances(lod.buffer);
397
+ }
398
+ if (k.shade) {
399
+ device.deleteMesh(k.shade.body);
400
+ device.deleteInstances(k.shade.buffer);
401
+ }
402
+ }
403
+ this.kinds.length = 0;
404
+ this.shadowChanged = true;
405
+ }
406
+ /** The instance records of a kind for this frame (`RIG_INSTANCE_FLOATS` each); the layer culls them when drawing. */
407
+ set(kind, data, count) {
408
+ const k = this.kinds[kind];
409
+ const n = Math.min(count, k.capacity);
410
+ k.source.set(data.subarray(0, n * RIG_INSTANCE_FLOATS));
411
+ k.count = n;
412
+ this.shadowChanged = true;
413
+ }
414
+ /** The buffer `set` copies from, to fill in place: write records, then call `commit`. */
415
+ staging(kind) {
416
+ return this.kinds[kind].source;
417
+ }
418
+ commit(kind, count) {
419
+ const k = this.kinds[kind];
420
+ k.count = Math.min(count, k.capacity);
421
+ this.shadowChanged = true;
422
+ }
423
+ /**
424
+ * With `castShadow`: true when records were set or committed since the layer was last drawn into the shadow map.
425
+ * The pose also moves with the frame time; set the records every frame for moving shadows.
426
+ */
427
+ shadowDirty() {
428
+ return this.castShadow && this.shadowChanged;
429
+ }
430
+ draw(ctx) {
431
+ if (!this.enabled)
432
+ return;
433
+ if (ctx.pass === 'shadow') {
434
+ if (this.castShadow)
435
+ this.drawShadow(ctx);
436
+ return;
437
+ }
438
+ if (ctx.pass !== 'color')
439
+ return;
440
+ this.split(ctx);
441
+ const device = ctx.device;
442
+ // Bodies first (opaque, writing depth): the plain kinds, then those of the extended pose; then the blended contact
443
+ // shadows of every kind, which a body in front hides.
444
+ const passes = [[this.body, false, false]];
445
+ if (this.bodyExt && this.kinds.some((k) => k.ext))
446
+ passes.push([this.bodyExt, false, true]);
447
+ if (this.contactShadows)
448
+ passes.push([this.contact, true, null]);
449
+ for (const [material, contact, ext] of passes) {
450
+ const program = material.program('color');
451
+ device.setState(material.state.color ?? {});
452
+ device.use(program);
453
+ if (!contact)
454
+ program.int('u_shadowMap', SHADOW_MAP_UNIT);
455
+ for (const k of this.kinds) {
456
+ if (ext !== null && k.ext !== ext)
457
+ continue;
458
+ for (const [lod, animate] of [[k.near, 1], [k.far, 0]]) {
459
+ if (lod.count === 0)
460
+ continue;
461
+ const mesh = contact ? lod.contact : lod.body;
462
+ program.set('u_rig', { vec4: k.rig });
463
+ program.set('u_rig2', { vec4: [k.rig2[0], animate, this.lamps, k.rig2[3]] });
464
+ if (ext)
465
+ program.set('u_rig3', { vec4: k.rig3 });
466
+ mesh.instances = lod.count;
467
+ device.draw(mesh);
468
+ ctx.count(mesh.triangles * lod.count);
469
+ }
470
+ }
471
+ }
472
+ }
473
+ /** The shadow pass: every instance of every kind, posed at near detail. */
474
+ drawShadow(ctx) {
475
+ this.shadowChanged = false;
476
+ const device = ctx.device;
477
+ for (const [material, ext] of [[this.depth, false], [this.depthExt, true]]) {
478
+ if (!material)
479
+ continue;
480
+ const program = material.program('shadow');
481
+ if (!program)
482
+ continue;
483
+ let used = false;
484
+ for (const k of this.kinds) {
485
+ if (k.ext !== ext || !k.shade || k.count === 0)
486
+ continue;
487
+ if (!used) {
488
+ const state = material.state.shadow;
489
+ if (state)
490
+ device.setState(state);
491
+ device.use(program);
492
+ program.mat4('u_lightRender', ctx.lightRender);
493
+ used = true;
494
+ }
495
+ device.writeBuffer(k.shade.buffer.buffer, k.source, k.count * RIG_INSTANCE_FLOATS);
496
+ program.set('u_rig', { vec4: k.rig });
497
+ program.set('u_rig2', { vec4: [k.rig2[0], 1, this.lamps, k.rig2[3]] });
498
+ if (ext)
499
+ program.set('u_rig3', { vec4: k.rig3 });
500
+ k.shade.body.instances = k.count;
501
+ device.draw(k.shade.body);
502
+ ctx.count(k.shade.body.triangles * k.count);
503
+ }
504
+ }
505
+ }
506
+ /** The material of the shadow pass: the game's when it has a `shadow` pass, else the engine's depth of the pose. */
507
+ depthDesc() {
508
+ const own = this.bodyDesc.passes.shadow;
509
+ if (own !== undefined && this.bodyDesc !== BODY_MATERIAL)
510
+ return { ...this.bodyDesc, name: `${this.bodyDesc.name}-shadow`, passes: { shadow: own } };
511
+ return DEPTH_MATERIAL;
512
+ }
513
+ /** Culls every kind against the view and sorts the rest into the near and far buffers, then uploads them. */
514
+ split(ctx) {
515
+ const view = ctx.view;
516
+ const volume = view.cullVolume();
517
+ const ax = view.axes();
518
+ const f = ax.forward;
519
+ const counts = this.counts;
520
+ counts.near = counts.far = counts.hidden = 0;
521
+ const S = RIG_INSTANCE_FLOATS;
522
+ // With radial up a rig stands away from this point; null: along +y.
523
+ const upC = this.renderer.upCenter();
524
+ for (const k of this.kinds) {
525
+ const d = k.desc;
526
+ const farPx = d.farPx ?? 24;
527
+ const hidePx = d.hidePx ?? 3;
528
+ let nn = 0;
529
+ let nf = 0;
530
+ for (let i = 0; i < k.count; i++) {
531
+ const o = i * S;
532
+ const x = k.source[o], y = k.source[o + 1], z = k.source[o + 2];
533
+ const scale = k.source[o + 17];
534
+ const reach = d.radius * scale;
535
+ const half = d.height * 0.5 * scale;
536
+ if (upC) {
537
+ const ux = x - upC[0], uy = y - upC[1], uz = z - upC[2];
538
+ const lift = half / (Math.sqrt(ux * ux + uy * uy + uz * uz) || 1);
539
+ if (!inside(volume, x + ux * lift, y + uy * lift, z + uz * lift, reach))
540
+ continue;
541
+ }
542
+ else if (!inside(volume, x, y + half, z, reach))
543
+ continue;
544
+ const mpp = Math.max(1e-4, view.metresPerPixel + view.pixelGrowth * (f[0] * x + f[1] * y + f[2] * z - view.refDepth));
545
+ const px = (d.height * scale) / mpp / view.dpr;
546
+ if (px < hidePx) {
547
+ counts.hidden++;
548
+ continue;
549
+ }
550
+ const lod = px < farPx ? k.far : k.near;
551
+ const at = (lod === k.far ? nf++ : nn++) * S;
552
+ lod.data.set(k.source.subarray(o, o + S), at);
553
+ }
554
+ k.near.count = nn;
555
+ k.far.count = nf;
556
+ counts.near += nn;
557
+ counts.far += nf;
558
+ if (nn > 0)
559
+ ctx.device.writeBuffer(k.near.buffer.buffer, k.near.data, nn * S);
560
+ if (nf > 0)
561
+ ctx.device.writeBuffer(k.far.buffer.buffer, k.far.data, nf * S);
562
+ }
563
+ }
564
+ }
565
+ function inside(v, x, y, z, r) {
566
+ if (v.kind === 'frustum') {
567
+ for (const p of v.planes)
568
+ if (p[0] * x + p[1] * y + p[2] * z + p[3] < -r)
569
+ return false;
570
+ return true;
571
+ }
572
+ const a = v.axes;
573
+ const u = a.right[0] * x + a.right[1] * y + a.right[2] * z - v.cx;
574
+ const w = a.up[0] * x + a.up[1] * y + a.up[2] * z - v.cy;
575
+ return Math.abs(u) <= v.halfW + r && Math.abs(w) <= v.halfH + r;
576
+ }