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,261 @@
1
+ // Perspective camera rig that orbits a target point: yaw around the vertical axis, pitch above the horizon and a
2
+ // distance. Drag turns it, the wheel zooms (see `bindOrbitInput` in platform). Implements `View` for the renderer.
3
+ import { DEG, clamp, cross, crossInto, dot, normalize } from '../base/math.js';
4
+ export const DEFAULT_ORBIT = { fovYDeg: 45, minPitchDeg: 5, maxPitchDeg: 85, minDistance: 2, maxDistance: 500, turnPerPx: 0.006 };
5
+ export class PerspectiveOrbitRig {
6
+ setup;
7
+ target = [0, 0, 0];
8
+ /** Rotation around the vertical axis in radians; 0 looks toward −z. */
9
+ yaw = 0;
10
+ /** Elevation of the view above the horizon in radians (positive looks down). */
11
+ pitch = 30 * DEG;
12
+ distance = 50;
13
+ /** Canvas size in physical pixels and physical pixels per CSS pixel. */
14
+ width = 1;
15
+ height = 1;
16
+ dpr = 1;
17
+ /** Scratch of the methods that take `out`, so they allocate nothing themselves. */
18
+ scratch = { right: [0, 0, 0], up: [0, 0, 0], forward: [0, 0, 0] };
19
+ scratchVp = { right: [0, 0, 0], up: [0, 0, 0], forward: [0, 0, 0] };
20
+ scratchEye = [0, 0, 0];
21
+ /** The numbers the last `key` was made from (NaN: none yet) and that key. */
22
+ keyOf = new Float64Array(8).fill(NaN);
23
+ keyText = '';
24
+ constructor(setup = {}, state) {
25
+ this.setup = { ...DEFAULT_ORBIT, ...setup };
26
+ if (state)
27
+ this.setState(state);
28
+ }
29
+ setState(s) {
30
+ if (s.target)
31
+ this.target = [...s.target];
32
+ if (s.yawDeg !== undefined)
33
+ this.yaw = s.yawDeg * DEG;
34
+ if (s.pitchDeg !== undefined)
35
+ this.pitch = clamp(s.pitchDeg, this.setup.minPitchDeg, this.setup.maxPitchDeg) * DEG;
36
+ if (s.distance !== undefined)
37
+ this.distance = clamp(s.distance, this.setup.minDistance, this.setup.maxDistance);
38
+ this.keepAboveGround();
39
+ }
40
+ /**
41
+ * With `setup.groundAt`: raises the pitch (never above `maxPitchDeg`) until the eye stands at least `minEyeHeight`
42
+ * over the ground, the least raise that does it (to a hundredth of a degree). `setState`, `orbitPixels` and `zoomBy`
43
+ * call it; call it yourself when the ground under the eye changed. Returns whether the pitch changed. Allocates
44
+ * nothing; without `groundAt` it does nothing.
45
+ */
46
+ keepAboveGround() {
47
+ const ground = this.setup.groundAt;
48
+ if (!ground)
49
+ return false;
50
+ const min = this.setup.minEyeHeight ?? 1;
51
+ const maxPitch = this.setup.maxPitchDeg * DEG;
52
+ const start = this.pitch;
53
+ if (this.eyeClear(ground, min, start) >= 0 || start >= maxPitch)
54
+ return false;
55
+ if (this.eyeClear(ground, min, maxPitch) < 0) {
56
+ this.pitch = maxPitch;
57
+ return true;
58
+ }
59
+ // Bisection between a pitch that is too low and one that clears the ground.
60
+ let lo = start;
61
+ let hi = maxPitch;
62
+ while (hi - lo > 1e-4) {
63
+ const mid = (lo + hi) / 2;
64
+ if (this.eyeClear(ground, min, mid) >= 0)
65
+ hi = mid;
66
+ else
67
+ lo = mid;
68
+ }
69
+ this.pitch = hi;
70
+ return true;
71
+ }
72
+ /** Height of the eye over the ground minus `min` for the pitch `p`, the rest of the state as it is. */
73
+ eyeClear(ground, min, p) {
74
+ const t = this.target;
75
+ const d = this.distance;
76
+ const h = Math.cos(p) * d;
77
+ const x = t[0] + Math.sin(this.yaw) * h;
78
+ const y = t[1] + Math.sin(p) * d;
79
+ const z = t[2] + Math.cos(this.yaw) * h;
80
+ return y - ground(x, z) - min;
81
+ }
82
+ state() {
83
+ return { target: [...this.target], yawDeg: this.yaw / DEG, pitchDeg: this.pitch / DEG, distance: this.distance };
84
+ }
85
+ get heading() {
86
+ return this.yaw;
87
+ }
88
+ get tanHalfY() {
89
+ return Math.tan((this.setup.fovYDeg * DEG) / 2);
90
+ }
91
+ /** View: pixel size at the target. */
92
+ get metresPerPixel() {
93
+ return this.distance * this.pixelGrowth;
94
+ }
95
+ /** View: metres per pixel gained per metre of depth. */
96
+ get pixelGrowth() {
97
+ return (2 * this.tanHalfY) / this.height;
98
+ }
99
+ /** View: depth of the target along the view direction. */
100
+ get refDepth() {
101
+ return dot(this.axes(this.scratch).forward, this.target);
102
+ }
103
+ /** View: screen right, screen up and the view direction. With `out` its arrays are written and `out` is returned. */
104
+ axes(out) {
105
+ const yaw = this.yaw;
106
+ const p = this.pitch;
107
+ const f = out ?? { right: [0, 0, 0], up: [0, 0, 0], forward: [0, 0, 0] };
108
+ const right = f.right;
109
+ const forward = f.forward;
110
+ right[0] = Math.cos(yaw);
111
+ right[1] = 0;
112
+ right[2] = -Math.sin(yaw);
113
+ forward[0] = -Math.sin(yaw) * Math.cos(p);
114
+ forward[1] = -Math.sin(p);
115
+ forward[2] = -Math.cos(yaw) * Math.cos(p);
116
+ crossInto(right, forward, f.up);
117
+ return f;
118
+ }
119
+ /** The eye: the target moved back along the view direction by the distance. With `out` it is written there. */
120
+ eye(out) {
121
+ const f = this.axes(this.scratch).forward;
122
+ const t = this.target;
123
+ const d = this.distance;
124
+ const e = out ?? [0, 0, 0];
125
+ e[0] = t[0] - f[0] * d;
126
+ e[1] = t[1] - f[1] * d;
127
+ e[2] = t[2] - f[2] * d;
128
+ return e;
129
+ }
130
+ /** World → clip matrix. Near and far planes enclose the sphere of `depthHalfRange` around `sceneCenter`. */
131
+ viewProj(sceneCenter, depthHalfRange, out) {
132
+ const { right: r, up: u, forward: w } = this.axes(this.scratchVp);
133
+ const eye = this.eye(this.scratchEye);
134
+ const dc = dot(w, sceneCenter) - dot(w, eye);
135
+ const far = Math.max(dc + depthHalfRange, 1);
136
+ const near = Math.max(dc - depthHalfRange, far / 2000, 0.05);
137
+ const fy = 1 / this.tanHalfY;
138
+ const fx = fy / (this.width / this.height);
139
+ const a = (far + near) / (far - near);
140
+ const b = (-2 * far * near) / (far - near);
141
+ const m = out ?? new Float32Array(16);
142
+ m.fill(0);
143
+ const re = dot(r, eye);
144
+ const ue = dot(u, eye);
145
+ const we = dot(w, eye);
146
+ for (let i = 0; i < 3; i++) {
147
+ m[i * 4 + 0] = fx * r[i];
148
+ m[i * 4 + 1] = fy * u[i];
149
+ m[i * 4 + 2] = a * w[i];
150
+ m[i * 4 + 3] = w[i];
151
+ }
152
+ m[12] = -fx * re;
153
+ m[13] = -fy * ue;
154
+ m[14] = -a * we + b;
155
+ m[15] = -we;
156
+ return m;
157
+ }
158
+ /** `dirAt` for the axes `ax`, written into `out` (the same numbers, nothing allocated). */
159
+ dirInto(sx, sy, ax, out) {
160
+ const { right, up, forward } = ax;
161
+ const ty = this.tanHalfY;
162
+ const tx = ty * (this.width / this.height);
163
+ const x = forward[0] + right[0] * sx * tx + up[0] * sy * ty;
164
+ const y = forward[1] + right[1] * sx * tx + up[1] * sy * ty;
165
+ const z = forward[2] + right[2] * sx * tx + up[2] * sy * ty;
166
+ const l = Math.hypot(x, y, z) || 1;
167
+ out[0] = x / l;
168
+ out[1] = y / l;
169
+ out[2] = z / l;
170
+ return out;
171
+ }
172
+ /** Direction through a point given in normalised screen coordinates (−1…1, y up). */
173
+ dirAt(sx, sy) {
174
+ const { right, up, forward } = this.axes();
175
+ const ty = this.tanHalfY;
176
+ const tx = ty * (this.width / this.height);
177
+ return normalize([
178
+ forward[0] + right[0] * sx * tx + up[0] * sy * ty,
179
+ forward[1] + right[1] * sx * tx + up[1] * sy * ty,
180
+ forward[2] + right[2] * sx * tx + up[2] * sy * ty,
181
+ ]);
182
+ }
183
+ /**
184
+ * View: rays from the eye through the image corners. Without `out` the four rays share one origin array; with `out`
185
+ * (see `newCornerRays`) each ray's own arrays are written and `out` is returned.
186
+ */
187
+ cornerRays(out) {
188
+ if (!out) {
189
+ const eye = this.eye();
190
+ const rays = [];
191
+ for (const sx of [-1, 1])
192
+ for (const sy of [-1, 1])
193
+ rays.push({ origin: eye, dir: this.dirAt(sx, sy) });
194
+ return rays;
195
+ }
196
+ const ax = this.axes(this.scratch);
197
+ for (let i = 0; i < 4; i++) {
198
+ const r = out[i];
199
+ this.eye(r.origin);
200
+ this.dirInto(i < 2 ? -1 : 1, i % 2 === 0 ? -1 : 1, ax, r.dir);
201
+ }
202
+ return out;
203
+ }
204
+ /** View: the four side planes through the eye and the plane of the eye itself, normals inward. */
205
+ cullVolume() {
206
+ const eye = this.eye();
207
+ const { forward } = this.axes();
208
+ const d = [this.dirAt(-1, -1), this.dirAt(-1, 1), this.dirAt(1, 1), this.dirAt(1, -1)];
209
+ const planes = [[forward[0], forward[1], forward[2], -dot(forward, eye)]];
210
+ for (let i = 0; i < 4; i++) {
211
+ let n = normalize(cross(d[i], d[(i + 1) % 4]));
212
+ if (dot(n, forward) < 0)
213
+ n = [-n[0], -n[1], -n[2]];
214
+ planes.push([n[0], n[1], n[2], -dot(n, eye)]);
215
+ }
216
+ return { kind: 'frustum', planes };
217
+ }
218
+ key() {
219
+ const t = this.target;
220
+ // The string is made again only when a number in it changed, so a still view allocates nothing.
221
+ const k = this.keyOf;
222
+ if (k[0] === t[0] && k[1] === t[1] && k[2] === t[2] && k[3] === this.yaw && k[4] === this.pitch && k[5] === this.distance && k[6] === this.width && k[7] === this.height)
223
+ return this.keyText;
224
+ k[0] = t[0];
225
+ k[1] = t[1];
226
+ k[2] = t[2];
227
+ k[3] = this.yaw;
228
+ k[4] = this.pitch;
229
+ k[5] = this.distance;
230
+ k[6] = this.width;
231
+ k[7] = this.height;
232
+ this.keyText = `orbit|${t[0].toFixed(3)},${t[1].toFixed(3)},${t[2].toFixed(3)}|${this.yaw.toFixed(5)}|${this.pitch.toFixed(5)}|${this.distance.toFixed(3)}|${this.width}x${this.height}`;
233
+ return this.keyText;
234
+ }
235
+ /** Ray under a screen point in physical pixels from the top-left corner. */
236
+ screenRay(px, py) {
237
+ return { origin: this.eye(), dir: this.dirAt((2 * px) / this.width - 1, 1 - (2 * py) / this.height) };
238
+ }
239
+ /** Where a world point lands on the canvas, in physical pixels from the top-left corner. */
240
+ project(p) {
241
+ const { right, up, forward } = this.axes();
242
+ const e = this.eye();
243
+ const v = [p[0] - e[0], p[1] - e[1], p[2] - e[2]];
244
+ const z = dot(forward, v);
245
+ const ty = this.tanHalfY;
246
+ const tx = ty * (this.width / this.height);
247
+ return [((dot(right, v) / (z * tx) + 1) * this.width) / 2, ((1 - dot(up, v) / (z * ty)) * this.height) / 2];
248
+ }
249
+ /** Drag by physical pixels: horizontal turns around the target, vertical tilts. */
250
+ orbitPixels(dx, dy) {
251
+ const k = this.setup.turnPerPx / this.dpr;
252
+ this.yaw -= dx * k;
253
+ this.pitch = clamp(this.pitch + dy * k, this.setup.minPitchDeg * DEG, this.setup.maxPitchDeg * DEG);
254
+ this.keepAboveGround();
255
+ }
256
+ /** Multiplies the distance to the target (below 1 comes closer). */
257
+ zoomBy(factor) {
258
+ this.distance = clamp(this.distance * factor, this.setup.minDistance, this.setup.maxDistance);
259
+ this.keepAboveGround();
260
+ }
261
+ }
@@ -0,0 +1,48 @@
1
+ import type { OrbitState, PerspectiveOrbitRig } from './orbit.ts';
2
+ /** One key of an orbit path: the orbit state at a time. */
3
+ export interface OrbitKey extends OrbitState {
4
+ /** Time of the key in seconds from the start of the path. */
5
+ t: number;
6
+ }
7
+ export interface OrbitPathOptions {
8
+ /** True: the path repeats with the period `duration`, closing from the last key back to the first. Default false. */
9
+ loop?: boolean;
10
+ /**
11
+ * Length of the path in seconds. A looping path needs it to be later than the last key (the time of the closing
12
+ * segment is `duration` minus the last key time); default: the last key time plus the mean key spacing. An open path
13
+ * always ends at its last key and ignores it.
14
+ */
15
+ duration?: number;
16
+ }
17
+ export declare class OrbitPath {
18
+ readonly keys: readonly OrbitKey[];
19
+ readonly loop: boolean;
20
+ /** Seconds from the first key time (0 at the start of the path) to its end, or to where a loop repeats. */
21
+ readonly duration: number;
22
+ /** Key times, with one extra key at `duration` for a loop (the first key again). */
23
+ private readonly times;
24
+ /** Channel values and tangents (per second) per key, `CH` numbers per key. */
25
+ private readonly values;
26
+ private readonly slopes;
27
+ /** One sample's channels, reused. */
28
+ private readonly channels;
29
+ private readonly scratch;
30
+ constructor(keys: readonly OrbitKey[], options?: OrbitPathOptions);
31
+ /** A time folded into the path: modulo `duration` for a loop, clamped to `0 … duration` otherwise. */
32
+ wrap(t: number): number;
33
+ /** The state at time `t` (seconds from the start), written into `out`, which is returned. */
34
+ sample(t: number, out: OrbitState): OrbitState;
35
+ /** Samples the path at `t` and writes it into the rig in place (see `writeOrbitState`). */
36
+ apply(rig: PerspectiveOrbitRig, t: number): void;
37
+ }
38
+ /**
39
+ * Writes a state into an orbit rig without allocating: the target is copied into the rig's own array, pitch and
40
+ * distance are clamped to the rig's setup as `setState` does.
41
+ */
42
+ export declare function writeOrbitState(rig: PerspectiveOrbitRig, s: OrbitState): void;
43
+ /**
44
+ * Between two orbit states at `x` (0: `a`, 1: `b`), written into `out`: the target and pitch linearly, the yaw the
45
+ * shorter way round, the distance in log space. For blending from a free view into a path and back. `out` may be `a`
46
+ * or `b`.
47
+ */
48
+ export declare function blendOrbitState(a: OrbitState, b: OrbitState, x: number, out: OrbitState): OrbitState;
@@ -0,0 +1,179 @@
1
+ // A camera path for the orbit rig: keyframes of the orbit state (target, yaw, pitch, distance) at times in seconds,
2
+ // sampled as a smooth curve. Each channel is a cubic Hermite curve through the keys with Catmull-Rom tangents for
3
+ // uneven key spacing (the mean of the slopes to both neighbours, per second), so the velocity is continuous at every
4
+ // key. The yaw takes the shorter way round between neighbouring keys, the distance is interpolated in log space (equal
5
+ // zoom factors take equal time and it never reaches zero). An open path starts and ends at rest; a looping path closes
6
+ // from its last key back to its first, with the same smoothness at the seam. Sampling writes into a state the caller
7
+ // owns and allocates nothing.
8
+ import { DEG, clamp } from '../base/math.js';
9
+ /** Channels per key: target x, y, z, yaw (degrees, unwrapped), pitch (degrees), log of the distance. */
10
+ const CH = 6;
11
+ export class OrbitPath {
12
+ keys;
13
+ loop;
14
+ /** Seconds from the first key time (0 at the start of the path) to its end, or to where a loop repeats. */
15
+ duration;
16
+ /** Key times, with one extra key at `duration` for a loop (the first key again). */
17
+ times;
18
+ /** Channel values and tangents (per second) per key, `CH` numbers per key. */
19
+ values;
20
+ slopes;
21
+ /** One sample's channels, reused. */
22
+ channels = new Float64Array(CH);
23
+ scratch = { target: [0, 0, 0], yawDeg: 0, pitchDeg: 0, distance: 1 };
24
+ constructor(keys, options = {}) {
25
+ if (keys.length < 1)
26
+ throw new Error('OrbitPath: needs at least one key');
27
+ for (let i = 1; i < keys.length; i++)
28
+ if (!(keys[i].t > keys[i - 1].t))
29
+ throw new Error(`OrbitPath: key times must increase (key ${i})`);
30
+ this.keys = keys.map((k) => ({ ...k, target: [...k.target] }));
31
+ this.loop = options.loop ?? false;
32
+ const t0 = keys[0].t;
33
+ const last = keys[keys.length - 1].t;
34
+ const meanGap = keys.length > 1 ? (last - t0) / (keys.length - 1) : 1;
35
+ if (this.loop) {
36
+ const end = options.duration !== undefined ? t0 + options.duration : last + meanGap;
37
+ if (!(end > last))
38
+ throw new Error('OrbitPath: a looping path needs a duration beyond its last key');
39
+ this.duration = end - t0;
40
+ }
41
+ else
42
+ this.duration = last - t0;
43
+ // A loop gets the first key again at its end; its yaw continues from the last key.
44
+ const n = keys.length + (this.loop ? 1 : 0);
45
+ this.times = new Float64Array(n);
46
+ this.values = new Float64Array(n * CH);
47
+ this.slopes = new Float64Array(n * CH);
48
+ for (let i = 0; i < n; i++) {
49
+ const k = keys[i % keys.length];
50
+ this.times[i] = i < keys.length ? k.t - t0 : this.duration;
51
+ const v = i * CH;
52
+ this.values[v] = k.target[0];
53
+ this.values[v + 1] = k.target[1];
54
+ this.values[v + 2] = k.target[2];
55
+ // Shortest way round from the previous key: unwrap into ±180° of it.
56
+ const prev = i > 0 ? this.values[v - CH + 3] : k.yawDeg;
57
+ this.values[v + 3] = prev + wrapDeg(k.yawDeg - prev);
58
+ this.values[v + 4] = k.pitchDeg;
59
+ this.values[v + 5] = Math.log(Math.max(1e-6, k.distance));
60
+ }
61
+ // Turns of the yaw over one loop (a multiple of 360°): the neighbour before the first key is the last one, less it.
62
+ const turn = this.loop ? this.values[(n - 1) * CH + 3] - this.values[3] : 0;
63
+ for (let i = 0; i < n; i++) {
64
+ for (let c = 0; c < CH; c++) {
65
+ let slope = 0;
66
+ if (n > 1) {
67
+ const lo = i > 0 ? i - 1 : this.loop ? n - 2 : -1;
68
+ const hi = i < n - 1 ? i + 1 : this.loop ? 1 : -1;
69
+ // An open path rests at its ends.
70
+ if (lo >= 0 && hi >= 0) {
71
+ const tLo = i > 0 ? this.times[lo] : this.times[lo] - this.duration;
72
+ const tHi = i < n - 1 ? this.times[hi] : this.times[hi] + this.duration;
73
+ let vLo = this.values[lo * CH + c];
74
+ let vHi = this.values[hi * CH + c];
75
+ if (c === 3 && i === 0)
76
+ vLo -= turn;
77
+ if (c === 3 && i === n - 1)
78
+ vHi += turn;
79
+ const vi = this.values[i * CH + c];
80
+ slope = ((vi - vLo) / (this.times[i] - tLo) + (vHi - vi) / (tHi - this.times[i])) / 2;
81
+ }
82
+ }
83
+ this.slopes[i * CH + c] = slope;
84
+ }
85
+ }
86
+ }
87
+ /** A time folded into the path: modulo `duration` for a loop, clamped to `0 … duration` otherwise. */
88
+ wrap(t) {
89
+ if (!this.loop)
90
+ return clamp(t, 0, this.duration);
91
+ const d = this.duration;
92
+ return d > 0 ? t - Math.floor(t / d) * d : 0;
93
+ }
94
+ /** The state at time `t` (seconds from the start), written into `out`, which is returned. */
95
+ sample(t, out) {
96
+ const times = this.times;
97
+ const n = times.length;
98
+ const tt = this.wrap(t);
99
+ // The segment: the last key at or before the time (binary search, at most n − 2).
100
+ let lo = 0;
101
+ let hi = n - 1;
102
+ while (hi - lo > 1) {
103
+ const mid = (lo + hi) >> 1;
104
+ if (times[mid] <= tt)
105
+ lo = mid;
106
+ else
107
+ hi = mid;
108
+ }
109
+ const a = lo * CH;
110
+ const vs = this.values;
111
+ const ss = this.slopes;
112
+ if (n === 1) {
113
+ out.target[0] = vs[0];
114
+ out.target[1] = vs[1];
115
+ out.target[2] = vs[2];
116
+ out.yawDeg = vs[3];
117
+ out.pitchDeg = vs[4];
118
+ out.distance = Math.exp(vs[5]);
119
+ return out;
120
+ }
121
+ const b = hi * CH;
122
+ const h = times[hi] - times[lo];
123
+ const x = clamp((tt - times[lo]) / h, 0, 1);
124
+ const x2 = x * x;
125
+ const x3 = x2 * x;
126
+ const h00 = 2 * x3 - 3 * x2 + 1;
127
+ const h10 = x3 - 2 * x2 + x;
128
+ const h01 = -2 * x3 + 3 * x2;
129
+ const h11 = x3 - x2;
130
+ const r = this.channels;
131
+ for (let c = 0; c < CH; c++)
132
+ r[c] = h00 * vs[a + c] + h10 * h * ss[a + c] + h01 * vs[b + c] + h11 * h * ss[b + c];
133
+ out.target[0] = r[0];
134
+ out.target[1] = r[1];
135
+ out.target[2] = r[2];
136
+ out.yawDeg = r[3];
137
+ out.pitchDeg = r[4];
138
+ out.distance = Math.exp(r[5]);
139
+ return out;
140
+ }
141
+ /** Samples the path at `t` and writes it into the rig in place (see `writeOrbitState`). */
142
+ apply(rig, t) {
143
+ writeOrbitState(rig, this.sample(t, this.scratch));
144
+ }
145
+ }
146
+ /** An angle in degrees folded into −180 … 180. */
147
+ function wrapDeg(d) {
148
+ return d - 360 * Math.round(d / 360);
149
+ }
150
+ /**
151
+ * Writes a state into an orbit rig without allocating: the target is copied into the rig's own array, pitch and
152
+ * distance are clamped to the rig's setup as `setState` does.
153
+ */
154
+ export function writeOrbitState(rig, s) {
155
+ rig.target[0] = s.target[0];
156
+ rig.target[1] = s.target[1];
157
+ rig.target[2] = s.target[2];
158
+ rig.yaw = s.yawDeg * DEG;
159
+ rig.pitch = clamp(s.pitchDeg, rig.setup.minPitchDeg, rig.setup.maxPitchDeg) * DEG;
160
+ rig.distance = clamp(s.distance, rig.setup.minDistance, rig.setup.maxDistance);
161
+ }
162
+ /**
163
+ * Between two orbit states at `x` (0: `a`, 1: `b`), written into `out`: the target and pitch linearly, the yaw the
164
+ * shorter way round, the distance in log space. For blending from a free view into a path and back. `out` may be `a`
165
+ * or `b`.
166
+ */
167
+ export function blendOrbitState(a, b, x, out) {
168
+ const ax = a.target[0], ay = a.target[1], az = a.target[2];
169
+ const yaw = a.yawDeg + wrapDeg(b.yawDeg - a.yawDeg) * x;
170
+ const pitch = a.pitchDeg + (b.pitchDeg - a.pitchDeg) * x;
171
+ const distance = Math.exp(Math.log(Math.max(1e-6, a.distance)) * (1 - x) + Math.log(Math.max(1e-6, b.distance)) * x);
172
+ out.target[0] = ax + (b.target[0] - ax) * x;
173
+ out.target[1] = ay + (b.target[1] - ay) * x;
174
+ out.target[2] = az + (b.target[2] - az) * x;
175
+ out.yawDeg = yaw;
176
+ out.pitchDeg = pitch;
177
+ out.distance = distance;
178
+ return out;
179
+ }
@@ -0,0 +1,66 @@
1
+ import type { VertexAttrib } from '../base/data.ts';
2
+ /** Gravity of the tracks (m/s²). */
3
+ export declare const TRACK_GRAVITY = 9.81;
4
+ /** After landing a piece crumbles into the ground over this long. */
5
+ export declare const TRACK_CRUMBLE_S = 0.45;
6
+ /** Longest flight; a track that would fly longer lands at this time wherever it is. */
7
+ export declare const TRACK_MAX_FLIGHT_S = 4;
8
+ export interface BallisticTrack {
9
+ /** Clock time (seconds) at which the piece lets go; before it, the piece stands exactly where it is. */
10
+ start: number;
11
+ /** Pivot at rest (metres) and its initial velocity (m/s). */
12
+ x: number;
13
+ y: number;
14
+ z: number;
15
+ vx: number;
16
+ vy: number;
17
+ vz: number;
18
+ /** Unit axis of the turn and the turn rate (rad/s). */
19
+ ax: number;
20
+ ay: number;
21
+ az: number;
22
+ spin: number;
23
+ /** Height at which the pivot comes to rest. */
24
+ land: number;
25
+ /** Dust mode: the piece does not fly, it is gone when it lets go. */
26
+ dust?: boolean;
27
+ }
28
+ /** A dust cloud: centre, clock time it starts, size (metres). */
29
+ export interface DustPuff {
30
+ x: number;
31
+ y: number;
32
+ z: number;
33
+ start: number;
34
+ size: number;
35
+ }
36
+ /** Seconds of flight from letting go until the pivot reaches its landing height (0 in dust mode). */
37
+ export declare function trackFlightTime(t: BallisticTrack): number;
38
+ /** Seconds from letting go until nothing of the piece is left. */
39
+ export declare function trackLifetime(t: BallisticTrack): number;
40
+ export interface TrackPose {
41
+ /** Pivot position. */
42
+ x: number;
43
+ y: number;
44
+ z: number;
45
+ /** Turn about the track axis (radians). */
46
+ angle: number;
47
+ /** 0 while whole, rising to 1 while it crumbles after landing. */
48
+ crumble: number;
49
+ phase: 'waiting' | 'flying' | 'crumbling' | 'gone';
50
+ }
51
+ /** Pose of a track at clock time `now`. */
52
+ export declare function trackPose(t: BallisticTrack, now: number): TrackPose;
53
+ /**
54
+ * Floats of a track in a vertex: pivot and start, velocity and landing height, axis and spin, flight time, mode and a
55
+ * moving-only mark (a vertex with the mark is collapsed onto the pivot until the piece lets go).
56
+ */
57
+ export declare const TRACK_FLOATS = 16;
58
+ /** Vertex attributes of the track at byte `offset` of a vertex (names a_track0 … a_track3). */
59
+ export declare function trackAttribs(offset: number): VertexAttrib[];
60
+ /** Writes the track floats at `f32[at]`; `movingOnly` marks a vertex that is not drawn before the piece lets go. */
61
+ export declare function writeTrack(f32: Float32Array, at: number, t: BallisticTrack, movingOnly?: boolean): void;
62
+ /**
63
+ * GLSL of the tracks (needs `u_time` of the frame block): `trackPlace(p, n, a_track0, a_track1, a_track2, a_track3)`
64
+ * returns the moved position of a vertex at `p` and turns its normal `n`.
65
+ */
66
+ export declare const TRACK_GLSL: string;
@@ -0,0 +1,104 @@
1
+ // Closed-form tracks of transient rigid pieces: a piece stands still until it lets go, flies on a ballistic arc while
2
+ // it turns about a fixed axis through its pivot, and on landing crumbles (flattens and spreads into the ground) and is
3
+ // gone. A piece in dust mode stands still and is gone the moment it lets go (its place is taken by a dust puff).
4
+ // Everything is a function of the track and the clock time, with no state carried between frames, so pausing or
5
+ // scrubbing the clock shows the same pose every time, also back to before the piece landed.
6
+ //
7
+ // The vertex shader evaluates the same formulas (`TRACK_GLSL`); `trackPose` is the reference. A mesh that moves on
8
+ // tracks carries the track in every vertex: `TRACK_FLOATS` floats after its own attributes (`writeTrack`).
9
+ /** Gravity of the tracks (m/s²). */
10
+ export const TRACK_GRAVITY = 9.81;
11
+ /** After landing a piece crumbles into the ground over this long. */
12
+ export const TRACK_CRUMBLE_S = 0.45;
13
+ /** Longest flight; a track that would fly longer lands at this time wherever it is. */
14
+ export const TRACK_MAX_FLIGHT_S = 4;
15
+ /** Sideways spread of a crumbling piece at the end of its crumble (fraction of its size). */
16
+ const SPREAD = 0.35;
17
+ /** Seconds of flight from letting go until the pivot reaches its landing height (0 in dust mode). */
18
+ export function trackFlightTime(t) {
19
+ if (t.dust)
20
+ return 0;
21
+ const a = TRACK_GRAVITY / 2;
22
+ const drop = t.y - t.land;
23
+ if (drop <= 0 && t.vy <= 0)
24
+ return 0;
25
+ const disc = t.vy * t.vy + 4 * a * drop;
26
+ const tl = (t.vy + Math.sqrt(Math.max(0, disc))) / (2 * a);
27
+ return Math.min(TRACK_MAX_FLIGHT_S, Math.max(0, tl));
28
+ }
29
+ /** Seconds from letting go until nothing of the piece is left. */
30
+ export function trackLifetime(t) {
31
+ return t.dust ? 0 : trackFlightTime(t) + TRACK_CRUMBLE_S;
32
+ }
33
+ /** Pose of a track at clock time `now`. */
34
+ export function trackPose(t, now) {
35
+ const age = now - t.start;
36
+ if (age <= 0)
37
+ return { x: t.x, y: t.y, z: t.z, angle: 0, crumble: 0, phase: 'waiting' };
38
+ if (t.dust)
39
+ return { x: t.x, y: t.y, z: t.z, angle: 0, crumble: 1, phase: 'gone' };
40
+ const fl = trackFlightTime(t);
41
+ const tau = Math.min(age, fl);
42
+ const k = Math.min(1, Math.max(0, (age - fl) / TRACK_CRUMBLE_S));
43
+ const x = t.x + t.vx * tau;
44
+ const z = t.z + t.vz * tau;
45
+ const y = Math.max(t.land, t.y + t.vy * tau - (TRACK_GRAVITY / 2) * tau * tau) - 0.4 * k;
46
+ const phase = age < fl ? 'flying' : k < 1 ? 'crumbling' : 'gone';
47
+ return { x, y, z, angle: t.spin * tau, crumble: k, phase };
48
+ }
49
+ /**
50
+ * Floats of a track in a vertex: pivot and start, velocity and landing height, axis and spin, flight time, mode and a
51
+ * moving-only mark (a vertex with the mark is collapsed onto the pivot until the piece lets go).
52
+ */
53
+ export const TRACK_FLOATS = 16;
54
+ /** Vertex attributes of the track at byte `offset` of a vertex (names a_track0 … a_track3). */
55
+ export function trackAttribs(offset) {
56
+ return [0, 1, 2, 3].map((k) => ({ name: `a_track${k}`, size: 4, type: 'f32', offset: offset + k * 16 }));
57
+ }
58
+ /** Writes the track floats at `f32[at]`; `movingOnly` marks a vertex that is not drawn before the piece lets go. */
59
+ export function writeTrack(f32, at, t, movingOnly = false) {
60
+ f32[at] = t.x;
61
+ f32[at + 1] = t.y;
62
+ f32[at + 2] = t.z;
63
+ f32[at + 3] = t.start;
64
+ f32[at + 4] = t.vx;
65
+ f32[at + 5] = t.vy;
66
+ f32[at + 6] = t.vz;
67
+ f32[at + 7] = t.land;
68
+ f32[at + 8] = t.ax;
69
+ f32[at + 9] = t.ay;
70
+ f32[at + 10] = t.az;
71
+ f32[at + 11] = t.spin;
72
+ f32[at + 12] = trackFlightTime(t);
73
+ f32[at + 13] = t.dust ? 1 : 0;
74
+ f32[at + 14] = movingOnly ? 1 : 0;
75
+ f32[at + 15] = 0;
76
+ }
77
+ const g = (v) => v.toFixed(4);
78
+ /**
79
+ * GLSL of the tracks (needs `u_time` of the frame block): `trackPlace(p, n, a_track0, a_track1, a_track2, a_track3)`
80
+ * returns the moved position of a vertex at `p` and turns its normal `n`.
81
+ */
82
+ export const TRACK_GLSL = /* glsl */ `
83
+ vec3 trackTurn(vec3 v, vec3 k, float a) {
84
+ float c = cos(a), s = sin(a);
85
+ return v * c + cross(k, v) * s + k * dot(k, v) * (1.0 - c);
86
+ }
87
+ vec3 trackPlace(vec3 p, inout vec3 n, vec4 t0, vec4 t1, vec4 t2, vec4 t3) {
88
+ float age = u_time.x - t0.w;
89
+ if (age <= 0.0) return t3.z > 0.5 ? t0.xyz : p;
90
+ if (t3.y > 0.5) return t0.xyz;
91
+ float fl = t3.x;
92
+ float tau = min(age, fl);
93
+ vec3 c = t0.xyz + t1.xyz * tau;
94
+ c.y = max(t1.w, t0.y + t1.y * tau - ${g(TRACK_GRAVITY / 2)} * tau * tau);
95
+ float ang = t2.w * tau;
96
+ vec3 local = trackTurn(p - t0.xyz, t2.xyz, ang);
97
+ n = trackTurn(n, t2.xyz, ang);
98
+ float k = clamp((age - fl) / ${g(TRACK_CRUMBLE_S)}, 0.0, 1.0);
99
+ if (k >= 1.0) return c;
100
+ local *= vec3(1.0 + ${g(SPREAD)} * k, 1.0 - k, 1.0 + ${g(SPREAD)} * k);
101
+ c.y -= 0.4 * k;
102
+ return c + local;
103
+ }
104
+ `;