babylon-box3d 0.2.0 → 0.3.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 +12 -0
- package/dist/box3dPlugin.d.ts +12 -0
- package/dist/box3dPlugin.d.ts.map +1 -1
- package/dist/box3dPlugin.js +26 -2
- package/dist/box3dPlugin.js.map +1 -1
- package/package.json +1 -1
- package/umd/babylon.box3d.js +37 -3
- package/umd/babylon.box3d.js.map +2 -2
- package/umd/babylon.box3d.min.js +2 -2
- package/umd/babylon.box3d.min.js.map +3 -3
package/umd/babylon.box3d.min.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
/* babylon-box3d 0.
|
|
2
|
-
"use strict";var BABYLONBOX3D=(()=>{var Tt=Object.create;var G=Object.defineProperty;var vt=Object.getOwnPropertyDescriptor;var It=Object.getOwnPropertyNames;var Mt=Object.getPrototypeOf,Et=Object.prototype.hasOwnProperty;var B=(m,t)=>()=>(t||m((t={exports:{}}).exports,t),t.exports),Lt=(m,t)=>{for(var e in t)G(m,e,{get:t[e],enumerable:!0})},$=(m,t,e,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of It(t))!Et.call(m,i)&&i!==e&&G(m,i,{get:()=>t[i],enumerable:!(s=vt(t,i))||s.enumerable});return m};var A=(m,t,e)=>(e=m!=null?Tt(Mt(m)):{},$(t||!m||!m.__esModule?G(e,"default",{value:m,enumerable:!0}):e,m)),Ot=m=>$(G({},"__esModule",{value:!0}),m);var et=B((Jt,tt)=>{if(typeof BABYLON>"u")throw new Error("babylon.box3d.js needs babylon.js loaded first");tt.exports=BABYLON});var it=B((Vt,st)=>{if(typeof BABYLON>"u")throw new Error("babylon.box3d.js needs babylon.js loaded first");st.exports=BABYLON});var nt=B((kt,ot)=>{if(typeof BABYLON>"u")throw new Error("babylon.box3d.js needs babylon.js loaded first");ot.exports=BABYLON});var rt=B((Ut,at)=>{if(typeof BABYLON>"u")throw new Error("babylon.box3d.js needs babylon.js loaded first");at.exports=BABYLON});var ct=B((Wt,lt)=>{if(typeof BABYLON>"u")throw new Error("babylon.box3d.js needs babylon.js loaded first");lt.exports=BABYLON});var ht=B((Ht,_t)=>{if(typeof BABYLON>"u")throw new Error("babylon.box3d.js needs babylon.js loaded first");_t.exports=BABYLON});var bt=B((Yt,pt)=>{if(typeof BABYLON>"u")throw new Error("babylon.box3d.js needs babylon.js loaded first");pt.exports=BABYLON});var ut=B((jt,dt)=>{if(typeof BABYLON>"u")throw new Error("babylon.box3d.js needs babylon.js loaded first");dt.exports=BABYLON});var xt=B((Qt,yt)=>{if(typeof BABYLON>"u")throw new Error("babylon.box3d.js needs babylon.js loaded first");yt.exports=BABYLON});var zt={};Lt(zt,{BOX3D_HEIGHT_FIELD_HOLE:()=>gt,Box3D:()=>St,Box3DPlugin:()=>V,Box3DWheelJoint:()=>L});var p=A(et(),1),_=A(it(),1),Dt=A(nt(),1),I=A(rt(),1),U=A(ct(),1),W=A(ht(),1),H=A(bt(),1),q=A(ut(),1),M=A(xt(),1),gt=255,Y=class{constructor(t){this.slot=t;this.userMassProps={};this.angularLocks=0;this.eventMask=0;this.motionType=_.PhysicsMotionType.STATIC;this.activation=_.PhysicsActivationControl.SIMULATION_CONTROLLED}},j=class{constructor(t,e){this.slot=t;this.type=e;this.material={};this.users=new Set;this.children=[];this.parents=new Set}},Q=class{constructor(){this.joints=[];this.pairs=[];this.type=_.PhysicsConstraintType.LOCK;this.jointType=0;this.enabled=!0;this.collisions=!1;this.frames=new Float32Array(14);this.length=0;this.axes=new Map;this.plan=null}};var L=class{constructor(t,e){this._plugin=t;this._slot=e}setSpinSpeed(t){this._plugin._wheelCall("_bx_WheelJoint_SetSpinMotorSpeed",this._slot,t)}setMaxSpinTorque(t){this._plugin._wheelCall("_bx_WheelJoint_SetMaxSpinTorque",this._slot,t)}enableSpinMotor(t){this._plugin._wheelCall("_bx_WheelJoint_EnableSpinMotor",this._slot,t?1:0)}setSteeringAngle(t){this._plugin._wheelCall("_bx_WheelJoint_SetTargetSteeringAngle",this._slot,t)}setSuspension(t,e){this._plugin._wheelCall("_bx_WheelJoint_SetSuspension",this._slot,t,e)}dispose(){this._plugin._destroyExtraJoint(this._slot),this._slot=0}};var wt=1e5;function Rt(m,t,e){let s=[[m[0],m[3],m[4]],[m[3],m[1],m[5]],[m[4],m[5],m[2]]];e.set(0,0,0,1),t.set(m[0],m[1],m[2]);let i=[0,0,0,1];for(let n=0;n<24;n++){let[a,o,r,l]=i,h=[[1-2*(o*o+r*r),2*(a*o-r*l),2*(a*r+o*l)],[2*(a*o+r*l),1-2*(a*a+r*r),2*(o*r-a*l)],[2*(a*r-o*l),2*(o*r+a*l),1-2*(a*a+o*o)]],c=[0,1,2].map(E=>[0,1,2].map(At=>[0,1,2].reduce((Pt,Z)=>Pt+h[Z][E]*[0,1,2].reduce((Ct,K)=>Ct+s[Z][K]*h[K][At],0),0)));t.set(c[0][0],c[1][1],c[2][2]),e.set(i[0],i[1],i[2],i[3]);let b=[Math.abs(c[1][2]),Math.abs(c[0][2]),Math.abs(c[0][1])],x=b.indexOf(Math.max(...b)),u=(x+1)%3,d=(x+2)%3,g=c[u][d],D=Math.abs(c[0][0])+Math.abs(c[1][1])+Math.abs(c[2][2])+1e-30;if(Math.abs(g)<=1e-9*D)return;let y=(c[d][d]-c[u][u])/(2*g),f=y>0?1:-1;y*=f;let S=f/(y+(y<1e6?Math.sqrt(y*y+1):y)),P=1/Math.sqrt(S*S+1);if(P===1)return;let C=[0,0,0,0];C[x]=-f*Math.sqrt((1-P)/2),C[3]=Math.sqrt(1-C[x]*C[x]);let[T,v,O,w]=i,[R,N,F,z]=C;i[0]=w*R+T*z+v*F-O*N,i[1]=w*N-T*F+v*z+O*R,i[2]=w*F+T*N-v*R+O*z,i[3]=w*z-T*R-v*N-O*F;let Bt=Math.hypot(i[0],i[1],i[2],i[3])||1;for(let E=0;E<4;E++)i[E]/=Bt}}function Nt(m,t){let{x:e,y:s,z:i,w:n}=t,a=[[1-2*(s*s+i*i),2*(e*s-i*n),2*(e*i+s*n)],[2*(e*s+i*n),1-2*(e*e+i*i),2*(s*i-e*n)],[2*(e*i-s*n),2*(s*i+e*n),1-2*(e*e+s*s)]],o=(r,l)=>a[r][0]*m[0]*a[l][0]+a[r][1]*m[1]*a[l][1]+a[r][2]*m[2]*a[l][2];return[o(0,0),o(1,1),o(2,2),o(0,1),o(0,2),o(1,2)]}var k=9,mt=12,ft=5,J=11,X=class{constructor(t,e){this._collectIndices=t;this._flipWinding=e;this._vertices=[];this._indices=[]}addNodeMeshes(t,e){t.computeWorldMatrix(!0);let s=p.TmpVectors.Matrix[0];if(p.Matrix.ScalingToRef(t.absoluteScaling.x,t.absoluteScaling.y,t.absoluteScaling.z,s),t instanceof W.Mesh?this._addMesh(t,s):t instanceof H.InstancedMesh&&this._addMesh(t.sourceMesh,s),e){let i=p.TmpVectors.Matrix[1];t.computeWorldMatrix().invertToRef(i);let n=p.TmpVectors.Matrix[2];i.multiplyToRef(s,n);let a=t.getChildMeshes(!1).filter(o=>!o.physicsBody);for(let o of a){let r=o.computeWorldMatrix(),l=p.TmpVectors.Matrix[3];r.multiplyToRef(n,l),o instanceof W.Mesh?this._addMesh(o,l):o instanceof H.InstancedMesh&&this._addMesh(o.sourceMesh,l)}}}_addMesh(t,e){let s=t.getVerticesData(q.VertexBuffer.PositionKind)||[],i=s.length/3,n=this._vertices.length/3,a=p.TmpVectors.Vector3[0];for(let o=0;o<i;o++)a.set(s[o*3+0],s[o*3+1],s[o*3+2]),p.Vector3.TransformCoordinatesToRef(a,e,a),this._vertices.push(a.x,a.y,a.z);if(this._collectIndices){let o=t.getIndices();if(o)for(let r=0;r<o.length;r+=3)this._flipWinding?this._indices.push(o[r+2]+n,o[r+1]+n,o[r+0]+n):this._indices.push(o[r+0]+n,o[r+1]+n,o[r+2]+n)}}get vertexCount(){return this._vertices.length/3}get triangleCount(){return this._indices.length/3}get vertices(){return this._vertices}get indices(){return this._indices}},Ft=["_bx_Body_AllowFastRotation","_bx_Body_ApplyAngularImpulse","_bx_Body_ApplyForce","_bx_Body_ApplyLinearImpulse","_bx_Body_ApplyMassFromShapes","_bx_Body_ApplyTorque","_bx_Body_ComputeShapeMassData","_bx_Body_EnableSleep","_bx_Body_GetAABB","_bx_Body_GetAngularDamping","_bx_Body_GetAngularVelocity","_bx_Body_GetGravityScale","_bx_Body_GetLinearDamping","_bx_Body_GetLinearVelocity","_bx_Body_GetMassData","_bx_Body_GetTransform","_bx_Body_SetAngularDamping","_bx_Body_SetAngularVelocity","_bx_Body_SetAwake","_bx_Body_SetEventFlags","_bx_Body_SetGravityScale","_bx_Body_SetLinearDamping","_bx_Body_SetLinearVelocity","_bx_Body_SetMassDataFull","_bx_Body_SetMotionLocks","_bx_Body_SetShape","_bx_Body_SetTargetTransform","_bx_Body_SetTransform","_bx_Body_SetType","_bx_Body_SyncShapeDesc","_bx_ComputeAlignedFrameB","_bx_ContactEventsPtr","_bx_CreateBody","_bx_CreateJoint","_bx_CreateWorld","_bx_DebugIndexCount","_bx_DebugIndicesPtr","_bx_DebugPositionsPtr","_bx_DestroyBody","_bx_DestroyJoint","_bx_DestroyWorld","_bx_GetRejectedSlotCount","_bx_Joint_EnableLimit","_bx_Joint_EnableMotor","_bx_Joint_EnableSpring","_bx_Joint_IsValid","_bx_Joint_SetCollideConnected","_bx_Joint_SetConstraintTuning","_bx_Joint_SetLimits","_bx_Joint_SetMaxMotorForce","_bx_Joint_SetMotorSpeed","_bx_Joint_SetSphericalMotor","_bx_Joint_SetSphericalTarget","_bx_Joint_SetSpring","_bx_Joint_SetTarget","_bx_Joint_SetTwistLimits","_bx_Joint_UpdateMotorFrame","_bx_Joint_WakeBodies","_bx_MoveEventsPtr","_bx_RayHitsPtr","_bx_Scratch","_bx_SensorEventsPtr","_bx_ShapeDesc_AddChild","_bx_ShapeDesc_BuildDebugGeometry","_bx_ShapeDesc_CreateBox","_bx_ShapeDesc_CreateCapsule","_bx_ShapeDesc_CreateContainer","_bx_ShapeDesc_CreateCylinder","_bx_ShapeDesc_CreateHeightField","_bx_ShapeDesc_CreateHull","_bx_ShapeDesc_CreateMesh","_bx_ShapeDesc_CreateSphere","_bx_ShapeDesc_Destroy","_bx_ShapeDesc_GetAABB","_bx_ShapeDesc_GetCategoryBits","_bx_ShapeDesc_GetChildCount","_bx_ShapeDesc_GetDensity","_bx_ShapeDesc_GetFriction","_bx_ShapeDesc_GetMaskBits","_bx_ShapeDesc_GetRestitution","_bx_ShapeDesc_RemoveChild","_bx_ShapeDesc_SetDensity","_bx_ShapeDesc_SetFilter","_bx_ShapeDesc_SetGroup","_bx_ShapeDesc_SetMaterial","_bx_ShapeDesc_SetRollingResistance","_bx_ShapeDesc_SetSensor","_bx_World_CastRay","_bx_World_EnableContinuous","_bx_World_EnableSleeping","_bx_World_Explode","_bx_World_GetContactEvents","_bx_World_GetMaximumLinearSpeed","_bx_World_GetMoveEvents","_bx_World_GetSensorEvents","_bx_World_GetStats","_bx_World_SetGravity","_bx_World_SetMaximumLinearSpeed","_bx_World_Step"],V=class{constructor(t=!0,e){this._useDeltaForWorldStep=t;this.name="Box3DPlugin";this.subStepCount=4;this.lastStepTimeMs=0;this._worldSlot=0;this._fixedTimeStep=1/60;this._timeStep=1/60;this._maxAngularVelocity=4*Math.PI*10;this._bodies=[];this._shapes=[];this._bodyCollisionObservable=new Map;this._bodyCollisionEndedObservable=new Map;this._touchedInstanceMeshes=new Set;this._motorJoints=new Set;this._tmpVec3=[new p.Vector3,new p.Vector3,new p.Vector3,new p.Vector3];this._tmpQuat=[new p.Quaternion,new p.Quaternion,new p.Quaternion];this._warned=new Set;this._rejectedSlots=0;this.onCollisionObservable=new M.Observable;this.onCollisionEndedObservable=new M.Observable;this.onTriggerCollisionObservable=new M.Observable;if(!e)throw new Error("Box3D module is required: pass the awaited result of the Box3D module factory.");let s=Ft.filter(i=>typeof e[i]!="function");if(s.length>0)throw new Error(`Box3DPlugin: the Box3D module is missing ${s.length} entry point${s.length===1?"":"s"} this build needs (${s.slice(0,3).join(", ")}${s.length>3?", ...":""}). box3d.js and box3d.wasm have to come from the same build of this package: check what locateFile points at and clear any copied or cached box3d.wasm.`);if(this._b3=e,this.world=e,this._worldSlot=this._b3._bx_CreateWorld(0,-9.81,0),!this._worldSlot)throw new Error("Box3D: could not create a world (max worlds reached?)")}_scratch(){return new Float32Array(this._b3.HEAPF32.buffer,this._b3._bx_Scratch(),64)}_warnOnce(t,e){this._warned.has(t)||(this._warned.add(t),I.Logger.Warn(`Box3DPlugin: ${e}`))}_getPluginReference(t,e){return t._pluginDataInstances?.length?t._pluginDataInstances[e??0]:t._pluginData}_applyToBodyOrInstances(t,e,s){if(t._pluginDataInstances?.length>0&&s===void 0)for(let i of t._pluginDataInstances)e(i);else e(this._getPluginReference(t,s))}_motionTypeToNative(t){switch(t){case _.PhysicsMotionType.STATIC:return 0;case _.PhysicsMotionType.ANIMATED:return 1;default:return 2}}_nodeTransformToRef(t,e,s){if(t.parent){t.computeWorldMatrix(!0),e.copyFrom(t.absolutePosition),s.copyFrom(t.absoluteRotationQuaternion);return}e.copyFrom(t.position),t.rotationQuaternion?s.copyFrom(t.rotationQuaternion):p.Quaternion.FromEulerAnglesToRef(t.rotation.x,t.rotation.y,t.rotation.z,s)}setGravity(t){this._b3._bx_World_SetGravity(this._worldSlot,t.x,t.y,t.z)}setTimeStep(t){this._fixedTimeStep=t}getTimeStep(){return this._fixedTimeStep}executeStep(t,e){for(let o of e)o.disablePreStep||this.setPhysicsBodyTransformation(o,o.transformNode);for(let o of this._motorJoints)this._b3._bx_Joint_IsValid(o)?this._b3._bx_Joint_UpdateMotorFrame(o):this._motorJoints.delete(o);let s=this._useDeltaForWorldStep?t:this._fixedTimeStep;this._timeStep=s;let i=performance.now();this._b3._bx_World_Step(this._worldSlot,s,this.subStepCount);let n=this._b3._bx_World_GetMoveEvents(this._worldSlot);if(n>0){let o=this._b3._bx_MoveEventsPtr(),r=new Float32Array(this._b3.HEAPF32.buffer,o,n*k);for(let l=0;l<n;l++){r.buffer!==this._b3.HEAPF32.buffer&&(r=new Float32Array(this._b3.HEAPF32.buffer,o,n*k));let h=l*k,c=this._bodies[r[h]];!c||c.body.disableSync||this._syncBodyFromBuffer(c.body,c.index,r,h+1)}for(let l of this._touchedInstanceMeshes)l.thinInstanceBufferUpdated("matrix");this._touchedInstanceMeshes.clear()}this._notifyCollisions(),this._notifyTriggers(),this.lastStepTimeMs=performance.now()-i;let a=this._b3._bx_GetRejectedSlotCount();a>this._rejectedSlots&&(this._rejectedSlots=a,this._warnOnce("rejected-slots",`the native module did not recognize ${a} of the handles it was given, and those calls did nothing. Something is using a body, shape or joint after it was destroyed, or box3d.js and box3d.wasm are from different builds.`))}getPluginVersion(){return 2}setVelocityLimits(t,e){this._b3._bx_World_SetMaximumLinearSpeed(this._worldSlot,t),this._maxAngularVelocity=e}getMaxLinearVelocity(){return this._b3._bx_World_GetMaximumLinearSpeed(this._worldSlot)}getMaxAngularVelocity(){return this._maxAngularVelocity}getStats(){this._b3._bx_World_GetStats(this._worldSlot);let t=this._scratch();return{bodies:t[0],shapes:t[1],contacts:t[2],joints:t[3],islands:t[4],stepMs:t[5],collideMs:t[6],solveMs:t[7]}}setSleepingEnabled(t){this._b3._bx_World_EnableSleeping(this._worldSlot,t?1:0)}setContinuousEnabled(t){this._b3._bx_World_EnableContinuous(this._worldSlot,t?1:0)}_createNativeBody(t,e,s,i){let n=this._b3._bx_CreateBody(this._worldSlot,this._motionTypeToNative(t),e.x,e.y,e.z,s.x,s.y,s.z,s.w,i?0:1),a=new Y(n);return a.motionType=t,a}initBody(t,e,s,i){let n=this._createNativeBody(e,s,i,t.startAsleep);t._pluginData=n,this._bodies[n.slot]={body:t,index:0,data:n}}initBodyInstances(t,e,s){let i=s._thinInstanceDataStorage?.instancesCount??0,n=s._thinInstanceDataStorage?.matrixData;if(!n)return;let a=t.shape?._pluginData;i>8&&a&&this._bakesMeshCopies(a)&&this._warnOnce("instanced-mesh-child","a mesh inside a container has no local transform in Box3D, so every instance gets its own baked copy of the mesh data; put the mesh on the body directly, or bake the offset into the mesh, to share it."),this._createOrUpdateBodyInstances(t,e,n,0,i,!1)}_createOrUpdateBodyInstances(t,e,s,i,n,a){let o=p.TmpVectors.Quaternion[0],r=p.TmpVectors.Matrix[0],l=p.TmpVectors.Vector3[0];for(let h=i;h<n;h++)if(l.set(s[h*16+12],s[h*16+13],s[h*16+14]),p.Matrix.FromArrayToRef(s,h*16,r),r.decompose(void 0,o,void 0),a){let c=t._pluginDataInstances[h];this._b3._bx_Body_SetTransform(c.slot,l.x,l.y,l.z,o.x,o.y,o.z,o.w)}else{let c=this._createNativeBody(e,l,o,t.startAsleep),b=t._pluginDataInstances[0];b&&(c.userMassProps=b.userMassProps,c.eventMask=b.eventMask,this._applyEventFlags(c),b.activation!==_.PhysicsActivationControl.SIMULATION_CONTROLLED&&(c.activation=b.activation,this._applyNativeMotionType(c),this._b3._bx_Body_EnableSleep(c.slot,b.activation===_.PhysicsActivationControl.ALWAYS_ACTIVE?0:1))),t._pluginDataInstances.push(c),this._bodies[c.slot]={body:t,index:h,data:c}}}updateBodyInstances(t,e){let s=e._thinInstanceDataStorage?.instancesCount??0,i=e._thinInstanceDataStorage?.matrixData;if(!i)return;let n=t._pluginDataInstances.length,a=this.getMotionType(t);if(s>n){this._createOrUpdateBodyInstances(t,a,i,n,s,!1);let o=t.shape;if(o&&o._pluginData)for(let r=n;r<s;r++)this._setBodyShape(t._pluginDataInstances[r],o._pluginData)}else if(s<n){let o=n-s;for(let r=0;r<o;r++){let l=t._pluginDataInstances.pop();this._destroyBodyData(l)}this._createOrUpdateBodyInstances(t,a,i,0,s,!0)}}_destroyBodyData(t){if(t){for(let e of this._shapes)e?.users.delete(t);this._bodyCollisionObservable.delete(t.slot),this._bodyCollisionEndedObservable.delete(t.slot),this._bodies[t.slot]=void 0,this._b3._bx_DestroyBody(t.slot),t.slot=0}}removeBody(t){if(t._pluginDataInstances?.length)for(let e of t._pluginDataInstances)this._destroyBodyData(e);t._pluginData&&this._destroyBodyData(t._pluginData)}sync(t){this.syncTransform(t,t.transformNode)}_syncBodyFromBuffer(t,e,s,i){let n=t.transformNode;if(t._pluginDataInstances.length){let a=n,o=a._thinInstanceDataStorage?.matrixData;if(!o)return;let r=this._tmpVec3[0],l=e*16;r.set(Math.hypot(o[l],o[l+1],o[l+2]),Math.hypot(o[l+4],o[l+5],o[l+6]),Math.hypot(o[l+8],o[l+9],o[l+10]));let h=this._tmpQuat[0];h.set(s[i+3],s[i+4],s[i+5],s[i+6]);let c=this._tmpVec3[1];c.set(s[i],s[i+1],s[i+2]);let b=p.TmpVectors.Matrix[0];p.Matrix.ComposeToRef(r,h,c,b),b.copyToArray(o,l),this._touchedInstanceMeshes.add(a);return}this._applyTransformToNode(n,s[i],s[i+1],s[i+2],s[i+3],s[i+4],s[i+5],s[i+6])}_applyTransformToNode(t,e,s,i,n,a,o,r){let l=this._tmpQuat[0];l.set(n,a,o,r);let h=t.parent;if(h&&!h.getWorldMatrix().isIdentity()){h.computeWorldMatrix(!0);let c=this._tmpVec3[2];c.copyFrom(t.scaling);let b=p.TmpVectors.Matrix[0],x=this._tmpVec3[3];x.set(e,s,i),p.Matrix.ComposeToRef(t.absoluteScaling,l,x,b);let u=p.TmpVectors.Matrix[1];h.getWorldMatrix().invertToRef(u);let d=p.TmpVectors.Matrix[2];b.multiplyToRef(u,d),d.decomposeToTransformNode(t),t.rotationQuaternion?.normalize(),t.scaling.copyFrom(c)}else t.position.set(e,s,i),t.rotationQuaternion?t.rotationQuaternion.copyFrom(l):l.toEulerAnglesToRef(t.rotation)}syncTransform(t,e){if(t._pluginDataInstances.length){let n=e;if(!n._thinInstanceDataStorage?.matrixData)return;for(let o=0;o<t._pluginDataInstances.length;o++){let r=t._pluginDataInstances[o];this._b3._bx_Body_GetTransform(r.slot),this._syncBodyFromBuffer(t,o,this._scratch(),0)}n.thinInstanceBufferUpdated("matrix"),this._touchedInstanceMeshes.delete(n);return}let s=t._pluginData;if(!s||!s.slot)return;this._b3._bx_Body_GetTransform(s.slot);let i=this._scratch();this._applyTransformToNode(e,i[0],i[1],i[2],i[3],i[4],i[5],i[6])}setPhysicsBodyTransformation(t,e){let s=t.getPrestepType();if(s==_.PhysicsPrestepType.TELEPORT){if(t._pluginDataInstances.length>0){let r=e._thinInstanceDataStorage?.matrixData;if(!r)return;this._createOrUpdateBodyInstances(t,t.getMotionType(),r,0,t._pluginDataInstances.length,!0);return}let i=t._pluginData,n=this._tmpVec3[0],a=this._tmpQuat[0];this._nodeTransformToRef(e,n,a),this._b3._bx_Body_SetTransform(i.slot,n.x,n.y,n.z,a.x,a.y,a.z,a.w)}else s==_.PhysicsPrestepType.ACTION?this.setTargetTransform(t,e.absolutePosition,e.absoluteRotationQuaternion):s==_.PhysicsPrestepType.DISABLED&&I.Logger.Warn("Prestep type is set to DISABLED. Unable to set physics body transformation.")}setTargetTransform(t,e,s,i){this._applyToBodyOrInstances(t,n=>{this._b3._bx_Body_SetTargetTransform(n.slot,e.x,e.y,e.z,s.x,s.y,s.z,s.w,this._timeStep)},i)}_bakesMeshCopies(t,e=new Set){return e.has(t)?!1:(e.add(t),t.children.some(s=>s.type===_.PhysicsShapeType.MESH||this._bakesMeshCopies(s,e)))}_setBodyShape(t,e){for(let s of this._shapes)s?.users.delete(t);this._b3._bx_Body_SetShape(t.slot,e?e.slot:0),e&&e.users.add(t),this._applyEventFlags(t),this._internalUpdateMassProperties(t)}setShape(t,e){let s=e&&e._pluginData?e._pluginData:null;this._applyToBodyOrInstances(t,i=>this._setBodyShape(i,s))}getShape(t){return t.shape??null}getShapeType(t){return t.type??t._pluginData?.type??_.PhysicsShapeType.CONTAINER}_applyEventFlags(t){let e=t.eventMask&5?1:0,s=t.eventMask&2?1:0;this._b3._bx_Body_SetEventFlags(t.slot,e,s)}setEventMask(t,e,s){this._applyToBodyOrInstances(t,i=>{i.eventMask=e,this._applyEventFlags(i)},s)}getEventMask(t,e){return this._getPluginReference(t,e).eventMask}setMotionType(t,e,s){this._applyToBodyOrInstances(t,i=>{i.motionType=e,this._applyNativeMotionType(i),this._internalUpdateMassProperties(i)},s)}_applyNativeMotionType(t){let e=t.activation===_.PhysicsActivationControl.ALWAYS_INACTIVE?0:this._motionTypeToNative(t.motionType);this._b3._bx_Body_SetType(t.slot,e)}setActivationControl(t,e,s){this._applyToBodyOrInstances(t,i=>{if(i.activation===e)return;let n=i.activation===_.PhysicsActivationControl.ALWAYS_INACTIVE;if(i.activation=e,e===_.PhysicsActivationControl.ALWAYS_INACTIVE){this._b3._bx_Body_SetLinearVelocity(i.slot,0,0,0),this._b3._bx_Body_SetAngularVelocity(i.slot,0,0,0),this._applyNativeMotionType(i);return}this._applyNativeMotionType(i),this._internalUpdateMassProperties(i),this._b3._bx_Body_EnableSleep(i.slot,e===_.PhysicsActivationControl.ALWAYS_ACTIVE?0:1),n&&e===_.PhysicsActivationControl.SIMULATION_CONTROLLED&&this._b3._bx_Body_SetAwake(i.slot,0)},s)}getMotionType(t,e){return this._getPluginReference(t,e).motionType}_readMassData(t){t.motionType===_.PhysicsMotionType.DYNAMIC&&t.activation!==_.PhysicsActivationControl.ALWAYS_INACTIVE?this._b3._bx_Body_GetMassData(t.slot):this._b3._bx_Body_ComputeShapeMassData(t.slot);let e=this._scratch(),s=e[0],i=new p.Vector3,n=new p.Quaternion;return Rt([e[4],e[5],e[6],e[7],e[8],e[9]],i,n),s>0&&i.scaleInPlace(1/s),{mass:s,centerOfMass:new p.Vector3(e[1],e[2],e[3]),inertia:i,inertiaOrientation:n}}_resolveMassProperties(t){let e=this._readMassData(t),s=t.userMassProps??{};return{mass:s.mass!==void 0&&s.mass>0?s.mass:e.mass,centerOfMass:s.centerOfMass??e.centerOfMass,inertia:s.inertia??e.inertia,inertiaOrientation:s.inertiaOrientation??(s.inertia?p.Quaternion.Identity():e.inertiaOrientation)}}_internalUpdateMassProperties(t){if(t.motionType!==_.PhysicsMotionType.DYNAMIC||t.activation===_.PhysicsActivationControl.ALWAYS_INACTIVE){this._applyMotionLocks(t,!1,!1,!1);return}this._b3._bx_Body_ApplyMassFromShapes(t.slot);let e=t.userMassProps;if(!(!!e&&(e.mass!==void 0||!!e.centerOfMass||!!e.inertia||!!e.inertiaOrientation))){this._applyMotionLocks(t,!1,!1,!1);return}let i=this._resolveMassProperties(t),n=i.mass>0?i.mass:1,a=i.inertia,o=i.inertiaOrientation,r=[a.x<=0,a.y<=0,a.z<=0],l=Math.max(a.x,a.y,a.z,1e-4)*wt,h=[r[0]?l:a.x,r[1]?l:a.y,r[2]?l:a.z].map(b=>b*n),c=Nt(h,o);this._b3._bx_Body_SetMassDataFull(t.slot,n,i.centerOfMass.x,i.centerOfMass.y,i.centerOfMass.z,c[0],c[1],c[2],c[3],c[4],c[5]),this._applyLockedAxes(t,r,o)}_applyLockedAxes(t,e,s){let i=e.filter(Boolean).length;if(i===3){this._applyMotionLocks(t,!0,!0,!0);return}if(i!==2){this._applyMotionLocks(t,!1,!1,!1);return}this._b3._bx_Body_GetTransform(t.slot);let n=this._scratch(),a=this._tmpQuat[0].set(n[3],n[4],n[5],n[6]),o=e.indexOf(!1),r=this._tmpVec3[0].set(o===0?1:0,o===1?1:0,o===2?1:0);r.applyRotationQuaternionInPlace(s).applyRotationQuaternionInPlace(a);let l=[Math.abs(r.x),Math.abs(r.y),Math.abs(r.z)],h=l.indexOf(Math.max(...l));l[h]>.9999?this._applyMotionLocks(t,h!==0,h!==1,h!==2):this._applyMotionLocks(t,!1,!1,!1)}_applyMotionLocks(t,e,s,i){let n=(e?1:0)|(s?2:0)|(i?4:0);n!==t.angularLocks&&(t.angularLocks=n,this._b3._bx_Body_SetMotionLocks(t.slot,0,0,0,e?1:0,s?1:0,i?1:0))}computeMassProperties(t,e){let s=this._getPluginReference(t,e);this._b3._bx_Body_ApplyMassFromShapes(s.slot);let i=this._readMassData(s);return this._internalUpdateMassProperties(s),i}setMassProperties(t,e,s){this._applyToBodyOrInstances(t,i=>{i.userMassProps=e,this._internalUpdateMassProperties(i)},s)}getMassProperties(t,e){return this._resolveMassProperties(this._getPluginReference(t,e))}setLinearDamping(t,e,s){this._applyToBodyOrInstances(t,i=>this._b3._bx_Body_SetLinearDamping(i.slot,e),s)}getLinearDamping(t,e){return this._b3._bx_Body_GetLinearDamping(this._getPluginReference(t,e).slot)}setAngularDamping(t,e,s){this._applyToBodyOrInstances(t,i=>this._b3._bx_Body_SetAngularDamping(i.slot,e),s)}getAngularDamping(t,e){return this._b3._bx_Body_GetAngularDamping(this._getPluginReference(t,e).slot)}setLinearVelocity(t,e,s){this._applyToBodyOrInstances(t,i=>this._b3._bx_Body_SetLinearVelocity(i.slot,e.x,e.y,e.z),s)}getLinearVelocityToRef(t,e,s){this._b3._bx_Body_GetLinearVelocity(this._getPluginReference(t,s).slot);let i=this._scratch();e.set(i[0],i[1],i[2])}applyImpulse(t,e,s,i){this._applyToBodyOrInstances(t,n=>this._b3._bx_Body_ApplyLinearImpulse(n.slot,e.x,e.y,e.z,s.x,s.y,s.z),i)}applyAngularImpulse(t,e,s){this._applyToBodyOrInstances(t,i=>this._b3._bx_Body_ApplyAngularImpulse(i.slot,e.x,e.y,e.z),s)}applyForce(t,e,s,i){this._applyToBodyOrInstances(t,n=>this._b3._bx_Body_ApplyForce(n.slot,e.x,e.y,e.z,s.x,s.y,s.z),i)}applyTorque(t,e,s){this._applyToBodyOrInstances(t,i=>this._b3._bx_Body_ApplyTorque(i.slot,e.x,e.y,e.z),s)}setAngularVelocity(t,e,s){this._applyToBodyOrInstances(t,i=>this._b3._bx_Body_SetAngularVelocity(i.slot,e.x,e.y,e.z),s)}getAngularVelocityToRef(t,e,s){this._b3._bx_Body_GetAngularVelocity(this._getPluginReference(t,s).slot);let i=this._scratch();e.set(i[0],i[1],i[2])}getBodyGeometry(t){let e=t.shape?._pluginData;if(!e)return{positions:[],indices:[]};let s=this._b3._bx_ShapeDesc_BuildDebugGeometry(e.slot),i=this._b3._bx_DebugIndexCount();if(!s||!i)return{positions:[],indices:[]};let n=new Float32Array(this._b3.HEAPF32.buffer,this._b3._bx_DebugPositionsPtr(),s*3).slice(),a=new Uint32Array(this._b3.HEAPU32.buffer,this._b3._bx_DebugIndicesPtr(),i).slice();return{positions:n,indices:a}}disposeBody(t){this.removeBody(t),t._pluginData=void 0,t._pluginDataInstances.length=0}setCollisionCallbackEnabled(t,e,s){this.setEventMask(t,e?7:0,s)}setCollisionEndedCallbackEnabled(t,e,s){this._applyToBodyOrInstances(t,i=>{i.eventMask=e?i.eventMask|4:i.eventMask&-5,this._applyEventFlags(i)},s)}getCollisionObservable(t,e){let s=this._getPluginReference(t,e).slot,i=this._bodyCollisionObservable.get(s);return i||(i=new M.Observable,this._bodyCollisionObservable.set(s,i)),i}getCollisionEndedObservable(t,e){let s=this._getPluginReference(t,e).slot,i=this._bodyCollisionEndedObservable.get(s);return i||(i=new M.Observable,this._bodyCollisionEndedObservable.set(s,i)),i}setGravityFactor(t,e,s){this._applyToBodyOrInstances(t,i=>this._b3._bx_Body_SetGravityScale(i.slot,e),s)}getGravityFactor(t,e){return this._b3._bx_Body_GetGravityScale(this._getPluginReference(t,e).slot)}_registerShape(t,e,s){if(!e)throw new Error("Box3DPlugin: shape creation failed (degenerate geometry?)");let i=new j(e,s);return t._pluginData=i,this._shapes[e]=i,i}_withFloatBuffer(t,e){let s=this._b3._malloc(t.length*4);new Float32Array(this._b3.HEAPF32.buffer,s,t.length).set(t);try{return e(s)}finally{this._b3._free(s)}}_withByteBuffer(t,e){if(!t||t.length===0)return e(0);let s=this._b3._malloc(t.length);new Uint8Array(this._b3.HEAPU8.buffer,s,t.length).set(t);try{return e(s)}finally{this._b3._free(s)}}_withIntBuffer(t,e){let s=this._b3._malloc(t.length*4);new Int32Array(this._b3.HEAP32.buffer,s,t.length).set(t);try{return e(s)}finally{this._b3._free(s)}}_createOptionsFromGroundMesh(t){let e=t.groundMesh;if(!e)return;let s=e.getVerticesData(q.VertexBuffer.PositionKind),i=e.computeWorldMatrix(!0),n=[],a=p.TmpVectors.Vector3[0];for(let d=0;d<s.length;d+=3)p.Vector3.FromArrayToRef(s,d,a),p.Vector3.TransformCoordinatesToRef(a,i,a),a.toArray(n,d);let o=~~(Math.sqrt(n.length/3)-1),r=e.getBoundingInfo(),l=Math.min(r.boundingBox.extendSizeWorld.x,r.boundingBox.extendSizeWorld.z),h=r.boundingBox.minimumWorld.x,c=r.boundingBox.minimumWorld.y,b=r.boundingBox.minimumWorld.z,x=new Float32Array((o+1)*(o+1)),u=l*2/o;x.fill(c);for(let d=0;d<n.length;d+=3){let g=Math.round((n[d+0]-h)/u),D=o-Math.round((n[d+2]-b)/u);x[D*(o+1)+g]=n[d+1]-c}t.numHeightFieldSamplesX=o+1,t.numHeightFieldSamplesZ=o+1,t.heightFieldSizeX=r.boundingBox.extendSizeWorld.x*2,t.heightFieldSizeZ=r.boundingBox.extendSizeWorld.z*2,t.heightFieldData=x}initShape(t,e,s){let i=this._b3,n=s.center??p.Vector3.ZeroReadOnly,a=s.rotation??p.Quaternion.Identity();switch(e){case _.PhysicsShapeType.SPHERE:{let o=s.radius??1;this._registerShape(t,i._bx_ShapeDesc_CreateSphere(n.x,n.y,n.z,o),e);break}case _.PhysicsShapeType.CAPSULE:{let o=s.pointA??p.Vector3.ZeroReadOnly,r=s.pointB??p.Vector3.UpReadOnly;this._registerShape(t,i._bx_ShapeDesc_CreateCapsule(o.x,o.y,o.z,r.x,r.y,r.z,s.radius??0),e);break}case _.PhysicsShapeType.BOX:{let o=s.extents??p.Vector3.OneReadOnly;this._registerShape(t,i._bx_ShapeDesc_CreateBox(o.x*.5,o.y*.5,o.z*.5,n.x,n.y,n.z,a.x,a.y,a.z,a.w),e);break}case _.PhysicsShapeType.CYLINDER:{let o=s.pointA??p.Vector3.ZeroReadOnly,r=s.pointB??p.Vector3.UpReadOnly,l=r.subtract(o),h=l.length(),c=o.add(r).scaleInPlace(.5),b=this._tmpQuat[0];h>1e-6?(l.scaleInPlace(1/h),p.Quaternion.FromUnitVectorsToRef(p.Vector3.UpReadOnly,l,b)):b.set(0,0,0,1),this._registerShape(t,i._bx_ShapeDesc_CreateCylinder(h,s.radius??0,16,c.x,c.y,c.z,b.x,b.y,b.z,b.w),e);break}case _.PhysicsShapeType.CONVEX_HULL:case _.PhysicsShapeType.MESH:{let o=s.mesh;if(!o)throw new Error("No mesh provided to create physics shape.");let r=e===_.PhysicsShapeType.MESH,l=!o.getScene().useRightHandedSystem,h=new X(r,l);if(h.addNodeMeshes(o,!!s.includeChildMeshes),e===_.PhysicsShapeType.CONVEX_HULL){let c=this._withFloatBuffer(h.vertices,b=>i._bx_ShapeDesc_CreateHull(b,h.vertexCount));this._registerShape(t,c,e)}else{let c=this._withFloatBuffer(h.vertices,b=>this._withIntBuffer(h.indices,x=>i._bx_ShapeDesc_CreateMesh(b,h.vertexCount,x,h.triangleCount,1,1,1,0,0)));this._registerShape(t,c,e)}break}case _.PhysicsShapeType.HEIGHTFIELD:{s.groundMesh&&this._createOptionsFromGroundMesh(s);let o=s.numHeightFieldSamplesX,r=s.numHeightFieldSamplesZ;if(!o||!r||!s.heightFieldSizeX||!s.heightFieldSizeZ||!s.heightFieldData)throw new Error("Missing required heightfield parameters");let l=new Float32Array(o*r);for(let D=0;D<o;D++)for(let y=0;y<r;y++)l[y*o+D]=s.heightFieldData[(r-1-y)*o+D];let h=s.heightFieldSizeX/(o-1),c=s.heightFieldSizeZ/(r-1),b=o-1,x=r-1,u=s.heightFieldMaterials;if(u&&u.length!==b*x)throw new Error(`heightFieldMaterials needs ${b*x} cells (${b} \xD7 ${x}), got ${u.length}`);let d=u?new Uint8Array(b*x):null;if(u&&d)for(let D=0;D<b;D++)for(let y=0;y<x;y++)d[y*b+D]=u[(x-1-y)*b+D];let g=this._withFloatBuffer(l,D=>this._withByteBuffer(d,y=>i._bx_ShapeDesc_CreateHeightField(D,y,o,r,h,1,c,0,-s.heightFieldSizeX*.5,0,-s.heightFieldSizeZ*.5)));this._registerShape(t,g,e);break}case _.PhysicsShapeType.CONTAINER:{this._registerShape(t,i._bx_ShapeDesc_CreateContainer(),e);break}default:throw new Error("Unsupported Shape Type.")}}_refreshShapeUsers(t,e=new Set){if(!e.has(t)){e.add(t);for(let s of t.users)this._b3._bx_Body_SetShape(s.slot,t.slot),this._applyEventFlags(s),this._internalUpdateMassProperties(s);for(let s of t.parents)this._refreshShapeUsers(s,e)}}_syncShapeUsers(t,e,s=new Set,i=t){if(!s.has(t)){s.add(t);for(let n of t.users)this._b3._bx_Body_SyncShapeDesc(n.slot,i.slot,e),e&4&&this._internalUpdateMassProperties(n);for(let n of t.parents)this._syncShapeUsers(n,e,s,i)}}setShapeFilterMembershipMask(t,e){let s=t._pluginData,i=this._b3._bx_ShapeDesc_GetMaskBits(s.slot);this._b3._bx_ShapeDesc_SetFilter(s.slot,e>>>0,i>>>0),this._syncShapeUsers(s,1)}getShapeFilterMembershipMask(t){return this._b3._bx_ShapeDesc_GetCategoryBits(t._pluginData.slot)}setShapeFilterCollideMask(t,e){let s=t._pluginData,i=this._b3._bx_ShapeDesc_GetCategoryBits(s.slot);this._b3._bx_ShapeDesc_SetFilter(s.slot,i>>>0,e>>>0),this._syncShapeUsers(s,1)}getShapeFilterCollideMask(t){return this._b3._bx_ShapeDesc_GetMaskBits(t._pluginData.slot)}setMaterial(t,e){let s=t._pluginData;s.material=e,this._b3._bx_ShapeDesc_SetMaterial(s.slot,e.friction??.5,e.restitution??0),this._syncShapeUsers(s,2)}getMaterial(t){let e=t._pluginData;return{friction:this._b3._bx_ShapeDesc_GetFriction(e.slot),staticFriction:e.material.staticFriction,restitution:this._b3._bx_ShapeDesc_GetRestitution(e.slot),frictionCombine:e.material.frictionCombine,restitutionCombine:e.material.restitutionCombine}}setDensity(t,e){let s=t._pluginData;this._b3._bx_ShapeDesc_SetDensity(s.slot,e),this._syncShapeUsers(s,4)}getDensity(t){return this._b3._bx_ShapeDesc_GetDensity(t._pluginData.slot)}addChild(t,e,s,i,n){let a=t._pluginData,o=e._pluginData,r=s??p.Vector3.ZeroReadOnly,l=i??p.Quaternion.Identity(),h=n??p.Vector3.OneReadOnly;this._b3._bx_ShapeDesc_AddChild(a.slot,o.slot,r.x,r.y,r.z,l.x,l.y,l.z,l.w,h.x,h.y,h.z),a.children.push(o),o.parents.add(a),this._refreshShapeUsers(a)}removeChild(t,e){let s=t._pluginData;this._b3._bx_ShapeDesc_RemoveChild(s.slot,e);let[i]=s.children.splice(e,1);i&&!s.children.includes(i)&&i.parents.delete(s),this._refreshShapeUsers(s)}getNumChildren(t){return this._b3._bx_ShapeDesc_GetChildCount(t._pluginData.slot)}getBoundingBox(t){this._b3._bx_ShapeDesc_GetAABB(t._pluginData.slot);let e=this._scratch();return p.TmpVectors.Vector3[0].set(e[0],e[1],e[2]),p.TmpVectors.Vector3[1].set(e[3],e[4],e[5]),new U.BoundingBox(p.TmpVectors.Vector3[0],p.TmpVectors.Vector3[1],p.Matrix.IdentityReadOnly)}getBodyBoundingBox(t){let e=this._getPluginReference(t);this._b3._bx_Body_GetAABB(e.slot);let s=this._scratch();return p.TmpVectors.Vector3[0].set(s[0],s[1],s[2]),p.TmpVectors.Vector3[1].set(s[3],s[4],s[5]),new U.BoundingBox(p.TmpVectors.Vector3[0],p.TmpVectors.Vector3[1],p.Matrix.IdentityReadOnly)}disposeShape(t){let e=t._pluginData;if(e){for(let s of e.parents){let i=s.children.indexOf(e);i>=0&&(s.children.splice(i,1),this._b3._bx_ShapeDesc_RemoveChild(s.slot,i))}for(let s of e.children)s.parents.delete(e);this._shapes[e.slot]=void 0,this._b3._bx_ShapeDesc_Destroy(e.slot),t._pluginData=void 0}}setTrigger(t,e){let s=t._pluginData;this._b3._bx_ShapeDesc_SetSensor(s.slot,e?1:0),this._refreshShapeUsers(s)}_frameToBuffer(t,e,s,i,n,a){let o=p.TmpVectors.Matrix[0];p.Matrix.FromValuesToRef(i.x,i.y,i.z,0,n.x,n.y,n.z,0,a.x,a.y,a.z,0,0,0,0,1,o);let r=this._tmpQuat[2];p.Quaternion.FromRotationMatrixToRef(o,r),r.normalize(),t[e]=s.x,t[e+1]=s.y,t[e+2]=s.z,t[e+3]=r.x,t[e+4]=r.y,t[e+5]=r.z,t[e+6]=r.w}_buildFrames(t,e,s,i){let n=e.options,a=n.pivotA??p.Vector3.ZeroReadOnly,o=n.pivotB??p.Vector3.ZeroReadOnly,r=(n.axisA??p.Vector3.RightReadOnly).normalizeToNew(),l=(n.axisB??p.Vector3.RightReadOnly).normalizeToNew(),h=n.perpAxisA?n.perpAxisA.normalizeToNew():r.getNormalToRef(new p.Vector3),c=n.perpAxisB?n.perpAxisB.normalizeToNew():l.getNormalToRef(new p.Vector3);h.subtractInPlace(r.scale(p.Vector3.Dot(r,h))).normalize(),c.subtractInPlace(l.scale(p.Vector3.Dot(l,c))).normalize();let b=p.Vector3.Cross(r,h),x=p.Vector3.Cross(l,c);e._initOptions||(e._initOptions={axisA:r.clone(),axisB:l.clone(),perpAxisA:h.clone(),perpAxisB:c.clone(),pivotA:a.clone(),pivotB:o.clone()});let u=t.frames;if(t.plan){let d=t.plan.basis;if(d){let g=[r,h,b],D=[l,c,x];this._frameToBuffer(u,0,a,g[d[0]],g[d[1]],g[d[2]]),this._frameToBuffer(u,7,o,D[d[0]],D[d[1]],D[d[2]])}else u.set([a.x,a.y,a.z,0,0,0,1,o.x,o.y,o.z,0,0,0,1]);return}switch(t.type){case _.PhysicsConstraintType.HINGE:case _.PhysicsConstraintType.BALL_AND_SOCKET:this._frameToBuffer(u,0,a,h,p.Vector3.Cross(r,h),r),this._frameToBuffer(u,7,o,c,p.Vector3.Cross(l,c),l);break;case _.PhysicsConstraintType.PRISMATIC:case _.PhysicsConstraintType.SLIDER:this._frameToBuffer(u,0,a,r,h,b),this._frameToBuffer(u,7,o,l,c,x);break;case _.PhysicsConstraintType.LOCK:{this._b3._bx_Body_GetTransform(s.slot);let d=this._scratch(),g=this._tmpQuat[0];g.set(d[3],d[4],d[5],d[6]),this._b3._bx_Body_GetTransform(i.slot),d=this._scratch();let D=this._tmpQuat[1];D.set(d[3],d[4],d[5],d[6]);let y=D.invert().multiply(g).normalize();u.set([a.x,a.y,a.z,0,0,0,1,o.x,o.y,o.z,y.x,y.y,y.z,y.w]);break}default:u.set([a.x,a.y,a.z,0,0,0,1,o.x,o.y,o.z,0,0,0,1]);break}}_createJoint(t,e,s){this._scratch().set(t.frames);let i=this._b3._bx_CreateJoint(this._worldSlot,t.jointType,e.slot,s.slot,t.collisions?1:0,t.length);return i?(this._applyAxisState(t,i,e,s),i):(I.Logger.Warn("Box3DPlugin: joint creation failed."),0)}_readSixDofLimits(t,e){for(let s of e??[]){let i=this._axisState(t,s.axis);s.minLimit===0&&s.maxLimit===0?(i.mode=_.PhysicsConstraintAxisLimitMode.LOCKED,i.min=0,i.max=0):(s.minLimit!==void 0||s.maxLimit!==void 0)&&(i.mode=_.PhysicsConstraintAxisLimitMode.LIMITED,i.min=s.minLimit??-1/0,i.max=s.maxLimit??1/0),i.stiffness=s.stiffness??0,i.damping=s.damping??0}}_planSixDof(t){let e=_.PhysicsConstraintAxis,s=_.PhysicsConstraintAxisLimitMode,i=y=>t.axes.get(y)?.mode??s.FREE,n=y=>{let f=t.axes.get(y);return f.mode===s.LOCKED?[0,0]:[Math.min(f.min,f.max),Math.max(f.min,f.max)]},a=(y,f)=>Math.max(-f,Math.min(f,y)),o=.99*Math.PI,r=1e5,l={jointType:0,basis:[0,1,2],primaryAxis:null,coneAngle:-1,twist:null,limit:null,spring:!1,stiffness:0,damping:0,angularStiffness:!1};for(let[y,f]of t.axes)(f.stiffness>0||f.damping>0)&&f.stiffness>=l.stiffness&&(l.stiffness=f.stiffness,l.damping=f.damping,l.angularStiffness=y===e.ANGULAR_X||y===e.ANGULAR_Y||y===e.ANGULAR_Z);let h=[e.LINEAR_X,e.LINEAR_Y,e.LINEAR_Z],c=[e.ANGULAR_X,e.ANGULAR_Y,e.ANGULAR_Z],b=h.filter(y=>i(y)===s.LOCKED).length,x=h.filter(y=>i(y)===s.FREE).length,u=c.filter(y=>i(y)!==s.LOCKED),d=c.filter(y=>i(y)===s.FREE).length;if(i(e.LINEAR_DISTANCE)!==s.FREE){if(x===3&&d===3){let[y,f]=n(e.LINEAR_DISTANCE);return l.jointType=4,l.basis=null,l.primaryAxis=e.LINEAR_DISTANCE,l.limit=[Math.min(Math.max(0,y),r),Math.min(Math.max(0,f),r)],l.spring=l.stiffness>0&&Math.abs(f-y)<1e-6&&isFinite(y),l}this._warnOnce("sixdof-distance","SIX_DOF: LINEAR_DISTANCE combined with other constrained axes is not supported with Box3D, the distance limit is ignored.")}let D=b===3;if(!D&&b!==2&&x===0&&(this._warnOnce("sixdof-linear-limited","SIX_DOF: two or more limited linear axes are treated as locked with Box3D."),D=!0),D){if(u.length===0)return l;if(u.length===1){let f=u[0];if(l.jointType=2,l.basis=[[1,2,0],[2,0,1],[0,1,2]][c.indexOf(f)],l.primaryAxis=f,i(f)===s.LIMITED){let[S,P]=n(f);l.limit=[a(S,o),a(P,o)]}return l}if(l.jointType=1,l.basis=[1,2,0],i(e.ANGULAR_X)!==s.FREE){let[f,S]=n(e.ANGULAR_X);l.twist=[a(f,o),a(S,o)]}let y=[i(e.ANGULAR_Y),i(e.ANGULAR_Z)];if(y.some(f=>f!==s.FREE)){let f=[],S=!1;for(let C of[e.ANGULAR_Y,e.ANGULAR_Z]){if(i(C)===s.FREE)continue;let[T,v]=n(C);f.push(Math.max(Math.abs(T),Math.abs(v))),S||(S=Math.abs(T+v)>1e-4)}let P=Math.max(...f);isFinite(P)&&((y.includes(s.FREE)||Math.abs(f[0]-f[1])>1e-4||S)&&this._warnOnce("sixdof-cone","SIX_DOF: Box3D has one symmetric swing cone, ANGULAR_Y and ANGULAR_Z use the larger of their ranges."),P>Math.PI/2&&this._warnOnce("sixdof-cone-90","SIX_DOF: Box3D cone limits are at most 90 degrees, larger swing ranges are clamped."),l.coneAngle=Math.min(P,Math.PI/2))}return l}if(b===2){let y=h.find(f=>i(f)!==s.LOCKED);if(l.jointType=3,l.basis=[[0,1,2],[1,2,0],[2,0,1]][h.indexOf(y)],l.primaryAxis=y,i(y)===s.LIMITED){let[f,S]=n(y);l.limit=[a(f,r),a(S,r)]}return u.length&&this._warnOnce("sixdof-prismatic","SIX_DOF: a sliding axis with rotation is not supported with Box3D, rotation is locked."),l}return l.jointType=5,l.basis=null,(x!==3||d!==3)&&this._warnOnce("sixdof-unsupported","SIX_DOF: this combination of free and limited axes is not supported with Box3D, the bodies are left unconstrained."),l}_springToHertz(t,e,s,i,n){let a=h=>{if(h.motionType!==_.PhysicsMotionType.DYNAMIC)return 0;this._b3._bx_Body_GetMassData(h.slot);let c=this._scratch(),b=s?(c[4]+c[5]+c[6])/3:c[0];return b>0?1/b:0},o=a(i)+a(n),r=o>0?1/o:1,l=Math.max(t,0);return{hertz:Math.sqrt(l/r)/(2*Math.PI),dampingRatio:l>0?e/(2*Math.sqrt(l*r)):1}}initConstraint(t,e,s,i,n){let a=t.type,o=t.options;if(!a||!o){I.Logger.Warn("No constraint type or options. Constraint is invalid.");return}if(e._pluginDataInstances.length>0&&i===void 0||s._pluginDataInstances.length>0&&n===void 0){I.Logger.Warn("Body is instanced but no instance index was specified. Constraint will not be applied.");return}let r=!t._pluginData,l=t._pluginData??new Q;switch(t._pluginData=l,l.type=a,r&&(l.collisions=!!o.collision),a){case _.PhysicsConstraintType.BALL_AND_SOCKET:l.jointType=1;break;case _.PhysicsConstraintType.HINGE:l.jointType=2;break;case _.PhysicsConstraintType.PRISMATIC:case _.PhysicsConstraintType.SLIDER:l.jointType=3;break;case _.PhysicsConstraintType.LOCK:l.jointType=0;break;case _.PhysicsConstraintType.DISTANCE:l.jointType=4;break;case _.PhysicsConstraintType.SIX_DOF:r&&this._readSixDofLimits(l,t.limits),l.plan=this._planSixDof(l),l.jointType=l.plan.jointType;break;default:I.Logger.Warn("Box3DPlugin: unknown constraint type "+a);return}let h=this._getPluginReference(e,i),c=this._getPluginReference(s,n);this._buildFrames(l,t,h,c),this._updateJointLength(l,o.maxDistance,h,c);let b=l.enabled?this._createJoint(l,h,c):0;l.joints.push(b),l.pairs.push({parent:e,parentIndex:i??0,child:s,childIndex:n??0,parentData:h,childData:c})}_updateJointLength(t,e,s,i){if(t.type===_.PhysicsConstraintType.DISTANCE)t.length=e??0;else if(t.plan?.jointType===4&&t.plan.limit)t.length=t.plan.limit[0];else return;t.length<=0&&(t.length=Math.max(.01,this._currentPivotDistance(t,s,i)))}_currentPivotDistance(t,e,s){let i=t.frames,n=this._tmpVec3[0],a=this._tmpVec3[1];this._b3._bx_Body_GetTransform(e.slot);let o=this._scratch();return this._tmpQuat[0].set(o[3],o[4],o[5],o[6]),n.set(i[0],i[1],i[2]).applyRotationQuaternionInPlace(this._tmpQuat[0]).addInPlaceFromFloats(o[0],o[1],o[2]),this._b3._bx_Body_GetTransform(s.slot),o=this._scratch(),this._tmpQuat[0].set(o[3],o[4],o[5],o[6]),a.set(i[7],i[8],i[9]).applyRotationQuaternionInPlace(this._tmpQuat[0]).addInPlaceFromFloats(o[0],o[1],o[2]),p.Vector3.Distance(n,a)}setEnabled(t,e){let s=t._pluginData;!s||s.enabled===e||(s.enabled=e,this._recreateJoints(s))}_destroyJoint(t){this._motorJoints.delete(t),this._b3._bx_DestroyJoint(t)}_recreateJoints(t){for(let e=0;e<t.pairs.length;e++){t.joints[e]&&this._destroyJoint(t.joints[e]);let s=t.pairs[e];t.joints[e]=t.enabled&&s.parentData.slot&&s.childData.slot?this._createJoint(t,s.parentData,s.childData):0}}getEnabled(t){return t._pluginData?.enabled??!1}setCollisionsEnabled(t,e){let s=t._pluginData;s.collisions=e;for(let i of s.joints)i&&this._b3._bx_Joint_SetCollideConnected(i,e?1:0)}getCollisionsEnabled(t){return t._pluginData?.collisions??!1}_primaryAxis(t){switch(t.type){case _.PhysicsConstraintType.HINGE:return _.PhysicsConstraintAxis.ANGULAR_X;case _.PhysicsConstraintType.PRISMATIC:case _.PhysicsConstraintType.SLIDER:return _.PhysicsConstraintAxis.LINEAR_X;case _.PhysicsConstraintType.DISTANCE:return _.PhysicsConstraintAxis.LINEAR_DISTANCE;case _.PhysicsConstraintType.BALL_AND_SOCKET:return _.PhysicsConstraintAxis.ANGULAR_Y;case _.PhysicsConstraintType.SIX_DOF:return t.plan?.primaryAxis??null;default:return null}}_isAngularAxis(t){return t===_.PhysicsConstraintAxis.ANGULAR_X||t===_.PhysicsConstraintAxis.ANGULAR_Y||t===_.PhysicsConstraintAxis.ANGULAR_Z}_isPrimaryAxis(t,e){return t.type===_.PhysicsConstraintType.BALL_AND_SOCKET||t.plan?.jointType===1?this._isAngularAxis(e):e===this._primaryAxis(t)}_axisState(t,e){let s=t.axes.get(e);return s||(s={mode:_.PhysicsConstraintAxisLimitMode.FREE,min:0,max:0,motor:_.PhysicsConstraintMotorType.NONE,target:0,maxForce:0,friction:0,stiffness:0,damping:0},t.axes.set(e,s)),s}_applyAxisState(t,e,s,i){let n=this._b3;if(t.plan){this._applySixDofPlan(t,e,s,i),n._bx_Joint_WakeBodies(e);return}if(this._primaryAxis(t)!==null){for(let[o,r]of t.axes){if(!this._isPrimaryAxis(t,o))continue;if(t.type===_.PhysicsConstraintType.BALL_AND_SOCKET&&o===_.PhysicsConstraintAxis.ANGULAR_X){n._bx_Joint_SetTwistLimits(e,r.mode===_.PhysicsConstraintAxisLimitMode.FREE?0:1,r.min,r.max);continue}let l=r.mode!==_.PhysicsConstraintAxisLimitMode.FREE;if(l){let h=r.mode===_.PhysicsConstraintAxisLimitMode.LOCKED?0:r.min,c=r.mode===_.PhysicsConstraintAxisLimitMode.LOCKED?0:r.max;n._bx_Joint_SetLimits(e,h,c)}n._bx_Joint_EnableLimit(e,l?1:0),t.type!==_.PhysicsConstraintType.BALL_AND_SOCKET&&this._applyMotor(e,r)}t.type===_.PhysicsConstraintType.BALL_AND_SOCKET&&this._applySphericalMotor(t,e),n._bx_Joint_WakeBodies(e)}}_applySphericalMotor(t,e){let s=this._b3,i=t.axes.get(_.PhysicsConstraintAxis.ANGULAR_X),n=t.axes.get(_.PhysicsConstraintAxis.ANGULAR_Y),a=t.axes.get(_.PhysicsConstraintAxis.ANGULAR_Z),o=[i,n,a],r=x=>x?.motor===_.PhysicsConstraintMotorType.VELOCITY?x.target:0,l=o.some(x=>x?.motor===_.PhysicsConstraintMotorType.VELOCITY),h=o.some(x=>x?.motor===_.PhysicsConstraintMotorType.POSITION),c=Math.max(...o.map(x=>x?.motor===_.PhysicsConstraintMotorType.VELOCITY?x.maxForce:0));s._bx_Joint_SetSphericalMotor(e,l?1:0,r(n),r(a),r(i),c);let b=r(n)!==0||r(a)!==0||r(i)!==0;if(l&&b?this._motorJoints.add(e):this._motorJoints.delete(e),s._bx_Joint_EnableSpring(e,h?1:0),h){let x=g=>g?.motor===_.PhysicsConstraintMotorType.POSITION?g.target:0,u=this._tmpQuat[0];p.Quaternion.RotationYawPitchRollToRef(0,0,x(i),u);let d=this._tmpQuat[1];p.Quaternion.RotationYawPitchRollToRef(x(a),x(n),0,d),u.multiplyInPlace(d),s._bx_Joint_SetSpring(e,5,1),s._bx_Joint_SetSphericalTarget(e,u.x,u.y,u.z,u.w)}}_applyMotor(t,e){let s=this._b3;switch(e.motor){case _.PhysicsConstraintMotorType.VELOCITY:s._bx_Joint_EnableSpring(t,0),s._bx_Joint_EnableMotor(t,1),s._bx_Joint_SetMotorSpeed(t,e.target),s._bx_Joint_SetMaxMotorForce(t,e.maxForce);break;case _.PhysicsConstraintMotorType.POSITION:s._bx_Joint_EnableMotor(t,0),s._bx_Joint_EnableSpring(t,1),s._bx_Joint_SetSpring(t,5,1),s._bx_Joint_SetTarget(t,e.target);break;default:s._bx_Joint_EnableMotor(t,0),s._bx_Joint_EnableSpring(t,0);break}}_applySixDofPlan(t,e,s,i){let n=this._b3,a=t.plan;switch(a.jointType){case 1:a.coneAngle>=0&&n._bx_Joint_SetLimits(e,-a.coneAngle,a.coneAngle),n._bx_Joint_EnableLimit(e,a.coneAngle>=0?1:0),n._bx_Joint_SetTwistLimits(e,a.twist?1:0,a.twist?a.twist[0]:0,a.twist?a.twist[1]:0);break;case 2:case 3:a.limit&&n._bx_Joint_SetLimits(e,a.limit[0],a.limit[1]),n._bx_Joint_EnableLimit(e,a.limit?1:0);break;case 4:if(a.spring){let o=this._springToHertz(a.stiffness,a.damping,!1,s,i);n._bx_Joint_EnableLimit(e,0),n._bx_Joint_EnableSpring(e,1),n._bx_Joint_SetSpring(e,o.hertz,o.dampingRatio),n._bx_Joint_SetTarget(e,t.length)}else n._bx_Joint_SetLimits(e,a.limit[0],a.limit[1]),n._bx_Joint_EnableLimit(e,1),n._bx_Joint_EnableSpring(e,1),n._bx_Joint_SetSpring(e,0,0);break;default:break}if(a.stiffness>0&&!a.spring){this._warnOnce("sixdof-softness","SIX_DOF: limit stiffness and damping are approximated with Box3D's per joint constraint softness, which applies to every axis of the joint.");let o=this._springToHertz(a.stiffness,a.damping,a.angularStiffness,s,i);n._bx_Joint_SetConstraintTuning(e,o.hertz,o.dampingRatio)}if(a.jointType===1)this._applySphericalMotor(t,e);else if(a.primaryAxis!==null&&a.jointType!==4){let o=t.axes.get(a.primaryAxis);o&&this._applyMotor(e,o)}}_setAxis(t,e,s){let i=t._pluginData;if(!i)return;let n=this._axisState(i,e),a={motor:n.motor,target:n.target,maxForce:n.maxForce};if(s(n),i.plan){let o=this._planSixDof(i),r=o.jointType!==i.plan.jointType||String(o.basis)!==String(i.plan.basis);i.plan=o,i.jointType=o.jointType,(a.motor!==n.motor||a.target!==n.target||a.maxForce!==n.maxForce)&&n.motor!==_.PhysicsConstraintMotorType.NONE&&!this._isPrimaryAxis(i,e)&&this._warnOnce(`axis-motor-${e}`,`SIX_DOF: a motor on axis ${e} has no effect with the Box3D joint this constraint maps to.`);let h=i.pairs[0];if(h&&this._updateJointLength(i,t.options.maxDistance,h.parentData,h.childData),r){h&&this._buildFrames(i,t,h.parentData,h.childData),this._recreateJoints(i);return}}else if(!this._isPrimaryAxis(i,e)){this._warnOnce(`axis-${i.type}-${e}`,`axis ${e} is not configurable on constraint type ${i.type} with Box3D; the value is stored but has no effect.`);return}for(let o=0;o<i.joints.length;o++)i.joints[o]&&this._applyAxisState(i,i.joints[o],i.pairs[o].parentData,i.pairs[o].childData)}setAxisFriction(t,e,s){let i=t._pluginData;i&&(this._axisState(i,e).friction=s)}getAxisFriction(t,e){return t._pluginData?.axes.get(e)?.friction??null}setAxisMode(t,e,s){this._setAxis(t,e,i=>i.mode=s)}getAxisMode(t,e){return t._pluginData?.axes.get(e)?.mode??null}setAxisMinLimit(t,e,s){this._setAxis(t,e,i=>{i.min=s,i.mode===_.PhysicsConstraintAxisLimitMode.FREE&&(i.mode=_.PhysicsConstraintAxisLimitMode.LIMITED)})}getAxisMinLimit(t,e){return t._pluginData?.axes.get(e)?.min??null}setAxisMaxLimit(t,e,s){this._setAxis(t,e,i=>{i.max=s,i.mode===_.PhysicsConstraintAxisLimitMode.FREE&&(i.mode=_.PhysicsConstraintAxisLimitMode.LIMITED)})}getAxisMaxLimit(t,e){return t._pluginData?.axes.get(e)?.max??null}setAxisMotorType(t,e,s){this._setAxis(t,e,i=>i.motor=s)}getAxisMotorType(t,e){return t._pluginData?.axes.get(e)?.motor??null}setAxisMotorTarget(t,e,s){this._setAxis(t,e,i=>i.target=s)}getAxisMotorTarget(t,e){return t._pluginData?.axes.get(e)?.target??null}setAxisMotorMaxForce(t,e,s){this._setAxis(t,e,i=>i.maxForce=s)}getAxisMotorMaxForce(t,e){return t._pluginData?.axes.get(e)?.maxForce??null}disposeConstraint(t){let e=t._pluginData;if(e){for(let s of e.joints)s&&this._destroyJoint(s);e.joints.length=0,e.pairs.length=0}}getBodiesUsingConstraint(t){let e=t._pluginData;return e?e.pairs.map(s=>({parentBody:s.parent,parentBodyIndex:s.parentIndex,childBody:s.child,childBodyIndex:s.childIndex})):[]}addConstraint(t,e,s,i,n){this.initConstraint(s,t,e,i,n)}explode(t,e,s,i=0){this._b3._bx_World_Explode(this._worldSlot,t.x,t.y,t.z,e,i,s)}setShapeFilterGroup(t,e){let s=t._pluginData;this._b3._bx_ShapeDesc_SetGroup(s.slot,e|0),this._syncShapeUsers(s,1)}setShapeRollingResistance(t,e){let s=t._pluginData;this._b3._bx_ShapeDesc_SetRollingResistance(s.slot,e),this._syncShapeUsers(s,2)}setAllowFastRotation(t,e,s){this._applyToBodyOrInstances(t,i=>this._b3._bx_Body_AllowFastRotation(i.slot,e?1:0),s)}createWheelJoint(t,e,s){let i=this._scratch(),n=(s.axisA??p.Vector3.UpReadOnly).normalizeToNew(),a=(s.axleA??p.Vector3.Forward()).normalizeToNew();a.subtractInPlace(n.scale(p.Vector3.Dot(n,a))).normalize(),this._frameToBuffer(i,0,s.pivotA,n,p.Vector3.Cross(a,n),a);let o=s.pivotB??p.Vector3.ZeroReadOnly;i[7]=o.x,i[8]=o.y,i[9]=o.z;let r=this._getPluginReference(t),l=this._getPluginReference(e);this._b3._bx_ComputeAlignedFrameB(r.slot,l.slot);let h=s.suspensionLimits??[-.2,.2],c=s.steeringLimits??[-Math.PI/4,Math.PI/4];i.set([1,s.suspensionHertz??4,s.suspensionDampingRatio??.7,1,h[0],h[1],s.enableSpinMotor?1:0,s.maxSpinTorque??5,0,s.enableSteering?1:0,s.steeringHertz??10,s.steeringDampingRatio??.7,0,s.maxSteeringTorque??5,1,c[0],c[1]],14);let b=this._b3._bx_CreateJoint(this._worldSlot,6,r.slot,l.slot,0,0);if(!b)throw new Error("Box3DPlugin: wheel joint creation failed");return new L(this,b)}createParallelJoint(t,e,s,i,n=1,a=1,o=34e37){let r=this._scratch(),l=s.normalizeToNew(),h=l.getNormalToRef(new p.Vector3);this._frameToBuffer(r,0,p.Vector3.ZeroReadOnly,h,p.Vector3.Cross(l,h),l);let c=i.normalizeToNew(),b=c.getNormalToRef(new p.Vector3);return this._frameToBuffer(r,7,p.Vector3.ZeroReadOnly,b,p.Vector3.Cross(c,b),c),r.set([n,a,o],14),this._b3._bx_CreateJoint(this._worldSlot,7,this._getPluginReference(t).slot,this._getPluginReference(e).slot,1,0)}disposeExtraJoint(t){this._destroyExtraJoint(t)}_destroyExtraJoint(t){t&&this._destroyJoint(t)}_wheelCall(t,e,...s){e&&this._b3[t](e,...s)}raycast(t,e,s,i){let n=Array.isArray(s)?s:[s];for(let u of n)u.reset(t,e);let a=(i?.membership??-1)>>>0,o=(i?.collideWith??-1)>>>0,r=i?.ignoreBody?i.ignoreBody._pluginData?.slot??0:0,l=n.length===1,h=this._b3._bx_World_CastRay(this._worldSlot,t.x,t.y,t.z,e.x-t.x,e.y-t.y,e.z-t.z,a,o,r,i?.shouldHitTriggers?1:0,l?1:0);if(h<=0)return;let c=new Float32Array(this._b3.HEAPF32.buffer,this._b3._bx_RayHitsPtr(),h*J),b=[];for(let u=0;u<h;u++)b.push(u);if(b.sort((u,d)=>c[u*J+6]-c[d*J+6]),!n.length)for(let u=0;u<h;u++){let d=new Dt.PhysicsRaycastResult;d.reset(t,e),n.push(d)}let x=Math.min(h,n.length);for(let u=0;u<x;u++){let d=b[u]*J,g=n[u],D=this._bodies[c[d+7]];g.body=D?.body,g.bodyIndex=D?.index,g.shape=D?.body.shape??void 0,g.setHitData({x:c[d+3],y:c[d+4],z:c[d+5]},{x:c[d],y:c[d+1],z:c[d+2]},c[d+9]),g.calculateHitDistance()}}_readEvents(t,e,s,i){let n=new Float32Array(this._b3.HEAPF32.buffer,t,e*s).slice(),a=new Array(e*2);for(let o=0;o<e;o++)a[o*2]=this._bodies[n[o*s+i]],a[o*2+1]=this._bodies[n[o*s+i+1]];return{records:n,refs:a}}_notifyCollisions(){let t=this._b3._bx_World_GetContactEvents(this._worldSlot);if(!t)return;let{records:e,refs:s}=this._readEvents(this._b3._bx_ContactEventsPtr(),t,mt,3);for(let i=0;i<t;i++){let n=i*mt,a=e[n],o=s[i*2],r=s[i*2+1];if(!o||!r||!o.data.slot||!r.data.slot)continue;let l=a===0?1:a===1?4:2;if(((o.data.eventMask|r.data.eventMask)&l)===0)continue;if(a===1){let u={collider:o.body,colliderIndex:o.index,collidedAgainst:r.body,collidedAgainstIndex:r.index,type:_.PhysicsEventType.COLLISION_FINISHED};this.onCollisionEndedObservable.notifyObservers(u),this._bodyCollisionEndedObservable.get(o.data.slot)?.notifyObservers(u);let d=this._bodyCollisionEndedObservable.get(r.data.slot);d&&d.notifyObservers({...u,collider:r.body,colliderIndex:r.index,collidedAgainst:o.body,collidedAgainstIndex:o.index});continue}let h=a===0?_.PhysicsEventType.COLLISION_STARTED:_.PhysicsEventType.COLLISION_CONTINUED,c=new p.Vector3(e[n+5],e[n+6],e[n+7]),b=new p.Vector3(e[n+8],e[n+9],e[n+10]),x={collider:o.body,colliderIndex:o.index,collidedAgainst:r.body,collidedAgainstIndex:r.index,type:h,point:c,normal:b,distance:0,impulse:e[n+11]};if(this.onCollisionObservable.notifyObservers(x),this._bodyCollisionObservable.size){this._bodyCollisionObservable.get(o.data.slot)?.notifyObservers(x);let u=this._bodyCollisionObservable.get(r.data.slot);u&&u.notifyObservers({...x,collider:r.body,colliderIndex:r.index,collidedAgainst:o.body,collidedAgainstIndex:o.index,normal:b?b.negate():null})}}}_notifyTriggers(){let t=this._b3._bx_World_GetSensorEvents(this._worldSlot);if(!t||!this.onTriggerCollisionObservable.hasObservers())return;let{records:e,refs:s}=this._readEvents(this._b3._bx_SensorEventsPtr(),t,ft,3);for(let i=0;i<t;i++){let n=i*ft,a=s[i*2],o=s[i*2+1];!a||!o||!a.data.slot||!o.data.slot||this.onTriggerCollisionObservable.notifyObservers({collider:a.body,colliderIndex:a.index,collidedAgainst:o.body,collidedAgainstIndex:o.index,type:e[n]===0?_.PhysicsEventType.TRIGGER_ENTERED:_.PhysicsEventType.TRIGGER_EXITED})}}dispose(){this._motorJoints.clear(),this.onCollisionObservable.clear(),this.onCollisionEndedObservable.clear(),this.onTriggerCollisionObservable.clear(),this._bodyCollisionObservable.clear(),this._bodyCollisionEndedObservable.clear();for(let t of this._shapes)t&&this._b3._bx_ShapeDesc_Destroy(t.slot);this._shapes.length=0,this._bodies.length=0,this._worldSlot&&(this._b3._bx_DestroyWorld(this._worldSlot),this._worldSlot=0)}};if(typeof Box3D>"u")throw new Error("babylon.box3d.js needs lib/umd/box3d.umd.js loaded first");var St=Box3D;return Ot(zt);})();
|
|
1
|
+
/* babylon-box3d 0.3.0 | MIT | https://github.com/Pryme8/babylon-box3d */
|
|
2
|
+
"use strict";var BABYLONBOX3D=(()=>{var vt=Object.create;var G=Object.defineProperty;var Tt=Object.getOwnPropertyDescriptor;var It=Object.getOwnPropertyNames;var Mt=Object.getPrototypeOf,Et=Object.prototype.hasOwnProperty;var B=(m,t)=>()=>(t||m((t={exports:{}}).exports,t),t.exports),Lt=(m,t)=>{for(var e in t)G(m,e,{get:t[e],enumerable:!0})},K=(m,t,e,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of It(t))!Et.call(m,i)&&i!==e&&G(m,i,{get:()=>t[i],enumerable:!(s=Tt(t,i))||s.enumerable});return m};var A=(m,t,e)=>(e=m!=null?vt(Mt(m)):{},K(t||!m||!m.__esModule?G(e,"default",{value:m,enumerable:!0}):e,m)),wt=m=>K(G({},"__esModule",{value:!0}),m);var et=B((Jt,tt)=>{if(typeof BABYLON>"u")throw new Error("babylon.box3d.js needs babylon.js loaded first");tt.exports=BABYLON});var it=B((Vt,st)=>{if(typeof BABYLON>"u")throw new Error("babylon.box3d.js needs babylon.js loaded first");st.exports=BABYLON});var ot=B((kt,nt)=>{if(typeof BABYLON>"u")throw new Error("babylon.box3d.js needs babylon.js loaded first");nt.exports=BABYLON});var rt=B((Ut,at)=>{if(typeof BABYLON>"u")throw new Error("babylon.box3d.js needs babylon.js loaded first");at.exports=BABYLON});var ct=B((Wt,lt)=>{if(typeof BABYLON>"u")throw new Error("babylon.box3d.js needs babylon.js loaded first");lt.exports=BABYLON});var ht=B((Ht,_t)=>{if(typeof BABYLON>"u")throw new Error("babylon.box3d.js needs babylon.js loaded first");_t.exports=BABYLON});var bt=B((Yt,pt)=>{if(typeof BABYLON>"u")throw new Error("babylon.box3d.js needs babylon.js loaded first");pt.exports=BABYLON});var ut=B((jt,dt)=>{if(typeof BABYLON>"u")throw new Error("babylon.box3d.js needs babylon.js loaded first");dt.exports=BABYLON});var xt=B((Qt,yt)=>{if(typeof BABYLON>"u")throw new Error("babylon.box3d.js needs babylon.js loaded first");yt.exports=BABYLON});var zt={};Lt(zt,{BOX3D_HEIGHT_FIELD_HOLE:()=>Dt,Box3D:()=>St,Box3DPlugin:()=>V,Box3DWheelJoint:()=>L});var p=A(et(),1),_=A(it(),1),gt=A(ot(),1),I=A(rt(),1),U=A(ct(),1),W=A(ht(),1),H=A(bt(),1),q=A(ut(),1),M=A(xt(),1),Dt=255,Y=class{constructor(t){this.slot=t;this.userMassProps={};this.angularLocks=0;this.eventMask=0;this.motionType=_.PhysicsMotionType.STATIC;this.activation=_.PhysicsActivationControl.SIMULATION_CONTROLLED}},j=class{constructor(t,e){this.slot=t;this.type=e;this.material={};this.users=new Set;this.children=[];this.parents=new Set}},Q=class{constructor(){this.joints=[];this.pairs=[];this.type=_.PhysicsConstraintType.LOCK;this.jointType=0;this.enabled=!0;this.collisions=!1;this.frames=new Float32Array(14);this.length=0;this.axes=new Map;this.plan=null}};var L=class{constructor(t,e){this._plugin=t;this._slot=e}setSpinSpeed(t){this._plugin._wheelCall("_bx_WheelJoint_SetSpinMotorSpeed",this._slot,t)}setMaxSpinTorque(t){this._plugin._wheelCall("_bx_WheelJoint_SetMaxSpinTorque",this._slot,t)}enableSpinMotor(t){this._plugin._wheelCall("_bx_WheelJoint_EnableSpinMotor",this._slot,t?1:0)}setSteeringAngle(t){this._plugin._wheelCall("_bx_WheelJoint_SetTargetSteeringAngle",this._slot,t)}setSuspension(t,e){this._plugin._wheelCall("_bx_WheelJoint_SetSuspension",this._slot,t,e)}dispose(){this._plugin._destroyExtraJoint(this._slot),this._slot=0}};var Ot=1e5;function Rt(m,t,e){let s=[[m[0],m[3],m[4]],[m[3],m[1],m[5]],[m[4],m[5],m[2]]];e.set(0,0,0,1),t.set(m[0],m[1],m[2]);let i=[0,0,0,1];for(let o=0;o<24;o++){let[a,n,r,l]=i,h=[[1-2*(n*n+r*r),2*(a*n-r*l),2*(a*r+n*l)],[2*(a*n+r*l),1-2*(a*a+r*r),2*(n*r-a*l)],[2*(a*r-n*l),2*(n*r+a*l),1-2*(a*a+n*n)]],c=[0,1,2].map(E=>[0,1,2].map(At=>[0,1,2].reduce((Pt,Z)=>Pt+h[Z][E]*[0,1,2].reduce((Ct,$)=>Ct+s[Z][$]*h[$][At],0),0)));t.set(c[0][0],c[1][1],c[2][2]),e.set(i[0],i[1],i[2],i[3]);let u=[Math.abs(c[1][2]),Math.abs(c[0][2]),Math.abs(c[0][1])],x=u.indexOf(Math.max(...u)),d=(x+1)%3,b=(x+2)%3,D=c[d][b],g=Math.abs(c[0][0])+Math.abs(c[1][1])+Math.abs(c[2][2])+1e-30;if(Math.abs(D)<=1e-9*g)return;let y=(c[b][b]-c[d][d])/(2*D),f=y>0?1:-1;y*=f;let S=f/(y+(y<1e6?Math.sqrt(y*y+1):y)),P=1/Math.sqrt(S*S+1);if(P===1)return;let C=[0,0,0,0];C[x]=-f*Math.sqrt((1-P)/2),C[3]=Math.sqrt(1-C[x]*C[x]);let[v,T,w,O]=i,[R,N,F,z]=C;i[0]=O*R+v*z+T*F-w*N,i[1]=O*N-v*F+T*z+w*R,i[2]=O*F+v*N-T*R+w*z,i[3]=O*z-v*R-T*N-w*F;let Bt=Math.hypot(i[0],i[1],i[2],i[3])||1;for(let E=0;E<4;E++)i[E]/=Bt}}function Nt(m,t){let{x:e,y:s,z:i,w:o}=t,a=[[1-2*(s*s+i*i),2*(e*s-i*o),2*(e*i+s*o)],[2*(e*s+i*o),1-2*(e*e+i*i),2*(s*i-e*o)],[2*(e*i-s*o),2*(s*i+e*o),1-2*(e*e+s*s)]],n=(r,l)=>a[r][0]*m[0]*a[l][0]+a[r][1]*m[1]*a[l][1]+a[r][2]*m[2]*a[l][2];return[n(0,0),n(1,1),n(2,2),n(0,1),n(0,2),n(1,2)]}var k=9,mt=12,ft=5,J=11,X=class{constructor(t,e){this._collectIndices=t;this._flipWinding=e;this._vertices=[];this._indices=[]}addNodeMeshes(t,e){t.computeWorldMatrix(!0);let s=p.TmpVectors.Matrix[0];if(p.Matrix.ScalingToRef(t.absoluteScaling.x,t.absoluteScaling.y,t.absoluteScaling.z,s),t instanceof W.Mesh?this._addMesh(t,s):t instanceof H.InstancedMesh&&this._addMesh(t.sourceMesh,s),e){let i=p.TmpVectors.Matrix[1];t.computeWorldMatrix().invertToRef(i);let o=p.TmpVectors.Matrix[2];i.multiplyToRef(s,o);let a=t.getChildMeshes(!1).filter(n=>!n.physicsBody);for(let n of a){let r=n.computeWorldMatrix(),l=p.TmpVectors.Matrix[3];r.multiplyToRef(o,l),n instanceof W.Mesh?this._addMesh(n,l):n instanceof H.InstancedMesh&&this._addMesh(n.sourceMesh,l)}}}_addMesh(t,e){let s=t.getVerticesData(q.VertexBuffer.PositionKind)||[],i=s.length/3,o=this._vertices.length/3,a=p.TmpVectors.Vector3[0];for(let n=0;n<i;n++)a.set(s[n*3+0],s[n*3+1],s[n*3+2]),p.Vector3.TransformCoordinatesToRef(a,e,a),this._vertices.push(a.x,a.y,a.z);if(this._collectIndices){let n=t.getIndices();if(n)for(let r=0;r<n.length;r+=3)this._flipWinding?this._indices.push(n[r+2]+o,n[r+1]+o,n[r+0]+o):this._indices.push(n[r+0]+o,n[r+1]+o,n[r+2]+o)}}get vertexCount(){return this._vertices.length/3}get triangleCount(){return this._indices.length/3}get vertices(){return this._vertices}get indices(){return this._indices}},Ft=["_bx_Body_AllowFastRotation","_bx_Body_ApplyAngularImpulse","_bx_Body_ApplyForce","_bx_Body_ApplyLinearImpulse","_bx_Body_ApplyMassFromShapes","_bx_Body_ApplyTorque","_bx_Body_ComputeShapeMassData","_bx_Body_EnableSleep","_bx_Body_GetAABB","_bx_Body_GetAngularDamping","_bx_Body_GetAngularVelocity","_bx_Body_GetGravityScale","_bx_Body_GetLinearDamping","_bx_Body_GetLinearVelocity","_bx_Body_GetMassData","_bx_Body_GetTransform","_bx_Body_SetAngularDamping","_bx_Body_SetAngularVelocity","_bx_Body_SetAwake","_bx_Body_SetEventFlags","_bx_Body_SetGravityScale","_bx_Body_SetLinearDamping","_bx_Body_SetLinearVelocity","_bx_Body_SetMassDataFull","_bx_Body_SetMotionLocks","_bx_Body_SetShape","_bx_Body_SetTargetTransform","_bx_Body_SetTransform","_bx_Body_SetType","_bx_Body_SyncShapeDesc","_bx_ComputeAlignedFrameB","_bx_ContactEventsPtr","_bx_CreateBody","_bx_CreateJoint","_bx_CreateWorld","_bx_DebugIndexCount","_bx_DebugIndicesPtr","_bx_DebugPositionsPtr","_bx_DestroyBody","_bx_DestroyJoint","_bx_DestroyWorld","_bx_GetRejectedSlotCount","_bx_Joint_EnableLimit","_bx_Joint_EnableMotor","_bx_Joint_EnableSpring","_bx_Joint_IsValid","_bx_Joint_SetCollideConnected","_bx_Joint_SetConstraintTuning","_bx_Joint_SetLimits","_bx_Joint_SetMaxMotorForce","_bx_Joint_SetMotorSpeed","_bx_Joint_SetSphericalMotor","_bx_Joint_SetSphericalTarget","_bx_Joint_SetSpring","_bx_Joint_SetTarget","_bx_Joint_SetTwistLimits","_bx_Joint_UpdateMotorFrame","_bx_Joint_WakeBodies","_bx_MoveEventsPtr","_bx_RayHitsPtr","_bx_Scratch","_bx_SensorEventsPtr","_bx_ShapeDesc_AddChild","_bx_ShapeDesc_BuildDebugGeometry","_bx_ShapeDesc_CreateBox","_bx_ShapeDesc_CreateCapsule","_bx_ShapeDesc_CreateContainer","_bx_ShapeDesc_CreateCylinder","_bx_ShapeDesc_CreateHeightField","_bx_ShapeDesc_CreateHull","_bx_ShapeDesc_CreateMesh","_bx_ShapeDesc_CreateSphere","_bx_ShapeDesc_Destroy","_bx_ShapeDesc_GetAABB","_bx_ShapeDesc_GetCategoryBits","_bx_ShapeDesc_GetChildCount","_bx_ShapeDesc_GetDensity","_bx_ShapeDesc_GetFriction","_bx_ShapeDesc_GetMaskBits","_bx_ShapeDesc_GetRestitution","_bx_ShapeDesc_RemoveChild","_bx_ShapeDesc_SetDensity","_bx_ShapeDesc_SetFilter","_bx_ShapeDesc_SetGroup","_bx_ShapeDesc_SetMaterial","_bx_ShapeDesc_SetRollingResistance","_bx_ShapeDesc_SetSensor","_bx_World_CastRay","_bx_World_EnableContinuous","_bx_World_EnableSleeping","_bx_World_Explode","_bx_World_GetContactEvents","_bx_World_GetMaximumLinearSpeed","_bx_World_GetMoveEvents","_bx_World_GetSensorEvents","_bx_World_GetStats","_bx_World_SetGravity","_bx_World_SetMaximumLinearSpeed","_bx_World_Step"],V=class{constructor(t=!0,e){this._useDeltaForWorldStep=t;this.name="Box3DPlugin";this.subStepCount=4;this.lastStepTimeMs=0;this._worldSlot=0;this._fixedTimeStep=1/60;this._timeStep=1/60;this._maxAngularVelocity=4*Math.PI*10;this._bodies=[];this._shapes=[];this._bodyCollisionObservable=new Map;this._bodyCollisionEndedObservable=new Map;this._touchedInstanceMeshes=new Set;this._motorJoints=new Set;this._tmpVec3=[new p.Vector3,new p.Vector3,new p.Vector3,new p.Vector3];this._tmpQuat=[new p.Quaternion,new p.Quaternion,new p.Quaternion];this._warned=new Set;this._rejectedSlots=0;this.onCollisionObservable=new M.Observable;this.onCollisionEndedObservable=new M.Observable;this.onTriggerCollisionObservable=new M.Observable;if(!e)throw new Error("Box3D module is required: pass the awaited result of the Box3D module factory.");let s=Ft.filter(i=>typeof e[i]!="function");if(s.length>0)throw new Error(`Box3DPlugin: the Box3D module is missing ${s.length} entry point${s.length===1?"":"s"} this build needs (${s.slice(0,3).join(", ")}${s.length>3?", ...":""}). box3d.js and box3d.wasm have to come from the same build of this package: check what locateFile points at and clear any copied or cached box3d.wasm.`);if(this._b3=e,this.world=e,this._worldSlot=this._b3._bx_CreateWorld(0,-9.81,0),!this._worldSlot)throw new Error("Box3D: could not create a world (max worlds reached?)")}_scratch(){return new Float32Array(this._b3.HEAPF32.buffer,this._b3._bx_Scratch(),64)}_warnOnce(t,e){this._warned.has(t)||(this._warned.add(t),I.Logger.Warn(`Box3DPlugin: ${e}`))}_getPluginReference(t,e){return t._pluginDataInstances?.length?t._pluginDataInstances[e??0]:t._pluginData}_applyToBodyOrInstances(t,e,s){if(t._pluginDataInstances?.length>0&&s===void 0)for(let i of t._pluginDataInstances)e(i);else e(this._getPluginReference(t,s))}_motionTypeToNative(t){switch(t){case _.PhysicsMotionType.STATIC:return 0;case _.PhysicsMotionType.ANIMATED:return 1;default:return 2}}_nodeTransformToRef(t,e,s){if(t.parent){t.computeWorldMatrix(!0),e.copyFrom(t.absolutePosition),s.copyFrom(t.absoluteRotationQuaternion);return}e.copyFrom(t.position),t.rotationQuaternion?s.copyFrom(t.rotationQuaternion):p.Quaternion.FromEulerAnglesToRef(t.rotation.x,t.rotation.y,t.rotation.z,s)}setGravity(t){this._b3._bx_World_SetGravity(this._worldSlot,t.x,t.y,t.z)}setTimeStep(t){this._fixedTimeStep=t}getTimeStep(){return this._fixedTimeStep}executeStep(t,e){for(let n of e)n.disablePreStep||this.setPhysicsBodyTransformation(n,n.transformNode);for(let n of this._motorJoints)this._b3._bx_Joint_IsValid(n)?this._b3._bx_Joint_UpdateMotorFrame(n):this._motorJoints.delete(n);let s=this._useDeltaForWorldStep?t:this._fixedTimeStep;this._timeStep=s;let i=performance.now();this._b3._bx_World_Step(this._worldSlot,s,this.subStepCount);let o=this._b3._bx_World_GetMoveEvents(this._worldSlot);if(o>0){let n=this._b3._bx_MoveEventsPtr(),r=new Float32Array(this._b3.HEAPF32.buffer,n,o*k);for(let l=0;l<o;l++){r.buffer!==this._b3.HEAPF32.buffer&&(r=new Float32Array(this._b3.HEAPF32.buffer,n,o*k));let h=l*k,c=this._bodies[r[h]];!c||c.body.disableSync||this._syncBodyFromBuffer(c.body,c.index,r,h+1)}for(let l of this._touchedInstanceMeshes)l.thinInstanceBufferUpdated("matrix");this._touchedInstanceMeshes.clear()}this._notifyCollisions(),this._notifyTriggers(),this.lastStepTimeMs=performance.now()-i;let a=this._b3._bx_GetRejectedSlotCount();a>this._rejectedSlots&&(this._rejectedSlots=a,this._warnOnce("rejected-slots",`the native module did not recognize ${a} of the handles it was given, and those calls did nothing. Something is using a body, shape or joint after it was destroyed, or box3d.js and box3d.wasm are from different builds.`))}getPluginVersion(){return 2}setVelocityLimits(t,e){this._b3._bx_World_SetMaximumLinearSpeed(this._worldSlot,t),this._maxAngularVelocity=e}getMaxLinearVelocity(){return this._b3._bx_World_GetMaximumLinearSpeed(this._worldSlot)}getMaxAngularVelocity(){return this._maxAngularVelocity}getStats(){this._b3._bx_World_GetStats(this._worldSlot);let t=this._scratch();return{bodies:t[0],shapes:t[1],contacts:t[2],joints:t[3],islands:t[4],stepMs:t[5],collideMs:t[6],solveMs:t[7]}}setSleepingEnabled(t){this._b3._bx_World_EnableSleeping(this._worldSlot,t?1:0)}setContinuousEnabled(t){this._b3._bx_World_EnableContinuous(this._worldSlot,t?1:0)}_createNativeBody(t,e,s,i){let o=this._b3._bx_CreateBody(this._worldSlot,this._motionTypeToNative(t),e.x,e.y,e.z,s.x,s.y,s.z,s.w,i?0:1),a=new Y(o);return a.motionType=t,a}initBody(t,e,s,i){let o=this._createNativeBody(e,s,i,t.startAsleep);t._pluginData=o,this._bodies[o.slot]={body:t,index:0,data:o}}initBodyInstances(t,e,s){let i=s._thinInstanceDataStorage?.instancesCount??0,o=s._thinInstanceDataStorage?.matrixData;if(!o)return;let a=t.shape?._pluginData;i>8&&a&&this._bakesMeshCopies(a)&&this._warnOnce("instanced-mesh-child","a mesh inside a container has no local transform in Box3D, so every instance gets its own baked copy of the mesh data; put the mesh on the body directly, or bake the offset into the mesh, to share it."),this._createOrUpdateBodyInstances(t,e,o,0,i,!1)}_createOrUpdateBodyInstances(t,e,s,i,o,a){let n=p.TmpVectors.Quaternion[0],r=p.TmpVectors.Matrix[0],l=p.TmpVectors.Vector3[0];for(let h=i;h<o;h++)if(l.set(s[h*16+12],s[h*16+13],s[h*16+14]),p.Matrix.FromArrayToRef(s,h*16,r),r.decompose(void 0,n,void 0),a){let c=t._pluginDataInstances[h];this._b3._bx_Body_SetTransform(c.slot,l.x,l.y,l.z,n.x,n.y,n.z,n.w)}else{let c=this._createNativeBody(e,l,n,t.startAsleep),u=t._pluginDataInstances[0];u&&(c.userMassProps=u.userMassProps,c.eventMask=u.eventMask,this._applyEventFlags(c),u.activation!==_.PhysicsActivationControl.SIMULATION_CONTROLLED&&(c.activation=u.activation,this._applyNativeMotionType(c),this._b3._bx_Body_EnableSleep(c.slot,u.activation===_.PhysicsActivationControl.ALWAYS_ACTIVE?0:1))),t._pluginDataInstances.push(c),this._bodies[c.slot]={body:t,index:h,data:c}}}updateBodyInstances(t,e){let s=e._thinInstanceDataStorage?.instancesCount??0,i=e._thinInstanceDataStorage?.matrixData;if(!i)return;let o=t._pluginDataInstances.length,a=this.getMotionType(t);if(s>o){this._createOrUpdateBodyInstances(t,a,i,o,s,!1);let n=t.shape;if(n&&n._pluginData)for(let r=o;r<s;r++)this._setBodyShape(t._pluginDataInstances[r],n._pluginData)}else if(s<o){let n=o-s;for(let r=0;r<n;r++){let l=t._pluginDataInstances.pop();this._destroyBodyData(l)}this._createOrUpdateBodyInstances(t,a,i,0,s,!0)}}_destroyBodyData(t){if(t){for(let e of this._shapes)e?.users.delete(t);this._bodyCollisionObservable.delete(t.slot),this._bodyCollisionEndedObservable.delete(t.slot),this._bodies[t.slot]=void 0,this._b3._bx_DestroyBody(t.slot),t.slot=0}}removeBody(t){if(t._pluginDataInstances?.length)for(let e of t._pluginDataInstances)this._destroyBodyData(e);t._pluginData&&this._destroyBodyData(t._pluginData)}sync(t){this.syncTransform(t,t.transformNode)}_syncBodyFromBuffer(t,e,s,i){let o=t.transformNode;if(t._pluginDataInstances.length){let a=o,n=a._thinInstanceDataStorage?.matrixData;if(!n)return;let r=this._tmpVec3[0],l=e*16;r.set(Math.hypot(n[l],n[l+1],n[l+2]),Math.hypot(n[l+4],n[l+5],n[l+6]),Math.hypot(n[l+8],n[l+9],n[l+10]));let h=this._tmpQuat[0];h.set(s[i+3],s[i+4],s[i+5],s[i+6]);let c=this._tmpVec3[1];c.set(s[i],s[i+1],s[i+2]);let u=p.TmpVectors.Matrix[0];p.Matrix.ComposeToRef(r,h,c,u),u.copyToArray(n,l),this._touchedInstanceMeshes.add(a);return}this._applyTransformToNode(o,s[i],s[i+1],s[i+2],s[i+3],s[i+4],s[i+5],s[i+6])}_applyTransformToNode(t,e,s,i,o,a,n,r){let l=this._tmpQuat[0];l.set(o,a,n,r);let h=t.parent;if(h&&!h.getWorldMatrix().isIdentity()){h.computeWorldMatrix(!0);let c=this._tmpVec3[2];c.copyFrom(t.scaling);let u=p.TmpVectors.Matrix[0],x=this._tmpVec3[3];x.set(e,s,i),p.Matrix.ComposeToRef(t.absoluteScaling,l,x,u);let d=p.TmpVectors.Matrix[1];h.getWorldMatrix().invertToRef(d);let b=p.TmpVectors.Matrix[2];u.multiplyToRef(d,b),b.decomposeToTransformNode(t),t.rotationQuaternion?.normalize(),t.scaling.copyFrom(c)}else t.position.set(e,s,i),t.rotationQuaternion?t.rotationQuaternion.copyFrom(l):l.toEulerAnglesToRef(t.rotation)}syncTransform(t,e){if(t._pluginDataInstances.length){let o=e;if(!o._thinInstanceDataStorage?.matrixData)return;for(let n=0;n<t._pluginDataInstances.length;n++){let r=t._pluginDataInstances[n];this._b3._bx_Body_GetTransform(r.slot),this._syncBodyFromBuffer(t,n,this._scratch(),0)}o.thinInstanceBufferUpdated("matrix"),this._touchedInstanceMeshes.delete(o);return}let s=t._pluginData;if(!s||!s.slot)return;this._b3._bx_Body_GetTransform(s.slot);let i=this._scratch();this._applyTransformToNode(e,i[0],i[1],i[2],i[3],i[4],i[5],i[6])}setPhysicsBodyTransformation(t,e){let s=t.getPrestepType();if(s==_.PhysicsPrestepType.TELEPORT){if(t._pluginDataInstances.length>0){let r=e._thinInstanceDataStorage?.matrixData;if(!r)return;this._createOrUpdateBodyInstances(t,t.getMotionType(),r,0,t._pluginDataInstances.length,!0);return}let i=t._pluginData,o=this._tmpVec3[0],a=this._tmpQuat[0];this._nodeTransformToRef(e,o,a),this._b3._bx_Body_SetTransform(i.slot,o.x,o.y,o.z,a.x,a.y,a.z,a.w)}else s==_.PhysicsPrestepType.ACTION?this.setTargetTransform(t,e.absolutePosition,e.absoluteRotationQuaternion):s==_.PhysicsPrestepType.DISABLED&&I.Logger.Warn("Prestep type is set to DISABLED. Unable to set physics body transformation.")}setTargetTransform(t,e,s,i){this._applyToBodyOrInstances(t,o=>{this._b3._bx_Body_SetTargetTransform(o.slot,e.x,e.y,e.z,s.x,s.y,s.z,s.w,this._timeStep)},i)}_bakesMeshCopies(t,e=new Set){return e.has(t)?!1:(e.add(t),t.children.some(s=>s.type===_.PhysicsShapeType.MESH||this._bakesMeshCopies(s,e)))}_setBodyShape(t,e){for(let s of this._shapes)s?.users.delete(t);this._b3._bx_Body_SetShape(t.slot,e?e.slot:0),e&&e.users.add(t),this._applyEventFlags(t),this._internalUpdateMassProperties(t)}setShape(t,e){let s=e&&e._pluginData?e._pluginData:null;this._applyToBodyOrInstances(t,i=>this._setBodyShape(i,s))}getShape(t){return t.shape??null}getShapeType(t){return t.type??t._pluginData?.type??_.PhysicsShapeType.CONTAINER}_applyEventFlags(t){let e=t.eventMask&5?1:0,s=t.eventMask&2?1:0;this._b3._bx_Body_SetEventFlags(t.slot,e,s)}setEventMask(t,e,s){this._applyToBodyOrInstances(t,i=>{i.eventMask=e,this._applyEventFlags(i)},s)}getEventMask(t,e){return this._getPluginReference(t,e).eventMask}setMotionType(t,e,s){this._applyToBodyOrInstances(t,i=>{i.motionType=e,this._applyNativeMotionType(i),this._internalUpdateMassProperties(i)},s)}_applyNativeMotionType(t){let e=t.activation===_.PhysicsActivationControl.ALWAYS_INACTIVE?0:this._motionTypeToNative(t.motionType);this._b3._bx_Body_SetType(t.slot,e)}setActivationControl(t,e,s){this._applyToBodyOrInstances(t,i=>{if(i.activation===e)return;let o=i.activation===_.PhysicsActivationControl.ALWAYS_INACTIVE;if(i.activation=e,e===_.PhysicsActivationControl.ALWAYS_INACTIVE){this._b3._bx_Body_SetLinearVelocity(i.slot,0,0,0),this._b3._bx_Body_SetAngularVelocity(i.slot,0,0,0),this._applyNativeMotionType(i);return}this._applyNativeMotionType(i),this._internalUpdateMassProperties(i),this._b3._bx_Body_EnableSleep(i.slot,e===_.PhysicsActivationControl.ALWAYS_ACTIVE?0:1),o&&e===_.PhysicsActivationControl.SIMULATION_CONTROLLED&&this._b3._bx_Body_SetAwake(i.slot,0)},s)}getMotionType(t,e){return this._getPluginReference(t,e).motionType}_readMassData(t){t.motionType===_.PhysicsMotionType.DYNAMIC&&t.activation!==_.PhysicsActivationControl.ALWAYS_INACTIVE?this._b3._bx_Body_GetMassData(t.slot):this._b3._bx_Body_ComputeShapeMassData(t.slot);let e=this._scratch(),s=e[0],i=new p.Vector3,o=new p.Quaternion;return Rt([e[4],e[5],e[6],e[7],e[8],e[9]],i,o),s>0&&i.scaleInPlace(1/s),{mass:s,centerOfMass:new p.Vector3(e[1],e[2],e[3]),inertia:i,inertiaOrientation:o}}_resolveMassProperties(t){let e=this._readMassData(t),s=t.userMassProps??{};return{mass:s.mass!==void 0&&s.mass>0?s.mass:e.mass,centerOfMass:s.centerOfMass??e.centerOfMass,inertia:s.inertia??e.inertia,inertiaOrientation:s.inertiaOrientation??(s.inertia?p.Quaternion.Identity():e.inertiaOrientation)}}_internalUpdateMassProperties(t){if(t.motionType!==_.PhysicsMotionType.DYNAMIC||t.activation===_.PhysicsActivationControl.ALWAYS_INACTIVE){this._applyMotionLocks(t,!1,!1,!1);return}this._b3._bx_Body_ApplyMassFromShapes(t.slot);let e=t.userMassProps;if(!(!!e&&(e.mass!==void 0||!!e.centerOfMass||!!e.inertia||!!e.inertiaOrientation))){this._applyMotionLocks(t,!1,!1,!1);return}let i=this._resolveMassProperties(t),o=i.mass>0?i.mass:1,a=i.inertia,n=i.inertiaOrientation,r=[a.x<=0,a.y<=0,a.z<=0],l=Math.max(a.x,a.y,a.z,1e-4)*Ot,h=[r[0]?l:a.x,r[1]?l:a.y,r[2]?l:a.z].map(u=>u*o),c=Nt(h,n);this._b3._bx_Body_SetMassDataFull(t.slot,o,i.centerOfMass.x,i.centerOfMass.y,i.centerOfMass.z,c[0],c[1],c[2],c[3],c[4],c[5]),this._applyLockedAxes(t,r,n)}_applyLockedAxes(t,e,s){let i=e.filter(Boolean).length;if(i===3){this._applyMotionLocks(t,!0,!0,!0);return}if(i!==2){this._applyMotionLocks(t,!1,!1,!1);return}this._b3._bx_Body_GetTransform(t.slot);let o=this._scratch(),a=this._tmpQuat[0].set(o[3],o[4],o[5],o[6]),n=e.indexOf(!1),r=this._tmpVec3[0].set(n===0?1:0,n===1?1:0,n===2?1:0);r.applyRotationQuaternionInPlace(s).applyRotationQuaternionInPlace(a);let l=[Math.abs(r.x),Math.abs(r.y),Math.abs(r.z)],h=l.indexOf(Math.max(...l));l[h]>.9999?this._applyMotionLocks(t,h!==0,h!==1,h!==2):this._applyMotionLocks(t,!1,!1,!1)}_applyMotionLocks(t,e,s,i){let o=(e?1:0)|(s?2:0)|(i?4:0);o!==t.angularLocks&&(t.angularLocks=o,this._b3._bx_Body_SetMotionLocks(t.slot,0,0,0,e?1:0,s?1:0,i?1:0))}computeMassProperties(t,e){let s=this._getPluginReference(t,e);this._b3._bx_Body_ApplyMassFromShapes(s.slot);let i=this._readMassData(s);return this._internalUpdateMassProperties(s),i}setMassProperties(t,e,s){this._applyToBodyOrInstances(t,i=>{i.userMassProps=e,this._internalUpdateMassProperties(i)},s)}getMassProperties(t,e){return this._resolveMassProperties(this._getPluginReference(t,e))}setLinearDamping(t,e,s){this._applyToBodyOrInstances(t,i=>this._b3._bx_Body_SetLinearDamping(i.slot,e),s)}getLinearDamping(t,e){return this._b3._bx_Body_GetLinearDamping(this._getPluginReference(t,e).slot)}setAngularDamping(t,e,s){this._applyToBodyOrInstances(t,i=>this._b3._bx_Body_SetAngularDamping(i.slot,e),s)}getAngularDamping(t,e){return this._b3._bx_Body_GetAngularDamping(this._getPluginReference(t,e).slot)}setLinearVelocity(t,e,s){this._applyToBodyOrInstances(t,i=>this._b3._bx_Body_SetLinearVelocity(i.slot,e.x,e.y,e.z),s)}getLinearVelocityToRef(t,e,s){this._b3._bx_Body_GetLinearVelocity(this._getPluginReference(t,s).slot);let i=this._scratch();e.set(i[0],i[1],i[2])}applyImpulse(t,e,s,i){this._applyToBodyOrInstances(t,o=>this._b3._bx_Body_ApplyLinearImpulse(o.slot,e.x,e.y,e.z,s.x,s.y,s.z),i)}applyAngularImpulse(t,e,s){this._applyToBodyOrInstances(t,i=>this._b3._bx_Body_ApplyAngularImpulse(i.slot,e.x,e.y,e.z),s)}applyForce(t,e,s,i){this._applyToBodyOrInstances(t,o=>this._b3._bx_Body_ApplyForce(o.slot,e.x,e.y,e.z,s.x,s.y,s.z),i)}applyTorque(t,e,s){this._applyToBodyOrInstances(t,i=>this._b3._bx_Body_ApplyTorque(i.slot,e.x,e.y,e.z),s)}setAngularVelocity(t,e,s){this._applyToBodyOrInstances(t,i=>this._b3._bx_Body_SetAngularVelocity(i.slot,e.x,e.y,e.z),s)}getAngularVelocityToRef(t,e,s){this._b3._bx_Body_GetAngularVelocity(this._getPluginReference(t,s).slot);let i=this._scratch();e.set(i[0],i[1],i[2])}getBodyGeometry(t){let e=t.shape?._pluginData;if(!e)return{positions:[],indices:[]};let s=this._b3._bx_ShapeDesc_BuildDebugGeometry(e.slot),i=this._b3._bx_DebugIndexCount();if(!s||!i)return{positions:[],indices:[]};let o=new Float32Array(this._b3.HEAPF32.buffer,this._b3._bx_DebugPositionsPtr(),s*3).slice(),a=new Uint32Array(this._b3.HEAPU32.buffer,this._b3._bx_DebugIndicesPtr(),i).slice();return{positions:o,indices:a}}disposeBody(t){this.removeBody(t),t._pluginData=void 0,t._pluginDataInstances.length=0}setCollisionCallbackEnabled(t,e,s){this.setEventMask(t,e?7:0,s)}setCollisionEndedCallbackEnabled(t,e,s){this._applyToBodyOrInstances(t,i=>{i.eventMask=e?i.eventMask|4:i.eventMask&-5,this._applyEventFlags(i)},s)}getCollisionObservable(t,e){let s=this._getPluginReference(t,e).slot,i=this._bodyCollisionObservable.get(s);return i||(i=new M.Observable,this._bodyCollisionObservable.set(s,i)),i}getCollisionEndedObservable(t,e){let s=this._getPluginReference(t,e).slot,i=this._bodyCollisionEndedObservable.get(s);return i||(i=new M.Observable,this._bodyCollisionEndedObservable.set(s,i)),i}setGravityFactor(t,e,s){this._applyToBodyOrInstances(t,i=>this._b3._bx_Body_SetGravityScale(i.slot,e),s)}getGravityFactor(t,e){return this._b3._bx_Body_GetGravityScale(this._getPluginReference(t,e).slot)}_registerShape(t,e,s){if(!e)throw new Error("Box3DPlugin: shape creation failed (degenerate geometry?)");let i=new j(e,s);return t._pluginData=i,this._shapes[e]=i,i}_withFloatBuffer(t,e){let s=this._b3._malloc(t.length*4);new Float32Array(this._b3.HEAPF32.buffer,s,t.length).set(t);try{return e(s)}finally{this._b3._free(s)}}_withByteBuffer(t,e){if(!t||t.length===0)return e(0);let s=this._b3._malloc(t.length);new Uint8Array(this._b3.HEAPU8.buffer,s,t.length).set(t);try{return e(s)}finally{this._b3._free(s)}}_withIntBuffer(t,e){let s=this._b3._malloc(t.length*4);new Int32Array(this._b3.HEAP32.buffer,s,t.length).set(t);try{return e(s)}finally{this._b3._free(s)}}_createOptionsFromGroundMesh(t){let e=t.groundMesh;if(!e)return;let s=e.getVerticesData(q.VertexBuffer.PositionKind),i=e.computeWorldMatrix(!0),o=[],a=p.TmpVectors.Vector3[0];for(let b=0;b<s.length;b+=3)p.Vector3.FromArrayToRef(s,b,a),p.Vector3.TransformCoordinatesToRef(a,i,a),a.toArray(o,b);let n=~~(Math.sqrt(o.length/3)-1),r=e.getBoundingInfo(),l=Math.min(r.boundingBox.extendSizeWorld.x,r.boundingBox.extendSizeWorld.z),h=r.boundingBox.minimumWorld.x,c=r.boundingBox.minimumWorld.y,u=r.boundingBox.minimumWorld.z,x=new Float32Array((n+1)*(n+1)),d=l*2/n;x.fill(c);for(let b=0;b<o.length;b+=3){let D=Math.round((o[b+0]-h)/d),g=n-Math.round((o[b+2]-u)/d);x[g*(n+1)+D]=o[b+1]-c}t.numHeightFieldSamplesX=n+1,t.numHeightFieldSamplesZ=n+1,t.heightFieldSizeX=r.boundingBox.extendSizeWorld.x*2,t.heightFieldSizeZ=r.boundingBox.extendSizeWorld.z*2,t.heightFieldData=x}initShape(t,e,s){let i=this._b3,o=s.center??p.Vector3.ZeroReadOnly,a=s.rotation??p.Quaternion.Identity();switch(e){case _.PhysicsShapeType.SPHERE:{let n=s.radius??1;this._registerShape(t,i._bx_ShapeDesc_CreateSphere(o.x,o.y,o.z,n),e);break}case _.PhysicsShapeType.CAPSULE:{let n=s.pointA??p.Vector3.ZeroReadOnly,r=s.pointB??p.Vector3.UpReadOnly;this._registerShape(t,i._bx_ShapeDesc_CreateCapsule(n.x,n.y,n.z,r.x,r.y,r.z,s.radius??0),e);break}case _.PhysicsShapeType.BOX:{let n=s.extents??p.Vector3.OneReadOnly;this._registerShape(t,i._bx_ShapeDesc_CreateBox(n.x*.5,n.y*.5,n.z*.5,o.x,o.y,o.z,a.x,a.y,a.z,a.w),e);break}case _.PhysicsShapeType.CYLINDER:{let n=s.pointA??p.Vector3.ZeroReadOnly,l=(s.pointB??p.Vector3.UpReadOnly).subtract(n),h=l.length(),c=this._tmpQuat[0];h>1e-6?(l.scaleInPlace(1/h),p.Quaternion.FromUnitVectorsToRef(p.Vector3.UpReadOnly,l,c)):c.set(0,0,0,1),this._registerShape(t,i._bx_ShapeDesc_CreateCylinder(h,s.radius??0,16,n.x,n.y,n.z,c.x,c.y,c.z,c.w),e);break}case _.PhysicsShapeType.CONVEX_HULL:case _.PhysicsShapeType.MESH:{if(!s.mesh&&s.positions){this._initShapeFromPositions(t,e,s.positions,s.positionIndices);break}let n=s.mesh;if(!n)throw new Error("No mesh provided to create physics shape.");let r=e===_.PhysicsShapeType.MESH,l=!n.getScene().useRightHandedSystem,h=new X(r,l);if(h.addNodeMeshes(n,!!s.includeChildMeshes),e===_.PhysicsShapeType.CONVEX_HULL){let c=this._withFloatBuffer(h.vertices,u=>i._bx_ShapeDesc_CreateHull(u,h.vertexCount));this._registerShape(t,c,e)}else{let c=this._withFloatBuffer(h.vertices,u=>this._withIntBuffer(h.indices,x=>i._bx_ShapeDesc_CreateMesh(u,h.vertexCount,x,h.triangleCount,1,1,1,0,0)));this._registerShape(t,c,e)}break}case _.PhysicsShapeType.HEIGHTFIELD:{s.groundMesh&&this._createOptionsFromGroundMesh(s);let n=s.numHeightFieldSamplesX,r=s.numHeightFieldSamplesZ;if(!n||!r||!s.heightFieldSizeX||!s.heightFieldSizeZ||!s.heightFieldData)throw new Error("Missing required heightfield parameters");let l=new Float32Array(n*r);for(let g=0;g<n;g++)for(let y=0;y<r;y++)l[y*n+g]=s.heightFieldData[(r-1-y)*n+g];let h=s.heightFieldSizeX/(n-1),c=s.heightFieldSizeZ/(r-1),u=n-1,x=r-1,d=s.heightFieldMaterials;if(d&&d.length!==u*x)throw new Error(`heightFieldMaterials needs ${u*x} cells (${u} \xD7 ${x}), got ${d.length}`);let b=d?new Uint8Array(u*x):null;if(d&&b)for(let g=0;g<u;g++)for(let y=0;y<x;y++)b[y*u+g]=d[(x-1-y)*u+g];let D=this._withFloatBuffer(l,g=>this._withByteBuffer(b,y=>i._bx_ShapeDesc_CreateHeightField(g,y,n,r,h,1,c,0,-s.heightFieldSizeX*.5,0,-s.heightFieldSizeZ*.5)));this._registerShape(t,D,e);break}case _.PhysicsShapeType.CONTAINER:{this._registerShape(t,i._bx_ShapeDesc_CreateContainer(),e);break}default:throw new Error("Unsupported Shape Type.")}}_initShapeFromPositions(t,e,s,i){let o=this._b3;if(s.length%3!==0)throw new Error(`positions needs x, y, z triplets, got ${s.length} numbers`);let a=s.length/3;if(e===_.PhysicsShapeType.CONVEX_HULL){this._registerShape(t,this._withFloatBuffer(s,n=>o._bx_ShapeDesc_CreateHull(n,a)),e);return}if(!i||i.length%3!==0)throw new Error("a MESH shape from positions needs positionIndices, three per triangle");for(let n=0;n<i.length;n++)if(i[n]<0||i[n]>=a||!Number.isInteger(i[n]))throw new Error(`positionIndices[${n}] is ${i[n]}, but there are ${a} points`);this._registerShape(t,this._withFloatBuffer(s,n=>this._withIntBuffer(i,r=>o._bx_ShapeDesc_CreateMesh(n,a,r,i.length/3,1,1,1,0,0))),e)}_refreshShapeUsers(t,e=new Set){if(!e.has(t)){e.add(t);for(let s of t.users)this._b3._bx_Body_SetShape(s.slot,t.slot),this._applyEventFlags(s),this._internalUpdateMassProperties(s);for(let s of t.parents)this._refreshShapeUsers(s,e)}}_syncShapeUsers(t,e,s=new Set,i=t){if(!s.has(t)){s.add(t);for(let o of t.users)this._b3._bx_Body_SyncShapeDesc(o.slot,i.slot,e),e&4&&this._internalUpdateMassProperties(o);for(let o of t.parents)this._syncShapeUsers(o,e,s,i)}}setShapeFilterMembershipMask(t,e){let s=t._pluginData,i=this._b3._bx_ShapeDesc_GetMaskBits(s.slot);this._b3._bx_ShapeDesc_SetFilter(s.slot,e>>>0,i>>>0),this._syncShapeUsers(s,1)}getShapeFilterMembershipMask(t){return this._b3._bx_ShapeDesc_GetCategoryBits(t._pluginData.slot)}setShapeFilterCollideMask(t,e){let s=t._pluginData,i=this._b3._bx_ShapeDesc_GetCategoryBits(s.slot);this._b3._bx_ShapeDesc_SetFilter(s.slot,i>>>0,e>>>0),this._syncShapeUsers(s,1)}getShapeFilterCollideMask(t){return this._b3._bx_ShapeDesc_GetMaskBits(t._pluginData.slot)}setMaterial(t,e){let s=t._pluginData;s.material=e,this._b3._bx_ShapeDesc_SetMaterial(s.slot,e.friction??.5,e.restitution??0),this._syncShapeUsers(s,2)}getMaterial(t){let e=t._pluginData;return{friction:this._b3._bx_ShapeDesc_GetFriction(e.slot),staticFriction:e.material.staticFriction,restitution:this._b3._bx_ShapeDesc_GetRestitution(e.slot),frictionCombine:e.material.frictionCombine,restitutionCombine:e.material.restitutionCombine}}setDensity(t,e){let s=t._pluginData;this._b3._bx_ShapeDesc_SetDensity(s.slot,e),this._syncShapeUsers(s,4)}getDensity(t){return this._b3._bx_ShapeDesc_GetDensity(t._pluginData.slot)}addChild(t,e,s,i,o){let a=t._pluginData,n=e._pluginData,r=s??p.Vector3.ZeroReadOnly,l=i??p.Quaternion.Identity(),h=o??p.Vector3.OneReadOnly;this._b3._bx_ShapeDesc_AddChild(a.slot,n.slot,r.x,r.y,r.z,l.x,l.y,l.z,l.w,h.x,h.y,h.z),a.children.push(n),n.parents.add(a),this._refreshShapeUsers(a)}removeChild(t,e){let s=t._pluginData;this._b3._bx_ShapeDesc_RemoveChild(s.slot,e);let[i]=s.children.splice(e,1);i&&!s.children.includes(i)&&i.parents.delete(s),this._refreshShapeUsers(s)}getNumChildren(t){return this._b3._bx_ShapeDesc_GetChildCount(t._pluginData.slot)}getBoundingBox(t){this._b3._bx_ShapeDesc_GetAABB(t._pluginData.slot);let e=this._scratch();return p.TmpVectors.Vector3[0].set(e[0],e[1],e[2]),p.TmpVectors.Vector3[1].set(e[3],e[4],e[5]),new U.BoundingBox(p.TmpVectors.Vector3[0],p.TmpVectors.Vector3[1],p.Matrix.IdentityReadOnly)}getBodyBoundingBox(t){let e=this._getPluginReference(t);this._b3._bx_Body_GetAABB(e.slot);let s=this._scratch();return p.TmpVectors.Vector3[0].set(s[0],s[1],s[2]),p.TmpVectors.Vector3[1].set(s[3],s[4],s[5]),new U.BoundingBox(p.TmpVectors.Vector3[0],p.TmpVectors.Vector3[1],p.Matrix.IdentityReadOnly)}disposeShape(t){let e=t._pluginData;if(e){for(let s of e.parents){let i=s.children.indexOf(e);i>=0&&(s.children.splice(i,1),this._b3._bx_ShapeDesc_RemoveChild(s.slot,i))}for(let s of e.children)s.parents.delete(e);this._shapes[e.slot]=void 0,this._b3._bx_ShapeDesc_Destroy(e.slot),t._pluginData=void 0}}setTrigger(t,e){let s=t._pluginData;this._b3._bx_ShapeDesc_SetSensor(s.slot,e?1:0),this._refreshShapeUsers(s)}_frameToBuffer(t,e,s,i,o,a){let n=p.TmpVectors.Matrix[0];p.Matrix.FromValuesToRef(i.x,i.y,i.z,0,o.x,o.y,o.z,0,a.x,a.y,a.z,0,0,0,0,1,n);let r=this._tmpQuat[2];p.Quaternion.FromRotationMatrixToRef(n,r),r.normalize(),t[e]=s.x,t[e+1]=s.y,t[e+2]=s.z,t[e+3]=r.x,t[e+4]=r.y,t[e+5]=r.z,t[e+6]=r.w}_buildFrames(t,e,s,i){let o=e.options,a=o.pivotA??p.Vector3.ZeroReadOnly,n=o.pivotB??p.Vector3.ZeroReadOnly,r=(o.axisA??p.Vector3.RightReadOnly).normalizeToNew(),l=(o.axisB??p.Vector3.RightReadOnly).normalizeToNew(),h=o.perpAxisA?o.perpAxisA.normalizeToNew():r.getNormalToRef(new p.Vector3),c=o.perpAxisB?o.perpAxisB.normalizeToNew():l.getNormalToRef(new p.Vector3);h.subtractInPlace(r.scale(p.Vector3.Dot(r,h))).normalize(),c.subtractInPlace(l.scale(p.Vector3.Dot(l,c))).normalize();let u=p.Vector3.Cross(r,h),x=p.Vector3.Cross(l,c);e._initOptions||(e._initOptions={axisA:r.clone(),axisB:l.clone(),perpAxisA:h.clone(),perpAxisB:c.clone(),pivotA:a.clone(),pivotB:n.clone()});let d=t.frames;if(t.plan){let b=t.plan.basis;if(b){let D=[r,h,u],g=[l,c,x];this._frameToBuffer(d,0,a,D[b[0]],D[b[1]],D[b[2]]),this._frameToBuffer(d,7,n,g[b[0]],g[b[1]],g[b[2]])}else d.set([a.x,a.y,a.z,0,0,0,1,n.x,n.y,n.z,0,0,0,1]);return}switch(t.type){case _.PhysicsConstraintType.HINGE:case _.PhysicsConstraintType.BALL_AND_SOCKET:this._frameToBuffer(d,0,a,h,p.Vector3.Cross(r,h),r),this._frameToBuffer(d,7,n,c,p.Vector3.Cross(l,c),l);break;case _.PhysicsConstraintType.PRISMATIC:case _.PhysicsConstraintType.SLIDER:this._frameToBuffer(d,0,a,r,h,u),this._frameToBuffer(d,7,n,l,c,x);break;case _.PhysicsConstraintType.LOCK:{this._b3._bx_Body_GetTransform(s.slot);let b=this._scratch(),D=this._tmpQuat[0];D.set(b[3],b[4],b[5],b[6]),this._b3._bx_Body_GetTransform(i.slot),b=this._scratch();let g=this._tmpQuat[1];g.set(b[3],b[4],b[5],b[6]);let y=g.invert().multiply(D).normalize();d.set([a.x,a.y,a.z,0,0,0,1,n.x,n.y,n.z,y.x,y.y,y.z,y.w]);break}default:d.set([a.x,a.y,a.z,0,0,0,1,n.x,n.y,n.z,0,0,0,1]);break}}_createJoint(t,e,s){this._scratch().set(t.frames);let i=this._b3._bx_CreateJoint(this._worldSlot,t.jointType,e.slot,s.slot,t.collisions?1:0,t.length);return i?(this._applyAxisState(t,i,e,s),i):(I.Logger.Warn("Box3DPlugin: joint creation failed."),0)}_readSixDofLimits(t,e){for(let s of e??[]){let i=this._axisState(t,s.axis);s.minLimit===0&&s.maxLimit===0?(i.mode=_.PhysicsConstraintAxisLimitMode.LOCKED,i.min=0,i.max=0):(s.minLimit!==void 0||s.maxLimit!==void 0)&&(i.mode=_.PhysicsConstraintAxisLimitMode.LIMITED,i.min=s.minLimit??-1/0,i.max=s.maxLimit??1/0),i.stiffness=s.stiffness??0,i.damping=s.damping??0}}_planSixDof(t){let e=_.PhysicsConstraintAxis,s=_.PhysicsConstraintAxisLimitMode,i=y=>t.axes.get(y)?.mode??s.FREE,o=y=>{let f=t.axes.get(y);return f.mode===s.LOCKED?[0,0]:[Math.min(f.min,f.max),Math.max(f.min,f.max)]},a=(y,f)=>Math.max(-f,Math.min(f,y)),n=.99*Math.PI,r=1e5,l={jointType:0,basis:[0,1,2],primaryAxis:null,coneAngle:-1,twist:null,limit:null,spring:!1,stiffness:0,damping:0,angularStiffness:!1};for(let[y,f]of t.axes)(f.stiffness>0||f.damping>0)&&f.stiffness>=l.stiffness&&(l.stiffness=f.stiffness,l.damping=f.damping,l.angularStiffness=y===e.ANGULAR_X||y===e.ANGULAR_Y||y===e.ANGULAR_Z);let h=[e.LINEAR_X,e.LINEAR_Y,e.LINEAR_Z],c=[e.ANGULAR_X,e.ANGULAR_Y,e.ANGULAR_Z],u=h.filter(y=>i(y)===s.LOCKED).length,x=h.filter(y=>i(y)===s.FREE).length,d=c.filter(y=>i(y)!==s.LOCKED),b=c.filter(y=>i(y)===s.FREE).length;if(i(e.LINEAR_DISTANCE)!==s.FREE){if(x===3&&b===3){let[y,f]=o(e.LINEAR_DISTANCE);return l.jointType=4,l.basis=null,l.primaryAxis=e.LINEAR_DISTANCE,l.limit=[Math.min(Math.max(0,y),r),Math.min(Math.max(0,f),r)],l.spring=l.stiffness>0&&Math.abs(f-y)<1e-6&&isFinite(y),l}this._warnOnce("sixdof-distance","SIX_DOF: LINEAR_DISTANCE combined with other constrained axes is not supported with Box3D, the distance limit is ignored.")}let g=u===3;if(!g&&u!==2&&x===0&&(this._warnOnce("sixdof-linear-limited","SIX_DOF: two or more limited linear axes are treated as locked with Box3D."),g=!0),g){if(d.length===0)return l;if(d.length===1){let f=d[0];if(l.jointType=2,l.basis=[[1,2,0],[2,0,1],[0,1,2]][c.indexOf(f)],l.primaryAxis=f,i(f)===s.LIMITED){let[S,P]=o(f);l.limit=[a(S,n),a(P,n)]}return l}if(l.jointType=1,l.basis=[1,2,0],i(e.ANGULAR_X)!==s.FREE){let[f,S]=o(e.ANGULAR_X);l.twist=[a(f,n),a(S,n)]}let y=[i(e.ANGULAR_Y),i(e.ANGULAR_Z)];if(y.some(f=>f!==s.FREE)){let f=[],S=!1;for(let C of[e.ANGULAR_Y,e.ANGULAR_Z]){if(i(C)===s.FREE)continue;let[v,T]=o(C);f.push(Math.max(Math.abs(v),Math.abs(T))),S||(S=Math.abs(v+T)>1e-4)}let P=Math.max(...f);isFinite(P)&&((y.includes(s.FREE)||Math.abs(f[0]-f[1])>1e-4||S)&&this._warnOnce("sixdof-cone","SIX_DOF: Box3D has one symmetric swing cone, ANGULAR_Y and ANGULAR_Z use the larger of their ranges."),P>Math.PI/2&&this._warnOnce("sixdof-cone-90","SIX_DOF: Box3D cone limits are at most 90 degrees, larger swing ranges are clamped."),l.coneAngle=Math.min(P,Math.PI/2))}return l}if(u===2){let y=h.find(f=>i(f)!==s.LOCKED);if(l.jointType=3,l.basis=[[0,1,2],[1,2,0],[2,0,1]][h.indexOf(y)],l.primaryAxis=y,i(y)===s.LIMITED){let[f,S]=o(y);l.limit=[a(f,r),a(S,r)]}return d.length&&this._warnOnce("sixdof-prismatic","SIX_DOF: a sliding axis with rotation is not supported with Box3D, rotation is locked."),l}return l.jointType=5,l.basis=null,(x!==3||b!==3)&&this._warnOnce("sixdof-unsupported","SIX_DOF: this combination of free and limited axes is not supported with Box3D, the bodies are left unconstrained."),l}_springToHertz(t,e,s,i,o){let a=h=>{if(h.motionType!==_.PhysicsMotionType.DYNAMIC)return 0;this._b3._bx_Body_GetMassData(h.slot);let c=this._scratch(),u=s?(c[4]+c[5]+c[6])/3:c[0];return u>0?1/u:0},n=a(i)+a(o),r=n>0?1/n:1,l=Math.max(t,0);return{hertz:Math.sqrt(l/r)/(2*Math.PI),dampingRatio:l>0?e/(2*Math.sqrt(l*r)):1}}initConstraint(t,e,s,i,o){let a=t.type,n=t.options;if(!a||!n){I.Logger.Warn("No constraint type or options. Constraint is invalid.");return}if(e._pluginDataInstances.length>0&&i===void 0||s._pluginDataInstances.length>0&&o===void 0){I.Logger.Warn("Body is instanced but no instance index was specified. Constraint will not be applied.");return}let r=!t._pluginData,l=t._pluginData??new Q;switch(t._pluginData=l,l.type=a,r&&(l.collisions=!!n.collision),a){case _.PhysicsConstraintType.BALL_AND_SOCKET:l.jointType=1;break;case _.PhysicsConstraintType.HINGE:l.jointType=2;break;case _.PhysicsConstraintType.PRISMATIC:case _.PhysicsConstraintType.SLIDER:l.jointType=3;break;case _.PhysicsConstraintType.LOCK:l.jointType=0;break;case _.PhysicsConstraintType.DISTANCE:l.jointType=4;break;case _.PhysicsConstraintType.SIX_DOF:r&&this._readSixDofLimits(l,t.limits),l.plan=this._planSixDof(l),l.jointType=l.plan.jointType;break;default:I.Logger.Warn("Box3DPlugin: unknown constraint type "+a);return}let h=this._getPluginReference(e,i),c=this._getPluginReference(s,o);this._buildFrames(l,t,h,c),this._updateJointLength(l,n.maxDistance,h,c);let u=l.enabled?this._createJoint(l,h,c):0;l.joints.push(u),l.pairs.push({parent:e,parentIndex:i??0,child:s,childIndex:o??0,parentData:h,childData:c})}_updateJointLength(t,e,s,i){if(t.type===_.PhysicsConstraintType.DISTANCE)t.length=e??0;else if(t.plan?.jointType===4&&t.plan.limit)t.length=t.plan.limit[0];else return;t.length<=0&&(t.length=Math.max(.01,this._currentPivotDistance(t,s,i)))}_currentPivotDistance(t,e,s){let i=t.frames,o=this._tmpVec3[0],a=this._tmpVec3[1];this._b3._bx_Body_GetTransform(e.slot);let n=this._scratch();return this._tmpQuat[0].set(n[3],n[4],n[5],n[6]),o.set(i[0],i[1],i[2]).applyRotationQuaternionInPlace(this._tmpQuat[0]).addInPlaceFromFloats(n[0],n[1],n[2]),this._b3._bx_Body_GetTransform(s.slot),n=this._scratch(),this._tmpQuat[0].set(n[3],n[4],n[5],n[6]),a.set(i[7],i[8],i[9]).applyRotationQuaternionInPlace(this._tmpQuat[0]).addInPlaceFromFloats(n[0],n[1],n[2]),p.Vector3.Distance(o,a)}setEnabled(t,e){let s=t._pluginData;!s||s.enabled===e||(s.enabled=e,this._recreateJoints(s))}_destroyJoint(t){this._motorJoints.delete(t),this._b3._bx_DestroyJoint(t)}_recreateJoints(t){for(let e=0;e<t.pairs.length;e++){t.joints[e]&&this._destroyJoint(t.joints[e]);let s=t.pairs[e];t.joints[e]=t.enabled&&s.parentData.slot&&s.childData.slot?this._createJoint(t,s.parentData,s.childData):0}}getEnabled(t){return t._pluginData?.enabled??!1}setCollisionsEnabled(t,e){let s=t._pluginData;s.collisions=e;for(let i of s.joints)i&&this._b3._bx_Joint_SetCollideConnected(i,e?1:0)}getCollisionsEnabled(t){return t._pluginData?.collisions??!1}_primaryAxis(t){switch(t.type){case _.PhysicsConstraintType.HINGE:return _.PhysicsConstraintAxis.ANGULAR_X;case _.PhysicsConstraintType.PRISMATIC:case _.PhysicsConstraintType.SLIDER:return _.PhysicsConstraintAxis.LINEAR_X;case _.PhysicsConstraintType.DISTANCE:return _.PhysicsConstraintAxis.LINEAR_DISTANCE;case _.PhysicsConstraintType.BALL_AND_SOCKET:return _.PhysicsConstraintAxis.ANGULAR_Y;case _.PhysicsConstraintType.SIX_DOF:return t.plan?.primaryAxis??null;default:return null}}_isAngularAxis(t){return t===_.PhysicsConstraintAxis.ANGULAR_X||t===_.PhysicsConstraintAxis.ANGULAR_Y||t===_.PhysicsConstraintAxis.ANGULAR_Z}_isPrimaryAxis(t,e){return t.type===_.PhysicsConstraintType.BALL_AND_SOCKET||t.plan?.jointType===1?this._isAngularAxis(e):e===this._primaryAxis(t)}_axisState(t,e){let s=t.axes.get(e);return s||(s={mode:_.PhysicsConstraintAxisLimitMode.FREE,min:0,max:0,motor:_.PhysicsConstraintMotorType.NONE,target:0,maxForce:0,friction:0,stiffness:0,damping:0},t.axes.set(e,s)),s}_applyAxisState(t,e,s,i){let o=this._b3;if(t.plan){this._applySixDofPlan(t,e,s,i),o._bx_Joint_WakeBodies(e);return}if(this._primaryAxis(t)!==null){for(let[n,r]of t.axes){if(!this._isPrimaryAxis(t,n))continue;if(t.type===_.PhysicsConstraintType.BALL_AND_SOCKET&&n===_.PhysicsConstraintAxis.ANGULAR_X){o._bx_Joint_SetTwistLimits(e,r.mode===_.PhysicsConstraintAxisLimitMode.FREE?0:1,r.min,r.max);continue}let l=r.mode!==_.PhysicsConstraintAxisLimitMode.FREE;if(l){let h=r.mode===_.PhysicsConstraintAxisLimitMode.LOCKED?0:r.min,c=r.mode===_.PhysicsConstraintAxisLimitMode.LOCKED?0:r.max;o._bx_Joint_SetLimits(e,h,c)}o._bx_Joint_EnableLimit(e,l?1:0),t.type!==_.PhysicsConstraintType.BALL_AND_SOCKET&&this._applyMotor(e,r)}t.type===_.PhysicsConstraintType.BALL_AND_SOCKET&&this._applySphericalMotor(t,e),o._bx_Joint_WakeBodies(e)}}_applySphericalMotor(t,e){let s=this._b3,i=t.axes.get(_.PhysicsConstraintAxis.ANGULAR_X),o=t.axes.get(_.PhysicsConstraintAxis.ANGULAR_Y),a=t.axes.get(_.PhysicsConstraintAxis.ANGULAR_Z),n=[i,o,a],r=x=>x?.motor===_.PhysicsConstraintMotorType.VELOCITY?x.target:0,l=n.some(x=>x?.motor===_.PhysicsConstraintMotorType.VELOCITY),h=n.some(x=>x?.motor===_.PhysicsConstraintMotorType.POSITION),c=Math.max(...n.map(x=>x?.motor===_.PhysicsConstraintMotorType.VELOCITY?x.maxForce:0));s._bx_Joint_SetSphericalMotor(e,l?1:0,r(o),r(a),r(i),c);let u=r(o)!==0||r(a)!==0||r(i)!==0;if(l&&u?this._motorJoints.add(e):this._motorJoints.delete(e),s._bx_Joint_EnableSpring(e,h?1:0),h){let x=D=>D?.motor===_.PhysicsConstraintMotorType.POSITION?D.target:0,d=this._tmpQuat[0];p.Quaternion.RotationYawPitchRollToRef(0,0,x(i),d);let b=this._tmpQuat[1];p.Quaternion.RotationYawPitchRollToRef(x(a),x(o),0,b),d.multiplyInPlace(b),s._bx_Joint_SetSpring(e,5,1),s._bx_Joint_SetSphericalTarget(e,d.x,d.y,d.z,d.w)}}_applyMotor(t,e){let s=this._b3;switch(e.motor){case _.PhysicsConstraintMotorType.VELOCITY:s._bx_Joint_EnableSpring(t,0),s._bx_Joint_EnableMotor(t,1),s._bx_Joint_SetMotorSpeed(t,e.target),s._bx_Joint_SetMaxMotorForce(t,e.maxForce);break;case _.PhysicsConstraintMotorType.POSITION:s._bx_Joint_EnableMotor(t,0),s._bx_Joint_EnableSpring(t,1),s._bx_Joint_SetSpring(t,5,1),s._bx_Joint_SetTarget(t,e.target);break;default:s._bx_Joint_EnableMotor(t,0),s._bx_Joint_EnableSpring(t,0);break}}_applySixDofPlan(t,e,s,i){let o=this._b3,a=t.plan;switch(a.jointType){case 1:a.coneAngle>=0&&o._bx_Joint_SetLimits(e,-a.coneAngle,a.coneAngle),o._bx_Joint_EnableLimit(e,a.coneAngle>=0?1:0),o._bx_Joint_SetTwistLimits(e,a.twist?1:0,a.twist?a.twist[0]:0,a.twist?a.twist[1]:0);break;case 2:case 3:a.limit&&o._bx_Joint_SetLimits(e,a.limit[0],a.limit[1]),o._bx_Joint_EnableLimit(e,a.limit?1:0);break;case 4:if(a.spring){let n=this._springToHertz(a.stiffness,a.damping,!1,s,i);o._bx_Joint_EnableLimit(e,0),o._bx_Joint_EnableSpring(e,1),o._bx_Joint_SetSpring(e,n.hertz,n.dampingRatio),o._bx_Joint_SetTarget(e,t.length)}else o._bx_Joint_SetLimits(e,a.limit[0],a.limit[1]),o._bx_Joint_EnableLimit(e,1),o._bx_Joint_EnableSpring(e,1),o._bx_Joint_SetSpring(e,0,0);break;default:break}if(a.stiffness>0&&!a.spring){this._warnOnce("sixdof-softness","SIX_DOF: limit stiffness and damping are approximated with Box3D's per joint constraint softness, which applies to every axis of the joint.");let n=this._springToHertz(a.stiffness,a.damping,a.angularStiffness,s,i);o._bx_Joint_SetConstraintTuning(e,n.hertz,n.dampingRatio)}if(a.jointType===1)this._applySphericalMotor(t,e);else if(a.primaryAxis!==null&&a.jointType!==4){let n=t.axes.get(a.primaryAxis);n&&this._applyMotor(e,n)}}_setAxis(t,e,s){let i=t._pluginData;if(!i)return;let o=this._axisState(i,e),a={motor:o.motor,target:o.target,maxForce:o.maxForce};if(s(o),i.plan){let n=this._planSixDof(i),r=n.jointType!==i.plan.jointType||String(n.basis)!==String(i.plan.basis);i.plan=n,i.jointType=n.jointType,(a.motor!==o.motor||a.target!==o.target||a.maxForce!==o.maxForce)&&o.motor!==_.PhysicsConstraintMotorType.NONE&&!this._isPrimaryAxis(i,e)&&this._warnOnce(`axis-motor-${e}`,`SIX_DOF: a motor on axis ${e} has no effect with the Box3D joint this constraint maps to.`);let h=i.pairs[0];if(h&&this._updateJointLength(i,t.options.maxDistance,h.parentData,h.childData),r){h&&this._buildFrames(i,t,h.parentData,h.childData),this._recreateJoints(i);return}}else if(!this._isPrimaryAxis(i,e)){this._warnOnce(`axis-${i.type}-${e}`,`axis ${e} is not configurable on constraint type ${i.type} with Box3D; the value is stored but has no effect.`);return}for(let n=0;n<i.joints.length;n++)i.joints[n]&&this._applyAxisState(i,i.joints[n],i.pairs[n].parentData,i.pairs[n].childData)}setAxisFriction(t,e,s){let i=t._pluginData;i&&(this._axisState(i,e).friction=s)}getAxisFriction(t,e){return t._pluginData?.axes.get(e)?.friction??null}setAxisMode(t,e,s){this._setAxis(t,e,i=>i.mode=s)}getAxisMode(t,e){return t._pluginData?.axes.get(e)?.mode??null}setAxisMinLimit(t,e,s){this._setAxis(t,e,i=>{i.min=s,i.mode===_.PhysicsConstraintAxisLimitMode.FREE&&(i.mode=_.PhysicsConstraintAxisLimitMode.LIMITED)})}getAxisMinLimit(t,e){return t._pluginData?.axes.get(e)?.min??null}setAxisMaxLimit(t,e,s){this._setAxis(t,e,i=>{i.max=s,i.mode===_.PhysicsConstraintAxisLimitMode.FREE&&(i.mode=_.PhysicsConstraintAxisLimitMode.LIMITED)})}getAxisMaxLimit(t,e){return t._pluginData?.axes.get(e)?.max??null}setAxisMotorType(t,e,s){this._setAxis(t,e,i=>i.motor=s)}getAxisMotorType(t,e){return t._pluginData?.axes.get(e)?.motor??null}setAxisMotorTarget(t,e,s){this._setAxis(t,e,i=>i.target=s)}getAxisMotorTarget(t,e){return t._pluginData?.axes.get(e)?.target??null}setAxisMotorMaxForce(t,e,s){this._setAxis(t,e,i=>i.maxForce=s)}getAxisMotorMaxForce(t,e){return t._pluginData?.axes.get(e)?.maxForce??null}disposeConstraint(t){let e=t._pluginData;if(e){for(let s of e.joints)s&&this._destroyJoint(s);e.joints.length=0,e.pairs.length=0}}getBodiesUsingConstraint(t){let e=t._pluginData;return e?e.pairs.map(s=>({parentBody:s.parent,parentBodyIndex:s.parentIndex,childBody:s.child,childBodyIndex:s.childIndex})):[]}addConstraint(t,e,s,i,o){this.initConstraint(s,t,e,i,o)}explode(t,e,s,i=0){this._b3._bx_World_Explode(this._worldSlot,t.x,t.y,t.z,e,i,s)}setShapeFilterGroup(t,e){let s=t._pluginData;this._b3._bx_ShapeDesc_SetGroup(s.slot,e|0),this._syncShapeUsers(s,1)}setShapeRollingResistance(t,e){let s=t._pluginData;this._b3._bx_ShapeDesc_SetRollingResistance(s.slot,e),this._syncShapeUsers(s,2)}setAllowFastRotation(t,e,s){this._applyToBodyOrInstances(t,i=>this._b3._bx_Body_AllowFastRotation(i.slot,e?1:0),s)}createWheelJoint(t,e,s){let i=this._scratch(),o=(s.axisA??p.Vector3.UpReadOnly).normalizeToNew(),a=(s.axleA??p.Vector3.Forward()).normalizeToNew();a.subtractInPlace(o.scale(p.Vector3.Dot(o,a))).normalize(),this._frameToBuffer(i,0,s.pivotA,o,p.Vector3.Cross(a,o),a);let n=s.pivotB??p.Vector3.ZeroReadOnly;i[7]=n.x,i[8]=n.y,i[9]=n.z;let r=this._getPluginReference(t),l=this._getPluginReference(e);this._b3._bx_ComputeAlignedFrameB(r.slot,l.slot);let h=s.suspensionLimits??[-.2,.2],c=s.steeringLimits??[-Math.PI/4,Math.PI/4];i.set([1,s.suspensionHertz??4,s.suspensionDampingRatio??.7,1,h[0],h[1],s.enableSpinMotor?1:0,s.maxSpinTorque??5,0,s.enableSteering?1:0,s.steeringHertz??10,s.steeringDampingRatio??.7,0,s.maxSteeringTorque??5,1,c[0],c[1]],14);let u=this._b3._bx_CreateJoint(this._worldSlot,6,r.slot,l.slot,0,0);if(!u)throw new Error("Box3DPlugin: wheel joint creation failed");return new L(this,u)}createParallelJoint(t,e,s,i,o=1,a=1,n=34e37){let r=this._scratch(),l=s.normalizeToNew(),h=l.getNormalToRef(new p.Vector3);this._frameToBuffer(r,0,p.Vector3.ZeroReadOnly,h,p.Vector3.Cross(l,h),l);let c=i.normalizeToNew(),u=c.getNormalToRef(new p.Vector3);return this._frameToBuffer(r,7,p.Vector3.ZeroReadOnly,u,p.Vector3.Cross(c,u),c),r.set([o,a,n],14),this._b3._bx_CreateJoint(this._worldSlot,7,this._getPluginReference(t).slot,this._getPluginReference(e).slot,1,0)}disposeExtraJoint(t){this._destroyExtraJoint(t)}_destroyExtraJoint(t){t&&this._destroyJoint(t)}_wheelCall(t,e,...s){e&&this._b3[t](e,...s)}raycast(t,e,s,i){let o=Array.isArray(s)?s:[s];for(let d of o)d.reset(t,e);let a=(i?.membership??-1)>>>0,n=(i?.collideWith??-1)>>>0,r=i?.ignoreBody?i.ignoreBody._pluginData?.slot??0:0,l=o.length===1,h=this._b3._bx_World_CastRay(this._worldSlot,t.x,t.y,t.z,e.x-t.x,e.y-t.y,e.z-t.z,a,n,r,i?.shouldHitTriggers?1:0,l?1:0);if(h<=0)return;let c=new Float32Array(this._b3.HEAPF32.buffer,this._b3._bx_RayHitsPtr(),h*J),u=[];for(let d=0;d<h;d++)u.push(d);if(u.sort((d,b)=>c[d*J+6]-c[b*J+6]),!o.length)for(let d=0;d<h;d++){let b=new gt.PhysicsRaycastResult;b.reset(t,e),o.push(b)}let x=Math.min(h,o.length);for(let d=0;d<x;d++){let b=u[d]*J,D=o[d],g=this._bodies[c[b+7]];D.body=g?.body,D.bodyIndex=g?.index,D.shape=g?.body.shape??void 0,D.setHitData({x:c[b+3],y:c[b+4],z:c[b+5]},{x:c[b],y:c[b+1],z:c[b+2]},c[b+9]),D.calculateHitDistance()}}_readEvents(t,e,s,i){let o=new Float32Array(this._b3.HEAPF32.buffer,t,e*s).slice(),a=new Array(e*2);for(let n=0;n<e;n++)a[n*2]=this._bodies[o[n*s+i]],a[n*2+1]=this._bodies[o[n*s+i+1]];return{records:o,refs:a}}_notifyCollisions(){let t=this._b3._bx_World_GetContactEvents(this._worldSlot);if(!t)return;let{records:e,refs:s}=this._readEvents(this._b3._bx_ContactEventsPtr(),t,mt,3);for(let i=0;i<t;i++){let o=i*mt,a=e[o],n=s[i*2],r=s[i*2+1];if(!n||!r||!n.data.slot||!r.data.slot)continue;let l=a===0?1:a===1?4:2;if(((n.data.eventMask|r.data.eventMask)&l)===0)continue;if(a===1){let d={collider:n.body,colliderIndex:n.index,collidedAgainst:r.body,collidedAgainstIndex:r.index,type:_.PhysicsEventType.COLLISION_FINISHED};this.onCollisionEndedObservable.notifyObservers(d),this._bodyCollisionEndedObservable.get(n.data.slot)?.notifyObservers(d);let b=this._bodyCollisionEndedObservable.get(r.data.slot);b&&b.notifyObservers({...d,collider:r.body,colliderIndex:r.index,collidedAgainst:n.body,collidedAgainstIndex:n.index});continue}let h=a===0?_.PhysicsEventType.COLLISION_STARTED:_.PhysicsEventType.COLLISION_CONTINUED,c=new p.Vector3(e[o+5],e[o+6],e[o+7]),u=new p.Vector3(e[o+8],e[o+9],e[o+10]),x={collider:n.body,colliderIndex:n.index,collidedAgainst:r.body,collidedAgainstIndex:r.index,type:h,point:c,normal:u,distance:0,impulse:e[o+11]};if(this.onCollisionObservable.notifyObservers(x),this._bodyCollisionObservable.size){this._bodyCollisionObservable.get(n.data.slot)?.notifyObservers(x);let d=this._bodyCollisionObservable.get(r.data.slot);d&&d.notifyObservers({...x,collider:r.body,colliderIndex:r.index,collidedAgainst:n.body,collidedAgainstIndex:n.index,normal:u?u.negate():null})}}}_notifyTriggers(){let t=this._b3._bx_World_GetSensorEvents(this._worldSlot);if(!t||!this.onTriggerCollisionObservable.hasObservers())return;let{records:e,refs:s}=this._readEvents(this._b3._bx_SensorEventsPtr(),t,ft,3);for(let i=0;i<t;i++){let o=i*ft,a=s[i*2],n=s[i*2+1];!a||!n||!a.data.slot||!n.data.slot||this.onTriggerCollisionObservable.notifyObservers({collider:a.body,colliderIndex:a.index,collidedAgainst:n.body,collidedAgainstIndex:n.index,type:e[o]===0?_.PhysicsEventType.TRIGGER_ENTERED:_.PhysicsEventType.TRIGGER_EXITED})}}dispose(){this._motorJoints.clear(),this.onCollisionObservable.clear(),this.onCollisionEndedObservable.clear(),this.onTriggerCollisionObservable.clear(),this._bodyCollisionObservable.clear(),this._bodyCollisionEndedObservable.clear();for(let t of this._shapes)t&&this._b3._bx_ShapeDesc_Destroy(t.slot);this._shapes.length=0,this._bodies.length=0,this._worldSlot&&(this._b3._bx_DestroyWorld(this._worldSlot),this._worldSlot=0)}};if(typeof Box3D>"u")throw new Error("babylon.box3d.js needs lib/umd/box3d.umd.js loaded first");var St=Box3D;return wt(zt);})();
|
|
3
3
|
//# sourceMappingURL=babylon.box3d.min.js.map
|