@fleox/agent3d 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -11
- package/changelog.json +21 -0
- package/dist/agent/agent3d.mjs +36 -18
- package/dist/agent/chunks/{depth-Dih8jLqx.mjs → depth-BZT4gseQ.mjs} +1 -1
- package/dist/agent/chunks/{depth-0bZsQn6N.mjs → depth-CD_ql3QJ.mjs} +1 -1
- package/dist/agent/chunks/{export-Cdqt3mt_.mjs → export-CEX5DFLo.mjs} +83 -5
- package/dist/agent/chunks/{package-d5e08XSG.mjs → package-DjRnLzcU.mjs} +2 -2
- package/dist/agent/chunks/{tools-Cv6N_YBI.mjs → tools-CZyIc4zw.mjs} +16 -6
- package/dist/runtime/agent3d-photo.js +31 -0
- package/dist/runtime/agent3d-runtime.js +41 -41
- package/dist/runtime/agent3d-runtime.single.js +252 -247
- package/dist/runtime/chunks/assets-kC2Z1Xy_.js +1 -0
- package/dist/runtime/chunks/game-CbQaEB4q.js +20 -0
- package/dist/runtime/chunks/materials-jwhXlLYl.js +107 -0
- package/dist/runtime/chunks/model-CG5T_jcB.js +1 -0
- package/dist/runtime/chunks/num-CQNAlL82.js +1 -0
- package/dist/runtime/chunks/{photo-BlDYgYFM.js → photo-w6dwHX7j.js} +27 -22
- package/dist/runtime/chunks/physicsHost-BU-2V-CZ.js +1 -0
- package/dist/runtime/chunks/{postfx-Cbf_vvOr.js → postfx-DnIeKXAh.js} +1 -1
- package/dist/runtime/chunks/postfx-ylpcQcVK.js +1 -0
- package/dist/runtime/chunks/svg-fYjsY-rV.js +1 -0
- package/dist/runtime/chunks/{text-rSo_2Luk.js → text-CySfj5sH.js} +1 -1
- package/dist/runtime/chunks/util-BEflQURG.js +1 -0
- package/dist/studio/assets/assets--ehPZf8R.js +1 -0
- package/dist/studio/assets/camera-C0sxfRxy.js +2 -0
- package/dist/studio/assets/{editor-BjgHj9g8.js → editor-DzyukvQl.js} +1 -1
- package/dist/studio/assets/game-DNpTN_Ga.js +19 -0
- package/dist/studio/assets/{generators-DqkPMEzm.js → generators-BuVeRXnC.js} +5 -5
- package/dist/studio/assets/index-C2nMYSoW.js +479 -0
- package/dist/studio/assets/model-DQgOrTs4.js +2 -0
- package/dist/studio/assets/num-CQNAlL82.js +1 -0
- package/dist/studio/assets/{params-gVpEEiSK.js → params-D-Ul6GSt.js} +1 -1
- package/dist/studio/assets/{photo-DRoVO5iZ.js → photo-Dx2IBJ11.js} +27 -22
- package/dist/studio/assets/physicsHost-z9qn_mpg.js +1 -0
- package/dist/studio/assets/{postfx-JFUGdY0d.js → postfx-B3jDEaEK.js} +13 -13
- package/dist/studio/assets/runtime-rd9RW3WH.js +572 -0
- package/dist/studio/assets/{shaders-C7u6rN_6.js → shaders-bFhELLmD.js} +1 -1
- package/dist/studio/assets/svg-BkV9LeqP.js +2 -0
- package/dist/studio/assets/{text-LISSAism.js → text-Dp8gdjSA.js} +1 -1
- package/dist/studio/index.html +1 -1
- package/package.json +2 -2
- package/plugin/.claude-plugin/plugin.json +1 -1
- package/plugin/skills/agent3d-assets/SKILL.md +27 -27
- package/plugin/skills/agent3d-create/SKILL.md +28 -27
- package/plugin/skills/agent3d-create/references/art-direction.md +3 -3
- package/plugin/skills/agent3d-create/references/recipes.md +6 -6
- package/plugin/skills/agent3d-edit/SKILL.md +22 -22
- package/plugin/skills/agent3d-edit/references/patch-ops.md +2 -2
- package/plugin/skills/agent3d-game/SKILL.md +7 -7
- package/plugin/skills/agent3d-publish/SKILL.md +24 -22
- package/plugin/skills/agent3d-publish/references/integrations.md +1 -1
- package/templates/hero-photo-3d/assets/depth/street-depth.png +0 -0
- package/templates/hero-photo-3d/assets/manifest.json +3 -3
- package/templates/hero-photo-3d/assets/textures/street.webp +0 -0
- package/templates/hero-photo-3d/scene.json +1 -1
- package/dist/runtime/chunks/assets-qnxDnVkj.js +0 -1
- package/dist/runtime/chunks/game-BoEiyZrL.js +0 -20
- package/dist/runtime/chunks/materials-dC4vDNHh.js +0 -107
- package/dist/runtime/chunks/model-BCAl_NRl.js +0 -1
- package/dist/runtime/chunks/physicsHost-ChPgysMQ.js +0 -1
- package/dist/runtime/chunks/postfx-CFucYRev.js +0 -1
- package/dist/runtime/chunks/svg-CLUxY6Zy.js +0 -1
- package/dist/runtime/chunks/util-CyaW1fcW.js +0 -1
- package/dist/studio/assets/assets-BFJsr9Oo.js +0 -1
- package/dist/studio/assets/camera-DT0xHipx.js +0 -2
- package/dist/studio/assets/game-BXZEc9HN.js +0 -19
- package/dist/studio/assets/index-DVTzVcBD.js +0 -479
- package/dist/studio/assets/model-BqQW4Dg-.js +0 -2
- package/dist/studio/assets/physicsHost-DNdZ7AsT.js +0 -1
- package/dist/studio/assets/runtime-CMAP4azF.js +0 -572
- package/dist/studio/assets/svg-DkIOJjzS.js +0 -2
- package/templates/hero-photo-3d/assets/textures/street.jpg +0 -0
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
const __vite__mapDeps=(i,m=__vite__mapDeps,d=(m.f||(m.f=["./GLTFLoader-1Zd9N406.js","./three.module-CnI50X0U.js","./BufferGeometryUtils-CAlXAboW.js","./SkeletonUtils-CG5akqME.js"])))=>i.map(i=>d[i]);
|
|
2
|
-
import{Ln as e,U as t,c as n,d as r,ht as i,mt as a}from"./three.module-CnI50X0U.js";import{v as o}from"./camera-DT0xHipx.js";import{o as s}from"./runtime-CMAP4azF.js";import{h as c}from"./index-DVTzVcBD.js";var l=new Map;function u(e){let t=new Uint8Array(e),n;if(t.byteLength>=20&&new DataView(e).getUint32(0,!0)===1179937895){let r=new DataView(e).getUint32(12,!0);n=new TextDecoder().decode(t.subarray(20,20+r))}else n=new TextDecoder().decode(t);try{let e=JSON.parse(n);return Array.isArray(e.extensionsUsed)?e.extensionsUsed.filter(e=>typeof e==`string`):[]}catch{return[]}}async function d(e){let t=l.get(e);return t||(t=(async()=>{let[{GLTFLoader:t},n]=await Promise.all([c(()=>import(`./GLTFLoader-1Zd9N406.js`),__vite__mapDeps([0,1,2,3]),import.meta.url),fetch(e)]);if(!n.ok)throw Error(`HTTP ${n.status} for ${e}`);let r=await n.arrayBuffer(),i=u(r);if(i.includes(`KHR_draco_mesh_compression`))throw Error(`${e} uses Draco compression (KHR_draco_mesh_compression), which the Agent3D runtime does not decode. Re-export it without Draco (Blender: uncheck "Compression"), or convert it to meshopt with gltfpack (\`gltfpack -i in.glb -o out.glb -cc\`), then run \`agent3d assets optimize\`.`);let a=new t;if(i.includes(`EXT_meshopt_compression`)){let{MeshoptDecoder:e}=await c(async()=>{let{MeshoptDecoder:e}=await import(`./meshopt_decoder.module-DXTYc6wn.js`);return{MeshoptDecoder:e}},[],import.meta.url);await e.ready,a.setMeshoptDecoder(e)}let o=e.slice(0,e.lastIndexOf(`/`)+1);return a.parseAsync(r,/^data:/.test(e)?``:o)})(),t.catch(()=>l.delete(e)),l.set(e,t)),t}function f(e,t){e.traverse(e=>{let n=e;if(n.isMesh&&n.morphTargetDictionary&&n.morphTargetInfluences)for(let[e,r]of Object.entries(t)){let t=n.morphTargetDictionary[e];t!==void 0&&typeof r==`number`&&(n.morphTargetInfluences[t]=r)}})}async function p(l,u){let p=await d(u.resolveAsset(l.src)),{clone:m}=await c(async()=>{let{clone:e}=await import(`./SkeletonUtils-CG5akqME.js`);return{clone:e}},[],import.meta.url),h=m(p.scene),g=new t;g.add(h),h.updateMatrixWorld(!0);let _=new r().setFromObject(h);if(!_.isEmpty()){let t=_.getSize(new e),n=Math.max(t.x,t.y,t.z),r=l.fit&&n>0?l.fit/n:1;if(h.scale.multiplyScalar(r),l.center!==!1){let t=_.getCenter(new e);h.position.set(-t.x*r,-_.min.y*r,-t.z*r)}}h.traverse(e=>{let t=e;t.isMesh&&(t.castShadow=l.castShadow??!0,t.receiveShadow=l.receiveShadow??!1)}),l.material&&await s(h,l.material,u),l.morph&&f(h,l.morph);let v;if(p.animations.length){let e=new n(h),t=null,r=e=>typeof e==`number`||/^\d+$/.test(String(e))?p.animations[Number(e)]:p.animations.find(t=>t.name===e)??p.animations.find(t=>t.name.toLowerCase().includes(String(e).toLowerCase())),s=new Set,c=e=>{let t=r(e);return!t&&!s.has(String(e))&&(s.add(String(e)),u.issue(`asset-load`,`Model ${l.id} has no animation clip "${e}" (clips: ${p.animations.map(e=>e.name).join(`, `)})`,`objects#${l.id}.animation.clip`)),t},d=(n,r={})=>{let s=c(n);if(!s)return;let l=e.clipAction(s);l.setLoop(r.loop===!1?a:i,1/0),l.clampWhenFinished=r.loop===!1,l.timeScale=o(r.speed,1),l.enabled=!0,t&&t!==l?(l.reset().setEffectiveWeight(1).play(),t.crossFadeTo(l,o(r.crossfade,.3),!1)):l.setEffectiveWeight(1).play(),t=l},f=t=>{let n=c(t);if(!n)return null;let r=e.clipAction(n);return r.isRunning()||(r.enabled=!0,r.setLoop(i,1/0),r.play()),r},m={weight(e,t){f(e)?.setEffectiveWeight(Math.max(0,t))},speed(e,t){let n=f(e);n&&(n.timeScale=t)},time(e,t){let n=f(e);n&&(n.paused=!0,n.time=Math.max(0,t))}},g=l.animation??{},_=()=>{e.stopAllAction(),t=null,d(g.clip??0,g),g.offset&&t&&(t.time=Math.max(0,g.offset));for(let[e,t]of Object.entries(g.blend??{}))m.weight(e,t);e.update(0)};v={mixer:e,clips:p.animations,play:d,control:m,reset:_},_()}return{object:g,animator:v}}export{p as buildModel};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{d as e}from"./camera-DT0xHipx.js";import{I as t,Kt as n,Ln as r,N as i,On as a,U as o,bt as s,d as c,h as l,it as u,rt as d,st as f,zt as p}from"./three.module-CnI50X0U.js";var m=class e{constructor(e){e===void 0&&(e=[0,0,0,0,0,0,0,0,0]),this.elements=e}identity(){let e=this.elements;e[0]=1,e[1]=0,e[2]=0,e[3]=0,e[4]=1,e[5]=0,e[6]=0,e[7]=0,e[8]=1}setZero(){let e=this.elements;e[0]=0,e[1]=0,e[2]=0,e[3]=0,e[4]=0,e[5]=0,e[6]=0,e[7]=0,e[8]=0}setTrace(e){let t=this.elements;t[0]=e.x,t[4]=e.y,t[8]=e.z}getTrace(e){e===void 0&&(e=new g);let t=this.elements;return e.x=t[0],e.y=t[4],e.z=t[8],e}vmult(e,t){t===void 0&&(t=new g);let n=this.elements,r=e.x,i=e.y,a=e.z;return t.x=n[0]*r+n[1]*i+n[2]*a,t.y=n[3]*r+n[4]*i+n[5]*a,t.z=n[6]*r+n[7]*i+n[8]*a,t}smult(e){for(let t=0;t<this.elements.length;t++)this.elements[t]*=e}mmult(t,n){n===void 0&&(n=new e);let r=this.elements,i=t.elements,a=n.elements,o=r[0],s=r[1],c=r[2],l=r[3],u=r[4],d=r[5],f=r[6],p=r[7],m=r[8],h=i[0],g=i[1],_=i[2],v=i[3],y=i[4],b=i[5],x=i[6],S=i[7],C=i[8];return a[0]=o*h+s*v+c*x,a[1]=o*g+s*y+c*S,a[2]=o*_+s*b+c*C,a[3]=l*h+u*v+d*x,a[4]=l*g+u*y+d*S,a[5]=l*_+u*b+d*C,a[6]=f*h+p*v+m*x,a[7]=f*g+p*y+m*S,a[8]=f*_+p*b+m*C,n}scale(t,n){n===void 0&&(n=new e);let r=this.elements,i=n.elements;for(let e=0;e!==3;e++)i[3*e+0]=t.x*r[3*e+0],i[3*e+1]=t.y*r[3*e+1],i[3*e+2]=t.z*r[3*e+2];return n}solve(e,t){t===void 0&&(t=new g);let n=[],r,i;for(r=0;r<12;r++)n.push(0);for(r=0;r<3;r++)for(i=0;i<3;i++)n[r+4*i]=this.elements[r+3*i];n[3]=e.x,n[7]=e.y,n[11]=e.z;let a=3,o=a,s,c;do{if(r=o-a,n[r+4*r]===0){for(i=r+1;i<o;i++)if(n[r+4*i]!==0){s=4;do c=4-s,n[c+4*r]+=n[c+4*i];while(--s);break}}if(n[r+4*r]!==0)for(i=r+1;i<o;i++){let e=n[r+4*i]/n[r+4*r];s=4;do c=4-s,n[c+4*i]=c<=r?0:n[c+4*i]-n[c+4*r]*e;while(--s)}}while(--a);if(t.z=n[11]/n[10],t.y=(n[7]-n[6]*t.z)/n[5],t.x=(n[3]-n[2]*t.z-n[1]*t.y)/n[0],isNaN(t.x)||isNaN(t.y)||isNaN(t.z)||t.x===1/0||t.y===1/0||t.z===1/0)throw`Could not solve equation! Got x=[${t.toString()}], b=[${e.toString()}], A=[${this.toString()}]`;return t}e(e,t,n){if(n===void 0)return this.elements[t+3*e];this.elements[t+3*e]=n}copy(e){for(let t=0;t<e.elements.length;t++)this.elements[t]=e.elements[t];return this}toString(){let e=``;for(let t=0;t<9;t++)e+=this.elements[t]+`,`;return e}reverse(t){t===void 0&&(t=new e);let n=h,r,i;for(r=0;r<3;r++)for(i=0;i<3;i++)n[r+6*i]=this.elements[r+3*i];n[3]=1,n[9]=0,n[15]=0,n[4]=0,n[10]=1,n[16]=0,n[5]=0,n[11]=0,n[17]=1;let a=3,o=a,s,c;do{if(r=o-a,n[r+6*r]===0){for(i=r+1;i<o;i++)if(n[r+6*i]!==0){s=6;do c=6-s,n[c+6*r]+=n[c+6*i];while(--s);break}}if(n[r+6*r]!==0)for(i=r+1;i<o;i++){let e=n[r+6*i]/n[r+6*r];s=6;do c=6-s,n[c+6*i]=c<=r?0:n[c+6*i]-n[c+6*r]*e;while(--s)}}while(--a);r=2;do{i=r-1;do{let e=n[r+6*i]/n[r+6*r];s=6;do c=6-s,n[c+6*i]=n[c+6*i]-n[c+6*r]*e;while(--s)}while(i--)}while(--r);r=2;do{let e=1/n[r+6*r];s=6;do c=6-s,n[c+6*r]=n[c+6*r]*e;while(--s)}while(r--);r=2;do{i=2;do{if(c=n[3+i+6*r],isNaN(c)||c===1/0)throw`Could not reverse! A=[${this.toString()}]`;t.e(r,i,c)}while(i--)}while(r--);return t}setRotationFromQuaternion(e){let t=e.x,n=e.y,r=e.z,i=e.w,a=t+t,o=n+n,s=r+r,c=t*a,l=t*o,u=t*s,d=n*o,f=n*s,p=r*s,m=i*a,h=i*o,g=i*s,_=this.elements;return _[0]=1-(d+p),_[1]=l-g,_[2]=u+h,_[3]=l+g,_[4]=1-(c+p),_[5]=f-m,_[6]=u-h,_[7]=f+m,_[8]=1-(c+d),this}transpose(t){t===void 0&&(t=new e);let n=this.elements,r=t.elements,i;return r[0]=n[0],r[4]=n[4],r[8]=n[8],i=n[1],r[1]=n[3],r[3]=i,i=n[2],r[2]=n[6],r[6]=i,i=n[5],r[5]=n[7],r[7]=i,t}},h=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],g=class e{constructor(e,t,n){e===void 0&&(e=0),t===void 0&&(t=0),n===void 0&&(n=0),this.x=e,this.y=t,this.z=n}cross(t,n){n===void 0&&(n=new e);let r=t.x,i=t.y,a=t.z,o=this.x,s=this.y,c=this.z;return n.x=s*a-c*i,n.y=c*r-o*a,n.z=o*i-s*r,n}set(e,t,n){return this.x=e,this.y=t,this.z=n,this}setZero(){this.x=this.y=this.z=0}vadd(t,n){if(n)n.x=t.x+this.x,n.y=t.y+this.y,n.z=t.z+this.z;else return new e(this.x+t.x,this.y+t.y,this.z+t.z)}vsub(t,n){if(n)n.x=this.x-t.x,n.y=this.y-t.y,n.z=this.z-t.z;else return new e(this.x-t.x,this.y-t.y,this.z-t.z)}crossmat(){return new m([0,-this.z,this.y,this.z,0,-this.x,-this.y,this.x,0])}normalize(){let e=this.x,t=this.y,n=this.z,r=Math.sqrt(e*e+t*t+n*n);if(r>0){let e=1/r;this.x*=e,this.y*=e,this.z*=e}else this.x=0,this.y=0,this.z=0;return r}unit(t){t===void 0&&(t=new e);let n=this.x,r=this.y,i=this.z,a=Math.sqrt(n*n+r*r+i*i);return a>0?(a=1/a,t.x=n*a,t.y=r*a,t.z=i*a):(t.x=1,t.y=0,t.z=0),t}length(){let e=this.x,t=this.y,n=this.z;return Math.sqrt(e*e+t*t+n*n)}lengthSquared(){return this.dot(this)}distanceTo(e){let t=this.x,n=this.y,r=this.z,i=e.x,a=e.y,o=e.z;return Math.sqrt((i-t)*(i-t)+(a-n)*(a-n)+(o-r)*(o-r))}distanceSquared(e){let t=this.x,n=this.y,r=this.z,i=e.x,a=e.y,o=e.z;return(i-t)*(i-t)+(a-n)*(a-n)+(o-r)*(o-r)}scale(t,n){n===void 0&&(n=new e);let r=this.x,i=this.y,a=this.z;return n.x=t*r,n.y=t*i,n.z=t*a,n}vmul(t,n){return n===void 0&&(n=new e),n.x=t.x*this.x,n.y=t.y*this.y,n.z=t.z*this.z,n}addScaledVector(t,n,r){return r===void 0&&(r=new e),r.x=this.x+t*n.x,r.y=this.y+t*n.y,r.z=this.z+t*n.z,r}dot(e){return this.x*e.x+this.y*e.y+this.z*e.z}isZero(){return this.x===0&&this.y===0&&this.z===0}negate(t){return t===void 0&&(t=new e),t.x=-this.x,t.y=-this.y,t.z=-this.z,t}tangents(e,t){let n=this.length();if(n>0){let r=_,i=1/n;r.set(this.x*i,this.y*i,this.z*i);let a=v;Math.abs(r.x)<.9?(a.set(1,0,0),r.cross(a,e)):(a.set(0,1,0),r.cross(a,e)),r.cross(e,t)}else e.set(1,0,0),t.set(0,1,0)}toString(){return`${this.x},${this.y},${this.z}`}toArray(){return[this.x,this.y,this.z]}copy(e){return this.x=e.x,this.y=e.y,this.z=e.z,this}lerp(e,t,n){let r=this.x,i=this.y,a=this.z;n.x=r+(e.x-r)*t,n.y=i+(e.y-i)*t,n.z=a+(e.z-a)*t}almostEquals(e,t){return t===void 0&&(t=1e-6),!(Math.abs(this.x-e.x)>t||Math.abs(this.y-e.y)>t||Math.abs(this.z-e.z)>t)}almostZero(e){return e===void 0&&(e=1e-6),!(Math.abs(this.x)>e||Math.abs(this.y)>e||Math.abs(this.z)>e)}isAntiparallelTo(e,t){return this.negate(y),y.almostEquals(e,t)}clone(){return new e(this.x,this.y,this.z)}};g.ZERO=new g(0,0,0),g.UNIT_X=new g(1,0,0),g.UNIT_Y=new g(0,1,0),g.UNIT_Z=new g(0,0,1);var _=new g,v=new g,y=new g,b=class e{constructor(e){e===void 0&&(e={}),this.lowerBound=new g,this.upperBound=new g,e.lowerBound&&this.lowerBound.copy(e.lowerBound),e.upperBound&&this.upperBound.copy(e.upperBound)}setFromPoints(e,t,n,r){let i=this.lowerBound,a=this.upperBound,o=n;i.copy(e[0]),o&&o.vmult(i,i),a.copy(i);for(let t=1;t<e.length;t++){let n=e[t];o&&(o.vmult(n,x),n=x),n.x>a.x&&(a.x=n.x),n.x<i.x&&(i.x=n.x),n.y>a.y&&(a.y=n.y),n.y<i.y&&(i.y=n.y),n.z>a.z&&(a.z=n.z),n.z<i.z&&(i.z=n.z)}return t&&(t.vadd(i,i),t.vadd(a,a)),r&&(i.x-=r,i.y-=r,i.z-=r,a.x+=r,a.y+=r,a.z+=r),this}copy(e){return this.lowerBound.copy(e.lowerBound),this.upperBound.copy(e.upperBound),this}clone(){return new e().copy(this)}extend(e){this.lowerBound.x=Math.min(this.lowerBound.x,e.lowerBound.x),this.upperBound.x=Math.max(this.upperBound.x,e.upperBound.x),this.lowerBound.y=Math.min(this.lowerBound.y,e.lowerBound.y),this.upperBound.y=Math.max(this.upperBound.y,e.upperBound.y),this.lowerBound.z=Math.min(this.lowerBound.z,e.lowerBound.z),this.upperBound.z=Math.max(this.upperBound.z,e.upperBound.z)}overlaps(e){let t=this.lowerBound,n=this.upperBound,r=e.lowerBound,i=e.upperBound,a=r.x<=n.x&&n.x<=i.x||t.x<=i.x&&i.x<=n.x,o=r.y<=n.y&&n.y<=i.y||t.y<=i.y&&i.y<=n.y,s=r.z<=n.z&&n.z<=i.z||t.z<=i.z&&i.z<=n.z;return a&&o&&s}volume(){let e=this.lowerBound,t=this.upperBound;return(t.x-e.x)*(t.y-e.y)*(t.z-e.z)}contains(e){let t=this.lowerBound,n=this.upperBound,r=e.lowerBound,i=e.upperBound;return t.x<=r.x&&n.x>=i.x&&t.y<=r.y&&n.y>=i.y&&t.z<=r.z&&n.z>=i.z}getCorners(e,t,n,r,i,a,o,s){let c=this.lowerBound,l=this.upperBound;e.copy(c),t.set(l.x,c.y,c.z),n.set(l.x,l.y,c.z),r.set(c.x,l.y,l.z),i.set(l.x,c.y,l.z),a.set(c.x,l.y,c.z),o.set(c.x,c.y,l.z),s.copy(l)}toLocalFrame(e,t){let n=S,r=n[0],i=n[1],a=n[2],o=n[3],s=n[4],c=n[5],l=n[6],u=n[7];this.getCorners(r,i,a,o,s,c,l,u);for(let t=0;t!==8;t++){let r=n[t];e.pointToLocal(r,r)}return t.setFromPoints(n)}toWorldFrame(e,t){let n=S,r=n[0],i=n[1],a=n[2],o=n[3],s=n[4],c=n[5],l=n[6],u=n[7];this.getCorners(r,i,a,o,s,c,l,u);for(let t=0;t!==8;t++){let r=n[t];e.pointToWorld(r,r)}return t.setFromPoints(n)}overlapsRay(e){let{direction:t,from:n}=e,r=1/t.x,i=1/t.y,a=1/t.z,o=(this.lowerBound.x-n.x)*r,s=(this.upperBound.x-n.x)*r,c=(this.lowerBound.y-n.y)*i,l=(this.upperBound.y-n.y)*i,u=(this.lowerBound.z-n.z)*a,d=(this.upperBound.z-n.z)*a,f=Math.max(Math.max(Math.min(o,s),Math.min(c,l)),Math.min(u,d)),p=Math.min(Math.min(Math.max(o,s),Math.max(c,l)),Math.max(u,d));return!(p<0||f>p)}},x=new g,S=[new g,new g,new g,new g,new g,new g,new g,new g],C=class{constructor(){this.matrix=[]}get(e,t){let{index:n}=e,{index:r}=t;if(r>n){let e=r;r=n,n=e}return this.matrix[(n*(n+1)>>1)+r-1]}set(e,t,n){let{index:r}=e,{index:i}=t;if(i>r){let e=i;i=r,r=e}this.matrix[(r*(r+1)>>1)+i-1]=+!!n}reset(){for(let e=0,t=this.matrix.length;e!==t;e++)this.matrix[e]=0}setNumObjects(e){this.matrix.length=e*(e-1)>>1}},w=class{addEventListener(e,t){this._listeners===void 0&&(this._listeners={});let n=this._listeners;return n[e]===void 0&&(n[e]=[]),n[e].includes(t)||n[e].push(t),this}hasEventListener(e,t){if(this._listeners===void 0)return!1;let n=this._listeners;return!!(n[e]!==void 0&&n[e].includes(t))}hasAnyEventListener(e){return this._listeners!==void 0&&this._listeners[e]!==void 0}removeEventListener(e,t){if(this._listeners===void 0)return this;let n=this._listeners;if(n[e]===void 0)return this;let r=n[e].indexOf(t);return r!==-1&&n[e].splice(r,1),this}dispatchEvent(e){if(this._listeners===void 0)return this;let t=this._listeners[e.type];if(t!==void 0){e.target=this;for(let n=0,r=t.length;n<r;n++)t[n].call(this,e)}return this}},T=class e{constructor(e,t,n,r){e===void 0&&(e=0),t===void 0&&(t=0),n===void 0&&(n=0),r===void 0&&(r=1),this.x=e,this.y=t,this.z=n,this.w=r}set(e,t,n,r){return this.x=e,this.y=t,this.z=n,this.w=r,this}toString(){return`${this.x},${this.y},${this.z},${this.w}`}toArray(){return[this.x,this.y,this.z,this.w]}setFromAxisAngle(e,t){let n=Math.sin(t*.5);return this.x=e.x*n,this.y=e.y*n,this.z=e.z*n,this.w=Math.cos(t*.5),this}toAxisAngle(e){e===void 0&&(e=new g),this.normalize();let t=2*Math.acos(this.w),n=Math.sqrt(1-this.w*this.w);return n<.001?(e.x=this.x,e.y=this.y,e.z=this.z):(e.x=this.x/n,e.y=this.y/n,e.z=this.z/n),[e,t]}setFromVectors(e,t){if(e.isAntiparallelTo(t)){let t=E,n=D;e.tangents(t,n),this.setFromAxisAngle(t,Math.PI)}else{let n=e.cross(t);this.x=n.x,this.y=n.y,this.z=n.z,this.w=Math.sqrt(e.length()**2*t.length()**2)+e.dot(t),this.normalize()}return this}mult(t,n){n===void 0&&(n=new e);let r=this.x,i=this.y,a=this.z,o=this.w,s=t.x,c=t.y,l=t.z,u=t.w;return n.x=r*u+o*s+i*l-a*c,n.y=i*u+o*c+a*s-r*l,n.z=a*u+o*l+r*c-i*s,n.w=o*u-r*s-i*c-a*l,n}inverse(t){t===void 0&&(t=new e);let n=this.x,r=this.y,i=this.z,a=this.w;this.conjugate(t);let o=1/(n*n+r*r+i*i+a*a);return t.x*=o,t.y*=o,t.z*=o,t.w*=o,t}conjugate(t){return t===void 0&&(t=new e),t.x=-this.x,t.y=-this.y,t.z=-this.z,t.w=this.w,t}normalize(){let e=Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.z+this.w*this.w);return e===0?(this.x=0,this.y=0,this.z=0,this.w=0):(e=1/e,this.x*=e,this.y*=e,this.z*=e,this.w*=e),this}normalizeFast(){let e=(3-(this.x*this.x+this.y*this.y+this.z*this.z+this.w*this.w))/2;return e===0?(this.x=0,this.y=0,this.z=0,this.w=0):(this.x*=e,this.y*=e,this.z*=e,this.w*=e),this}vmult(e,t){t===void 0&&(t=new g);let n=e.x,r=e.y,i=e.z,a=this.x,o=this.y,s=this.z,c=this.w,l=c*n+o*i-s*r,u=c*r+s*n-a*i,d=c*i+a*r-o*n,f=-a*n-o*r-s*i;return t.x=l*c+f*-a+u*-s-d*-o,t.y=u*c+f*-o+d*-a-l*-s,t.z=d*c+f*-s+l*-o-u*-a,t}copy(e){return this.x=e.x,this.y=e.y,this.z=e.z,this.w=e.w,this}toEuler(e,t){t===void 0&&(t=`YZX`);let n,r,i,a=this.x,o=this.y,s=this.z,c=this.w;switch(t){case`YZX`:let e=a*o+s*c;if(e>.499&&(n=2*Math.atan2(a,c),r=Math.PI/2,i=0),e<-.499&&(n=-2*Math.atan2(a,c),r=-Math.PI/2,i=0),n===void 0){let t=a*a,l=o*o,u=s*s;n=Math.atan2(2*o*c-2*a*s,1-2*l-2*u),r=Math.asin(2*e),i=Math.atan2(2*a*c-2*o*s,1-2*t-2*u)}break;default:throw Error(`Euler order ${t} not supported yet.`)}e.y=n,e.z=r,e.x=i}setFromEuler(e,t,n,r){r===void 0&&(r=`XYZ`);let i=Math.cos(e/2),a=Math.cos(t/2),o=Math.cos(n/2),s=Math.sin(e/2),c=Math.sin(t/2),l=Math.sin(n/2);return r===`XYZ`?(this.x=s*a*o+i*c*l,this.y=i*c*o-s*a*l,this.z=i*a*l+s*c*o,this.w=i*a*o-s*c*l):r===`YXZ`?(this.x=s*a*o+i*c*l,this.y=i*c*o-s*a*l,this.z=i*a*l-s*c*o,this.w=i*a*o+s*c*l):r===`ZXY`?(this.x=s*a*o-i*c*l,this.y=i*c*o+s*a*l,this.z=i*a*l+s*c*o,this.w=i*a*o-s*c*l):r===`ZYX`?(this.x=s*a*o-i*c*l,this.y=i*c*o+s*a*l,this.z=i*a*l-s*c*o,this.w=i*a*o+s*c*l):r===`YZX`?(this.x=s*a*o+i*c*l,this.y=i*c*o+s*a*l,this.z=i*a*l-s*c*o,this.w=i*a*o-s*c*l):r===`XZY`&&(this.x=s*a*o-i*c*l,this.y=i*c*o-s*a*l,this.z=i*a*l+s*c*o,this.w=i*a*o+s*c*l),this}clone(){return new e(this.x,this.y,this.z,this.w)}slerp(t,n,r){r===void 0&&(r=new e);let i=this.x,a=this.y,o=this.z,s=this.w,c=t.x,l=t.y,u=t.z,d=t.w,f,p,m,h,g;return p=i*c+a*l+o*u+s*d,p<0&&(p=-p,c=-c,l=-l,u=-u,d=-d),1-p>1e-6?(f=Math.acos(p),m=Math.sin(f),h=Math.sin((1-n)*f)/m,g=Math.sin(n*f)/m):(h=1-n,g=n),r.x=h*i+g*c,r.y=h*a+g*l,r.z=h*o+g*u,r.w=h*s+g*d,r}integrate(t,n,r,i){i===void 0&&(i=new e);let a=t.x*r.x,o=t.y*r.y,s=t.z*r.z,c=this.x,l=this.y,u=this.z,d=this.w,f=n*.5;return i.x+=f*(a*d+o*u-s*l),i.y+=f*(o*d+s*c-a*u),i.z+=f*(s*d+a*l-o*c),i.w+=f*(-a*c-o*l-s*u),i}},E=new g,D=new g,O={SPHERE:1,PLANE:2,BOX:4,COMPOUND:8,CONVEXPOLYHEDRON:16,HEIGHTFIELD:32,PARTICLE:64,CYLINDER:128,TRIMESH:256},k=class e{constructor(t){t===void 0&&(t={}),this.id=e.idCounter++,this.type=t.type||0,this.boundingSphereRadius=0,this.collisionResponse=!t.collisionResponse||t.collisionResponse,this.collisionFilterGroup=t.collisionFilterGroup===void 0?1:t.collisionFilterGroup,this.collisionFilterMask=t.collisionFilterMask===void 0?-1:t.collisionFilterMask,this.material=t.material?t.material:null,this.body=null}updateBoundingSphereRadius(){throw`computeBoundingSphereRadius() not implemented for shape type ${this.type}`}volume(){throw`volume() not implemented for shape type ${this.type}`}calculateLocalInertia(e,t){throw`calculateLocalInertia() not implemented for shape type ${this.type}`}calculateWorldAABB(e,t,n,r){throw`calculateWorldAABB() not implemented for shape type ${this.type}`}};k.idCounter=0,k.types=O;var A=class e{constructor(e){e===void 0&&(e={}),this.position=new g,this.quaternion=new T,e.position&&this.position.copy(e.position),e.quaternion&&this.quaternion.copy(e.quaternion)}pointToLocal(t,n){return e.pointToLocalFrame(this.position,this.quaternion,t,n)}pointToWorld(t,n){return e.pointToWorldFrame(this.position,this.quaternion,t,n)}vectorToWorldFrame(e,t){return t===void 0&&(t=new g),this.quaternion.vmult(e,t),t}static pointToLocalFrame(e,t,n,r){return r===void 0&&(r=new g),n.vsub(e,r),t.conjugate(ee),ee.vmult(r,r),r}static pointToWorldFrame(e,t,n,r){return r===void 0&&(r=new g),t.vmult(n,r),r.vadd(e,r),r}static vectorToWorldFrame(e,t,n){return n===void 0&&(n=new g),e.vmult(t,n),n}static vectorToLocalFrame(e,t,n,r){return r===void 0&&(r=new g),t.w*=-1,t.vmult(n,r),t.w*=-1,r}},ee=new T,j=class e extends k{constructor(e){e===void 0&&(e={});let{vertices:t=[],faces:n=[],normals:r=[],axes:i,boundingSphereRadius:a}=e;super({type:k.types.CONVEXPOLYHEDRON}),this.vertices=t,this.faces=n,this.faceNormals=r,this.faceNormals.length===0&&this.computeNormals(),a?this.boundingSphereRadius=a:this.updateBoundingSphereRadius(),this.worldVertices=[],this.worldVerticesNeedsUpdate=!0,this.worldFaceNormals=[],this.worldFaceNormalsNeedsUpdate=!0,this.uniqueAxes=i?i.slice():null,this.uniqueEdges=[],this.computeEdges()}computeEdges(){let e=this.faces,t=this.vertices,n=this.uniqueEdges;n.length=0;let r=new g;for(let i=0;i!==e.length;i++){let a=e[i],o=a.length;for(let e=0;e!==o;e++){let i=(e+1)%o;t[a[e]].vsub(t[a[i]],r),r.normalize();let s=!1;for(let e=0;e!==n.length;e++)if(n[e].almostEquals(r)||n[e].almostEquals(r)){s=!0;break}s||n.push(r.clone())}}}computeNormals(){this.faceNormals.length=this.faces.length;for(let e=0;e<this.faces.length;e++){for(let t=0;t<this.faces[e].length;t++)if(!this.vertices[this.faces[e][t]])throw Error(`Vertex ${this.faces[e][t]} not found!`);let t=this.faceNormals[e]||new g;this.getFaceNormal(e,t),t.negate(t),this.faceNormals[e]=t;let n=this.vertices[this.faces[e][0]];if(t.dot(n)<0){console.error(`.faceNormals[${e}] = Vec3(${t.toString()}) looks like it points into the shape? The vertices follow. Make sure they are ordered CCW around the normal, using the right hand rule.`);for(let t=0;t<this.faces[e].length;t++)console.warn(`.vertices[${this.faces[e][t]}] = Vec3(${this.vertices[this.faces[e][t]].toString()})`)}}}getFaceNormal(t,n){let r=this.faces[t],i=this.vertices[r[0]],a=this.vertices[r[1]],o=this.vertices[r[2]];e.computeNormal(i,a,o,n)}static computeNormal(e,t,n,r){let i=new g,a=new g;t.vsub(e,a),n.vsub(t,i),i.cross(a,r),r.isZero()||r.normalize()}clipAgainstHull(e,t,n,r,i,a,o,s,c){let l=new g,u=-1,d=-Number.MAX_VALUE;for(let e=0;e<n.faces.length;e++){l.copy(n.faceNormals[e]),i.vmult(l,l);let t=l.dot(a);t>d&&(d=t,u=e)}let f=[];for(let e=0;e<n.faces[u].length;e++){let t=n.vertices[n.faces[u][e]],a=new g;a.copy(t),i.vmult(a,a),r.vadd(a,a),f.push(a)}u>=0&&this.clipFaceAgainstHull(a,e,t,f,o,s,c)}findSeparatingAxis(e,t,n,r,i,a,o,s){let c=new g,l=new g,u=new g,d=new g,f=new g,p=new g,m=Number.MAX_VALUE,h=this;if(h.uniqueAxes)for(let o=0;o!==h.uniqueAxes.length;o++){n.vmult(h.uniqueAxes[o],c);let s=h.testSepAxis(c,e,t,n,r,i);if(s===!1)return!1;s<m&&(m=s,a.copy(c))}else{let s=o?o.length:h.faces.length;for(let l=0;l<s;l++){let s=o?o[l]:l;c.copy(h.faceNormals[s]),n.vmult(c,c);let u=h.testSepAxis(c,e,t,n,r,i);if(u===!1)return!1;u<m&&(m=u,a.copy(c))}}if(e.uniqueAxes)for(let o=0;o!==e.uniqueAxes.length;o++){i.vmult(e.uniqueAxes[o],l);let s=h.testSepAxis(l,e,t,n,r,i);if(s===!1)return!1;s<m&&(m=s,a.copy(l))}else{let o=s?s.length:e.faces.length;for(let c=0;c<o;c++){let o=s?s[c]:c;l.copy(e.faceNormals[o]),i.vmult(l,l);let u=h.testSepAxis(l,e,t,n,r,i);if(u===!1)return!1;u<m&&(m=u,a.copy(l))}}for(let o=0;o!==h.uniqueEdges.length;o++){n.vmult(h.uniqueEdges[o],d);for(let o=0;o!==e.uniqueEdges.length;o++)if(i.vmult(e.uniqueEdges[o],f),d.cross(f,p),!p.almostZero()){p.normalize();let o=h.testSepAxis(p,e,t,n,r,i);if(o===!1)return!1;o<m&&(m=o,a.copy(p))}}return r.vsub(t,u),u.dot(a)>0&&a.negate(a),!0}testSepAxis(t,n,r,i,a,o){let s=this;e.project(s,t,r,i,te),e.project(n,t,a,o,ne);let c=te[0],l=te[1],u=ne[0],d=ne[1];if(c<d||u<l)return!1;let f=c-d,p=u-l;return f<p?f:p}calculateLocalInertia(e,t){let n=new g,r=new g;this.computeLocalAABB(r,n);let i=n.x-r.x,a=n.y-r.y,o=n.z-r.z;t.x=1/12*e*(2*a*2*a+2*o*2*o),t.y=1/12*e*(2*i*2*i+2*o*2*o),t.z=1/12*e*(2*a*2*a+2*i*2*i)}getPlaneConstantOfFace(e){let t=this.faces[e],n=this.faceNormals[e],r=this.vertices[t[0]];return-n.dot(r)}clipFaceAgainstHull(e,t,n,r,i,a,o){let s=new g,c=new g,l=new g,u=new g,d=new g,f=new g,p=new g,m=new g,h=this,_=[],v=r,y=_,b=-1,x=Number.MAX_VALUE;for(let t=0;t<h.faces.length;t++){s.copy(h.faceNormals[t]),n.vmult(s,s);let r=s.dot(e);r<x&&(x=r,b=t)}if(b<0)return;let S=h.faces[b];S.connectedFaces=[];for(let e=0;e<h.faces.length;e++)for(let t=0;t<h.faces[e].length;t++)S.indexOf(h.faces[e][t])!==-1&&e!==b&&S.connectedFaces.indexOf(e)===-1&&S.connectedFaces.push(e);let C=S.length;for(let e=0;e<C;e++){let r=h.vertices[S[e]],i=h.vertices[S[(e+1)%C]];r.vsub(i,c),l.copy(c),n.vmult(l,l),t.vadd(l,l),u.copy(this.faceNormals[b]),n.vmult(u,u),t.vadd(u,u),l.cross(u,d),d.negate(d),f.copy(r),n.vmult(f,f),t.vadd(f,f);let a=S.connectedFaces[e];p.copy(this.faceNormals[a]);let o=this.getPlaneConstantOfFace(a);m.copy(p),n.vmult(m,m);let s=o-m.dot(t);for(this.clipFaceAgainstPlane(v,y,m,s);v.length;)v.shift();for(;y.length;)v.push(y.shift())}p.copy(this.faceNormals[b]);let w=this.getPlaneConstantOfFace(b);m.copy(p),n.vmult(m,m);let T=w-m.dot(t);for(let e=0;e<v.length;e++){let t=m.dot(v[e])+T;if(t<=i&&(console.log(`clamped: depth=${t} to minDist=${i}`),t=i),t<=a){let n=v[e];if(t<=1e-6){let e={point:n,normal:m,depth:t};o.push(e)}}}}clipFaceAgainstPlane(e,t,n,r){let i,a,o=e.length;if(o<2)return t;let s=e[e.length-1],c=e[0];i=n.dot(s)+r;for(let l=0;l<o;l++){if(c=e[l],a=n.dot(c)+r,i<0){if(a<0){let e=new g;e.copy(c),t.push(e)}else{let e=new g;s.lerp(c,i/(i-a),e),t.push(e)}}else if(a<0){let e=new g;s.lerp(c,i/(i-a),e),t.push(e),t.push(c)}s=c,i=a}return t}computeWorldVertices(e,t){for(;this.worldVertices.length<this.vertices.length;)this.worldVertices.push(new g);let n=this.vertices,r=this.worldVertices;for(let i=0;i!==this.vertices.length;i++)t.vmult(n[i],r[i]),e.vadd(r[i],r[i]);this.worldVerticesNeedsUpdate=!1}computeLocalAABB(e,t){let n=this.vertices;e.set(Number.MAX_VALUE,Number.MAX_VALUE,Number.MAX_VALUE),t.set(-Number.MAX_VALUE,-Number.MAX_VALUE,-Number.MAX_VALUE);for(let r=0;r<this.vertices.length;r++){let i=n[r];i.x<e.x?e.x=i.x:i.x>t.x&&(t.x=i.x),i.y<e.y?e.y=i.y:i.y>t.y&&(t.y=i.y),i.z<e.z?e.z=i.z:i.z>t.z&&(t.z=i.z)}}computeWorldFaceNormals(e){let t=this.faceNormals.length;for(;this.worldFaceNormals.length<t;)this.worldFaceNormals.push(new g);let n=this.faceNormals,r=this.worldFaceNormals;for(let i=0;i!==t;i++)e.vmult(n[i],r[i]);this.worldFaceNormalsNeedsUpdate=!1}updateBoundingSphereRadius(){let e=0,t=this.vertices;for(let n=0;n!==t.length;n++){let r=t[n].lengthSquared();r>e&&(e=r)}this.boundingSphereRadius=Math.sqrt(e)}calculateWorldAABB(e,t,n,r){let i=this.vertices,a,o,s,c,l,u,d=new g;for(let n=0;n<i.length;n++){d.copy(i[n]),t.vmult(d,d),e.vadd(d,d);let r=d;(a===void 0||r.x<a)&&(a=r.x),(c===void 0||r.x>c)&&(c=r.x),(o===void 0||r.y<o)&&(o=r.y),(l===void 0||r.y>l)&&(l=r.y),(s===void 0||r.z<s)&&(s=r.z),(u===void 0||r.z>u)&&(u=r.z)}n.set(a,o,s),r.set(c,l,u)}volume(){return 4*Math.PI*this.boundingSphereRadius/3}getAveragePointLocal(e){e===void 0&&(e=new g);let t=this.vertices;for(let n=0;n<t.length;n++)e.vadd(t[n],e);return e.scale(1/t.length,e),e}transformAllPoints(e,t){let n=this.vertices.length,r=this.vertices;if(t){for(let e=0;e<n;e++){let n=r[e];t.vmult(n,n)}for(let e=0;e<this.faceNormals.length;e++){let n=this.faceNormals[e];t.vmult(n,n)}}if(e)for(let t=0;t<n;t++){let n=r[t];n.vadd(e,n)}}pointIsInside(e){let t=this.vertices,n=this.faces,r=this.faceNormals,i=new g;this.getAveragePointLocal(i);for(let a=0;a<this.faces.length;a++){let o=r[a],s=t[n[a][0]],c=new g;e.vsub(s,c);let l=o.dot(c),u=new g;i.vsub(s,u);let d=o.dot(u);if(l<0&&d>0||l>0&&d<0)return!1}return-1}static project(e,t,n,r,i){let a=e.vertices.length,o=re,s=0,c=0,l=ie,u=e.vertices;l.setZero(),A.vectorToLocalFrame(n,r,t,o),A.pointToLocalFrame(n,r,l,l);let d=l.dot(o);c=s=u[0].dot(o);for(let e=1;e<a;e++){let t=u[e].dot(o);t>s&&(s=t),t<c&&(c=t)}if(c-=d,s-=d,c>s){let e=c;c=s,s=e}i[0]=s,i[1]=c}},te=[],ne=[];new g;var re=new g,ie=new g,ae=class e extends k{constructor(e){super({type:k.types.BOX}),this.halfExtents=e,this.convexPolyhedronRepresentation=null,this.updateConvexPolyhedronRepresentation(),this.updateBoundingSphereRadius()}updateConvexPolyhedronRepresentation(){let e=this.halfExtents.x,t=this.halfExtents.y,n=this.halfExtents.z,r=g,i=new j({vertices:[new r(-e,-t,-n),new r(e,-t,-n),new r(e,t,-n),new r(-e,t,-n),new r(-e,-t,n),new r(e,-t,n),new r(e,t,n),new r(-e,t,n)],faces:[[3,2,1,0],[4,5,6,7],[5,4,0,1],[2,3,7,6],[0,4,7,3],[1,2,6,5]],axes:[new r(0,0,1),new r(0,1,0),new r(1,0,0)]});this.convexPolyhedronRepresentation=i,i.material=this.material}calculateLocalInertia(t,n){return n===void 0&&(n=new g),e.calculateInertia(this.halfExtents,t,n),n}static calculateInertia(e,t,n){let r=e;n.x=1/12*t*(2*r.y*2*r.y+2*r.z*2*r.z),n.y=1/12*t*(2*r.x*2*r.x+2*r.z*2*r.z),n.z=1/12*t*(2*r.y*2*r.y+2*r.x*2*r.x)}getSideNormals(e,t){let n=e,r=this.halfExtents;if(n[0].set(r.x,0,0),n[1].set(0,r.y,0),n[2].set(0,0,r.z),n[3].set(-r.x,0,0),n[4].set(0,-r.y,0),n[5].set(0,0,-r.z),t!==void 0)for(let e=0;e!==n.length;e++)t.vmult(n[e],n[e]);return n}volume(){return 8*this.halfExtents.x*this.halfExtents.y*this.halfExtents.z}updateBoundingSphereRadius(){this.boundingSphereRadius=this.halfExtents.length()}forEachWorldCorner(e,t,n){let r=this.halfExtents,i=[[r.x,r.y,r.z],[-r.x,r.y,r.z],[-r.x,-r.y,r.z],[-r.x,-r.y,-r.z],[r.x,-r.y,-r.z],[r.x,r.y,-r.z],[-r.x,r.y,-r.z],[r.x,-r.y,r.z]];for(let r=0;r<i.length;r++)M.set(i[r][0],i[r][1],i[r][2]),t.vmult(M,M),e.vadd(M,M),n(M.x,M.y,M.z)}calculateWorldAABB(e,t,n,r){let i=this.halfExtents;N[0].set(i.x,i.y,i.z),N[1].set(-i.x,i.y,i.z),N[2].set(-i.x,-i.y,i.z),N[3].set(-i.x,-i.y,-i.z),N[4].set(i.x,-i.y,-i.z),N[5].set(i.x,i.y,-i.z),N[6].set(-i.x,i.y,-i.z),N[7].set(i.x,-i.y,i.z);let a=N[0];t.vmult(a,a),e.vadd(a,a),r.copy(a),n.copy(a);for(let i=1;i<8;i++){let a=N[i];t.vmult(a,a),e.vadd(a,a);let o=a.x,s=a.y,c=a.z;o>r.x&&(r.x=o),s>r.y&&(r.y=s),c>r.z&&(r.z=c),o<n.x&&(n.x=o),s<n.y&&(n.y=s),c<n.z&&(n.z=c)}}},M=new g,N=[new g,new g,new g,new g,new g,new g,new g,new g],oe={DYNAMIC:1,STATIC:2,KINEMATIC:4},se={AWAKE:0,SLEEPY:1,SLEEPING:2},P=class e extends w{constructor(t){t===void 0&&(t={}),super(),this.id=e.idCounter++,this.index=-1,this.world=null,this.vlambda=new g,this.collisionFilterGroup=typeof t.collisionFilterGroup==`number`?t.collisionFilterGroup:1,this.collisionFilterMask=typeof t.collisionFilterMask==`number`?t.collisionFilterMask:-1,this.collisionResponse=typeof t.collisionResponse!=`boolean`||t.collisionResponse,this.position=new g,this.previousPosition=new g,this.interpolatedPosition=new g,this.initPosition=new g,t.position&&(this.position.copy(t.position),this.previousPosition.copy(t.position),this.interpolatedPosition.copy(t.position),this.initPosition.copy(t.position)),this.velocity=new g,t.velocity&&this.velocity.copy(t.velocity),this.initVelocity=new g,this.force=new g;let n=typeof t.mass==`number`?t.mass:0;this.mass=n,this.invMass=n>0?1/n:0,this.material=t.material||null,this.linearDamping=typeof t.linearDamping==`number`?t.linearDamping:.01,this.type=n<=0?e.STATIC:e.DYNAMIC,typeof t.type==typeof e.STATIC&&(this.type=t.type),this.allowSleep=t.allowSleep===void 0||t.allowSleep,this.sleepState=e.AWAKE,this.sleepSpeedLimit=t.sleepSpeedLimit===void 0?.1:t.sleepSpeedLimit,this.sleepTimeLimit=t.sleepTimeLimit===void 0?1:t.sleepTimeLimit,this.timeLastSleepy=0,this.wakeUpAfterNarrowphase=!1,this.torque=new g,this.quaternion=new T,this.initQuaternion=new T,this.previousQuaternion=new T,this.interpolatedQuaternion=new T,t.quaternion&&(this.quaternion.copy(t.quaternion),this.initQuaternion.copy(t.quaternion),this.previousQuaternion.copy(t.quaternion),this.interpolatedQuaternion.copy(t.quaternion)),this.angularVelocity=new g,t.angularVelocity&&this.angularVelocity.copy(t.angularVelocity),this.initAngularVelocity=new g,this.shapes=[],this.shapeOffsets=[],this.shapeOrientations=[],this.inertia=new g,this.invInertia=new g,this.invInertiaWorld=new m,this.invMassSolve=0,this.invInertiaSolve=new g,this.invInertiaWorldSolve=new m,this.fixedRotation=t.fixedRotation!==void 0&&t.fixedRotation,this.angularDamping=t.angularDamping===void 0?.01:t.angularDamping,this.linearFactor=new g(1,1,1),t.linearFactor&&this.linearFactor.copy(t.linearFactor),this.angularFactor=new g(1,1,1),t.angularFactor&&this.angularFactor.copy(t.angularFactor),this.aabb=new b,this.aabbNeedsUpdate=!0,this.boundingRadius=0,this.wlambda=new g,this.isTrigger=!!t.isTrigger,t.shape&&this.addShape(t.shape),this.updateMassProperties()}wakeUp(){let t=this.sleepState;this.sleepState=e.AWAKE,this.wakeUpAfterNarrowphase=!1,t===e.SLEEPING&&this.dispatchEvent(e.wakeupEvent)}sleep(){this.sleepState=e.SLEEPING,this.velocity.set(0,0,0),this.angularVelocity.set(0,0,0),this.wakeUpAfterNarrowphase=!1}sleepTick(t){if(this.allowSleep){let n=this.sleepState,r=this.velocity.lengthSquared()+this.angularVelocity.lengthSquared(),i=this.sleepSpeedLimit**2;n===e.AWAKE&&r<i?(this.sleepState=e.SLEEPY,this.timeLastSleepy=t,this.dispatchEvent(e.sleepyEvent)):n===e.SLEEPY&&r>i?this.wakeUp():n===e.SLEEPY&&t-this.timeLastSleepy>this.sleepTimeLimit&&(this.sleep(),this.dispatchEvent(e.sleepEvent))}}updateSolveMassProperties(){this.sleepState===e.SLEEPING||this.type===e.KINEMATIC?(this.invMassSolve=0,this.invInertiaSolve.setZero(),this.invInertiaWorldSolve.setZero()):(this.invMassSolve=this.invMass,this.invInertiaSolve.copy(this.invInertia),this.invInertiaWorldSolve.copy(this.invInertiaWorld))}pointToLocalFrame(e,t){return t===void 0&&(t=new g),e.vsub(this.position,t),this.quaternion.conjugate().vmult(t,t),t}vectorToLocalFrame(e,t){return t===void 0&&(t=new g),this.quaternion.conjugate().vmult(e,t),t}pointToWorldFrame(e,t){return t===void 0&&(t=new g),this.quaternion.vmult(e,t),t.vadd(this.position,t),t}vectorToWorldFrame(e,t){return t===void 0&&(t=new g),this.quaternion.vmult(e,t),t}addShape(e,t,n){let r=new g,i=new T;return t&&r.copy(t),n&&i.copy(n),this.shapes.push(e),this.shapeOffsets.push(r),this.shapeOrientations.push(i),this.updateMassProperties(),this.updateBoundingRadius(),this.aabbNeedsUpdate=!0,e.body=this,this}removeShape(e){let t=this.shapes.indexOf(e);return t===-1?(console.warn(`Shape does not belong to the body`),this):(this.shapes.splice(t,1),this.shapeOffsets.splice(t,1),this.shapeOrientations.splice(t,1),this.updateMassProperties(),this.updateBoundingRadius(),this.aabbNeedsUpdate=!0,e.body=null,this)}updateBoundingRadius(){let e=this.shapes,t=this.shapeOffsets,n=e.length,r=0;for(let i=0;i!==n;i++){let n=e[i];n.updateBoundingSphereRadius();let a=t[i].length(),o=n.boundingSphereRadius;a+o>r&&(r=a+o)}this.boundingRadius=r}updateAABB(){let e=this.shapes,t=this.shapeOffsets,n=this.shapeOrientations,r=e.length,i=ce,a=le,o=this.quaternion,s=this.aabb,c=ue;for(let l=0;l!==r;l++){let r=e[l];o.vmult(t[l],i),i.vadd(this.position,i),o.mult(n[l],a),r.calculateWorldAABB(i,a,c.lowerBound,c.upperBound),l===0?s.copy(c):s.extend(c)}this.aabbNeedsUpdate=!1}updateInertiaWorld(e){let t=this.invInertia;if(t.x!==t.y||t.y!==t.z||e){let e=de,n=fe;e.setRotationFromQuaternion(this.quaternion),e.transpose(n),e.scale(t,e),e.mmult(n,this.invInertiaWorld)}}applyForce(t,n){if(n===void 0&&(n=new g),this.type!==e.DYNAMIC)return;this.sleepState===e.SLEEPING&&this.wakeUp();let r=pe;n.cross(t,r),this.force.vadd(t,this.force),this.torque.vadd(r,this.torque)}applyLocalForce(t,n){if(n===void 0&&(n=new g),this.type!==e.DYNAMIC)return;let r=me,i=he;this.vectorToWorldFrame(t,r),this.vectorToWorldFrame(n,i),this.applyForce(r,i)}applyTorque(t){this.type===e.DYNAMIC&&(this.sleepState===e.SLEEPING&&this.wakeUp(),this.torque.vadd(t,this.torque))}applyImpulse(t,n){if(n===void 0&&(n=new g),this.type!==e.DYNAMIC)return;this.sleepState===e.SLEEPING&&this.wakeUp();let r=n,i=ge;i.copy(t),i.scale(this.invMass,i),this.velocity.vadd(i,this.velocity);let a=_e;r.cross(t,a),this.invInertiaWorld.vmult(a,a),this.angularVelocity.vadd(a,this.angularVelocity)}applyLocalImpulse(t,n){if(n===void 0&&(n=new g),this.type!==e.DYNAMIC)return;let r=ve,i=ye;this.vectorToWorldFrame(t,r),this.vectorToWorldFrame(n,i),this.applyImpulse(r,i)}updateMassProperties(){let e=be;this.invMass=this.mass>0?1/this.mass:0;let t=this.inertia,n=this.fixedRotation;this.updateAABB(),e.set((this.aabb.upperBound.x-this.aabb.lowerBound.x)/2,(this.aabb.upperBound.y-this.aabb.lowerBound.y)/2,(this.aabb.upperBound.z-this.aabb.lowerBound.z)/2),ae.calculateInertia(e,this.mass,t),this.invInertia.set(t.x>0&&!n?1/t.x:0,t.y>0&&!n?1/t.y:0,t.z>0&&!n?1/t.z:0),this.updateInertiaWorld(!0)}getVelocityAtWorldPoint(e,t){let n=new g;return e.vsub(this.position,n),this.angularVelocity.cross(n,t),this.velocity.vadd(t,t),t}integrate(t,n,r){if(this.previousPosition.copy(this.position),this.previousQuaternion.copy(this.quaternion),this.type!==e.DYNAMIC&&this.type!==e.KINEMATIC||this.sleepState===e.SLEEPING)return;let i=this.velocity,a=this.angularVelocity,o=this.position,s=this.force,c=this.torque,l=this.quaternion,u=this.invMass,d=this.invInertiaWorld,f=this.linearFactor,p=u*t;i.x+=s.x*p*f.x,i.y+=s.y*p*f.y,i.z+=s.z*p*f.z;let m=d.elements,h=this.angularFactor,g=c.x*h.x,_=c.y*h.y,v=c.z*h.z;a.x+=t*(m[0]*g+m[1]*_+m[2]*v),a.y+=t*(m[3]*g+m[4]*_+m[5]*v),a.z+=t*(m[6]*g+m[7]*_+m[8]*v),o.x+=i.x*t,o.y+=i.y*t,o.z+=i.z*t,l.integrate(this.angularVelocity,t,this.angularFactor,l),n&&(r?l.normalizeFast():l.normalize()),this.aabbNeedsUpdate=!0,this.updateInertiaWorld()}};P.idCounter=0,P.COLLIDE_EVENT_NAME=`collide`,P.DYNAMIC=oe.DYNAMIC,P.STATIC=oe.STATIC,P.KINEMATIC=oe.KINEMATIC,P.AWAKE=se.AWAKE,P.SLEEPY=se.SLEEPY,P.SLEEPING=se.SLEEPING,P.wakeupEvent={type:`wakeup`},P.sleepyEvent={type:`sleepy`},P.sleepEvent={type:`sleep`};var ce=new g,le=new T,ue=new b,de=new m,fe=new m;new m;var pe=new g,me=new g,he=new g,ge=new g,_e=new g,ve=new g,ye=new g,be=new g,xe=class{constructor(){this.world=null,this.useBoundingBoxes=!1,this.dirty=!0}collisionPairs(e,t,n){throw Error(`collisionPairs not implemented for this BroadPhase class!`)}needBroadphaseCollision(e,t){return(e.collisionFilterGroup&t.collisionFilterMask)!==0&&(t.collisionFilterGroup&e.collisionFilterMask)!==0&&((e.type&P.STATIC)===0&&e.sleepState!==P.SLEEPING||(t.type&P.STATIC)===0&&t.sleepState!==P.SLEEPING)}intersectionTest(e,t,n,r){this.useBoundingBoxes?this.doBoundingBoxBroadphase(e,t,n,r):this.doBoundingSphereBroadphase(e,t,n,r)}doBoundingSphereBroadphase(e,t,n,r){let i=Se;t.position.vsub(e.position,i);let a=(e.boundingRadius+t.boundingRadius)**2;i.lengthSquared()<a&&(n.push(e),r.push(t))}doBoundingBoxBroadphase(e,t,n,r){e.aabbNeedsUpdate&&e.updateAABB(),t.aabbNeedsUpdate&&t.updateAABB(),e.aabb.overlaps(t.aabb)&&(n.push(e),r.push(t))}makePairsUnique(e,t){let n=Ce,r=we,i=Te,a=e.length;for(let n=0;n!==a;n++)r[n]=e[n],i[n]=t[n];e.length=0,t.length=0;for(let e=0;e!==a;e++){let t=r[e].id,a=i[e].id,o=t<a?`${t},${a}`:`${a},${t}`;n[o]=e,n.keys.push(o)}for(let a=0;a!==n.keys.length;a++){let a=n.keys.pop(),o=n[a];e.push(r[o]),t.push(i[o]),delete n[a]}}setWorld(e){}static boundingSphereCheck(e,t){let n=new g;e.position.vsub(t.position,n);let r=e.shapes[0],i=t.shapes[0];return(r.boundingSphereRadius+i.boundingSphereRadius)**2>n.lengthSquared()}aabbQuery(e,t,n){return console.warn(`.aabbQuery is not implemented in this Broadphase subclass.`),[]}},Se=new g;new g,new T,new g;var Ce={keys:[]},we=[],Te=[];new g,new g,new g;var Ee=class extends xe{constructor(){super()}collisionPairs(e,t,n){let r=e.bodies,i=r.length,a,o;for(let e=0;e!==i;e++)for(let i=0;i!==e;i++)a=r[e],o=r[i],this.needBroadphaseCollision(a,o)&&this.intersectionTest(a,o,t,n)}aabbQuery(e,t,n){n===void 0&&(n=[]);for(let r=0;r<e.bodies.length;r++){let i=e.bodies[r];i.aabbNeedsUpdate&&i.updateAABB(),i.aabb.overlaps(t)&&n.push(i)}return n}},De=class{constructor(){this.rayFromWorld=new g,this.rayToWorld=new g,this.hitNormalWorld=new g,this.hitPointWorld=new g,this.hasHit=!1,this.shape=null,this.body=null,this.hitFaceIndex=-1,this.distance=-1,this.shouldStop=!1}reset(){this.rayFromWorld.setZero(),this.rayToWorld.setZero(),this.hitNormalWorld.setZero(),this.hitPointWorld.setZero(),this.hasHit=!1,this.shape=null,this.body=null,this.hitFaceIndex=-1,this.distance=-1,this.shouldStop=!1}abort(){this.shouldStop=!0}set(e,t,n,r,i,a,o){this.rayFromWorld.copy(e),this.rayToWorld.copy(t),this.hitNormalWorld.copy(n),this.hitPointWorld.copy(r),this.shape=i,this.body=a,this.distance=o}},Oe,ke,Ae,je,Me,Ne,Pe,Fe={CLOSEST:1,ANY:2,ALL:4};Oe=k.types.SPHERE,ke=k.types.PLANE,Ae=k.types.BOX,je=k.types.CYLINDER,Me=k.types.CONVEXPOLYHEDRON,Ne=k.types.HEIGHTFIELD,Pe=k.types.TRIMESH;var F=class e{get[Oe](){return this._intersectSphere}get[ke](){return this._intersectPlane}get[Ae](){return this._intersectBox}get[je](){return this._intersectConvex}get[Me](){return this._intersectConvex}get[Ne](){return this._intersectHeightfield}get[Pe](){return this._intersectTrimesh}constructor(t,n){t===void 0&&(t=new g),n===void 0&&(n=new g),this.from=t.clone(),this.to=n.clone(),this.direction=new g,this.precision=1e-4,this.checkCollisionResponse=!0,this.skipBackfaces=!1,this.collisionFilterMask=-1,this.collisionFilterGroup=-1,this.mode=e.ANY,this.result=new De,this.hasHit=!1,this.callback=e=>{}}intersectWorld(t,n){return this.mode=n.mode||e.ANY,this.result=n.result||new De,this.skipBackfaces=!!n.skipBackfaces,this.collisionFilterMask=n.collisionFilterMask===void 0?-1:n.collisionFilterMask,this.collisionFilterGroup=n.collisionFilterGroup===void 0?-1:n.collisionFilterGroup,this.checkCollisionResponse=n.checkCollisionResponse===void 0||n.checkCollisionResponse,n.from&&this.from.copy(n.from),n.to&&this.to.copy(n.to),this.callback=n.callback||(()=>{}),this.hasHit=!1,this.result.reset(),this.updateDirection(),this.getAABB(Ie),Le.length=0,t.broadphase.aabbQuery(t,Ie,Le),this.intersectBodies(Le),this.hasHit}intersectBody(e,t){t&&(this.result=t,this.updateDirection());let n=this.checkCollisionResponse;if(n&&!e.collisionResponse||(this.collisionFilterGroup&e.collisionFilterMask)===0||(e.collisionFilterGroup&this.collisionFilterMask)===0)return;let r=Be,i=Ve;for(let t=0,a=e.shapes.length;t<a;t++){let a=e.shapes[t];if((!n||a.collisionResponse)&&(e.quaternion.mult(e.shapeOrientations[t],i),e.quaternion.vmult(e.shapeOffsets[t],r),r.vadd(e.position,r),this.intersectShape(a,i,r,e),this.result.shouldStop))break}}intersectBodies(e,t){t&&(this.result=t,this.updateDirection());for(let t=0,n=e.length;!this.result.shouldStop&&t<n;t++)this.intersectBody(e[t])}updateDirection(){this.to.vsub(this.from,this.direction),this.direction.normalize()}intersectShape(e,t,n,r){let i=this.from;if(at(i,this.direction,n)>e.boundingSphereRadius)return;let a=this[e.type];a&&a.call(this,e,t,n,r,e)}_intersectBox(e,t,n,r,i){return this._intersectConvex(e.convexPolyhedronRepresentation,t,n,r,i)}_intersectPlane(e,t,n,r,i){let a=this.from,o=this.to,s=this.direction,c=new g(0,0,1);t.vmult(c,c);let l=new g;a.vsub(n,l);let u=l.dot(c);if(o.vsub(n,l),u*l.dot(c)>0||a.distanceTo(o)<u)return;let d=c.dot(s);if(Math.abs(d)<this.precision)return;let f=new g,p=new g,m=new g;a.vsub(n,f);let h=-c.dot(f)/d;s.scale(h,p),a.vadd(p,m),this.reportIntersection(c,m,i,r,-1)}getAABB(e){let{lowerBound:t,upperBound:n}=e,r=this.to,i=this.from;t.x=Math.min(r.x,i.x),t.y=Math.min(r.y,i.y),t.z=Math.min(r.z,i.z),n.x=Math.max(r.x,i.x),n.y=Math.max(r.y,i.y),n.z=Math.max(r.z,i.z)}_intersectHeightfield(e,t,n,r,i){e.data,e.elementSize;let a=We;a.from.copy(this.from),a.to.copy(this.to),A.pointToLocalFrame(n,t,a.from,a.from),A.pointToLocalFrame(n,t,a.to,a.to),a.updateDirection();let o=Ge,s,c,l,u;s=c=0,l=u=e.data.length-1;let d=new b;a.getAABB(d),e.getIndexOfPosition(d.lowerBound.x,d.lowerBound.y,o,!0),s=Math.max(s,o[0]),c=Math.max(c,o[1]),e.getIndexOfPosition(d.upperBound.x,d.upperBound.y,o,!0),l=Math.min(l,o[0]+1),u=Math.min(u,o[1]+1);for(let o=s;o<l;o++)for(let s=c;s<u;s++){if(this.result.shouldStop)return;if(e.getAabbAtIndex(o,s,d),d.overlapsRay(a)){if(e.getConvexTrianglePillar(o,s,!1),A.pointToWorldFrame(n,t,e.pillarOffset,Ue),this._intersectConvex(e.pillarConvex,t,Ue,r,i,He),this.result.shouldStop)return;e.getConvexTrianglePillar(o,s,!0),A.pointToWorldFrame(n,t,e.pillarOffset,Ue),this._intersectConvex(e.pillarConvex,t,Ue,r,i,He)}}}_intersectSphere(e,t,n,r,i){let a=this.from,o=this.to,s=e.radius,c=(o.x-a.x)**2+(o.y-a.y)**2+(o.z-a.z)**2,l=2*((o.x-a.x)*(a.x-n.x)+(o.y-a.y)*(a.y-n.y)+(o.z-a.z)*(a.z-n.z)),u=(a.x-n.x)**2+(a.y-n.y)**2+(a.z-n.z)**2-s**2,d=l**2-4*c*u,f=Ke,p=qe;if(!(d<0)){if(d===0)a.lerp(o,d,f),f.vsub(n,p),p.normalize(),this.reportIntersection(p,f,i,r,-1);else{let e=(-l-Math.sqrt(d))/(2*c),t=(-l+Math.sqrt(d))/(2*c);if(e>=0&&e<=1&&(a.lerp(o,e,f),f.vsub(n,p),p.normalize(),this.reportIntersection(p,f,i,r,-1)),this.result.shouldStop)return;t>=0&&t<=1&&(a.lerp(o,t,f),f.vsub(n,p),p.normalize(),this.reportIntersection(p,f,i,r,-1))}}}_intersectConvex(t,n,r,i,a,o){let s=Je,c=Ye,l=o&&o.faceList||null,u=t.faces,d=t.vertices,f=t.faceNormals,p=this.direction,m=this.from,h=this.to,g=m.distanceTo(h),_=l?l.length:u.length,v=this.result;for(let t=0;!v.shouldStop&&t<_;t++){let o=l?l[t]:t,h=u[o],_=f[o],y=n,b=r;c.copy(d[h[0]]),y.vmult(c,c),c.vadd(b,c),c.vsub(m,c),y.vmult(_,s);let x=p.dot(s);if(Math.abs(x)<this.precision)continue;let S=s.dot(c)/x;if(!(S<0)){p.scale(S,I),I.vadd(m,I),L.copy(d[h[0]]),y.vmult(L,L),b.vadd(L,L);for(let t=1;!v.shouldStop&&t<h.length-1;t++){R.copy(d[h[t]]),z.copy(d[h[t+1]]),y.vmult(R,R),y.vmult(z,z),b.vadd(R,R),b.vadd(z,z);let n=I.distanceTo(m);!(e.pointInTriangle(I,L,R,z)||e.pointInTriangle(I,R,L,z))||n>g||this.reportIntersection(s,I,a,i,o)}}}}_intersectTrimesh(t,n,r,i,a,o){let s=Xe,c=nt,l=rt,u=Ye,d=Ze,f=Qe,p=$e,m=tt,h=et,g=t.indices;t.vertices;let _=this.from,v=this.to,y=this.direction;l.position.copy(r),l.quaternion.copy(n),A.vectorToLocalFrame(r,n,y,d),A.pointToLocalFrame(r,n,_,f),A.pointToLocalFrame(r,n,v,p),p.x*=t.scale.x,p.y*=t.scale.y,p.z*=t.scale.z,f.x*=t.scale.x,f.y*=t.scale.y,f.z*=t.scale.z,p.vsub(f,d),d.normalize();let b=f.distanceSquared(p);t.tree.rayQuery(this,l,c);for(let o=0,l=c.length;!this.result.shouldStop&&o!==l;o++){let l=c[o];t.getNormal(l,s),t.getVertex(g[l*3],L),L.vsub(f,u);let p=d.dot(s),_=s.dot(u)/p;if(_<0)continue;d.scale(_,I),I.vadd(f,I),t.getVertex(g[l*3+1],R),t.getVertex(g[l*3+2],z);let v=I.distanceSquared(f);!(e.pointInTriangle(I,R,L,z)||e.pointInTriangle(I,L,R,z))||v>b||(A.vectorToWorldFrame(n,s,h),A.pointToWorldFrame(r,n,I,m),this.reportIntersection(h,m,a,i,l))}c.length=0}reportIntersection(t,n,r,i,a){let o=this.from,s=this.to,c=o.distanceTo(n),l=this.result;if(!(this.skipBackfaces&&t.dot(this.direction)>0))switch(l.hitFaceIndex=a===void 0?-1:a,this.mode){case e.ALL:this.hasHit=!0,l.set(o,s,t,n,r,i,c),l.hasHit=!0,this.callback(l);break;case e.CLOSEST:(c<l.distance||!l.hasHit)&&(this.hasHit=!0,l.hasHit=!0,l.set(o,s,t,n,r,i,c));break;case e.ANY:this.hasHit=!0,l.hasHit=!0,l.set(o,s,t,n,r,i,c),l.shouldStop=!0}}static pointInTriangle(e,t,n,r){r.vsub(t,B),n.vsub(t,Re),e.vsub(t,ze);let i=B.dot(B),a=B.dot(Re),o=B.dot(ze),s=Re.dot(Re),c=Re.dot(ze),l,u;return(l=s*o-a*c)>=0&&(u=i*c-a*o)>=0&&l+u<i*s-a*a}};F.CLOSEST=Fe.CLOSEST,F.ANY=Fe.ANY,F.ALL=Fe.ALL;var Ie=new b,Le=[],Re=new g,ze=new g,Be=new g,Ve=new T,I=new g,L=new g,R=new g,z=new g;new g,new De;var He={faceList:[0]},Ue=new g,We=new F,Ge=[],Ke=new g,qe=new g,Je=new g;new g,new g;var Ye=new g,Xe=new g,Ze=new g,Qe=new g,$e=new g,et=new g,tt=new g;new b;var nt=[],rt=new A,B=new g,it=new g;function at(e,t,n){n.vsub(e,B);let r=B.dot(t);return t.scale(r,it),it.vadd(e,it),n.distanceTo(it)}var ot=class{static defaults(e,t){e===void 0&&(e={});for(let n in t)n in e||(e[n]=t[n]);return e}},st=class e{constructor(t,n,r){r===void 0&&(r={}),r=ot.defaults(r,{collideConnected:!0,wakeUpBodies:!0}),this.equations=[],this.bodyA=t,this.bodyB=n,this.id=e.idCounter++,this.collideConnected=r.collideConnected,r.wakeUpBodies&&(t&&t.wakeUp(),n&&n.wakeUp())}update(){throw Error(`method update() not implmemented in this Constraint subclass!`)}enable(){let e=this.equations;for(let t=0;t<e.length;t++)e[t].enabled=!0}disable(){let e=this.equations;for(let t=0;t<e.length;t++)e[t].enabled=!1}};st.idCounter=0;var ct=class{constructor(){this.spatial=new g,this.rotational=new g}multiplyElement(e){return e.spatial.dot(this.spatial)+e.rotational.dot(this.rotational)}multiplyVectors(e,t){return e.dot(this.spatial)+t.dot(this.rotational)}},lt=class e{constructor(t,n,r,i){r===void 0&&(r=-1e6),i===void 0&&(i=1e6),this.id=e.idCounter++,this.minForce=r,this.maxForce=i,this.bi=t,this.bj=n,this.a=0,this.b=0,this.eps=0,this.jacobianElementA=new ct,this.jacobianElementB=new ct,this.enabled=!0,this.multiplier=0,this.setSpookParams(1e7,4,1/60)}setSpookParams(e,t,n){let r=t,i=e,a=n;this.a=4/(a*(1+4*r)),this.b=4*r/(1+4*r),this.eps=4/(a*a*i*(1+4*r))}computeB(e,t,n){let r=this.computeGW(),i=this.computeGq(),a=this.computeGiMf();return-i*e-r*t-a*n}computeGq(){let e=this.jacobianElementA,t=this.jacobianElementB,n=this.bi,r=this.bj,i=n.position,a=r.position;return e.spatial.dot(i)+t.spatial.dot(a)}computeGW(){let e=this.jacobianElementA,t=this.jacobianElementB,n=this.bi,r=this.bj,i=n.velocity,a=r.velocity,o=n.angularVelocity,s=r.angularVelocity;return e.multiplyVectors(i,o)+t.multiplyVectors(a,s)}computeGWlambda(){let e=this.jacobianElementA,t=this.jacobianElementB,n=this.bi,r=this.bj,i=n.vlambda,a=r.vlambda,o=n.wlambda,s=r.wlambda;return e.multiplyVectors(i,o)+t.multiplyVectors(a,s)}computeGiMf(){let e=this.jacobianElementA,t=this.jacobianElementB,n=this.bi,r=this.bj,i=n.force,a=n.torque,o=r.force,s=r.torque,c=n.invMassSolve,l=r.invMassSolve;return i.scale(c,ut),o.scale(l,dt),n.invInertiaWorldSolve.vmult(a,ft),r.invInertiaWorldSolve.vmult(s,pt),e.multiplyVectors(ut,ft)+t.multiplyVectors(dt,pt)}computeGiMGt(){let e=this.jacobianElementA,t=this.jacobianElementB,n=this.bi,r=this.bj,i=n.invMassSolve,a=r.invMassSolve,o=n.invInertiaWorldSolve,s=r.invInertiaWorldSolve,c=i+a;return o.vmult(e.rotational,mt),c+=mt.dot(e.rotational),s.vmult(t.rotational,mt),c+=mt.dot(t.rotational),c}addToWlambda(e){let t=this.jacobianElementA,n=this.jacobianElementB,r=this.bi,i=this.bj,a=ht;r.vlambda.addScaledVector(r.invMassSolve*e,t.spatial,r.vlambda),i.vlambda.addScaledVector(i.invMassSolve*e,n.spatial,i.vlambda),r.invInertiaWorldSolve.vmult(t.rotational,a),r.wlambda.addScaledVector(e,a,r.wlambda),i.invInertiaWorldSolve.vmult(n.rotational,a),i.wlambda.addScaledVector(e,a,i.wlambda)}computeC(){return this.computeGiMGt()+this.eps}};lt.idCounter=0;var ut=new g,dt=new g,ft=new g,pt=new g,mt=new g,ht=new g,gt=class extends lt{constructor(e,t,n){n===void 0&&(n=1e6),super(e,t,0,n),this.restitution=0,this.ri=new g,this.rj=new g,this.ni=new g}computeB(e){let t=this.a,n=this.b,r=this.bi,i=this.bj,a=this.ri,o=this.rj,s=_t,c=vt,l=r.velocity,u=r.angularVelocity;r.force,r.torque;let d=i.velocity,f=i.angularVelocity;i.force,i.torque;let p=yt,m=this.jacobianElementA,h=this.jacobianElementB,g=this.ni;a.cross(g,s),o.cross(g,c),g.negate(m.spatial),s.negate(m.rotational),h.spatial.copy(g),h.rotational.copy(c),p.copy(i.position),p.vadd(o,p),p.vsub(r.position,p),p.vsub(a,p);let _=g.dot(p),v=this.restitution+1,y=v*d.dot(g)-v*l.dot(g)+f.dot(c)-u.dot(s),b=this.computeGiMf();return-_*t-y*n-e*b}getImpactVelocityAlongNormal(){let e=bt,t=xt,n=St,r=Ct,i=wt;return this.bi.position.vadd(this.ri,n),this.bj.position.vadd(this.rj,r),this.bi.getVelocityAtWorldPoint(n,e),this.bj.getVelocityAtWorldPoint(r,t),e.vsub(t,i),this.ni.dot(i)}},_t=new g,vt=new g,yt=new g,bt=new g,xt=new g,St=new g,Ct=new g,wt=new g,Tt=class extends st{constructor(e,t,n,r,i){t===void 0&&(t=new g),r===void 0&&(r=new g),i===void 0&&(i=1e6),super(e,n),this.pivotA=t.clone(),this.pivotB=r.clone();let a=this.equationX=new gt(e,n),o=this.equationY=new gt(e,n),s=this.equationZ=new gt(e,n);this.equations.push(a,o,s),a.minForce=o.minForce=s.minForce=-i,a.maxForce=o.maxForce=s.maxForce=i,a.ni.set(1,0,0),o.ni.set(0,1,0),s.ni.set(0,0,1)}update(){let e=this.bodyA,t=this.bodyB,n=this.equationX,r=this.equationY,i=this.equationZ;e.quaternion.vmult(this.pivotA,n.ri),t.quaternion.vmult(this.pivotB,n.rj),r.ri.copy(n.ri),r.rj.copy(n.rj),i.ri.copy(n.ri),i.rj.copy(n.rj)}};new g,new g;var Et=class extends lt{constructor(e,t,n){n===void 0&&(n={});let r=n.maxForce===void 0?1e6:n.maxForce;super(e,t,-r,r),this.axisA=n.axisA?n.axisA.clone():new g(1,0,0),this.axisB=n.axisB?n.axisB.clone():new g(0,1,0),this.maxAngle=Math.PI/2}computeB(e){let t=this.a,n=this.b,r=this.axisA,i=this.axisB,a=Dt,o=Ot,s=this.jacobianElementA,c=this.jacobianElementB;r.cross(i,a),i.cross(r,o),s.rotational.copy(o),c.rotational.copy(a);let l=Math.cos(this.maxAngle)-r.dot(i),u=this.computeGW(),d=this.computeGiMf();return-l*t-u*n-e*d}},Dt=new g,Ot=new g;new g,new g;var kt=class extends st{constructor(e,t,n,r){r===void 0&&(r=1e6),super(e,t),n===void 0&&(n=e.position.distanceTo(t.position)),this.distance=n;let i=this.distanceEquation=new gt(e,t);this.equations.push(i),i.minForce=-r,i.maxForce=r}update(){let e=this.bodyA,t=this.bodyB,n=this.distanceEquation,r=this.distance*.5,i=n.ni;t.position.vsub(e.position,i),i.normalize(),i.scale(r,n.ri),i.scale(-r,n.rj)}},At=class extends Tt{constructor(e,t,n){n===void 0&&(n={});let r=n.maxForce===void 0?1e6:n.maxForce,i=new g,a=new g,o=new g;e.position.vadd(t.position,o),o.scale(.5,o),t.pointToLocalFrame(o,a),e.pointToLocalFrame(o,i),super(e,i,t,a,r),this.xA=e.vectorToLocalFrame(g.UNIT_X),this.xB=t.vectorToLocalFrame(g.UNIT_X),this.yA=e.vectorToLocalFrame(g.UNIT_Y),this.yB=t.vectorToLocalFrame(g.UNIT_Y),this.zA=e.vectorToLocalFrame(g.UNIT_Z),this.zB=t.vectorToLocalFrame(g.UNIT_Z);let s=this.rotationalEquation1=new Et(e,t,n),c=this.rotationalEquation2=new Et(e,t,n),l=this.rotationalEquation3=new Et(e,t,n);this.equations.push(s,c,l)}update(){let e=this.bodyA,t=this.bodyB;this.motorEquation;let n=this.rotationalEquation1,r=this.rotationalEquation2,i=this.rotationalEquation3;super.update(),e.vectorToWorldFrame(this.xA,n.axisA),t.vectorToWorldFrame(this.yB,n.axisB),e.vectorToWorldFrame(this.yA,r.axisA),t.vectorToWorldFrame(this.zB,r.axisB),e.vectorToWorldFrame(this.zA,i.axisA),t.vectorToWorldFrame(this.xB,i.axisB)}};new g,new g;var jt=class extends lt{constructor(e,t,n){n===void 0&&(n=1e6),super(e,t,-n,n),this.axisA=new g,this.axisB=new g,this.targetVelocity=0}computeB(e){this.a;let t=this.b;this.bi,this.bj;let n=this.axisA,r=this.axisB,i=this.jacobianElementA,a=this.jacobianElementB;i.rotational.copy(n),r.negate(a.rotational);let o=this.computeGW()-this.targetVelocity,s=this.computeGiMf();return-o*t-e*s}},Mt=class extends Tt{constructor(e,t,n){n===void 0&&(n={});let r=n.maxForce===void 0?1e6:n.maxForce,i=n.pivotA?n.pivotA.clone():new g,a=n.pivotB?n.pivotB.clone():new g;super(e,i,t,a,r),(this.axisA=n.axisA?n.axisA.clone():new g(1,0,0)).normalize(),(this.axisB=n.axisB?n.axisB.clone():new g(1,0,0)).normalize(),this.collideConnected=!!n.collideConnected;let o=this.rotationalEquation1=new Et(e,t,n),s=this.rotationalEquation2=new Et(e,t,n),c=this.motorEquation=new jt(e,t,r);c.enabled=!1,this.equations.push(o,s,c)}enableMotor(){this.motorEquation.enabled=!0}disableMotor(){this.motorEquation.enabled=!1}setMotorSpeed(e){this.motorEquation.targetVelocity=e}setMotorMaxForce(e){this.motorEquation.maxForce=e,this.motorEquation.minForce=-e}update(){let e=this.bodyA,t=this.bodyB,n=this.motorEquation,r=this.rotationalEquation1,i=this.rotationalEquation2,a=Nt,o=Pt,s=this.axisA,c=this.axisB;super.update(),e.quaternion.vmult(s,a),t.quaternion.vmult(c,o),a.tangents(r.axisA,i.axisA),r.axisB.copy(o),i.axisB.copy(o),this.motorEquation.enabled&&(e.quaternion.vmult(this.axisA,n.axisA),t.quaternion.vmult(this.axisB,n.axisB))}},Nt=new g,Pt=new g,Ft=class extends lt{constructor(e,t,n){super(e,t,-n,n),this.ri=new g,this.rj=new g,this.t=new g}computeB(e){this.a;let t=this.b;this.bi,this.bj;let n=this.ri,r=this.rj,i=It,a=Lt,o=this.t;n.cross(o,i),r.cross(o,a);let s=this.jacobianElementA,c=this.jacobianElementB;o.negate(s.spatial),i.negate(s.rotational),c.spatial.copy(o),c.rotational.copy(a);let l=this.computeGW(),u=this.computeGiMf();return-l*t-e*u}},It=new g,Lt=new g,Rt=class e{constructor(t,n,r){r=ot.defaults(r,{friction:.3,restitution:.3,contactEquationStiffness:1e7,contactEquationRelaxation:3,frictionEquationStiffness:1e7,frictionEquationRelaxation:3}),this.id=e.idCounter++,this.materials=[t,n],this.friction=r.friction,this.restitution=r.restitution,this.contactEquationStiffness=r.contactEquationStiffness,this.contactEquationRelaxation=r.contactEquationRelaxation,this.frictionEquationStiffness=r.frictionEquationStiffness,this.frictionEquationRelaxation=r.frictionEquationRelaxation}};Rt.idCounter=0;var zt=class e{constructor(t){t===void 0&&(t={});let n=``;typeof t==`string`&&(n=t,t={}),this.name=n,this.id=e.idCounter++,this.friction=t.friction===void 0?-1:t.friction,this.restitution=t.restitution===void 0?-1:t.restitution}};zt.idCounter=0;var Bt=class{constructor(e,t,n){n===void 0&&(n={}),this.restLength=typeof n.restLength==`number`?n.restLength:1,this.stiffness=n.stiffness||100,this.damping=n.damping||1,this.bodyA=e,this.bodyB=t,this.localAnchorA=new g,this.localAnchorB=new g,n.localAnchorA&&this.localAnchorA.copy(n.localAnchorA),n.localAnchorB&&this.localAnchorB.copy(n.localAnchorB),n.worldAnchorA&&this.setWorldAnchorA(n.worldAnchorA),n.worldAnchorB&&this.setWorldAnchorB(n.worldAnchorB)}setWorldAnchorA(e){this.bodyA.pointToLocalFrame(e,this.localAnchorA)}setWorldAnchorB(e){this.bodyB.pointToLocalFrame(e,this.localAnchorB)}getWorldAnchorA(e){this.bodyA.pointToWorldFrame(this.localAnchorA,e)}getWorldAnchorB(e){this.bodyB.pointToWorldFrame(this.localAnchorB,e)}applyForce(){let e=this.stiffness,t=this.damping,n=this.restLength,r=this.bodyA,i=this.bodyB,a=Vt,o=Ht,s=Ut,c=Wt,l=Zt,u=Gt,d=Kt,f=qt,p=Jt,m=Yt,h=Xt;this.getWorldAnchorA(u),this.getWorldAnchorB(d),u.vsub(r.position,f),d.vsub(i.position,p),d.vsub(u,a);let g=a.length();o.copy(a),o.normalize(),i.velocity.vsub(r.velocity,s),i.angularVelocity.cross(p,l),s.vadd(l,s),r.angularVelocity.cross(f,l),s.vsub(l,s),o.scale(-e*(g-n)-t*s.dot(o),c),r.force.vsub(c,r.force),i.force.vadd(c,i.force),f.cross(c,m),p.cross(c,h),r.torque.vsub(m,r.torque),i.torque.vadd(h,i.torque)}},Vt=new g,Ht=new g,Ut=new g,Wt=new g,Gt=new g,Kt=new g,qt=new g,Jt=new g,Yt=new g,Xt=new g,Zt=new g;new g,new g,new g,new g,new g,new g,new g,new g,new F,new g,new g,new g,new g(1,0,0),new g(0,1,0),new g(0,0,1),new g,new g,new g,new g,new g,new g,new g,new g,new g,new g,new g;var Qt=class extends k{constructor(e){if(super({type:k.types.SPHERE}),this.radius=e===void 0?1:e,this.radius<0)throw Error(`The sphere radius cannot be negative.`);this.updateBoundingSphereRadius()}calculateLocalInertia(e,t){t===void 0&&(t=new g);let n=2*e*this.radius*this.radius/5;return t.x=n,t.y=n,t.z=n,t}volume(){return 4*Math.PI*this.radius**3/3}updateBoundingSphereRadius(){this.boundingSphereRadius=this.radius}calculateWorldAABB(e,t,n,r){let i=this.radius,a=[`x`,`y`,`z`];for(let t=0;t<a.length;t++){let o=a[t];n[o]=e[o]-i,r[o]=e[o]+i}}};new g,new g,new g,new g,new g,new g,new g,new g,new g;var $t=class extends j{constructor(e,t,n,r){if(e===void 0&&(e=1),t===void 0&&(t=1),n===void 0&&(n=1),r===void 0&&(r=8),e<0)throw Error(`The cylinder radiusTop cannot be negative.`);if(t<0)throw Error(`The cylinder radiusBottom cannot be negative.`);let i=r,a=[],o=[],s=[],c=[],l=[],u=Math.cos,d=Math.sin;a.push(new g(-t*d(0),-n*.5,t*u(0))),c.push(0),a.push(new g(-e*d(0),n*.5,e*u(0))),l.push(1);for(let r=0;r<i;r++){let f=2*Math.PI/i*(r+1),p=2*Math.PI/i*(r+.5);r<i-1?(a.push(new g(-t*d(f),-n*.5,t*u(f))),c.push(2*r+2),a.push(new g(-e*d(f),n*.5,e*u(f))),l.push(2*r+3),s.push([2*r,2*r+1,2*r+3,2*r+2])):s.push([2*r,2*r+1,1,0]),(i%2==1||r<i/2)&&o.push(new g(-d(p),0,u(p)))}s.push(c),o.push(new g(0,1,0));let f=[];for(let e=0;e<l.length;e++)f.push(l[l.length-e-1]);s.push(f),super({vertices:a,faces:s,axes:o}),this.type=k.types.CYLINDER,this.radiusTop=e,this.radiusBottom=t,this.height=n,this.numSegments=r}};new g;var en=class extends k{constructor(e,t){t===void 0&&(t={}),t=ot.defaults(t,{maxValue:null,minValue:null,elementSize:1}),super({type:k.types.HEIGHTFIELD}),this.data=e,this.maxValue=t.maxValue,this.minValue=t.minValue,this.elementSize=t.elementSize,t.minValue===null&&this.updateMinValue(),t.maxValue===null&&this.updateMaxValue(),this.cacheEnabled=!0,this.pillarConvex=new j,this.pillarOffset=new g,this.updateBoundingSphereRadius(),this._cachedPillars={}}update(){this._cachedPillars={}}updateMinValue(){let e=this.data,t=e[0][0];for(let n=0;n!==e.length;n++)for(let r=0;r!==e[n].length;r++){let i=e[n][r];i<t&&(t=i)}this.minValue=t}updateMaxValue(){let e=this.data,t=e[0][0];for(let n=0;n!==e.length;n++)for(let r=0;r!==e[n].length;r++){let i=e[n][r];i>t&&(t=i)}this.maxValue=t}setHeightValueAtIndex(e,t,n){let r=this.data;r[e][t]=n,this.clearCachedConvexTrianglePillar(e,t,!1),e>0&&(this.clearCachedConvexTrianglePillar(e-1,t,!0),this.clearCachedConvexTrianglePillar(e-1,t,!1)),t>0&&(this.clearCachedConvexTrianglePillar(e,t-1,!0),this.clearCachedConvexTrianglePillar(e,t-1,!1)),t>0&&e>0&&this.clearCachedConvexTrianglePillar(e-1,t-1,!0)}getRectMinMax(e,t,n,r,i){i===void 0&&(i=[]);let a=this.data,o=this.minValue;for(let i=e;i<=n;i++)for(let e=t;e<=r;e++){let t=a[i][e];t>o&&(o=t)}i[0]=this.minValue,i[1]=o}getIndexOfPosition(e,t,n,r){let i=this.elementSize,a=this.data,o=Math.floor(e/i),s=Math.floor(t/i);return n[0]=o,n[1]=s,r&&(o<0&&(o=0),s<0&&(s=0),o>=a.length-1&&(o=a.length-1),s>=a[0].length-1&&(s=a[0].length-1)),!(o<0||s<0||o>=a.length-1||s>=a[0].length-1)}getTriangleAt(e,t,n,r,i,a){let o=tn;this.getIndexOfPosition(e,t,o,n);let s=o[0],c=o[1],l=this.data;n&&(s=Math.min(l.length-2,Math.max(0,s)),c=Math.min(l[0].length-2,Math.max(0,c)));let u=this.elementSize,d=(e/u-s)**2+(t/u-c)**2>(e/u-(s+1))**2+(t/u-(c+1))**2;return this.getTriangle(s,c,d,r,i,a),d}getNormalAt(e,t,n,r){let i=sn,a=cn,o=ln,s=un,c=dn;this.getTriangleAt(e,t,n,i,a,o),a.vsub(i,s),o.vsub(i,c),s.cross(c,r),r.normalize()}getAabbAtIndex(e,t,n){let{lowerBound:r,upperBound:i}=n,a=this.data,o=this.elementSize;r.set(e*o,t*o,a[e][t]),i.set((e+1)*o,(t+1)*o,a[e+1][t+1])}getHeightAt(e,t,n){let r=this.data,i=rn,a=an,o=on,s=tn;this.getIndexOfPosition(e,t,s,n);let c=s[0],l=s[1];n&&(c=Math.min(r.length-2,Math.max(0,c)),l=Math.min(r[0].length-2,Math.max(0,l)));let u=this.getTriangleAt(e,t,n,i,a,o);fn(e,t,i.x,i.y,a.x,a.y,o.x,o.y,nn);let d=nn;return u?r[c+1][l+1]*d.x+r[c][l+1]*d.y+r[c+1][l]*d.z:r[c][l]*d.x+r[c+1][l]*d.y+r[c][l+1]*d.z}getCacheConvexTrianglePillarKey(e,t,n){return`${e}_${t}_${+!!n}`}getCachedConvexTrianglePillar(e,t,n){return this._cachedPillars[this.getCacheConvexTrianglePillarKey(e,t,n)]}setCachedConvexTrianglePillar(e,t,n,r,i){this._cachedPillars[this.getCacheConvexTrianglePillarKey(e,t,n)]={convex:r,offset:i}}clearCachedConvexTrianglePillar(e,t,n){delete this._cachedPillars[this.getCacheConvexTrianglePillarKey(e,t,n)]}getTriangle(e,t,n,r,i,a){let o=this.data,s=this.elementSize;n?(r.set((e+1)*s,(t+1)*s,o[e+1][t+1]),i.set(e*s,(t+1)*s,o[e][t+1]),a.set((e+1)*s,t*s,o[e+1][t])):(r.set(e*s,t*s,o[e][t]),i.set((e+1)*s,t*s,o[e+1][t]),a.set(e*s,(t+1)*s,o[e][t+1]))}getConvexTrianglePillar(e,t,n){let r=this.pillarConvex,i=this.pillarOffset;if(this.cacheEnabled){let a=this.getCachedConvexTrianglePillar(e,t,n);if(a){this.pillarConvex=a.convex,this.pillarOffset=a.offset;return}r=new j,i=new g,this.pillarConvex=r,this.pillarOffset=i}let a=this.data,o=this.elementSize,s=r.faces;r.vertices.length=6;for(let e=0;e<6;e++)r.vertices[e]||(r.vertices[e]=new g);s.length=5;for(let e=0;e<5;e++)s[e]||(s[e]=[]);let c=r.vertices,l=(Math.min(a[e][t],a[e+1][t],a[e][t+1],a[e+1][t+1])-this.minValue)/2+this.minValue;n?(i.set((e+.75)*o,(t+.75)*o,l),c[0].set(.25*o,.25*o,a[e+1][t+1]-l),c[1].set(-.75*o,.25*o,a[e][t+1]-l),c[2].set(.25*o,-.75*o,a[e+1][t]-l),c[3].set(.25*o,.25*o,-Math.abs(l)-1),c[4].set(-.75*o,.25*o,-Math.abs(l)-1),c[5].set(.25*o,-.75*o,-Math.abs(l)-1),s[0][0]=0,s[0][1]=1,s[0][2]=2,s[1][0]=5,s[1][1]=4,s[1][2]=3,s[2][0]=2,s[2][1]=5,s[2][2]=3,s[2][3]=0,s[3][0]=3,s[3][1]=4,s[3][2]=1,s[3][3]=0,s[4][0]=1,s[4][1]=4,s[4][2]=5,s[4][3]=2):(i.set((e+.25)*o,(t+.25)*o,l),c[0].set(-.25*o,-.25*o,a[e][t]-l),c[1].set(.75*o,-.25*o,a[e+1][t]-l),c[2].set(-.25*o,.75*o,a[e][t+1]-l),c[3].set(-.25*o,-.25*o,-Math.abs(l)-1),c[4].set(.75*o,-.25*o,-Math.abs(l)-1),c[5].set(-.25*o,.75*o,-Math.abs(l)-1),s[0][0]=0,s[0][1]=1,s[0][2]=2,s[1][0]=5,s[1][1]=4,s[1][2]=3,s[2][0]=0,s[2][1]=2,s[2][2]=5,s[2][3]=3,s[3][0]=1,s[3][1]=0,s[3][2]=3,s[3][3]=4,s[4][0]=4,s[4][1]=5,s[4][2]=2,s[4][3]=1),r.computeNormals(),r.computeEdges(),r.updateBoundingSphereRadius(),this.setCachedConvexTrianglePillar(e,t,n,r,i)}calculateLocalInertia(e,t){return t===void 0&&(t=new g),t.set(0,0,0),t}volume(){return Number.MAX_VALUE}calculateWorldAABB(e,t,n,r){n.set(-Number.MAX_VALUE,-Number.MAX_VALUE,-Number.MAX_VALUE),r.set(Number.MAX_VALUE,Number.MAX_VALUE,Number.MAX_VALUE)}updateBoundingSphereRadius(){let e=this.data,t=this.elementSize;this.boundingSphereRadius=new g(e.length*t,e[0].length*t,Math.max(Math.abs(this.maxValue),Math.abs(this.minValue))).length()}setHeightsFromImage(e,t){let{x:n,z:r,y:i}=t,a=document.createElement(`canvas`);a.width=e.width,a.height=e.height;let o=a.getContext(`2d`);o.drawImage(e,0,0);let s=o.getImageData(0,0,e.width,e.height),c=this.data;c.length=0,this.elementSize=Math.abs(n)/s.width;for(let e=0;e<s.height;e++){let t=[];for(let i=0;i<s.width;i++){let a=s.data[(e*s.height+i)*4],o=s.data[(e*s.height+i)*4+1],c=s.data[(e*s.height+i)*4+2],l=(a+o+c)/4/255*r;n<0?t.push(l):t.unshift(l)}i<0?c.unshift(t):c.push(t)}this.updateMaxValue(),this.updateMinValue(),this.update()}},tn=[],nn=new g,rn=new g,an=new g,on=new g,sn=new g,cn=new g,ln=new g,un=new g,dn=new g;function fn(e,t,n,r,i,a,o,s,c){c.x=((a-s)*(e-o)+(o-i)*(t-s))/((a-s)*(n-o)+(o-i)*(r-s)),c.y=((s-r)*(e-o)+(n-o)*(t-s))/((a-s)*(n-o)+(o-i)*(r-s)),c.z=1-c.x-c.y}new g,new b,new g,new b,new g,new g,new g,new g,new g,new g,new g,new b,new g,new A,new b;var pn=class{constructor(){this.equations=[]}solve(e,t){return 0}addEquation(e){e.enabled&&!e.bi.isTrigger&&!e.bj.isTrigger&&this.equations.push(e)}removeEquation(e){let t=this.equations,n=t.indexOf(e);n!==-1&&t.splice(n,1)}removeAllEquations(){this.equations.length=0}},mn=class extends pn{constructor(){super(),this.iterations=10,this.tolerance=1e-7}solve(e,t){let n=0,r=this.iterations,i=this.tolerance*this.tolerance,a=this.equations,o=a.length,s=t.bodies,c=s.length,l=e,u,d,f,p,m,h;if(o!==0)for(let e=0;e!==c;e++)s[e].updateSolveMassProperties();let g=gn,_=_n,v=hn;g.length=o,_.length=o,v.length=o;for(let e=0;e!==o;e++){let t=a[e];v[e]=0,_[e]=t.computeB(l),g[e]=1/t.computeC()}if(o!==0){for(let e=0;e!==c;e++){let t=s[e],n=t.vlambda,r=t.wlambda;n.set(0,0,0),r.set(0,0,0)}for(n=0;n!==r;n++){p=0;for(let e=0;e!==o;e++){let t=a[e];u=_[e],d=g[e],h=v[e],m=t.computeGWlambda(),f=d*(u-m-t.eps*h),h+f<t.minForce?f=t.minForce-h:h+f>t.maxForce&&(f=t.maxForce-h),v[e]+=f,p+=f>0?f:-f,t.addToWlambda(f)}if(p*p<i)break}for(let e=0;e!==c;e++){let t=s[e],n=t.velocity,r=t.angularVelocity;t.vlambda.vmul(t.linearFactor,t.vlambda),n.vadd(t.vlambda,n),t.wlambda.vmul(t.angularFactor,t.wlambda),r.vadd(t.wlambda,r)}let e=a.length,t=1/l;for(;e--;)a[e].multiplier=v[e]*t}return n}},hn=[],gn=[],_n=[];P.STATIC;var vn=class{constructor(){this.objects=[],this.type=Object}release(){let e=arguments.length;for(let t=0;t!==e;t++)this.objects.push(t<0||arguments.length<=t?void 0:arguments[t]);return this}get(){return this.objects.length===0?this.constructObject():this.objects.pop()}constructObject(){throw Error(`constructObject() not implemented in this Pool subclass yet!`)}resize(e){let t=this.objects;for(;t.length>e;)t.pop();for(;t.length<e;)t.push(this.constructObject());return this}},yn=class extends vn{constructor(){super(...arguments),this.type=g}constructObject(){return new g}},V={sphereSphere:k.types.SPHERE,spherePlane:k.types.SPHERE|k.types.PLANE,boxBox:k.types.BOX|k.types.BOX,sphereBox:k.types.SPHERE|k.types.BOX,planeBox:k.types.PLANE|k.types.BOX,convexConvex:k.types.CONVEXPOLYHEDRON,sphereConvex:k.types.SPHERE|k.types.CONVEXPOLYHEDRON,planeConvex:k.types.PLANE|k.types.CONVEXPOLYHEDRON,boxConvex:k.types.BOX|k.types.CONVEXPOLYHEDRON,sphereHeightfield:k.types.SPHERE|k.types.HEIGHTFIELD,boxHeightfield:k.types.BOX|k.types.HEIGHTFIELD,convexHeightfield:k.types.CONVEXPOLYHEDRON|k.types.HEIGHTFIELD,sphereParticle:k.types.PARTICLE|k.types.SPHERE,planeParticle:k.types.PLANE|k.types.PARTICLE,boxParticle:k.types.BOX|k.types.PARTICLE,convexParticle:k.types.PARTICLE|k.types.CONVEXPOLYHEDRON,cylinderCylinder:k.types.CYLINDER,sphereCylinder:k.types.SPHERE|k.types.CYLINDER,planeCylinder:k.types.PLANE|k.types.CYLINDER,boxCylinder:k.types.BOX|k.types.CYLINDER,convexCylinder:k.types.CONVEXPOLYHEDRON|k.types.CYLINDER,heightfieldCylinder:k.types.HEIGHTFIELD|k.types.CYLINDER,particleCylinder:k.types.PARTICLE|k.types.CYLINDER,sphereTrimesh:k.types.SPHERE|k.types.TRIMESH,planeTrimesh:k.types.PLANE|k.types.TRIMESH},bn=class{get[V.sphereSphere](){return this.sphereSphere}get[V.spherePlane](){return this.spherePlane}get[V.boxBox](){return this.boxBox}get[V.sphereBox](){return this.sphereBox}get[V.planeBox](){return this.planeBox}get[V.convexConvex](){return this.convexConvex}get[V.sphereConvex](){return this.sphereConvex}get[V.planeConvex](){return this.planeConvex}get[V.boxConvex](){return this.boxConvex}get[V.sphereHeightfield](){return this.sphereHeightfield}get[V.boxHeightfield](){return this.boxHeightfield}get[V.convexHeightfield](){return this.convexHeightfield}get[V.sphereParticle](){return this.sphereParticle}get[V.planeParticle](){return this.planeParticle}get[V.boxParticle](){return this.boxParticle}get[V.convexParticle](){return this.convexParticle}get[V.cylinderCylinder](){return this.convexConvex}get[V.sphereCylinder](){return this.sphereConvex}get[V.planeCylinder](){return this.planeConvex}get[V.boxCylinder](){return this.boxConvex}get[V.convexCylinder](){return this.convexConvex}get[V.heightfieldCylinder](){return this.heightfieldCylinder}get[V.particleCylinder](){return this.particleCylinder}get[V.sphereTrimesh](){return this.sphereTrimesh}get[V.planeTrimesh](){return this.planeTrimesh}constructor(e){this.contactPointPool=[],this.frictionEquationPool=[],this.result=[],this.frictionResult=[],this.v3pool=new yn,this.world=e,this.currentContactMaterial=e.defaultContactMaterial,this.enableFrictionReduction=!1}createContactEquation(e,t,n,r,i,a){let o;this.contactPointPool.length?(o=this.contactPointPool.pop(),o.bi=e,o.bj=t):o=new gt(e,t),o.enabled=e.collisionResponse&&t.collisionResponse&&n.collisionResponse&&r.collisionResponse;let s=this.currentContactMaterial;o.restitution=s.restitution,o.setSpookParams(s.contactEquationStiffness,s.contactEquationRelaxation,this.world.dt);let c=n.material||e.material,l=r.material||t.material;return c&&l&&c.restitution>=0&&l.restitution>=0&&(o.restitution=c.restitution*l.restitution),o.si=i||n,o.sj=a||r,o}createFrictionEquationsFromContact(e,t){let n=e.bi,r=e.bj,i=e.si,a=e.sj,o=this.world,s=this.currentContactMaterial,c=s.friction,l=i.material||n.material,u=a.material||r.material;if(l&&u&&l.friction>=0&&u.friction>=0&&(c=l.friction*u.friction),c>0){let i=c*(o.frictionGravity||o.gravity).length(),a=n.invMass+r.invMass;a>0&&(a=1/a);let l=this.frictionEquationPool,u=l.length?l.pop():new Ft(n,r,i*a),d=l.length?l.pop():new Ft(n,r,i*a);return u.bi=d.bi=n,u.bj=d.bj=r,u.minForce=d.minForce=-i*a,u.maxForce=d.maxForce=i*a,u.ri.copy(e.ri),u.rj.copy(e.rj),d.ri.copy(e.ri),d.rj.copy(e.rj),e.ni.tangents(u.t,d.t),u.setSpookParams(s.frictionEquationStiffness,s.frictionEquationRelaxation,o.dt),d.setSpookParams(s.frictionEquationStiffness,s.frictionEquationRelaxation,o.dt),u.enabled=d.enabled=e.enabled,t.push(u,d),!0}return!1}createFrictionFromAverage(e){let t=this.result[this.result.length-1];if(!this.createFrictionEquationsFromContact(t,this.frictionResult)||e===1)return;let n=this.frictionResult[this.frictionResult.length-2],r=this.frictionResult[this.frictionResult.length-1];H.setZero(),U.setZero(),W.setZero();let i=t.bi;t.bj;for(let n=0;n!==e;n++)t=this.result[this.result.length-1-n],t.bi===i?(H.vsub(t.ni,H),U.vadd(t.rj,U),W.vadd(t.ri,W)):(H.vadd(t.ni,H),U.vadd(t.ri,U),W.vadd(t.rj,W));let a=1/e;U.scale(a,n.ri),W.scale(a,n.rj),r.ri.copy(n.ri),r.rj.copy(n.rj),H.normalize(),H.tangents(n.t,r.t)}getContacts(e,t,n,r,i,a,o){this.contactPointPool=i,this.frictionEquationPool=o,this.result=r,this.frictionResult=a;let s=Cn,c=wn,l=xn,u=Sn;for(let r=0,i=e.length;r!==i;r++){let i=e[r],a=t[r],o=null;i.material&&a.material&&(o=n.getContactMaterial(i.material,a.material)||null);let d=i.type&P.KINEMATIC&&a.type&P.STATIC||i.type&P.STATIC&&a.type&P.KINEMATIC||i.type&P.KINEMATIC&&a.type&P.KINEMATIC;for(let e=0;e<i.shapes.length;e++){i.quaternion.mult(i.shapeOrientations[e],s),i.quaternion.vmult(i.shapeOffsets[e],l),l.vadd(i.position,l);let t=i.shapes[e];for(let e=0;e<a.shapes.length;e++){a.quaternion.mult(a.shapeOrientations[e],c),a.quaternion.vmult(a.shapeOffsets[e],u),u.vadd(a.position,u);let r=a.shapes[e];if(!(t.collisionFilterMask&r.collisionFilterGroup&&r.collisionFilterMask&t.collisionFilterGroup)||l.distanceTo(u)>t.boundingSphereRadius+r.boundingSphereRadius)continue;let f=null;t.material&&r.material&&(f=n.getContactMaterial(t.material,r.material)||null),this.currentContactMaterial=f||o||n.defaultContactMaterial;let p=t.type|r.type,m=this[p];if(m){let e=!1;e=t.type<r.type?m.call(this,t,r,l,u,s,c,i,a,t,r,d):m.call(this,r,t,u,l,c,s,a,i,t,r,d),e&&d&&(n.shapeOverlapKeeper.set(t.id,r.id),n.bodyOverlapKeeper.set(i.id,a.id))}}}}}sphereSphere(e,t,n,r,i,a,o,s,c,l,u){if(u)return n.distanceSquared(r)<(e.radius+t.radius)**2;let d=this.createContactEquation(o,s,e,t,c,l);r.vsub(n,d.ni),d.ni.normalize(),d.ri.copy(d.ni),d.rj.copy(d.ni),d.ri.scale(e.radius,d.ri),d.rj.scale(-t.radius,d.rj),d.ri.vadd(n,d.ri),d.ri.vsub(o.position,d.ri),d.rj.vadd(r,d.rj),d.rj.vsub(s.position,d.rj),this.result.push(d),this.createFrictionEquationsFromContact(d,this.frictionResult)}spherePlane(e,t,n,r,i,a,o,s,c,l,u){let d=this.createContactEquation(o,s,e,t,c,l);if(d.ni.set(0,0,1),a.vmult(d.ni,d.ni),d.ni.negate(d.ni),d.ni.normalize(),d.ni.scale(e.radius,d.ri),n.vsub(r,Un),d.ni.scale(d.ni.dot(Un),Wn),Un.vsub(Wn,d.rj),-Un.dot(d.ni)<=e.radius){if(u)return!0;let e=d.ri,t=d.rj;e.vadd(n,e),e.vsub(o.position,e),t.vadd(r,t),t.vsub(s.position,t),this.result.push(d),this.createFrictionEquationsFromContact(d,this.frictionResult)}}boxBox(e,t,n,r,i,a,o,s,c,l,u){return e.convexPolyhedronRepresentation.material=e.material,t.convexPolyhedronRepresentation.material=t.material,e.convexPolyhedronRepresentation.collisionResponse=e.collisionResponse,t.convexPolyhedronRepresentation.collisionResponse=t.collisionResponse,this.convexConvex(e.convexPolyhedronRepresentation,t.convexPolyhedronRepresentation,n,r,i,a,o,s,e,t,u)}sphereBox(e,t,n,r,i,a,o,s,c,l,u){let d=this.v3pool,f=$n;n.vsub(r,Yn),t.getSideNormals(f,a);let p=e.radius,m=!1,h=tr,g=nr,_=rr,v=null,y=0,b=0,x=0,S=null;for(let e=0,t=f.length;e!==t&&m===!1;e++){let t=Xn;t.copy(f[e]);let n=t.length();t.normalize();let r=Yn.dot(t);if(r<n+p&&r>0){let i=Zn,a=Qn;i.copy(f[(e+1)%3]),a.copy(f[(e+2)%3]);let o=i.length(),s=a.length();i.normalize(),a.normalize();let c=Yn.dot(i),l=Yn.dot(a);if(c<o&&c>-o&&l<s&&l>-s){let e=Math.abs(r-n-p);if((S===null||e<S)&&(S=e,b=c,x=l,v=n,h.copy(t),g.copy(i),_.copy(a),y++,u))return!0}}}if(y){m=!0;let i=this.createContactEquation(o,s,e,t,c,l);h.scale(-p,i.ri),i.ni.copy(h),i.ni.negate(i.ni),h.scale(v,h),g.scale(b,g),h.vadd(g,h),_.scale(x,_),h.vadd(_,i.rj),i.ri.vadd(n,i.ri),i.ri.vsub(o.position,i.ri),i.rj.vadd(r,i.rj),i.rj.vsub(s.position,i.rj),this.result.push(i),this.createFrictionEquationsFromContact(i,this.frictionResult)}let C=d.get(),w=er;for(let i=0;i!==2&&!m;i++)for(let a=0;a!==2&&!m;a++)for(let d=0;d!==2&&!m;d++)if(C.set(0,0,0),i?C.vadd(f[0],C):C.vsub(f[0],C),a?C.vadd(f[1],C):C.vsub(f[1],C),d?C.vadd(f[2],C):C.vsub(f[2],C),r.vadd(C,w),w.vsub(n,w),w.lengthSquared()<p*p){if(u)return!0;m=!0;let i=this.createContactEquation(o,s,e,t,c,l);i.ri.copy(w),i.ri.normalize(),i.ni.copy(i.ri),i.ri.scale(p,i.ri),i.rj.copy(C),i.ri.vadd(n,i.ri),i.ri.vsub(o.position,i.ri),i.rj.vadd(r,i.rj),i.rj.vsub(s.position,i.rj),this.result.push(i),this.createFrictionEquationsFromContact(i,this.frictionResult)}d.release(C),C=null;let T=d.get(),E=d.get(),D=d.get(),O=d.get(),k=d.get(),A=f.length;for(let i=0;i!==A&&!m;i++)for(let a=0;a!==A&&!m;a++)if(i%3!=a%3){f[a].cross(f[i],T),T.normalize(),f[i].vadd(f[a],E),D.copy(n),D.vsub(E,D),D.vsub(r,D);let d=D.dot(T);T.scale(d,O);let h=0;for(;h===i%3||h===a%3;)h++;k.copy(n),k.vsub(O,k),k.vsub(E,k),k.vsub(r,k);let g=Math.abs(d),_=k.length();if(g<f[h].length()&&_<p){if(u)return!0;m=!0;let i=this.createContactEquation(o,s,e,t,c,l);E.vadd(O,i.rj),i.rj.copy(i.rj),k.negate(i.ni),i.ni.normalize(),i.ri.copy(i.rj),i.ri.vadd(r,i.ri),i.ri.vsub(n,i.ri),i.ri.normalize(),i.ri.scale(p,i.ri),i.ri.vadd(n,i.ri),i.ri.vsub(o.position,i.ri),i.rj.vadd(r,i.rj),i.rj.vsub(s.position,i.rj),this.result.push(i),this.createFrictionEquationsFromContact(i,this.frictionResult)}}d.release(T,E,D,O,k)}planeBox(e,t,n,r,i,a,o,s,c,l,u){return t.convexPolyhedronRepresentation.material=t.material,t.convexPolyhedronRepresentation.collisionResponse=t.collisionResponse,t.convexPolyhedronRepresentation.id=t.id,this.planeConvex(e,t.convexPolyhedronRepresentation,n,r,i,a,o,s,e,t,u)}convexConvex(e,t,n,r,i,a,o,s,c,l,u,d,f){let p=vr;if(!(n.distanceTo(r)>e.boundingSphereRadius+t.boundingSphereRadius)&&e.findSeparatingAxis(t,n,i,r,a,p,d,f)){let d=[],f=yr;e.clipAgainstHull(n,i,t,r,a,p,-100,100,d);let m=0;for(let i=0;i!==d.length;i++){if(u)return!0;let a=this.createContactEquation(o,s,e,t,c,l),h=a.ri,g=a.rj;p.negate(a.ni),d[i].normal.negate(f),f.scale(d[i].depth,f),d[i].point.vadd(f,h),g.copy(d[i].point),h.vsub(n,h),g.vsub(r,g),h.vadd(n,h),h.vsub(o.position,h),g.vadd(r,g),g.vsub(s.position,g),this.result.push(a),m++,this.enableFrictionReduction||this.createFrictionEquationsFromContact(a,this.frictionResult)}this.enableFrictionReduction&&m&&this.createFrictionFromAverage(m)}}sphereConvex(e,t,n,r,i,a,o,s,c,l,u){let d=this.v3pool;n.vsub(r,ir);let f=t.faceNormals,p=t.faces,m=t.vertices,h=e.radius,g=!1;for(let i=0;i!==m.length;i++){let d=m[i],f=cr;a.vmult(d,f),r.vadd(f,f);let p=sr;if(f.vsub(n,p),p.lengthSquared()<h*h){if(u)return!0;g=!0;let i=this.createContactEquation(o,s,e,t,c,l);i.ri.copy(p),i.ri.normalize(),i.ni.copy(i.ri),i.ri.scale(h,i.ri),f.vsub(r,i.rj),i.ri.vadd(n,i.ri),i.ri.vsub(o.position,i.ri),i.rj.vadd(r,i.rj),i.rj.vsub(s.position,i.rj),this.result.push(i),this.createFrictionEquationsFromContact(i,this.frictionResult);return}}for(let i=0,_=p.length;i!==_&&g===!1;i++){let _=f[i],v=p[i],y=lr;a.vmult(_,y);let b=ur;a.vmult(m[v[0]],b),b.vadd(r,b);let x=dr;y.scale(-h,x),n.vadd(x,x);let S=fr;x.vsub(b,S);let C=S.dot(y),w=pr;if(n.vsub(b,w),C<0&&w.dot(y)>0){let i=[];for(let e=0,t=v.length;e!==t;e++){let t=d.get();a.vmult(m[v[e]],t),r.vadd(t,t),i.push(t)}if(Jn(i,y,n)){if(u)return!0;g=!0;let a=this.createContactEquation(o,s,e,t,c,l);y.scale(-h,a.ri),y.negate(a.ni);let f=d.get();y.scale(-C,f);let p=d.get();y.scale(-h,p),n.vsub(r,a.rj),a.rj.vadd(p,a.rj),a.rj.vadd(f,a.rj),a.rj.vadd(r,a.rj),a.rj.vsub(s.position,a.rj),a.ri.vadd(n,a.ri),a.ri.vsub(o.position,a.ri),d.release(f),d.release(p),this.result.push(a),this.createFrictionEquationsFromContact(a,this.frictionResult);for(let e=0,t=i.length;e!==t;e++)d.release(i[e]);return}for(let f=0;f!==v.length;f++){let p=d.get(),g=d.get();a.vmult(m[v[(f+1)%v.length]],p),a.vmult(m[v[(f+2)%v.length]],g),r.vadd(p,p),r.vadd(g,g);let _=ar;g.vsub(p,_);let y=or;_.unit(y);let b=d.get(),x=d.get();n.vsub(p,x);let S=x.dot(y);y.scale(S,b),b.vadd(p,b);let C=d.get();if(b.vsub(n,C),S>0&&S*S<_.lengthSquared()&&C.lengthSquared()<h*h){if(u)return!0;let a=this.createContactEquation(o,s,e,t,c,l);b.vsub(r,a.rj),b.vsub(n,a.ni),a.ni.normalize(),a.ni.scale(h,a.ri),a.rj.vadd(r,a.rj),a.rj.vsub(s.position,a.rj),a.ri.vadd(n,a.ri),a.ri.vsub(o.position,a.ri),this.result.push(a),this.createFrictionEquationsFromContact(a,this.frictionResult);for(let e=0,t=i.length;e!==t;e++)d.release(i[e]);d.release(p),d.release(g),d.release(b),d.release(C),d.release(x);return}d.release(p),d.release(g),d.release(b),d.release(C),d.release(x)}for(let e=0,t=i.length;e!==t;e++)d.release(i[e])}}}planeConvex(e,t,n,r,i,a,o,s,c,l,u){let d=mr,f=hr;f.set(0,0,1),i.vmult(f,f);let p=0,m=gr;for(let i=0;i!==t.vertices.length;i++)if(d.copy(t.vertices[i]),a.vmult(d,d),r.vadd(d,d),d.vsub(n,m),f.dot(m)<=0){if(u)return!0;let i=this.createContactEquation(o,s,e,t,c,l),a=_r;f.scale(f.dot(m),a),d.vsub(a,a),a.vsub(n,i.ri),i.ni.copy(f),d.vsub(r,i.rj),i.ri.vadd(n,i.ri),i.ri.vsub(o.position,i.ri),i.rj.vadd(r,i.rj),i.rj.vsub(s.position,i.rj),this.result.push(i),p++,this.enableFrictionReduction||this.createFrictionEquationsFromContact(i,this.frictionResult)}this.enableFrictionReduction&&p&&this.createFrictionFromAverage(p)}boxConvex(e,t,n,r,i,a,o,s,c,l,u){return e.convexPolyhedronRepresentation.material=e.material,e.convexPolyhedronRepresentation.collisionResponse=e.collisionResponse,this.convexConvex(e.convexPolyhedronRepresentation,t,n,r,i,a,o,s,e,t,u)}sphereHeightfield(e,t,n,r,i,a,o,s,c,l,u){let d=t.data,f=e.radius,p=t.elementSize,m=Nr,h=Mr;A.pointToLocalFrame(r,a,n,h);let g=Math.floor((h.x-f)/p)-1,_=Math.ceil((h.x+f)/p)+1,v=Math.floor((h.y-f)/p)-1,y=Math.ceil((h.y+f)/p)+1;if(_<0||y<0||g>d.length||v>d[0].length)return;g<0&&(g=0),_<0&&(_=0),v<0&&(v=0),y<0&&(y=0),g>=d.length&&(g=d.length-1),_>=d.length&&(_=d.length-1),y>=d[0].length&&(y=d[0].length-1),v>=d[0].length&&(v=d[0].length-1);let b=[];t.getRectMinMax(g,v,_,y,b);let x=b[0],S=b[1];if(h.z-f>S||h.z+f<x)return;let C=this.result;for(let c=g;c<_;c++)for(let l=v;l<y;l++){let d=C.length,f=!1;if(t.getConvexTrianglePillar(c,l,!1),A.pointToWorldFrame(r,a,t.pillarOffset,m),n.distanceTo(m)<t.pillarConvex.boundingSphereRadius+e.boundingSphereRadius&&(f=this.sphereConvex(e,t.pillarConvex,n,m,i,a,o,s,e,t,u)),u&&f||(t.getConvexTrianglePillar(c,l,!0),A.pointToWorldFrame(r,a,t.pillarOffset,m),n.distanceTo(m)<t.pillarConvex.boundingSphereRadius+e.boundingSphereRadius&&(f=this.sphereConvex(e,t.pillarConvex,n,m,i,a,o,s,e,t,u)),u&&f))return!0;if(C.length-d>2)return}}boxHeightfield(e,t,n,r,i,a,o,s,c,l,u){return e.convexPolyhedronRepresentation.material=e.material,e.convexPolyhedronRepresentation.collisionResponse=e.collisionResponse,this.convexHeightfield(e.convexPolyhedronRepresentation,t,n,r,i,a,o,s,e,t,u)}convexHeightfield(e,t,n,r,i,a,o,s,c,l,u){let d=t.data,f=t.elementSize,p=e.boundingSphereRadius,m=Ar,h=jr,g=kr;A.pointToLocalFrame(r,a,n,g);let _=Math.floor((g.x-p)/f)-1,v=Math.ceil((g.x+p)/f)+1,y=Math.floor((g.y-p)/f)-1,b=Math.ceil((g.y+p)/f)+1;if(v<0||b<0||_>d.length||y>d[0].length)return;_<0&&(_=0),v<0&&(v=0),y<0&&(y=0),b<0&&(b=0),_>=d.length&&(_=d.length-1),v>=d.length&&(v=d.length-1),b>=d[0].length&&(b=d[0].length-1),y>=d[0].length&&(y=d[0].length-1);let x=[];t.getRectMinMax(_,y,v,b,x);let S=x[0],C=x[1];if(!(g.z-p>C||g.z+p<S))for(let c=_;c<v;c++)for(let l=y;l<b;l++){let d=!1;if(t.getConvexTrianglePillar(c,l,!1),A.pointToWorldFrame(r,a,t.pillarOffset,m),n.distanceTo(m)<t.pillarConvex.boundingSphereRadius+e.boundingSphereRadius&&(d=this.convexConvex(e,t.pillarConvex,n,m,i,a,o,s,null,null,u,h,null)),u&&d||(t.getConvexTrianglePillar(c,l,!0),A.pointToWorldFrame(r,a,t.pillarOffset,m),n.distanceTo(m)<t.pillarConvex.boundingSphereRadius+e.boundingSphereRadius&&(d=this.convexConvex(e,t.pillarConvex,n,m,i,a,o,s,null,null,u,h,null)),u&&d))return!0}}sphereParticle(e,t,n,r,i,a,o,s,c,l,u){let d=Cr;if(d.set(0,0,1),r.vsub(n,d),d.lengthSquared()<=e.radius*e.radius){if(u)return!0;let n=this.createContactEquation(s,o,t,e,c,l);d.normalize(),n.rj.copy(d),n.rj.scale(e.radius,n.rj),n.ni.copy(d),n.ni.negate(n.ni),n.ri.set(0,0,0),this.result.push(n),this.createFrictionEquationsFromContact(n,this.frictionResult)}}planeParticle(e,t,n,r,i,a,o,s,c,l,u){let d=br;d.set(0,0,1),o.quaternion.vmult(d,d);let f=xr;if(r.vsub(o.position,f),d.dot(f)<=0){if(u)return!0;let n=this.createContactEquation(s,o,t,e,c,l);n.ni.copy(d),n.ni.negate(n.ni),n.ri.set(0,0,0);let i=Sr;d.scale(d.dot(r),i),r.vsub(i,i),n.rj.copy(i),this.result.push(n),this.createFrictionEquationsFromContact(n,this.frictionResult)}}boxParticle(e,t,n,r,i,a,o,s,c,l,u){return e.convexPolyhedronRepresentation.material=e.material,e.convexPolyhedronRepresentation.collisionResponse=e.collisionResponse,this.convexParticle(e.convexPolyhedronRepresentation,t,n,r,i,a,o,s,e,t,u)}convexParticle(e,t,n,r,i,a,o,s,c,l,u){let d=-1,f=Er,p=Or,m=null,h=Tr;if(h.copy(r),h.vsub(n,h),i.conjugate(wr),wr.vmult(h,h),e.pointIsInside(h)){e.worldVerticesNeedsUpdate&&e.computeWorldVertices(n,i),e.worldFaceNormalsNeedsUpdate&&e.computeWorldFaceNormals(i);for(let t=0,n=e.faces.length;t!==n;t++){let n=[e.worldVertices[e.faces[t][0]]],i=e.worldFaceNormals[t];r.vsub(n[0],Dr);let a=-i.dot(Dr);if(m===null||Math.abs(a)<Math.abs(m)){if(u)return!0;m=a,d=t,f.copy(i)}}if(d!==-1){let i=this.createContactEquation(s,o,t,e,c,l);f.scale(m,p),p.vadd(r,p),p.vsub(n,p),i.rj.copy(p),f.negate(i.ni),i.ri.set(0,0,0);let a=i.ri,u=i.rj;a.vadd(r,a),a.vsub(s.position,a),u.vadd(n,u),u.vsub(o.position,u),this.result.push(i),this.createFrictionEquationsFromContact(i,this.frictionResult)}else console.warn(`Point found inside convex, but did not find penetrating face!`)}}heightfieldCylinder(e,t,n,r,i,a,o,s,c,l,u){return this.convexHeightfield(t,e,r,n,a,i,s,o,c,l,u)}particleCylinder(e,t,n,r,i,a,o,s,c,l,u){return this.convexParticle(t,e,r,n,a,i,s,o,c,l,u)}sphereTrimesh(e,t,n,r,i,a,o,s,c,l,u){let d=Mn,f=Nn,p=Pn,m=Fn,h=In,g=Ln,_=Vn,v=jn,y=kn,b=Hn;A.pointToLocalFrame(r,a,n,h);let x=e.radius;_.lowerBound.set(h.x-x,h.y-x,h.z-x),_.upperBound.set(h.x+x,h.y+x,h.z+x),t.getTrianglesInAABB(_,b);let S=An,C=e.radius*e.radius;for(let i=0;i<b.length;i++)for(let d=0;d<3;d++)if(t.getVertex(t.indices[b[i]*3+d],S),S.vsub(h,y),y.lengthSquared()<=C){if(v.copy(S),A.pointToWorldFrame(r,a,v,S),S.vsub(n,y),u)return!0;let i=this.createContactEquation(o,s,e,t,c,l);i.ni.copy(y),i.ni.normalize(),i.ri.copy(i.ni),i.ri.scale(e.radius,i.ri),i.ri.vadd(n,i.ri),i.ri.vsub(o.position,i.ri),i.rj.copy(S),i.rj.vsub(s.position,i.rj),this.result.push(i),this.createFrictionEquationsFromContact(i,this.frictionResult)}for(let i=0;i<b.length;i++)for(let _=0;_<3;_++){t.getVertex(t.indices[b[i]*3+_],d),t.getVertex(t.indices[b[i]*3+(_+1)%3],f),f.vsub(d,p),h.vsub(f,g);let v=g.dot(p);h.vsub(d,g);let y=g.dot(p);if(y>0&&v<0&&(h.vsub(d,g),m.copy(p),m.normalize(),y=g.dot(m),m.scale(y,g),g.vadd(d,g),g.distanceTo(h)<e.radius)){if(u)return!0;let i=this.createContactEquation(o,s,e,t,c,l);g.vsub(h,i.ni),i.ni.normalize(),i.ni.scale(e.radius,i.ri),i.ri.vadd(n,i.ri),i.ri.vsub(o.position,i.ri),A.pointToWorldFrame(r,a,g,g),g.vsub(s.position,i.rj),A.vectorToWorldFrame(a,i.ni,i.ni),A.vectorToWorldFrame(a,i.ri,i.ri),this.result.push(i),this.createFrictionEquationsFromContact(i,this.frictionResult)}}let w=Rn,T=zn,E=Bn,D=On;for(let i=0,d=b.length;i!==d;i++){t.getTriangleVertices(b[i],w,T,E),t.getNormal(b[i],D),h.vsub(w,g);let d=g.dot(D);if(D.scale(d,g),h.vsub(g,g),d=g.distanceTo(h),F.pointInTriangle(g,w,T,E)&&d<e.radius){if(u)return!0;let i=this.createContactEquation(o,s,e,t,c,l);g.vsub(h,i.ni),i.ni.normalize(),i.ni.scale(e.radius,i.ri),i.ri.vadd(n,i.ri),i.ri.vsub(o.position,i.ri),A.pointToWorldFrame(r,a,g,g),g.vsub(s.position,i.rj),A.vectorToWorldFrame(a,i.ni,i.ni),A.vectorToWorldFrame(a,i.ri,i.ri),this.result.push(i),this.createFrictionEquationsFromContact(i,this.frictionResult)}}b.length=0}planeTrimesh(e,t,n,r,i,a,o,s,c,l,u){let d=new g,f=Tn;f.set(0,0,1),i.vmult(f,f);for(let i=0;i<t.vertices.length/3;i++){t.getVertex(i,d);let p=new g;p.copy(d),A.pointToWorldFrame(r,a,p,d);let m=En;if(d.vsub(n,m),f.dot(m)<=0){if(u)return!0;let n=this.createContactEquation(o,s,e,t,c,l);n.ni.copy(f);let r=Dn;f.scale(m.dot(f),r),d.vsub(r,r),n.ri.copy(r),n.ri.vsub(o.position,n.ri),n.rj.copy(d),n.rj.vsub(s.position,n.rj),this.result.push(n),this.createFrictionEquationsFromContact(n,this.frictionResult)}}}},H=new g,U=new g,W=new g,xn=new g,Sn=new g,Cn=new T,wn=new T,Tn=new g,En=new g,Dn=new g,On=new g,kn=new g;new g;var An=new g,jn=new g,Mn=new g,Nn=new g,Pn=new g,Fn=new g,In=new g,Ln=new g,Rn=new g,zn=new g,Bn=new g,Vn=new b,Hn=[],Un=new g,Wn=new g,Gn=new g,Kn=new g,qn=new g;function Jn(e,t,n){let r=null,i=e.length;for(let a=0;a!==i;a++){let o=e[a],s=Gn;e[(a+1)%i].vsub(o,s);let c=Kn;s.cross(t,c);let l=qn;n.vsub(o,l);let u=c.dot(l);if(r===null||u>0&&r===!0||u<=0&&r===!1)r===null&&(r=u>0);else return!1}return!0}var Yn=new g,Xn=new g,Zn=new g,Qn=new g,$n=[new g,new g,new g,new g,new g,new g],er=new g,tr=new g,nr=new g,rr=new g,ir=new g,ar=new g,or=new g,sr=new g,cr=new g,lr=new g,ur=new g,dr=new g,fr=new g,pr=new g;new g,new g;var mr=new g,hr=new g,gr=new g,_r=new g,vr=new g,yr=new g,br=new g,xr=new g,Sr=new g,Cr=new g,wr=new T,Tr=new g;new g;var Er=new g,Dr=new g,Or=new g,kr=new g,Ar=new g,jr=[0],Mr=new g,Nr=new g,Pr=class{constructor(){this.current=[],this.previous=[]}getKey(e,t){if(t<e){let n=t;t=e,e=n}return e<<16|t}set(e,t){let n=this.getKey(e,t),r=this.current,i=0;for(;n>r[i];)i++;if(n!==r[i]){for(let e=r.length-1;e>=i;e--)r[e+1]=r[e];r[i]=n}}tick(){let e=this.current;this.current=this.previous,this.previous=e,this.current.length=0}getDiff(e,t){let n=this.current,r=this.previous,i=n.length,a=r.length,o=0;for(let t=0;t<i;t++){let i=!1,a=n[t];for(;a>r[o];)o++;i=a===r[o],i||Fr(e,a)}o=0;for(let e=0;e<a;e++){let i=!1,a=r[e];for(;a>n[o];)o++;i=n[o]===a,i||Fr(t,a)}}};function Fr(e,t){e.push((t&4294901760)>>16,t&65535)}var Ir=(e,t)=>e<t?`${e}-${t}`:`${t}-${e}`,Lr=class{constructor(){this.data={keys:[]}}get(e,t){let n=Ir(e,t);return this.data[n]}set(e,t,n){let r=Ir(e,t);this.get(e,t)||this.data.keys.push(r),this.data[r]=n}delete(e,t){let n=Ir(e,t),r=this.data.keys.indexOf(n);r!==-1&&this.data.keys.splice(r,1),delete this.data[n]}reset(){let e=this.data,t=e.keys;for(;t.length>0;){let n=t.pop();delete e[n]}}},Rr=class extends w{constructor(e){e===void 0&&(e={}),super(),this.dt=-1,this.allowSleep=!!e.allowSleep,this.contacts=[],this.frictionEquations=[],this.quatNormalizeSkip=e.quatNormalizeSkip===void 0?0:e.quatNormalizeSkip,this.quatNormalizeFast=e.quatNormalizeFast!==void 0&&e.quatNormalizeFast,this.time=0,this.stepnumber=0,this.default_dt=1/60,this.nextId=0,this.gravity=new g,e.gravity&&this.gravity.copy(e.gravity),e.frictionGravity&&(this.frictionGravity=new g,this.frictionGravity.copy(e.frictionGravity)),this.broadphase=e.broadphase===void 0?new Ee:e.broadphase,this.bodies=[],this.hasActiveBodies=!1,this.solver=e.solver===void 0?new mn:e.solver,this.constraints=[],this.narrowphase=new bn(this),this.collisionMatrix=new C,this.collisionMatrixPrevious=new C,this.bodyOverlapKeeper=new Pr,this.shapeOverlapKeeper=new Pr,this.contactmaterials=[],this.contactMaterialTable=new Lr,this.defaultMaterial=new zt(`default`),this.defaultContactMaterial=new Rt(this.defaultMaterial,this.defaultMaterial,{friction:.3,restitution:0}),this.doProfiling=!1,this.profile={solve:0,makeContactConstraints:0,broadphase:0,integrate:0,narrowphase:0},this.accumulator=0,this.subsystems=[],this.addBodyEvent={type:`addBody`,body:null},this.removeBodyEvent={type:`removeBody`,body:null},this.idToBodyMap={},this.broadphase.setWorld(this)}getContactMaterial(e,t){return this.contactMaterialTable.get(e.id,t.id)}collisionMatrixTick(){let e=this.collisionMatrixPrevious;this.collisionMatrixPrevious=this.collisionMatrix,this.collisionMatrix=e,this.collisionMatrix.reset(),this.bodyOverlapKeeper.tick(),this.shapeOverlapKeeper.tick()}addConstraint(e){this.constraints.push(e)}removeConstraint(e){let t=this.constraints.indexOf(e);t!==-1&&this.constraints.splice(t,1)}rayTest(e,t,n){n instanceof De?this.raycastClosest(e,t,{skipBackfaces:!0},n):this.raycastAll(e,t,{skipBackfaces:!0},n)}raycastAll(e,t,n,r){return n===void 0&&(n={}),n.mode=F.ALL,n.from=e,n.to=t,n.callback=r,zr.intersectWorld(this,n)}raycastAny(e,t,n,r){return n===void 0&&(n={}),n.mode=F.ANY,n.from=e,n.to=t,n.result=r,zr.intersectWorld(this,n)}raycastClosest(e,t,n,r){return n===void 0&&(n={}),n.mode=F.CLOSEST,n.from=e,n.to=t,n.result=r,zr.intersectWorld(this,n)}addBody(e){this.bodies.includes(e)||(e.index=this.bodies.length,this.bodies.push(e),e.world=this,e.initPosition.copy(e.position),e.initVelocity.copy(e.velocity),e.timeLastSleepy=this.time,e instanceof P&&(e.initAngularVelocity.copy(e.angularVelocity),e.initQuaternion.copy(e.quaternion)),this.collisionMatrix.setNumObjects(this.bodies.length),this.addBodyEvent.body=e,this.idToBodyMap[e.id]=e,this.dispatchEvent(this.addBodyEvent))}removeBody(e){e.world=null;let t=this.bodies.length-1,n=this.bodies,r=n.indexOf(e);if(r!==-1){n.splice(r,1);for(let e=0;e!==n.length;e++)n[e].index=e;this.collisionMatrix.setNumObjects(t),this.removeBodyEvent.body=e,delete this.idToBodyMap[e.id],this.dispatchEvent(this.removeBodyEvent)}}getBodyById(e){return this.idToBodyMap[e]}getShapeById(e){let t=this.bodies;for(let n=0;n<t.length;n++){let r=t[n].shapes;for(let t=0;t<r.length;t++){let n=r[t];if(n.id===e)return n}}return null}addContactMaterial(e){this.contactmaterials.push(e),this.contactMaterialTable.set(e.materials[0].id,e.materials[1].id,e)}removeContactMaterial(e){let t=this.contactmaterials.indexOf(e);t!==-1&&(this.contactmaterials.splice(t,1),this.contactMaterialTable.delete(e.materials[0].id,e.materials[1].id))}fixedStep(e,t){e===void 0&&(e=1/60),t===void 0&&(t=10);let n=G.now()/1e3;if(!this.lastCallTime)this.step(e,void 0,t);else{let r=n-this.lastCallTime;this.step(e,r,t)}this.lastCallTime=n}step(e,t,n){if(n===void 0&&(n=10),t===void 0)this.internalStep(e),this.time+=e;else{this.accumulator+=t;let r=G.now(),i=0;for(;this.accumulator>=e&&i<n&&(this.internalStep(e),this.accumulator-=e,i++,!(G.now()-r>e*1e3)););this.accumulator%=e;let a=this.accumulator/e;for(let e=0;e!==this.bodies.length;e++){let t=this.bodies[e];t.previousPosition.lerp(t.position,a,t.interpolatedPosition),t.previousQuaternion.slerp(t.quaternion,a,t.interpolatedQuaternion),t.previousQuaternion.normalize()}this.time+=t}}internalStep(e){this.dt=e;let t=this.contacts,n=Gr,r=Kr,i=this.bodies.length,a=this.bodies,o=this.solver,s=this.gravity,c=this.doProfiling,l=this.profile,u=P.DYNAMIC,d=-1/0,f=this.constraints,p=Wr;s.length();let m=s.x,h=s.y,g=s.z,_=0;for(c&&(d=G.now()),_=0;_!==i;_++){let e=a[_];if(e.type===u){let t=e.force,n=e.mass;t.x+=n*m,t.y+=n*h,t.z+=n*g}}for(let e=0,t=this.subsystems.length;e!==t;e++)this.subsystems[e].update();c&&(d=G.now()),n.length=0,r.length=0,this.broadphase.collisionPairs(this,n,r),c&&(l.broadphase=G.now()-d);let v=f.length;for(_=0;_!==v;_++){let e=f[_];if(!e.collideConnected)for(let t=n.length-1;t>=0;--t)(e.bodyA===n[t]&&e.bodyB===r[t]||e.bodyB===n[t]&&e.bodyA===r[t])&&(n.splice(t,1),r.splice(t,1))}this.collisionMatrixTick(),c&&(d=G.now());let y=Ur,b=t.length;for(_=0;_!==b;_++)y.push(t[_]);t.length=0;let x=this.frictionEquations.length;for(_=0;_!==x;_++)p.push(this.frictionEquations[_]);for(this.frictionEquations.length=0,this.narrowphase.getContacts(n,r,this,t,y,this.frictionEquations,p),c&&(l.narrowphase=G.now()-d),c&&(d=G.now()),_=0;_<this.frictionEquations.length;_++)o.addEquation(this.frictionEquations[_]);let S=t.length;for(let e=0;e!==S;e++){let n=t[e],r=n.bi,i=n.bj,a=n.si,s=n.sj,c;c=r.material&&i.material&&this.getContactMaterial(r.material,i.material)||this.defaultContactMaterial,c.friction,r.material&&i.material&&(r.material.friction>=0&&i.material.friction>=0&&r.material.friction*i.material.friction,r.material.restitution>=0&&i.material.restitution>=0&&(n.restitution=r.material.restitution*i.material.restitution)),o.addEquation(n),r.allowSleep&&r.type===P.DYNAMIC&&r.sleepState===P.SLEEPING&&i.sleepState===P.AWAKE&&i.type!==P.STATIC&&i.velocity.lengthSquared()+i.angularVelocity.lengthSquared()>=i.sleepSpeedLimit**2*2&&(r.wakeUpAfterNarrowphase=!0),i.allowSleep&&i.type===P.DYNAMIC&&i.sleepState===P.SLEEPING&&r.sleepState===P.AWAKE&&r.type!==P.STATIC&&r.velocity.lengthSquared()+r.angularVelocity.lengthSquared()>=r.sleepSpeedLimit**2*2&&(i.wakeUpAfterNarrowphase=!0),this.collisionMatrix.set(r,i,!0),this.collisionMatrixPrevious.get(r,i)||(Hr.body=i,Hr.contact=n,r.dispatchEvent(Hr),Hr.body=r,i.dispatchEvent(Hr)),this.bodyOverlapKeeper.set(r.id,i.id),this.shapeOverlapKeeper.set(a.id,s.id)}for(this.emitContactEvents(),c&&(l.makeContactConstraints=G.now()-d,d=G.now()),_=0;_!==i;_++){let e=a[_];e.wakeUpAfterNarrowphase&&=(e.wakeUp(),!1)}for(v=f.length,_=0;_!==v;_++){let e=f[_];e.update();for(let t=0,n=e.equations.length;t!==n;t++){let n=e.equations[t];o.addEquation(n)}}o.solve(e,this),c&&(l.solve=G.now()-d),o.removeAllEquations();let C=Math.pow;for(_=0;_!==i;_++){let t=a[_];if(t.type&u){let n=C(1-t.linearDamping,e),r=t.velocity;r.scale(n,r);let i=t.angularVelocity;if(i){let n=C(1-t.angularDamping,e);i.scale(n,i)}}}this.dispatchEvent(Vr),c&&(d=G.now());let w=this.stepnumber%(this.quatNormalizeSkip+1)===0,T=this.quatNormalizeFast;for(_=0;_!==i;_++)a[_].integrate(e,w,T);this.clearForces(),this.broadphase.dirty=!0,c&&(l.integrate=G.now()-d),this.stepnumber+=1,this.dispatchEvent(Br);let E=!0;if(this.allowSleep)for(E=!1,_=0;_!==i;_++){let e=a[_];e.sleepTick(this.time),e.sleepState!==P.SLEEPING&&(E=!0)}this.hasActiveBodies=E}emitContactEvents(){let e=this.hasAnyEventListener(`beginContact`),t=this.hasAnyEventListener(`endContact`);if((e||t)&&this.bodyOverlapKeeper.getDiff(K,q),e){for(let e=0,t=K.length;e<t;e+=2)qr.bodyA=this.getBodyById(K[e]),qr.bodyB=this.getBodyById(K[e+1]),this.dispatchEvent(qr);qr.bodyA=qr.bodyB=null}if(t){for(let e=0,t=q.length;e<t;e+=2)Jr.bodyA=this.getBodyById(q[e]),Jr.bodyB=this.getBodyById(q[e+1]),this.dispatchEvent(Jr);Jr.bodyA=Jr.bodyB=null}K.length=q.length=0;let n=this.hasAnyEventListener(`beginShapeContact`),r=this.hasAnyEventListener(`endShapeContact`);if((n||r)&&this.shapeOverlapKeeper.getDiff(K,q),n){for(let e=0,t=K.length;e<t;e+=2){let t=this.getShapeById(K[e]),n=this.getShapeById(K[e+1]);J.shapeA=t,J.shapeB=n,t&&(J.bodyA=t.body),n&&(J.bodyB=n.body),this.dispatchEvent(J)}J.bodyA=J.bodyB=J.shapeA=J.shapeB=null}if(r){for(let e=0,t=q.length;e<t;e+=2){let t=this.getShapeById(q[e]),n=this.getShapeById(q[e+1]);Y.shapeA=t,Y.shapeB=n,t&&(Y.bodyA=t.body),n&&(Y.bodyB=n.body),this.dispatchEvent(Y)}Y.bodyA=Y.bodyB=Y.shapeA=Y.shapeB=null}}clearForces(){let e=this.bodies,t=e.length;for(let n=0;n!==t;n++){let t=e[n];t.force,t.torque,t.force.set(0,0,0),t.torque.set(0,0,0)}}};new b;var zr=new F,G=globalThis.performance||{};if(!G.now){let e=Date.now();G.timing&&G.timing.navigationStart&&(e=G.timing.navigationStart),G.now=()=>Date.now()-e}new g;var Br={type:`postStep`},Vr={type:`preStep`},Hr={type:P.COLLIDE_EVENT_NAME,body:null,contact:null},Ur=[],Wr=[],Gr=[],Kr=[],K=[],q=[],qr={type:`beginContact`,bodyA:null,bodyB:null},Jr={type:`endContact`,bodyA:null,bodyB:null},J={type:`beginShapeContact`,bodyA:null,bodyB:null,shapeA:null,shapeB:null},Y={type:`endShapeContact`,bodyA:null,bodyB:null,shapeA:null,shapeB:null};function Yr(e){let t=e.B.quaternion.conjugate().mult(e.A.quaternion).mult(e.rel0.conjugate()),n=t.x*e.axisB.x+t.y*e.axisB.y+t.z*e.axisB.z,r=2*Math.atan2(n,t.w);return r>Math.PI&&(r-=2*Math.PI),r<-Math.PI&&(r+=2*Math.PI),r}function Xr(e){let t=Yr(e),n=t>e.max?t-e.max:t<e.min?t-e.min:0;if(!n)return;let r=e.A.mass>0||e.B.mass<=0,i=r?e.A:e.B;if(i.mass<=0)return;let a=e.B.quaternion.vmult(e.axisB);a.normalize();let o=e.B.quaternion.vmult(e.pivotB).vadd(e.B.position),s=new T().setFromAxisAngle(a,r?-n:n),c=i.position.vsub(o);i.position.copy(s.vmult(c).vadd(o)),i.quaternion.copy(s.mult(i.quaternion)),i.quaternion.normalize();let l=e.A.angularVelocity.vsub(e.B.angularVelocity).dot(a);if(l*n>0){let e=a.scale(r?l:-l);i.angularVelocity.vsub(e,i.angularVelocity);let t=i.position.vsub(o);i.velocity.vsub(e.cross(t),i.velocity)}i.aabbNeedsUpdate=!0}var X=e=>[e.x,e.y,e.z],Zr=(e,t=[0,0,0])=>new g(...e??t),Qr=e=>e?new T(e[0],e[1],e[2],e[3]):new T,$r=.5,ei=.2,ti=.08,ni=.08,ri=.5,ii=48;function ai(e,t){let n=[],r=t?t.length:e.length/3,i=n=>{let r=(t?t[n]:n)*3;return new g(e[r],e[r+1],e[r+2])};for(let e=0;e+2<r;e+=3){let t=i(e),r=i(e+1),a=i(e+2),o=r.vsub(t).cross(a.vsub(t)),s=o.length();if(s<1e-9)continue;o.scale(ti/s,o);let c=t.vadd(r).vadd(a).scale(1/3).vsub(o.scale(.5)),l=new j({vertices:[t,r,a,t.vsub(o),r.vsub(o),a.vsub(o)].map(e=>e.vsub(c)),faces:[[0,1,2],[3,5,4],[0,3,4,1],[1,4,5,2],[2,5,3,0]]});n.push({shape:l,center:X(c)})}return n}function oi(e,t){if(e.length<=t)return e.length?[e]:[];let n=[1/0,1/0,1/0],r=[-1/0,-1/0,-1/0];for(let t of e)for(let e=0;e<3;e++)n[e]=Math.min(n[e],t.center[e]),r[e]=Math.max(r[e],t.center[e]);let i=[0,1,2].reduce((e,t)=>r[t]-n[t]>r[e]-n[e]?t:e,0),a=[...e].sort((e,t)=>e.center[i]-t.center[i]),o=a.length>>1;return[...oi(a.slice(0,o),t),...oi(a.slice(o),t)]}function si(e){let t=1/0;for(let n of e)if(n.type===`box`)t=Math.min(t,2*Math.min(...n.half));else if(n.type===`sphere`||n.type===`cylinder`||n.type===`capsule`)t=Math.min(t,2*n.radius);else if(n.type===`convex`){let e=[0,1,2].map(e=>Math.max(...n.vertices.map(t=>t[e]))-Math.min(...n.vertices.map(t=>t[e])));t=Math.min(t,...e)}return Number.isFinite(t)?Math.max(t,.01):1}var ci=class{world;recs=[];byId=new Map;byBody=new Map;order=0;materials=new Map;materialProps=new Map;namedMaterials;pairOverrides=new Map;layerBits=new Map;joints=[];contactsNow=new Map;contactsPrev=new Map;beginHandlers=[];endHandlers=[];substeps;time=0;preVel=new Map;stepDt=1/60;constructor(e={}){let t=Array.isArray(e)?{gravity:e}:e;this.world=new Rr({gravity:Zr(t.gravity,[0,-9.81,0]),allowSleep:t.sleep??!0}),this.world.broadphase.useBoundingBoxes=!0,this.world.solver.iterations=t.iterations??10,this.world.defaultContactMaterial.friction=$r,this.world.defaultContactMaterial.restitution=ei,this.substeps=Math.min(8,Math.max(1,Math.round(t.substeps??1))),this.namedMaterials=t.materials??{};for(let e of t.contacts??[])this.pairOverrides.set([e.a,e.b].sort().join(`|`),e);this.layerBits.set(`default`,1);for(let e of t.layers??[])this.layerBit(e)}get bodies(){return this.recs.map(e=>e.view)}get(e){return this.byId.get(e)?.view}layerBit(e){let t=this.layerBits.get(e);if(t===void 0){if(this.layerBits.size>=16)return 1;t=1<<this.layerBits.size,this.layerBits.set(e,t)}return t}layerMask(e){return e?e.reduce((e,t)=>e|this.layerBit(t),0):-1}material(e,t,n){let r=`${e??``}|${t}|${n}`,i=this.materials.get(r);if(i)return i;i=new zt({friction:-1,restitution:-1}),this.materialProps.set(i,{name:e,friction:t,restitution:n}),this.materials.set(r,i);for(let e of this.materials.values()){let t=this.materialProps.get(i),n=this.materialProps.get(e),r=t.name&&n.name?this.pairOverrides.get([t.name,n.name].sort().join(`|`)):void 0;this.world.addContactMaterial(new Rt(i,e,{friction:r?.friction??Math.sqrt(t.friction*n.friction),restitution:r?.restitution??Math.max(t.restitution,n.restitution)}))}return i}add(e){let t=this.byId.get(e.id),n=t?[...this.contactsNow].filter(([,e])=>e.a===t||e.b===t):[],r=t?[...this.contactsPrev].filter(([,e])=>e.a===t||e.b===t):[];this.remove(e.id);let i=e.material?this.namedMaterials[e.material]:void 0,a=!!e.character,o=a?0:e.friction??i?.friction??$r,s=a?0:e.restitution??i?.restitution??ei,c=e.type,l=c===`dynamic`,u=l?Math.max(e.mass??1,1e-4):0,d=this.material(e.material,o,s),f=this.layerBit(e.layer??`default`),p=this.layerMask(e.collidesWith),m=()=>new P({mass:u,type:l?P.DYNAMIC:c===`static`?P.STATIC:P.KINEMATIC,position:Zr(e.position),quaternion:Qr(e.quaternion),material:d,isTrigger:c===`trigger`,linearDamping:e.linearDamping??.01,angularDamping:e.angularDamping??(a?1:.01),fixedRotation:a||!!e.fixedRotation,allowSleep:l&&!a&&e.sleep!==!1,sleepSpeedLimit:.12,sleepTimeLimit:.6,collisionFilterGroup:f,collisionFilterMask:p}),h=m(),_=[];for(let t of e.shapes)this.addShape(h,t,_,m);e.velocity&&h.velocity.set(...e.velocity),e.angularVelocity&&l&&h.angularVelocity.set(...e.angularVelocity),c===`kinematic`&&e.velocity&&h.velocity.set(...e.velocity),h.updateMassProperties(),this.world.addBody(h);for(let e of _)this.world.addBody(e);let v={id:e.id,type:c,shapes:e.shapes,main:h,extra:_,order:this.order++,gravityScale:e.gravityScale??1,ccd:!!e.ccd,character:a,minExtent:si(e.shapes),userVel:c===`kinematic`||c===`trigger`?h.velocity.clone():new g,kinFrom:null,forces:[],torques:[],lastGrounded:-1/0,jumpedAt:-1/0,ground:null,onGround:!1,carry:null,view:null};v.view=this.viewOf(v),this.recs.push(v),this.byId.set(v.id,v),this.byBody.set(h,v);for(let e of _)this.byBody.set(e,v);if(t){v.order=t.order,v.lastGrounded=t.lastGrounded,v.jumpedAt=t.jumpedAt,v.ground=t.ground,v.onGround=t.onGround,t.character&&this.makeCharacter(v);let e=e=>({...e,a:e.a===t?v:e.a,b:e.b===t?v:e.b});for(let[t,r]of n)this.contactsNow.set(t,e(r));for(let[t,n]of r)this.contactsPrev.set(t,e(n));for(let e of this.recs)e.ground===t&&(e.ground=v)}return v.view}viewOf(e){let t=this;return{id:e.id,type:e.type,shapes:e.shapes,get position(){return X(e.main.position)},get quaternion(){let t=e.main.quaternion;return[t.x,t.y,t.z,t.w]},get velocity(){return X(e.main.velocity)},get angularVelocity(){return X(e.main.angularVelocity)},get mass(){return e.main.mass},get sleeping(){return e.main.sleepState===P.SLEEPING},get grounded(){return t.isGrounded(e)},character:e.character}}addShape(e,t,n,r){let i=Zr(t.offset),a=Qr(t.quat);switch(t.type){case`box`:e.addShape(new ae(new g(Math.max(t.half[0],.001),Math.max(t.half[1],.001),Math.max(t.half[2],.001))),i,a);break;case`sphere`:e.addShape(new Qt(Math.max(t.radius,.001)),i,a);break;case`cylinder`:e.addShape(new $t(t.radius,t.radius,Math.max(t.height,.001),t.segments??16),i,a);break;case`capsule`:{let n=Math.max(t.radius,.001),r=Math.max(t.height-2*n,0);r>.001&&e.addShape(new $t(n,n,r,12),i,a);for(let t of r>.001?[1,-1]:[0]){let o=a.vmult(new g(0,t*r/2,0));e.addShape(new Qt(n),i.vadd(o),a)}break}case`convex`:{let n=new j({vertices:t.vertices.map(e=>new g(...e)),faces:t.faces});e.addShape(n,i,a);break}case`heightfield`:e.addShape(new en(t.data,{elementSize:t.elementSize}),i,a);break;case`trimesh`:oi(ai(t.vertices,t.indices),ii).forEach((t,o)=>{let s=o===0?e:r();for(let e of t)s.addShape(e.shape,i.vadd(a.vmult(new g(...e.center))),a);o>0&&n.push(s)})}}remove(e){let t=this.byId.get(e);if(t){for(let t of this.joints.filter(t=>t.a===e||t.b===e))this.removeJoint(t.id);this.world.removeBody(t.main);for(let e of t.extra)this.world.removeBody(e);this.byId.delete(e),this.byBody.delete(t.main);for(let e of t.extra)this.byBody.delete(e);this.recs.splice(this.recs.indexOf(t),1);for(let[e,n]of this.contactsNow)(n.a===t||n.b===t)&&this.contactsNow.delete(e);for(let[e,n]of this.contactsPrev)(n.a===t||n.b===t)&&this.contactsPrev.delete(e);for(let e of this.recs)e.ground===t&&(e.ground=null)}}setPose(e,t,n){let r=this.byId.get(e);if(!r)return;let i=r.main;r.type!==`kinematic`&&r.type!==`trigger`||r.kinFrom||(r.kinFrom={p:i.position.clone(),q:i.quaternion.clone()}),r.character&&Math.hypot(t[0]-i.position.x,t[1]-i.position.y,t[2]-i.position.z)>ri&&(r.ground=null,r.onGround=!1,r.lastGrounded=-1/0);for(let e of[i,...r.extra])e.position.set(...t),n&&e.quaternion.set(...n),e.previousPosition.copy(e.position),e.interpolatedPosition.copy(e.position),e.previousQuaternion.copy(e.quaternion),e.interpolatedQuaternion.copy(e.quaternion),e.aabbNeedsUpdate=!0;r.type===`dynamic`?i.wakeUp():this.wakeAround(r)}wakeAround(e){let t=this.aabb(e);if(t)for(let e of this.recs){if(e.type!==`dynamic`||e.main.sleepState!==P.SLEEPING)continue;let n=this.aabb(e);n&&n.overlaps(t)&&this.wakeIsland(e)}}aabb(e){let t=[e.main,...e.extra].filter(e=>e.shapes.length);if(!t.length)return null;let n=new b;return t.forEach((e,t)=>{e.aabbNeedsUpdate&&e.updateAABB(),t===0?n.copy(e.aabb):n.extend(e.aabb)}),n.lowerBound.vsub(new g(.001,.001,.001),n.lowerBound),n.upperBound.vadd(new g(.001,.001,.001),n.upperBound),n}setVelocity(e,t){let n=this.byId.get(e);n&&n.type!==`static`&&(n.main.velocity.set(...t),n.type!==`dynamic`&&n.userVel.set(...t),n.main.wakeUp())}setAngularVelocity(e,t){let n=this.byId.get(e);n&&n.type!==`static`&&(n.main.angularVelocity.set(...t),n.main.wakeUp())}applyImpulse(e,t,n){let r=this.byId.get(e);if(!r||r.type!==`dynamic`)return;let i=n?new g(...n).vsub(r.main.position):new g;r.main.applyImpulse(new g(...t),i),r.main.wakeUp()}applyForce(e,t,n){let r=this.byId.get(e);r&&r.type===`dynamic`&&(r.forces.push({f:new g(...t),p:n?new g(...n):null}),r.main.wakeUp())}applyTorque(e,t){let n=this.byId.get(e);n&&n.type===`dynamic`&&(n.torques.push(new g(...t)),n.main.wakeUp())}wake(e){this.byId.get(e)?.main.wakeUp()}addJoint(e){let t=this.byId.get(e.a),n=e.b?this.byId.get(e.b):void 0;if(!t||e.b&&!n)return null;let r=e.id??`joint-${this.joints.length+1}`,i=t.main,a=new g(...e.pivot??X(n?n.main.position:i.position)),o,s;n?s=n.main:(o=new P({mass:0,type:P.STATIC,position:a.clone(),collisionFilterGroup:0,collisionFilterMask:0}),this.world.addBody(o),s=o);let c=(e,t)=>e.quaternion.conjugate().vmult(t.vsub(e.position)),l=(e,t)=>e.quaternion.conjugate().vmult(t),u=e.maxForce??1e6,d=e.collideConnected??!1,f,p,m;switch(e.type){case`hinge`:{let t=new g(...e.axis??[0,1,0]);t.length()<1e-9&&t.set(0,1,0),t.normalize();let r=new Mt(i,s,{pivotA:c(i,a),pivotB:c(s,a),axisA:l(i,t),axisB:l(s,t),maxForce:u,collideConnected:d});e.limits&&(m={A:i,B:s,pivotB:c(s,a),axisB:l(s,t),rel0:s.quaternion.conjugate().mult(i.quaternion),min:Math.min(e.limits[0],e.limits[1]),max:Math.max(e.limits[0],e.limits[1])}),e.motor&&(r.enableMotor(),r.setMotorSpeed(n?e.motor.speed:-e.motor.speed),r.setMotorMaxForce(e.motor.maxForce??100)),f=r;break}case`point`:f=new Tt(i,c(i,a),s,c(s,a),u),f.collideConnected=d;break;case`distance`:{let t=e.distance??i.position.distanceTo(s.position);f=new kt(i,s,t,u),f.collideConnected=d;break}case`lock`:f=new At(i,s,{maxForce:u}),f.collideConnected=d;break;case`spring`:{let t=e.pivot&&n?c(i,a):new g,r=e.pivot?c(s,a):new g,o=i.quaternion.vmult(t).vadd(i.position),l=s.quaternion.vmult(r).vadd(s.position);p=new Bt(i,s,{localAnchorA:t,localAnchorB:r,restLength:e.restLength??o.distanceTo(l),stiffness:e.stiffness??50,damping:e.damping??1});break}}return f&&this.world.addConstraint(f),this.joints.push({id:r,a:e.a,b:e.b,constraint:f,spring:p,anchor:o,limit:m}),r}removeJoint(e){let t=this.joints.findIndex(t=>t.id===e);if(t<0)return;let n=this.joints[t];n.constraint&&this.world.removeConstraint(n.constraint),n.anchor&&this.world.removeBody(n.anchor),this.joints.splice(t,1)}setMotorSpeed(e,t){let n=this.joints.find(t=>t.id===e);n?.constraint instanceof Mt&&(n.constraint.enableMotor(),n.constraint.setMotorSpeed(n.b?t:-t),this.byId.get(n.a)?.main.wakeUp())}hasJoints(e){return this.joints.some(t=>t.a===e||t.b===e)}jointIds(){return this.joints.map(e=>e.id)}onCollision(e){this.beginHandlers.push(e)}onCollisionEnd(e){this.endHandlers.push(e)}isGrounded(e){return e.character?e.onGround:!!this.contactGround(e)}contactGround(e){let t=null;for(let n of this.contactsNow.values()){if(n.trigger)continue;let r=n.a===e&&-n.info.normal[1]>.5?n.b:n.b===e&&n.info.normal[1]>.5?n.a:null;if(r){if(r.type===`kinematic`)return r;t??=r}}return t}updateGround(e){let t=e.main,n=this.contactGround(e),r=this.time-e.jumpedAt<.15,i=t.velocity.y>1,a=r&&i?null:n;if(!a&&!i&&!r){let n=this.aabb(e);if(n){let r=[t.position.x,t.position.y,t.position.z],i=t.position.y-n.lowerBound.y+ni,o=this.raycast(r,[0,-1,0],i,{triggers:!1,mask:t.collisionFilterMask,ignore:t=>t.id===e.id});if(o&&o.normal[1]>.5&&(a=this.byId.get(o.body.id)??null,a)){let[t,n]=e.order<a.order?[e,a]:[a,e],r=`${t.id}|${n.id}`,i=o.normal;if(!this.contactsNow.has(r)){let a=t===e?[-i[0],-i[1],-i[2]]:[i[0],i[1],i[2]];this.contactsNow.set(r,{a:t,b:n,trigger:!1,info:{point:o.point,normal:a,impulse:0,speed:0}})}}}}e.ground=a,e.onGround=!!a}groundOf(e){let t=this.byId.get(e);if(!t)return null;let n=t.character?t.ground:this.contactGround(t);return n?n.view:null}grounded(e){let t=this.byId.get(e);return!!t&&this.isGrounded(t)}moveCharacter(e,t,n={}){let r=this.byId.get(e);if(!r||r.type===`static`)return;this.makeCharacter(r);let i=r.main,[a,o]=t,s=Math.hypot(a,o);s>1&&(a/=s,o/=s);let c=n.speed??5,l=(n.acceleration??40)*(this.isGrounded(r)||r.type!==`dynamic`?1:n.airControl??.35)*this.stepDt,u=(e,t)=>e+Math.max(-l,Math.min(l,t-e)),d=u(i.velocity.x,a*c),f=u(i.velocity.z,o*c);i.velocity.set(d,i.velocity.y,f),r.type!==`dynamic`&&r.userVel.set(d,i.velocity.y,f),i.wakeUp()}jump(e,t=6){let n=this.byId.get(e);return!n||n.type!==`dynamic`||(this.makeCharacter(n),!(this.isGrounded(n)||this.time-n.lastGrounded<=.12)||this.time-n.jumpedAt<.2)?!1:(n.jumpedAt=this.time,n.lastGrounded=-1/0,n.onGround=!1,n.main.velocity.y=t,n.main.wakeUp(),!0)}makeCharacter(e){if(e.character)return;e.character=!0,e.view.character=!0;let t=e.main;t.fixedRotation=!0,t.angularVelocity.set(0,0,0),t.allowSleep=!1,t.updateMassProperties();let n=t.material?this.materialProps.get(t.material):void 0;t.material=this.material(n?.name,0,0)}step(e){this.stepDt=e;let t=this.substeps;for(let n of this.recs){if(!n.ccd||n.main.sleepState===P.SLEEPING)continue;let r=n.main.velocity.length()*e;t=Math.max(t,Math.min(8,Math.ceil(r/(.5*n.minExtent))))}let n=e/t;this.wakeAhead(e);let r=[];for(let t of this.recs){if(!t.kinFrom)continue;let n={p:t.main.position.clone(),q:t.main.quaternion.clone()},i=n.p.vsub(t.kinFrom.p);if(i.length()<2.5&&t.type===`kinematic`){for(let e of[t.main,...t.extra])e.position.copy(t.kinFrom.p),e.quaternion.copy(t.kinFrom.q);t.main.velocity.copy(i.scale(1/e).vadd(t.userVel));let a=n.q.mult(t.kinFrom.q.conjugate());a.w<0&&a.set(-a.x,-a.y,-a.z,-a.w);let o=Math.sqrt(Math.max(0,1-a.w*a.w)),s=2*Math.acos(Math.min(1,a.w));o>1e-6?t.main.angularVelocity.set(a.x/o*s/e,a.y/o*s/e,a.z/o*s/e):t.main.angularVelocity.set(0,0,0);for(let e of t.extra)e.velocity.copy(t.main.velocity),e.angularVelocity.copy(t.main.angularVelocity);r.push({rec:t,...n})}t.kinFrom=null}for(let e of this.recs){if(e.carry=null,!e.character||e.type!==`dynamic`||!e.onGround||e.ground?.type!==`kinematic`)continue;let t=e.ground.main,n=e.main.position.vsub(t.position),r=t.velocity.vadd(t.angularVelocity.cross(n));r.lengthSquared()<1e-10||(e.carry=r,e.main.velocity.vadd(r,e.main.velocity))}let i=new Map,a=this.world.gravity;for(let e=0;e<t;e++){for(let e of this.recs){if(e.type!==`dynamic`)continue;let t=e.main;e.gravityScale!==1&&t.force.vadd(a.scale(t.mass*(e.gravityScale-1)),t.force);for(let n of e.forces)t.applyForce(n.f,n.p?n.p.vsub(t.position):new g);for(let n of e.torques)t.torque.vadd(n,t.torque)}for(let e of this.joints)e.spring?.applyForce();this.preVel.clear();for(let e of this.recs)e.type!==`static`&&this.preVel.set(e,e.main.velocity.clone());this.world.step(n);for(let e of this.joints)e.limit&&Xr(e.limit);this.time+=n,this.collectContacts(i,n)}for(let e of this.recs)e.forces=[],e.torques=[];for(let e of this.recs)e.carry&&e.main.velocity.vsub(e.carry,e.main.velocity);for(let e of r)for(let t of[e.rec.main,...e.rec.extra])t.position.copy(e.p),t.quaternion.copy(e.q),t.velocity.copy(e.rec.userVel),t.angularVelocity.set(0,0,0),t.aabbNeedsUpdate=!0;for(let e of this.recs)if(e.type===`trigger`)for(let t of this.recs){if(t===e||t.type===`dynamic`||t.type===`trigger`||t.type===`static`||!this.filterOk(e,t))continue;let[n,r]=e.order<t.order?[e,t]:[t,e],a=`${n.id}|${r.id}`;if(i.has(a)||!this.overlapRecs(n,r))continue;let o=n.main.position,s=r.main.position,c=s.vsub(o),l=c.length()||1;i.set(a,{a:n,b:r,trigger:!0,info:{point:X(o.vadd(s).scale(.5)),normal:[c.x/l,c.y/l,c.z/l],impulse:0,speed:0}})}let o=e=>e.type===`static`||e.main.sleepState===P.SLEEPING;for(let[e,t]of this.contactsNow)!i.has(e)&&o(t.a)&&o(t.b)&&this.byId.has(t.a.id)&&this.byId.has(t.b.id)&&i.set(e,{...t,info:{...t.info,impulse:0,speed:0}});this.contactsPrev=this.contactsNow,this.contactsNow=i;for(let e of this.recs)e.character&&this.updateGround(e),this.isGrounded(e)&&(e.lastGrounded=this.time);for(let[e,t]of i)if(!this.contactsPrev.has(e))for(let e of this.beginHandlers)e(t.a.view,t.b.view,t.info);for(let[e,t]of this.contactsPrev)if(!i.has(e)&&this.byId.has(t.a.id)&&this.byId.has(t.b.id))for(let e of this.endHandlers)e(t.a.view,t.b.view)}wakeAhead(e){let t=null;for(let n of this.recs){if(n.type===`static`||n.type===`trigger`||n.main.sleepState===P.SLEEPING)continue;let r=n.main.velocity;if(r.length()<.5)continue;let i=this.aabb(n);if(!i)continue;if(!t){t=[];for(let e of this.recs){if(e.type!==`dynamic`||e.main.sleepState!==P.SLEEPING)continue;let n=this.aabb(e);n&&t.push({r:e,box:n})}}if(!t.length)return;let a=new g(Math.abs(r.x)*e*2+.05,Math.abs(r.y)*e*2+.05,Math.abs(r.z)*e*2+.05);i.lowerBound.vsub(a,i.lowerBound),i.upperBound.vadd(a,i.upperBound);for(let e of t)e.r.main.sleepState===P.SLEEPING&&e.box.overlaps(i)&&this.wakeIsland(e.r)}}wakeIsland(e){let t=[e];for(e.main.wakeUp();t.length;){let e=t.pop();for(let n of this.contactsNow.values()){let r=n.a===e?n.b:n.b===e?n.a:null;r&&r.type===`dynamic`&&r.main.sleepState===P.SLEEPING&&(r.main.wakeUp(),t.push(r))}}}filterOk(e,t){return(e.main.collisionFilterGroup&t.main.collisionFilterMask)!==0&&(t.main.collisionFilterGroup&e.main.collisionFilterMask)!==0}collectContacts(e,t){for(let n of this.world.contacts){let r=this.byBody.get(n.bi),i=this.byBody.get(n.bj);if(!r||!i||r===i)continue;let a=X(n.ni),o=X(n.bi.position.vadd(n.ri));i.order<r.order&&([r,i]=[i,r],a=[-a[0],-a[1],-a[2]],o=X(n.bj.position.vadd(n.rj)));let s=`${r.id}|${i.id}`,c=Math.abs(n.multiplier)*t,l=e.get(s);if(l){c>l.info.impulse&&(l.info.impulse=c);continue}let u=new g,d=(this.preVel.get(i)??u).vsub(this.preVel.get(r)??u),f=Math.max(0,-(d.x*a[0]+d.y*a[1]+d.z*a[2]));e.set(s,{a:r,b:i,trigger:r.type===`trigger`||i.type===`trigger`,info:{point:o,normal:a,impulse:c,speed:this.contactsPrev.has(s)?0:f}})}}contacts(){return[...this.contactsNow.values()].map(e=>({a:e.a.view,b:e.b.view,info:e.info,trigger:e.trigger}))}contactsOf(e){let t=[];for(let n of this.contactsNow.values())n.a.id===e?t.push(n.b.view):n.b.id===e&&t.push(n.a.view);return t}overlaps(e,t){let n=this.byId.get(e),r=this.byId.get(t);return!n||!r||n===r?!1:this.overlapRecs(n,r)}overlapRecs(e,t){let n=this.aabb(e),r=this.aabb(t);if(!n||!r||!n.overlaps(r))return!1;for(let n of[e.main,...e.extra])for(let e of[t.main,...t.extra])if(this.bodiesOverlap(n,e))return!0;return!1}bodiesOverlap(e,t){let n=this.world.narrowphase,r=new T,i=new T,a=new g,o=new g;for(let s=0;s<e.shapes.length;s++){e.quaternion.mult(e.shapeOrientations[s],r),e.quaternion.vmult(e.shapeOffsets[s],a),a.vadd(e.position,a);let c=e.shapes[s];for(let s=0;s<t.shapes.length;s++){t.quaternion.mult(t.shapeOrientations[s],i),t.quaternion.vmult(t.shapeOffsets[s],o),o.vadd(t.position,o);let l=t.shapes[s];if(a.distanceTo(o)>c.boundingSphereRadius+l.boundingSphereRadius+.001)continue;let u=n[c.type|l.type];if(u&&(c.type<l.type?u.call(n,c,l,a,o,r,i,e,t,c,l,!0):u.call(n,l,c,o,a,i,r,t,e,c,l,!0)))return!0}}return!1}raycast(e,t,n=1/0,r={}){let i=Math.hypot(t[0],t[1],t[2]);if(i<1e-12)return null;let a=Number.isFinite(n)?n:1e4,o=[t[0]/i,t[1]/i,t[2]/i],s=new g(...e),c=new g(e[0]+o[0]*a,e[1]+o[1]*a,e[2]+o[2]*a),l=null;return this.world.raycastAll(s,c,{skipBackfaces:!0,collisionFilterMask:r.mask??-1,checkCollisionResponse:!1},e=>{let t=this.byBody.get(e.body);!t||r.triggers===!1&&t.type===`trigger`||r.ignore?.(t.view)||l&&e.distance>=l.distance||(l={body:t.view,distance:e.distance,point:X(e.hitPointWorld),normal:X(e.hitNormalWorld)})}),l}},li={dynamic:4054148,static:5155071,kinematic:16755008,trigger:16732120,sleeping:10133670};function Z(e,t,n,r=0,i=24){for(let a=0;a<i;a++){let o=a/i*Math.PI*2,s=(a+1)/i*Math.PI*2,c=e=>n===`y`?[Math.cos(e)*t,r,Math.sin(e)*t]:n===`x`?[r,Math.cos(e)*t,Math.sin(e)*t]:[Math.cos(e)*t,Math.sin(e)*t,r];e.push(...c(o),...c(s))}}function ui(e){let t=[];switch(e.type){case`box`:{let[n,r,i]=e.half,a=[[-n,-r,-i],[n,-r,-i],[n,r,-i],[-n,r,-i],[-n,-r,i],[n,-r,i],[n,r,i],[-n,r,i]];for(let[e,n]of[[0,1],[1,2],[2,3],[3,0],[4,5],[5,6],[6,7],[7,4],[0,4],[1,5],[2,6],[3,7]])t.push(...a[e],...a[n]);break}case`sphere`:Z(t,e.radius,`x`),Z(t,e.radius,`y`),Z(t,e.radius,`z`);break;case`cylinder`:case`capsule`:{let n=e.radius,r=e.type===`capsule`?Math.max(e.height-2*n,0):e.height;Z(t,n,`y`,r/2),Z(t,n,`y`,-r/2);for(let e=0;e<8;e++){let i=e/8*Math.PI*2;t.push(Math.cos(i)*n,-r/2,Math.sin(i)*n,Math.cos(i)*n,r/2,Math.sin(i)*n)}if(e.type===`capsule`)for(let e of[1,-1])for(let i=0;i<12;i++){let a=i/12*(Math.PI/2),o=(i+1)/12*(Math.PI/2);for(let[i,s]of[[1,0],[0,1],[-1,0],[0,-1]])t.push(i*Math.cos(a)*n,e*(r/2+Math.sin(a)*n),s*Math.cos(a)*n,i*Math.cos(o)*n,e*(r/2+Math.sin(o)*n),s*Math.cos(o)*n)}break}case`convex`:for(let n of e.faces)for(let r=0;r<n.length;r++)t.push(...e.vertices[n[r]],...e.vertices[n[(r+1)%n.length]]);break;case`trimesh`:{let n=e.vertices,r=e.indices,i=r?r.length:n.length/3,a=e=>{let t=(r?r[e]:e)*3;return[n[t],n[t+1],n[t+2]]};for(let e=0;e+2<i;e+=3){let n=a(e),r=a(e+1),i=a(e+2);t.push(...n,...r,...r,...i,...i,...n)}break}case`heightfield`:{let n=e.data,r=e.elementSize,i=Math.max(1,Math.round(n.length/48));for(let e=0;e<n.length;e+=i)for(let a=0;a+i<n[e].length;a+=i)t.push(e*r,a*r,n[e][a],e*r,(a+i)*r,n[e][a+i]);for(let e=0;e<n[0].length;e+=i)for(let a=0;a+i<n.length;a+=i)t.push(a*r,e*r,n[a][e],(a+i)*r,e*r,n[a+i][e]);break}}if(e.offset||e.quat){let i=new s().compose(new r(...e.offset??[0,0,0]),new n(...e.quat??[0,0,0,1]),new r(1,1,1)),a=new r;for(let e=0;e<t.length;e+=3)a.set(t[e],t[e+1],t[e+2]).applyMatrix4(i),t[e]=a.x,t[e+1]=a.y,t[e+2]=a.z}return t}var di=class{group=new o;items=new Map;constructor(e){this.group.name=`agent3d-physics-debug`,this.group.renderOrder=999,e.add(this.group)}sync(e){let n=new Set;for(let r of e){n.add(r.id);let e=this.items.get(r.id);if(!e||e.body!==r){e&&this.drop(r.id);let n=r.shapes.flatMap(ui),i=new l;i.setAttribute(`position`,new t(n,3));let a=new u({color:li[r.type],depthTest:!1,depthWrite:!1,transparent:!0,opacity:.9,toneMapped:!1}),o=new f(i,a);o.renderOrder=999,o.frustumCulled=!1,this.group.add(o),e={lines:o,body:r},this.items.set(r.id,e)}let[i,a,o]=r.position,[s,c,d,p]=r.quaternion;e.lines.position.set(i,a,o),e.lines.quaternion.set(s,c,d,p),e.lines.material.color.setHex(r.sleeping?li.sleeping:li[r.type])}for(let e of[...this.items.keys()])n.has(e)||this.drop(e)}drop(e){let t=this.items.get(e);t&&(t.lines.removeFromParent(),t.lines.geometry.dispose(),t.lines.material.dispose(),this.items.delete(e))}dispose(){for(let e of[...this.items.keys()])this.drop(e);this.group.removeFromParent()}},fi=0,pi=1,mi=new r,hi=new d,gi=new p,_i=new r,vi=new a,yi=class{constructor(){this.tolerance=-1,this.faces=[],this.newFaces=[],this.assigned=new Si,this.unassigned=new Si,this.vertices=[]}setFromPoints(e){if(e.length>=4){this.makeEmpty();for(let t=0,n=e.length;t<n;t++)this.vertices.push(new xi(e[t]));this._compute()}return this}setFromObject(e){let t=[];return e.updateMatrixWorld(!0),e.traverse(function(e){let n=e.geometry;if(n!==void 0){let i=n.attributes.position;if(i!==void 0)for(let n=0,a=i.count;n<a;n++){let a=new r;a.fromBufferAttribute(i,n).applyMatrix4(e.matrixWorld),t.push(a)}}}),this.setFromPoints(t)}containsPoint(e){let t=this.faces;for(let n=0,r=t.length;n<r;n++)if(t[n].distanceToPoint(e)>this.tolerance)return!1;return!0}intersectRay(e,t){let n=this.faces,r=-1/0,i=1/0;for(let t=0,a=n.length;t<a;t++){let a=n[t],o=a.distanceToPoint(e.origin),s=a.normal.dot(e.direction);if(o>0&&s>=0)return null;let c=s===0?0:-o/s;if(!(c<=0)&&(s>0?i=Math.min(c,i):r=Math.max(c,r),r>i))return null}return r===-1/0?e.at(i,t):e.at(r,t),t}intersectsRay(e){return this.intersectRay(e,mi)!==null}makeEmpty(){return this.faces=[],this.vertices=[],this}_addVertexToFace(e,t){return e.face=t,t.outside===null?this.assigned.append(e):this.assigned.insertBefore(t.outside,e),t.outside=e,this}_removeVertexFromFace(e,t){return e===t.outside&&(t.outside=e.next!==null&&e.next.face===t?e.next:null),this.assigned.remove(e),this}_removeAllVerticesFromFace(e){if(e.outside!==null){let t=e.outside,n=e.outside;for(;n.next!==null&&n.next.face===e;)n=n.next;return this.assigned.removeSubList(t,n),t.prev=n.next=null,e.outside=null,t}}_deleteFaceVertices(e,t){let n=this._removeAllVerticesFromFace(e);if(n!==void 0){if(t===void 0)this.unassigned.appendChain(n);else{let e=n;do{let n=e.next;t.distanceToPoint(e.point)>this.tolerance?this._addVertexToFace(e,t):this.unassigned.append(e),e=n}while(e!==null)}}return this}_resolveUnassignedPoints(e){if(this.unassigned.isEmpty()===!1){let t=this.unassigned.first();do{let n=t.next,r=this.tolerance,i=null;for(let n=0;n<e.length;n++){let a=e[n];if(a.mark===fi){let e=a.distanceToPoint(t.point);if(e>r&&(r=e,i=a),r>1e3*this.tolerance)break}}i!==null&&this._addVertexToFace(t,i),t=n}while(t!==null)}return this}_computeExtremes(){let e=new r,t=new r,n=[],i=[];for(let e=0;e<3;e++)n[e]=i[e]=this.vertices[0];e.copy(this.vertices[0].point),t.copy(this.vertices[0].point);for(let r=0,a=this.vertices.length;r<a;r++){let a=this.vertices[r],o=a.point;for(let t=0;t<3;t++)o.getComponent(t)<e.getComponent(t)&&(e.setComponent(t,o.getComponent(t)),n[t]=a);for(let e=0;e<3;e++)o.getComponent(e)>t.getComponent(e)&&(t.setComponent(e,o.getComponent(e)),i[e]=a)}return this.tolerance=3*2**-52*(Math.max(Math.abs(e.x),Math.abs(t.x))+Math.max(Math.abs(e.y),Math.abs(t.y))+Math.max(Math.abs(e.z),Math.abs(t.z))),{min:n,max:i}}_computeInitialHull(){let e=this.vertices,t=this._computeExtremes(),n=t.min,r=t.max,i=0,a=0;for(let e=0;e<3;e++){let t=r[e].point.getComponent(e)-n[e].point.getComponent(e);t>i&&(i=t,a=e)}let o=n[a],s=r[a],c,l;i=0,hi.set(o.point,s.point);for(let t=0,n=this.vertices.length;t<n;t++){let n=e[t];if(n!==o&&n!==s){hi.closestPointToPoint(n.point,!0,_i);let e=_i.distanceToSquared(n.point);e>i&&(i=e,c=n)}}i=-1,gi.setFromCoplanarPoints(o.point,s.point,c.point);for(let t=0,n=this.vertices.length;t<n;t++){let n=e[t];if(n!==o&&n!==s&&n!==c){let e=Math.abs(gi.distanceToPoint(n.point));e>i&&(i=e,l=n)}}let u=[];if(gi.distanceToPoint(l.point)<0){u.push(Q.create(o,s,c),Q.create(l,s,o),Q.create(l,c,s),Q.create(l,o,c));for(let e=0;e<3;e++){let t=(e+1)%3;u[e+1].getEdge(2).setTwin(u[0].getEdge(t)),u[e+1].getEdge(1).setTwin(u[t+1].getEdge(0))}}else{u.push(Q.create(o,c,s),Q.create(l,o,s),Q.create(l,s,c),Q.create(l,c,o));for(let e=0;e<3;e++){let t=(e+1)%3;u[e+1].getEdge(2).setTwin(u[0].getEdge((3-e)%3)),u[e+1].getEdge(0).setTwin(u[t+1].getEdge(1))}}for(let e=0;e<4;e++)this.faces.push(u[e]);for(let t=0,n=e.length;t<n;t++){let n=e[t];if(n!==o&&n!==s&&n!==c&&n!==l){i=this.tolerance;let e=null;for(let t=0;t<4;t++){let r=this.faces[t].distanceToPoint(n.point);r>i&&(i=r,e=this.faces[t])}e!==null&&this._addVertexToFace(n,e)}}return this}_reindexFaces(){let e=[];for(let t=0;t<this.faces.length;t++){let n=this.faces[t];n.mark===fi&&e.push(n)}return this.faces=e,this}_nextVertexToAdd(){if(this.assigned.isEmpty()===!1){let e,t=0,n=this.assigned.first().face,r=n.outside;do{let i=n.distanceToPoint(r.point);i>t&&(t=i,e=r),r=r.next}while(r!==null&&r.face===n);return e}}_computeHorizon(e,t,n,r){this._deleteFaceVertices(n),n.mark=pi;let i;i=t===null?t=n.getEdge(0):t.next;do{let t=i.twin,n=t.face;n.mark===fi&&(n.distanceToPoint(e)>this.tolerance?this._computeHorizon(e,t,n,r):r.push(i)),i=i.next}while(i!==t);return this}_addAdjoiningFace(e,t){let n=Q.create(e,t.tail(),t.head());return this.faces.push(n),n.getEdge(-1).setTwin(t.twin),n.getEdge(0)}_addNewFaces(e,t){this.newFaces=[];let n=null,r=null;for(let i=0;i<t.length;i++){let a=t[i],o=this._addAdjoiningFace(e,a);n===null?n=o:o.next.setTwin(r),this.newFaces.push(o.face),r=o}return n.next.setTwin(r),this}_addVertexToHull(e){let t=[];return this.unassigned.clear(),this._removeVertexFromFace(e,e.face),this._computeHorizon(e.point,null,e.face,t),this._addNewFaces(e,t),this._resolveUnassignedPoints(this.newFaces),this}_cleanup(){return this.assigned.clear(),this.unassigned.clear(),this.newFaces=[],this}_compute(){let e;for(this._computeInitialHull();(e=this._nextVertexToAdd())!==void 0;)this._addVertexToHull(e);return this._reindexFaces(),this._cleanup(),this}},Q=class e{constructor(){this.normal=new r,this.midpoint=new r,this.area=0,this.constant=0,this.outside=null,this.mark=fi,this.edge=null}static create(t,n,r){let i=new e,a=new bi(t,i),o=new bi(n,i),s=new bi(r,i);return a.next=s.prev=o,o.next=a.prev=s,s.next=o.prev=a,i.edge=a,i.compute()}getEdge(e){let t=this.edge;for(;e>0;)t=t.next,e--;for(;e<0;)t=t.prev,e++;return t}compute(){let e=this.edge.tail(),t=this.edge.head(),n=this.edge.next.head();return vi.set(e.point,t.point,n.point),vi.getNormal(this.normal),vi.getMidpoint(this.midpoint),this.area=vi.getArea(),this.constant=this.normal.dot(this.midpoint),this}distanceToPoint(e){return this.normal.dot(e)-this.constant}},bi=class{constructor(e,t){this.vertex=e,this.prev=null,this.next=null,this.twin=null,this.face=t}head(){return this.vertex}tail(){return this.prev?this.prev.vertex:null}length(){let e=this.head(),t=this.tail();return t===null?-1:t.point.distanceTo(e.point)}lengthSquared(){let e=this.head(),t=this.tail();return t===null?-1:t.point.distanceToSquared(e.point)}setTwin(e){return this.twin=e,e.twin=this,this}},xi=class{constructor(e){this.point=e,this.prev=null,this.next=null,this.face=null}},Si=class{constructor(){this.head=null,this.tail=null}first(){return this.head}last(){return this.tail}clear(){return this.head=this.tail=null,this}insertBefore(e,t){return t.prev=e.prev,t.next=e,t.prev===null?this.head=t:t.prev.next=t,e.prev=t,this}insertAfter(e,t){return t.prev=e,t.next=e.next,t.next===null?this.tail=t:t.next.prev=t,e.next=t,this}append(e){return this.head===null?this.head=e:this.tail.next=e,e.prev=this.tail,e.next=null,this.tail=e,this}appendChain(e){for(this.head===null?this.head=e:this.tail.next=e,e.prev=this.tail;e.next!==null;)e=e.next;return this.tail=e,this}remove(e){return e.prev===null?this.head=e.next:e.prev.next=e.next,e.next===null?this.tail=e.prev:e.next.prev=e.prev,this}removeSubList(e,t){return e.prev===null?this.head=t.next:e.prev.next=t.next,t.next===null?this.tail=e.prev:t.next.prev=e.prev,this}isEmpty(){return this.head===null}},Ci=12e3,wi=600;function Ti(e){e.updateWorldMatrix(!0,!0);let t=new r,i=new n,a=new r;return e.matrixWorld.decompose(t,i,a),{inv:new s().compose(t,i,new r(1,1,1)).invert(),scale:a}}function Ei(e,t){let n=e.userData?.a3dId;return!!n&&n!==t&&!n.includes(`/`)}function $(e,t=`own`,n){let r=[],i=e.userData?.a3dId,a=o=>{if(o!==e&&(t===`own`&&Ei(o,i)||n?.(o)))return;let s=o;s.isMesh&&s.geometry?.getAttribute(`position`)&&!o.userData.noCollide&&r.push(s);for(let e of o.children)a(e)};return a(e),r}function Di(e){let t=$(e.object);return t.length?t:$(e.object,`tree`,Oi(e.objectDef))}function Oi(e){let t=new Set,n=e=>{for(let r of e??[])r.body&&t.add(r.id),n(r.children)};return n(e?.children),e=>t.has(e.userData?.a3dId)}function ki(e,t){let n=new c,r=new c,i=new s;for(let a of e){let e=a;e.isInstancedMesh?(e.boundingBox||e.computeBoundingBox(),e.boundingBox&&r.copy(e.boundingBox)):(a.geometry.boundingBox||a.geometry.computeBoundingBox(),r.copy(a.geometry.boundingBox)),r.isEmpty()||n.union(r.applyMatrix4(i.multiplyMatrices(t,a.matrixWorld)))}return n}function Ai(e,t){return ki(Di({object:e,objectDef:t}),Ti(e).inv)}function ji(e,t){let{inv:n}=Ti(e),i=[ki($(e),n)],a=Oi(t),o=e.userData?.a3dId,s=e=>{for(let t of e.children)a(t)||(Ei(t,o)?i.push(ki($(t,`tree`,a),n)):s(t))};return s(e),i.filter(e=>!e.isEmpty()).map(e=>({size:e.getSize(new r),center:e.getCenter(new r)}))}function Mi(e,t){let{inv:n}=Ti(e.object),i=new s,a=new r,o=[],c=0;for(let r of Di(e)){if(r.isInstancedMesh)continue;let e=r.geometry,s=e.getAttribute(`position`),l=e.getIndex(),u=l?l.count:s.count;if(c+=u/3,c>t)return null;i.multiplyMatrices(n,r.matrixWorld);let d=new Float32Array(u*3);for(let e=0;e<u;e++)a.fromBufferAttribute(s,l?l.getX(e):e).applyMatrix4(i),d[e*3]=a.x,d[e*3+1]=a.y,d[e*3+2]=a.z;o.push(d)}let l=new Float32Array(o.reduce((e,t)=>e+t.length,0)),u=0;for(let e of o)l.set(e,u),u+=e.length;return l}function Ni(e,t){let{inv:n}=Ti(e.object),i=new s,a=[],o=Di(e),l=o.reduce((e,t)=>e+t.geometry.getAttribute(`position`).count,0),u=Math.max(1,Math.ceil(l/t));for(let e of o){let t=e.geometry.getAttribute(`position`);i.multiplyMatrices(n,e.matrixWorld);for(let e=0;e<t.count;e+=u)a.push(new r().fromBufferAttribute(t,e).applyMatrix4(i))}let d=new c;for(let e of o){let t=e.geometry.getAttribute(`position`);i.multiplyMatrices(n,e.matrixWorld);let o=[];for(let e=0;e<t.count;e++){let n=new r().fromBufferAttribute(t,e).applyMatrix4(i);for(let e=0;e<3;e++)(!o[e*2]||n.getComponent(e)<o[e*2].getComponent(e))&&(o[e*2]=n),(!o[e*2+1]||n.getComponent(e)>o[e*2+1].getComponent(e))&&(o[e*2+1]=n)}for(let e of o)e&&(a.push(e),d.expandByPoint(e))}return a}function Pi(e,t){if(e.length<4)return null;let n;try{n=new yi().setFromPoints(e)}catch{return null}let r=new Map,i=[],a=[];for(let e of n.faces){let n=[],o=e.edge;do{let e=o.head().point,a=r.get(e);a===void 0&&(a=i.length,r.set(e,a),i.push([e.x-t.x,e.y-t.y,e.z-t.z])),n.push(a),o=o.next}while(o!==e.edge);n.length>=3&&a.push(n)}return a.length>=4?{vertices:i,faces:a}:null}var Fi=t=>{if(!t)return;let r=new n().setFromEuler(new i(t[0]*e,t[1]*e,t[2]*e));return[r.x,r.y,r.z,r.w]};function Ii(e,t){let n=e.offset,i=Fi(e.rotation),a=e.size?new r(...e.size):t;switch(e.shape){case`sphere`:return{type:`sphere`,radius:e.radius??Math.max(a.x,a.y,a.z)/2,offset:n,quat:i};case`cylinder`:return{type:`cylinder`,radius:e.radius??Math.max(a.x,a.z)/2,height:e.height??a.y,offset:n,quat:i};case`capsule`:return{type:`capsule`,radius:e.radius??Math.max(a.x,a.z)/2,height:e.height??a.y,offset:n,quat:i};default:return{type:`box`,half:[a.x/2,a.y/2,a.z/2],offset:n,quat:i}}}function Li(e){let t=e.objectDef,n=e.def.type===`static`||e.def.type===`kinematic`;if(e.built?.heightAt&&n&&typeof e.object.userData.size==`number`)return`heightfield`;if(t?.kind===`mesh`){let e=t.geometry?.type;return e===`sphere`||e===`icosahedron`||e===`dodecahedron`?`sphere`:e===`capsule`?`capsule`:e===`cylinder`?t.geometry.radiusTop===void 0||t.geometry.radiusTop===t.geometry.radiusBottom?`cylinder`:`convex`:e===`box`||e===`roundedBox`||e===`plane`?`box`:n?`trimesh`:`convex`}return t?.kind===`model`||t?.kind===`svg`?n?`trimesh`:`convex`:t?.kind===`generator`&&n?`trimesh`:`box`}function Ri(e){let t=e.def,n=Ai(e.object,e.objectDef),i=n.isEmpty(),a=i?new r(1,1,1):n.getSize(new r),o=i?new r:n.getCenter(new r);t.size&&a.set(t.size[0],t.size[1],t.size[2]);let s=t.offset?new r(...t.offset):o.clone(),c=t.shape??`box`;c===`auto`&&(c=Li(e));let l=()=>({type:`box`,half:[a.x/2,a.y/2,a.z/2]});switch(c){case`sphere`:return{shapes:[{type:`sphere`,radius:t.radius??Math.max(a.x,a.y,a.z)/2}],offset:s};case`cylinder`:return{shapes:[{type:`cylinder`,radius:t.radius??Math.max(a.x,a.z)/2,height:t.height??a.y}],offset:s};case`capsule`:return{shapes:[{type:`capsule`,radius:t.radius??Math.max(a.x,a.z)/2,height:t.height??a.y}],offset:s};case`compound`:{let n=t.offset?new r(...t.offset):new r;if(!t.shapes?.length){let t=ji(e.object,e.objectDef);if(t.length)return{shapes:t.map(e=>({type:`box`,half:[e.size.x/2,e.size.y/2,e.size.z/2],offset:[e.center.x-n.x,e.center.y-n.y,e.center.z-n.z]})),offset:n}}let i=(t.shapes??[]).map(e=>{let t=Ii(e,a),r=e.offset??[0,0,0];return{...t,offset:[r[0]-n.x,r[1]-n.y,r[2]-n.z]}});return i.length?{shapes:i,offset:n}:{shapes:[l()],offset:s}}case`convex`:{let t=Pi(Ni(e,wi),s);return t?{shapes:[{type:`convex`,...t}],offset:s}:{shapes:[l()],offset:s,note:`convex hull failed (too few points): using the bounding box`}}case`heightfield`:return zi(e)||u(e,`heightfield needs a terrain generator (heightAt): using the triangles`);case`trimesh`:return u(e);default:return{shapes:[l()],offset:s}}function u(e,t){let n=Mi(e,Ci);if(!n||n.length===0)return{shapes:[l()],offset:s,note:n?`no triangles: using the bounding box`:`more than ${Ci} triangles: using the bounding box (use a simpler collision mesh, "compound" or "box")`};for(let e=0;e<n.length;e+=3)n[e]-=s.x,n[e+1]-=s.y,n[e+2]-=s.z;return{shapes:[{type:`trimesh`,vertices:n}],offset:s,note:t}}}function zi(e){let t=e.built?.heightAt?.bind(e.built),n=e.object.userData.size;if(!t||typeof n!=`number`||!(n>0))return null;let{scale:i}=Ti(e.object),a=0;for(let t of $(e.object))if(t.userData.materialTarget||t.name===`terrain-ground`){let e=t.geometry;a=(e.getIndex()?.count??e.getAttribute(`position`).count)/3;break}let o=Math.min(160,Math.max(8,a?Math.round(Math.sqrt(a/2)):64)),s=o+1,c=n/o,l=[];for(let e=0;e<s;e++){let r=[];for(let a=0;a<s;a++)r.push(t(-n/2+e*c,n/2-a*c)*i.y);l.push(r)}let u=i.x,d=new r;return{shapes:[{type:`heightfield`,data:l,elementSize:c*u,offset:[-n/2*u,0,n/2*u],quat:[-Math.SQRT1_2,0,0,Math.SQRT1_2]}],offset:d}}function Bi(e,t){e.updateWorldMatrix(!0,!1);let i=new r,a=new n,o=new r;return e.matrixWorld.decompose(i,a,o),i.add(t.clone().applyQuaternion(a)),{position:[i.x,i.y,i.z],quaternion:[a.x,a.y,a.z,a.w]}}function Vi(t,n){let{shapes:r,offset:i,note:a}=Ri(n),o=Bi(n.object,i),s=n.def,c=s.angularVelocity;return{init:{id:t,type:s.type,shapes:r,position:o.position,quaternion:o.quaternion,velocity:s.velocity,angularVelocity:c?[c[0]*e,c[1]*e,c[2]*e]:void 0,mass:s.mass,friction:s.friction,restitution:s.restitution,material:s.material,linearDamping:s.linearDamping,angularDamping:s.angularDamping,gravityScale:s.gravityScale,fixedRotation:s.fixedRotation,character:s.character,sleep:s.sleep,ccd:s.ccd,layer:s.layer,collidesWith:s.collidesWith},offset:i,note:a}}var Hi=class{def;issue;engine;offsets=new Map;scales=new Map;scaleWarned=new Set;debug=null;world;constructor(e,t,n){this.def=e,this.issue=n,this.world=t;let r=e?.gravity??[0,-9.81,0];this.engine=new ci({gravity:[r[0],r[1],r[2]],iterations:e?.iterations,substeps:e?.substeps,sleep:e?.sleep,materials:e?.materials,contacts:e?.contacts,layers:e?.layers}),e?.debug&&(this.debug=new di(t.scene))}attachAll(){this.world.scene.updateMatrixWorld(!0);for(let e of this.world.entries.values())this.addEntry(e),this.addGeneratorBodies(e);(this.def?.joints??[]).forEach((t,n)=>{this.engine.addJoint({id:t.id??`joint-${n+1}`,type:t.type,a:t.a,b:t.b,pivot:t.pivot,axis:t.axis,distance:t.distance,restLength:t.restLength,stiffness:t.stiffness,damping:t.damping,motor:t.motor?{speed:t.motor.speed*e,maxForce:t.motor.maxForce}:void 0,limits:t.limits?[t.limits[0]*e,t.limits[1]*e]:void 0,maxForce:t.maxForce,collideConnected:t.collideConnected})||this.issue({code:`physics`,message:`physics.joints[${n}]: "${t.a}"${t.b?` or "${t.b}"`:``} has no body.`,path:`physics.joints[${n}]`})}),this.debug?.sync(this.engine.bodies)}addEntry(e,t){let n=t??e.def.body;if(!n||e.kind===`light`)return!1;let{init:i,offset:a,note:o}=Vi(e.id,{object:e.object,def:n,objectDef:e.def,built:e.built});return o&&this.issue({code:`physics`,message:`objects#${e.id}.body: ${o}.`,path:`objects#${e.id}.body`}),this.offsets.set(e.id,a),this.scales.set(e.id,e.object.getWorldScale(new r)),this.engine.add(i),!0}addGeneratorBodies(e){if(e.kind===`light`||e.def.body)return;let t=e.object.userData.bodies;if(!Array.isArray(t)||!t.length)return;e.object.updateWorldMatrix(!0,!1);let i=new r,a=new n,o=new r;e.object.matrixWorld.decompose(i,a,o);let s=t.map(e=>({type:`box`,half:[Math.abs(e.max[0]-e.min[0])*o.x/2,Math.abs(e.max[1]-e.min[1])*o.y/2,Math.abs(e.max[2]-e.min[2])*o.z/2],offset:[(e.min[0]+e.max[0])/2*o.x,(e.min[1]+e.max[1])/2*o.y,(e.min[2]+e.max[2])/2*o.z]}));this.offsets.set(e.id,new r),this.engine.add({id:e.id,type:`static`,shapes:s,position:[i.x,i.y,i.z],quaternion:[a.x,a.y,a.z,a.w]})}has(e){return!!this.engine.get(e)}moved(e){let t=this.world.entries.get(e),n=this.offsets.get(e),i=this.engine.get(e);if(!t||!n||!i)return;let a=this.scales.get(e);if(a&&t.def.body){let n=t.object.getWorldScale(new r);if(Math.abs(n.x-a.x)>1e-4*Math.abs(a.x)+1e-6||Math.abs(n.y-a.y)>1e-4*Math.abs(a.y)+1e-6||Math.abs(n.z-a.z)>1e-4*Math.abs(a.z)+1e-6){if(this.engine.hasJoints(e))this.scaleWarned.has(e)||(this.scaleWarned.add(e),this.issue({code:`physics`,message:`objects#${e}: its scale changed but its collider keeps the start size (the body is held by a joint).`,path:`objects#${e}.body`}));else{let n=[...i.velocity],r=[...i.angularVelocity];t.object.updateWorldMatrix(!0,!1),this.addEntry(t),this.engine.setVelocity(e,n),this.engine.setAngularVelocity(e,r),this.debug?.sync(this.engine.bodies);return}}}let o=Bi(t.object,n);this.engine.setPose(e,o.position,o.quaternion)}remove(e){this.engine.remove(e),this.offsets.delete(e),this.scales.delete(e)}step(e){this.engine.step(e),this.syncObjects(),this.debug?.sync(this.engine.bodies)}syncObjects(){let e=new r,t=new n,i=new n;for(let n of this.engine.bodies){if(n.type!==`dynamic`&&n.type!==`kinematic`||n.sleeping)continue;if(n.type===`kinematic`){let e=n.velocity,t=n.angularVelocity;if(!e[0]&&!e[1]&&!e[2]&&!t[0]&&!t[1]&&!t[2])continue}let a=this.world.entries.get(n.id);if(!a)continue;let o=a.object;t.set(...n.quaternion),e.set(...n.position).sub((this.offsets.get(n.id)??new r).clone().applyQuaternion(t)),o.parent&&(o.parent.updateWorldMatrix(!0,!1),o.parent.worldToLocal(e),o.parent.getWorldQuaternion(i),t.premultiply(i.invert())),o.position.copy(e),o.quaternion.copy(t),a.animated&&(a.rest.position.copy(e),a.rest.quaternion.copy(t))}}dispose(){this.debug?.dispose(),this.debug=null}};export{Hi as PhysicsBridge,ci as PhysicsWorld};
|