@open-take/compositor 0.5.3 → 0.6.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 (439) hide show
  1. package/dist/cdp-CzT5BKNY.js +3 -0
  2. package/dist/cdp-QXbQfXna.js +373 -0
  3. package/dist/cdp-QXbQfXna.js.map +1 -0
  4. package/dist/index.d.ts +4327 -195
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +26282 -160
  7. package/dist/index.js.map +1 -1
  8. package/dist/server-841PMF2H.js +163 -0
  9. package/dist/server-841PMF2H.js.map +1 -0
  10. package/dist/server-C2aVv4-t.js +3 -0
  11. package/kit/audio.mjs +2366 -0
  12. package/kit/camera.js +587 -0
  13. package/kit/cursor.js +264 -0
  14. package/kit/cut.js +427 -0
  15. package/kit/draw.js +346 -0
  16. package/kit/look.js +589 -0
  17. package/kit/map.js +170 -0
  18. package/kit/media.js +3568 -0
  19. package/kit/motion.js +1115 -0
  20. package/kit/seams.js +1006 -0
  21. package/kit/stage.js +923 -0
  22. package/kit/time.js +812 -0
  23. package/kit/world.js +1072 -0
  24. package/package.json +7 -3
  25. package/src/ffmpeg.ts +63 -0
  26. package/src/film/beats.ts +1285 -0
  27. package/src/film/carry.ts +221 -0
  28. package/src/film/changes.ts +330 -0
  29. package/src/film/check.ts +996 -0
  30. package/src/film/clock.ts +2530 -0
  31. package/src/film/components-probe.ts +809 -0
  32. package/src/film/components.ts +1566 -0
  33. package/src/film/frames.ts +389 -0
  34. package/src/film/gallery.ts +3276 -0
  35. package/src/film/kit.ts +515 -0
  36. package/src/film/layout.ts +701 -0
  37. package/src/film/listen.ts +1979 -0
  38. package/src/film/look.ts +3829 -0
  39. package/src/film/materials.ts +976 -0
  40. package/src/film/moving.ts +248 -0
  41. package/src/film/part-files.ts +729 -0
  42. package/src/film/picture.ts +74 -0
  43. package/src/film/probe-text.ts +843 -0
  44. package/src/film/probe.ts +3456 -0
  45. package/src/film/provenance.ts +596 -0
  46. package/src/film/render.ts +2447 -0
  47. package/src/film/report.ts +1631 -0
  48. package/src/film/sheet.ts +156 -0
  49. package/src/film/study-declared.ts +123 -0
  50. package/src/film/study-picture.ts +791 -0
  51. package/src/film/study-sound.ts +416 -0
  52. package/src/film/study.ts +864 -0
  53. package/src/film/tuning.ts +155 -0
  54. package/src/film/viewer.ts +174 -0
  55. package/src/html/cdp.ts +575 -0
  56. package/src/html/server.ts +208 -0
  57. package/src/index.ts +202 -0
  58. package/src/launch-render.ts +3 -2
  59. package/src/math.ts +3 -3
  60. package/src/mjpeg-matroska.ts +228 -0
  61. package/src/render.ts +6 -4
  62. package/src/state-diff.ts +215 -0
  63. package/src/state-index.ts +99 -0
  64. package/src/types.ts +59 -1
  65. package/starter/PLAN.md +84 -0
  66. package/starter/START.md +460 -0
  67. package/starter/lib/anim.js +760 -0
  68. package/starter/lib/devices.js +1402 -0
  69. package/starter/lib/handoff.js +421 -0
  70. package/starter/lib/hud.js +1879 -0
  71. package/starter/lib/measure.mjs +154 -0
  72. package/starter/lib/moves.js +1095 -0
  73. package/starter/lib/palette.mjs +803 -0
  74. package/starter/lib/physics-seeds.mjs +198 -0
  75. package/starter/lib/physics.js +738 -0
  76. package/starter/lib/sampler.mjs +242 -0
  77. package/starter/lib/solid.js +727 -0
  78. package/starter/lib/space.js +1227 -0
  79. package/starter/lib/swarm.js +1398 -0
  80. package/starter/reference/motion.md +276 -0
  81. package/starter/reference/structure.md +466 -0
  82. package/starter/reference/travel/motion.md +433 -0
  83. package/starter/reference/travel/sound.md +241 -0
  84. package/starter/reference/travel/structure.md +518 -0
  85. package/starter/samples/NOTICE.txt +55 -0
  86. package/starter/samples/celli/celli_24_1.opus +0 -0
  87. package/starter/samples/celli/celli_24_3.opus +0 -0
  88. package/starter/samples/celli/celli_28_1.opus +0 -0
  89. package/starter/samples/celli/celli_28_3.opus +0 -0
  90. package/starter/samples/celli/celli_31_1.opus +0 -0
  91. package/starter/samples/celli/celli_31_3.opus +0 -0
  92. package/starter/samples/celli/celli_35_1.opus +0 -0
  93. package/starter/samples/celli/celli_35_3.opus +0 -0
  94. package/starter/samples/celli/celli_38_1.opus +0 -0
  95. package/starter/samples/celli/celli_38_3.opus +0 -0
  96. package/starter/samples/celli/celli_41_1.opus +0 -0
  97. package/starter/samples/celli/celli_41_3.opus +0 -0
  98. package/starter/samples/celli/celli_45_1.opus +0 -0
  99. package/starter/samples/celli/celli_45_3.opus +0 -0
  100. package/starter/samples/celli/celli_48_1.opus +0 -0
  101. package/starter/samples/celli/celli_48_3.opus +0 -0
  102. package/starter/samples/celli/celli_52_1.opus +0 -0
  103. package/starter/samples/celli/celli_52_3.opus +0 -0
  104. package/starter/samples/celli/celli_55_1.opus +0 -0
  105. package/starter/samples/celli/celli_55_3.opus +0 -0
  106. package/starter/samples/celli/celli_59_1.opus +0 -0
  107. package/starter/samples/celli/celli_59_3.opus +0 -0
  108. package/starter/samples/celli/celli_62_1.opus +0 -0
  109. package/starter/samples/celli/celli_62_3.opus +0 -0
  110. package/starter/samples/celli/celli_65_1.opus +0 -0
  111. package/starter/samples/celli/celli_65_3.opus +0 -0
  112. package/starter/samples/crash/crash_f_1.flac +0 -0
  113. package/starter/samples/crash/crash_fff_1.flac +0 -0
  114. package/starter/samples/ebass/ebass_0_x.flac +0 -0
  115. package/starter/samples/ebass/ebass_10_x.flac +0 -0
  116. package/starter/samples/ebass/ebass_11_x.flac +0 -0
  117. package/starter/samples/ebass/ebass_1_x.flac +0 -0
  118. package/starter/samples/ebass/ebass_2_x.flac +0 -0
  119. package/starter/samples/ebass/ebass_3_x.flac +0 -0
  120. package/starter/samples/ebass/ebass_4_x.flac +0 -0
  121. package/starter/samples/ebass/ebass_5_x.flac +0 -0
  122. package/starter/samples/ebass/ebass_6_x.flac +0 -0
  123. package/starter/samples/ebass/ebass_7_x.flac +0 -0
  124. package/starter/samples/ebass/ebass_8_x.flac +0 -0
  125. package/starter/samples/ebass/ebass_9_x.flac +0 -0
  126. package/starter/samples/ek_clap/ek_clap_1_1.flac +0 -0
  127. package/starter/samples/ek_hat/ek_hat_1_1.flac +0 -0
  128. package/starter/samples/ek_hat/ek_hat_1_2.flac +0 -0
  129. package/starter/samples/ek_kick/ek_kick_1_1.flac +0 -0
  130. package/starter/samples/ek_kick/ek_kick_1_2.flac +0 -0
  131. package/starter/samples/glock/glock_67_x.flac +0 -0
  132. package/starter/samples/glock/glock_72_x.flac +0 -0
  133. package/starter/samples/glock/glock_79_x.flac +0 -0
  134. package/starter/samples/glock/glock_84_x.flac +0 -0
  135. package/starter/samples/glock/glock_91_x.flac +0 -0
  136. package/starter/samples/glock/glock_96_x.flac +0 -0
  137. package/starter/samples/harp/harp_101_f.opus +0 -0
  138. package/starter/samples/harp/harp_28_f.opus +0 -0
  139. package/starter/samples/harp/harp_31_mp.opus +0 -0
  140. package/starter/samples/harp/harp_35_mf.opus +0 -0
  141. package/starter/samples/harp/harp_38_mf.opus +0 -0
  142. package/starter/samples/harp/harp_41_mf.opus +0 -0
  143. package/starter/samples/harp/harp_45_mf.opus +0 -0
  144. package/starter/samples/harp/harp_48_mf.opus +0 -0
  145. package/starter/samples/harp/harp_52_mf.opus +0 -0
  146. package/starter/samples/harp/harp_55_mf.opus +0 -0
  147. package/starter/samples/harp/harp_59_mf.opus +0 -0
  148. package/starter/samples/harp/harp_62_mf.opus +0 -0
  149. package/starter/samples/harp/harp_65_mf.opus +0 -0
  150. package/starter/samples/harp/harp_69_mf.opus +0 -0
  151. package/starter/samples/harp/harp_72_mf.opus +0 -0
  152. package/starter/samples/harp/harp_76_mf.opus +0 -0
  153. package/starter/samples/harp/harp_79_mf.opus +0 -0
  154. package/starter/samples/harp/harp_83_mf.opus +0 -0
  155. package/starter/samples/harp/harp_86_mf.opus +0 -0
  156. package/starter/samples/harp/harp_89_mf.opus +0 -0
  157. package/starter/samples/harp/harp_93_mf.opus +0 -0
  158. package/starter/samples/harp/harp_95_mf.opus +0 -0
  159. package/starter/samples/harp/harp_98_f.opus +0 -0
  160. package/starter/samples/manifest.json +2951 -0
  161. package/starter/samples/marimba/marimba_29_x.flac +0 -0
  162. package/starter/samples/marimba/marimba_36_x.flac +0 -0
  163. package/starter/samples/marimba/marimba_43_x.flac +0 -0
  164. package/starter/samples/marimba/marimba_47_x.flac +0 -0
  165. package/starter/samples/marimba/marimba_53_x.flac +0 -0
  166. package/starter/samples/marimba/marimba_60_x.flac +0 -0
  167. package/starter/samples/marimba/marimba_67_x.flac +0 -0
  168. package/starter/samples/marimba/marimba_71_x.flac +0 -0
  169. package/starter/samples/marimba/marimba_77_x.flac +0 -0
  170. package/starter/samples/marimba/marimba_84_x.flac +0 -0
  171. package/starter/samples/piano/piano_102_12.opus +0 -0
  172. package/starter/samples/piano/piano_102_16.opus +0 -0
  173. package/starter/samples/piano/piano_102_4.opus +0 -0
  174. package/starter/samples/piano/piano_102_8.opus +0 -0
  175. package/starter/samples/piano/piano_105_12.opus +0 -0
  176. package/starter/samples/piano/piano_105_16.opus +0 -0
  177. package/starter/samples/piano/piano_105_4.opus +0 -0
  178. package/starter/samples/piano/piano_105_8.opus +0 -0
  179. package/starter/samples/piano/piano_108_12.opus +0 -0
  180. package/starter/samples/piano/piano_108_16.opus +0 -0
  181. package/starter/samples/piano/piano_108_4.opus +0 -0
  182. package/starter/samples/piano/piano_108_8.opus +0 -0
  183. package/starter/samples/piano/piano_21_12.opus +0 -0
  184. package/starter/samples/piano/piano_21_16.opus +0 -0
  185. package/starter/samples/piano/piano_21_4.opus +0 -0
  186. package/starter/samples/piano/piano_21_8.opus +0 -0
  187. package/starter/samples/piano/piano_24_12.opus +0 -0
  188. package/starter/samples/piano/piano_24_16.opus +0 -0
  189. package/starter/samples/piano/piano_24_4.opus +0 -0
  190. package/starter/samples/piano/piano_24_8.opus +0 -0
  191. package/starter/samples/piano/piano_27_12.opus +0 -0
  192. package/starter/samples/piano/piano_27_16.opus +0 -0
  193. package/starter/samples/piano/piano_27_4.opus +0 -0
  194. package/starter/samples/piano/piano_27_8.opus +0 -0
  195. package/starter/samples/piano/piano_30_12.opus +0 -0
  196. package/starter/samples/piano/piano_30_16.opus +0 -0
  197. package/starter/samples/piano/piano_30_4.opus +0 -0
  198. package/starter/samples/piano/piano_30_8.opus +0 -0
  199. package/starter/samples/piano/piano_33_12.opus +0 -0
  200. package/starter/samples/piano/piano_33_16.opus +0 -0
  201. package/starter/samples/piano/piano_33_4.opus +0 -0
  202. package/starter/samples/piano/piano_33_8.opus +0 -0
  203. package/starter/samples/piano/piano_36_12.opus +0 -0
  204. package/starter/samples/piano/piano_36_16.opus +0 -0
  205. package/starter/samples/piano/piano_36_4.opus +0 -0
  206. package/starter/samples/piano/piano_36_8.opus +0 -0
  207. package/starter/samples/piano/piano_39_12.opus +0 -0
  208. package/starter/samples/piano/piano_39_16.opus +0 -0
  209. package/starter/samples/piano/piano_39_4.opus +0 -0
  210. package/starter/samples/piano/piano_39_8.opus +0 -0
  211. package/starter/samples/piano/piano_42_12.opus +0 -0
  212. package/starter/samples/piano/piano_42_16.opus +0 -0
  213. package/starter/samples/piano/piano_42_4.opus +0 -0
  214. package/starter/samples/piano/piano_42_8.opus +0 -0
  215. package/starter/samples/piano/piano_45_12.opus +0 -0
  216. package/starter/samples/piano/piano_45_16.opus +0 -0
  217. package/starter/samples/piano/piano_45_4.opus +0 -0
  218. package/starter/samples/piano/piano_45_8.opus +0 -0
  219. package/starter/samples/piano/piano_48_12.opus +0 -0
  220. package/starter/samples/piano/piano_48_16.opus +0 -0
  221. package/starter/samples/piano/piano_48_4.opus +0 -0
  222. package/starter/samples/piano/piano_48_8.opus +0 -0
  223. package/starter/samples/piano/piano_51_12.opus +0 -0
  224. package/starter/samples/piano/piano_51_16.opus +0 -0
  225. package/starter/samples/piano/piano_51_4.opus +0 -0
  226. package/starter/samples/piano/piano_51_8.opus +0 -0
  227. package/starter/samples/piano/piano_54_12.opus +0 -0
  228. package/starter/samples/piano/piano_54_16.opus +0 -0
  229. package/starter/samples/piano/piano_54_4.opus +0 -0
  230. package/starter/samples/piano/piano_54_8.opus +0 -0
  231. package/starter/samples/piano/piano_57_12.opus +0 -0
  232. package/starter/samples/piano/piano_57_16.opus +0 -0
  233. package/starter/samples/piano/piano_57_4.opus +0 -0
  234. package/starter/samples/piano/piano_57_8.opus +0 -0
  235. package/starter/samples/piano/piano_60_12.opus +0 -0
  236. package/starter/samples/piano/piano_60_16.opus +0 -0
  237. package/starter/samples/piano/piano_60_4.opus +0 -0
  238. package/starter/samples/piano/piano_60_8.opus +0 -0
  239. package/starter/samples/piano/piano_63_12.opus +0 -0
  240. package/starter/samples/piano/piano_63_16.opus +0 -0
  241. package/starter/samples/piano/piano_63_4.opus +0 -0
  242. package/starter/samples/piano/piano_63_8.opus +0 -0
  243. package/starter/samples/piano/piano_66_12.opus +0 -0
  244. package/starter/samples/piano/piano_66_16.opus +0 -0
  245. package/starter/samples/piano/piano_66_4.opus +0 -0
  246. package/starter/samples/piano/piano_66_8.opus +0 -0
  247. package/starter/samples/piano/piano_69_12.opus +0 -0
  248. package/starter/samples/piano/piano_69_16.opus +0 -0
  249. package/starter/samples/piano/piano_69_4.opus +0 -0
  250. package/starter/samples/piano/piano_69_8.opus +0 -0
  251. package/starter/samples/piano/piano_72_12.opus +0 -0
  252. package/starter/samples/piano/piano_72_16.opus +0 -0
  253. package/starter/samples/piano/piano_72_4.opus +0 -0
  254. package/starter/samples/piano/piano_72_8.opus +0 -0
  255. package/starter/samples/piano/piano_75_12.opus +0 -0
  256. package/starter/samples/piano/piano_75_16.opus +0 -0
  257. package/starter/samples/piano/piano_75_4.opus +0 -0
  258. package/starter/samples/piano/piano_75_8.opus +0 -0
  259. package/starter/samples/piano/piano_78_12.opus +0 -0
  260. package/starter/samples/piano/piano_78_16.opus +0 -0
  261. package/starter/samples/piano/piano_78_4.opus +0 -0
  262. package/starter/samples/piano/piano_78_8.opus +0 -0
  263. package/starter/samples/piano/piano_81_12.opus +0 -0
  264. package/starter/samples/piano/piano_81_16.opus +0 -0
  265. package/starter/samples/piano/piano_81_4.opus +0 -0
  266. package/starter/samples/piano/piano_81_8.opus +0 -0
  267. package/starter/samples/piano/piano_84_12.opus +0 -0
  268. package/starter/samples/piano/piano_84_16.opus +0 -0
  269. package/starter/samples/piano/piano_84_4.opus +0 -0
  270. package/starter/samples/piano/piano_84_8.opus +0 -0
  271. package/starter/samples/piano/piano_87_12.opus +0 -0
  272. package/starter/samples/piano/piano_87_16.opus +0 -0
  273. package/starter/samples/piano/piano_87_4.opus +0 -0
  274. package/starter/samples/piano/piano_87_8.opus +0 -0
  275. package/starter/samples/piano/piano_90_12.opus +0 -0
  276. package/starter/samples/piano/piano_90_16.opus +0 -0
  277. package/starter/samples/piano/piano_90_4.opus +0 -0
  278. package/starter/samples/piano/piano_90_8.opus +0 -0
  279. package/starter/samples/piano/piano_93_12.opus +0 -0
  280. package/starter/samples/piano/piano_93_16.opus +0 -0
  281. package/starter/samples/piano/piano_93_4.opus +0 -0
  282. package/starter/samples/piano/piano_93_8.opus +0 -0
  283. package/starter/samples/piano/piano_96_12.opus +0 -0
  284. package/starter/samples/piano/piano_96_16.opus +0 -0
  285. package/starter/samples/piano/piano_96_4.opus +0 -0
  286. package/starter/samples/piano/piano_96_8.opus +0 -0
  287. package/starter/samples/piano/piano_99_12.opus +0 -0
  288. package/starter/samples/piano/piano_99_16.opus +0 -0
  289. package/starter/samples/piano/piano_99_4.opus +0 -0
  290. package/starter/samples/piano/piano_99_8.opus +0 -0
  291. package/starter/samples/piano/piano_rel_102_x.opus +0 -0
  292. package/starter/samples/piano/piano_rel_105_x.opus +0 -0
  293. package/starter/samples/piano/piano_rel_108_x.opus +0 -0
  294. package/starter/samples/piano/piano_rel_21_x.opus +0 -0
  295. package/starter/samples/piano/piano_rel_24_x.opus +0 -0
  296. package/starter/samples/piano/piano_rel_27_x.opus +0 -0
  297. package/starter/samples/piano/piano_rel_30_x.opus +0 -0
  298. package/starter/samples/piano/piano_rel_33_x.opus +0 -0
  299. package/starter/samples/piano/piano_rel_36_x.opus +0 -0
  300. package/starter/samples/piano/piano_rel_39_x.opus +0 -0
  301. package/starter/samples/piano/piano_rel_42_x.opus +0 -0
  302. package/starter/samples/piano/piano_rel_45_x.opus +0 -0
  303. package/starter/samples/piano/piano_rel_48_x.opus +0 -0
  304. package/starter/samples/piano/piano_rel_51_x.opus +0 -0
  305. package/starter/samples/piano/piano_rel_54_x.opus +0 -0
  306. package/starter/samples/piano/piano_rel_57_x.opus +0 -0
  307. package/starter/samples/piano/piano_rel_60_x.opus +0 -0
  308. package/starter/samples/piano/piano_rel_63_x.opus +0 -0
  309. package/starter/samples/piano/piano_rel_66_x.opus +0 -0
  310. package/starter/samples/piano/piano_rel_69_x.opus +0 -0
  311. package/starter/samples/piano/piano_rel_72_x.opus +0 -0
  312. package/starter/samples/piano/piano_rel_75_x.opus +0 -0
  313. package/starter/samples/piano/piano_rel_78_x.opus +0 -0
  314. package/starter/samples/piano/piano_rel_81_x.opus +0 -0
  315. package/starter/samples/piano/piano_rel_84_x.opus +0 -0
  316. package/starter/samples/piano/piano_rel_87_x.opus +0 -0
  317. package/starter/samples/piano/piano_rel_90_x.opus +0 -0
  318. package/starter/samples/piano/piano_rel_93_x.opus +0 -0
  319. package/starter/samples/piano/piano_rel_96_x.opus +0 -0
  320. package/starter/samples/piano/piano_rel_99_x.opus +0 -0
  321. package/starter/samples/pizz/pizz_24_2.flac +0 -0
  322. package/starter/samples/pizz/pizz_28_2.flac +0 -0
  323. package/starter/samples/pizz/pizz_31_2.flac +0 -0
  324. package/starter/samples/pizz/pizz_35_2.flac +0 -0
  325. package/starter/samples/pizz/pizz_38_2.flac +0 -0
  326. package/starter/samples/pizz/pizz_41_2.flac +0 -0
  327. package/starter/samples/pizz/pizz_45_2.flac +0 -0
  328. package/starter/samples/pizz/pizz_48_2.flac +0 -0
  329. package/starter/samples/pizz/pizz_52_2.flac +0 -0
  330. package/starter/samples/pizz/pizz_57_2.flac +0 -0
  331. package/starter/samples/pizz/pizz_60_2.flac +0 -0
  332. package/starter/samples/pizz/pizz_64_2.flac +0 -0
  333. package/starter/samples/pizz/pizz_67_2.flac +0 -0
  334. package/starter/samples/pizz/pizz_71_2.flac +0 -0
  335. package/starter/samples/pizz/pizz_74_2.flac +0 -0
  336. package/starter/samples/pk_hat/pk_hat_2_1.flac +0 -0
  337. package/starter/samples/pk_hat/pk_hat_2_2.flac +0 -0
  338. package/starter/samples/pk_hat/pk_hat_6_1.flac +0 -0
  339. package/starter/samples/pk_hat/pk_hat_6_2.flac +0 -0
  340. package/starter/samples/pk_kick/pk_kick_3_1.flac +0 -0
  341. package/starter/samples/pk_kick/pk_kick_3_2.flac +0 -0
  342. package/starter/samples/pk_kick/pk_kick_7_1.flac +0 -0
  343. package/starter/samples/pk_kick/pk_kick_7_2.flac +0 -0
  344. package/starter/samples/pk_ohat/pk_ohat_3_1.flac +0 -0
  345. package/starter/samples/pk_ohat/pk_ohat_3_2.flac +0 -0
  346. package/starter/samples/pk_ohat/pk_ohat_6_1.flac +0 -0
  347. package/starter/samples/pk_ohat/pk_ohat_6_2.flac +0 -0
  348. package/starter/samples/pk_rim/pk_rim_2_1.flac +0 -0
  349. package/starter/samples/pk_rim/pk_rim_2_2.flac +0 -0
  350. package/starter/samples/pk_rim/pk_rim_5_1.flac +0 -0
  351. package/starter/samples/pk_rim/pk_rim_5_2.flac +0 -0
  352. package/starter/samples/pk_snare/pk_snare_3_1.flac +0 -0
  353. package/starter/samples/pk_snare/pk_snare_3_2.flac +0 -0
  354. package/starter/samples/pk_snare/pk_snare_7_1.flac +0 -0
  355. package/starter/samples/pk_snare/pk_snare_7_2.flac +0 -0
  356. package/starter/samples/shaker/shaker_1_1.flac +0 -0
  357. package/starter/samples/shaker/shaker_2_1.flac +0 -0
  358. package/starter/samples/snap/snap_r_1.flac +0 -0
  359. package/starter/samples/snap/snap_r_2.flac +0 -0
  360. package/starter/samples/snap/snap_r_3.flac +0 -0
  361. package/starter/samples/snap/snap_r_4.flac +0 -0
  362. package/starter/samples/snap/snap_r_5.flac +0 -0
  363. package/starter/samples/snap/snap_r_6.flac +0 -0
  364. package/starter/samples/tom/tom_3_1.flac +0 -0
  365. package/starter/samples/tom/tom_3_2.flac +0 -0
  366. package/starter/samples/tom/tom_4_1.flac +0 -0
  367. package/starter/samples/tom/tom_4_2.flac +0 -0
  368. package/starter/samples/vibes/vibes_41_1.opus +0 -0
  369. package/starter/samples/vibes/vibes_41_2.opus +0 -0
  370. package/starter/samples/vibes/vibes_45_1.opus +0 -0
  371. package/starter/samples/vibes/vibes_45_2.opus +0 -0
  372. package/starter/samples/vibes/vibes_48_1.opus +0 -0
  373. package/starter/samples/vibes/vibes_48_2.opus +0 -0
  374. package/starter/samples/vibes/vibes_52_1.opus +0 -0
  375. package/starter/samples/vibes/vibes_52_2.opus +0 -0
  376. package/starter/samples/vibes/vibes_55_1.opus +0 -0
  377. package/starter/samples/vibes/vibes_55_2.opus +0 -0
  378. package/starter/samples/vibes/vibes_59_1.opus +0 -0
  379. package/starter/samples/vibes/vibes_59_2.opus +0 -0
  380. package/starter/samples/vibes/vibes_62_1.opus +0 -0
  381. package/starter/samples/vibes/vibes_62_2.opus +0 -0
  382. package/starter/samples/vibes/vibes_65_1.opus +0 -0
  383. package/starter/samples/vibes/vibes_65_2.opus +0 -0
  384. package/starter/samples/vibes/vibes_69_1.opus +0 -0
  385. package/starter/samples/vibes/vibes_69_2.opus +0 -0
  386. package/starter/samples/vibes/vibes_72_1.opus +0 -0
  387. package/starter/samples/vibes/vibes_72_2.opus +0 -0
  388. package/starter/samples/vibes/vibes_76_1.opus +0 -0
  389. package/starter/samples/vibes/vibes_76_2.opus +0 -0
  390. package/starter/samples/violas/violas_36_1.opus +0 -0
  391. package/starter/samples/violas/violas_36_2.opus +0 -0
  392. package/starter/samples/violas/violas_38_1.opus +0 -0
  393. package/starter/samples/violas/violas_38_2.opus +0 -0
  394. package/starter/samples/violas/violas_40_1.opus +0 -0
  395. package/starter/samples/violas/violas_40_2.opus +0 -0
  396. package/starter/samples/violas/violas_43_1.opus +0 -0
  397. package/starter/samples/violas/violas_43_2.opus +0 -0
  398. package/starter/samples/violas/violas_47_1.opus +0 -0
  399. package/starter/samples/violas/violas_47_2.opus +0 -0
  400. package/starter/samples/violas/violas_50_1.opus +0 -0
  401. package/starter/samples/violas/violas_50_2.opus +0 -0
  402. package/starter/samples/violas/violas_53_1.opus +0 -0
  403. package/starter/samples/violas/violas_53_2.opus +0 -0
  404. package/starter/samples/violas/violas_57_1.opus +0 -0
  405. package/starter/samples/violas/violas_57_2.opus +0 -0
  406. package/starter/samples/violas/violas_60_1.opus +0 -0
  407. package/starter/samples/violas/violas_60_2.opus +0 -0
  408. package/starter/samples/violas/violas_64_1.opus +0 -0
  409. package/starter/samples/violas/violas_64_2.opus +0 -0
  410. package/starter/samples/violas/violas_67_1.opus +0 -0
  411. package/starter/samples/violas/violas_67_2.opus +0 -0
  412. package/starter/samples/violas/violas_71_1.opus +0 -0
  413. package/starter/samples/violas/violas_71_2.opus +0 -0
  414. package/starter/samples/violas/violas_74_1.opus +0 -0
  415. package/starter/samples/violas/violas_74_2.opus +0 -0
  416. package/starter/samples/violins/violins_43_1.opus +0 -0
  417. package/starter/samples/violins/violins_43_2.opus +0 -0
  418. package/starter/samples/violins/violins_45_1.opus +0 -0
  419. package/starter/samples/violins/violins_45_2.opus +0 -0
  420. package/starter/samples/violins/violins_47_1.opus +0 -0
  421. package/starter/samples/violins/violins_47_2.opus +0 -0
  422. package/starter/samples/violins/violins_50_1.opus +0 -0
  423. package/starter/samples/violins/violins_50_2.opus +0 -0
  424. package/starter/samples/violins/violins_54_1.opus +0 -0
  425. package/starter/samples/violins/violins_54_2.opus +0 -0
  426. package/starter/samples/violins/violins_57_1.opus +0 -0
  427. package/starter/samples/violins/violins_57_2.opus +0 -0
  428. package/starter/samples/violins/violins_60_1.opus +0 -0
  429. package/starter/samples/violins/violins_60_2.opus +0 -0
  430. package/starter/samples/violins/violins_64_1.opus +0 -0
  431. package/starter/samples/violins/violins_64_2.opus +0 -0
  432. package/starter/samples/violins/violins_67_1.opus +0 -0
  433. package/starter/samples/violins/violins_67_2.opus +0 -0
  434. package/starter/samples/violins/violins_71_1.opus +0 -0
  435. package/starter/samples/violins/violins_71_2.opus +0 -0
  436. package/starter/samples/violins/violins_74_1.opus +0 -0
  437. package/starter/samples/violins/violins_74_2.opus +0 -0
  438. package/starter/samples/woodblock/woodblock_f_1.flac +0 -0
  439. package/starter/samples/woodblock/woodblock_f_2.flac +0 -0
