clipoox-render 0.4.5 → 0.4.6

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 (115) hide show
  1. package/package.json +1 -1
  2. package/scripts/render-local.mjs +999 -779
  3. package/web/assets/{FxEmbed-DbfuM8Yo.js → FxEmbed-ROS6C_HI.js} +1 -1
  4. package/web/assets/{MotionEmbed-CBQ-8Tzh.js → MotionEmbed-E2hCysi-.js} +1 -1
  5. package/web/assets/{PluginStore-OOEx0lP5.js → PluginStore-CwUSpwPp.js} +1 -1
  6. package/web/assets/{ShotComposer-Cng515xa.js → ShotComposer-DLxWpdYH.js} +8 -8
  7. package/web/assets/{TemplateEmbed-hjwM2b0o.js → TemplateEmbed-qG1AXdtf.js} +1 -1
  8. package/web/assets/{TimelinePlugins-D-NakABP.js → TimelinePlugins-CB1b6-Cb.js} +3 -3
  9. package/web/assets/{TimelineShotFx-D3kGhHN3.js → TimelineShotFx-CyHTgJHj.js} +2 -2
  10. package/web/assets/{TransitionEmbed-DR26yI3a.js → TransitionEmbed-Dym9P7--.js} +1 -1
  11. package/web/assets/{Whiteboard-BhxY0D0Z.js → Whiteboard-BdGjJZXW.js} +1 -1
  12. package/web/assets/{aerial-BisYSXzG.js → aerial-yLD_gDzw.js} +2 -2
  13. package/web/assets/{agentTools-CPFQeiDP.js → agentTools-B7g6tXTR.js} +2 -2
  14. package/web/assets/{aiInbetween-AOOM1e6p.js → aiInbetween-6eKDYbYS.js} +2 -2
  15. package/web/assets/{analyzerRun-Dtp2o7gT.js → analyzerRun-Cr-_79RH.js} +1 -1
  16. package/web/assets/{analyzerSandbox-BG_3Cea1.js → analyzerSandbox-N92T6amg.js} +1 -1
  17. package/web/assets/{api-BFN7blbw.js → api-Dnl2uaKO.js} +3 -3
  18. package/web/assets/auraTrack-C8eAEIWQ.js +1 -0
  19. package/web/assets/{breakthrough-0qFpZW-V.js → breakthrough-DSiuVwEX.js} +2 -2
  20. package/web/assets/{cameraPresets-eK3mP7qb.js → cameraPresets-Cfn7qhXK.js} +1 -1
  21. package/web/assets/{cameraTracker-BAQHuTV-.js → cameraTracker-DHdF8Zqx.js} +5 -5
  22. package/web/assets/{captionLayout-CWPcbid-.js → captionLayout-cckQ7p-t.js} +1 -1
  23. package/web/assets/{clientExport-DvpTTndR.js → clientExport-pkLbOmEF.js} +1 -1
  24. package/web/assets/{clientJobs-CIrYTMpn.js → clientJobs-CcsLDqFM.js} +2 -2
  25. package/web/assets/{depthEstimate-B7D7cGCl.js → depthEstimate-Cav4MmQ1.js} +1 -1
  26. package/web/assets/{directorTools-CJD4Huqk.js → directorTools-DMsatulF.js} +1 -1
  27. package/web/assets/{distortFrames-BLt1icUb.js → distortFrames-D_v-aSVO.js} +2 -2
  28. package/web/assets/{distortGL-BA5molk-.js → distortGL-vhnBRB59.js} +1 -1
  29. package/web/assets/domino-DBrinEUu.js +1 -0
  30. package/web/assets/{editTools-NKLsWZD1.js → editTools-BNnAvCw-.js} +1 -1
  31. package/web/assets/{engine-DioUF2mX.js → engine-Co-ivzlQ.js} +1 -1
  32. package/web/assets/{exportGlb-BOdI9cVS.js → exportGlb-D3PrWUvi.js} +1 -1
  33. package/web/assets/{faceTracker-D7CbF-qX.js → faceTracker-ClR-lS6Y.js} +1 -1
  34. package/web/assets/fightDirector-BlpVAURl.js +1 -0
  35. package/web/assets/{fightDrama-DIPcRKnc.js → fightDrama-BK0QidpM.js} +2 -2
  36. package/web/assets/{focusTool-vfabPqQ5.js → focusTool-sVOb2GGS.js} +1 -1
  37. package/web/assets/{frameSink-DYs2gTzq.js → frameSink-CSRqCnwI.js} +3 -3
  38. package/web/assets/{glTransitionSources.generated-DM5sRMX2.js → glTransitionSources.generated-q0H-ZbnZ.js} +2 -2
  39. package/web/assets/{gpuCompose-DbnZuelB.js → gpuCompose-B5bgbHdQ.js} +1 -1
  40. package/web/assets/{gpuExport-CQ7ZhpsX.js → gpuExport-oA2mUsdm.js} +7 -7
  41. package/web/assets/{handMatte-DIbMuqCu.js → handMatte-Dj2taFoC.js} +1 -1
  42. package/web/assets/handTracker-c6_rvuwA.js +2 -0
  43. package/web/assets/i18n-C9zFX25m.js +3 -0
  44. package/web/assets/{index-BfrDwjDI.js → index-Ds_8isyf.js} +1 -1
  45. package/web/assets/{layer-worker-DK67WH1a.js → layer-worker-ClVZG6ZW.js} +3 -3
  46. package/web/assets/library-BK0QVG4O.js +1 -0
  47. package/web/assets/{lottieLayer-C2YbiXhK.js → lottieLayer-Cv8ub9f6.js} +1 -1
  48. package/web/assets/{main-B27kf5aL.css → main-B_MAOvi6.css} +1 -1
  49. package/web/assets/{main-BKatDoGw.js → main-CwrkBJ63.js} +104 -104
  50. package/web/assets/{mapBake-C-G7WpFM.js → mapBake-DtQboFIX.js} +1 -1
  51. package/web/assets/{maskTrack-LMrwm5ub.js → maskTrack-D4QZfMWc.js} +1 -1
  52. package/web/assets/{matteFixRun-B38CbeEt.js → matteFixRun-CytaeRzS.js} +1 -1
  53. package/web/assets/{objectLocate-CKi3OTnL.js → objectLocate-XuAcemlO.js} +1 -1
  54. package/web/assets/{objectSegmenter-CziPuM61.js → objectSegmenter-D66spD7o.js} +1 -1
  55. package/web/assets/{objectTrack.worker-b7DJ-ub6.js → objectTrack.worker-BZS6WZ6l.js} +547 -327
  56. package/web/assets/{pathTracer-bqau16sX.js → pathTracer-DykgPkkh.js} +2 -2
  57. package/web/assets/{pendingMatte-CQb7jPg1.js → pendingMatte-BWBIWajI.js} +2 -2
  58. package/web/assets/{personAi-CQntGDqK.js → personAi-EhBknW6X.js} +2 -2
  59. package/web/assets/{personHq-B1c0QVJT.js → personHq-Ca351o06.js} +2 -2
  60. package/web/assets/{plateCamera-D7iLiNBy.js → plateCamera-BcdXD1uI.js} +1 -1
  61. package/web/assets/{plateCameraMatch-BM88dx-a.js → plateCameraMatch-GPZl1fQQ.js} +2 -2
  62. package/web/assets/{plateFollow-4iGUDTu1.js → plateFollow-CJUilsNV.js} +2 -2
  63. package/web/assets/{plateMatte-zTyuHHqh.js → plateMatte-CoEoVjxZ.js} +2 -2
  64. package/web/assets/{pluginAutomation-DSg-sYLF.js → pluginAutomation-DAPHWZPN.js} +1 -1
  65. package/web/assets/{poseTracker-DZl8l2qS.js → poseTracker-CzEKvJph.js} +1 -1
  66. package/web/assets/{productTools-B-jh8ukh.js → productTools-ce-zyZO0.js} +2 -2
  67. package/web/assets/remote-8XzEAnAJ.js +974 -0
  68. package/web/assets/{rigTrack-XaMj1yaH.js → rigTrack-DMyqb0pb.js} +1 -1
  69. package/web/assets/{rotoMaskStore-DdpmT9C3.js → rotoMaskStore-pE1AIt3I.js} +1 -1
  70. package/web/assets/{rotoMatte-i-cJTYmK.js → rotoMatte-7b7Ow64f.js} +1 -1
  71. package/web/assets/{rotoscope-DqRSWmHR.js → rotoscope-BUqNTKLd.js} +1 -1
  72. package/web/assets/{sceneTrack-DIPzBmMK.js → sceneTrack-CA21MCOP.js} +1 -1
  73. package/web/assets/{sceneTrack-Bbov7zET.js → sceneTrack-DlAH5s74.js} +1 -1
  74. package/web/assets/{sceneTrack.worker-Bav9h0pv.js → sceneTrack.worker-COlhlHRe.js} +547 -327
  75. package/web/assets/{shot-worker-m-q77W9f.js → shot-worker-DL1ZoIym.js} +1 -1
  76. package/web/assets/{shotFxAgent-DIMS7BGA.js → shotFxAgent-DqngnGFb.js} +1 -1
  77. package/web/assets/{skin-DrUzJE1u.js → skin-xFScwS3T.js} +1 -1
  78. package/web/assets/{spark.module-Y1SEI8Xz.js → spark.module-DjaIF6J1.js} +1 -1
  79. package/web/assets/{spellDuel-DNnyQDVr.js → spellDuel-BYNtnE8f.js} +2 -2
  80. package/web/assets/{store-GFMJzaWS.js → store-CpqHUcA2.js} +1 -1
  81. package/web/assets/{surfaceLight-C--moyc9.js → surfaceLight-C7YI9SV8.js} +1 -1
  82. package/web/assets/{surfacePin-B4-1UqXD.js → surfacePin-CzpUj1Cw.js} +2 -2
  83. package/web/assets/{textAnimators-BVXTl8bU.js → textAnimators-B9vO_hbP.js} +1 -1
  84. package/web/assets/{thumb-CTfDJgLp.js → thumb-BTFsBS1u.js} +1 -1
  85. package/web/assets/{timeline3d-Dn7PqmEe.js → timeline3d-DrFaMPPB.js} +2 -2
  86. package/web/assets/{timeline3dExport-DCOr6ARB.js → timeline3dExport-Uo0S0CR0.js} +1 -1
  87. package/web/assets/{timelineComp-Dx_kED5V.js → timelineComp-i-zdaoB2.js} +1 -1
  88. package/web/assets/timelineOps-DSSaDsU2.js +525 -0
  89. package/web/assets/{timelineProps-eYgO6gs5.js → timelineProps-0v-s69pw.js} +1 -1
  90. package/web/assets/{timelineText-RKyRP988.js → timelineText-DurRhyuh.js} +1 -1
  91. package/web/assets/{titleArt-CUtA0C3I.js → titleArt-CnC0A7x7.js} +3 -3
  92. package/web/assets/{titleFrameBudget-X7HLSDre.js → titleFrameBudget-DsM0V482.js} +801 -581
  93. package/web/assets/{titleFrames-aMG7HdVe.js → titleFrames-C8kvflgT.js} +1 -1
  94. package/web/assets/{tool-DpFjybaq.js → tool-D1FpErrX.js} +1 -1
  95. package/web/assets/{tools-CeOU0p13.js → tools-DIvrRu-5.js} +1 -1
  96. package/web/assets/toonTools-D9MNdRty.js +2 -0
  97. package/web/assets/{trackAnchor-hAOtEhKO.js → trackAnchor-XvUuvkWP.js} +1 -1
  98. package/web/assets/{userFonts-1kuRjWOo.js → userFonts-Djka6RJX.js} +1 -1
  99. package/web/assets/{vfxAlpha-DxS5-QwY.js → vfxAlpha-B-JzzRI1.js} +1 -1
  100. package/web/assets/vfxBackground-b053ziDK.js +2 -0
  101. package/web/assets/{videoExport-Cg4B2Nyh.js → videoExport-NN-QijHd.js} +1 -1
  102. package/web/assets/{views-CUXyRb9x.js → views-RQBs4zva.js} +1 -1
  103. package/web/assets/{weaponDuel-nFECu9nk.js → weaponDuel-DKAEYJjO.js} +1 -1
  104. package/web/assets/{windowFrames-1wAkkWnv.js → windowFrames-DhhJLIs-.js} +1 -1
  105. package/web/layer-worker.html +3 -3
  106. package/web/shot-worker.html +23 -23
  107. package/web/assets/auraTrack-D_gxHNVe.js +0 -1
  108. package/web/assets/fightDirector-CYP8jxo8.js +0 -1
  109. package/web/assets/handTracker-DY_g5Xmg.js +0 -2
  110. package/web/assets/i18n-BJppIrtF.js +0 -3
  111. package/web/assets/library-B6KsZUKq.js +0 -1
  112. package/web/assets/remote-DJd9xWjn.js +0 -963
  113. package/web/assets/timelineOps-afzSo188.js +0 -525
  114. package/web/assets/toonTools-DLmo37Z_.js +0 -2
  115. package/web/assets/vfxBackground-CYpaQaTa.js +0 -2
