wallhammer-web 0.0.0-stage → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (408) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +88 -2
  3. package/dist/base/data.d.ts +67 -0
  4. package/dist/base/data.js +10 -0
  5. package/dist/base/destructible.d.ts +167 -0
  6. package/dist/base/destructible.js +47 -0
  7. package/dist/base/env-slot.d.ts +101 -0
  8. package/dist/base/env-slot.js +8 -0
  9. package/dist/base/frames.d.ts +138 -0
  10. package/dist/base/frames.js +459 -0
  11. package/dist/base/index.d.ts +21 -0
  12. package/dist/base/index.js +11 -0
  13. package/dist/base/mat34.d.ts +46 -0
  14. package/dist/base/mat34.js +173 -0
  15. package/dist/base/material.d.ts +87 -0
  16. package/dist/base/material.js +6 -0
  17. package/dist/base/math.d.ts +27 -0
  18. package/dist/base/math.js +60 -0
  19. package/dist/base/progress.d.ts +38 -0
  20. package/dist/base/progress.js +81 -0
  21. package/dist/base/rig.d.ts +177 -0
  22. package/dist/base/rig.js +97 -0
  23. package/dist/base/trig.d.ts +29 -0
  24. package/dist/base/trig.js +169 -0
  25. package/dist/base/up.d.ts +26 -0
  26. package/dist/base/up.js +40 -0
  27. package/dist/base/view.d.ts +128 -0
  28. package/dist/base/view.js +136 -0
  29. package/dist/camera/camera.d.ts +99 -0
  30. package/dist/camera/camera.js +228 -0
  31. package/dist/camera/follow.d.ts +120 -0
  32. package/dist/camera/follow.js +320 -0
  33. package/dist/camera/free.d.ts +153 -0
  34. package/dist/camera/free.js +359 -0
  35. package/dist/camera/index.d.ts +10 -0
  36. package/dist/camera/index.js +6 -0
  37. package/dist/camera/orbit.d.ts +93 -0
  38. package/dist/camera/orbit.js +261 -0
  39. package/dist/camera/path.d.ts +48 -0
  40. package/dist/camera/path.js +179 -0
  41. package/dist/core/ballistic.d.ts +66 -0
  42. package/dist/core/ballistic.js +104 -0
  43. package/dist/core/bodies.d.ts +77 -0
  44. package/dist/core/bodies.js +125 -0
  45. package/dist/core/clock.d.ts +37 -0
  46. package/dist/core/clock.js +87 -0
  47. package/dist/core/cover-fade.d.ts +14 -0
  48. package/dist/core/cover-fade.js +38 -0
  49. package/dist/core/index.d.ts +13 -0
  50. package/dist/core/index.js +8 -0
  51. package/dist/core/kepler.d.ts +74 -0
  52. package/dist/core/kepler.js +108 -0
  53. package/dist/core/level-fade.d.ts +66 -0
  54. package/dist/core/level-fade.js +123 -0
  55. package/dist/core/palette.d.ts +15 -0
  56. package/dist/core/palette.js +47 -0
  57. package/dist/env/atmosphere.d.ts +129 -0
  58. package/dist/env/atmosphere.js +707 -0
  59. package/dist/env/cloud-field.d.ts +44 -0
  60. package/dist/env/cloud-field.js +111 -0
  61. package/dist/env/environment.d.ts +217 -0
  62. package/dist/env/environment.js +441 -0
  63. package/dist/env/frame-block.d.ts +29 -0
  64. package/dist/env/frame-block.js +152 -0
  65. package/dist/env/glsl.d.ts +35 -0
  66. package/dist/env/glsl.js +246 -0
  67. package/dist/env/index.d.ts +13 -0
  68. package/dist/env/index.js +9 -0
  69. package/dist/env/sky.d.ts +48 -0
  70. package/dist/env/sky.js +227 -0
  71. package/dist/env/sun.d.ts +31 -0
  72. package/dist/env/sun.js +42 -0
  73. package/dist/gen-arch/build.d.ts +17 -0
  74. package/dist/gen-arch/build.js +414 -0
  75. package/dist/gen-arch/damage/damaged.d.ts +44 -0
  76. package/dist/gen-arch/damage/damaged.js +1119 -0
  77. package/dist/gen-arch/damage/fall-material.d.ts +10 -0
  78. package/dist/gen-arch/damage/fall-material.js +47 -0
  79. package/dist/gen-arch/damage/fall.d.ts +78 -0
  80. package/dist/gen-arch/damage/fall.js +797 -0
  81. package/dist/gen-arch/damage/rubble.d.ts +22 -0
  82. package/dist/gen-arch/damage/rubble.js +188 -0
  83. package/dist/gen-arch/index.d.ts +25 -0
  84. package/dist/gen-arch/index.js +16 -0
  85. package/dist/gen-arch/interior/interior-plan.d.ts +114 -0
  86. package/dist/gen-arch/interior/interior-plan.js +615 -0
  87. package/dist/gen-arch/interior/interior.d.ts +97 -0
  88. package/dist/gen-arch/interior/interior.js +966 -0
  89. package/dist/gen-arch/structure/building-shader.d.ts +25 -0
  90. package/dist/gen-arch/structure/building-shader.js +946 -0
  91. package/dist/gen-arch/structure/buildings.d.ts +248 -0
  92. package/dist/gen-arch/structure/buildings.js +294 -0
  93. package/dist/gen-arch/structure/dims.d.ts +77 -0
  94. package/dist/gen-arch/structure/dims.js +39 -0
  95. package/dist/gen-arch/structure/distant.d.ts +62 -0
  96. package/dist/gen-arch/structure/distant.js +316 -0
  97. package/dist/gen-arch/structure/env-slots.d.ts +18 -0
  98. package/dist/gen-arch/structure/env-slots.js +47 -0
  99. package/dist/gen-arch/structure/facade.d.ts +193 -0
  100. package/dist/gen-arch/structure/facade.js +159 -0
  101. package/dist/gen-arch/structure/parts.d.ts +31 -0
  102. package/dist/gen-arch/structure/parts.js +69 -0
  103. package/dist/gen-arch/structure/roof.d.ts +76 -0
  104. package/dist/gen-arch/structure/roof.js +568 -0
  105. package/dist/gen-arch/structure/structure-material.d.ts +23 -0
  106. package/dist/gen-arch/structure/structure-material.js +55 -0
  107. package/dist/gen-arch/structure/writer.d.ts +94 -0
  108. package/dist/gen-arch/structure/writer.js +195 -0
  109. package/dist/gen-core/cell.d.ts +138 -0
  110. package/dist/gen-core/cell.js +150 -0
  111. package/dist/gen-core/cube-sphere.d.ts +57 -0
  112. package/dist/gen-core/cube-sphere.js +159 -0
  113. package/dist/gen-core/field.d.ts +55 -0
  114. package/dist/gen-core/field.js +154 -0
  115. package/dist/gen-core/geometry.d.ts +31 -0
  116. package/dist/gen-core/geometry.js +231 -0
  117. package/dist/gen-core/hash.d.ts +18 -0
  118. package/dist/gen-core/hash.js +58 -0
  119. package/dist/gen-core/height-field.d.ts +33 -0
  120. package/dist/gen-core/height-field.js +137 -0
  121. package/dist/gen-core/index.d.ts +22 -0
  122. package/dist/gen-core/index.js +17 -0
  123. package/dist/gen-core/instance-cells.d.ts +37 -0
  124. package/dist/gen-core/instance-cells.js +72 -0
  125. package/dist/gen-core/near-scatter.d.ts +56 -0
  126. package/dist/gen-core/near-scatter.js +120 -0
  127. package/dist/gen-core/noise.d.ts +23 -0
  128. package/dist/gen-core/noise.js +72 -0
  129. package/dist/gen-core/occupancy.d.ts +21 -0
  130. package/dist/gen-core/occupancy.js +104 -0
  131. package/dist/gen-core/sphere-patch.d.ts +138 -0
  132. package/dist/gen-core/sphere-patch.js +544 -0
  133. package/dist/gen-core/strip-surface.d.ts +34 -0
  134. package/dist/gen-core/strip-surface.js +246 -0
  135. package/dist/gen-core/strip.d.ts +102 -0
  136. package/dist/gen-core/strip.js +817 -0
  137. package/dist/gen-ground/env-slots.d.ts +48 -0
  138. package/dist/gen-ground/env-slots.js +78 -0
  139. package/dist/gen-ground/ground.d.ts +78 -0
  140. package/dist/gen-ground/ground.js +208 -0
  141. package/dist/gen-ground/index.d.ts +9 -0
  142. package/dist/gen-ground/index.js +7 -0
  143. package/dist/gen-ground/markings.d.ts +46 -0
  144. package/dist/gen-ground/markings.js +124 -0
  145. package/dist/gen-ground/materials.d.ts +18 -0
  146. package/dist/gen-ground/materials.js +22 -0
  147. package/dist/gen-ground/terrain-shader.d.ts +5 -0
  148. package/dist/gen-ground/terrain-shader.js +151 -0
  149. package/dist/gen-mesh/bare-shapes.d.ts +194 -0
  150. package/dist/gen-mesh/bare-shapes.js +596 -0
  151. package/dist/gen-mesh/bisect-surface.d.ts +47 -0
  152. package/dist/gen-mesh/bisect-surface.js +261 -0
  153. package/dist/gen-mesh/box.d.ts +18 -0
  154. package/dist/gen-mesh/box.js +2 -0
  155. package/dist/gen-mesh/cell-morph-mesh.d.ts +52 -0
  156. package/dist/gen-mesh/cell-morph-mesh.js +258 -0
  157. package/dist/gen-mesh/cell-surface-mesh.d.ts +32 -0
  158. package/dist/gen-mesh/cell-surface-mesh.js +140 -0
  159. package/dist/gen-mesh/chunk-mesh.d.ts +25 -0
  160. package/dist/gen-mesh/chunk-mesh.js +112 -0
  161. package/dist/gen-mesh/drape-mesh.d.ts +36 -0
  162. package/dist/gen-mesh/drape-mesh.js +98 -0
  163. package/dist/gen-mesh/height-field-mesh.d.ts +14 -0
  164. package/dist/gen-mesh/height-field-mesh.js +98 -0
  165. package/dist/gen-mesh/index.d.ts +26 -0
  166. package/dist/gen-mesh/index.js +15 -0
  167. package/dist/gen-mesh/line-mesh.d.ts +25 -0
  168. package/dist/gen-mesh/line-mesh.js +62 -0
  169. package/dist/gen-mesh/mesh-builder.d.ts +115 -0
  170. package/dist/gen-mesh/mesh-builder.js +315 -0
  171. package/dist/gen-mesh/packed-mesh.d.ts +53 -0
  172. package/dist/gen-mesh/packed-mesh.js +205 -0
  173. package/dist/gen-mesh/prism-mesh.d.ts +33 -0
  174. package/dist/gen-mesh/prism-mesh.js +176 -0
  175. package/dist/gen-mesh/rig-mesh.d.ts +9 -0
  176. package/dist/gen-mesh/rig-mesh.js +135 -0
  177. package/dist/gen-mesh/strip-mesh.d.ts +45 -0
  178. package/dist/gen-mesh/strip-mesh.js +258 -0
  179. package/dist/gen-plan/drain.d.ts +58 -0
  180. package/dist/gen-plan/drain.js +270 -0
  181. package/dist/gen-plan/grid.d.ts +37 -0
  182. package/dist/gen-plan/grid.js +182 -0
  183. package/dist/gen-plan/horizon.d.ts +50 -0
  184. package/dist/gen-plan/horizon.js +185 -0
  185. package/dist/gen-plan/index.d.ts +15 -0
  186. package/dist/gen-plan/index.js +13 -0
  187. package/dist/gen-plan/line-tiles.d.ts +35 -0
  188. package/dist/gen-plan/line-tiles.js +190 -0
  189. package/dist/gen-plan/lines.d.ts +31 -0
  190. package/dist/gen-plan/lines.js +183 -0
  191. package/dist/gen-plan/plots.d.ts +40 -0
  192. package/dist/gen-plan/plots.js +282 -0
  193. package/dist/gen-plan/route.d.ts +55 -0
  194. package/dist/gen-plan/route.js +297 -0
  195. package/dist/gen-plan/sites.d.ts +32 -0
  196. package/dist/gen-plan/sites.js +209 -0
  197. package/dist/gen-tex/code-stamp.d.ts +31 -0
  198. package/dist/gen-tex/code-stamp.js +137 -0
  199. package/dist/gen-tex/index.d.ts +7 -0
  200. package/dist/gen-tex/index.js +5 -0
  201. package/dist/gen-tex/light-horizon.d.ts +47 -0
  202. package/dist/gen-tex/light-horizon.js +180 -0
  203. package/dist/gen-tex/textures.d.ts +8 -0
  204. package/dist/gen-tex/textures.js +49 -0
  205. package/dist/gen-tex/value-textures.d.ts +46 -0
  206. package/dist/gen-tex/value-textures.js +102 -0
  207. package/dist/gen-vegetation/env-slots.d.ts +20 -0
  208. package/dist/gen-vegetation/env-slots.js +29 -0
  209. package/dist/gen-vegetation/index.d.ts +8 -0
  210. package/dist/gen-vegetation/index.js +7 -0
  211. package/dist/gen-vegetation/materials.d.ts +7 -0
  212. package/dist/gen-vegetation/materials.js +17 -0
  213. package/dist/gen-vegetation/planting.d.ts +170 -0
  214. package/dist/gen-vegetation/planting.js +363 -0
  215. package/dist/gen-vegetation/tree-shader.d.ts +23 -0
  216. package/dist/gen-vegetation/tree-shader.js +229 -0
  217. package/dist/gen-vegetation/trees.d.ts +59 -0
  218. package/dist/gen-vegetation/trees.js +329 -0
  219. package/dist/gpu/device.d.ts +286 -0
  220. package/dist/gpu/device.js +1157 -0
  221. package/dist/gpu/gl.d.ts +134 -0
  222. package/dist/gpu/gl.js +276 -0
  223. package/dist/gpu/index.d.ts +4 -0
  224. package/dist/gpu/index.js +3 -0
  225. package/dist/index.d.ts +11 -0
  226. package/dist/index.js +13 -0
  227. package/dist/motion/graph.d.ts +244 -0
  228. package/dist/motion/graph.js +739 -0
  229. package/dist/motion/index.d.ts +8 -0
  230. package/dist/motion/index.js +8 -0
  231. package/dist/motion/pose-ops.d.ts +30 -0
  232. package/dist/motion/pose-ops.js +232 -0
  233. package/dist/motion/reach.d.ts +45 -0
  234. package/dist/motion/reach.js +264 -0
  235. package/dist/motion/skeleton.d.ts +49 -0
  236. package/dist/motion/skeleton.js +99 -0
  237. package/dist/platform/byte-store.d.ts +21 -0
  238. package/dist/platform/byte-store.js +137 -0
  239. package/dist/platform/canvas.d.ts +29 -0
  240. package/dist/platform/canvas.js +76 -0
  241. package/dist/platform/context-loss.d.ts +8 -0
  242. package/dist/platform/context-loss.js +17 -0
  243. package/dist/platform/free-input.d.ts +51 -0
  244. package/dist/platform/free-input.js +100 -0
  245. package/dist/platform/index.d.ts +18 -0
  246. package/dist/platform/index.js +10 -0
  247. package/dist/platform/input.d.ts +48 -0
  248. package/dist/platform/input.js +117 -0
  249. package/dist/platform/keys.d.ts +12 -0
  250. package/dist/platform/keys.js +29 -0
  251. package/dist/platform/loop.d.ts +41 -0
  252. package/dist/platform/loop.js +84 -0
  253. package/dist/platform/walk-input.d.ts +74 -0
  254. package/dist/platform/walk-input.js +226 -0
  255. package/dist/platform/worker.d.ts +51 -0
  256. package/dist/platform/worker.js +170 -0
  257. package/dist/render/bone-instances.d.ts +24 -0
  258. package/dist/render/bone-instances.js +74 -0
  259. package/dist/render/cells.d.ts +70 -0
  260. package/dist/render/cells.js +144 -0
  261. package/dist/render/glsl.d.ts +66 -0
  262. package/dist/render/glsl.js +173 -0
  263. package/dist/render/index.d.ts +26 -0
  264. package/dist/render/index.js +16 -0
  265. package/dist/render/instance-sets.d.ts +97 -0
  266. package/dist/render/instance-sets.js +241 -0
  267. package/dist/render/near-instances.d.ts +75 -0
  268. package/dist/render/near-instances.js +219 -0
  269. package/dist/render/particles.d.ts +63 -0
  270. package/dist/render/particles.js +215 -0
  271. package/dist/render/pieces.d.ts +57 -0
  272. package/dist/render/pieces.js +151 -0
  273. package/dist/render/point-lights.d.ts +97 -0
  274. package/dist/render/point-lights.js +243 -0
  275. package/dist/render/renderer.d.ts +447 -0
  276. package/dist/render/renderer.js +1100 -0
  277. package/dist/render/rig.d.ts +74 -0
  278. package/dist/render/rig.js +576 -0
  279. package/dist/render/shadow-areas.d.ts +49 -0
  280. package/dist/render/shadow-areas.js +125 -0
  281. package/dist/render/shadow-glsl.d.ts +12 -0
  282. package/dist/render/shadow-glsl.js +146 -0
  283. package/dist/render/transient.d.ts +61 -0
  284. package/dist/render/transient.js +213 -0
  285. package/dist/render/value-glsl.d.ts +17 -0
  286. package/dist/render/value-glsl.js +208 -0
  287. package/dist/render/value-layers.d.ts +180 -0
  288. package/dist/render/value-layers.js +333 -0
  289. package/dist/runtime/atmosphere.d.ts +37 -0
  290. package/dist/runtime/atmosphere.js +304 -0
  291. package/dist/runtime/cloud-shell.d.ts +29 -0
  292. package/dist/runtime/cloud-shell.js +158 -0
  293. package/dist/runtime/engine.d.ts +155 -0
  294. package/dist/runtime/engine.js +305 -0
  295. package/dist/runtime/environment-link.d.ts +22 -0
  296. package/dist/runtime/environment-link.js +49 -0
  297. package/dist/runtime/index.d.ts +13 -0
  298. package/dist/runtime/index.js +12 -0
  299. package/dist/runtime/precipitation-glsl.d.ts +2 -0
  300. package/dist/runtime/precipitation-glsl.js +70 -0
  301. package/dist/runtime/precipitation.d.ts +11 -0
  302. package/dist/runtime/precipitation.js +78 -0
  303. package/dist/runtime/sky.d.ts +30 -0
  304. package/dist/runtime/sky.js +114 -0
  305. package/dist/runtime/streaming.d.ts +245 -0
  306. package/dist/runtime/streaming.js +602 -0
  307. package/dist/sim/agenda.d.ts +167 -0
  308. package/dist/sim/agenda.js +425 -0
  309. package/dist/sim/bytes.d.ts +34 -0
  310. package/dist/sim/bytes.js +125 -0
  311. package/dist/sim/destruction/apply.d.ts +59 -0
  312. package/dist/sim/destruction/apply.js +243 -0
  313. package/dist/sim/destruction/cell-states.d.ts +54 -0
  314. package/dist/sim/destruction/cell-states.js +198 -0
  315. package/dist/sim/destruction/index.d.ts +11 -0
  316. package/dist/sim/destruction/index.js +10 -0
  317. package/dist/sim/destruction/int-geometry.d.ts +17 -0
  318. package/dist/sim/destruction/int-geometry.js +131 -0
  319. package/dist/sim/destruction/pick.d.ts +13 -0
  320. package/dist/sim/destruction/pick.js +59 -0
  321. package/dist/sim/destruction/rubble.d.ts +8 -0
  322. package/dist/sim/destruction/rubble.js +159 -0
  323. package/dist/sim/destruction/state.d.ts +15 -0
  324. package/dist/sim/destruction/state.js +83 -0
  325. package/dist/sim/destruction/structure.d.ts +30 -0
  326. package/dist/sim/destruction/structure.js +139 -0
  327. package/dist/sim/events.d.ts +37 -0
  328. package/dist/sim/events.js +106 -0
  329. package/dist/sim/fixed.d.ts +21 -0
  330. package/dist/sim/fixed.js +64 -0
  331. package/dist/sim/flow-probe.d.ts +58 -0
  332. package/dist/sim/flow-probe.js +158 -0
  333. package/dist/sim/flow.d.ts +257 -0
  334. package/dist/sim/flow.js +967 -0
  335. package/dist/sim/index.d.ts +28 -0
  336. package/dist/sim/index.js +20 -0
  337. package/dist/sim/interlock.d.ts +47 -0
  338. package/dist/sim/interlock.js +252 -0
  339. package/dist/sim/movers.d.ts +228 -0
  340. package/dist/sim/movers.js +1083 -0
  341. package/dist/sim/nav.d.ts +72 -0
  342. package/dist/sim/nav.js +292 -0
  343. package/dist/sim/paths.d.ts +80 -0
  344. package/dist/sim/paths.js +197 -0
  345. package/dist/sim/regions.d.ts +87 -0
  346. package/dist/sim/regions.js +517 -0
  347. package/dist/sim/simulation.d.ts +114 -0
  348. package/dist/sim/simulation.js +291 -0
  349. package/dist/sim/states.d.ts +23 -0
  350. package/dist/sim/states.js +54 -0
  351. package/dist/sim/table.d.ts +49 -0
  352. package/dist/sim/table.js +126 -0
  353. package/dist/sim/zones.d.ts +11 -0
  354. package/dist/sim/zones.js +196 -0
  355. package/dist/tools/collision-lines.d.ts +12 -0
  356. package/dist/tools/collision-lines.js +50 -0
  357. package/dist/tools/data-view-glsl.d.ts +3 -0
  358. package/dist/tools/data-view-glsl.js +63 -0
  359. package/dist/tools/data-view-layers.d.ts +20 -0
  360. package/dist/tools/data-view-layers.js +60 -0
  361. package/dist/tools/debug-view.d.ts +30 -0
  362. package/dist/tools/debug-view.js +4 -0
  363. package/dist/tools/index.d.ts +10 -0
  364. package/dist/tools/index.js +7 -0
  365. package/dist/tools/mesh-checks.d.ts +67 -0
  366. package/dist/tools/mesh-checks.js +188 -0
  367. package/dist/tools/profile.d.ts +85 -0
  368. package/dist/tools/profile.js +173 -0
  369. package/dist/world/cell-cache.d.ts +32 -0
  370. package/dist/world/cell-cache.js +60 -0
  371. package/dist/world/cell-format.d.ts +25 -0
  372. package/dist/world/cell-format.js +232 -0
  373. package/dist/world/collide.d.ts +192 -0
  374. package/dist/world/collide.js +822 -0
  375. package/dist/world/index.d.ts +16 -0
  376. package/dist/world/index.js +12 -0
  377. package/dist/world/mesh-solids.d.ts +48 -0
  378. package/dist/world/mesh-solids.js +169 -0
  379. package/dist/world/pick.d.ts +34 -0
  380. package/dist/world/pick.js +177 -0
  381. package/dist/world/providers.d.ts +66 -0
  382. package/dist/world/providers.js +83 -0
  383. package/dist/world/sphere.d.ts +69 -0
  384. package/dist/world/sphere.js +271 -0
  385. package/dist/world/streaming.d.ts +288 -0
  386. package/dist/world/streaming.js +801 -0
  387. package/dist/world/walker.d.ts +145 -0
  388. package/dist/world/walker.js +502 -0
  389. package/docs/architecture.md +111 -0
  390. package/docs/cameras.md +302 -0
  391. package/docs/concepts.md +123 -0
  392. package/docs/destruction.md +127 -0
  393. package/docs/frames.md +199 -0
  394. package/docs/generators.md +575 -0
  395. package/docs/getting-started.md +123 -0
  396. package/docs/headless.md +50 -0
  397. package/docs/motion.md +160 -0
  398. package/docs/planet.md +152 -0
  399. package/docs/recipes.md +521 -0
  400. package/docs/reference.md +364 -0
  401. package/docs/rendering.md +823 -0
  402. package/docs/rigs.md +203 -0
  403. package/docs/simulation.md +235 -0
  404. package/docs/streaming.md +590 -0
  405. package/docs/system.md +166 -0
  406. package/docs/tools.md +175 -0
  407. package/docs/walking.md +262 -0
  408. package/package.json +131 -3