@@ -0,0 +1,738 @@
1
+ // physics.js — rigid bodies for a film page: simulated once when the page
2
+ // loads, then read back at any film time t. A scene is plain data (statics,
3
+ // bodies, each body's release time and kick); bake() steps an engine at a
4
+ // fixed step and records every body's pose, and sample() reads a pose at any
5
+ // t, between steps too (position lerped, rotation slerped). __render(t) only
6
+ // reads the bake, so a frame drawn twice, out of order, by another worker or
7
+ // between frames for the shutter comes out the same. Nothing here reads a
8
+ // clock or Math.random: everything random comes from rng(seed).
9
+ //
10
+ // The film brings its own engine, as files in its own folder (vendor/):
11
+ // Rapier: npm @dimforge/rapier3d-deterministic-compat (Apache-2.0), its
12
+ // dist/rapier.mjs — one ES module, the wasm inlined. Copy it as
13
+ // vendor/rapier.mjs (any name with "rapier" in it).
14
+ // ammo.js (Bullet, zlib): builds/ammo.wasm.js and builds/ammo.wasm.wasm of
15
+ // github.com/kripken/ammo.js, both in the same folder (the script fetches
16
+ // the .wasm from beside itself). Not the npm 0.0.10 package, which is a
17
+ // years-old asm.js build.
18
+ // This file imports neither (only lib/anim.js, for rng): engine(url) loads the
19
+ // one the film names, when it is called. The two take the same scene: boxes
20
+ // are rounded the same (r, 0.04 m unless said), damping means the same, and a
21
+ // pose is read at the same step; the paths still differ, since the solvers
22
+ // do. Rapier first: the shortest scenes, a build that is the same across
23
+ // machines. Neither settles a stack cleanly most of the time: of 20 seeds of
24
+ // 40 chips falling into a bar chart, 6 came to rest clean with Rapier and 7
25
+ // with ammo (settle(): the others still creeping, trembling a few millimetres
26
+ // in place, or a chip leaning). ammo's stacks drift less — counting only how
27
+ // far each chip got in the last 0.2 s, 17 against 10 — but tremble more.
28
+ // Same input, same numbers: each engine gave the same hash in one tab three
29
+ // times, in two fresh tabs, and in Node 22.
30
+ //
31
+ // Physics gets things somewhere believable; whether it got them somewhere
32
+ // readable is a matter of the seed. Run lib/physics-seeds.mjs over 20 seeds
33
+ // in Node (a few seconds) and write a clean one into the page.
34
+ //
35
+ // import { engine, bake, sample, publish, explode, landings, snap } from "./lib/physics.js";
36
+ // const E = await engine("./vendor/rapier.mjs"); // or "./vendor/ammo.wasm.js"
37
+ // const fall = bake(E.make(), myScene(7)); // at load, in __film.ready
38
+ // publish({ fall }); // window.__bake.hash: the renderer stops if two workers differ
39
+ // // in __render(t): a body not yet released is at its spawn pose — hide it
40
+ // mesh.visible = t >= scene.bodies[i].t;
41
+ // sample(fall, i, t, mesh.position, mesh.quaternion);
42
+ //
43
+ // rng(seed) a seeded random 0..1 (mulberry32)
44
+ // quat(x, y, z) Euler angles (rad, XYZ) as a quaternion [x, y, z, w]
45
+ // engine(url, kind?) loads the film's engine → { kind, module, make() → an adapter }
46
+ // adapters.rapier(R), .ammo(A) an engine's module as the five calls bake() makes
47
+ // bake(adapter, scene, dt?) → { frames, steps, n, dt, duration, hash }
48
+ // sample(baked, i, s, pos, quat) body i's pose at sim time s (written into pos / quat)
49
+ // fnv1a(f32, h?) a bake's hash (8 hex digits)
50
+ // publish(bakes) window.__bake = { hash, parts }: one hash over every bake
51
+ // settle(baked, o?) how it came to rest: speed and spin in the last 0.2 s, tilt, fallen through
52
+ // explode(parts, o?) a scene that blows parts out from where they belong (play it backwards;
53
+ // in the object's own coordinates: add where it stands after sample())
54
+ // byLayers(cells, o?) an assembly order: bottom layer first, round the axis within a layer
55
+ // landings(order, start, o?) each part's landing time: in pairs, 0.06 s apart
56
+ // snap(t, land, S, o?) the sim time of a part on the snap assembly timeline
57
+ //
58
+ // Scene: { gravity (m/s², along y; -25 reads as a table-top drop), duration (s),
59
+ // statics: [{ half: [hx, hy, hz], pos, quat?, friction? }], bodies: [{ t (when it
60
+ // is released; before that it is recorded at its spawn pose, so the page hides
61
+ // it), half, r? (corner radius, under the smallest half; 0.04 m, or a quarter
62
+ // of the smallest half when that is less; 0: sharp), shape? ("box" | "ball";
63
+ // a ball's radius is r), pos, quat, linvel?, angvel?, friction?, restitution?,
64
+ // linDamp?, angDamp? (per second: a speed falls as e^(-damp·t)), density? }] }. Metres,
65
+ // seconds, quaternions [x, y, z, w]. A bake is 7 floats a body a step: 40
66
+ // bodies for 2.9 s at 1/120 s is 391 KB, and costs 40–150 ms in Chrome (the
67
+ // first one more, while the engine's wasm warms up) — once per worker.
68
+ //
69
+ // Assembly: a burst baked from the assembled object and played backwards puts
70
+ // every part on the path it would fly out on. The reference's assembly (a
71
+ // wooden plane's parts in a launch film, 23.3–31.8 s) is not physics in time:
72
+ // parts hang still as an exploded view, then land one or two at a time, each
73
+ // crossing in 4 frames at 30 fps, ease in-out, no overshoot. snap() is that:
74
+ // physics gives the path and the tumble, the timeline gives the when.
75
+
76
+ import { rng } from "./anim.js";
77
+
78
+ /** @typedef {[number, number, number]} V3 */
79
+ /** @typedef {[number, number, number, number]} Q4 x, y, z, w */
80
+ /**
81
+ * @typedef {object} Static
82
+ * @property {V3} half half extents (m)
83
+ * @property {V3} pos
84
+ * @property {Q4} [quat]
85
+ * @property {number} [friction] 0.5
86
+ */
87
+ /**
88
+ * @typedef {object} Body
89
+ * @property {number} [t] when it is released (s); 0
90
+ * @property {V3} half half extents (m); a ball's are its radius each way
91
+ * @property {number} [r] corner radius, under the smallest half (a ball: its radius); see cornerOf
92
+ * @property {"box" | "ball"} [shape] "box"
93
+ * @property {V3} pos
94
+ * @property {Q4} [quat]
95
+ * @property {V3} [linvel]
96
+ * @property {V3} [angvel]
97
+ * @property {number} [friction] 0.5
98
+ * @property {number} [restitution] 0
99
+ * @property {number} [linDamp] per second: the speed falls as e^(-linDamp·t); 0
100
+ * @property {number} [angDamp] per second, the same for the spin; 0
101
+ * @property {number} [density] 1
102
+ */
103
+ /**
104
+ * @typedef {object} Scene
105
+ * @property {number} gravity along y (m/s²)
106
+ * @property {number} duration how long to simulate (s)
107
+ * @property {Static[]} statics
108
+ * @property {Body[]} bodies
109
+ */
110
+ /**
111
+ * @typedef {object} Adapter one engine's world, as the calls bake() makes
112
+ * @property {(g: number) => void} setGravity
113
+ * @property {(s: Static) => void} addStatic
114
+ * @property {(b: Body) => unknown} addBody → a handle read() takes
115
+ * @property {(dt: number) => void} step
116
+ * @property {(h: any, out: Float32Array, o: number) => void} read px py pz qx qy qz qw into out at o
117
+ * @property {() => void} [dispose]
118
+ */
119
+ /**
120
+ * @typedef {object} Baked
121
+ * @property {Float32Array} frames ((step * n) + i) * 7 → px py pz qx qy qz qw
122
+ * @property {number} steps steps after the first pose (frames hold steps + 1)
123
+ * @property {number} n bodies
124
+ * @property {number} dt the step (s)
125
+ * @property {number} duration steps * dt
126
+ * @property {string} hash fnv1a of frames
127
+ */
128
+ /** @typedef {{ set: (...v: number[]) => unknown } | number[] | Float32Array} Out a THREE.Vector3 / Quaternion, or an array */
129
+
130
+ /** the step: 1/120 s, two steps a 60 fps frame */
131
+ export const DT = 1 / 120;
132
+
133
+ /** a seeded random 0..1 (mulberry32), lib/anim.js's: `const r = rng(7); r()` */
134
+ export { rng };
135
+
136
+ /** Euler angles (rad, three.js's "XYZ": about X, then about the turned Y, then
137
+ * the turned Z — q = qx·qy·qz) as [x, y, z, w]
138
+ * @param {number} x @param {number} y @param {number} z @returns {Q4} */
139
+ export function quat(x, y, z) {
140
+ const c1 = Math.cos(x / 2), s1 = Math.sin(x / 2);
141
+ const c2 = Math.cos(y / 2), s2 = Math.sin(y / 2);
142
+ const c3 = Math.cos(z / 2), s3 = Math.sin(z / 2);
143
+ return [
144
+ s1 * c2 * c3 + c1 * s2 * s3,
145
+ c1 * s2 * c3 - s1 * c2 * s3,
146
+ c1 * c2 * s3 + s1 * s2 * c3,
147
+ c1 * c2 * c3 - s1 * s2 * s3,
148
+ ];
149
+ }
150
+
151
+ // ---------------------------------------------------------------- engines
152
+
153
+ /** which engine a file is, by its name @param {string} url @returns {"rapier" | "ammo"} */
154
+ function kindOf(url) {
155
+ if (/rapier/i.test(url)) return "rapier";
156
+ if (/ammo/i.test(url)) return "ammo";
157
+ throw new Error(`physics: can't tell which engine ${url} is — say engine(url, "rapier") or engine(url, "ammo")`);
158
+ }
159
+
160
+ /**
161
+ * Loads the engine the film brings, from a file in its folder (relative to
162
+ * the page), and gives back a maker of fresh worlds. Rapier is an ES module
163
+ * with its wasm inside; ammo is a classic script that fetches ammo.wasm.wasm
164
+ * from beside itself. In a browser only — lib/physics-seeds.mjs loads the same
165
+ * files in Node.
166
+ * `const E = await engine("./vendor/rapier3d-deterministic-compat.mjs"); bake(E.make(), scene)`
167
+ * @param {string} url the engine's file, relative to the page
168
+ * @param {"rapier" | "ammo"} [kind] which engine it is (default: from the file's name)
169
+ * @returns {Promise<{ kind: "rapier" | "ammo", module: any, make: () => Adapter }>}
170
+ */
171
+ export function engine(url, kind = kindOf(url)) {
172
+ const href = new URL(url, document.baseURI).href;
173
+ // once a page: ammo's script, once run, puts its module where its factory
174
+ // was (window.Ammo), so a second load would add the script and start the
175
+ // wasm over
176
+ let got = loaded.get(`${kind} ${href}`);
177
+ if (!got) {
178
+ got = load(href, kind);
179
+ got.catch(() => loaded.delete(`${kind} ${href}`));
180
+ loaded.set(`${kind} ${href}`, got);
181
+ }
182
+ return got;
183
+ }
184
+
185
+ /** @type {Map<string, Promise<{ kind: "rapier" | "ammo", module: any, make: () => Adapter }>>} */
186
+ const loaded = new Map();
187
+
188
+ /** @param {string} href @param {"rapier" | "ammo"} kind */
189
+ async function load(href, kind) {
190
+ if (kind === "rapier") {
191
+ const mod = await import(href);
192
+ const R = mod.default ?? mod;
193
+ await R.init?.();
194
+ return { kind, module: R, make: () => adapters.rapier(R) };
195
+ }
196
+ const w = /** @type {any} */ (window);
197
+ if (typeof w.Ammo !== "function") {
198
+ await new Promise((done, fail) => {
199
+ const s = document.createElement("script");
200
+ s.src = href;
201
+ s.onload = done;
202
+ s.onerror = () => fail(new Error(`physics: ammo didn't load from ${href}`));
203
+ document.head.append(s);
204
+ });
205
+ }
206
+ const A = await w.Ammo();
207
+ return { kind, module: A, make: () => adapters.ammo(A) };
208
+ }
209
+
210
+ /**
211
+ * A box's corner radius, the same in both engines: b.r, or 0.04 m (Bullet's
212
+ * own collision margin) but no more than a quarter of the smallest half. A
213
+ * radius that reaches the smallest half is not a box: say shape: "ball".
214
+ * @param {Body} b @returns {number}
215
+ */
216
+ export function cornerOf(b) {
217
+ const least = Math.min(...b.half);
218
+ const r = b.r ?? Math.min(0.04, least / 4);
219
+ if (!(r >= 0 && r < least))
220
+ throw new Error(
221
+ `physics: a box of half ${JSON.stringify(b.half)} can't have corner radius r ${b.r} — it must be at least 0 and under the smallest half (${least}); a box rounded that far is a ball (shape: "ball")`,
222
+ );
223
+ return r;
224
+ }
225
+
226
+ /** damping per second (a speed falls as e^(-c·t), Rapier's meaning) as
227
+ * Bullet's factor: Bullet keeps (1 - d)^dt of a speed each step and clamps d
228
+ * to 0..1, so a rate over 1 given straight would stop a body dead
229
+ * @param {number} c */
230
+ const bulletDamping = (c) => 1 - Math.exp(-Math.max(0, c));
231
+
232
+ /** Each engine's module as the five calls bake() makes. The film passes the
233
+ * module it loaded; nothing here imports one. */
234
+ export const adapters = {
235
+ /** @param {any} R Rapier, after `await R.init()` @returns {Adapter} */
236
+ rapier(R) {
237
+ const world = new R.World({ x: 0, y: -9.81, z: 0 });
238
+ /** @param {Q4 | undefined} q */
239
+ const rot = (q) => (q ? { x: q[0], y: q[1], z: q[2], w: q[3] } : { x: 0, y: 0, z: 0, w: 1 });
240
+ return {
241
+ setGravity(g) {
242
+ world.gravity = { x: 0, y: g, z: 0 };
243
+ },
244
+ addStatic(s) {
245
+ const [hx, hy, hz] = s.half;
246
+ world.createCollider(
247
+ R.ColliderDesc.cuboid(hx, hy, hz)
248
+ .setTranslation(...s.pos)
249
+ .setRotation(rot(s.quat))
250
+ .setFriction(s.friction ?? 0.5),
251
+ );
252
+ },
253
+ addBody(b) {
254
+ const lv = b.linvel ?? [0, 0, 0], av = b.angvel ?? [0, 0, 0];
255
+ const rb = world.createRigidBody(
256
+ R.RigidBodyDesc.dynamic()
257
+ .setTranslation(...b.pos)
258
+ .setRotation(rot(b.quat))
259
+ .setLinvel(lv[0], lv[1], lv[2])
260
+ .setAngvel({ x: av[0], y: av[1], z: av[2] })
261
+ .setLinearDamping(b.linDamp ?? 0)
262
+ .setAngularDamping(b.angDamp ?? 0)
263
+ .setCanSleep(false),
264
+ );
265
+ const [hx, hy, hz] = b.half;
266
+ const r = b.shape === "ball" ? 0 : cornerOf(b);
267
+ const desc =
268
+ b.shape === "ball"
269
+ ? R.ColliderDesc.ball(b.r || hx)
270
+ : r > 0
271
+ ? R.ColliderDesc.roundCuboid(hx - r, hy - r, hz - r, r) // Rapier's rounding is outside the core box
272
+ : R.ColliderDesc.cuboid(hx, hy, hz);
273
+ world.createCollider(
274
+ desc
275
+ .setFriction(b.friction ?? 0.5)
276
+ .setRestitution(b.restitution ?? 0)
277
+ .setDensity(b.density ?? 1),
278
+ rb,
279
+ );
280
+ return rb;
281
+ },
282
+ step(dt) {
283
+ world.timestep = dt;
284
+ world.step();
285
+ },
286
+ read(rb, out, o) {
287
+ const p = rb.translation(), q = rb.rotation();
288
+ out[o] = p.x; out[o + 1] = p.y; out[o + 2] = p.z;
289
+ out[o + 3] = q.x; out[o + 4] = q.y; out[o + 5] = q.z; out[o + 6] = q.w;
290
+ },
291
+ dispose() {
292
+ world.free();
293
+ },
294
+ };
295
+ },
296
+
297
+ /** @param {any} A ammo, after `await Ammo()` @returns {Adapter} */
298
+ ammo(A) {
299
+ const cfg = new A.btDefaultCollisionConfiguration();
300
+ const disp = new A.btCollisionDispatcher(cfg);
301
+ const bp = new A.btDbvtBroadphase();
302
+ const solver = new A.btSequentialImpulseConstraintSolver();
303
+ const world = new A.btDiscreteDynamicsWorld(disp, bp, solver, cfg);
304
+ /** @type {any[]} */ const made = [];
305
+ /** @type {any[]} */ const bodies = [];
306
+ /** @template T @param {T} x @returns {T} */
307
+ const keep = (x) => {
308
+ made.push(x);
309
+ return x;
310
+ };
311
+ /** @param {number} x @param {number} y @param {number} z */
312
+ const v = (x, y, z) => keep(new A.btVector3(x, y, z));
313
+ /** @param {number} mass @param {any} shape @param {V3} pos @param {Q4} q */
314
+ const rigid = (mass, shape, pos, q) => {
315
+ const t = keep(new A.btTransform());
316
+ t.setIdentity();
317
+ t.setOrigin(v(...pos));
318
+ t.setRotation(keep(new A.btQuaternion(...q)));
319
+ const inertia = v(0, 0, 0);
320
+ if (mass > 0) shape.calculateLocalInertia(mass, inertia);
321
+ const info = keep(new A.btRigidBodyConstructionInfo(mass, keep(new A.btDefaultMotionState(t)), shape, inertia));
322
+ const body = new A.btRigidBody(info);
323
+ bodies.push(body);
324
+ return body;
325
+ };
326
+ return {
327
+ setGravity(g) {
328
+ world.setGravity(v(0, g, 0));
329
+ },
330
+ addStatic(s) {
331
+ const body = rigid(0, keep(new A.btBoxShape(v(...s.half))), s.pos, s.quat ?? [0, 0, 0, 1]);
332
+ body.setFriction(s.friction ?? 0.5);
333
+ world.addRigidBody(body);
334
+ },
335
+ addBody(b) {
336
+ const [hx, hy, hz] = b.half;
337
+ // a box's collision margin is inside it and rounds its corners
338
+ const shape = keep(b.shape === "ball" ? new A.btSphereShape(b.r || hx) : new A.btBoxShape(v(hx, hy, hz)));
339
+ if (b.shape !== "ball") shape.setMargin(cornerOf(b));
340
+ const vol = b.shape === "ball" ? (4 / 3) * Math.PI * (b.r || hx) ** 3 : 8 * hx * hy * hz;
341
+ const body = rigid(vol * (b.density ?? 1), shape, b.pos, b.quat ?? [0, 0, 0, 1]);
342
+ body.setFriction(b.friction ?? 0.5);
343
+ body.setRestitution(b.restitution ?? 0);
344
+ body.setDamping(bulletDamping(b.linDamp ?? 0), bulletDamping(b.angDamp ?? 0));
345
+ body.setActivationState(4); // DISABLE_DEACTIVATION: nothing falls asleep mid-shot
346
+ body.setLinearVelocity(v(...(b.linvel ?? [0, 0, 0])));
347
+ body.setAngularVelocity(v(...(b.angvel ?? [0, 0, 0])));
348
+ world.addRigidBody(body);
349
+ return body;
350
+ },
351
+ step(dt) {
352
+ world.stepSimulation(dt, 1, dt); // one fixed step, nothing carried over
353
+ },
354
+ read(body, out, o) {
355
+ // the body's own transform: its motion state's is a step behind
356
+ const t = body.getWorldTransform();
357
+ const p = t.getOrigin(), q = t.getRotation();
358
+ out[o] = p.x(); out[o + 1] = p.y(); out[o + 2] = p.z();
359
+ out[o + 3] = q.x(); out[o + 4] = q.y(); out[o + 5] = q.z(); out[o + 6] = q.w();
360
+ },
361
+ dispose() {
362
+ // ammo's objects live on its heap until destroyed: the bodies out of the world first
363
+ for (const b of bodies) {
364
+ world.removeRigidBody(b);
365
+ A.destroy(b);
366
+ }
367
+ for (const x of [world, solver, bp, disp, cfg, ...made]) A.destroy(x);
368
+ },
369
+ };
370
+ },
371
+ };
372
+
373
+ // ---------------------------------------------------------------- bake and read
374
+
375
+ /**
376
+ * Steps a fresh world at a fixed dt and records every body's pose. The
377
+ * adapter is used up (disposed) by it: one make() per bake.
378
+ * @param {Adapter} adapter @param {Scene} scene @param {number} [dt] @returns {Baked}
379
+ */
380
+ export function bake(adapter, scene, dt = DT) {
381
+ const steps = Math.round(scene.duration / dt);
382
+ const n = scene.bodies.length;
383
+ const out = new Float32Array((steps + 1) * n * 7);
384
+ /** @type {unknown[]} */ const handles = new Array(n).fill(null);
385
+ /** @param {number} step */
386
+ const write = (step) => {
387
+ for (let i = 0; i < n; i++) {
388
+ const o = (step * n + i) * 7;
389
+ const h = handles[i];
390
+ if (h == null) {
391
+ const b = /** @type {Body} */ (scene.bodies[i]);
392
+ out.set(b.pos, o);
393
+ out.set(b.quat ?? [0, 0, 0, 1], o + 3);
394
+ } else adapter.read(h, out, o);
395
+ }
396
+ };
397
+ /** @param {number} time */
398
+ const release = (time) => {
399
+ for (let i = 0; i < n; i++) {
400
+ const b = /** @type {Body} */ (scene.bodies[i]);
401
+ if (handles[i] == null && (b.t ?? 0) <= time + 1e-9) handles[i] = adapter.addBody(b);
402
+ }
403
+ };
404
+ try {
405
+ adapter.setGravity(scene.gravity);
406
+ for (const s of scene.statics) adapter.addStatic(s);
407
+ release(0);
408
+ write(0);
409
+ for (let step = 1; step <= steps; step++) {
410
+ adapter.step(dt);
411
+ release(step * dt);
412
+ write(step);
413
+ }
414
+ } finally {
415
+ adapter.dispose?.();
416
+ }
417
+ return { frames: out, steps, n, dt, duration: steps * dt, hash: fnv1a(out) };
418
+ }
419
+
420
+ /** FNV-1a over a bake's bytes, as 8 hex digits; pass h to go on from another
421
+ * bake's (as a number: parseInt(hash, 16))
422
+ * @param {Float32Array} f32 @param {number} [h] @returns {string} */
423
+ export function fnv1a(f32, h = 0x811c9dc5) {
424
+ const u8 = new Uint8Array(f32.buffer, f32.byteOffset, f32.byteLength);
425
+ for (let i = 0; i < u8.length; i++) {
426
+ h ^= /** @type {number} */ (u8[i]);
427
+ h = Math.imul(h, 0x01000193);
428
+ }
429
+ return (h >>> 0).toString(16).padStart(8, "0");
430
+ }
431
+
432
+ /** @param {Out} o @param {number[]} v */
433
+ function put(o, v) {
434
+ if (Array.isArray(o) || ArrayBuffer.isView(o)) for (let k = 0; k < v.length; k++) /** @type {number[]} */ (o)[k] = /** @type {number} */ (v[k]);
435
+ else o.set(...v);
436
+ }
437
+
438
+ const P3 = [0, 0, 0], Q = [0, 0, 0, 1];
439
+
440
+ /**
441
+ * Body i's pose at sim time s, written into pos and quat (THREE.Vector3 and
442
+ * Quaternion, or arrays). Between two steps the position is lerped and the
443
+ * rotation slerped along the shorter arc; s is held inside the bake.
444
+ * `sample(fall, i, t, mesh.position, mesh.quaternion)`
445
+ * @param {Baked} b @param {number} i @param {number} s @param {Out} [pos] @param {Out} [quat]
446
+ * @returns {{ pos: number[], quat: number[] }} the same numbers (reused between calls: copy to keep)
447
+ */
448
+ export function sample(b, i, s, pos, quat) {
449
+ if (!(Number.isInteger(i) && i >= 0 && i < b.n)) throw new RangeError(`physics: sample() of body ${i}, in a bake of ${b.n} (0..${b.n - 1})`);
450
+ const k = Math.min(Math.max(s / b.dt, 0), b.steps);
451
+ const k0 = Math.min(Math.floor(k), Math.max(0, b.steps - 1)), f = b.steps ? k - k0 : 0;
452
+ const F = /** @type {any} */ (b.frames);
453
+ const o0 = (k0 * b.n + i) * 7, o1 = b.steps ? o0 + b.n * 7 : o0;
454
+ for (let c = 0; c < 3; c++) P3[c] = F[o0 + c] + (F[o1 + c] - F[o0 + c]) * f;
455
+ const ax = F[o0 + 3], ay = F[o0 + 4], az = F[o0 + 5], aw = F[o0 + 6];
456
+ let bx = F[o1 + 3], by = F[o1 + 4], bz = F[o1 + 5], bw = F[o1 + 6];
457
+ let d = ax * bx + ay * by + az * bz + aw * bw;
458
+ if (d < 0) {
459
+ bx = -bx;
460
+ by = -by;
461
+ bz = -bz;
462
+ bw = -bw;
463
+ d = -d;
464
+ }
465
+ let wa = 1 - f, wb = f;
466
+ if (d < 0.9995) {
467
+ const th = Math.acos(d), sn = Math.sin(th);
468
+ wa = Math.sin((1 - f) * th) / sn;
469
+ wb = Math.sin(f * th) / sn;
470
+ }
471
+ const x = ax * wa + bx * wb, y = ay * wa + by * wb, z = az * wa + bz * wb, w = aw * wa + bw * wb;
472
+ const len = Math.hypot(x, y, z, w) || 1;
473
+ Q[0] = x / len; Q[1] = y / len; Q[2] = z / len; Q[3] = w / len;
474
+ if (pos) put(pos, P3);
475
+ if (quat) put(quat, Q);
476
+ return { pos: P3, quat: Q };
477
+ }
478
+
479
+ /**
480
+ * Puts the bakes' hash on the page as window.__bake = { hash, parts }: one hash
481
+ * over every bake, in the order given, and each one's own. Every worker bakes
482
+ * for itself, so the renderer reads window.__bake.hash from each page it opens
483
+ * and stops when two differ (those workers would draw different films); a
484
+ * range rendered into an earlier master must come from the same hash too.
485
+ * Returns what it published; in Node it only returns it.
486
+ * `publish({ fall, burst })` → { hash: "1c0e…", parts: { fall: "fddeaa2b", burst: "1ec63dd0" } }
487
+ * @param {Record<string, Baked>} bakes @returns {{ hash: string, parts: Record<string, string> }}
488
+ */
489
+ export function publish(bakes) {
490
+ let h = 0x811c9dc5;
491
+ /** @type {Record<string, string>} */ const parts = {};
492
+ for (const [name, b] of Object.entries(bakes)) {
493
+ parts[name] = b.hash;
494
+ h = parseInt(fnv1a(b.frames, h), 16);
495
+ }
496
+ const info = { hash: (h >>> 0).toString(16).padStart(8, "0"), parts };
497
+ if (typeof window !== "undefined") /** @type {any} */ (window).__bake = info;
498
+ return info;
499
+ }
500
+
501
+ /** The farthest body `i` strays, in m, from the straight line fitted to its
502
+ * positions from step `from` to the last
503
+ * @param {any} F @param {Baked} b @param {number} i @param {number} from */
504
+ function stray(F, b, i, from) {
505
+ const n = b.steps - from + 1;
506
+ if (n < 3) return 0;
507
+ const mid = (n - 1) / 2;
508
+ let tt = 0;
509
+ for (let k = 0; k < n; k++) tt += (k - mid) ** 2;
510
+ /** @type {{ mean: number, slope: number }[]} */
511
+ const fit = [0, 1, 2].map((ax) => {
512
+ let mean = 0, slope = 0;
513
+ for (let k = 0; k < n; k++) mean += F[((from + k) * b.n + i) * 7 + ax];
514
+ mean /= n;
515
+ for (let k = 0; k < n; k++) slope += (k - mid) * (F[((from + k) * b.n + i) * 7 + ax] - mean);
516
+ return { mean, slope: slope / tt };
517
+ });
518
+ let far = 0;
519
+ for (let k = 0; k < n; k++) {
520
+ const p = ((from + k) * b.n + i) * 7;
521
+ far = Math.max(far, Math.hypot(...fit.map(({ mean, slope }, ax) => F[p + ax] - mean - slope * (k - mid))));
522
+ }
523
+ return far;
524
+ }
525
+
526
+ /**
527
+ * How a bake came to rest, from its last frames: each body's fastest speed and
528
+ * spin over any `over` s (a frame at 30 fps) of the last `window` s, and
529
+ * how far it strays from a steady line across that window over `over` (a
530
+ * tremble at 30 or 60 Hz is back in place at every frame's comparison) — so
531
+ * a body trembling in place, or turning on the spot, is not at rest, while
532
+ * the engine's own sub-millimetre jitter from one step to the next, which no
533
+ * film frame shows, does not count — its tilt (degrees between its own `up`
534
+ * axis and the world's: "y" — upright either way up, as a chip that looks the
535
+ * same both ways; "+y" — the right way up, as a logo; "any" — from the
536
+ * nearest face, so a box lying on any side is level), and whether it fell
537
+ * below `floor`. clean: everything still and level.
538
+ * `settle(fall).clean` — in 20 seeds of the bar chart, the unclean ones were a
539
+ * chip leaning on its neighbour (tilt), still creeping, or trembling a few
540
+ * millimetres about one place (speed).
541
+ * @param {Baked} b
542
+ * @param {{ window?: number, over?: number, still?: number, spin?: number, level?: number,
543
+ * up?: "x" | "y" | "z" | "+x" | "+y" | "+z" | "any", floor?: number }} [o]
544
+ * still: m/s under which a body is at rest (0.05); spin: rad/s, the same for
545
+ * its turning (0.2); level: degrees under which it is level (10)
546
+ */
547
+ export function settle(b, o = {}) {
548
+ const { window: win = 0.2, over = 1 / 30, still = 0.05, spin: calm = 0.2, level = 10, up = "y", floor = -Infinity } = o;
549
+ if (b.steps < 1) throw new Error("physics: settle() needs a bake of at least one step (its duration under dt)");
550
+ const F = /** @type {any} */ (b.frames);
551
+ const back = Math.min(b.steps, Math.max(1, Math.round(win / b.dt)));
552
+ const m = Math.min(back, Math.max(1, Math.round(over / b.dt)));
553
+ const last = b.steps * b.n * 7;
554
+ const bodies = [];
555
+ let maxSpeed = 0, maxSpin = 0, maxTilt = 0;
556
+ for (let i = 0; i < b.n; i++) {
557
+ const o1 = last + i * 7;
558
+ const pos = [F[o1], F[o1 + 1], F[o1 + 2]];
559
+ let speed = 0, spin = 0;
560
+ for (let k = b.steps - back; k + m <= b.steps; k++) {
561
+ const a = (k * b.n + i) * 7, c = a + m * b.n * 7;
562
+ speed = Math.max(speed, Math.hypot(F[c] - F[a], F[c + 1] - F[a + 1], F[c + 2] - F[a + 2]) / (m * b.dt));
563
+ const d = Math.abs(F[a + 3] * F[c + 3] + F[a + 4] * F[c + 4] + F[a + 5] * F[c + 5] + F[a + 6] * F[c + 6]);
564
+ spin = Math.max(spin, (2 * Math.acos(Math.min(1, d))) / (m * b.dt));
565
+ }
566
+ // A tremble whose period divides `over` is back where it was at each
567
+ // comparison. How far the body strays from a steady line through the
568
+ // window, as a speed over `over`, catches it: a creep is on the line,
569
+ // and the engine's sub-millimetre jitter strays too little to count.
570
+ speed = Math.max(speed, stray(F, b, i, b.steps - back) / over);
571
+ const [x, y, z, w] = [F[o1 + 3], F[o1 + 4], F[o1 + 5], F[o1 + 6]];
572
+ // the world-y component of the body's own x, y and z axes
573
+ const ups = { x: 2 * (x * y + w * z), y: 1 - 2 * (x * x + z * z), z: 2 * (y * z - w * x) };
574
+ const c =
575
+ up === "any"
576
+ ? Math.max(...Object.values(ups).map(Math.abs))
577
+ : up.startsWith("+")
578
+ ? ups[/** @type {"x" | "y" | "z"} */ (up[1])]
579
+ : Math.abs(ups[/** @type {"x" | "y" | "z"} */ (up)]);
580
+ const tilt = (Math.acos(Math.max(-1, Math.min(1, c))) * 180) / Math.PI;
581
+ maxSpeed = Math.max(maxSpeed, speed);
582
+ maxSpin = Math.max(maxSpin, spin);
583
+ maxTilt = Math.max(maxTilt, tilt);
584
+ bodies.push({ i, pos, speed: +speed.toFixed(4), spin: +spin.toFixed(4), tilt: +tilt.toFixed(2), fell: pos[1] < floor });
585
+ }
586
+ const moving = bodies.filter((x) => x.speed >= still || x.spin >= calm).map((x) => x.i);
587
+ const tilted = bodies.filter((x) => x.tilt >= level).map((x) => x.i);
588
+ const fell = bodies.filter((x) => x.fell).map((x) => x.i);
589
+ return {
590
+ clean: !moving.length && !tilted.length && !fell.length,
591
+ moving,
592
+ tilted,
593
+ fell,
594
+ maxSpeed: +maxSpeed.toFixed(4),
595
+ maxSpin: +maxSpin.toFixed(4),
596
+ maxTilt: +maxTilt.toFixed(2),
597
+ bodies,
598
+ };
599
+ }
600
+
601
+ // ---------------------------------------------------------------- assembly
602
+
603
+ /**
604
+ * A scene that blows parts out from where they belong, with no gravity: baked
605
+ * and played backwards (sim time running down to 0) it is the object
606
+ * assembling, each part arriving tumbling along its own path. Parts fly out
607
+ * from `center`, faster the further out they sit, and slow to a stop.
608
+ *
609
+ * Everything is in the object's own coordinates (parts' pos, center, floor):
610
+ * the page adds where the object stands after sample(). Nothing stops a part
611
+ * flying down, so an object on the ground would scatter through it: give
612
+ * `floor` (the ground's y, in the object's coordinates) and no part's flight
613
+ * ends below it (the downward ones are turned shallower, and a slab there
614
+ * catches any knocked down).
615
+ *
616
+ * How far out: the centres of a 3×3×3 cube of 0.5 m parts (1.54 m across)
617
+ * span about 3.4 times its size at snap()'s hold (0.36 of the burst), 3.7 at
618
+ * 0.45, 4.6 at the end — frame the camera for that, or give snap() a smaller
619
+ * hold.
620
+ * @param {{ pos: V3, half: V3, r?: number, quat?: Q4 }[]} parts each part at home
621
+ * @param {{ seed?: number, center?: V3, speed?: number, spin?: number, scatter?: number,
622
+ * linDamp?: number, angDamp?: number, duration?: number, floor?: number }} [o]
623
+ * speed m/s (5), spin rad/s (6), scatter m/s of sideways noise (1.2), damping 1.6 / 1.2 a second,
624
+ * duration 1.8 s
625
+ * @returns {Scene}
626
+ */
627
+ export function explode(parts, o = {}) {
628
+ const { seed = 11, center = [0, 0, 0], speed = 5, spin = 6, scatter = 1.2, linDamp = 1.6, angDamp = 1.2, duration = 1.8, floor } = o;
629
+ const r = rng(seed);
630
+ // how far a speed of 1 m/s carries a part, damped, in the burst
631
+ const reach = linDamp > 0 ? (1 - Math.exp(-linDamp * duration)) / linDamp : duration;
632
+ const bodies = parts.map((p) => {
633
+ const d = [p.pos[0] - center[0], p.pos[1] - center[1], p.pos[2] - center[2]];
634
+ const len = Math.hypot(...d) || 1;
635
+ const v = speed * (0.7 + 0.6 * r());
636
+ const linvel = /** @type {V3} */ (d.map((x) => (x / len) * v + (r() - 0.5) * scatter));
637
+ if (floor != null) {
638
+ const lowest = floor + Math.hypot(...p.half); // however it has turned
639
+ linvel[1] = Math.max(linvel[1], Math.min(0, (lowest - p.pos[1]) / reach));
640
+ }
641
+ return {
642
+ t: 0,
643
+ half: p.half,
644
+ r: p.r,
645
+ pos: p.pos,
646
+ quat: p.quat ?? /** @type {Q4} */ ([0, 0, 0, 1]),
647
+ linvel,
648
+ angvel: /** @type {V3} */ ([(r() - 0.5) * spin, (r() - 0.5) * spin, (r() - 0.5) * spin]),
649
+ friction: 0.3,
650
+ restitution: 0.3,
651
+ linDamp,
652
+ angDamp,
653
+ };
654
+ });
655
+ /** @type {Static[]} */
656
+ const statics = floor == null ? [] : [{ half: [50, 0.5, 50], pos: [center[0], floor - 0.5, center[2]] }];
657
+ return { gravity: 0, duration, statics, bodies };
658
+ }
659
+
660
+ /**
661
+ * An assembly order: the bottom layer first, then up, and within a layer
662
+ * round the vertical axis — a thing built from the ground. Returns indices.
663
+ * @param {V3[]} cells each part's home position (or grid cell)
664
+ * @param {{ axis?: 0 | 1 | 2, from?: number }} [o] axis: up (1, y); from: the angle (rad) the round starts at
665
+ * @returns {number[]}
666
+ */
667
+ export function byLayers(cells, o = {}) {
668
+ const { axis = 1, from = 0 } = o;
669
+ const [a, b] = /** @type {[number, number]} */ ([0, 1, 2].filter((k) => k !== axis));
670
+ /** @param {V3} c */
671
+ const ang = (c) => {
672
+ const x = Math.atan2(c[b] ?? 0, c[a] ?? 0) - from;
673
+ return ((x % (2 * Math.PI)) + 2 * Math.PI) % (2 * Math.PI);
674
+ };
675
+ const q = 1e-6;
676
+ return cells
677
+ .map((_, i) => i)
678
+ .sort((i, j) => {
679
+ const ci = /** @type {V3} */ (cells[i]), cj = /** @type {V3} */ (cells[j]);
680
+ const dy = Math.round(((ci[axis] ?? 0) - (cj[axis] ?? 0)) / q);
681
+ return dy || ang(ci) - ang(cj) || i - j;
682
+ });
683
+ }
684
+
685
+ /**
686
+ * Each part's landing time, in the order given: `pair` parts land together,
687
+ * the next pair `gap` s later. The reference's parts landed one or two at a
688
+ * time; 0.06 s apart keeps 26 parts under a second (13 pairs: 0.72 s).
689
+ * `const land = landings(byLayers(cells), 3.4)` → land[i] is part i's
690
+ * @param {number[]} order part indices, first to land first
691
+ * @param {number} start when the first pair lands (s)
692
+ * @param {{ pair?: number, gap?: number }} [o]
693
+ * @returns {number[]} indexed by part
694
+ */
695
+ export function landings(order, start, o = {}) {
696
+ const { pair = 2, gap = 0.06 } = o;
697
+ /** @type {number[]} */ const land = [];
698
+ order.forEach((i, rank) => {
699
+ land[i] = start + Math.floor(rank / Math.max(1, pair)) * gap;
700
+ });
701
+ return land;
702
+ }
703
+
704
+ /** @param {number} u */
705
+ const inOut3 = (u) => (u <= 0 ? 0 : u >= 1 ? 1 : u < 0.5 ? 4 * u ** 3 : 1 - (-2 * u + 2) ** 3 / 2);
706
+
707
+ /**
708
+ * The snap assembly: where along its reversed burst a part is at film time t,
709
+ * as a sim time to sample() the burst at (S: the burst's duration, its parts
710
+ * furthest out; 0: home). Before its landing the part hangs as an exploded
711
+ * view at `hold` of the burst (0.36: about 3.4 times the object's size, see
712
+ * explode()) — from film time `from` on, drifting inward
713
+ * `drift` (a share of S a second; 0.065 is 0.12 s of a 1.8 s burst) but never
714
+ * nearer than `near`; without `from` it hangs still — then it crosses the rest
715
+ * in `dur` s on an in-out cubic and is home, still, at land.
716
+ *
717
+ * Measured: the reference's wheel (a launch film's plane assembly, 23.4 s)
718
+ * crossed in 4 frames at 30 fps — about 30, 120, 65, 20 px a frame, slow in,
719
+ * fastest in the middle, slow out, no overshoot. dur 0.17 s on this cubic
720
+ * measures the same: 4 frames of visible motion at 30 fps (0.133 s),
721
+ * power1–2 in-out, 0 % overshoot (curve fit on a single part rendered alone).
722
+ * Pair it with landings(): the parts snapping in, two by two.
723
+ * `sample(burst, i, snap(t, land[i], burst.duration), m.position, m.quaternion)`
724
+ * @param {number} t film time (s)
725
+ * @param {number} land when this part is home (s)
726
+ * @param {number} S the burst's duration (s)
727
+ * @param {{ dur?: number, hold?: number, near?: number, drift?: number, from?: number, ease?: (u: number) => number }} [o]
728
+ * @returns {number}
729
+ */
730
+ export function snap(t, land, S, o = {}) {
731
+ const { dur = 0.17, hold = 0.36, near = 0.25, drift = 0.065, from, ease = inOut3 } = o;
732
+ /** @param {number} x */
733
+ const hover = (x) => S * Math.max(near, hold - (from == null ? 0 : drift * Math.max(0, x - from)));
734
+ const t1 = land - dur;
735
+ if (t < t1) return hover(t);
736
+ if (t >= land) return 0;
737
+ return hover(t1) * (1 - ease((t - t1) / dur));
738
+ }