@@ -1 +1 @@
1
- import{_ as qo,fL as Ct,dX as Ht,dY as wt,ca as Yt,dW as gt,nc as Ko,c_ as mt,hx as Xo,m0 as Lo,ff as it,f6 as No,fM as fo,fy as Oo,iB as Yo,au as vt,F as ut,cw as $o,l6 as ho,nd as uo,ne as Vo,i7 as kt,i6 as Wt,l8 as Bo,fa as Uo,la as po,nf as mo,ng as Go,nh as to,ni as Qo,nj as Jo,l9 as $t,lb as Zo}from"./titleFrameBudget-X7HLSDre.js";const Ft=.1,zt=t=>Math.abs(t)>180?(t%360+540)%360-180:t;function Mo(t,o,s,e,n,a,r,l,i){const c=l-r,f=t==null?s-o:(s-t)/Math.max(1e-9,l-a)*c,h=e==null?s-o:(e-o)/Math.max(1e-9,i-r)*c,p=n*n,u=p*n;return(2*u-3*p+1)*o+(u-2*p+n)*f+(-2*u+3*p)*s+(u-p)*h}function yo(t,o,s,e,n,a,r,l,i){const c=o+zt(s-o),f=t==null?null:o+zt(t-o),h=e==null?null:c+zt(e-c);return Mo(f,o,c,h,n,a,r,l,i)}function Ho(t,o,s=e=>e.time){if(globalThis.__clipooxLinearKeys)return!1;const e=(n,a)=>s(t[a])-s(t[n]);return!(e(o,o+1)>Ft||o>0&&e(o-1,o)>Ft||o+2<t.length&&e(o+1,o+2)>Ft)}function Vt(t,o,s,e){let n=o>0,a=o+2<t.length;if(e!=null){const l=n?s(t[o-1]):null,i=s(t[o]),c=s(t[o+1]),f=a?s(t[o+2]):null;if(i<e-1e-9&&c>=e-1e-9)return{before:!1,after:!1};a&&c<e-1e-9&&f>=e-1e-9&&(a=!1),n&&i>=e-1e-9&&l<e-1e-9&&(n=!1)}return{before:n,after:a}}function Fn(t,o,s,e=r=>r.pose,n=r=>r.time,a=null){const r=Vt(t,o,n,a),l=e(t[o]),i=e(t[o+1]),c=r.before?e(t[o-1]):null,f=r.after?e(t[o+2]):null,h=n(t[o]),p=n(t[o+1]),u=c?n(t[o-1]):h-(p-h),m=f?n(t[o+2]):p+(p-h),y={};for(const M of new Set([...Object.keys(l),...Object.keys(i)])){const d=l[M]??0,x=i[M]??0;if(typeof d!="number"||typeof x!="number")continue;const g=c&&typeof c[M]=="number"?c[M]:null,w=f&&typeof f[M]=="number"?f[M]:null;y[M]=yo(g,d,x,w,s,u,h,p,m)}return y}function ko(t,o,s,e=r=>r.position,n=r=>r.time,a=null){const r=Vt(t,o,n,a),l=e(t[o]),i=e(t[o+1]),c=r.before?e(t[o-1]):null,f=r.after?e(t[o+2]):null,h=n(t[o]),p=n(t[o+1]),u=c?n(t[o-1]):h-(p-h),m=f?n(t[o+2]):p+(p-h);return l.map((y,M)=>Mo(c?c[M]:null,y,i[M],f?f[M]:null,s,u,h,p,m))}function Wo(t,o,s,e,n=r=>r.time,a=null){const r=Vt(t,o,n,a),l=e(t[o]),i=e(t[o+1]),c=r.before?e(t[o-1]):null,f=r.after?e(t[o+2]):null,h=n(t[o]),p=n(t[o+1]),u=c!=null?n(t[o-1]):h-(p-h),m=f!=null?n(t[o+2]):p+(p-h);return yo(c,l,i,f,s,u,h,p,m)}const Et=30,oo=4,xt=Object.freeze({min:.25,max:6,default:3}),Rt=40,go=300,bo=Object.freeze({position:.01,rotation:2}),xo=80,wo=Object.freeze(["here","above","explode","throw"]),Xt=Object.freeze({bouncy:Object.freeze({restitution:.6,friction:.45}),normal:Object.freeze({restitution:.25,friction:.6}),heavy:Object.freeze({restitution:.04,friction:.9})}),tn=9.81;let qt=null,vo=null;const Eo=()=>vo;function Ao(){return qt??=(async()=>{const t=await qo(()=>import("./rapier-DBjBuyhF.js"),[]),o=t.default??t,s=console.warn;console.warn=(...e)=>{String(e[0]).includes("deprecated parameters")||s(...e)};try{await o.init()}finally{console.warn=s}return vo=o,o})().catch(t=>{throw qt=null,t}),qt}const ot=t=>typeof t=="number"&&Number.isFinite(t),Y=(t,o)=>Array.isArray(t)&&t.length===3&&t.every(ot)?[t[0],t[1],t[2]]:o,Z=Math.PI/180,St=t=>(t%360+540)%360-180,Tt=t=>Math.round(t*1e4)/1e4,Lt=t=>Math.round(t*100)/100;function At(t){const o=Math.cos(t[0]*Z/2),s=Math.cos(t[1]*Z/2),e=Math.cos(t[2]*Z/2),n=Math.sin(t[0]*Z/2),a=Math.sin(t[1]*Z/2),r=Math.sin(t[2]*Z/2);return[n*s*e+o*a*r,o*a*e-n*s*r,o*s*r+n*a*e,o*s*e-n*a*r]}function Co(t){const[o,s,e,n]=t,a=1-2*(s*s+e*e),r=2*(o*s-e*n),l=2*(o*e+s*n),i=1-2*(o*o+e*e),c=2*(s*e-o*n),f=2*(s*e+o*n),h=1-2*(o*o+s*s),p=Math.asin(Math.max(-1,Math.min(1,l)));return Math.abs(l)<.9999999?[Math.atan2(-c,h)/Z,p/Z,Math.atan2(-r,a)/Z]:[Math.atan2(f,i)/Z,p/Z,0]}const no=(t,o)=>{const[s,e,n,a]=t,r=a*o[0]+e*o[2]-n*o[1],l=a*o[1]+n*o[0]-s*o[2],i=a*o[2]+s*o[1]-e*o[0],c=-s*o[0]-e*o[1]-n*o[2];return[r*a+c*-s+l*-n-i*-e,l*a+c*-e+i*-s-r*-n,i*a+c*-n+r*-e-l*-s]},Po=(t,o)=>2*Math.acos(Math.min(1,Math.abs(t[0]*o[0]+t[1]*o[1]+t[2]*o[2]+t[3]*o[3])))/Z;function So(t){let o=2166136261;for(const e of String(t))o=Math.imul(o^e.codePointAt(0),16777619);let s=o>>>0;return()=>{s=s+1831565813|0;let e=Math.imul(s^s>>>15,1|s);return e=e+Math.imul(e^e>>>7,61|e)^e,((e^e>>>14)>>>0)/4294967296}}const eo=new Set(["box","sphere","cylinder","wall","platform"]),on=new Set(["person","fx","splat","set","floor"]);function nn(t){return t?t.locked?"Đối tượng đang khoá":t.hold?"Đồ đang cầm đi theo tay người cầm":t.kind==="person"||t.kind==="model"&&(t.skeleton==="native"||t.rig?.kind==="humanoid")?"Nhân vật không rơi như vật thể — dùng chuyển động":on.has(t.kind)?"Loại đối tượng này không rơi được":null:"Chưa chọn đối tượng"}function en(t){const o=Y(t.scale,[1,1,1]);return t.kind==="sphere"&&Math.abs(o[0]-o[1])<1e-6&&Math.abs(o[1]-o[2])<1e-6||t.kind==="prop"&&t.prop?.kind==="ball"?"ball":t.kind==="cylinder"&&Math.abs(o[0]-o[2])<1e-6?"cylinder":"box"}function sn(t){const o=Y(t.scale,[1,1,1]).map(Math.abs),s=Y(t.mirror,[1,1,1]),e=Y(t.origin,[0,0,0]);let n=[1,1,1],a=[0,.5,0];if(t.kind==="proxy"&&t.proxy?.dims)n=Y(t.proxy.dims,n),a=[0,n[1]/2,0];else if(t.kind==="prop"){const i=t.prop?.kind==="ball"?.12:.15;n=[2*i,2*i,2*i],a=[0,t.prop?.kind==="ball"?0:i,0]}else if(t.kind==="model"){const i=t.model?.height??2;n=[i*.6,i,i*.6],a=[0,i/2,0]}else eo.has(t.kind)||(n=[.5,.5,.5],a=[0,.25,0]);const r=eo.has(t.kind)?[(a[0]-e[0])*s[0],(a[1]-e[1])*s[1],(a[2]-e[2])*s[2]]:a,l=i=>[i[0]*(t.scale?.[0]??1),i[1]*(t.scale?.[1]??1),i[2]*(t.scale?.[2]??1)];return{half:[n[0]*o[0]/2,n[1]*o[1]/2,n[2]*o[2]/2],offset:l(r)}}function rn(t,o){let s=0,e=t.length-1;for(;e-s>1;){const n=s+e>>1;t[n].time<=o?s=n:e=n}return s}function To(t,o,s=null,e=null){const n=t[0],a=t[t.length-1];if(o<=n.time)return{position:n.position,rotation:n.rotation};if(o>=a.time)return{position:a.position,rotation:a.rotation};const r=rn(t,o),l=t[r],i=t[r+1],c=(o-l.time)/(i.time-l.time),f=h=>l.rotation[h]+St(i.rotation[h]-l.rotation[h])*c;return Ho(t,r,e>0?h=>h.time*e:void 0)?{position:ko(t,r,c,h=>h.position,h=>h.time,s),rotation:[f(0),Wo(t,r,c,h=>h.rotation[1],h=>h.time,s),f(2)]}:{position:[0,1,2].map(h=>l.position[h]+(i.position[h]-l.position[h])*c),rotation:[f(0),f(1),f(2)]}}function Ro(t,{tolerance:o=bo,maxKeys:s=xo,before:e=null,seam:n=null,duration:a=null}={}){const r=t.length;if(r<=2)return{keys:t.map(({time:f,position:h,rotation:p})=>({time:f,position:h,rotation:p})),error:{position:0,rotation:0}};const l=new Set([0,r-1]),i=()=>[...l].sort((f,h)=>f-h).map(f=>t[f]);let c={position:0,rotation:0};for(;;){const f=i(),h=e?[e,...f]:f;let p=-1,u=1;c={position:0,rotation:0};for(let m=1;m<r-1;m++){if(l.has(m))continue;const y=t[m],M=To(h,y.time,n,a),d=Math.hypot(M.position[0]-y.position[0],M.position[1]-y.position[1],M.position[2]-y.position[2]),x=Po(At(M.rotation),y.quat);c={position:Math.max(c.position,d),rotation:Math.max(c.rotation,x)};const g=Math.max(d/o.position,x/o.rotation);g>u&&(u=g,p=m)}if(p<0||l.size>=s)break;l.add(p)}return{keys:i().map(({time:f,position:h,rotation:p})=>({time:f,position:h,rotation:p})),error:{position:Tt(c.position),rotation:Lt(c.rotation)}}}const an=t=>ot(t)?{restitution:Math.max(0,Math.min(.95,t)),friction:.6}:Xt[t]??Xt.normal;async function ln(t){const o=Bt(await Ao(),t),{duration:s,mode:e}=o,n=ot(t.start)?Math.max(0,t.start):0,a=ot(t.shotDuration)&&t.shotDuration>0?t.shotDuration:n+s,r={},l=n/a;for(const i of o.bodies){let c=null;const f=i.samples.map(m=>{let y=Co(m.q);if(c){const M=[y[0]+180,180-y[1],y[2]+180],d=x=>x.reduce((g,w,b)=>g+Math.abs(St(w-c[b])),0);d(M)<d(y)&&(y=M)}return y=y.map(M=>Lt(St(M)===-180?180:St(M))),c=y,{time:l+m.sec/a,position:m.pivot.map(Tt),rotation:y,quat:At(y)}}),h=t.before?.[i.id],{keys:p,error:u}=Ro(f,{before:h&&ot(h.time)&&h.time<l?h:null,seam:ot(t.seam)?t.seam:null,duration:a});r[i.id]={keys:p,error:u,settledAt:i.settledAt,from:{position:i.position.map(Tt),rotation:i.rotation.map(Lt)},rest:f[f.length-1].position,restCentre:i.samples[i.samples.length-1].centre.map(Tt),restQuat:i.samples[i.samples.length-1].q}}return{tracks:r,settled:o.settled,seconds:s,mode:e}}function Bt(t,o){const s=!!o.planar,e=wo.includes(o.mode)?o.mode:"here",n=Math.max(xt.min,Math.min(xt.max,ot(o.duration)?o.duration:xt.default)),a=ot(o.floor)?o.floor:0,r=an(o.bounciness),l=So(`${o.seed??"drop"}|${e}`),i=m=>(l()*2-1)*m,c=(o.bodies||[]).filter(m=>m&&m.id!=null).map(m=>({id:String(m.id),shape:["ball","cylinder"].includes(m.shape)?m.shape:"box",position:Y(m.position,[0,0,0]),rotation:Y(m.rotation,[0,0,0]),half:Y(m.half,[.25,.25,.25]).map(y=>Math.max(.01,Math.abs(y))),offset:Y(m.offset,[0,0,0])})).sort((m,y)=>m.id<y.id?-1:m.id>y.id?1:0).slice(0,Rt);if(!c.length)throw new Error("no objects to drop");const f=m=>Math.hypot(...m.half);for(const m of c){m.q=At(m.rotation);const y=no(m.q,m.offset);m.c=[m.position[0]+y[0],m.position[1]+y[1],m.position[2]+y[2]],m.v=[0,0,0],m.w=[0,0,0]}if(e==="above"){const m=ot(o.height)?Math.max(.3,Math.min(20,o.height)):2,y=Math.max(...c.map(d=>d.c[1]+f(d))),M=[];for(const d of[...c].sort((x,g)=>x.c[1]-g.c[1]||(x.id<g.id?-1:1))){d.q=At(s?[d.rotation[0],d.rotation[1],d.rotation[2]+i(35)]:[d.rotation[0]+i(35),d.rotation[1]+i(180),d.rotation[2]+i(35)]);let x=y+m+f(d);for(const g of M)Math.hypot(g.c[0]-d.c[0],g.c[2]-d.c[2])<f(g)+f(d)&&(x=Math.max(x,g.c[1]+f(g)+f(d)+.05));d.c=s?[d.c[0],x,d.c[2]]:[d.c[0]+i(.05),x,d.c[2]+i(.05)],M.push(d)}}if(e==="here")for(const m of c)m.w=[i(1.2),i(1.2),i(1.2)];const h=m=>s?[0,0,m[2]]:m,p=m=>s?[m[0],m[1],0]:m;if(e==="explode"){const m=Y(o.center,[0,1,2].map(M=>c.reduce((d,x)=>d+x.c[M],0)/c.length)),y=ot(o.strength)?Math.max(.5,Math.min(20,o.strength)):4;for(const M of c){let d=M.c[0]-m[0],x=s?0:M.c[2]-m[2],g=Math.hypot(d,x);if(g<.001){const b=l()*Math.PI*2;d=Math.cos(b),x=s?0:Math.sin(b),g=Math.hypot(d,x)||1}const w=y*(.75+l()*.5);M.v=[d/g*w,w*(.6+l()*.4),x/g*w],M.w=[i(7),i(7),i(7)]}}if(e==="throw"){const m=Y(o.velocity,[0,2,3]);for(const y of c)y.v=m.map(M=>M*(1+i(.08))),y.w=[i(5),i(5),i(5)]}const u=new t.World({x:0,y:-(ot(o.gravity)?o.gravity:tn),z:0});try{u.timestep=1/(Et*oo);for(const g of c)g.v=p(g.v),g.w=h(g.w);const m=(g,w)=>{const b=u.createRigidBody(t.RigidBodyDesc.fixed().setTranslation(g[0],g[1],g[2]));u.createCollider(t.ColliderDesc.cuboid(w[0],w[1],w[2]).setRestitution(r.restitution).setFriction(r.friction),b)};m([0,a-1,0],[1e3,1,1e3]);let y=0;for(const g of o.statics||[]){const w=Y(g?.min,null),b=Y(g?.max,null);if(!w||!b||y>=go)continue;const v=[0,1,2].map(R=>(b[R]-w[R])/2);v.some(R=>!(R>.002))||b[1]<=a+.001||(m([0,1,2].map(R=>(w[R]+b[R])/2),v),y++)}for(const g of c){g.rb=u.createRigidBody(t.RigidBodyDesc.dynamic().setTranslation(g.c[0],g.c[1],g.c[2]).setRotation({x:g.q[0],y:g.q[1],z:g.q[2],w:g.q[3]}).setLinvel(g.v[0],g.v[1],g.v[2]).setAngvel({x:g.w[0],y:g.w[1],z:g.w[2]}).setLinearDamping(.05).setAngularDamping(g.shape==="box"?.2:1.2).setCcdEnabled(!0)),s&&(g.rb.setEnabledTranslations(!0,!0,!1,!1),g.rb.setEnabledRotations(!1,!1,!0,!1));const w=g.shape==="ball"?t.ColliderDesc.ball(Math.max(...g.half)):g.shape==="cylinder"?t.ColliderDesc.cylinder(g.half[1],Math.max(g.half[0],g.half[2])):t.ColliderDesc.cuboid(g.half[0],g.half[1],g.half[2]);u.createCollider(w.setRestitution(r.restitution).setFriction(r.friction).setDensity(400),g.rb),g.samples=[]}const M=Math.round(n*Et);for(let g=0;g<=M;g++){if(g)for(let w=0;w<oo;w++)u.step();for(const w of c){const b=w.rb.translation(),v=w.rb.rotation(),R=[v.x,v.y,v.z,v.w],T=no(R,w.offset),D=w.rb.linvel(),q=w.rb.angvel();w.samples.push({sec:g/Et,centre:[b.x,b.y,b.z],pivot:[b.x-T[0],b.y-T[1],b.z-T[2]],q:R,speed:Math.hypot(D.x,D.y,D.z)+Math.hypot(q.x,q.y,q.z)*f(w)})}}let d=!0;return{bodies:c.map(g=>{let w=null;for(let b=g.samples.length-1;b>=0&&g.samples[b].speed<.05;b--)w=g.samples[b].sec;return w==null&&(d=!1),{id:g.id,position:g.position,rotation:g.rotation,half:g.half,offset:g.offset,samples:g.samples,settledAt:w}}),settled:d,duration:n,mode:e}}finally{u.free()}}function cn(t,o,s,{scale:e={},newId:n,lifted:a=!1}={}){const r=n??(()=>globalThis.crypto.randomUUID());return{...t,shots:t.shots.map(l=>{if(l.id!==o)return l;const i={...l.objectKeys};for(const[c,f]of Object.entries(s)){if(!f.keys.length)continue;const h=f.keys[0].time,p=t.items.find(M=>M.id===c),u=Y(e[c],Y(p?.scale,[1,1,1])),m=(i[c]||[]).filter(M=>M.time<h-1e-6),y=1/(Et*Math.max(1e-6,l.duration||1));a&&f.from&&m.length&&h-y>m[m.length-1].time+1e-6&&m.push({id:r(),time:h-y,position:[...f.from.position],rotation:[...f.from.rotation],scale:[...u]}),i[c]=[...m,...f.keys.map(M=>({id:r(),time:M.time,position:[...M.position],rotation:[...M.rotation],scale:[...u]}))]}return{...l,objectKeys:i}})}}const zn=Object.freeze(Object.defineProperty({__proto__:null,BOUNCE_PRESETS:Xt,DROP_MODES:wo,DROP_SECONDS:xt,KEY_TOLERANCE:bo,MAX_KEYS_PER_BODY:xo,MAX_PHYSICS_BODIES:Rt,MAX_PHYSICS_STATICS:go,PHYSICS_FPS:Et,applyDropKeys:cn,bakeDrop:ln,dropUnavailable:nn,eulerFromQuat:Co,fallbackShape:sn,loadRapier:Ao,quatAngle:Po,quatFromEuler:At,rapierReady:Eo,sampleBakedKeys:To,seededRandom:So,shapeKind:en,simulateDrop:Bt,thinKeys:Ro},Symbol.toStringTag,{value:"Module"})),Io=["x","y","zoom","rot","opacity","depth"],L=t=>Math.round(t*1e4)/1e4,Pt=(t,o)=>Object.keys(it).filter(s=>(!o||o.includes(s))&&No(t,s));function qn(t,{in:o=.33,out:s=.33}={},e){const n=Lo([Math.max(0,Math.min(1,s)),0,Math.max(0,Math.min(1,1-o)),1]);let a={...t},r={};for(const l of Pt(t)){const i=gt(a,l),c=e?e.filter(h=>h.prop===l):i;if(!c.length)continue;const f=i.map((h,p)=>p<i.length-1&&c.some(u=>Math.abs(u.time-h.time)<1/120)?{...h,ease:n}:h);r={...r,...mt(a,l,f)},a={...a,...r}}return r}function Kn(t,{in:o=null,out:s=null}={},e,n){const a=c=>Math.max(.001,Math.min(1,c)),r={...s!=null?{out:[L(a(s)),0]}:{},...o!=null?{in:[L(1-a(o)),1]}:{}};let l={...t},i={};for(const c of Pt(t,n)){const f=gt(l,c);if(f.length<2)continue;const h=Ko(t,f,e);h.length&&(i={...i,...mt(l,c,Xo(f,h,r))},l={...l,...i})}return i}const Ut={tl:[0,0],t:[.5,0],tr:[1,0],l:[0,.5],c:[.5,.5],r:[1,.5],bl:[0,1],b:[.5,1],br:[1,1]},Xn=Object.keys(Ut);function Ln(t,o,s,e,n,a){const[r,l]=Ut[s],i=o.angle*Math.PI/180,c=Math.abs(o.w*Math.cos(i))+Math.abs(o.h*Math.sin(i)),f=Math.abs(o.w*Math.sin(i))+Math.abs(o.h*Math.cos(i)),h=c/2+(n-c)*r,p=f/2+(a-f)*l,u=(h-o.cx)/n,m=(p-o.cy)/a,y=(d,x)=>Math.max(it[d].min,Math.min(it[d].max,x));let M=Ht(t,"x",L(y("x",wt(t,"x",e)+u)),e);return M={...M,...Ht({...t,...M},"y",L(y("y",wt(t,"y",e)+m)),e)},M}function Nn(t,o,s,e,n,a){const[r,l]=Ut[s],i=Yt(t,e,n,a).zoom||1;return Yo(t,e,L((r-.5)*o.w/(n*i)),L((l-.5)*o.h/(a*i)),n,a)}function On(t,o,s,e="forward",n=1){let a=t.filter(i=>o.includes(i.id)).sort((i,c)=>c.track-i.track||i.start-c.start);if(a.length<2)return t;if(e==="reverse")a.reverse();else if(e==="center"){const i=(a.length-1)/2;a=[...a].sort((c,f)=>Math.abs(a.indexOf(c)-i)-Math.abs(a.indexOf(f)-i))}else if(e==="random"){let i=n>>>0||1;const c=()=>(i=Math.imul(i^i>>>15,2246822507)>>>0)/4294967295;a=a.map(f=>[c(),f]).sort((f,h)=>f[0]-h[0]).map(f=>f[1])}const r=Math.min(...a.map(i=>i.start)),l=new Map(a.map((i,c)=>[i.id,L(Math.max(0,r+c*s))]));return t.map(i=>l.has(i.id)?{...i,start:Math.min(l.get(i.id),600-i.duration)}:i)}function Yn(t,o){const s={};for(const n of Pt(t)){const a=gt(t,n).filter(r=>!o||o.some(l=>l.prop===n&&Math.abs(l.time-r.time)<.008333333333333333));a.length&&(s[n]=a)}const e=Math.min(...Object.values(s).flat().map(n=>n.time));return Object.keys(s).length?{props:Object.fromEntries(Object.entries(s).map(([n,a])=>[n,a.map(r=>({...r,time:L(r.time-e)}))]))}:null}function $n(t,o,s){let e={...t},n={};for(const[a,r]of Object.entries(o.props)){if(!it[a]||t.kind==="model"&&a==="rot"||t.kind!=="model"&&["rx","ry","rz"].includes(a))continue;const l=r.map(c=>({...c,time:L(s+c.time)})).filter(c=>c.time<=t.duration+1e-6),i=gt(e,a).filter(c=>!l.some(f=>Math.abs(f.time-c.time)<1/120));n={...n,...mt(e,a,[...i,...l].sort((c,f)=>c.time-f.time))},e={...e,...n}}return n}function Vn(t,o="elastic",{amount:s=.5,cycles:e=3,props:n}={}){let a={...t},r={};for(const l of Pt(t,n??Io)){const i=gt(a,l);if(i.length<2)continue;const c=i.at(-2),f=i.at(-1),h=f.time-c.time,p=t.duration-f.time;if(h<=0||p<.1)continue;const u=f.value-c.value;if(Math.abs(u)<1e-6)continue;const m=Math.max(1,Math.min(6,Math.round(e))),y=Math.min(p/(m*2),Math.max(.06,h*.6)),M=[];for(let d=1;d<=m*2;d++){const x=s*Math.pow(.45,Math.ceil(d/2)-1),g=o==="bounce"?(d%2?-1:0)*Math.abs(u)*x*Math.sign(u):(d%2?1:-1)*u*x*.5,w=d===m*2?f.value:f.value+g,b=it[l].min,v=it[l].max;M.push({time:L(f.time+y*d),value:L(Math.max(b,Math.min(v,w))),ease:so})}r={...r,...mt(a,l,[...i.map((d,x)=>x===i.length-1?{...d,ease:so}:d),...M])},a={...a,...r}}return r}const so=[.33,0,.67,1];function Bn(t,o="cycle",{props:s,maxKeys:e=60}={}){let n={...t},a={};for(const r of Pt(t,s)){const l=gt(n,r);if(l.length<2)continue;const i=l[0].time,c=l.at(-1).time,f=c-i;if(f<=1/30)continue;const h=[...l],p=l.at(-1).value-l[0].value;for(let u=1;h.length<e;u++){const m=o==="pingpong"&&u%2?[...l].reverse().map(M=>({...M,time:c-(M.time-i)})):l;let y=!1;for(const M of m.slice(1)){const d=L(M.time+f*u);if(d>t.duration+1e-6||h.length>=e)break;const x=o==="offset"?M.value+p*u:M.value,{min:g,max:w}=it[r];h.push({...M,time:d,value:L(Math.max(g,Math.min(w,x)))}),y=!0}if(!y)break}a={...a,...mt(n,r,h)},n={...n,...a}}return a}function Un(t,o){const s=Ct(t.shape)?t.shape:fo(t.shape);return s.items.map(e=>({...t,id:o(),label:`${t.label} · ${e.name}`.slice(0,60),shape:{...s,items:[{...e}]}}))}const fn=["posX","posY","scale","rotation","anchorX","anchorY","parentId","opacity"];function Gn(t){if(t.length<2)throw Error("Chọn ít nhất 2 layer Shape để gộp");const[o,...s]=t,e=a=>fn.every(r=>(a[r]??null)===(o[r]??null))&&!a.motionKeys?.some(r=>Io.some(l=>l in r));if(!t.every(e))throw Error("Chỉ gộp được các layer Shape cùng vị trí / tỉ lệ / góc xoay và không có key chuyển động");const n=[];for(const a of t){const r=Ct(a.shape)?a.shape:fo(a.shape);for(const l of r.items)n.push({...l,id:Oo(n)})}if(n.length>24)throw Error("Một layer Shape chứa tối đa 24 hình");return{...o,start:Math.min(...t.map(a=>a.start)),duration:Math.max(...t.map(a=>a.start+a.duration))-Math.min(...t.map(a=>a.start)),shape:{v:2,items:n}}}function Qn(t,o,s,{fps:e=10,makeId:n,label:a}={}){const r=Math.min(o/1280,s/720),l=(y,M)=>[L(((y-o/2)/r+640-280)/.72),L(((M-s/2)/r+360)/.72)],i=Math.max(2,Math.min(255,Math.round(t.duration*e)+1)),c=[];for(let y=0;y<i;y++){const M=Yt(t,t.duration*y/(i-1),o,s);c.push(l(o/2+M.x*o,s/2+M.y*s))}const f=c.map(y=>y[0]),h=c.map(y=>y[1]),p=(Math.min(...f)+Math.max(...f))/2,u=(Math.min(...h)+Math.max(...h))/2,m={id:"s1",name:"Position Trace",type:"path",params:{pts:c.map(([y,M])=>[L(y-p),L(M-u)]),closed:!1},pos:[L(p),L(u)],rot:0,scale:[1,1],opacity:1,fill:{type:"none"},stroke:{color:"#c6f432",width:8,cap:"round",join:"round"}};return{id:n(),nodeId:"",kind:"image",url:"",label:a??`${t.label} · trace`.slice(0,60),track:t.track,start:t.start,duration:t.duration,volume:0,motionV:2,shape:{v:2,items:[m]}}}const Gt=/^#[0-9a-fA-F]{6}$/,hn=24,Jn=["#c6f432","#2bd4c9","#ffffff","#0d0f12","#ff5bd1"];function Zn(t,o=16){const s=new Map,e=n=>{if(typeof n=="string"&&Gt.test(n)){const a=n.toLowerCase();s.set(a,(s.get(a)||0)+1)}};for(const n of t){n.title&&(e(n.title.color),e(n.title.accentColor));const a=n.shape;if(a)if(Ct(a))for(const r of a.items){e(r.fill.color);for(const l of r.fill.stops||[])e(l[0]);e(r.stroke?.color)}else for(const r of a.paths)e(r.stroke),e(r.fill)}return[...s.entries()].sort((n,a)=>a[1]-n[1]).slice(0,o).map(n=>n[0])}function Hn(t,o,{stroke:s=!1,itemId:e}={}){if(!Gt.test(o))throw Error("Invalid colour");if(t.title)return{title:{...t.title,[s?"accentColor":"color"]:o}};if(!t.shape)throw Error("Chọn layer Shape hoặc chữ để áp màu");return Ct(t.shape)?{shape:{...t.shape,items:t.shape.items.map(n=>e&&n.id!==e?n:s?{...n,stroke:{...n.stroke??{width:8,cap:"round",join:"round"},color:o}}:{...n,fill:n.fill.type==="linear"||n.fill.type==="radial"?{...n.fill,stops:n.fill.stops.map((a,r)=>r===0?[o,a[1]]:a)}:{...n.fill,type:"color",color:o}})}}:{shape:{...t.shape,paths:t.shape.paths.map(n=>s||!n.fill?{...n,stroke:o}:{...n,fill:o})}}}function kn(t,o){if(!Array.isArray(o)||!o.length||o.length>hn||!o.every(n=>Gt.test(n)))throw Error("Invalid palette");const s=n=>o[n%o.length];if(t.title)return{title:{...t.title,color:s(0),...o.length>1?{accentColor:s(1)}:{}}};if(!t.shape)throw Error("Chọn layer Shape hoặc chữ để áp màu");if(!Ct(t.shape))return{shape:{...t.shape,paths:t.shape.paths.map((n,a)=>({...n,...n.fill?{fill:s(a)}:{},...n.stroke?{stroke:n.fill?n.stroke:s(a)}:{}}))}};let e=0;return{shape:{...t.shape,items:t.shape.items.map(n=>{const a=n.fill;return a.type==="linear"||a.type==="radial"?{...n,fill:{...a,stops:a.stops.map(r=>[s(e++),r[1]])}}:a.type==="color"?{...n,fill:{...a,color:s(e++)}}:n.stroke?{...n,stroke:{...n.stroke,color:s(e++)}}:n})}}}function un(t,o,s,e){const n=Yt(t,o,s,e),a=t.card?t.card.width/t.card.height:t.lottie?t.lottie.w/t.lottie.h:null,[r,l]=a&&Number.isFinite(a)&&a>0?a>s/e?[s,s/a]:[e*a,e]:[s,e];return{cx:s/2+n.x*s,cy:e/2+n.y*e,w:r*n.zoom,h:l*n.zoom,angle:n.rot}}const pn=3,Nt=30,Kt=Object.freeze({xy:.002,rot:1.5}),mn=.2,ro=t=>typeof t=="number"&&Number.isFinite(t),ao=t=>Math.round(t*1e4)/1e4;function dn(t){return t?t.kind==="audio"?"Âm thanh không rơi được":t.adjust===!0?"Lớp điều chỉnh không có hình để rơi":t.parentId?"Lớp đang gắn vào lớp cha — bỏ gắn rồi thử lại":null:"Không tìm thấy lớp"}function Qt(t,o,s,e=Nt){const n=t.length;if(n<=2)return[...Array(n).keys()];const a=[0,n-1];for(;;){a.sort((i,c)=>i-c);let r=-1,l=s;for(let i=0;i+1<a.length;i++){const c=a[i],f=a[i+1];for(let h=c+1;h<f;h++){const p=o[c]+(o[f]-o[c])*(t[h]-t[c])/(t[f]-t[c]),u=Math.abs(p-o[h]);u>l&&(l=u,r=h)}}if(r<0||a.length>=e)return a;a.push(r)}}function Mn(t,o,s){let e=0;for(let n=0;n+1<s.length;n++)for(let a=s[n]+1;a<s[n+1];a++){const r=s[n],l=s[n+1];e=Math.max(e,Math.abs(o[r]+(o[l]-o[r])*(t[a]-t[r])/(t[l]-t[r])-o[a]))}return e}function yn(t,o,s){const e=[],n=r=>Math.floor((r+180)/360),a=(r,l)=>r-360*l;for(let r=0;r<s.length;r++){const l=s[r],i=o[l],c=n(i);if(r){const f=s[r-1],h=n(o[f]);if(h!==c){const p=c>h,u=(p?h+1:h)*360-180,m=(u-o[f])/(i-o[f]),y=vt(t[f]+(t[l]-t[f])*m),M=Math.max(e.at(-1).time+1/ut,Math.min(y,vt(t[l]-2/ut))),d=M+1/ut;d<t[l]-1e-6?e.push({time:M,value:p?180:-180,ease:"hold"},{time:d,value:p?-180:180}):e[e.length-1]={...e[e.length-1],ease:"hold"}}}e.push({time:t[l],value:Math.max(-180,Math.min(180,a(i,c)))})}return e}function Wn(t,o,s={}){const e=s.R??Eo();if(!e)throw Error("Physics engine not loaded yet — try again");const n=s.W??1280,a=s.H??720,r=pn/a,l=ro(s.at)?s.at:0,i=Math.max(1,Math.min(xt.max,ro(s.duration)?s.duration:xt.default)),c=s.mode==="above"?"above":"here",f=[...new Set(o)].map(b=>t.find(v=>v.id===b));if(!f.length)throw Error("physicsDrop needs clipIds");if(f.length>Rt)throw Error(`At most ${Rt} layers per drop`);for(const[b,v]of f.entries()){const R=dn(v);if(R)throw Error(`${v?.label??o[b]}: ${R}`)}const h=f.map(b=>{const v=vt(l-b.start);if(v<0||v>b.duration-2/ut)throw Error(`${b.label??b.id}: the playhead is outside the layer`);const R=wt(b,"x",v),T=wt(b,"y",v),D=wt(b,"rot",v),q=s.rectOf?.(b)??un(b,v,n,a),N=[R*n*r,-T*a*r],X=[(q.cx-n/2)*r,-(q.cy-a/2)*r],C=D*Math.PI/180,E=[X[0]-N[0],X[1]-N[1]],I=[E[0]*Math.cos(C)-E[1]*Math.sin(C),E[0]*Math.sin(C)+E[1]*Math.cos(C)];return{id:b.id,position:[N[0],N[1],0],rotation:[0,0,-D],half:[Math.max(4,q.w)*r/2,Math.max(4,q.h)*r/2,mn],offset:[I[0],I[1],0],local:v,x:R,y:T,rot:D}}),p=-a*r/2,u=a*r/2,m=n*r/2,y=s.walls?[{min:[-m-2,p,-2],max:[-m,u+20,2]},{min:[m,p,-2],max:[m+2,u+20,2]}]:[],M=b=>Math.hypot(b.half[0],b.half[1],b.half[2]),d=Math.max(...h.map(b=>b.position[1]+M(b))),x=Bt(e,{bodies:h,statics:y,floor:p,mode:c,bounciness:s.bounciness,duration:i,height:Math.max(.3,u-d+.05),seed:s.seed??[...o].sort().join(","),planar:!0}),g={},w=t.map(b=>({...b}));for(const b of h){const v=x.bodies.find(F=>F.id===b.id),R=w.findIndex(F=>F.id===b.id);let T=w[R];const D=[],q=[],N=[],X=[];let C=b.rot;for(const F of v.samples){const Q=vt(b.local+F.sec);if(Q>T.duration+1e-6)break;let k=-2*Math.atan2(F.q[2],F.q[3])*180/Math.PI;k+=360*Math.round((C-k)/360),C=k,D.push(Q),q.push(F.pivot[0]/r/n),N.push(-F.pivot[1]/r/a),X.push(k)}const E=c==="above"&&b.local>=1/ut,I={},V={},K=Math.min(...X),B=Math.max(...X),U=B-K<360?Math.ceil((B-180)/360)*360:0,dt=U&&K-U>=-180?X.map(F=>F-U):X,Mt=dt!==X&&b.local>=1/ut;for(const[F,Q,k]of[["x",q,Kt.xy],["y",N,Kt.xy],["rot",dt,Kt.rot]]){const yt=E||F==="rot"&&Mt,W=gt(T,F).filter(O=>O.time<b.local-(yt?1.5:.5)/ut),tt=yt?[{time:vt(b.local-1/ut),value:F==="x"?b.x:F==="y"?b.y:b.rot,...F==="rot"&&Mt?{ease:"hold"}:{}}]:[],ft=F==="rot"?Q.reduce((O,J,at)=>O+(at&&Math.floor((J+180)/360)!==Math.floor((Q[at-1]+180)/360)?1:0),0):0,rt=Nt-W.length-tt.length-2*ft;if(rt<2)throw Error(`${T.label??T.id}: too many ${F} keys before the playhead (or too many turns) to add the drop`);const ht=Qt(D,Q,k,rt),bt=it[F].min,j=it[F].max,z=F==="rot"?yn(D,Q,ht):ht.map(O=>({time:D[O],value:Math.max(bt,Math.min(j,Q[O]))})),et=[...W,...tt,...z].map(O=>({time:O.time,value:ao(O.value),...O.ease?{ease:O.ease}:{}}));T={...T,...mt(T,F,et.slice(0,Nt))},I[F]=z.length,V[F]=ao(Mn(D,Q,ht))}w[R]=T,g[b.id]={keys:I,error:V,settledAt:v.settledAt==null?null:Math.round((l+v.settledAt)*1e3)/1e3}}return{clips:w,report:g,settled:x.settled}}const $=Object.freeze({work:480,focals:[.45,.55,.7,.85,1,1.25],maxFrames:900,keyTolPx:.05,points:400,smooth:.3,smoothFrames:4,smoothTolPx:.25,dense:{cols:40,rows:22,keep:700,minFrames:8,max:1500},slide:.01}),P=t=>Math.round(t*1e6)/1e6,A=(t,o)=>[t[0]-o[0],t[1]-o[1],t[2]-o[2]],G=(t,o)=>[t[0]+o[0],t[1]+o[1],t[2]+o[2]],_=(t,o)=>[t[0]*o,t[1]*o,t[2]*o],S=(t,o)=>t[0]*o[0]+t[1]*o[1]+t[2]*o[2],st=(t,o)=>[t[1]*o[2]-t[2]*o[1],t[2]*o[0]-t[0]*o[2],t[0]*o[1]-t[1]*o[0]],nt=t=>{const o=Math.hypot(...t)||1;return _(t,1/o)},H=(t,o)=>[S(t[0],o),S(t[1],o),S(t[2],o)],pt=t=>[0,1,2].map(o=>[t[0][o],t[1][o],t[2][o]]),io=(t,o)=>t.map(s=>[0,1,2].map(e=>s[0]*o[0][e]+s[1]*o[1][e]+s[2]*o[2][e]));function gn(t,o,s,e=uo(29)){const n=t;if(n.length<20)return null;const a=n.map(r=>Math.hypot(...A(r,s))).sort((r,l)=>r-l)[n.length>>1];for(const r of[.015,.03,.05]){const l=a*r;let i=null;for(let u=0;u<2e3;u++){const m=n[Math.floor(e()*n.length)],y=n[Math.floor(e()*n.length)],M=n[Math.floor(e()*n.length)];let d=st(A(y,m),A(M,m));const x=Math.hypot(...d);if(x<1e-9||(d=_(d,1/x),S(d,o)<0&&(d=_(d,-1)),S(d,o)<Math.cos(35*Math.PI/180)))continue;const g=-S(d,m);if(S(d,s)+g<=l)continue;let w=0;for(const b of n)Math.abs(S(d,b)+g)<l&&w++;(!i||w>i.k)&&(i={n:d,d:g,k:w})}if(!i||i.k<n.length*.2)continue;const c=n.filter(u=>Math.abs(S(i.n,u)+i.d)<l),f=_(c.reduce((u,m)=>G(u,m),[0,0,0]),1/c.length),h=[[0,0,0],[0,0,0],[0,0,0]];for(const u of c){const m=A(u,f);for(let y=0;y<3;y++)for(let M=0;M<3;M++)h[y][M]+=m[y]*m[M]}let p=jt(h);return S(p,o)<0&&(p=_(p,-1)),{n:p,d:-S(p,f),count:c.length}}return null}function jt(t){const o=t.map(n=>[...n]),s=[[1,0,0],[0,1,0],[0,0,1]];for(let n=0;n<30;n++){let a=0;for(let r=0;r<3;r++)for(let l=r+1;l<3;l++)a+=o[r][l]**2;if(a<1e-24)break;for(let r=0;r<3;r++)for(let l=r+1;l<3;l++){if(Math.abs(o[r][l])<1e-30)continue;const i=(o[l][l]-o[r][r])/(2*o[r][l]),c=Math.sign(i||1)/(Math.abs(i)+Math.sqrt(i*i+1)),f=1/Math.sqrt(c*c+1),h=c*f;for(let p=0;p<3;p++){const u=o[p][r],m=o[p][l];o[p][r]=f*u-h*m,o[p][l]=h*u+f*m}for(let p=0;p<3;p++){const u=o[r][p],m=o[l][p];o[r][p]=f*u-h*m,o[l][p]=h*u+f*m}for(let p=0;p<3;p++){const u=s[p][r],m=s[p][l];s[p][r]=f*u-h*m,s[p][l]=h*u+f*m}}}const e=[0,1,2].reduce((n,a)=>o[a][a]<o[n][n]?a:n,0);return nt([s[0][e],s[1][e],s[2][e]])}function It(t,o,s,e){const n=new Map;t.poses.forEach((a,r)=>{if(!(!a||a.lost))for(const[l,i]of t.obs[r]){const c=t.points.get(l);if(!c)continue;const f=H(a.R,A(c,a.C)),h=f[2]>-1e-6?50:Math.hypot(s+o*f[0]/-f[2]-i[0],e-o*f[1]/-f[2]-i[1]),p=n.get(l)??[0,0,0];p[0]+=h,p[1]++,p[2]=Math.max(p[2],h),n.set(l,p)}});for(const a of n.values())a[0]/=a[1];return n}function _t(t,o,s,e){const n=[...It(t,o,s,e).values()].map(r=>r[0]).sort((r,l)=>r-l),a=Math.floor(n.length*.8);return a>30?n.slice(0,a).reduce((r,l)=>r+l,0)/a:1/0}function lo(t,o,s,e){const{solved:n,keyframes:a}=_o(t);for(let r=0;r<3;r++){const l=It(t,o,s,e),i=[...l.values()].map(h=>h[2]).sort((h,p)=>h-p),c=Math.max(1.2,2*(i[i.length>>1]??1));let f=0;for(const[h,p]of l)p[2]>c&&(t.points.delete(h),f++);if(!f)break;$t(t.poses,t.points,t.obs,a,o,s,e,8),jo(t,a,n,o,s,e)}}function _o(t){const o=t.poses.map((e,n)=>e&&!e.lost?n:-1).filter(e=>e>=0),s=Math.max(1,Math.ceil(o.length/60));return{solved:o,keyframes:[t.pair[0],...o.filter((e,n)=>e!==t.pair[0]&&(n%s===0||n===o.length-1))]}}function jo(t,o,s,e,n,a){const r=Math.hypot(...A(t.poses[t.pair[1]].C,t.poses[t.pair[0]].C))||1,l=new Set(o);for(const i of s){if(l.has(i))continue;const c=[];for(const[f,h]of t.obs[i])t.points.has(f)&&c.push({X:t.points.get(f),px:h});c.length>=10&&(t.poses[i]=Zo(c,t.poses[i],e,n,a,r).pose)}}function bn(t,o,s,e){const{solved:n,keyframes:a}=_o(t),r={poses:t.poses.map(c=>c&&{...c,R:c.R.map(f=>[...f]),C:[...c.C]}),points:new Map([...t.points].map(([c,f])=>[c,[...f]]))},l=_t(t,o,s,e),i=$t(t.poses,t.points,t.obs,a,o,s,e,12,{focal:!0});return Math.abs(i/o-1)<1e-4?o:(jo(t,a,n,i,s,e),Math.abs(i/o-1)<.15&&_t(t,i,s,e)<l?i:(t.poses=r.poses,t.points=r.points,o))}function xn(t,o,{w:s,h:e,mw:n,mh:a,focals:r=$.focals,focal:l=null,onProgress:i=null}={}){const c=s/2,f=e/2;let h=l,p=0;const u=l?1:r.length+9;if(!h){const C=new Map,E=Math.max(s,e)/s;let I=t;const V=j=>{if(j=Math.round(j*1e3)/1e3,!(j>.2&&j<3)||C.has(j))return;const z=to(null,j*s,{obs:I,w:s,h:e});z.mode==="free"&&lo(z,j*s,c,f),C.set(j,{s:z,e:z.mode==="free"?_t(z,j*s,c,f):1/0}),i?.(++p/u)};let K=[];for(let j=Math.max(1,Math.round(t.length/25));;j=Math.max(1,j>>1)){I=t.filter((z,et)=>et%j===0),C.clear(),p=0;for(const z of r)V(z*E);if(K=[...C.entries()].filter(([,z])=>Number.isFinite(z.e)).sort((z,et)=>z[0]-et[0]),K.length||j===1)break}if(!K.length)return i?.(1),null;const B=K.reduce((j,z,et)=>z[1].e<K[j][1].e?et:j,0),U=K[B][1].s,dt=B>0?K[B-1][0]:K[B][0]*.8,Mt=B<K.length-1?K[B+1][0]:K[B][0]*1.25,F=U.poses.map((j,z)=>j&&!j.lost?z:-1).filter(j=>j>=0),Q=U.pair[0],k=j=>{const z={...U,poses:U.poses.map(J=>J&&{...J,R:J.R.map(at=>[...at]),C:[...J.C]}),points:new Map([...U.points].map(([J,at])=>[J,[...at]]))};$t(z.poses,z.points,z.obs,[Q,...F.filter(J=>J!==Q)],j*s,c,f,8),i?.(++p/u);let et=0,O=0;for(const[J,at]of It(z,j*s,c,f).values())et+=J*at,O+=at;return O?et/O:1/0},yt=(Math.sqrt(5)-1)/2;let W=dt,tt=Mt,ft=tt-yt*(tt-W),rt=W+yt*(tt-W),ht=k(ft),bt=k(rt);for(let j=0;j<7;j++)ht<bt?(tt=rt,rt=ft,bt=ht,ft=tt-yt*(tt-W),ht=k(ft)):(W=ft,ft=rt,ht=bt,rt=W+yt*(tt-W),bt=k(rt));h=(W+tt)/2}const m=to(null,h*s,{obs:t,w:s,h:e});if(i?.(1),m.mode!=="free")return null;let y=h*s;const M=m.poses[m.pair[0]].C,d=m.poses[m.pair[0]].R;if(lo(m,y,c,f),!l)for(let C=0;C<3;C++){const E=bn(m,y,c,f),I=Math.abs(E/y-1)<5e-4;if(y=E,I)break}const x=It(m,y,c,f),g=[...x.values()].map(C=>C[0]).sort((C,E)=>C-E),w=Math.max(1.5,3*(g[g.length>>1]??1)),b=[...x.entries()].filter(([C,E])=>m.points.has(C)&&E[0]<=w&&E[1]>=2),v=C=>t.flatMap((E,I)=>E.has(C)?[o[I]]:[]);let R=0,T=0;for(const[,C]of b)R+=C[0]*C[1],T+=C[1];const D=H(pt(d),[0,1,0]),q=gn([...m.points.values()],D,M)??Qo(m.points,D,M)??Jo(m.points,D,M),N={v:1,w:s,h:e,mw:n||s,mh:a||e,mode:"free",f:P(y),err:P(T?R/T:_t(m,y,c,f)),frames:m.poses.map((C,E)=>[P(o[E]),...po(C.R).map(P),...C.C.map(P)]),floor:q?[...q.n.map(P),P(q.d)]:null},X=[];for(const[C]of b.sort((E,I)=>I[1][1]-E[1][1])){if(X.length>=$.points)break;const E=m.points.get(C),I=v(C);Do(N,E,I)<=$.slide*s&&X.push([E.map(P),[P(I[0]),P(I.at(-1))]])}return{...N,points:X.map(C=>C[0]),seen:X.map(C=>C[1])}}function Do(t,o,s){if(!s.length)return 1/0;const n=(s.length>24?s.filter((p,u)=>u%Math.ceil(s.length/24)===0||u===s.length-1):s).map(p=>nt(A(o,lt(t,p).C)));let a=0;for(const p of[n[0],n.at(-1)])for(const u of n)a=Math.max(a,Math.acos(Math.min(1,S(p,u))));const r=lt(t,s[s.length>>1]).C,l=Math.hypot(...A(o,r)),i=G(o,_(nt(A(o,r)),l*(.5/t.f)/Math.max(.001,Math.sin(a)))),c=t.frames,f=Math.max(1,Math.floor(c.length/40));let h=0;for(let p=0;p<c.length;p+=f){const u=ct(t,o,c[p][0]),m=ct(t,i,c[p][0]);u&&m&&(h=Math.max(h,Math.hypot(u[0]-m[0],u[1]-m[1])))}return h}function te(t,o,{mw:s,mh:e,focals:n=$.focals,focal:a=null,onProgress:r=null,dense:l=!0}={}){if(t.length<3)throw Error("FLAT");const i=t[0].w,c=t[0].h,f=mo(t,{anchor:!0,onProgress:r&&(u=>r(u*.35))}),h=xn(f,o,{w:i,h:c,mw:s,mh:e,focals:n,focal:a,onProgress:r&&(u=>r(.35+u*.57))});if(h){const u=l?wn(h,t,o,r&&(m=>r(.92+m*.08))):h;return r?.(1),u}const p=Go(t,(a??.7)*i);if(p.filter(u=>!u.lost).length<t.length*.5)throw Error("FLAT");return{v:1,w:i,h:c,mw:s||i,mh:e||c,mode:"tripod",frames:p.map((u,m)=>[P(o[m]),...u.q.map(P),P(u.f)])}}function wn(t,o,s,e=null,n=$.dense){const a=mo(o,{anchor:!0,keep:n.keep,cols:n.cols,rows:n.rows,onProgress:e&&(u=>e(u*.8))}),r=new Map;a.forEach((u,m)=>{for(const[y,M]of u){let d=r.get(y);d||r.set(y,d=[]),d.push([s[m],M[0],M[1]])}});const l=[...r.values()].filter(u=>u.length>=n.minFrames),i=t.points??[],c=u=>i.some(m=>Math.abs(m[0]-u[0])+Math.abs(m[1]-u[1])+Math.abs(m[2]-u[2])<1e-6),f=t.seen??i.map(()=>[t.frames[0][0],t.frames.at(-1)[0]]),h=[...i],p=[...f];for(const u of l){if(h.length>=n.max)break;const m=Fo(t,u);!m||c(m)||Do(t,m,u.map(y=>y[0]))>$.slide*t.w||(h.push(m.map(P)),p.push([P(u[0][0]),P(u.at(-1)[0])]))}return e?.(1),{...t,points:h,seen:p}}const vn=t=>!!t&&typeof t.ref=="string"&&!Array.isArray(t.frames);function oe(t){if(!t||t.v!==1||!["free","tripod"].includes(t.mode))throw Error("Invalid scene track");for(const e of["w","h","mw","mh"])if(!(Number.isFinite(t[e])&&t[e]>0&&t[e]<=2e4))throw Error("Invalid scene track");if(t.smooth!==void 0&&!(Number.isFinite(t.smooth)&&t.smooth>=0&&t.smooth<=1))throw Error("Invalid scene track");if(vn(t)){if(!/^[0-9a-f]{64}$/.test(t.ref))throw Error("Invalid scene track");return t}const o=t.mode==="free"?8:6;if(!Array.isArray(t.frames)||t.frames.length<2)throw Error("Invalid scene track");let s=-1/0;for(const e of t.frames){if(!Array.isArray(e)||e.length!==o||!e.every(Number.isFinite)||e[0]<s)throw Error("Invalid scene track");s=e[0]}if(t.mode==="free"&&(!(Number.isFinite(t.f)&&t.f>0)||t.floor!=null&&!(Array.isArray(t.floor)&&t.floor.length===4&&t.floor.every(Number.isFinite))||!Array.isArray(t.points)||t.points.length>5e3||!t.points.every(e=>Array.isArray(e)&&e.length===3&&e.every(Number.isFinite))||t.seen!=null&&!(Array.isArray(t.seen)&&t.seen.length===t.points.length&&t.seen.every(e=>Array.isArray(e)&&e.length===2&&e.every(Number.isFinite)&&e[0]<=e[1]))))throw Error("Invalid scene track");return t}function lt(t,o){const s=t.frames,e=s.length;let n=0,a=Math.max(0,e-2);for(;n<a;){const u=n+a+1>>1;s[u][0]<=o?n=u:a=u-1}const r=s[n],l=s[Math.min(e-1,n+1)],i=l[0]>r[0]?Math.max(0,Math.min(1,(o-r[0])/(l[0]-r[0]))):0;let c=r.slice(1,5),f=l.slice(1,5);Jt(c,f)<0&&(f=f.map(u=>-u));const h=En(c,f,i),p=ho(h);return t.mode==="tripod"?{R:p,C:[0,0,0],f:r[5]+(l[5]-r[5])*i}:{R:p,C:[0,1,2].map(u=>r[5+u]+(l[5+u]-r[5+u])*i),f:t.f}}const Jt=(t,o)=>t[0]*o[0]+t[1]*o[1]+t[2]*o[2]+t[3]*o[3];function En(t,o,s){const e=Math.min(1,Math.max(-1,Jt(t,o)));if(e>.9995){const i=t.map((f,h)=>f+(o[h]-f)*s),c=Math.hypot(...i);return i.map(f=>f/c)}const n=Math.acos(e),a=Math.sin(n),r=Math.sin((1-s)*n)/a,l=Math.sin(s*n)/a;return t.map((i,c)=>i*r+o[c]*l)}function An(t,o,s){const e=t.seen?.[o];if(!e)return!0;const n=t.frames.length>1?(t.frames.at(-1)[0]-t.frames[0][0])/(t.frames.length-1):0;return s>=e[0]-n-1e-4&&s<=e[1]+n+1e-4}const Zt=(t,o)=>(t.points??[]).map((s,e)=>[s,e]).filter(([,s])=>An(t,s,o));function ct(t,o,s){const e=lt(t,s),n=H(e.R,t.mode==="tripod"?o:A(o,e.C));return n[2]>-1e-6?null:[t.w/2+e.f*n[0]/-n[2],t.h/2-e.f*n[1]/-n[2]]}function Dt(t,o,s,e){const n=lt(t,e),a=nt([(o-t.w/2)/n.f,-(s-t.h/2)/n.f,-1]);return{o:n.C,d:H(pt(n.R),a),pose:n}}function Cn(t,o,s,e,{radius:n=null}={}){const{o:a,d:r}=Dt(t,o,s,e);if(t.mode==="tripod")return r.map(P);const l=n??Math.max(10,t.w*.04),i=[];for(const[h]of Zt(t,e)){const p=ct(t,h,e);if(!p)continue;const u=Math.hypot(p[0]-o,p[1]-s),m=S(A(h,a),r);m>0&&u<=t.w*.15&&i.push({X:h,depth:m,dist:u,w:1/(1+u)})}i.sort((h,p)=>h.dist-p.dist);const c=i.filter((h,p)=>h.dist<=l||p<6);let f=null;if(c.length>=3&&(f=Pn(c,a,r)),f===null&&c.length>=2){c.sort((u,m)=>u.depth-m.depth);const h=c.reduce((u,m)=>u+m.w,0);let p=0;for(const u of c)if(p+=u.w,p>=h/2){f=u.depth;break}}else if(f===null&&t.floor){const h=t.floor.slice(0,3),p=S(h,r);if(Math.abs(p)>1e-6){const u=-(S(h,a)+t.floor[3])/p;u>0&&(f=u)}}if(f===null){const h=(t.points??[]).map(p=>S(A(p,a),r)).filter(p=>p>0).sort((p,u)=>p-u);f=h.length?h[h.length>>1]:1}return G(a,_(r,f)).map(P)}function Pn(t,o,s){const e=t.slice(0,20),n=e.map(d=>d.depth).sort((d,x)=>d-x),a=n[n.length>>1]*.015,r=e.reduce((d,x)=>d+x.w,0);let l=null,i=0;for(let d=0;d<e.length;d++)for(let x=d+1;x<e.length;x++)for(let g=x+1;g<e.length;g++){let w=st(A(e[x].X,e[d].X),A(e[g].X,e[d].X));const b=Math.hypot(...w);if(b<1e-12)continue;w=_(w,1/b);const v=e.filter(T=>Math.abs(S(w,A(T.X,e[d].X)))<a),R=v.reduce((T,D)=>T+D.w,0);R>i&&(l=v,i=R)}if(!l||l.length<3||i<r*.5)return null;const c=_(l.reduce((d,x)=>G(d,x.X),[0,0,0]),1/l.length),f=[[0,0,0],[0,0,0],[0,0,0]];for(const{X:d}of l){const x=A(d,c);for(let g=0;g<3;g++)for(let w=0;w<3;w++)f[g][w]+=x[g]*x[w]}if(!(f[0][0]+f[1][1]+f[2][2]>0))return null;const p=jt(f),u=S(p,s);if(Math.abs(u)<.05)return null;const m=Math.min(...l.map(d=>d.depth)),y=Math.max(...l.map(d=>d.depth)),M=S(p,A(c,o))/u;return M>=m*.7&&M<=y*1.4?M:null}function Sn(t,o,s,e){if(t.mode!=="free"||!t.floor)return null;const{o:n,d:a}=Dt(t,o,s,e),r=t.floor.slice(0,3),l=S(r,a);if(Math.abs(l)<1e-6)return null;const i=-(S(r,n)+t.floor[3])/l;return i>0?G(n,_(a,i)):null}function ne(t,o){return t.mode!=="free"?[]:o.map(s=>Fo(t,s)).filter(Boolean)}function Fo(t,o){if(t.mode!=="free"||o.length<4)return null;const s=o.map(([l,i,c])=>Dt(t,i,c,l));let e=0;for(const l of s)e=Math.max(e,Math.acos(Math.min(1,S(l.d,s[0].d))));if(e<.5*Math.PI/180)return null;const n=[[0,0,0],[0,0,0],[0,0,0]],a=[0,0,0];for(const{o:l,d:i}of s)for(let c=0;c<3;c++)for(let f=0;f<3;f++){const h=(c===f?1:0)-i[c]*i[f];n[c][f]+=h,a[c]+=h*l[f]}const r=Tn(n,a);return r&&o.every(([l,i,c])=>{const f=ct(t,r,l);return f&&Math.hypot(f[0]-i,f[1]-c)<1.5})?r:null}function Tn(t,o){const s=n=>n[0][0]*(n[1][1]*n[2][2]-n[1][2]*n[2][1])-n[0][1]*(n[1][0]*n[2][2]-n[1][2]*n[2][0])+n[0][2]*(n[1][0]*n[2][1]-n[1][1]*n[2][0]),e=s(t);return Math.abs(e)<1e-12?null:[0,1,2].map(n=>s(t.map((a,r)=>a.map((l,i)=>i===n?o[r]:l)))/e)}function ee(t,o,s,{extra:e=[]}={}){if(t.mode!=="free")return null;const n=(p,u)=>{let m=0;for(let y=0;y<4;y++){const[M,d]=o[y],[x,g]=o[(y+1)%4],w=(x-M)*(u-d)-(g-d)*(p-M);if(w){if(m&&Math.sign(w)!==m)return!1;m=Math.sign(w)}}return!0},a=lt(t,s),r=[...e,...Zt(t,s).map(([p])=>p)].filter(p=>{const u=ct(t,p,s);return u&&n(u[0],u[1])}),l=o.map(([p,u])=>Dt(t,p,u,s)),i=(p,u)=>{const m=[];for(const{o:y,d:M}of l){const d=S(p,M);if(Math.abs(d)<1e-9)return null;const x=-(S(p,y)+u)/d;if(!(x>0))return null;m.push(G(y,_(M,x)))}return m};let c=null,f="points";if(r.length>=4){const p=r.map(y=>Math.hypot(...A(y,a.C))).sort((y,M)=>y-M)[r.length>>1],u=p*.015;let m=null;for(let y=0;y<r.length;y++)for(let M=y+1;M<r.length;M++)for(let d=M+1;d<r.length&&d<M+12;d++){let x=st(A(r[M],r[y]),A(r[d],r[y]));const g=Math.hypot(...x);if(g<1e-12)continue;x=_(x,1/g);const w=-S(x,r[y]);let b=0;for(const v of r)Math.abs(S(x,v)+w)<u&&b++;(!m||b>m.c)&&(m={n:x,d:w,c:b})}if(m&&m.c>=4){const y=r.filter(g=>Math.abs(S(m.n,g)+m.d)<u),M=_(y.reduce((g,w)=>G(g,w),[0,0,0]),1/y.length),d=[[0,0,0],[0,0,0],[0,0,0]];for(const g of y){const w=A(g,M);for(let b=0;b<3;b++)for(let v=0;v<3;v++)d[b][v]+=w[b]*w[v]}let x=jt(d);S(x,A(a.C,M))<0&&(x=_(x,-1)),c={n:x,d:-S(x,M),count:y.length}}}if(!c){const p=o.reduce((d,x)=>d+x[0],0)/4,u=o.reduce((d,x)=>d+x[1],0)/4,m=Cn(t,p,u,s),y=t.floor?Sn(t,p,u,s):null,M=Math.hypot(...A(m,a.C));if(y&&Math.abs(Math.hypot(...A(y,a.C))-M)<M*.12)c={n:t.floor.slice(0,3),d:t.floor[3],count:0},f="ground";else{const d=t.floor?t.floor.slice(0,3):H(pt(a.R),[0,1,0]),x=A(a.C,m);let g=A(x,_(d,S(x,d)));Math.hypot(...g)<1e-9&&(g=x),g=nt(g),c={n:g,d:-S(g,m),count:0},f="upright"}}const h=i(c.n,c.d);return h?{corners:h.map(p=>p.map(P)),n:c.n.map(P),d:P(c.d),count:c.count,by:f}:null}function se(t,o,s,e=null){const n=o.crop||{left:0,right:0,top:0,bottom:0},a=1-n.left-n.right,r=1-n.top-n.bottom,l=[];for(const i of e??t.frames.map(c=>c[0])){const c=s.map(f=>ct(t,f,i));c.some(f=>!f)||l.push([+i.toFixed(4),...c.flatMap(([f,h])=>[P((f/t.w-n.left)/a),P((h/t.h-n.top)/r)]),1])}return l}function re(t,o,s,e){const n=o.crop||{left:0,right:0,top:0,bottom:0};return[(n.left+s*(1-n.left-n.right))*t.w,(n.top+e*(1-n.top-n.bottom))*t.h]}function Ot(t,o,s,e,n,a,r){const l=o.crop||{left:0,right:0,top:0,bottom:0},i=(s/t.w-l.left)/(1-l.left-l.right),c=(e/t.h-l.top)/(1-l.top-l.bottom),f=$o(o,i,c,n,a,r,t.mw,t.mh);return[f.px,f.py]}function ae(t,o,s,e,n,a){const r=Math.max(t.start,o.start),l=Math.min(t.start+t.duration,o.start+o.duration);if(!(l>r))return{};const i=[],c=[],f=[r,...s.frames.map(p=>o.start+p[0]).filter(p=>p>r+1e-4&&p<l-1e-4),l];for(const p of f){const u=ct(s,e,p-o.start);if(!u)continue;const[m,y]=Ot(s,o,u[0],u[1],p-o.start,n,a),M=+(p-t.start).toFixed(4);i.push({time:M,value:P((m-n/2)/n)}),c.push({time:M,value:P((y-a/2)/a)})}if(!i.length)return{};let h={...t,motionV:2};return delete h.posX,delete h.posY,h={...h,...mt(h,"x",co(i,$.keyTolPx/n))},h={...h,...mt(h,"y",co(c,$.keyTolPx/a))},{motionV:2,motionKeys:h.motionKeys,posX:void 0,posY:void 0}}const co=(t,o)=>Qt(t.map(s=>s.time),t.map(s=>s.value),o,1/0).map(s=>t[s]);function Rn(t,o,s){if(!(s>0)||o.length<3)return o;const e=s*$.smoothFrames,n=Math.ceil(e*2.5),a=t.mode==="free",r=$.smoothTolPx,l=a?(t.points??[]).filter((h,p)=>p%Math.max(1,Math.floor((t.points?.length??0)/80))===0):[-.4,0,.4].flatMap(h=>[-.25,0,.25].map(p=>[h,p,-1])),i=(h,p)=>{const u=H(h.R,a?A(p,h.C):H(pt(o[0].R),p));return u[2]>-1e-6?null:[h.f*u[0]/-u[2],-h.f*u[1]/-u[2]]},c=(h,p)=>{let u=0,m=0;for(const y of l){const M=i(h,y),d=i(p,y);M&&d&&(u=Math.max(u,Math.hypot(M[0]-d[0],M[1]-d[1])),m++)}return m?u:0},f=o.map(h=>po(h.R));return o.map((h,p)=>{let u=0,m=[0,0,0],y=[0,0,0,0],M=0;for(let b=Math.max(0,p-n);b<=Math.min(o.length-1,p+n);b++){const v=Math.exp(-((b-p)**2)/(2*e*e)),R=Jt(f[b],f[p])<0?f[b].map(T=>-T):f[b];u+=v,m=G(m,_(o[b].C,v)),y=y.map((T,D)=>T+R[D]*v),M+=o[b].f*v}const d=b=>{const v=f[p].map((T,D)=>T+(y[D]/u-T)*b),R=Math.hypot(...v);return{R:ho(v.map(T=>T/R)),C:G(h.C,_(A(_(m,1/u),h.C),b)),f:h.f+(M/u-h.f)*b}};let x=1,g=d(1),w=c(h,g);for(let b=0;b<6&&w>r;b++)x*=Math.max(.3,r/w*.95),g=d(x),w=c(h,g);return w>r?h:g})}function zo(t,o,s,e){const n=Uo(s,e),a=lt(t,t.frames[0][0]),r=t.mode==="free",l=r&&t.floor?t.floor.slice(0,3):H(pt(a.R),[0,1,0]),i=H(pt(a.R),[0,0,-1]),c=[0,1,2].map(E=>{const I=(t.points??[]).map(V=>V[E]).sort((V,K)=>V-K);return I.length?I[I.length>>1]:a.C[E]+(r?0:i[E])}),f=r&&t.floor?A(c,_(t.floor.slice(0,3),S(t.floor.slice(0,3),c)+t.floor[3])):c,h=nt(A(i,_(l,S(i,l)))),u=[st(_(l,-1),h),_(l,-1),h],m=8*n/Math.max(1e-9,Math.hypot(...A(a.C,f))),y=E=>_(H(u,A(E,f)),m),M=Ot(t,o,t.w/2,t.h/2,0,s,e),d=Ot(t,o,t.w/2+10,t.h/2,0,s,e),x=Math.hypot(d[0]-M[0],d[1]-M[1])/10,g=[[1,0,0],[0,-1,0],[0,0,-1]],w=t.frames.filter(E=>E[0]>=-1e-6&&E[0]<=o.duration+1e-6),b=[],v={},R=(E,I)=>{if(E===void 0)return I;for(;I-E>180;)I-=360;for(;I-E<-180;)I+=360;return I},T=Rn(t,w.map(E=>lt(t,E[0])),t.smooth??0);for(const[E,I]of w.entries()){const V=T[E],K=io(io(u,pt(V.R)),g),B=y(V.C),U=Vo(K),dt=R(v.pan,U.pan),Mt=R(v.roll,U.roll);v.pan=dt,v.roll=Mt,b.push({time:+(o.start+I[0]).toFixed(4),cx:P(B[0]/s),cy:P(B[1]/e),cz:P(B[2]/n),pan:P(dt),tilt:P(U.tilt),roll:P(Mt),zoom:P(V.f*x/(5*n))})}const D=5*n*(b[0]?.zoom??1),q=$.keyTolPx/D*180/Math.PI,N={cx:$.keyTolPx/s,cy:$.keyTolPx/e,cz:$.keyTolPx/n,pan:q,tilt:q,roll:q,zoom:$.keyTolPx/D},X=new Set;for(const[E,I]of Object.entries(N))for(const V of Qt(b.map(K=>K.time),b.map(K=>K[E]),I,1/0))X.add(V);return{camera:{enabled:!0,motionV:2,motionKeys:b.filter((E,I)=>X.has(I))},toTimeline:y,fromTimeline:E=>G(H(pt(u),_(E,1/m)),f),M:u,k:m,U:n,first:a}}function In(t,o,s,e,n,{size:a=null,fill:r=1}={}){if(t.mode!=="free")throw Error("TRIPOD");const{camera:l,toTimeline:i,k:c,U:f,first:h}=zo(t,o,e,n),p=(a??Math.hypot(...A(h.C,s))*.2)*c*r,u=i(s),m=A(i(h.C),u),y=Math.atan2(m[0],-m[2])*180/Math.PI;return{camera:l,place:{is3D:!0,posX:P(u[0]/e),posY:P(u[1]/n),spatialDepth:P(-u[2]/f),modelDepth:P(-u[2]/f),modelGround:!0,modelRotation:{x:0,y:P(y),z:0},scale:P(Math.max(1e-6,p/(1.8*f)))},toTimeline:i}}function _n(t,o,s,e,n){const a=[];for(const[r,l]of Zt(t,e)){const i=ct(t,r,e);if(i){const c=Math.hypot(i[0]-o,i[1]-s);c<=n&&a.push([l,c])}}return a.sort((r,l)=>r[1]-l[1]).map(r=>r[0])}function ie(t,o,s){if(!o.length)return null;const e=lt(t,s),n=t.mode==="free"?e.C:[0,0,0],a=h=>_(h.reduce((p,u)=>G(p,u),[0,0,0]),1/h.length);let r=o.map(h=>t.mode==="free"?h:nt(h)),l=a(r),i=r,c="points";if(t.mode==="free"&&r.length<3){const h=ct(t,l,s),p=Math.hypot(...A(l,n));if(h){const u=_n(t,h[0],h[1],s,t.w*.12).map(d=>t.points[d]).filter(d=>Math.abs(Math.hypot(...A(d,n))/p-1)<.25).slice(0,14),m=p*.02,y=r[0];let M=null;for(let d=0;d<u.length;d++)for(let x=d+1;x<u.length;x++){let g=st(A(u[d],y),A(u[x],y));const w=Math.hypot(...g);if(w<1e-12)continue;g=_(g,1/w);const b=u.filter(v=>Math.abs(S(g,A(v,y)))<m);r.every(v=>Math.abs(S(g,A(v,y)))<m)&&(!M||b.length>M.length)&&(M=b)}M&&M.length>=3&&(i=[...r,...M],c="around")}}let f=null;if(t.mode==="free"&&i.length>=3){if(c==="points"&&i.length>=6){const u=Math.hypot(...A(l,n)),m=u*.01,y=uo(31);let M=null;for(let d=0;d<300;d++){const x=i[Math.floor(y()*i.length)],g=i[Math.floor(y()*i.length)],w=i[Math.floor(y()*i.length)];let b=st(A(g,x),A(w,x));const v=Math.hypot(...b);if(v<1e-12)continue;b=_(b,1/v);const R=i.filter(T=>Math.abs(S(b,A(T,x)))<m);(!M||R.length>M.length)&&(M=R)}M&&M.length>=Math.max(3,i.length*.5)&&(i=M)}const h=a(i),p=[[0,0,0],[0,0,0],[0,0,0]];for(const u of i){const m=A(u,h);for(let y=0;y<3;y++)for(let M=0;M<3;M++)p[y][M]+=m[y]*m[M]}f=jt(p),c!=="around"&&(l=h)}return f||(f=nt(A(n,l)),c="facing"),S(f,A(n,l))<0&&(f=_(f,-1)),{center:l.map(P),n:f.map(P),radius:P(Math.hypot(...A(l,n))*.08),by:c}}function le(t,o=t.radius,s=36){const e=t.n,n=nt(Math.abs(e[1])<.9?st(e,[0,1,0]):st(e,[1,0,0])),a=st(e,n);return Array.from({length:s},(r,l)=>{const i=l/s*2*Math.PI;return G(t.center,G(_(n,o*Math.cos(i)),_(a,o*Math.sin(i))))})}const jn=["x","y","depth","zoom","rot","tiltX","tiltY"];function ce(t,o,s,e,n,a,{width:r=null}={}){if(t.kind==="model"){const{camera:C,place:E}=In(o,s,e.center,n,a,{size:e.radius*2});if(!(E.spatialDepth>=kt&&E.spatialDepth<=Wt))throw Error("FAR");return{camera:C,patch:E}}const{camera:l,toTimeline:i,M:c,k:f,U:h,first:p}=zo(o,s,n,a),u=i(e.center),m=nt(H(c,e.n)),y=i(o.mode==="free"?lt(o,o.frames[0][0]).C:p.C),M=-u[2]/h;if(!(M>=kt&&M<=Wt))throw Error("FAR");const d=C=>A(C,_(m,S(C,m)));let x=Math.abs(m[1])<.85?d([0,1,0]):d(A(y,u));Math.hypot(...x)<1e-6&&(x=d([0,1,0])),x=nt(x);const g=_(m,-1),w=st(x,g),b=Math.asin(Math.max(-1,Math.min(1,g[1]))),v=Math.atan2(-g[0],g[2]),R=[Math.cos(v),0,Math.sin(v)],T=[Math.sin(b)*Math.sin(v),Math.cos(b),-Math.sin(b)*Math.cos(v)],D=Math.atan2(S(w,T),S(w,R)),q=C=>P(C*180/Math.PI);let N={...t,motionV:2};for(const C of jn)(N.motionKeys??[]).some(E=>E[C]!==void 0)&&(N={...N,motionKeys:Bo(N,C,[])});const X=(r??e.radius*6)*f;return{camera:l,patch:{is3D:!0,motionV:2,motionKeys:N.motionKeys??[],posX:P(u[0]/n),posY:P(u[1]/a),spatialDepth:P(M),scale:P(Math.max(1e-4,X/n)),rotation:q(D),tiltX:q(b),tiltY:q(v)}}}export{vn as A,oe as B,re as C,Jn as D,Sn as E,Cn as F,Xn as G,In as H,ae as I,ne as J,ee as K,se as L,te as M,Qt as N,lt as O,hn as P,kn as Q,Wn as R,$ as S,un as T,Kn as U,An as V,_n as W,zn as X,ce as a,Ot as b,le as c,Zn as d,Qn as e,Un as f,Yn as g,On as h,Hn as i,Vn as j,$n as k,Bn as l,Gn as m,Nn as n,Ln as o,ct as p,qn as q,Ao as r,zo as s,ie as t,dn as u,nn as v,Ho as w,Wo as x,ko as y,Fn as z};
1
+ import{_ as qo,fL as Ct,dX as Ht,dY as wt,ca as Yt,dW as gt,nc as Ko,c_ as mt,hx as Xo,m0 as Lo,ff as it,f6 as No,fM as fo,fy as Oo,iB as Yo,au as vt,F as ut,cw as $o,l6 as ho,nd as uo,ne as Vo,i7 as kt,i6 as Wt,l8 as Bo,fa as Uo,la as po,nf as mo,ng as Go,nh as to,ni as Qo,nj as Zo,l9 as $t,lb as Jo}from"./titleFrameBudget-DsM0V482.js";const Ft=.1,zt=t=>Math.abs(t)>180?(t%360+540)%360-180:t;function Mo(t,o,s,e,n,a,r,l,i){const c=l-r,f=t==null?s-o:(s-t)/Math.max(1e-9,l-a)*c,h=e==null?s-o:(e-o)/Math.max(1e-9,i-r)*c,p=n*n,u=p*n;return(2*u-3*p+1)*o+(u-2*p+n)*f+(-2*u+3*p)*s+(u-p)*h}function yo(t,o,s,e,n,a,r,l,i){const c=o+zt(s-o),f=t==null?null:o+zt(t-o),h=e==null?null:c+zt(e-c);return Mo(f,o,c,h,n,a,r,l,i)}function Ho(t,o,s=e=>e.time){if(globalThis.__clipooxLinearKeys)return!1;const e=(n,a)=>s(t[a])-s(t[n]);return!(e(o,o+1)>Ft||o>0&&e(o-1,o)>Ft||o+2<t.length&&e(o+1,o+2)>Ft)}function Vt(t,o,s,e){let n=o>0,a=o+2<t.length;if(e!=null){const l=n?s(t[o-1]):null,i=s(t[o]),c=s(t[o+1]),f=a?s(t[o+2]):null;if(i<e-1e-9&&c>=e-1e-9)return{before:!1,after:!1};a&&c<e-1e-9&&f>=e-1e-9&&(a=!1),n&&i>=e-1e-9&&l<e-1e-9&&(n=!1)}return{before:n,after:a}}function Fn(t,o,s,e=r=>r.pose,n=r=>r.time,a=null){const r=Vt(t,o,n,a),l=e(t[o]),i=e(t[o+1]),c=r.before?e(t[o-1]):null,f=r.after?e(t[o+2]):null,h=n(t[o]),p=n(t[o+1]),u=c?n(t[o-1]):h-(p-h),m=f?n(t[o+2]):p+(p-h),y={};for(const M of new Set([...Object.keys(l),...Object.keys(i)])){const d=l[M]??0,x=i[M]??0;if(typeof d!="number"||typeof x!="number")continue;const g=c&&typeof c[M]=="number"?c[M]:null,w=f&&typeof f[M]=="number"?f[M]:null;y[M]=yo(g,d,x,w,s,u,h,p,m)}return y}function ko(t,o,s,e=r=>r.position,n=r=>r.time,a=null){const r=Vt(t,o,n,a),l=e(t[o]),i=e(t[o+1]),c=r.before?e(t[o-1]):null,f=r.after?e(t[o+2]):null,h=n(t[o]),p=n(t[o+1]),u=c?n(t[o-1]):h-(p-h),m=f?n(t[o+2]):p+(p-h);return l.map((y,M)=>Mo(c?c[M]:null,y,i[M],f?f[M]:null,s,u,h,p,m))}function Wo(t,o,s,e,n=r=>r.time,a=null){const r=Vt(t,o,n,a),l=e(t[o]),i=e(t[o+1]),c=r.before?e(t[o-1]):null,f=r.after?e(t[o+2]):null,h=n(t[o]),p=n(t[o+1]),u=c!=null?n(t[o-1]):h-(p-h),m=f!=null?n(t[o+2]):p+(p-h);return yo(c,l,i,f,s,u,h,p,m)}const Et=30,oo=4,xt=Object.freeze({min:.25,max:6,default:3}),Rt=40,go=300,bo=Object.freeze({position:.01,rotation:2}),xo=80,wo=Object.freeze(["here","above","explode","throw"]),Xt=Object.freeze({bouncy:Object.freeze({restitution:.6,friction:.45}),normal:Object.freeze({restitution:.25,friction:.6}),heavy:Object.freeze({restitution:.04,friction:.9})}),tn=9.81;let qt=null,vo=null;const Eo=()=>vo;function Ao(){return qt??=(async()=>{const t=await qo(()=>import("./rapier-DBjBuyhF.js"),[]),o=t.default??t,s=console.warn;console.warn=(...e)=>{String(e[0]).includes("deprecated parameters")||s(...e)};try{await o.init()}finally{console.warn=s}return vo=o,o})().catch(t=>{throw qt=null,t}),qt}const ot=t=>typeof t=="number"&&Number.isFinite(t),Y=(t,o)=>Array.isArray(t)&&t.length===3&&t.every(ot)?[t[0],t[1],t[2]]:o,J=Math.PI/180,St=t=>(t%360+540)%360-180,Tt=t=>Math.round(t*1e4)/1e4,Lt=t=>Math.round(t*100)/100;function At(t){const o=Math.cos(t[0]*J/2),s=Math.cos(t[1]*J/2),e=Math.cos(t[2]*J/2),n=Math.sin(t[0]*J/2),a=Math.sin(t[1]*J/2),r=Math.sin(t[2]*J/2);return[n*s*e+o*a*r,o*a*e-n*s*r,o*s*r+n*a*e,o*s*e-n*a*r]}function Co(t){const[o,s,e,n]=t,a=1-2*(s*s+e*e),r=2*(o*s-e*n),l=2*(o*e+s*n),i=1-2*(o*o+e*e),c=2*(s*e-o*n),f=2*(s*e+o*n),h=1-2*(o*o+s*s),p=Math.asin(Math.max(-1,Math.min(1,l)));return Math.abs(l)<.9999999?[Math.atan2(-c,h)/J,p/J,Math.atan2(-r,a)/J]:[Math.atan2(f,i)/J,p/J,0]}const no=(t,o)=>{const[s,e,n,a]=t,r=a*o[0]+e*o[2]-n*o[1],l=a*o[1]+n*o[0]-s*o[2],i=a*o[2]+s*o[1]-e*o[0],c=-s*o[0]-e*o[1]-n*o[2];return[r*a+c*-s+l*-n-i*-e,l*a+c*-e+i*-s-r*-n,i*a+c*-n+r*-e-l*-s]},Po=(t,o)=>2*Math.acos(Math.min(1,Math.abs(t[0]*o[0]+t[1]*o[1]+t[2]*o[2]+t[3]*o[3])))/J;function So(t){let o=2166136261;for(const e of String(t))o=Math.imul(o^e.codePointAt(0),16777619);let s=o>>>0;return()=>{s=s+1831565813|0;let e=Math.imul(s^s>>>15,1|s);return e=e+Math.imul(e^e>>>7,61|e)^e,((e^e>>>14)>>>0)/4294967296}}const eo=new Set(["box","sphere","cylinder","wall","platform"]),on=new Set(["person","fx","splat","set","floor"]);function nn(t){return t?t.locked?"Đối tượng đang khoá":t.hold?"Đồ đang cầm đi theo tay người cầm":t.kind==="person"||t.kind==="model"&&(t.skeleton==="native"||t.rig?.kind==="humanoid")?"Nhân vật không rơi như vật thể — dùng chuyển động":on.has(t.kind)?"Loại đối tượng này không rơi được":null:"Chưa chọn đối tượng"}function en(t){const o=Y(t.scale,[1,1,1]);return t.kind==="sphere"&&Math.abs(o[0]-o[1])<1e-6&&Math.abs(o[1]-o[2])<1e-6||t.kind==="prop"&&t.prop?.kind==="ball"?"ball":t.kind==="cylinder"&&Math.abs(o[0]-o[2])<1e-6?"cylinder":"box"}function sn(t){const o=Y(t.scale,[1,1,1]).map(Math.abs),s=Y(t.mirror,[1,1,1]),e=Y(t.origin,[0,0,0]);let n=[1,1,1],a=[0,.5,0];if(t.kind==="proxy"&&t.proxy?.dims)n=Y(t.proxy.dims,n),a=[0,n[1]/2,0];else if(t.kind==="prop"){const i=t.prop?.kind==="ball"?.12:.15;n=[2*i,2*i,2*i],a=[0,t.prop?.kind==="ball"?0:i,0]}else if(t.kind==="model"){const i=t.model?.height??2;n=[i*.6,i,i*.6],a=[0,i/2,0]}else eo.has(t.kind)||(n=[.5,.5,.5],a=[0,.25,0]);const r=eo.has(t.kind)?[(a[0]-e[0])*s[0],(a[1]-e[1])*s[1],(a[2]-e[2])*s[2]]:a,l=i=>[i[0]*(t.scale?.[0]??1),i[1]*(t.scale?.[1]??1),i[2]*(t.scale?.[2]??1)];return{half:[n[0]*o[0]/2,n[1]*o[1]/2,n[2]*o[2]/2],offset:l(r)}}function rn(t,o){let s=0,e=t.length-1;for(;e-s>1;){const n=s+e>>1;t[n].time<=o?s=n:e=n}return s}function To(t,o,s=null,e=null){const n=t[0],a=t[t.length-1];if(o<=n.time)return{position:n.position,rotation:n.rotation};if(o>=a.time)return{position:a.position,rotation:a.rotation};const r=rn(t,o),l=t[r],i=t[r+1],c=(o-l.time)/(i.time-l.time),f=h=>l.rotation[h]+St(i.rotation[h]-l.rotation[h])*c;return Ho(t,r,e>0?h=>h.time*e:void 0)?{position:ko(t,r,c,h=>h.position,h=>h.time,s),rotation:[f(0),Wo(t,r,c,h=>h.rotation[1],h=>h.time,s),f(2)]}:{position:[0,1,2].map(h=>l.position[h]+(i.position[h]-l.position[h])*c),rotation:[f(0),f(1),f(2)]}}function Ro(t,{tolerance:o=bo,maxKeys:s=xo,before:e=null,seam:n=null,duration:a=null}={}){const r=t.length;if(r<=2)return{keys:t.map(({time:f,position:h,rotation:p})=>({time:f,position:h,rotation:p})),error:{position:0,rotation:0}};const l=new Set([0,r-1]),i=()=>[...l].sort((f,h)=>f-h).map(f=>t[f]);let c={position:0,rotation:0};for(;;){const f=i(),h=e?[e,...f]:f;let p=-1,u=1;c={position:0,rotation:0};for(let m=1;m<r-1;m++){if(l.has(m))continue;const y=t[m],M=To(h,y.time,n,a),d=Math.hypot(M.position[0]-y.position[0],M.position[1]-y.position[1],M.position[2]-y.position[2]),x=Po(At(M.rotation),y.quat);c={position:Math.max(c.position,d),rotation:Math.max(c.rotation,x)};const g=Math.max(d/o.position,x/o.rotation);g>u&&(u=g,p=m)}if(p<0||l.size>=s)break;l.add(p)}return{keys:i().map(({time:f,position:h,rotation:p})=>({time:f,position:h,rotation:p})),error:{position:Tt(c.position),rotation:Lt(c.rotation)}}}const an=t=>ot(t)?{restitution:Math.max(0,Math.min(.95,t)),friction:.6}:Xt[t]??Xt.normal;async function ln(t){const o=Bt(await Ao(),t),{duration:s,mode:e}=o,n=ot(t.start)?Math.max(0,t.start):0,a=ot(t.shotDuration)&&t.shotDuration>0?t.shotDuration:n+s,r={},l=n/a;for(const i of o.bodies){let c=null;const f=i.samples.map(m=>{let y=Co(m.q);if(c){const M=[y[0]+180,180-y[1],y[2]+180],d=x=>x.reduce((g,w,b)=>g+Math.abs(St(w-c[b])),0);d(M)<d(y)&&(y=M)}return y=y.map(M=>Lt(St(M)===-180?180:St(M))),c=y,{time:l+m.sec/a,position:m.pivot.map(Tt),rotation:y,quat:At(y)}}),h=t.before?.[i.id],{keys:p,error:u}=Ro(f,{before:h&&ot(h.time)&&h.time<l?h:null,seam:ot(t.seam)?t.seam:null,duration:a});r[i.id]={keys:p,error:u,settledAt:i.settledAt,from:{position:i.position.map(Tt),rotation:i.rotation.map(Lt)},rest:f[f.length-1].position,restCentre:i.samples[i.samples.length-1].centre.map(Tt),restQuat:i.samples[i.samples.length-1].q}}return{tracks:r,settled:o.settled,seconds:s,mode:e}}function Bt(t,o){const s=!!o.planar,e=wo.includes(o.mode)?o.mode:"here",n=Math.max(xt.min,Math.min(xt.max,ot(o.duration)?o.duration:xt.default)),a=ot(o.floor)?o.floor:0,r=an(o.bounciness),l=So(`${o.seed??"drop"}|${e}`),i=m=>(l()*2-1)*m,c=(o.bodies||[]).filter(m=>m&&m.id!=null).map(m=>({id:String(m.id),shape:["ball","cylinder"].includes(m.shape)?m.shape:"box",position:Y(m.position,[0,0,0]),rotation:Y(m.rotation,[0,0,0]),half:Y(m.half,[.25,.25,.25]).map(y=>Math.max(.01,Math.abs(y))),offset:Y(m.offset,[0,0,0])})).sort((m,y)=>m.id<y.id?-1:m.id>y.id?1:0).slice(0,Rt);if(!c.length)throw new Error("no objects to drop");const f=m=>Math.hypot(...m.half);for(const m of c){m.q=At(m.rotation);const y=no(m.q,m.offset);m.c=[m.position[0]+y[0],m.position[1]+y[1],m.position[2]+y[2]],m.v=[0,0,0],m.w=[0,0,0]}if(e==="above"){const m=ot(o.height)?Math.max(.3,Math.min(20,o.height)):2,y=Math.max(...c.map(d=>d.c[1]+f(d))),M=[];for(const d of[...c].sort((x,g)=>x.c[1]-g.c[1]||(x.id<g.id?-1:1))){d.q=At(s?[d.rotation[0],d.rotation[1],d.rotation[2]+i(35)]:[d.rotation[0]+i(35),d.rotation[1]+i(180),d.rotation[2]+i(35)]);let x=y+m+f(d);for(const g of M)Math.hypot(g.c[0]-d.c[0],g.c[2]-d.c[2])<f(g)+f(d)&&(x=Math.max(x,g.c[1]+f(g)+f(d)+.05));d.c=s?[d.c[0],x,d.c[2]]:[d.c[0]+i(.05),x,d.c[2]+i(.05)],M.push(d)}}if(e==="here")for(const m of c)m.w=[i(1.2),i(1.2),i(1.2)];const h=m=>s?[0,0,m[2]]:m,p=m=>s?[m[0],m[1],0]:m;if(e==="explode"){const m=Y(o.center,[0,1,2].map(M=>c.reduce((d,x)=>d+x.c[M],0)/c.length)),y=ot(o.strength)?Math.max(.5,Math.min(20,o.strength)):4;for(const M of c){let d=M.c[0]-m[0],x=s?0:M.c[2]-m[2],g=Math.hypot(d,x);if(g<.001){const b=l()*Math.PI*2;d=Math.cos(b),x=s?0:Math.sin(b),g=Math.hypot(d,x)||1}const w=y*(.75+l()*.5);M.v=[d/g*w,w*(.6+l()*.4),x/g*w],M.w=[i(7),i(7),i(7)]}}if(e==="throw"){const m=Y(o.velocity,[0,2,3]);for(const y of c)y.v=m.map(M=>M*(1+i(.08))),y.w=[i(5),i(5),i(5)]}const u=new t.World({x:0,y:-(ot(o.gravity)?o.gravity:tn),z:0});try{u.timestep=1/(Et*oo);for(const g of c)g.v=p(g.v),g.w=h(g.w);const m=(g,w)=>{const b=u.createRigidBody(t.RigidBodyDesc.fixed().setTranslation(g[0],g[1],g[2]));u.createCollider(t.ColliderDesc.cuboid(w[0],w[1],w[2]).setRestitution(r.restitution).setFriction(r.friction),b)};m([0,a-1,0],[1e3,1,1e3]);let y=0;for(const g of o.statics||[]){const w=Y(g?.min,null),b=Y(g?.max,null);if(!w||!b||y>=go)continue;const v=[0,1,2].map(R=>(b[R]-w[R])/2);v.some(R=>!(R>.002))||b[1]<=a+.001||(m([0,1,2].map(R=>(w[R]+b[R])/2),v),y++)}for(const g of c){g.rb=u.createRigidBody(t.RigidBodyDesc.dynamic().setTranslation(g.c[0],g.c[1],g.c[2]).setRotation({x:g.q[0],y:g.q[1],z:g.q[2],w:g.q[3]}).setLinvel(g.v[0],g.v[1],g.v[2]).setAngvel({x:g.w[0],y:g.w[1],z:g.w[2]}).setLinearDamping(.05).setAngularDamping(g.shape==="box"?.2:1.2).setCcdEnabled(!0)),s&&(g.rb.setEnabledTranslations(!0,!0,!1,!1),g.rb.setEnabledRotations(!1,!1,!0,!1));const w=g.shape==="ball"?t.ColliderDesc.ball(Math.max(...g.half)):g.shape==="cylinder"?t.ColliderDesc.cylinder(g.half[1],Math.max(g.half[0],g.half[2])):t.ColliderDesc.cuboid(g.half[0],g.half[1],g.half[2]);u.createCollider(w.setRestitution(r.restitution).setFriction(r.friction).setDensity(400),g.rb),g.samples=[]}const M=Math.round(n*Et);for(let g=0;g<=M;g++){if(g)for(let w=0;w<oo;w++)u.step();for(const w of c){const b=w.rb.translation(),v=w.rb.rotation(),R=[v.x,v.y,v.z,v.w],T=no(R,w.offset),D=w.rb.linvel(),q=w.rb.angvel();w.samples.push({sec:g/Et,centre:[b.x,b.y,b.z],pivot:[b.x-T[0],b.y-T[1],b.z-T[2]],q:R,speed:Math.hypot(D.x,D.y,D.z)+Math.hypot(q.x,q.y,q.z)*f(w)})}}let d=!0;return{bodies:c.map(g=>{let w=null;for(let b=g.samples.length-1;b>=0&&g.samples[b].speed<.05;b--)w=g.samples[b].sec;return w==null&&(d=!1),{id:g.id,position:g.position,rotation:g.rotation,half:g.half,offset:g.offset,samples:g.samples,settledAt:w}}),settled:d,duration:n,mode:e}}finally{u.free()}}function cn(t,o,s,{scale:e={},newId:n,lifted:a=!1}={}){const r=n??(()=>globalThis.crypto.randomUUID());return{...t,shots:t.shots.map(l=>{if(l.id!==o)return l;const i={...l.objectKeys};for(const[c,f]of Object.entries(s)){if(!f.keys.length)continue;const h=f.keys[0].time,p=t.items.find(M=>M.id===c),u=Y(e[c],Y(p?.scale,[1,1,1])),m=(i[c]||[]).filter(M=>M.time<h-1e-6),y=1/(Et*Math.max(1e-6,l.duration||1));a&&f.from&&m.length&&h-y>m[m.length-1].time+1e-6&&m.push({id:r(),time:h-y,position:[...f.from.position],rotation:[...f.from.rotation],scale:[...u]}),i[c]=[...m,...f.keys.map(M=>({id:r(),time:M.time,position:[...M.position],rotation:[...M.rotation],scale:[...u]}))]}return{...l,objectKeys:i}})}}const zn=Object.freeze(Object.defineProperty({__proto__:null,BOUNCE_PRESETS:Xt,DROP_MODES:wo,DROP_SECONDS:xt,KEY_TOLERANCE:bo,MAX_KEYS_PER_BODY:xo,MAX_PHYSICS_BODIES:Rt,MAX_PHYSICS_STATICS:go,PHYSICS_FPS:Et,applyDropKeys:cn,bakeDrop:ln,dropUnavailable:nn,eulerFromQuat:Co,fallbackShape:sn,loadRapier:Ao,quatAngle:Po,quatFromEuler:At,rapierReady:Eo,sampleBakedKeys:To,seededRandom:So,shapeKind:en,simulateDrop:Bt,thinKeys:Ro},Symbol.toStringTag,{value:"Module"})),Io=["x","y","zoom","rot","opacity","depth"],L=t=>Math.round(t*1e4)/1e4,Pt=(t,o)=>Object.keys(it).filter(s=>(!o||o.includes(s))&&No(t,s));function qn(t,{in:o=.33,out:s=.33}={},e){const n=Lo([Math.max(0,Math.min(1,s)),0,Math.max(0,Math.min(1,1-o)),1]);let a={...t},r={};for(const l of Pt(t)){const i=gt(a,l),c=e?e.filter(h=>h.prop===l):i;if(!c.length)continue;const f=i.map((h,p)=>p<i.length-1&&c.some(u=>Math.abs(u.time-h.time)<1/120)?{...h,ease:n}:h);r={...r,...mt(a,l,f)},a={...a,...r}}return r}function Kn(t,{in:o=null,out:s=null}={},e,n){const a=c=>Math.max(.001,Math.min(1,c)),r={...s!=null?{out:[L(a(s)),0]}:{},...o!=null?{in:[L(1-a(o)),1]}:{}};let l={...t},i={};for(const c of Pt(t,n)){const f=gt(l,c);if(f.length<2)continue;const h=Ko(t,f,e);h.length&&(i={...i,...mt(l,c,Xo(f,h,r))},l={...l,...i})}return i}const Ut={tl:[0,0],t:[.5,0],tr:[1,0],l:[0,.5],c:[.5,.5],r:[1,.5],bl:[0,1],b:[.5,1],br:[1,1]},Xn=Object.keys(Ut);function Ln(t,o,s,e,n,a){const[r,l]=Ut[s],i=o.angle*Math.PI/180,c=Math.abs(o.w*Math.cos(i))+Math.abs(o.h*Math.sin(i)),f=Math.abs(o.w*Math.sin(i))+Math.abs(o.h*Math.cos(i)),h=c/2+(n-c)*r,p=f/2+(a-f)*l,u=(h-o.cx)/n,m=(p-o.cy)/a,y=(d,x)=>Math.max(it[d].min,Math.min(it[d].max,x));let M=Ht(t,"x",L(y("x",wt(t,"x",e)+u)),e);return M={...M,...Ht({...t,...M},"y",L(y("y",wt(t,"y",e)+m)),e)},M}function Nn(t,o,s,e,n,a){const[r,l]=Ut[s],i=Yt(t,e,n,a).zoom||1;return Yo(t,e,L((r-.5)*o.w/(n*i)),L((l-.5)*o.h/(a*i)),n,a)}function On(t,o,s,e="forward",n=1){let a=t.filter(i=>o.includes(i.id)).sort((i,c)=>c.track-i.track||i.start-c.start);if(a.length<2)return t;if(e==="reverse")a.reverse();else if(e==="center"){const i=(a.length-1)/2;a=[...a].sort((c,f)=>Math.abs(a.indexOf(c)-i)-Math.abs(a.indexOf(f)-i))}else if(e==="random"){let i=n>>>0||1;const c=()=>(i=Math.imul(i^i>>>15,2246822507)>>>0)/4294967295;a=a.map(f=>[c(),f]).sort((f,h)=>f[0]-h[0]).map(f=>f[1])}const r=Math.min(...a.map(i=>i.start)),l=new Map(a.map((i,c)=>[i.id,L(Math.max(0,r+c*s))]));return t.map(i=>l.has(i.id)?{...i,start:Math.min(l.get(i.id),600-i.duration)}:i)}function Yn(t,o){const s={};for(const n of Pt(t)){const a=gt(t,n).filter(r=>!o||o.some(l=>l.prop===n&&Math.abs(l.time-r.time)<.008333333333333333));a.length&&(s[n]=a)}const e=Math.min(...Object.values(s).flat().map(n=>n.time));return Object.keys(s).length?{props:Object.fromEntries(Object.entries(s).map(([n,a])=>[n,a.map(r=>({...r,time:L(r.time-e)}))]))}:null}function $n(t,o,s){let e={...t},n={};for(const[a,r]of Object.entries(o.props)){if(!it[a]||t.kind==="model"&&a==="rot"||t.kind!=="model"&&["rx","ry","rz"].includes(a))continue;const l=r.map(c=>({...c,time:L(s+c.time)})).filter(c=>c.time<=t.duration+1e-6),i=gt(e,a).filter(c=>!l.some(f=>Math.abs(f.time-c.time)<1/120));n={...n,...mt(e,a,[...i,...l].sort((c,f)=>c.time-f.time))},e={...e,...n}}return n}function Vn(t,o="elastic",{amount:s=.5,cycles:e=3,props:n}={}){let a={...t},r={};for(const l of Pt(t,n??Io)){const i=gt(a,l);if(i.length<2)continue;const c=i.at(-2),f=i.at(-1),h=f.time-c.time,p=t.duration-f.time;if(h<=0||p<.1)continue;const u=f.value-c.value;if(Math.abs(u)<1e-6)continue;const m=Math.max(1,Math.min(6,Math.round(e))),y=Math.min(p/(m*2),Math.max(.06,h*.6)),M=[];for(let d=1;d<=m*2;d++){const x=s*Math.pow(.45,Math.ceil(d/2)-1),g=o==="bounce"?(d%2?-1:0)*Math.abs(u)*x*Math.sign(u):(d%2?1:-1)*u*x*.5,w=d===m*2?f.value:f.value+g,b=it[l].min,v=it[l].max;M.push({time:L(f.time+y*d),value:L(Math.max(b,Math.min(v,w))),ease:so})}r={...r,...mt(a,l,[...i.map((d,x)=>x===i.length-1?{...d,ease:so}:d),...M])},a={...a,...r}}return r}const so=[.33,0,.67,1];function Bn(t,o="cycle",{props:s,maxKeys:e=60}={}){let n={...t},a={};for(const r of Pt(t,s)){const l=gt(n,r);if(l.length<2)continue;const i=l[0].time,c=l.at(-1).time,f=c-i;if(f<=1/30)continue;const h=[...l],p=l.at(-1).value-l[0].value;for(let u=1;h.length<e;u++){const m=o==="pingpong"&&u%2?[...l].reverse().map(M=>({...M,time:c-(M.time-i)})):l;let y=!1;for(const M of m.slice(1)){const d=L(M.time+f*u);if(d>t.duration+1e-6||h.length>=e)break;const x=o==="offset"?M.value+p*u:M.value,{min:g,max:w}=it[r];h.push({...M,time:d,value:L(Math.max(g,Math.min(w,x)))}),y=!0}if(!y)break}a={...a,...mt(n,r,h)},n={...n,...a}}return a}function Un(t,o){const s=Ct(t.shape)?t.shape:fo(t.shape);return s.items.map(e=>({...t,id:o(),label:`${t.label} · ${e.name}`.slice(0,60),shape:{...s,items:[{...e}]}}))}const fn=["posX","posY","scale","rotation","anchorX","anchorY","parentId","opacity"];function Gn(t){if(t.length<2)throw Error("Chọn ít nhất 2 layer Shape để gộp");const[o,...s]=t,e=a=>fn.every(r=>(a[r]??null)===(o[r]??null))&&!a.motionKeys?.some(r=>Io.some(l=>l in r));if(!t.every(e))throw Error("Chỉ gộp được các layer Shape cùng vị trí / tỉ lệ / góc xoay và không có key chuyển động");const n=[];for(const a of t){const r=Ct(a.shape)?a.shape:fo(a.shape);for(const l of r.items)n.push({...l,id:Oo(n)})}if(n.length>24)throw Error("Một layer Shape chứa tối đa 24 hình");return{...o,start:Math.min(...t.map(a=>a.start)),duration:Math.max(...t.map(a=>a.start+a.duration))-Math.min(...t.map(a=>a.start)),shape:{v:2,items:n}}}function Qn(t,o,s,{fps:e=10,makeId:n,label:a}={}){const r=Math.min(o/1280,s/720),l=(y,M)=>[L(((y-o/2)/r+640-280)/.72),L(((M-s/2)/r+360)/.72)],i=Math.max(2,Math.min(255,Math.round(t.duration*e)+1)),c=[];for(let y=0;y<i;y++){const M=Yt(t,t.duration*y/(i-1),o,s);c.push(l(o/2+M.x*o,s/2+M.y*s))}const f=c.map(y=>y[0]),h=c.map(y=>y[1]),p=(Math.min(...f)+Math.max(...f))/2,u=(Math.min(...h)+Math.max(...h))/2,m={id:"s1",name:"Position Trace",type:"path",params:{pts:c.map(([y,M])=>[L(y-p),L(M-u)]),closed:!1},pos:[L(p),L(u)],rot:0,scale:[1,1],opacity:1,fill:{type:"none"},stroke:{color:"#c6f432",width:8,cap:"round",join:"round"}};return{id:n(),nodeId:"",kind:"image",url:"",label:a??`${t.label} · trace`.slice(0,60),track:t.track,start:t.start,duration:t.duration,volume:0,motionV:2,shape:{v:2,items:[m]}}}const Gt=/^#[0-9a-fA-F]{6}$/,hn=24,Zn=["#c6f432","#2bd4c9","#ffffff","#0d0f12","#ff5bd1"];function Jn(t,o=16){const s=new Map,e=n=>{if(typeof n=="string"&&Gt.test(n)){const a=n.toLowerCase();s.set(a,(s.get(a)||0)+1)}};for(const n of t){n.title&&(e(n.title.color),e(n.title.accentColor));const a=n.shape;if(a)if(Ct(a))for(const r of a.items){e(r.fill.color);for(const l of r.fill.stops||[])e(l[0]);e(r.stroke?.color)}else for(const r of a.paths)e(r.stroke),e(r.fill)}return[...s.entries()].sort((n,a)=>a[1]-n[1]).slice(0,o).map(n=>n[0])}function Hn(t,o,{stroke:s=!1,itemId:e}={}){if(!Gt.test(o))throw Error("Invalid colour");if(t.title)return{title:{...t.title,[s?"accentColor":"color"]:o}};if(!t.shape)throw Error("Chọn layer Shape hoặc chữ để áp màu");return Ct(t.shape)?{shape:{...t.shape,items:t.shape.items.map(n=>e&&n.id!==e?n:s?{...n,stroke:{...n.stroke??{width:8,cap:"round",join:"round"},color:o}}:{...n,fill:n.fill.type==="linear"||n.fill.type==="radial"?{...n.fill,stops:n.fill.stops.map((a,r)=>r===0?[o,a[1]]:a)}:{...n.fill,type:"color",color:o}})}}:{shape:{...t.shape,paths:t.shape.paths.map(n=>s||!n.fill?{...n,stroke:o}:{...n,fill:o})}}}function kn(t,o){if(!Array.isArray(o)||!o.length||o.length>hn||!o.every(n=>Gt.test(n)))throw Error("Invalid palette");const s=n=>o[n%o.length];if(t.title)return{title:{...t.title,color:s(0),...o.length>1?{accentColor:s(1)}:{}}};if(!t.shape)throw Error("Chọn layer Shape hoặc chữ để áp màu");if(!Ct(t.shape))return{shape:{...t.shape,paths:t.shape.paths.map((n,a)=>({...n,...n.fill?{fill:s(a)}:{},...n.stroke?{stroke:n.fill?n.stroke:s(a)}:{}}))}};let e=0;return{shape:{...t.shape,items:t.shape.items.map(n=>{const a=n.fill;return a.type==="linear"||a.type==="radial"?{...n,fill:{...a,stops:a.stops.map(r=>[s(e++),r[1]])}}:a.type==="color"?{...n,fill:{...a,color:s(e++)}}:n.stroke?{...n,stroke:{...n.stroke,color:s(e++)}}:n})}}}function un(t,o,s,e){const n=Yt(t,o,s,e),a=t.card?t.card.width/t.card.height:t.lottie?t.lottie.w/t.lottie.h:null,[r,l]=a&&Number.isFinite(a)&&a>0?a>s/e?[s,s/a]:[e*a,e]:[s,e];return{cx:s/2+n.x*s,cy:e/2+n.y*e,w:r*n.zoom,h:l*n.zoom,angle:n.rot}}const pn=3,Nt=30,Kt=Object.freeze({xy:.002,rot:1.5}),mn=.2,ro=t=>typeof t=="number"&&Number.isFinite(t),ao=t=>Math.round(t*1e4)/1e4;function dn(t){return t?t.kind==="audio"?"Âm thanh không rơi được":t.adjust===!0?"Lớp điều chỉnh không có hình để rơi":t.parentId?"Lớp đang gắn vào lớp cha — bỏ gắn rồi thử lại":null:"Không tìm thấy lớp"}function Qt(t,o,s,e=Nt){const n=t.length;if(n<=2)return[...Array(n).keys()];const a=[0,n-1];for(;;){a.sort((i,c)=>i-c);let r=-1,l=s;for(let i=0;i+1<a.length;i++){const c=a[i],f=a[i+1];for(let h=c+1;h<f;h++){const p=o[c]+(o[f]-o[c])*(t[h]-t[c])/(t[f]-t[c]),u=Math.abs(p-o[h]);u>l&&(l=u,r=h)}}if(r<0||a.length>=e)return a;a.push(r)}}function Mn(t,o,s){let e=0;for(let n=0;n+1<s.length;n++)for(let a=s[n]+1;a<s[n+1];a++){const r=s[n],l=s[n+1];e=Math.max(e,Math.abs(o[r]+(o[l]-o[r])*(t[a]-t[r])/(t[l]-t[r])-o[a]))}return e}function yn(t,o,s){const e=[],n=r=>Math.floor((r+180)/360),a=(r,l)=>r-360*l;for(let r=0;r<s.length;r++){const l=s[r],i=o[l],c=n(i);if(r){const f=s[r-1],h=n(o[f]);if(h!==c){const p=c>h,u=(p?h+1:h)*360-180,m=(u-o[f])/(i-o[f]),y=vt(t[f]+(t[l]-t[f])*m),M=Math.max(e.at(-1).time+1/ut,Math.min(y,vt(t[l]-2/ut))),d=M+1/ut;d<t[l]-1e-6?e.push({time:M,value:p?180:-180,ease:"hold"},{time:d,value:p?-180:180}):e[e.length-1]={...e[e.length-1],ease:"hold"}}}e.push({time:t[l],value:Math.max(-180,Math.min(180,a(i,c)))})}return e}function Wn(t,o,s={}){const e=s.R??Eo();if(!e)throw Error("Physics engine not loaded yet — try again");const n=s.W??1280,a=s.H??720,r=pn/a,l=ro(s.at)?s.at:0,i=Math.max(1,Math.min(xt.max,ro(s.duration)?s.duration:xt.default)),c=s.mode==="above"?"above":"here",f=[...new Set(o)].map(b=>t.find(v=>v.id===b));if(!f.length)throw Error("physicsDrop needs clipIds");if(f.length>Rt)throw Error(`At most ${Rt} layers per drop`);for(const[b,v]of f.entries()){const R=dn(v);if(R)throw Error(`${v?.label??o[b]}: ${R}`)}const h=f.map(b=>{const v=vt(l-b.start);if(v<0||v>b.duration-2/ut)throw Error(`${b.label??b.id}: the playhead is outside the layer`);const R=wt(b,"x",v),T=wt(b,"y",v),D=wt(b,"rot",v),q=s.rectOf?.(b)??un(b,v,n,a),N=[R*n*r,-T*a*r],X=[(q.cx-n/2)*r,-(q.cy-a/2)*r],C=D*Math.PI/180,E=[X[0]-N[0],X[1]-N[1]],I=[E[0]*Math.cos(C)-E[1]*Math.sin(C),E[0]*Math.sin(C)+E[1]*Math.cos(C)];return{id:b.id,position:[N[0],N[1],0],rotation:[0,0,-D],half:[Math.max(4,q.w)*r/2,Math.max(4,q.h)*r/2,mn],offset:[I[0],I[1],0],local:v,x:R,y:T,rot:D}}),p=-a*r/2,u=a*r/2,m=n*r/2,y=s.walls?[{min:[-m-2,p,-2],max:[-m,u+20,2]},{min:[m,p,-2],max:[m+2,u+20,2]}]:[],M=b=>Math.hypot(b.half[0],b.half[1],b.half[2]),d=Math.max(...h.map(b=>b.position[1]+M(b))),x=Bt(e,{bodies:h,statics:y,floor:p,mode:c,bounciness:s.bounciness,duration:i,height:Math.max(.3,u-d+.05),seed:s.seed??[...o].sort().join(","),planar:!0}),g={},w=t.map(b=>({...b}));for(const b of h){const v=x.bodies.find(F=>F.id===b.id),R=w.findIndex(F=>F.id===b.id);let T=w[R];const D=[],q=[],N=[],X=[];let C=b.rot;for(const F of v.samples){const Q=vt(b.local+F.sec);if(Q>T.duration+1e-6)break;let k=-2*Math.atan2(F.q[2],F.q[3])*180/Math.PI;k+=360*Math.round((C-k)/360),C=k,D.push(Q),q.push(F.pivot[0]/r/n),N.push(-F.pivot[1]/r/a),X.push(k)}const E=c==="above"&&b.local>=1/ut,I={},V={},K=Math.min(...X),B=Math.max(...X),U=B-K<360?Math.ceil((B-180)/360)*360:0,dt=U&&K-U>=-180?X.map(F=>F-U):X,Mt=dt!==X&&b.local>=1/ut;for(const[F,Q,k]of[["x",q,Kt.xy],["y",N,Kt.xy],["rot",dt,Kt.rot]]){const yt=E||F==="rot"&&Mt,W=gt(T,F).filter(O=>O.time<b.local-(yt?1.5:.5)/ut),tt=yt?[{time:vt(b.local-1/ut),value:F==="x"?b.x:F==="y"?b.y:b.rot,...F==="rot"&&Mt?{ease:"hold"}:{}}]:[],ft=F==="rot"?Q.reduce((O,Z,at)=>O+(at&&Math.floor((Z+180)/360)!==Math.floor((Q[at-1]+180)/360)?1:0),0):0,rt=Nt-W.length-tt.length-2*ft;if(rt<2)throw Error(`${T.label??T.id}: too many ${F} keys before the playhead (or too many turns) to add the drop`);const ht=Qt(D,Q,k,rt),bt=it[F].min,j=it[F].max,z=F==="rot"?yn(D,Q,ht):ht.map(O=>({time:D[O],value:Math.max(bt,Math.min(j,Q[O]))})),et=[...W,...tt,...z].map(O=>({time:O.time,value:ao(O.value),...O.ease?{ease:O.ease}:{}}));T={...T,...mt(T,F,et.slice(0,Nt))},I[F]=z.length,V[F]=ao(Mn(D,Q,ht))}w[R]=T,g[b.id]={keys:I,error:V,settledAt:v.settledAt==null?null:Math.round((l+v.settledAt)*1e3)/1e3}}return{clips:w,report:g,settled:x.settled}}const $=Object.freeze({work:480,focals:[.45,.55,.7,.85,1,1.25],maxFrames:900,keyTolPx:.05,points:400,smooth:.3,smoothFrames:4,smoothTolPx:.25,dense:{cols:40,rows:22,keep:700,minFrames:8,max:1500},slide:.01}),P=t=>Math.round(t*1e6)/1e6,A=(t,o)=>[t[0]-o[0],t[1]-o[1],t[2]-o[2]],G=(t,o)=>[t[0]+o[0],t[1]+o[1],t[2]+o[2]],_=(t,o)=>[t[0]*o,t[1]*o,t[2]*o],S=(t,o)=>t[0]*o[0]+t[1]*o[1]+t[2]*o[2],st=(t,o)=>[t[1]*o[2]-t[2]*o[1],t[2]*o[0]-t[0]*o[2],t[0]*o[1]-t[1]*o[0]],nt=t=>{const o=Math.hypot(...t)||1;return _(t,1/o)},H=(t,o)=>[S(t[0],o),S(t[1],o),S(t[2],o)],pt=t=>[0,1,2].map(o=>[t[0][o],t[1][o],t[2][o]]),io=(t,o)=>t.map(s=>[0,1,2].map(e=>s[0]*o[0][e]+s[1]*o[1][e]+s[2]*o[2][e]));function gn(t,o,s,e=uo(29)){const n=t;if(n.length<20)return null;const a=n.map(r=>Math.hypot(...A(r,s))).sort((r,l)=>r-l)[n.length>>1];for(const r of[.015,.03,.05]){const l=a*r;let i=null;for(let u=0;u<2e3;u++){const m=n[Math.floor(e()*n.length)],y=n[Math.floor(e()*n.length)],M=n[Math.floor(e()*n.length)];let d=st(A(y,m),A(M,m));const x=Math.hypot(...d);if(x<1e-9||(d=_(d,1/x),S(d,o)<0&&(d=_(d,-1)),S(d,o)<Math.cos(35*Math.PI/180)))continue;const g=-S(d,m);if(S(d,s)+g<=l)continue;let w=0;for(const b of n)Math.abs(S(d,b)+g)<l&&w++;(!i||w>i.k)&&(i={n:d,d:g,k:w})}if(!i||i.k<n.length*.2)continue;const c=n.filter(u=>Math.abs(S(i.n,u)+i.d)<l),f=_(c.reduce((u,m)=>G(u,m),[0,0,0]),1/c.length),h=[[0,0,0],[0,0,0],[0,0,0]];for(const u of c){const m=A(u,f);for(let y=0;y<3;y++)for(let M=0;M<3;M++)h[y][M]+=m[y]*m[M]}let p=jt(h);return S(p,o)<0&&(p=_(p,-1)),{n:p,d:-S(p,f),count:c.length}}return null}function jt(t){const o=t.map(n=>[...n]),s=[[1,0,0],[0,1,0],[0,0,1]];for(let n=0;n<30;n++){let a=0;for(let r=0;r<3;r++)for(let l=r+1;l<3;l++)a+=o[r][l]**2;if(a<1e-24)break;for(let r=0;r<3;r++)for(let l=r+1;l<3;l++){if(Math.abs(o[r][l])<1e-30)continue;const i=(o[l][l]-o[r][r])/(2*o[r][l]),c=Math.sign(i||1)/(Math.abs(i)+Math.sqrt(i*i+1)),f=1/Math.sqrt(c*c+1),h=c*f;for(let p=0;p<3;p++){const u=o[p][r],m=o[p][l];o[p][r]=f*u-h*m,o[p][l]=h*u+f*m}for(let p=0;p<3;p++){const u=o[r][p],m=o[l][p];o[r][p]=f*u-h*m,o[l][p]=h*u+f*m}for(let p=0;p<3;p++){const u=s[p][r],m=s[p][l];s[p][r]=f*u-h*m,s[p][l]=h*u+f*m}}}const e=[0,1,2].reduce((n,a)=>o[a][a]<o[n][n]?a:n,0);return nt([s[0][e],s[1][e],s[2][e]])}function It(t,o,s,e){const n=new Map;t.poses.forEach((a,r)=>{if(!(!a||a.lost))for(const[l,i]of t.obs[r]){const c=t.points.get(l);if(!c)continue;const f=H(a.R,A(c,a.C)),h=f[2]>-1e-6?50:Math.hypot(s+o*f[0]/-f[2]-i[0],e-o*f[1]/-f[2]-i[1]),p=n.get(l)??[0,0,0];p[0]+=h,p[1]++,p[2]=Math.max(p[2],h),n.set(l,p)}});for(const a of n.values())a[0]/=a[1];return n}function _t(t,o,s,e){const n=[...It(t,o,s,e).values()].map(r=>r[0]).sort((r,l)=>r-l),a=Math.floor(n.length*.8);return a>30?n.slice(0,a).reduce((r,l)=>r+l,0)/a:1/0}function lo(t,o,s,e){const{solved:n,keyframes:a}=_o(t);for(let r=0;r<3;r++){const l=It(t,o,s,e),i=[...l.values()].map(h=>h[2]).sort((h,p)=>h-p),c=Math.max(1.2,2*(i[i.length>>1]??1));let f=0;for(const[h,p]of l)p[2]>c&&(t.points.delete(h),f++);if(!f)break;$t(t.poses,t.points,t.obs,a,o,s,e,8),jo(t,a,n,o,s,e)}}function _o(t){const o=t.poses.map((e,n)=>e&&!e.lost?n:-1).filter(e=>e>=0),s=Math.max(1,Math.ceil(o.length/60));return{solved:o,keyframes:[t.pair[0],...o.filter((e,n)=>e!==t.pair[0]&&(n%s===0||n===o.length-1))]}}function jo(t,o,s,e,n,a){const r=Math.hypot(...A(t.poses[t.pair[1]].C,t.poses[t.pair[0]].C))||1,l=new Set(o);for(const i of s){if(l.has(i))continue;const c=[];for(const[f,h]of t.obs[i])t.points.has(f)&&c.push({X:t.points.get(f),px:h});c.length>=10&&(t.poses[i]=Jo(c,t.poses[i],e,n,a,r).pose)}}function bn(t,o,s,e){const{solved:n,keyframes:a}=_o(t),r={poses:t.poses.map(c=>c&&{...c,R:c.R.map(f=>[...f]),C:[...c.C]}),points:new Map([...t.points].map(([c,f])=>[c,[...f]]))},l=_t(t,o,s,e),i=$t(t.poses,t.points,t.obs,a,o,s,e,12,{focal:!0});return Math.abs(i/o-1)<1e-4?o:(jo(t,a,n,i,s,e),Math.abs(i/o-1)<.15&&_t(t,i,s,e)<l?i:(t.poses=r.poses,t.points=r.points,o))}function xn(t,o,{w:s,h:e,mw:n,mh:a,focals:r=$.focals,focal:l=null,onProgress:i=null}={}){const c=s/2,f=e/2;let h=l,p=0;const u=l?1:r.length+9;if(!h){const C=new Map,E=Math.max(s,e)/s;let I=t;const V=j=>{if(j=Math.round(j*1e3)/1e3,!(j>.2&&j<3)||C.has(j))return;const z=to(null,j*s,{obs:I,w:s,h:e});z.mode==="free"&&lo(z,j*s,c,f),C.set(j,{s:z,e:z.mode==="free"?_t(z,j*s,c,f):1/0}),i?.(++p/u)};let K=[];for(let j=Math.max(1,Math.round(t.length/25));;j=Math.max(1,j>>1)){I=t.filter((z,et)=>et%j===0),C.clear(),p=0;for(const z of r)V(z*E);if(K=[...C.entries()].filter(([,z])=>Number.isFinite(z.e)).sort((z,et)=>z[0]-et[0]),K.length||j===1)break}if(!K.length)return i?.(1),null;const B=K.reduce((j,z,et)=>z[1].e<K[j][1].e?et:j,0),U=K[B][1].s,dt=B>0?K[B-1][0]:K[B][0]*.8,Mt=B<K.length-1?K[B+1][0]:K[B][0]*1.25,F=U.poses.map((j,z)=>j&&!j.lost?z:-1).filter(j=>j>=0),Q=U.pair[0],k=j=>{const z={...U,poses:U.poses.map(Z=>Z&&{...Z,R:Z.R.map(at=>[...at]),C:[...Z.C]}),points:new Map([...U.points].map(([Z,at])=>[Z,[...at]]))};$t(z.poses,z.points,z.obs,[Q,...F.filter(Z=>Z!==Q)],j*s,c,f,8),i?.(++p/u);let et=0,O=0;for(const[Z,at]of It(z,j*s,c,f).values())et+=Z*at,O+=at;return O?et/O:1/0},yt=(Math.sqrt(5)-1)/2;let W=dt,tt=Mt,ft=tt-yt*(tt-W),rt=W+yt*(tt-W),ht=k(ft),bt=k(rt);for(let j=0;j<7;j++)ht<bt?(tt=rt,rt=ft,bt=ht,ft=tt-yt*(tt-W),ht=k(ft)):(W=ft,ft=rt,ht=bt,rt=W+yt*(tt-W),bt=k(rt));h=(W+tt)/2}const m=to(null,h*s,{obs:t,w:s,h:e});if(i?.(1),m.mode!=="free")return null;let y=h*s;const M=m.poses[m.pair[0]].C,d=m.poses[m.pair[0]].R;if(lo(m,y,c,f),!l)for(let C=0;C<3;C++){const E=bn(m,y,c,f),I=Math.abs(E/y-1)<5e-4;if(y=E,I)break}const x=It(m,y,c,f),g=[...x.values()].map(C=>C[0]).sort((C,E)=>C-E),w=Math.max(1.5,3*(g[g.length>>1]??1)),b=[...x.entries()].filter(([C,E])=>m.points.has(C)&&E[0]<=w&&E[1]>=2),v=C=>t.flatMap((E,I)=>E.has(C)?[o[I]]:[]);let R=0,T=0;for(const[,C]of b)R+=C[0]*C[1],T+=C[1];const D=H(pt(d),[0,1,0]),q=gn([...m.points.values()],D,M)??Qo(m.points,D,M)??Zo(m.points,D,M),N={v:1,w:s,h:e,mw:n||s,mh:a||e,mode:"free",f:P(y),err:P(T?R/T:_t(m,y,c,f)),frames:m.poses.map((C,E)=>[P(o[E]),...po(C.R).map(P),...C.C.map(P)]),floor:q?[...q.n.map(P),P(q.d)]:null},X=[];for(const[C]of b.sort((E,I)=>I[1][1]-E[1][1])){if(X.length>=$.points)break;const E=m.points.get(C),I=v(C);Do(N,E,I)<=$.slide*s&&X.push([E.map(P),[P(I[0]),P(I.at(-1))]])}return{...N,points:X.map(C=>C[0]),seen:X.map(C=>C[1])}}function Do(t,o,s){if(!s.length)return 1/0;const n=(s.length>24?s.filter((p,u)=>u%Math.ceil(s.length/24)===0||u===s.length-1):s).map(p=>nt(A(o,lt(t,p).C)));let a=0;for(const p of[n[0],n.at(-1)])for(const u of n)a=Math.max(a,Math.acos(Math.min(1,S(p,u))));const r=lt(t,s[s.length>>1]).C,l=Math.hypot(...A(o,r)),i=G(o,_(nt(A(o,r)),l*(.5/t.f)/Math.max(.001,Math.sin(a)))),c=t.frames,f=Math.max(1,Math.floor(c.length/40));let h=0;for(let p=0;p<c.length;p+=f){const u=ct(t,o,c[p][0]),m=ct(t,i,c[p][0]);u&&m&&(h=Math.max(h,Math.hypot(u[0]-m[0],u[1]-m[1])))}return h}function te(t,o,{mw:s,mh:e,focals:n=$.focals,focal:a=null,onProgress:r=null,dense:l=!0}={}){if(t.length<3)throw Error("FLAT");const i=t[0].w,c=t[0].h,f=mo(t,{anchor:!0,onProgress:r&&(u=>r(u*.35))}),h=xn(f,o,{w:i,h:c,mw:s,mh:e,focals:n,focal:a,onProgress:r&&(u=>r(.35+u*.57))});if(h){const u=l?wn(h,t,o,r&&(m=>r(.92+m*.08))):h;return r?.(1),u}const p=Go(t,(a??.7)*i);if(p.filter(u=>!u.lost).length<t.length*.5)throw Error("FLAT");return{v:1,w:i,h:c,mw:s||i,mh:e||c,mode:"tripod",frames:p.map((u,m)=>[P(o[m]),...u.q.map(P),P(u.f)])}}function wn(t,o,s,e=null,n=$.dense){const a=mo(o,{anchor:!0,keep:n.keep,cols:n.cols,rows:n.rows,onProgress:e&&(u=>e(u*.8))}),r=new Map;a.forEach((u,m)=>{for(const[y,M]of u){let d=r.get(y);d||r.set(y,d=[]),d.push([s[m],M[0],M[1]])}});const l=[...r.values()].filter(u=>u.length>=n.minFrames),i=t.points??[],c=u=>i.some(m=>Math.abs(m[0]-u[0])+Math.abs(m[1]-u[1])+Math.abs(m[2]-u[2])<1e-6),f=t.seen??i.map(()=>[t.frames[0][0],t.frames.at(-1)[0]]),h=[...i],p=[...f];for(const u of l){if(h.length>=n.max)break;const m=Fo(t,u);!m||c(m)||Do(t,m,u.map(y=>y[0]))>$.slide*t.w||(h.push(m.map(P)),p.push([P(u[0][0]),P(u.at(-1)[0])]))}return e?.(1),{...t,points:h,seen:p}}const vn=t=>!!t&&typeof t.ref=="string"&&!Array.isArray(t.frames);function oe(t){if(!t||t.v!==1||!["free","tripod"].includes(t.mode))throw Error("Invalid scene track");for(const e of["w","h","mw","mh"])if(!(Number.isFinite(t[e])&&t[e]>0&&t[e]<=2e4))throw Error("Invalid scene track");if(t.smooth!==void 0&&!(Number.isFinite(t.smooth)&&t.smooth>=0&&t.smooth<=1))throw Error("Invalid scene track");if(vn(t)){if(!/^[0-9a-f]{64}$/.test(t.ref))throw Error("Invalid scene track");return t}const o=t.mode==="free"?8:6;if(!Array.isArray(t.frames)||t.frames.length<2)throw Error("Invalid scene track");let s=-1/0;for(const e of t.frames){if(!Array.isArray(e)||e.length!==o||!e.every(Number.isFinite)||e[0]<s)throw Error("Invalid scene track");s=e[0]}if(t.mode==="free"&&(!(Number.isFinite(t.f)&&t.f>0)||t.floor!=null&&!(Array.isArray(t.floor)&&t.floor.length===4&&t.floor.every(Number.isFinite))||!Array.isArray(t.points)||t.points.length>5e3||!t.points.every(e=>Array.isArray(e)&&e.length===3&&e.every(Number.isFinite))||t.seen!=null&&!(Array.isArray(t.seen)&&t.seen.length===t.points.length&&t.seen.every(e=>Array.isArray(e)&&e.length===2&&e.every(Number.isFinite)&&e[0]<=e[1]))))throw Error("Invalid scene track");return t}function lt(t,o){const s=t.frames,e=s.length;let n=0,a=Math.max(0,e-2);for(;n<a;){const u=n+a+1>>1;s[u][0]<=o?n=u:a=u-1}const r=s[n],l=s[Math.min(e-1,n+1)],i=l[0]>r[0]?Math.max(0,Math.min(1,(o-r[0])/(l[0]-r[0]))):0;let c=r.slice(1,5),f=l.slice(1,5);Zt(c,f)<0&&(f=f.map(u=>-u));const h=En(c,f,i),p=ho(h);return t.mode==="tripod"?{R:p,C:[0,0,0],f:r[5]+(l[5]-r[5])*i}:{R:p,C:[0,1,2].map(u=>r[5+u]+(l[5+u]-r[5+u])*i),f:t.f}}const Zt=(t,o)=>t[0]*o[0]+t[1]*o[1]+t[2]*o[2]+t[3]*o[3];function En(t,o,s){const e=Math.min(1,Math.max(-1,Zt(t,o)));if(e>.9995){const i=t.map((f,h)=>f+(o[h]-f)*s),c=Math.hypot(...i);return i.map(f=>f/c)}const n=Math.acos(e),a=Math.sin(n),r=Math.sin((1-s)*n)/a,l=Math.sin(s*n)/a;return t.map((i,c)=>i*r+o[c]*l)}function An(t,o,s){const e=t.seen?.[o];if(!e)return!0;const n=t.frames.length>1?(t.frames.at(-1)[0]-t.frames[0][0])/(t.frames.length-1):0;return s>=e[0]-n-1e-4&&s<=e[1]+n+1e-4}const Jt=(t,o)=>(t.points??[]).map((s,e)=>[s,e]).filter(([,s])=>An(t,s,o));function ct(t,o,s){const e=lt(t,s),n=H(e.R,t.mode==="tripod"?o:A(o,e.C));return n[2]>-1e-6?null:[t.w/2+e.f*n[0]/-n[2],t.h/2-e.f*n[1]/-n[2]]}function Dt(t,o,s,e){const n=lt(t,e),a=nt([(o-t.w/2)/n.f,-(s-t.h/2)/n.f,-1]);return{o:n.C,d:H(pt(n.R),a),pose:n}}function Cn(t,o,s,e,{radius:n=null}={}){const{o:a,d:r}=Dt(t,o,s,e);if(t.mode==="tripod")return r.map(P);const l=n??Math.max(10,t.w*.04),i=[];for(const[h]of Jt(t,e)){const p=ct(t,h,e);if(!p)continue;const u=Math.hypot(p[0]-o,p[1]-s),m=S(A(h,a),r);m>0&&u<=t.w*.15&&i.push({X:h,depth:m,dist:u,w:1/(1+u)})}i.sort((h,p)=>h.dist-p.dist);const c=i.filter((h,p)=>h.dist<=l||p<6);let f=null;if(c.length>=3&&(f=Pn(c,a,r)),f===null&&c.length>=2){c.sort((u,m)=>u.depth-m.depth);const h=c.reduce((u,m)=>u+m.w,0);let p=0;for(const u of c)if(p+=u.w,p>=h/2){f=u.depth;break}}else if(f===null&&t.floor){const h=t.floor.slice(0,3),p=S(h,r);if(Math.abs(p)>1e-6){const u=-(S(h,a)+t.floor[3])/p;u>0&&(f=u)}}if(f===null){const h=(t.points??[]).map(p=>S(A(p,a),r)).filter(p=>p>0).sort((p,u)=>p-u);f=h.length?h[h.length>>1]:1}return G(a,_(r,f)).map(P)}function Pn(t,o,s){const e=t.slice(0,20),n=e.map(d=>d.depth).sort((d,x)=>d-x),a=n[n.length>>1]*.015,r=e.reduce((d,x)=>d+x.w,0);let l=null,i=0;for(let d=0;d<e.length;d++)for(let x=d+1;x<e.length;x++)for(let g=x+1;g<e.length;g++){let w=st(A(e[x].X,e[d].X),A(e[g].X,e[d].X));const b=Math.hypot(...w);if(b<1e-12)continue;w=_(w,1/b);const v=e.filter(T=>Math.abs(S(w,A(T.X,e[d].X)))<a),R=v.reduce((T,D)=>T+D.w,0);R>i&&(l=v,i=R)}if(!l||l.length<3||i<r*.5)return null;const c=_(l.reduce((d,x)=>G(d,x.X),[0,0,0]),1/l.length),f=[[0,0,0],[0,0,0],[0,0,0]];for(const{X:d}of l){const x=A(d,c);for(let g=0;g<3;g++)for(let w=0;w<3;w++)f[g][w]+=x[g]*x[w]}if(!(f[0][0]+f[1][1]+f[2][2]>0))return null;const p=jt(f),u=S(p,s);if(Math.abs(u)<.05)return null;const m=Math.min(...l.map(d=>d.depth)),y=Math.max(...l.map(d=>d.depth)),M=S(p,A(c,o))/u;return M>=m*.7&&M<=y*1.4?M:null}function Sn(t,o,s,e){if(t.mode!=="free"||!t.floor)return null;const{o:n,d:a}=Dt(t,o,s,e),r=t.floor.slice(0,3),l=S(r,a);if(Math.abs(l)<1e-6)return null;const i=-(S(r,n)+t.floor[3])/l;return i>0?G(n,_(a,i)):null}function ne(t,o){return t.mode!=="free"?[]:o.map(s=>Fo(t,s)).filter(Boolean)}function Fo(t,o){if(t.mode!=="free"||o.length<4)return null;const s=o.map(([l,i,c])=>Dt(t,i,c,l));let e=0;for(const l of s)e=Math.max(e,Math.acos(Math.min(1,S(l.d,s[0].d))));if(e<.5*Math.PI/180)return null;const n=[[0,0,0],[0,0,0],[0,0,0]],a=[0,0,0];for(const{o:l,d:i}of s)for(let c=0;c<3;c++)for(let f=0;f<3;f++){const h=(c===f?1:0)-i[c]*i[f];n[c][f]+=h,a[c]+=h*l[f]}const r=Tn(n,a);return r&&o.every(([l,i,c])=>{const f=ct(t,r,l);return f&&Math.hypot(f[0]-i,f[1]-c)<1.5})?r:null}function Tn(t,o){const s=n=>n[0][0]*(n[1][1]*n[2][2]-n[1][2]*n[2][1])-n[0][1]*(n[1][0]*n[2][2]-n[1][2]*n[2][0])+n[0][2]*(n[1][0]*n[2][1]-n[1][1]*n[2][0]),e=s(t);return Math.abs(e)<1e-12?null:[0,1,2].map(n=>s(t.map((a,r)=>a.map((l,i)=>i===n?o[r]:l)))/e)}function ee(t,o,s,{extra:e=[]}={}){if(t.mode!=="free")return null;const n=(p,u)=>{let m=0;for(let y=0;y<4;y++){const[M,d]=o[y],[x,g]=o[(y+1)%4],w=(x-M)*(u-d)-(g-d)*(p-M);if(w){if(m&&Math.sign(w)!==m)return!1;m=Math.sign(w)}}return!0},a=lt(t,s),r=[...e,...Jt(t,s).map(([p])=>p)].filter(p=>{const u=ct(t,p,s);return u&&n(u[0],u[1])}),l=o.map(([p,u])=>Dt(t,p,u,s)),i=(p,u)=>{const m=[];for(const{o:y,d:M}of l){const d=S(p,M);if(Math.abs(d)<1e-9)return null;const x=-(S(p,y)+u)/d;if(!(x>0))return null;m.push(G(y,_(M,x)))}return m};let c=null,f="points";if(r.length>=4){const p=r.map(y=>Math.hypot(...A(y,a.C))).sort((y,M)=>y-M)[r.length>>1],u=p*.015;let m=null;for(let y=0;y<r.length;y++)for(let M=y+1;M<r.length;M++)for(let d=M+1;d<r.length&&d<M+12;d++){let x=st(A(r[M],r[y]),A(r[d],r[y]));const g=Math.hypot(...x);if(g<1e-12)continue;x=_(x,1/g);const w=-S(x,r[y]);let b=0;for(const v of r)Math.abs(S(x,v)+w)<u&&b++;(!m||b>m.c)&&(m={n:x,d:w,c:b})}if(m&&m.c>=4){const y=r.filter(g=>Math.abs(S(m.n,g)+m.d)<u),M=_(y.reduce((g,w)=>G(g,w),[0,0,0]),1/y.length),d=[[0,0,0],[0,0,0],[0,0,0]];for(const g of y){const w=A(g,M);for(let b=0;b<3;b++)for(let v=0;v<3;v++)d[b][v]+=w[b]*w[v]}let x=jt(d);S(x,A(a.C,M))<0&&(x=_(x,-1)),c={n:x,d:-S(x,M),count:y.length}}}if(!c){const p=o.reduce((d,x)=>d+x[0],0)/4,u=o.reduce((d,x)=>d+x[1],0)/4,m=Cn(t,p,u,s),y=t.floor?Sn(t,p,u,s):null,M=Math.hypot(...A(m,a.C));if(y&&Math.abs(Math.hypot(...A(y,a.C))-M)<M*.12)c={n:t.floor.slice(0,3),d:t.floor[3],count:0},f="ground";else{const d=t.floor?t.floor.slice(0,3):H(pt(a.R),[0,1,0]),x=A(a.C,m);let g=A(x,_(d,S(x,d)));Math.hypot(...g)<1e-9&&(g=x),g=nt(g),c={n:g,d:-S(g,m),count:0},f="upright"}}const h=i(c.n,c.d);return h?{corners:h.map(p=>p.map(P)),n:c.n.map(P),d:P(c.d),count:c.count,by:f}:null}function se(t,o,s,e=null){const n=o.crop||{left:0,right:0,top:0,bottom:0},a=1-n.left-n.right,r=1-n.top-n.bottom,l=[];for(const i of e??t.frames.map(c=>c[0])){const c=s.map(f=>ct(t,f,i));c.some(f=>!f)||l.push([+i.toFixed(4),...c.flatMap(([f,h])=>[P((f/t.w-n.left)/a),P((h/t.h-n.top)/r)]),1])}return l}function re(t,o,s,e){const n=o.crop||{left:0,right:0,top:0,bottom:0};return[(n.left+s*(1-n.left-n.right))*t.w,(n.top+e*(1-n.top-n.bottom))*t.h]}function Ot(t,o,s,e,n,a,r){const l=o.crop||{left:0,right:0,top:0,bottom:0},i=(s/t.w-l.left)/(1-l.left-l.right),c=(e/t.h-l.top)/(1-l.top-l.bottom),f=$o(o,i,c,n,a,r,t.mw,t.mh);return[f.px,f.py]}function ae(t,o,s,e,n,a){const r=Math.max(t.start,o.start),l=Math.min(t.start+t.duration,o.start+o.duration);if(!(l>r))return{};const i=[],c=[],f=[r,...s.frames.map(p=>o.start+p[0]).filter(p=>p>r+1e-4&&p<l-1e-4),l];for(const p of f){const u=ct(s,e,p-o.start);if(!u)continue;const[m,y]=Ot(s,o,u[0],u[1],p-o.start,n,a),M=+(p-t.start).toFixed(4);i.push({time:M,value:P((m-n/2)/n)}),c.push({time:M,value:P((y-a/2)/a)})}if(!i.length)return{};let h={...t,motionV:2};return delete h.posX,delete h.posY,h={...h,...mt(h,"x",co(i,$.keyTolPx/n))},h={...h,...mt(h,"y",co(c,$.keyTolPx/a))},{motionV:2,motionKeys:h.motionKeys,posX:void 0,posY:void 0}}const co=(t,o)=>Qt(t.map(s=>s.time),t.map(s=>s.value),o,1/0).map(s=>t[s]);function Rn(t,o,s){if(!(s>0)||o.length<3)return o;const e=s*$.smoothFrames,n=Math.ceil(e*2.5),a=t.mode==="free",r=$.smoothTolPx,l=a?(t.points??[]).filter((h,p)=>p%Math.max(1,Math.floor((t.points?.length??0)/80))===0):[-.4,0,.4].flatMap(h=>[-.25,0,.25].map(p=>[h,p,-1])),i=(h,p)=>{const u=H(h.R,a?A(p,h.C):H(pt(o[0].R),p));return u[2]>-1e-6?null:[h.f*u[0]/-u[2],-h.f*u[1]/-u[2]]},c=(h,p)=>{let u=0,m=0;for(const y of l){const M=i(h,y),d=i(p,y);M&&d&&(u=Math.max(u,Math.hypot(M[0]-d[0],M[1]-d[1])),m++)}return m?u:0},f=o.map(h=>po(h.R));return o.map((h,p)=>{let u=0,m=[0,0,0],y=[0,0,0,0],M=0;for(let b=Math.max(0,p-n);b<=Math.min(o.length-1,p+n);b++){const v=Math.exp(-((b-p)**2)/(2*e*e)),R=Zt(f[b],f[p])<0?f[b].map(T=>-T):f[b];u+=v,m=G(m,_(o[b].C,v)),y=y.map((T,D)=>T+R[D]*v),M+=o[b].f*v}const d=b=>{const v=f[p].map((T,D)=>T+(y[D]/u-T)*b),R=Math.hypot(...v);return{R:ho(v.map(T=>T/R)),C:G(h.C,_(A(_(m,1/u),h.C),b)),f:h.f+(M/u-h.f)*b}};let x=1,g=d(1),w=c(h,g);for(let b=0;b<6&&w>r;b++)x*=Math.max(.3,r/w*.95),g=d(x),w=c(h,g);return w>r?h:g})}function zo(t,o,s,e){const n=Uo(s,e),a=lt(t,t.frames[0][0]),r=t.mode==="free",l=r&&t.floor?t.floor.slice(0,3):H(pt(a.R),[0,1,0]),i=H(pt(a.R),[0,0,-1]),c=[0,1,2].map(E=>{const I=(t.points??[]).map(V=>V[E]).sort((V,K)=>V-K);return I.length?I[I.length>>1]:a.C[E]+(r?0:i[E])}),f=r&&t.floor?A(c,_(t.floor.slice(0,3),S(t.floor.slice(0,3),c)+t.floor[3])):c,h=nt(A(i,_(l,S(i,l)))),u=[st(_(l,-1),h),_(l,-1),h],m=8*n/Math.max(1e-9,Math.hypot(...A(a.C,f))),y=E=>_(H(u,A(E,f)),m),M=Ot(t,o,t.w/2,t.h/2,0,s,e),d=Ot(t,o,t.w/2+10,t.h/2,0,s,e),x=Math.hypot(d[0]-M[0],d[1]-M[1])/10,g=[[1,0,0],[0,-1,0],[0,0,-1]],w=t.frames.filter(E=>E[0]>=-1e-6&&E[0]<=o.duration+1e-6),b=[],v={},R=(E,I)=>{if(E===void 0)return I;for(;I-E>180;)I-=360;for(;I-E<-180;)I+=360;return I},T=Rn(t,w.map(E=>lt(t,E[0])),t.smooth??0);for(const[E,I]of w.entries()){const V=T[E],K=io(io(u,pt(V.R)),g),B=y(V.C),U=Vo(K),dt=R(v.pan,U.pan),Mt=R(v.roll,U.roll);v.pan=dt,v.roll=Mt,b.push({time:+(o.start+I[0]).toFixed(4),cx:P(B[0]/s),cy:P(B[1]/e),cz:P(B[2]/n),pan:P(dt),tilt:P(U.tilt),roll:P(Mt),zoom:P(V.f*x/(5*n))})}const D=5*n*(b[0]?.zoom??1),q=$.keyTolPx/D*180/Math.PI,N={cx:$.keyTolPx/s,cy:$.keyTolPx/e,cz:$.keyTolPx/n,pan:q,tilt:q,roll:q,zoom:$.keyTolPx/D},X=new Set;for(const[E,I]of Object.entries(N))for(const V of Qt(b.map(K=>K.time),b.map(K=>K[E]),I,1/0))X.add(V);return{camera:{enabled:!0,motionV:2,motionKeys:b.filter((E,I)=>X.has(I))},toTimeline:y,fromTimeline:E=>G(H(pt(u),_(E,1/m)),f),M:u,k:m,U:n,first:a}}function In(t,o,s,e,n,{size:a=null,fill:r=1}={}){if(t.mode!=="free")throw Error("TRIPOD");const{camera:l,toTimeline:i,k:c,U:f,first:h}=zo(t,o,e,n),p=(a??Math.hypot(...A(h.C,s))*.2)*c*r,u=i(s),m=A(i(h.C),u),y=Math.atan2(m[0],-m[2])*180/Math.PI;return{camera:l,place:{is3D:!0,posX:P(u[0]/e),posY:P(u[1]/n),spatialDepth:P(-u[2]/f),modelDepth:P(-u[2]/f),modelGround:!0,modelRotation:{x:0,y:P(y),z:0},scale:P(Math.max(1e-6,p/(1.8*f)))},toTimeline:i}}function _n(t,o,s,e,n){const a=[];for(const[r,l]of Jt(t,e)){const i=ct(t,r,e);if(i){const c=Math.hypot(i[0]-o,i[1]-s);c<=n&&a.push([l,c])}}return a.sort((r,l)=>r[1]-l[1]).map(r=>r[0])}function ie(t,o,s){if(!o.length)return null;const e=lt(t,s),n=t.mode==="free"?e.C:[0,0,0],a=h=>_(h.reduce((p,u)=>G(p,u),[0,0,0]),1/h.length);let r=o.map(h=>t.mode==="free"?h:nt(h)),l=a(r),i=r,c="points";if(t.mode==="free"&&r.length<3){const h=ct(t,l,s),p=Math.hypot(...A(l,n));if(h){const u=_n(t,h[0],h[1],s,t.w*.12).map(d=>t.points[d]).filter(d=>Math.abs(Math.hypot(...A(d,n))/p-1)<.25).slice(0,14),m=p*.02,y=r[0];let M=null;for(let d=0;d<u.length;d++)for(let x=d+1;x<u.length;x++){let g=st(A(u[d],y),A(u[x],y));const w=Math.hypot(...g);if(w<1e-12)continue;g=_(g,1/w);const b=u.filter(v=>Math.abs(S(g,A(v,y)))<m);r.every(v=>Math.abs(S(g,A(v,y)))<m)&&(!M||b.length>M.length)&&(M=b)}M&&M.length>=3&&(i=[...r,...M],c="around")}}let f=null;if(t.mode==="free"&&i.length>=3){if(c==="points"&&i.length>=6){const u=Math.hypot(...A(l,n)),m=u*.01,y=uo(31);let M=null;for(let d=0;d<300;d++){const x=i[Math.floor(y()*i.length)],g=i[Math.floor(y()*i.length)],w=i[Math.floor(y()*i.length)];let b=st(A(g,x),A(w,x));const v=Math.hypot(...b);if(v<1e-12)continue;b=_(b,1/v);const R=i.filter(T=>Math.abs(S(b,A(T,x)))<m);(!M||R.length>M.length)&&(M=R)}M&&M.length>=Math.max(3,i.length*.5)&&(i=M)}const h=a(i),p=[[0,0,0],[0,0,0],[0,0,0]];for(const u of i){const m=A(u,h);for(let y=0;y<3;y++)for(let M=0;M<3;M++)p[y][M]+=m[y]*m[M]}f=jt(p),c!=="around"&&(l=h)}return f||(f=nt(A(n,l)),c="facing"),S(f,A(n,l))<0&&(f=_(f,-1)),{center:l.map(P),n:f.map(P),radius:P(Math.hypot(...A(l,n))*.08),by:c}}function le(t,o=t.radius,s=36){const e=t.n,n=nt(Math.abs(e[1])<.9?st(e,[0,1,0]):st(e,[1,0,0])),a=st(e,n);return Array.from({length:s},(r,l)=>{const i=l/s*2*Math.PI;return G(t.center,G(_(n,o*Math.cos(i)),_(a,o*Math.sin(i))))})}const jn=["x","y","depth","zoom","rot","tiltX","tiltY"];function ce(t,o,s,e,n,a,{width:r=null}={}){if(t.kind==="model"){const{camera:C,place:E}=In(o,s,e.center,n,a,{size:e.radius*2});if(!(E.spatialDepth>=kt&&E.spatialDepth<=Wt))throw Error("FAR");return{camera:C,patch:E}}const{camera:l,toTimeline:i,M:c,k:f,U:h,first:p}=zo(o,s,n,a),u=i(e.center),m=nt(H(c,e.n)),y=i(o.mode==="free"?lt(o,o.frames[0][0]).C:p.C),M=-u[2]/h;if(!(M>=kt&&M<=Wt))throw Error("FAR");const d=C=>A(C,_(m,S(C,m)));let x=Math.abs(m[1])<.85?d([0,1,0]):d(A(y,u));Math.hypot(...x)<1e-6&&(x=d([0,1,0])),x=nt(x);const g=_(m,-1),w=st(x,g),b=Math.asin(Math.max(-1,Math.min(1,g[1]))),v=Math.atan2(-g[0],g[2]),R=[Math.cos(v),0,Math.sin(v)],T=[Math.sin(b)*Math.sin(v),Math.cos(b),-Math.sin(b)*Math.cos(v)],D=Math.atan2(S(w,T),S(w,R)),q=C=>P(C*180/Math.PI);let N={...t,motionV:2};for(const C of jn)(N.motionKeys??[]).some(E=>E[C]!==void 0)&&(N={...N,motionKeys:Bo(N,C,[])});const X=(r??e.radius*6)*f;return{camera:l,patch:{is3D:!0,motionV:2,motionKeys:N.motionKeys??[],posX:P(u[0]/n),posY:P(u[1]/a),spatialDepth:P(M),scale:P(Math.max(1e-4,X/n)),rotation:q(D),tiltX:q(b),tiltY:q(v)}}}export{zn as $,vn as A,oe as B,re as C,Zn as D,Sn as E,Cn as F,Xn as G,In as H,ae as I,ne as J,ee as K,se as L,te as M,Qt as N,lt as O,hn as P,kn as Q,Wn as R,$ as S,un as T,Kn as U,An as V,_n as W,bo as X,Co as Y,At as Z,Ro as _,ce as a,Ot as b,le as c,Jn as d,Qn as e,Un as f,Yn as g,On as h,Hn as i,Vn as j,$n as k,Bn as l,Gn as m,Nn as n,Ln as o,ct as p,qn as q,Ao as r,zo as s,ie as t,dn as u,nn as v,Ho as w,Wo as x,ko as y,Fn as z};