@@ -0,0 +1,739 @@
1
+ // The motion graph: states, transitions and layers over motion sources that the game writes. One description is
2
+ // compiled once (`compileMotionGraph`) and shared by any number of figures; every figure has its own `MotionGraph`
3
+ // with a small state (a Float64Array: parameters, triggers and a short list of active states per layer), so many
4
+ // figures update without allocating and a figure's state saves to bytes and back.
5
+ //
6
+ // The game steers a figure only through parameters (`setParam`) and one-shot triggers (`trigger`); it reads back
7
+ // which states are active and the events since the last call (marks crossed, states entered and left). The engine
8
+ // holds no motion of its own: every pose comes from a `MotionSource` of the game.
9
+ //
10
+ // Time and phase. Every active state has a time (seconds since it was entered, times its rate) and a phase (passes
11
+ // of its motions, a running number: 2.25 is a quarter into the third pass). The phase advances with time divided by
12
+ // the motions' duration, or follows a parameter (`drive`: phase = parameter / per, for motions that keep step with a
13
+ // distance travelled). The sources of one state share its phase, so motions of different length stay in step; a
14
+ // transition between two states of the same `sync` group carries the phase over.
15
+ //
16
+ // Transitions. A layer keeps up to `maxBlend` active states: the newest fades in over the previous result, which
17
+ // fades out. A transition that interrupts another (even straight back) adds one more state at weight 0, so the pose
18
+ // never jumps; when the list is full the state of the smallest weight is dropped (the only case of a step, as large
19
+ // as that weight).
20
+ import { addPose, blendPose } from './pose-ops.js';
21
+ import { Pose } from './skeleton.js';
22
+ const OPS = ['<', '<=', '>', '>=', '==', '!='];
23
+ /** Numbers per active entry: state, phase, time, progress, seconds, smooth. */
24
+ const ENTRY = 6;
25
+ const KIND_NAMES = ['mark', 'enter', 'leave'];
26
+ const FORMAT = 1;
27
+ /** A compiled description: shared by every figure that uses it. Holds the scratch poses of evaluation. */
28
+ export class MotionGraphDef {
29
+ skeleton;
30
+ paramNames;
31
+ paramStart;
32
+ triggerNames;
33
+ maxBlend;
34
+ maxEvents;
35
+ /** Numbers of one figure's state (parameters, triggers, layers). */
36
+ stateSize;
37
+ /** @internal */ layers;
38
+ /** Event names: state names first (by layer), then mark names. @internal */
39
+ eventNames = [];
40
+ /** @internal */ scratch;
41
+ /** @internal */ weights;
42
+ params = new Map();
43
+ triggers = new Map();
44
+ constructor(desc) {
45
+ this.skeleton = desc.skeleton;
46
+ this.maxBlend = Math.max(1, desc.maxBlend ?? 4);
47
+ this.maxEvents = Math.max(1, desc.maxEvents ?? 32);
48
+ const pNames = [];
49
+ const pStart = [];
50
+ const param = (name) => {
51
+ let i = this.params.get(name);
52
+ if (i === undefined) {
53
+ i = pNames.length;
54
+ this.params.set(name, i);
55
+ pNames.push(name);
56
+ pStart.push(0);
57
+ }
58
+ return i;
59
+ };
60
+ for (const [k, v] of Object.entries(desc.params ?? {}))
61
+ pStart[param(k)] = v;
62
+ const tNames = [];
63
+ const trig = (name) => {
64
+ let i = this.triggers.get(name);
65
+ if (i === undefined) {
66
+ i = tNames.length;
67
+ if (i >= 31)
68
+ throw new Error('MotionGraph: at most 31 triggers');
69
+ this.triggers.set(name, i);
70
+ tNames.push(name);
71
+ }
72
+ return i;
73
+ };
74
+ for (const t of desc.triggers ?? [])
75
+ trig(t);
76
+ const markIds = new Map();
77
+ const markNames = [];
78
+ const markId = (name) => {
79
+ let i = markIds.get(name);
80
+ if (i === undefined) {
81
+ i = markNames.length;
82
+ markIds.set(name, i);
83
+ markNames.push(name);
84
+ }
85
+ return i;
86
+ };
87
+ const syncIds = new Map();
88
+ let maxSources = 1;
89
+ const layers = [];
90
+ const stateNameBase = [];
91
+ if (desc.layers.length === 0)
92
+ throw new Error('MotionGraph: no layers');
93
+ for (const l of desc.layers) {
94
+ if (l.states.length === 0)
95
+ throw new Error(`MotionGraph: layer ${l.name} has no states`);
96
+ stateNameBase.push(this.eventNames.length);
97
+ const index = new Map();
98
+ const states = l.states.map((s, k) => {
99
+ if (index.has(s.name))
100
+ throw new Error(`MotionGraph: two states ${s.name} in layer ${l.name}`);
101
+ index.set(s.name, k);
102
+ this.eventNames.push(s.name);
103
+ const sources = s.sources ?? (s.source ? [s.source] : []);
104
+ maxSources = Math.max(maxSources, sources.length);
105
+ if (s.drive && !(s.drive.per > 0))
106
+ throw new Error(`MotionGraph: state ${s.name} needs drive.per > 0`);
107
+ let sync = -1;
108
+ if (s.sync !== undefined) {
109
+ sync = syncIds.get(s.sync) ?? syncIds.size;
110
+ syncIds.set(s.sync, sync);
111
+ }
112
+ return {
113
+ name: s.name,
114
+ sources,
115
+ by: s.by !== undefined ? param(s.by) : -1,
116
+ rate: typeof s.rate === 'number' ? s.rate : 1,
117
+ rateParam: typeof s.rate === 'string' ? param(s.rate) : -1,
118
+ driveParam: s.drive ? param(s.drive.param) : -1,
119
+ per: s.drive?.per ?? 1,
120
+ sync,
121
+ loop: sources.length === 0 || sources.some((m) => m.loop !== false),
122
+ marks: sources.map((m) => {
123
+ const ms = [...(m.marks ?? [])].sort((a, b) => a.at - b.at);
124
+ return { at: Float64Array.from(ms.map((x) => x.at)), name: Int32Array.from(ms.map((x) => markId(x.name))) };
125
+ }),
126
+ };
127
+ });
128
+ const stateOf = (name) => {
129
+ const i = index.get(name);
130
+ if (i === undefined)
131
+ throw new Error(`MotionGraph: no state ${name} in layer ${l.name}`);
132
+ return i;
133
+ };
134
+ const transitions = (l.transitions ?? []).map((t) => ({
135
+ from: t.from === '*' ? -1 : stateOf(t.from),
136
+ to: stateOf(t.to),
137
+ seconds: Math.max(0, t.seconds),
138
+ smooth: (t.ease ?? 'smooth') === 'smooth',
139
+ trigger: t.trigger !== undefined ? trig(t.trigger) : -1,
140
+ after: t.after ?? -Infinity,
141
+ restart: t.restart ?? false,
142
+ conds: (t.when ?? []).map((c) => {
143
+ const op = OPS.indexOf(c.op);
144
+ if (op < 0)
145
+ throw new Error(`MotionGraph: unknown operator ${c.op}`);
146
+ return { param: param(c.param), op, value: c.value };
147
+ }),
148
+ }));
149
+ layers.push({
150
+ name: l.name,
151
+ additive: l.mode === 'additive',
152
+ mask: l.mask ?? null,
153
+ weight: typeof l.weight === 'number' ? l.weight : 1,
154
+ weightParam: typeof l.weight === 'string' ? param(l.weight) : -1,
155
+ states,
156
+ initial: l.initial !== undefined ? stateOf(l.initial) : 0,
157
+ transitions,
158
+ at: 0,
159
+ });
160
+ }
161
+ // Mark names follow the state names in the event table.
162
+ const markBase = this.eventNames.length;
163
+ this.eventNames.push(...markNames);
164
+ for (const l of layers)
165
+ for (const s of l.states)
166
+ for (const m of s.marks)
167
+ for (let k = 0; k < m.name.length; k++)
168
+ m.name[k] = m.name[k] + markBase;
169
+ this.paramNames = pNames;
170
+ this.paramStart = Float64Array.from(pStart);
171
+ this.triggerNames = tNames;
172
+ // State layout: [format, params…, triggers, per layer: count, entries…].
173
+ let at = 1 + pNames.length + 1;
174
+ for (const l of layers) {
175
+ l.at = at;
176
+ at += 1 + this.maxBlend * ENTRY;
177
+ }
178
+ this.stateSize = at;
179
+ this.layers = layers;
180
+ this.stateNameBase = stateNameBase;
181
+ const sk = desc.skeleton;
182
+ this.scratch = [new Pose(sk), new Pose(sk), new Pose(sk)];
183
+ this.weights = new Float64Array(maxSources);
184
+ }
185
+ /** @internal Index of the first state name of each layer in `eventNames`. */
186
+ stateNameBase;
187
+ /** Index of a parameter, −1 when there is none. */
188
+ param(name) {
189
+ return this.params.get(name) ?? -1;
190
+ }
191
+ /** Index of a trigger, −1 when there is none. */
192
+ trigger(name) {
193
+ return this.triggers.get(name) ?? -1;
194
+ }
195
+ /** Index of a layer by name (−1 none). */
196
+ layer(name) {
197
+ for (let i = 0; i < this.layers.length; i++)
198
+ if (this.layers[i].name === name)
199
+ return i;
200
+ return -1;
201
+ }
202
+ }
203
+ /** Compiles a description once; pass the result to every figure's `MotionGraph`. */
204
+ export function compileMotionGraph(desc) {
205
+ return new MotionGraphDef(desc);
206
+ }
207
+ /**
208
+ * Weights of motions by their speed ranges at `value` into `out` (one per range): the two motions whose ideal
209
+ * speeds enclose the value share the weight linearly; below the lowest ideal the slowest motion has it all, above the
210
+ * highest the fastest. Continuous in `value`, sum 1. Allocates nothing.
211
+ */
212
+ export function speedWeights(speeds, value, out) {
213
+ const n = speeds.length;
214
+ let lo = -1, hi = -1, min = -1, max = -1;
215
+ for (let i = 0; i < n; i++) {
216
+ out[i] = 0;
217
+ const s = speeds[i];
218
+ if (!s)
219
+ continue;
220
+ const v = s[1];
221
+ if (min < 0 || v < speeds[min][1])
222
+ min = i;
223
+ if (max < 0 || v > speeds[max][1])
224
+ max = i;
225
+ if (v <= value && (lo < 0 || v > speeds[lo][1]))
226
+ lo = i;
227
+ if (v >= value && (hi < 0 || v < speeds[hi][1]))
228
+ hi = i;
229
+ }
230
+ if (min < 0) {
231
+ // No ranges: equal shares.
232
+ for (let i = 0; i < n; i++)
233
+ out[i] = 1 / n;
234
+ return;
235
+ }
236
+ if (lo < 0) {
237
+ out[min] = 1;
238
+ return;
239
+ }
240
+ if (hi < 0) {
241
+ out[max] = 1;
242
+ return;
243
+ }
244
+ const a = speeds[lo][1], b = speeds[hi][1];
245
+ if (lo === hi || b <= a) {
246
+ out[lo] = 1;
247
+ return;
248
+ }
249
+ const t = (value - a) / (b - a);
250
+ out[lo] = 1 - t;
251
+ out[hi] = t;
252
+ }
253
+ // --- One figure ----------------------------------------------------------------------------------------------------
254
+ /**
255
+ * One figure's motion: the shared compiled description and this figure's small state. `input` is handed to every
256
+ * source's `sample` as its `params` (the game's own per-figure data; set it once, change its fields freely).
257
+ */
258
+ export class MotionGraph {
259
+ def;
260
+ /** Passed to the sources' `sample` as `params`. */
261
+ input;
262
+ /** Parameters, triggers and active states; `save` and `restore` copy it. */
263
+ data;
264
+ /** Events that did not fit between two `drainEvents` calls (counted, then lost). */
265
+ droppedEvents = 0;
266
+ events;
267
+ eventCount = 0;
268
+ bytes;
269
+ constructor(def, input) {
270
+ this.def = def instanceof MotionGraphDef ? def : new MotionGraphDef(def);
271
+ this.input = input;
272
+ this.data = new Float64Array(this.def.stateSize);
273
+ this.bytes = new Uint8Array(this.data.buffer);
274
+ this.events = new Int32Array(this.def.maxEvents * 4);
275
+ this.reset();
276
+ }
277
+ /** Start values of the parameters, every layer in its initial state at time and phase 0. */
278
+ reset() {
279
+ const d = this.data;
280
+ const def = this.def;
281
+ d.fill(0);
282
+ d[0] = FORMAT;
283
+ d.set(def.paramStart, 1);
284
+ for (const l of def.layers) {
285
+ d[l.at] = 1;
286
+ const e = l.at + 1;
287
+ d[e] = l.initial;
288
+ d[e + 3] = 1;
289
+ }
290
+ this.eventCount = 0;
291
+ }
292
+ /** Sets a parameter: the ONLY way game logic steers motion. */
293
+ setParam(name, value) {
294
+ const i = this.def.param(name);
295
+ if (i < 0)
296
+ throw new Error(`MotionGraph: no parameter ${name}`);
297
+ this.data[1 + i] = value;
298
+ }
299
+ getParam(name) {
300
+ const i = this.def.param(name);
301
+ if (i < 0)
302
+ throw new Error(`MotionGraph: no parameter ${name}`);
303
+ return this.data[1 + i];
304
+ }
305
+ /** A one-shot signal: lives until a transition consumes it or the next `update` ends. */
306
+ trigger(name) {
307
+ const i = this.def.trigger(name);
308
+ if (i < 0)
309
+ throw new Error(`MotionGraph: no trigger ${name}`);
310
+ const at = 1 + this.def.paramNames.length;
311
+ this.data[at] = (this.data[at] | (1 << i)) >>> 0;
312
+ }
313
+ /** Advances states, transitions and timelines by `dt` seconds; deterministic, allocates nothing. */
314
+ update(dt) {
315
+ const def = this.def;
316
+ const d = this.data;
317
+ const pAt = 1;
318
+ const tAt = 1 + def.paramNames.length;
319
+ for (let li = 0; li < def.layers.length; li++) {
320
+ const l = def.layers[li];
321
+ // Transitions from the newest state.
322
+ let n = d[l.at];
323
+ const curAt = l.at + 1 + (n - 1) * ENTRY;
324
+ const cur = d[curAt];
325
+ for (let k = 0; k < l.transitions.length; k++) {
326
+ const t = l.transitions[k];
327
+ if (t.from >= 0 && t.from !== cur)
328
+ continue;
329
+ if (t.to === cur && !t.restart)
330
+ continue;
331
+ if (t.trigger >= 0 && (d[tAt] & (1 << t.trigger)) === 0)
332
+ continue;
333
+ if (d[curAt + 1] < t.after)
334
+ continue;
335
+ let ok = true;
336
+ for (let c = 0; c < t.conds.length && ok; c++) {
337
+ const q = t.conds[c];
338
+ const v = d[pAt + q.param];
339
+ ok = q.op === 0 ? v < q.value : q.op === 1 ? v <= q.value : q.op === 2 ? v > q.value : q.op === 3 ? v >= q.value : q.op === 4 ? v === q.value : v !== q.value;
340
+ }
341
+ if (!ok)
342
+ continue;
343
+ if (t.trigger >= 0)
344
+ d[tAt] = (d[tAt] & ~(1 << t.trigger)) >>> 0;
345
+ this.push(li, t.to, t.seconds, t.smooth, curAt);
346
+ n = d[l.at];
347
+ break;
348
+ }
349
+ // Advance every active state; marks of the newest.
350
+ for (let i = 0; i < n; i++) {
351
+ const e = l.at + 1 + i * ENTRY;
352
+ const s = l.states[d[e]];
353
+ const p0 = d[e + 1];
354
+ const leader = this.advance(s, e, dt);
355
+ if (i === n - 1 && leader >= 0)
356
+ this.marks(li, d[e], s.marks[leader], p0, d[e + 1]);
357
+ if (i > 0) {
358
+ const secs = d[e + 4];
359
+ d[e + 3] = secs > 0 ? Math.min(1, d[e + 3] + dt / secs) : 1;
360
+ }
361
+ }
362
+ // Once a newer state is fully in, everything older is gone.
363
+ let full = 0;
364
+ for (let i = n - 1; i > 0; i--) {
365
+ if (d[l.at + 1 + i * ENTRY + 3] >= 1) {
366
+ full = i;
367
+ break;
368
+ }
369
+ }
370
+ if (full > 0) {
371
+ d.copyWithin(l.at + 1, l.at + 1 + full * ENTRY, l.at + 1 + n * ENTRY);
372
+ n -= full;
373
+ d[l.at] = n;
374
+ d[l.at + 1 + 3] = 1;
375
+ }
376
+ }
377
+ d[tAt] = 0;
378
+ }
379
+ /** Writes the pose of all layers into `out`. Allocates nothing (scratch poses belong to the shared description). */
380
+ evaluate(out) {
381
+ const def = this.def;
382
+ const d = this.data;
383
+ const [layerPose, entryPose] = def.scratch;
384
+ out.reset();
385
+ for (let li = 0; li < def.layers.length; li++) {
386
+ const l = def.layers[li];
387
+ const lw = l.weightParam >= 0 ? d[1 + l.weightParam] : l.weight;
388
+ if (lw <= 0)
389
+ continue;
390
+ const n = d[l.at];
391
+ for (let i = 0; i < n; i++) {
392
+ const e = l.at + 1 + i * ENTRY;
393
+ if (i === 0) {
394
+ this.sampleState(l, e, layerPose, out);
395
+ continue;
396
+ }
397
+ const pr = d[e + 3];
398
+ const w = d[e + 5] ? pr * pr * (3 - 2 * pr) : pr;
399
+ if (w <= 0)
400
+ continue;
401
+ this.sampleState(l, e, entryPose, out);
402
+ blendPose(layerPose, layerPose, entryPose, w);
403
+ }
404
+ if (l.additive)
405
+ addPose(out, out, layerPose, Math.min(lw, 1), l.mask);
406
+ else
407
+ blendPose(out, out, layerPose, Math.min(lw, 1), l.mask);
408
+ }
409
+ }
410
+ /**
411
+ * Marks crossed and states entered or left since the last call, oldest first: kind, name (of the mark or the
412
+ * state), state (the state it happened in) and layer. No allocation per call.
413
+ */
414
+ drainEvents(fn) {
415
+ const ev = this.events;
416
+ const def = this.def;
417
+ const names = def.eventNames;
418
+ const n = this.eventCount;
419
+ this.eventCount = 0;
420
+ for (let i = 0; i < n; i++) {
421
+ const o = i * 4;
422
+ const layer = ev[o + 3];
423
+ fn(KIND_NAMES[ev[o]], names[ev[o + 1]], names[def.stateNameBase[layer] + ev[o + 2]], def.layers[layer].name);
424
+ }
425
+ }
426
+ /** Name of the current (newest) state of a layer; default the first layer. */
427
+ state(layer) {
428
+ const l = this.layerOf(layer);
429
+ const n = this.data[l.at];
430
+ return l.states[this.data[l.at + 1 + (n - 1) * ENTRY]].name;
431
+ }
432
+ /** Phase (passes) of the current state of a layer. */
433
+ phase(layer) {
434
+ const l = this.layerOf(layer);
435
+ return this.data[l.at + 1 + (this.data[l.at] - 1) * ENTRY + 1];
436
+ }
437
+ /** Seconds (times rate) since the current state of a layer was entered. */
438
+ time(layer) {
439
+ const l = this.layerOf(layer);
440
+ return this.data[l.at + 1 + (this.data[l.at] - 1) * ENTRY + 2];
441
+ }
442
+ /** The share 0–1 a state has in its layer's pose now (its cross-fades included); 0 when it is not active. */
443
+ weightOf(state, layer) {
444
+ const l = this.layerOf(layer);
445
+ const d = this.data;
446
+ const n = d[l.at];
447
+ let total = 0;
448
+ // Walk from the newest: each entry takes its weight of what is left.
449
+ let left = 1;
450
+ for (let i = n - 1; i >= 0; i--) {
451
+ const e = l.at + 1 + i * ENTRY;
452
+ const pr = i === 0 ? 1 : d[e + 3];
453
+ const w = i === 0 ? 1 : d[e + 5] ? pr * pr * (3 - 2 * pr) : pr;
454
+ if (l.states[d[e]].name === state)
455
+ total += left * w;
456
+ left *= 1 - w;
457
+ }
458
+ return total;
459
+ }
460
+ /**
461
+ * Makes `state` the only active state of a layer at once, `time` seconds into it (seconds times its rate, as `time()`
462
+ * reads them; the phase follows from its sources' durations and the parameters as they are now, or from its driving
463
+ * parameter). For a figure that starts or comes back into view in the middle of something: it neither begins every
464
+ * loop at its start nor fades in from the layer's initial state. No cross-fade, no events (marks before `time` are not
465
+ * reported). Allocates nothing.
466
+ */
467
+ setState(state, layer, time = 0) {
468
+ const l = this.layerOf(layer);
469
+ let si = -1;
470
+ for (let k = 0; k < l.states.length && si < 0; k++)
471
+ if (l.states[k].name === state)
472
+ si = k;
473
+ if (si < 0)
474
+ throw new Error(`MotionGraph: no state ${state} in layer ${l.name}`);
475
+ const s = l.states[si];
476
+ const d = this.data;
477
+ const e = l.at + 1;
478
+ this.weigh(s);
479
+ let phase = s.driveParam >= 0 ? d[1 + s.driveParam] / s.per : time * this.passesPerSecond(s);
480
+ if (!s.loop)
481
+ phase = phase < 0 ? 0 : phase > 1 ? 1 : phase;
482
+ d[l.at] = 1;
483
+ d[e] = si;
484
+ d[e + 1] = phase;
485
+ d[e + 2] = time;
486
+ d[e + 3] = 1;
487
+ d[e + 4] = 0;
488
+ d[e + 5] = 0;
489
+ }
490
+ /** Bytes of the state (a copy, or written into `out` at `offset`). Drain the events first: they are not saved. */
491
+ save(out, offset = 0) {
492
+ if (!out)
493
+ return this.bytes.slice();
494
+ out.set(this.bytes, offset);
495
+ return out;
496
+ }
497
+ /** Brings back a state saved by `save` of a figure with the same description. */
498
+ restore(bytes, offset = 0) {
499
+ if (bytes.length - offset < this.bytes.length)
500
+ throw new Error('MotionGraph.restore: too few bytes');
501
+ this.bytes.set(bytes.subarray(offset, offset + this.bytes.length));
502
+ if (this.data[0] !== FORMAT)
503
+ throw new Error('MotionGraph.restore: not a motion graph state');
504
+ this.eventCount = 0;
505
+ }
506
+ /** Bytes `save` writes. */
507
+ get byteLength() {
508
+ return this.bytes.length;
509
+ }
510
+ layerOf(name) {
511
+ const def = this.def;
512
+ if (name === undefined)
513
+ return def.layers[0];
514
+ const i = def.layer(name);
515
+ if (i < 0)
516
+ throw new Error(`MotionGraph: no layer ${name}`);
517
+ return def.layers[i];
518
+ }
519
+ /** Starts a transition in layer `li` to state `to`; `curAt` is the entry of the current state. */
520
+ push(li, to, seconds, smooth, curAt) {
521
+ const def = this.def;
522
+ const l = def.layers[li];
523
+ const d = this.data;
524
+ const from = d[curAt];
525
+ let phase = 0;
526
+ const sync = l.states[to].sync;
527
+ if (sync >= 0 && l.states[from].sync === sync)
528
+ phase = d[curAt + 1];
529
+ let n = d[l.at];
530
+ if (n >= def.maxBlend) {
531
+ // Full: drop the older entry with the smallest share of the pose.
532
+ let drop = 0;
533
+ let least = Infinity;
534
+ let left = 1;
535
+ for (let i = n - 1; i >= 0; i--) {
536
+ const e = l.at + 1 + i * ENTRY;
537
+ const pr = i === 0 ? 1 : d[e + 3];
538
+ const w = i === 0 ? 1 : d[e + 5] ? pr * pr * (3 - 2 * pr) : pr;
539
+ if (left * w < least) {
540
+ least = left * w;
541
+ drop = i;
542
+ }
543
+ left *= 1 - w;
544
+ }
545
+ const at = l.at + 1 + drop * ENTRY;
546
+ d.copyWithin(at, at + ENTRY, l.at + 1 + n * ENTRY);
547
+ n--;
548
+ d[l.at + 1 + 3] = 1;
549
+ }
550
+ const e = l.at + 1 + n * ENTRY;
551
+ d[e] = to;
552
+ d[e + 1] = phase;
553
+ d[e + 2] = 0;
554
+ d[e + 3] = seconds > 0 ? 0 : 1;
555
+ d[e + 4] = seconds;
556
+ d[e + 5] = smooth ? 1 : 0;
557
+ d[l.at] = n + 1;
558
+ this.event(1, def.stateNameBase[li] + to, to, li);
559
+ this.event(2, def.stateNameBase[li] + from, from, li);
560
+ }
561
+ /** Advances one entry's time and phase; returns the source whose marks count (the heaviest), −1 none. */
562
+ advance(s, e, dt) {
563
+ const d = this.data;
564
+ const rate = s.rateParam >= 0 ? d[1 + s.rateParam] : s.rate;
565
+ d[e + 2] = d[e + 2] + dt * rate;
566
+ const leader = this.weigh(s);
567
+ let phase;
568
+ if (s.driveParam >= 0)
569
+ phase = d[1 + s.driveParam] / s.per;
570
+ else
571
+ phase = d[e + 1] + dt * rate * this.passesPerSecond(s);
572
+ if (!s.loop)
573
+ phase = phase < 0 ? 0 : phase > 1 ? 1 : phase;
574
+ d[e + 1] = phase;
575
+ return leader;
576
+ }
577
+ /** Passes per second (before the rate) of a state's weighted sources; `weigh(s)` must have run. */
578
+ passesPerSecond(s) {
579
+ const d = this.data;
580
+ const w = this.def.weights;
581
+ let speed = 0;
582
+ const v = s.by >= 0 ? d[1 + s.by] : 0;
583
+ for (let k = 0; k < s.sources.length; k++) {
584
+ const m = s.sources[k];
585
+ if (!(w[k] > 0) || !(m.duration > 0))
586
+ continue;
587
+ let f = 1;
588
+ const r = m.speed;
589
+ if (s.by >= 0 && r && r[1] > 0)
590
+ f = (v < r[0] ? r[0] : v > r[2] ? r[2] : v) / r[1];
591
+ speed += (w[k] * f) / m.duration;
592
+ }
593
+ return speed;
594
+ }
595
+ /** Weights of a state's sources into `def.weights`; returns the heaviest source with marks, −1 none. */
596
+ weigh(s) {
597
+ const w = this.def.weights;
598
+ const n = s.sources.length;
599
+ if (n === 0)
600
+ return -1;
601
+ if (n === 1)
602
+ w[0] = 1;
603
+ else if (s.by >= 0)
604
+ speedWeightsOf(s.sources, this.data[1 + s.by], w);
605
+ else
606
+ for (let k = 0; k < n; k++)
607
+ w[k] = 1 / n;
608
+ let best = -1;
609
+ for (let k = 0; k < n; k++)
610
+ if (s.marks[k].at.length > 0 && (best < 0 || w[k] > w[best]))
611
+ best = k;
612
+ return best;
613
+ }
614
+ /** Reports the marks crossed between phases p0 and p1 (either direction), in the order they were crossed. */
615
+ marks(li, state, m, p0, p1) {
616
+ const at = m.at;
617
+ const n = at.length;
618
+ if (n === 0 || p0 === p1)
619
+ return;
620
+ if (p1 > p0) {
621
+ const k1 = Math.floor(p1);
622
+ for (let k = Math.floor(p0); k <= k1; k++) {
623
+ for (let j = 0; j < n; j++) {
624
+ const v = k + at[j];
625
+ if (v > p0 && v <= p1 && !this.event(0, m.name[j], state, li))
626
+ return;
627
+ }
628
+ }
629
+ }
630
+ else {
631
+ const k1 = Math.floor(p1);
632
+ for (let k = Math.floor(p0); k >= k1; k--) {
633
+ for (let j = n - 1; j >= 0; j--) {
634
+ const v = k + at[j];
635
+ if (v < p0 && v >= p1 && !this.event(0, m.name[j], state, li))
636
+ return;
637
+ }
638
+ }
639
+ }
640
+ }
641
+ /** Queues one event; false (and counted as dropped) when the queue is full. */
642
+ event(kind, name, state, layer) {
643
+ if (this.eventCount >= this.def.maxEvents) {
644
+ this.droppedEvents++;
645
+ return false;
646
+ }
647
+ const o = this.eventCount * 4;
648
+ const ev = this.events;
649
+ ev[o] = kind;
650
+ ev[o + 1] = name;
651
+ ev[o + 2] = state;
652
+ ev[o + 3] = layer;
653
+ this.eventCount++;
654
+ return true;
655
+ }
656
+ /** The pose of one active entry of layer `l` into `dst`; `input` is the pose made by the layers before. */
657
+ sampleState(l, e, dst, input) {
658
+ const d = this.data;
659
+ const s = l.states[d[e]];
660
+ const n = s.sources.length;
661
+ if (n === 0) {
662
+ if (l.additive)
663
+ dst.reset();
664
+ else
665
+ dst.copy(input);
666
+ return;
667
+ }
668
+ const phase = d[e + 1];
669
+ const time = d[e + 2];
670
+ this.weigh(s);
671
+ const w = this.def.weights;
672
+ const tmp = this.def.scratch[2];
673
+ let acc = 0;
674
+ for (let k = 0; k < n; k++) {
675
+ const wk = w[k];
676
+ if (!(wk > 0))
677
+ continue;
678
+ const m = s.sources[k];
679
+ const target = acc === 0 ? dst : tmp;
680
+ target.reset();
681
+ m.sample(target, timeOf(m, phase, time, s.loop), this.input);
682
+ if (acc === 0)
683
+ acc = wk;
684
+ else {
685
+ acc += wk;
686
+ blendPose(dst, dst, tmp, wk / acc);
687
+ }
688
+ }
689
+ }
690
+ }
691
+ /** The `t` a source is sampled at. */
692
+ function timeOf(m, phase, time, loop) {
693
+ const dur = m.duration;
694
+ if (dur === undefined || !(dur > 0))
695
+ return m.unit === 'phase' ? phase - Math.floor(phase) : time;
696
+ const f = loop && m.loop !== false ? phase - Math.floor(phase) : phase < 0 ? 0 : phase > 1 ? 1 : phase;
697
+ return m.unit === 'phase' ? f : f * dur;
698
+ }
699
+ function speedWeightsOf(sources, value, out) {
700
+ // Same rule as `speedWeights`, read straight from the sources (no array of ranges per call).
701
+ const n = sources.length;
702
+ let lo = -1, hi = -1, min = -1, max = -1;
703
+ for (let i = 0; i < n; i++) {
704
+ out[i] = 0;
705
+ const s = sources[i].speed;
706
+ if (!s)
707
+ continue;
708
+ const v = s[1];
709
+ if (min < 0 || v < sources[min].speed[1])
710
+ min = i;
711
+ if (max < 0 || v > sources[max].speed[1])
712
+ max = i;
713
+ if (v <= value && (lo < 0 || v > sources[lo].speed[1]))
714
+ lo = i;
715
+ if (v >= value && (hi < 0 || v < sources[hi].speed[1]))
716
+ hi = i;
717
+ }
718
+ if (min < 0) {
719
+ for (let i = 0; i < n; i++)
720
+ out[i] = 1 / n;
721
+ return;
722
+ }
723
+ if (lo < 0) {
724
+ out[min] = 1;
725
+ return;
726
+ }
727
+ if (hi < 0) {
728
+ out[max] = 1;
729
+ return;
730
+ }
731
+ const a = sources[lo].speed[1], b = sources[hi].speed[1];
732
+ if (lo === hi || b <= a) {
733
+ out[lo] = 1;
734
+ return;
735
+ }
736
+ const t = (value - a) / (b - a);
737
+ out[lo] = 1 - t;
738
+ out[hi] = t;
739
+ }