babylon-box3d 0.6.0 → 0.6.1
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 +3 -1
- package/lib/esm/box3d.wasm +0 -0
- package/lib/esm-threads/box3d.wasm +0 -0
- package/lib/node/box3d.wasm +0 -0
- package/lib/node-threads/box3d.wasm +0 -0
- package/lib/umd/box3d.umd.wasm +0 -0
- package/package.json +1 -1
- package/umd/babylon.box3d.js +1 -1
- package/umd/babylon.box3d.min.js +1 -1
package/README.md
CHANGED
|
@@ -368,8 +368,10 @@ stays exactly upstream's commit, and a change to the patch set rebuilds every ob
|
|
|
368
368
|
| `0001-broad-phase-prunes-by-mask.patch` | A shape that moved queries the broad phase with its own mask bits instead of every category (unless it is in a positive group, which overrides masks). A body whose shapes are filtered against each other then skips its own subtree instead of visiting every sibling shape before rejecting it. Which pairs collide does not change. |
|
|
369
369
|
| `0002-continuous-per-body.patch` | `b3Body_EnableContinuous` and `b3Body_IsContinuousEnabled` (shim: `bx_Body_EnableContinuous`, `bx_Body_IsContinuousEnabled`): continuous collision switched for one body. The world's switch still applies. A body made of many small shapes is swept shape by shape whenever its smallest shape moves fast enough, which can cost more than the rest of the step. |
|
|
370
370
|
| `0003-crushable-contacts.patch` | `b3Shape_SetCrushLimit(shape, maxForce, plastic)`, `b3Shape_GetCrushLimit` and `b3Shape_IsCrushPlastic` (shim: `bx_Body_SetShapeCrush(body, i, maxForce, plastic)`, `bx_Body_GetShapeCrush`): each contact of a crushable shape pushes with at most `maxForce` newtons, the smaller of its two shapes' limits, shared between its points, so a body meeting something rigid decelerates over a distance instead of stopping within a step. A plastic contact gets no push-out and no restitution, so it keeps the overlap it reaches. Crushable contacts are routed to the scalar solver path, where each point's accumulated impulse is clamped; a point still apart pushes only for the part of the substep after the surfaces meet. Rigid contacts are untouched, bit for bit. The limit may change every step, and a shape turning crushable or rigid keeps its contacts. |
|
|
371
|
+
| `0004-hull-builder-fails-instead-of-spinning.patch` | `b3CreateHull` returns NULL where it used to loop forever. A flat or nearly flat point set a few metres from the origin (a crushed panel, a slab) can leave the builder's half-edge mesh inconsistent: a merge leaves two faces that share every edge and `b3HullBuilder_ConnectFaces` walks round them without end, or a face's ring stops closing and `b3NewellPlane` does. Such a call never returned and locked whatever made it. Now every walk over the mesh counts its steps against the edge pool, the merge loops count merges against the face pool, and the horizon's stack and edge list check their capacity, which only asserts guarded before. None of these limits can be reached by a consistent mesh, and a build that reaches one returns NULL (shim: `bx_ShapeDesc_CreateHull` and `bx_Body_SetShapeHull` return 0, and the plugin throws for a `CONVEX_HULL` shape). Hulls that were built before are built the same, bit for bit. |
|
|
371
372
|
|
|
372
|
-
`test/wasm.patches.test.ts` covers 0001 and 0002,
|
|
373
|
+
`test/wasm.patches.test.ts` covers 0001 and 0002, `test/wasm.crush.test.ts` covers 0003 and `test/wasm.hulls.test.ts` covers 0004.
|
|
374
|
+
None has been offered upstream.
|
|
373
375
|
|
|
374
376
|
**Crush accuracy.** Against a constant limit the crush depth lands within 1.5% of the continuous answer at 480 Hz
|
|
375
377
|
with two substeps, and within 2% on a height field. What remains is Box3D's, not the patch's: its semi-implicit
|
package/lib/esm/box3d.wasm
CHANGED
|
Binary file
|
|
Binary file
|
package/lib/node/box3d.wasm
CHANGED
|
Binary file
|
|
Binary file
|
package/lib/umd/box3d.umd.wasm
CHANGED
|
Binary file
|
package/package.json
CHANGED
package/umd/babylon.box3d.js
CHANGED
package/umd/babylon.box3d.min.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
/* babylon-box3d 0.6.
|
|
1
|
+
/* babylon-box3d 0.6.1 | MIT | https://github.com/Pryme8/babylon-box3d */
|
|
2
2
|
"use strict";var BABYLONBOX3D=(()=>{var Re=Object.create;var z=Object.defineProperty;var Ne=Object.getOwnPropertyDescriptor;var Fe=Object.getOwnPropertyNames;var ke=Object.getPrototypeOf,ze=Object.prototype.hasOwnProperty;var ie=(x,e)=>()=>(x&&(e=x(x=0)),e);var B=(x,e)=>()=>(e||x((e={exports:{}}).exports,e),e.exports),W=(x,e)=>{for(var t in e)z(x,t,{get:e[t],enumerable:!0})},oe=(x,e,t,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of Fe(e))!ze.call(x,i)&&i!==t&&z(x,i,{get:()=>e[i],enumerable:!(s=Ne(e,i))||s.enumerable});return x};var A=(x,e,t)=>(t=x!=null?Re(ke(x)):{},oe(e||!x||!x.__esModule?z(t,"default",{value:x,enumerable:!0}):t,x)),Ge=x=>oe(z({},"__esModule",{value:!0}),x);var ae=B((Qe,ne)=>{if(typeof BABYLON>"u")throw new Error("babylon.box3d.js needs babylon.js loaded first");ne.exports=BABYLON});var le=B((Xe,re)=>{if(typeof BABYLON>"u")throw new Error("babylon.box3d.js needs babylon.js loaded first");re.exports=BABYLON});var _e=B((qe,ce)=>{if(typeof BABYLON>"u")throw new Error("babylon.box3d.js needs babylon.js loaded first");ce.exports=BABYLON});var pe=B((Ze,he)=>{if(typeof BABYLON>"u")throw new Error("babylon.box3d.js needs babylon.js loaded first");he.exports=BABYLON});var be=B(($e,de)=>{if(typeof BABYLON>"u")throw new Error("babylon.box3d.js needs babylon.js loaded first");de.exports=BABYLON});var ye=B((Ke,ue)=>{if(typeof BABYLON>"u")throw new Error("babylon.box3d.js needs babylon.js loaded first");ue.exports=BABYLON});var me=B((et,xe)=>{if(typeof BABYLON>"u")throw new Error("babylon.box3d.js needs babylon.js loaded first");xe.exports=BABYLON});var ge=B((tt,fe)=>{if(typeof BABYLON>"u")throw new Error("babylon.box3d.js needs babylon.js loaded first");fe.exports=BABYLON});var Se=B((st,De)=>{if(typeof BABYLON>"u")throw new Error("babylon.box3d.js needs babylon.js loaded first");De.exports=BABYLON});var ve={};W(ve,{default:()=>K});var K,ee=ie(()=>{if(typeof Box3D>"u")throw new Error("babylon.box3d.js needs lib/umd/box3d.umd.js loaded first");K=Box3D});var Te={};W(Te,{default:()=>He});function He(){throw new Error('babylon.box3d.js is the script tag build and has no threaded module. Install the package and import it, or use LoadBox3D({ threads: "auto" }).')}var Ie=ie(()=>{});var Ye={};W(Ye,{BOX3D_HEIGHT_FIELD_HOLE:()=>Pe,Box3D:()=>K,Box3DPlugin:()=>J,Box3DWheelJoint:()=>w,CanUseBox3DThreads:()=>V,LoadBox3D:()=>Me});var p=A(ae(),1),_=A(le(),1),Ce=A(_e(),1),I=A(pe(),1),H=A(be(),1),Y=A(ye(),1),j=A(me(),1),$=A(ge(),1),M=A(Se(),1),Pe=255,Q=class{constructor(e){this.slot=e;this.userMassProps={};this.angularLocks=0;this.eventMask=0;this.motionType=_.PhysicsMotionType.STATIC;this.activation=_.PhysicsActivationControl.SIMULATION_CONTROLLED}},X=class{constructor(e,t){this.slot=e;this.type=t;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 w=class{constructor(e,t){this._plugin=e;this._slot=t}setSpinSpeed(e){this._plugin._wheelCall("_bx_WheelJoint_SetSpinMotorSpeed",this._slot,e)}setMaxSpinTorque(e){this._plugin._wheelCall("_bx_WheelJoint_SetMaxSpinTorque",this._slot,e)}enableSpinMotor(e){this._plugin._wheelCall("_bx_WheelJoint_EnableSpinMotor",this._slot,e?1:0)}setSteeringAngle(e){this._plugin._wheelCall("_bx_WheelJoint_SetTargetSteeringAngle",this._slot,e)}setSuspension(e,t){this._plugin._wheelCall("_bx_WheelJoint_SetSuspension",this._slot,e,t)}dispose(){this._plugin._destroyExtraJoint(this._slot),this._slot=0}};var Je=1e5;function Ve(x,e,t){let s=[[x[0],x[3],x[4]],[x[3],x[1],x[5]],[x[4],x[5],x[2]]];t.set(0,0,0,1),e.set(x[0],x[1],x[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(we=>[0,1,2].reduce((Le,te)=>Le+h[te][E]*[0,1,2].reduce((Oe,se)=>Oe+s[te][se]*h[se][we],0),0)));e.set(c[0][0],c[1][1],c[2][2]),t.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])],m=u.indexOf(Math.max(...u)),b=(m+1)%3,d=(m+2)%3,D=c[b][d],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[d][d]-c[b][b])/(2*D),f=y>0?1:-1;y*=f;let S=f/(y+(y<1e6?Math.sqrt(y*y+1):y)),C=1/Math.sqrt(S*S+1);if(C===1)return;let P=[0,0,0,0];P[m]=-f*Math.sqrt((1-C)/2),P[3]=Math.sqrt(1-P[m]*P[m]);let[v,T,L,O]=i,[R,N,F,k]=P;i[0]=O*R+v*k+T*F-L*N,i[1]=O*N-v*F+T*k+L*R,i[2]=O*F+v*N-T*R+L*k,i[3]=O*k-v*R-T*N-L*F;let Ee=Math.hypot(i[0],i[1],i[2],i[3])||1;for(let E=0;E<4;E++)i[E]/=Ee}}function We(x,e){let{x:t,y:s,z:i,w:n}=e,a=[[1-2*(s*s+i*i),2*(t*s-i*n),2*(t*i+s*n)],[2*(t*s+i*n),1-2*(t*t+i*i),2*(s*i-t*n)],[2*(t*i-s*n),2*(s*i+t*n),1-2*(t*t+s*s)]],o=(r,l)=>a[r][0]*x[0]*a[l][0]+a[r][1]*x[1]*a[l][1]+a[r][2]*x[2]*a[l][2];return[o(0,0),o(1,1),o(2,2),o(0,1),o(0,2),o(1,2)]}var U=9,Be=14,Ae=5,G=11,Z=class{constructor(e,t){this._collectIndices=e;this._flipWinding=t;this._vertices=[];this._indices=[]}addNodeMeshes(e,t){e.computeWorldMatrix(!0);let s=p.TmpVectors.Matrix[0];if(p.Matrix.ScalingToRef(e.absoluteScaling.x,e.absoluteScaling.y,e.absoluteScaling.z,s),e instanceof Y.Mesh?this._addMesh(e,s):e instanceof j.InstancedMesh&&this._addMesh(e.sourceMesh,s),t){let i=p.TmpVectors.Matrix[1];e.computeWorldMatrix().invertToRef(i);let n=p.TmpVectors.Matrix[2];i.multiplyToRef(s,n);let a=e.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 Y.Mesh?this._addMesh(o,l):o instanceof j.InstancedMesh&&this._addMesh(o.sourceMesh,l)}}}_addMesh(e,t){let s=e.getVerticesData($.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,t,a),this._vertices.push(a.x,a.y,a.z);if(this._collectIndices){let o=e.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}},Ue=["_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_GetMaxWorkers","_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_GetWorkerCount","_bx_World_SetGravity","_bx_World_SetMaximumLinearSpeed","_bx_World_Step"],J=class{constructor(e=!0,t,s){this._useDeltaForWorldStep=e;this.name="Box3DPlugin";this.subStepCount=4;this.workerCount=1;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(!t)throw new Error("Box3D module is required: pass the awaited result of the Box3D module factory.");let i=Ue.filter(n=>typeof t[n]!="function");if(i.length>0)throw new Error(`Box3DPlugin: the Box3D module is missing ${i.length} entry point${i.length===1?"":"s"} this build needs (${i.slice(0,3).join(", ")}${i.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=t,this.world=t,s?.subStepCount!==void 0&&(this.subStepCount=Math.max(1,Math.round(s.subStepCount))),this._worldSlot=this._b3._bx_CreateWorld(0,-9.81,0,this._resolveWorkerCount(s?.workerCount)),!this._worldSlot)throw new Error("Box3D: could not create a world (max worlds reached?)");this.workerCount=Math.max(1,this._b3._bx_World_GetWorkerCount(this._worldSlot))}_resolveWorkerCount(e){let t=Math.max(1,this._b3._bx_GetMaxWorkers());if(e===void 0)return 1;let s;if(e==="auto"){let i=typeof navigator<"u"&&navigator.hardwareConcurrency||4;s=Math.max(1,Math.floor(i/2))}else s=Math.max(1,Math.round(e));return s>1&&t===1&&this._warnOnce("single-threaded-module",`${s} workers were requested, but this Box3D module was built without threads, so the step stays on the calling thread. Load the threaded build (LoadBox3D({ threads: true }), or babylon-box3d/wasm/threads) from a cross origin isolated page: it needs SharedArrayBuffer, which browsers only allow with the COOP and COEP headers set.`),Math.min(s,t)}_scratch(){return new Float32Array(this._b3.HEAPF32.buffer,this._b3._bx_Scratch(),64)}_warnOnce(e,t){this._warned.has(e)||(this._warned.add(e),I.Logger.Warn(`Box3DPlugin: ${t}`))}_getPluginReference(e,t){return e._pluginDataInstances?.length?e._pluginDataInstances[t??0]:e._pluginData}_applyToBodyOrInstances(e,t,s){if(e._pluginDataInstances?.length>0&&s===void 0)for(let i of e._pluginDataInstances)t(i);else t(this._getPluginReference(e,s))}_motionTypeToNative(e){switch(e){case _.PhysicsMotionType.STATIC:return 0;case _.PhysicsMotionType.ANIMATED:return 1;default:return 2}}_nodeTransformToRef(e,t,s){if(e.parent){e.computeWorldMatrix(!0),t.copyFrom(e.absolutePosition),s.copyFrom(e.absoluteRotationQuaternion);return}t.copyFrom(e.position),e.rotationQuaternion?s.copyFrom(e.rotationQuaternion):p.Quaternion.FromEulerAnglesToRef(e.rotation.x,e.rotation.y,e.rotation.z,s)}setGravity(e){this._b3._bx_World_SetGravity(this._worldSlot,e.x,e.y,e.z)}setTimeStep(e){this._fixedTimeStep=e}getTimeStep(){return this._fixedTimeStep}executeStep(e,t){for(let o of t)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?e: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*U);for(let l=0;l<n;l++){r.buffer!==this._b3.HEAPF32.buffer&&(r=new Float32Array(this._b3.HEAPF32.buffer,o,n*U));let h=l*U,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(e,t){this._b3._bx_World_SetMaximumLinearSpeed(this._worldSlot,e),this._maxAngularVelocity=t}getMaxLinearVelocity(){return this._b3._bx_World_GetMaximumLinearSpeed(this._worldSlot)}getMaxAngularVelocity(){return this._maxAngularVelocity}getStats(){this._b3._bx_World_GetStats(this._worldSlot);let e=this._scratch();return{bodies:e[0],shapes:e[1],contacts:e[2],joints:e[3],islands:e[4],stepMs:e[5],collideMs:e[6],solveMs:e[7]}}setSleepingEnabled(e){this._b3._bx_World_EnableSleeping(this._worldSlot,e?1:0)}setContinuousEnabled(e){this._b3._bx_World_EnableContinuous(this._worldSlot,e?1:0)}_createNativeBody(e,t,s,i){let n=this._b3._bx_CreateBody(this._worldSlot,this._motionTypeToNative(e),t.x,t.y,t.z,s.x,s.y,s.z,s.w,i?0:1),a=new Q(n);return a.motionType=e,a}initBody(e,t,s,i){let n=this._createNativeBody(t,s,i,e.startAsleep);e._pluginData=n,this._bodies[n.slot]={body:e,index:0,data:n}}initBodyInstances(e,t,s){let i=s._thinInstanceDataStorage?.instancesCount??0,n=s._thinInstanceDataStorage?.matrixData;if(!n)return;let a=e.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(e,t,n,0,i,!1)}_createOrUpdateBodyInstances(e,t,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=e._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(t,l,o,e.startAsleep),u=e._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))),e._pluginDataInstances.push(c),this._bodies[c.slot]={body:e,index:h,data:c}}}updateBodyInstances(e,t){let s=t._thinInstanceDataStorage?.instancesCount??0,i=t._thinInstanceDataStorage?.matrixData;if(!i)return;let n=e._pluginDataInstances.length,a=this.getMotionType(e);if(s>n){this._createOrUpdateBodyInstances(e,a,i,n,s,!1);let o=e.shape;if(o&&o._pluginData)for(let r=n;r<s;r++)this._setBodyShape(e._pluginDataInstances[r],o._pluginData)}else if(s<n){let o=n-s;for(let r=0;r<o;r++){let l=e._pluginDataInstances.pop();this._destroyBodyData(l)}this._createOrUpdateBodyInstances(e,a,i,0,s,!0)}}_destroyBodyData(e){if(e){for(let t of this._shapes)t?.users.delete(e);this._bodyCollisionObservable.delete(e.slot),this._bodyCollisionEndedObservable.delete(e.slot),this._bodies[e.slot]=void 0,this._b3._bx_DestroyBody(e.slot),e.slot=0}}removeBody(e){if(e._pluginDataInstances?.length)for(let t of e._pluginDataInstances)this._destroyBodyData(t);e._pluginData&&this._destroyBodyData(e._pluginData)}sync(e){this.syncTransform(e,e.transformNode)}_syncBodyFromBuffer(e,t,s,i){let n=e.transformNode;if(e._pluginDataInstances.length){let a=n,o=a._thinInstanceDataStorage?.matrixData;if(!o)return;let r=this._tmpVec3[0],l=t*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 u=p.TmpVectors.Matrix[0];p.Matrix.ComposeToRef(r,h,c,u),u.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(e,t,s,i,n,a,o,r){let l=this._tmpQuat[0];l.set(n,a,o,r);let h=e.parent;if(h&&!h.getWorldMatrix().isIdentity()){h.computeWorldMatrix(!0);let c=this._tmpVec3[2];c.copyFrom(e.scaling);let u=p.TmpVectors.Matrix[0],m=this._tmpVec3[3];m.set(t,s,i),p.Matrix.ComposeToRef(e.absoluteScaling,l,m,u);let b=p.TmpVectors.Matrix[1];h.getWorldMatrix().invertToRef(b);let d=p.TmpVectors.Matrix[2];u.multiplyToRef(b,d),d.decomposeToTransformNode(e),e.rotationQuaternion?.normalize(),e.scaling.copyFrom(c)}else e.position.set(t,s,i),e.rotationQuaternion?e.rotationQuaternion.copyFrom(l):l.toEulerAnglesToRef(e.rotation)}syncTransform(e,t){if(e._pluginDataInstances.length){let n=t;if(!n._thinInstanceDataStorage?.matrixData)return;for(let o=0;o<e._pluginDataInstances.length;o++){let r=e._pluginDataInstances[o];this._b3._bx_Body_GetTransform(r.slot),this._syncBodyFromBuffer(e,o,this._scratch(),0)}n.thinInstanceBufferUpdated("matrix"),this._touchedInstanceMeshes.delete(n);return}let s=e._pluginData;if(!s||!s.slot)return;this._b3._bx_Body_GetTransform(s.slot);let i=this._scratch();this._applyTransformToNode(t,i[0],i[1],i[2],i[3],i[4],i[5],i[6])}setPhysicsBodyTransformation(e,t){let s=e.getPrestepType();if(s==_.PhysicsPrestepType.TELEPORT){if(e._pluginDataInstances.length>0){let r=t._thinInstanceDataStorage?.matrixData;if(!r)return;this._createOrUpdateBodyInstances(e,e.getMotionType(),r,0,e._pluginDataInstances.length,!0);return}let i=e._pluginData,n=this._tmpVec3[0],a=this._tmpQuat[0];this._nodeTransformToRef(t,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(e,t.absolutePosition,t.absoluteRotationQuaternion):s==_.PhysicsPrestepType.DISABLED&&I.Logger.Warn("Prestep type is set to DISABLED. Unable to set physics body transformation.")}setTargetTransform(e,t,s,i){this._applyToBodyOrInstances(e,n=>{this._b3._bx_Body_SetTargetTransform(n.slot,t.x,t.y,t.z,s.x,s.y,s.z,s.w,this._timeStep)},i)}_bakesMeshCopies(e,t=new Set){return t.has(e)?!1:(t.add(e),e.children.some(s=>s.type===_.PhysicsShapeType.MESH||this._bakesMeshCopies(s,t)))}_setBodyShape(e,t){for(let s of this._shapes)s?.users.delete(e);this._b3._bx_Body_SetShape(e.slot,t?t.slot:0),t&&t.users.add(e),this._applyEventFlags(e),this._internalUpdateMassProperties(e)}setShape(e,t){let s=t&&t._pluginData?t._pluginData:null;this._applyToBodyOrInstances(e,i=>this._setBodyShape(i,s))}getShape(e){return e.shape??null}getShapeType(e){return e.type??e._pluginData?.type??_.PhysicsShapeType.CONTAINER}_applyEventFlags(e){let t=e.eventMask&5?1:0,s=e.eventMask&2?1:0;this._b3._bx_Body_SetEventFlags(e.slot,t,s)}setEventMask(e,t,s){this._applyToBodyOrInstances(e,i=>{i.eventMask=t,this._applyEventFlags(i)},s)}getEventMask(e,t){return this._getPluginReference(e,t).eventMask}setMotionType(e,t,s){this._applyToBodyOrInstances(e,i=>{i.motionType=t,this._applyNativeMotionType(i),this._internalUpdateMassProperties(i)},s)}_applyNativeMotionType(e){let t=e.activation===_.PhysicsActivationControl.ALWAYS_INACTIVE?0:this._motionTypeToNative(e.motionType);this._b3._bx_Body_SetType(e.slot,t)}setActivationControl(e,t,s){this._applyToBodyOrInstances(e,i=>{if(i.activation===t)return;let n=i.activation===_.PhysicsActivationControl.ALWAYS_INACTIVE;if(i.activation=t,t===_.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,t===_.PhysicsActivationControl.ALWAYS_ACTIVE?0:1),n&&t===_.PhysicsActivationControl.SIMULATION_CONTROLLED&&this._b3._bx_Body_SetAwake(i.slot,0)},s)}getMotionType(e,t){return this._getPluginReference(e,t).motionType}_readMassData(e){e.motionType===_.PhysicsMotionType.DYNAMIC&&e.activation!==_.PhysicsActivationControl.ALWAYS_INACTIVE?this._b3._bx_Body_GetMassData(e.slot):this._b3._bx_Body_ComputeShapeMassData(e.slot);let t=this._scratch(),s=t[0],i=new p.Vector3,n=new p.Quaternion;return Ve([t[4],t[5],t[6],t[7],t[8],t[9]],i,n),s>0&&i.scaleInPlace(1/s),{mass:s,centerOfMass:new p.Vector3(t[1],t[2],t[3]),inertia:i,inertiaOrientation:n}}_resolveMassProperties(e){let t=this._readMassData(e),s=e.userMassProps??{};return{mass:s.mass!==void 0&&s.mass>0?s.mass:t.mass,centerOfMass:s.centerOfMass??t.centerOfMass,inertia:s.inertia??t.inertia,inertiaOrientation:s.inertiaOrientation??(s.inertia?p.Quaternion.Identity():t.inertiaOrientation)}}_internalUpdateMassProperties(e){if(e.motionType!==_.PhysicsMotionType.DYNAMIC||e.activation===_.PhysicsActivationControl.ALWAYS_INACTIVE){this._applyMotionLocks(e,!1,!1,!1);return}this._b3._bx_Body_ApplyMassFromShapes(e.slot);let t=e.userMassProps;if(!(!!t&&(t.mass!==void 0||!!t.centerOfMass||!!t.inertia||!!t.inertiaOrientation))){this._applyMotionLocks(e,!1,!1,!1);return}let i=this._resolveMassProperties(e),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)*Je,h=[r[0]?l:a.x,r[1]?l:a.y,r[2]?l:a.z].map(u=>u*n),c=We(h,o);this._b3._bx_Body_SetMassDataFull(e.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(e,r,o)}_applyLockedAxes(e,t,s){let i=t.filter(Boolean).length;if(i===3){this._applyMotionLocks(e,!0,!0,!0);return}if(i!==2){this._applyMotionLocks(e,!1,!1,!1);return}this._b3._bx_Body_GetTransform(e.slot);let n=this._scratch(),a=this._tmpQuat[0].set(n[3],n[4],n[5],n[6]),o=t.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(e,h!==0,h!==1,h!==2):this._applyMotionLocks(e,!1,!1,!1)}_applyMotionLocks(e,t,s,i){let n=(t?1:0)|(s?2:0)|(i?4:0);n!==e.angularLocks&&(e.angularLocks=n,this._b3._bx_Body_SetMotionLocks(e.slot,0,0,0,t?1:0,s?1:0,i?1:0))}computeMassProperties(e,t){let s=this._getPluginReference(e,t);this._b3._bx_Body_ApplyMassFromShapes(s.slot);let i=this._readMassData(s);return this._internalUpdateMassProperties(s),i}setMassProperties(e,t,s){this._applyToBodyOrInstances(e,i=>{i.userMassProps=t,this._internalUpdateMassProperties(i)},s)}getMassProperties(e,t){return this._resolveMassProperties(this._getPluginReference(e,t))}setLinearDamping(e,t,s){this._applyToBodyOrInstances(e,i=>this._b3._bx_Body_SetLinearDamping(i.slot,t),s)}getLinearDamping(e,t){return this._b3._bx_Body_GetLinearDamping(this._getPluginReference(e,t).slot)}setAngularDamping(e,t,s){this._applyToBodyOrInstances(e,i=>this._b3._bx_Body_SetAngularDamping(i.slot,t),s)}getAngularDamping(e,t){return this._b3._bx_Body_GetAngularDamping(this._getPluginReference(e,t).slot)}setLinearVelocity(e,t,s){this._applyToBodyOrInstances(e,i=>this._b3._bx_Body_SetLinearVelocity(i.slot,t.x,t.y,t.z),s)}getLinearVelocityToRef(e,t,s){this._b3._bx_Body_GetLinearVelocity(this._getPluginReference(e,s).slot);let i=this._scratch();t.set(i[0],i[1],i[2])}applyImpulse(e,t,s,i){this._applyToBodyOrInstances(e,n=>this._b3._bx_Body_ApplyLinearImpulse(n.slot,t.x,t.y,t.z,s.x,s.y,s.z),i)}applyAngularImpulse(e,t,s){this._applyToBodyOrInstances(e,i=>this._b3._bx_Body_ApplyAngularImpulse(i.slot,t.x,t.y,t.z),s)}applyForce(e,t,s,i){this._applyToBodyOrInstances(e,n=>this._b3._bx_Body_ApplyForce(n.slot,t.x,t.y,t.z,s.x,s.y,s.z),i)}applyTorque(e,t,s){this._applyToBodyOrInstances(e,i=>this._b3._bx_Body_ApplyTorque(i.slot,t.x,t.y,t.z),s)}setAngularVelocity(e,t,s){this._applyToBodyOrInstances(e,i=>this._b3._bx_Body_SetAngularVelocity(i.slot,t.x,t.y,t.z),s)}getAngularVelocityToRef(e,t,s){this._b3._bx_Body_GetAngularVelocity(this._getPluginReference(e,s).slot);let i=this._scratch();t.set(i[0],i[1],i[2])}getBodyGeometry(e){let t=e.shape?._pluginData;if(!t)return{positions:[],indices:[]};let s=this._b3._bx_ShapeDesc_BuildDebugGeometry(t.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(e){this.removeBody(e),e._pluginData=void 0,e._pluginDataInstances.length=0}setCollisionCallbackEnabled(e,t,s){this.setEventMask(e,t?7:0,s)}setCollisionEndedCallbackEnabled(e,t,s){this._applyToBodyOrInstances(e,i=>{i.eventMask=t?i.eventMask|4:i.eventMask&-5,this._applyEventFlags(i)},s)}getCollisionObservable(e,t){let s=this._getPluginReference(e,t).slot,i=this._bodyCollisionObservable.get(s);return i||(i=new M.Observable,this._bodyCollisionObservable.set(s,i)),i}getCollisionEndedObservable(e,t){let s=this._getPluginReference(e,t).slot,i=this._bodyCollisionEndedObservable.get(s);return i||(i=new M.Observable,this._bodyCollisionEndedObservable.set(s,i)),i}setGravityFactor(e,t,s){this._applyToBodyOrInstances(e,i=>this._b3._bx_Body_SetGravityScale(i.slot,t),s)}getGravityFactor(e,t){return this._b3._bx_Body_GetGravityScale(this._getPluginReference(e,t).slot)}_registerShape(e,t,s){if(!t)throw new Error("Box3DPlugin: shape creation failed (degenerate geometry?)");let i=new X(t,s);return e._pluginData=i,this._shapes[t]=i,i}_withFloatBuffer(e,t){let s=this._b3._malloc(e.length*4);new Float32Array(this._b3.HEAPF32.buffer,s,e.length).set(e);try{return t(s)}finally{this._b3._free(s)}}_withByteBuffer(e,t){if(!e||e.length===0)return t(0);let s=this._b3._malloc(e.length);new Uint8Array(this._b3.HEAPU8.buffer,s,e.length).set(e);try{return t(s)}finally{this._b3._free(s)}}_withIntBuffer(e,t){let s=this._b3._malloc(e.length*4);new Int32Array(this._b3.HEAP32.buffer,s,e.length).set(e);try{return t(s)}finally{this._b3._free(s)}}_createOptionsFromGroundMesh(e){let t=e.groundMesh;if(!t)return;let s=t.getVerticesData($.VertexBuffer.PositionKind),i=t.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=t.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,m=new Float32Array((o+1)*(o+1)),b=l*2/o;m.fill(c);for(let d=0;d<n.length;d+=3){let D=Math.round((n[d+0]-h)/b),g=o-Math.round((n[d+2]-u)/b);m[g*(o+1)+D]=n[d+1]-c}e.numHeightFieldSamplesX=o+1,e.numHeightFieldSamplesZ=o+1,e.heightFieldSizeX=r.boundingBox.extendSizeWorld.x*2,e.heightFieldSizeZ=r.boundingBox.extendSizeWorld.z*2,e.heightFieldData=m}initShape(e,t,s){let i=this._b3,n=s.center??p.Vector3.ZeroReadOnly,a=s.rotation??p.Quaternion.Identity();switch(t){case _.PhysicsShapeType.SPHERE:{let o=s.radius??1;this._registerShape(e,i._bx_ShapeDesc_CreateSphere(n.x,n.y,n.z,o),t);break}case _.PhysicsShapeType.CAPSULE:{let o=s.pointA??p.Vector3.ZeroReadOnly,r=s.pointB??p.Vector3.UpReadOnly;this._registerShape(e,i._bx_ShapeDesc_CreateCapsule(o.x,o.y,o.z,r.x,r.y,r.z,s.radius??0),t);break}case _.PhysicsShapeType.BOX:{let o=s.extents??p.Vector3.OneReadOnly;this._registerShape(e,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),t);break}case _.PhysicsShapeType.CYLINDER:{let o=s.pointA??p.Vector3.ZeroReadOnly,l=(s.pointB??p.Vector3.UpReadOnly).subtract(o),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(e,i._bx_ShapeDesc_CreateCylinder(h,s.radius??0,16,o.x,o.y,o.z,c.x,c.y,c.z,c.w),t);break}case _.PhysicsShapeType.CONVEX_HULL:case _.PhysicsShapeType.MESH:{if(!s.mesh&&s.positions){this._initShapeFromPositions(e,t,s.positions,s.positionIndices);break}let o=s.mesh;if(!o)throw new Error("No mesh provided to create physics shape.");let r=t===_.PhysicsShapeType.MESH,l=!o.getScene().useRightHandedSystem,h=new Z(r,l);if(h.addNodeMeshes(o,!!s.includeChildMeshes),t===_.PhysicsShapeType.CONVEX_HULL){let c=this._withFloatBuffer(h.vertices,u=>i._bx_ShapeDesc_CreateHull(u,h.vertexCount));this._registerShape(e,c,t)}else{let c=this._withFloatBuffer(h.vertices,u=>this._withIntBuffer(h.indices,m=>i._bx_ShapeDesc_CreateMesh(u,h.vertexCount,m,h.triangleCount,1,1,1,0,0)));this._registerShape(e,c,t)}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 g=0;g<o;g++)for(let y=0;y<r;y++)l[y*o+g]=s.heightFieldData[(r-1-y)*o+g];let h=s.heightFieldSizeX/(o-1),c=s.heightFieldSizeZ/(r-1),u=o-1,m=r-1,b=s.heightFieldMaterials;if(b&&b.length!==u*m)throw new Error(`heightFieldMaterials needs ${u*m} cells (${u} \xD7 ${m}), got ${b.length}`);let d=b?new Uint8Array(u*m):null;if(b&&d)for(let g=0;g<u;g++)for(let y=0;y<m;y++)d[y*u+g]=b[(m-1-y)*u+g];let D=this._withFloatBuffer(l,g=>this._withByteBuffer(d,y=>i._bx_ShapeDesc_CreateHeightField(g,y,o,r,h,1,c,0,-s.heightFieldSizeX*.5,0,-s.heightFieldSizeZ*.5)));this._registerShape(e,D,t);break}case _.PhysicsShapeType.CONTAINER:{this._registerShape(e,i._bx_ShapeDesc_CreateContainer(),t);break}default:throw new Error("Unsupported Shape Type.")}}_initShapeFromPositions(e,t,s,i){let n=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(t===_.PhysicsShapeType.CONVEX_HULL){this._registerShape(e,this._withFloatBuffer(s,o=>n._bx_ShapeDesc_CreateHull(o,a)),t);return}if(!i||i.length%3!==0)throw new Error("a MESH shape from positions needs positionIndices, three per triangle");for(let o=0;o<i.length;o++)if(i[o]<0||i[o]>=a||!Number.isInteger(i[o]))throw new Error(`positionIndices[${o}] is ${i[o]}, but there are ${a} points`);this._registerShape(e,this._withFloatBuffer(s,o=>this._withIntBuffer(i,r=>n._bx_ShapeDesc_CreateMesh(o,a,r,i.length/3,1,1,1,0,0))),t)}_refreshShapeUsers(e,t=new Set){if(!t.has(e)){t.add(e);for(let s of e.users)this._b3._bx_Body_SetShape(s.slot,e.slot),this._applyEventFlags(s),this._internalUpdateMassProperties(s);for(let s of e.parents)this._refreshShapeUsers(s,t)}}_syncShapeUsers(e,t,s=new Set,i=e){if(!s.has(e)){s.add(e);for(let n of e.users)this._b3._bx_Body_SyncShapeDesc(n.slot,i.slot,t),t&4&&this._internalUpdateMassProperties(n);for(let n of e.parents)this._syncShapeUsers(n,t,s,i)}}setShapeFilterMembershipMask(e,t){let s=e._pluginData,i=this._b3._bx_ShapeDesc_GetMaskBits(s.slot);this._b3._bx_ShapeDesc_SetFilter(s.slot,t>>>0,i>>>0),this._syncShapeUsers(s,1)}getShapeFilterMembershipMask(e){return this._b3._bx_ShapeDesc_GetCategoryBits(e._pluginData.slot)}setShapeFilterCollideMask(e,t){let s=e._pluginData,i=this._b3._bx_ShapeDesc_GetCategoryBits(s.slot);this._b3._bx_ShapeDesc_SetFilter(s.slot,i>>>0,t>>>0),this._syncShapeUsers(s,1)}getShapeFilterCollideMask(e){return this._b3._bx_ShapeDesc_GetMaskBits(e._pluginData.slot)}setMaterial(e,t){let s=e._pluginData;s.material=t,this._b3._bx_ShapeDesc_SetMaterial(s.slot,t.friction??.5,t.restitution??0),this._syncShapeUsers(s,2)}getMaterial(e){let t=e._pluginData;return{friction:this._b3._bx_ShapeDesc_GetFriction(t.slot),staticFriction:t.material.staticFriction,restitution:this._b3._bx_ShapeDesc_GetRestitution(t.slot),frictionCombine:t.material.frictionCombine,restitutionCombine:t.material.restitutionCombine}}setDensity(e,t){let s=e._pluginData;this._b3._bx_ShapeDesc_SetDensity(s.slot,t),this._syncShapeUsers(s,4)}getDensity(e){return this._b3._bx_ShapeDesc_GetDensity(e._pluginData.slot)}addChild(e,t,s,i,n){let a=e._pluginData,o=t._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(e,t){let s=e._pluginData;this._b3._bx_ShapeDesc_RemoveChild(s.slot,t);let[i]=s.children.splice(t,1);i&&!s.children.includes(i)&&i.parents.delete(s),this._refreshShapeUsers(s)}getNumChildren(e){return this._b3._bx_ShapeDesc_GetChildCount(e._pluginData.slot)}getBoundingBox(e){this._b3._bx_ShapeDesc_GetAABB(e._pluginData.slot);let t=this._scratch();return p.TmpVectors.Vector3[0].set(t[0],t[1],t[2]),p.TmpVectors.Vector3[1].set(t[3],t[4],t[5]),new H.BoundingBox(p.TmpVectors.Vector3[0],p.TmpVectors.Vector3[1],p.Matrix.IdentityReadOnly)}getBodyBoundingBox(e){let t=this._getPluginReference(e);this._b3._bx_Body_GetAABB(t.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 H.BoundingBox(p.TmpVectors.Vector3[0],p.TmpVectors.Vector3[1],p.Matrix.IdentityReadOnly)}disposeShape(e){let t=e._pluginData;if(t){for(let s of t.parents){let i=s.children.indexOf(t);i>=0&&(s.children.splice(i,1),this._b3._bx_ShapeDesc_RemoveChild(s.slot,i))}for(let s of t.children)s.parents.delete(t);this._shapes[t.slot]=void 0,this._b3._bx_ShapeDesc_Destroy(t.slot),e._pluginData=void 0}}setTrigger(e,t){let s=e._pluginData;this._b3._bx_ShapeDesc_SetSensor(s.slot,t?1:0),this._refreshShapeUsers(s)}_frameToBuffer(e,t,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(),e[t]=s.x,e[t+1]=s.y,e[t+2]=s.z,e[t+3]=r.x,e[t+4]=r.y,e[t+5]=r.z,e[t+6]=r.w}_buildFrames(e,t,s,i){let n=t.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 u=p.Vector3.Cross(r,h),m=p.Vector3.Cross(l,c);t._initOptions||(t._initOptions={axisA:r.clone(),axisB:l.clone(),perpAxisA:h.clone(),perpAxisB:c.clone(),pivotA:a.clone(),pivotB:o.clone()});let b=e.frames;if(e.plan){let d=e.plan.basis;if(d){let D=[r,h,u],g=[l,c,m];this._frameToBuffer(b,0,a,D[d[0]],D[d[1]],D[d[2]]),this._frameToBuffer(b,7,o,g[d[0]],g[d[1]],g[d[2]])}else b.set([a.x,a.y,a.z,0,0,0,1,o.x,o.y,o.z,0,0,0,1]);return}switch(e.type){case _.PhysicsConstraintType.HINGE:case _.PhysicsConstraintType.BALL_AND_SOCKET:this._frameToBuffer(b,0,a,h,p.Vector3.Cross(r,h),r),this._frameToBuffer(b,7,o,c,p.Vector3.Cross(l,c),l);break;case _.PhysicsConstraintType.PRISMATIC:case _.PhysicsConstraintType.SLIDER:this._frameToBuffer(b,0,a,r,h,u),this._frameToBuffer(b,7,o,l,c,m);break;case _.PhysicsConstraintType.LOCK:{this._b3._bx_Body_GetTransform(s.slot);let d=this._scratch(),D=this._tmpQuat[0];D.set(d[3],d[4],d[5],d[6]),this._b3._bx_Body_GetTransform(i.slot),d=this._scratch();let g=this._tmpQuat[1];g.set(d[3],d[4],d[5],d[6]);let y=g.invert().multiply(D).normalize();b.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:b.set([a.x,a.y,a.z,0,0,0,1,o.x,o.y,o.z,0,0,0,1]);break}}_createJoint(e,t,s){this._scratch().set(e.frames);let i=this._b3._bx_CreateJoint(this._worldSlot,e.jointType,t.slot,s.slot,e.collisions?1:0,e.length);return i?(this._applyAxisState(e,i,t,s),i):(I.Logger.Warn("Box3DPlugin: joint creation failed."),0)}_readSixDofLimits(e,t){for(let s of t??[]){let i=this._axisState(e,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(e){let t=_.PhysicsConstraintAxis,s=_.PhysicsConstraintAxisLimitMode,i=y=>e.axes.get(y)?.mode??s.FREE,n=y=>{let f=e.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 e.axes)(f.stiffness>0||f.damping>0)&&f.stiffness>=l.stiffness&&(l.stiffness=f.stiffness,l.damping=f.damping,l.angularStiffness=y===t.ANGULAR_X||y===t.ANGULAR_Y||y===t.ANGULAR_Z);let h=[t.LINEAR_X,t.LINEAR_Y,t.LINEAR_Z],c=[t.ANGULAR_X,t.ANGULAR_Y,t.ANGULAR_Z],u=h.filter(y=>i(y)===s.LOCKED).length,m=h.filter(y=>i(y)===s.FREE).length,b=c.filter(y=>i(y)!==s.LOCKED),d=c.filter(y=>i(y)===s.FREE).length;if(i(t.LINEAR_DISTANCE)!==s.FREE){if(m===3&&d===3){let[y,f]=n(t.LINEAR_DISTANCE);return l.jointType=4,l.basis=null,l.primaryAxis=t.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&&m===0&&(this._warnOnce("sixdof-linear-limited","SIX_DOF: two or more limited linear axes are treated as locked with Box3D."),g=!0),g){if(b.length===0)return l;if(b.length===1){let f=b[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,C]=n(f);l.limit=[a(S,o),a(C,o)]}return l}if(l.jointType=1,l.basis=[1,2,0],i(t.ANGULAR_X)!==s.FREE){let[f,S]=n(t.ANGULAR_X);l.twist=[a(f,o),a(S,o)]}let y=[i(t.ANGULAR_Y),i(t.ANGULAR_Z)];if(y.some(f=>f!==s.FREE)){let f=[],S=!1;for(let P of[t.ANGULAR_Y,t.ANGULAR_Z]){if(i(P)===s.FREE)continue;let[v,T]=n(P);f.push(Math.max(Math.abs(v),Math.abs(T))),S||(S=Math.abs(v+T)>1e-4)}let C=Math.max(...f);isFinite(C)&&((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."),C>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(C,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]=n(y);l.limit=[a(f,r),a(S,r)]}return b.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,(m!==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(e,t,s,i,n){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},o=a(i)+a(n),r=o>0?1/o:1,l=Math.max(e,0);return{hertz:Math.sqrt(l/r)/(2*Math.PI),dampingRatio:l>0?t/(2*Math.sqrt(l*r)):1}}initConstraint(e,t,s,i,n){let a=e.type,o=e.options;if(!a||!o){I.Logger.Warn("No constraint type or options. Constraint is invalid.");return}if(t._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=!e._pluginData,l=e._pluginData??new q;switch(e._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,e.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(t,i),c=this._getPluginReference(s,n);this._buildFrames(l,e,h,c),this._updateJointLength(l,o.maxDistance,h,c);let u=l.enabled?this._createJoint(l,h,c):0;l.joints.push(u),l.pairs.push({parent:t,parentIndex:i??0,child:s,childIndex:n??0,parentData:h,childData:c})}_updateJointLength(e,t,s,i){if(e.type===_.PhysicsConstraintType.DISTANCE)e.length=t??0;else if(e.plan?.jointType===4&&e.plan.limit)e.length=e.plan.limit[0];else return;e.length<=0&&(e.length=Math.max(.01,this._currentPivotDistance(e,s,i)))}_currentPivotDistance(e,t,s){let i=e.frames,n=this._tmpVec3[0],a=this._tmpVec3[1];this._b3._bx_Body_GetTransform(t.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(e,t){let s=e._pluginData;!s||s.enabled===t||(s.enabled=t,this._recreateJoints(s))}_destroyJoint(e){this._motorJoints.delete(e),this._b3._bx_DestroyJoint(e)}_recreateJoints(e){for(let t=0;t<e.pairs.length;t++){e.joints[t]&&this._destroyJoint(e.joints[t]);let s=e.pairs[t];e.joints[t]=e.enabled&&s.parentData.slot&&s.childData.slot?this._createJoint(e,s.parentData,s.childData):0}}getEnabled(e){return e._pluginData?.enabled??!1}setCollisionsEnabled(e,t){let s=e._pluginData;s.collisions=t;for(let i of s.joints)i&&this._b3._bx_Joint_SetCollideConnected(i,t?1:0)}getCollisionsEnabled(e){return e._pluginData?.collisions??!1}_primaryAxis(e){switch(e.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 e.plan?.primaryAxis??null;default:return null}}_isAngularAxis(e){return e===_.PhysicsConstraintAxis.ANGULAR_X||e===_.PhysicsConstraintAxis.ANGULAR_Y||e===_.PhysicsConstraintAxis.ANGULAR_Z}_isPrimaryAxis(e,t){return e.type===_.PhysicsConstraintType.BALL_AND_SOCKET||e.plan?.jointType===1?this._isAngularAxis(t):t===this._primaryAxis(e)}_axisState(e,t){let s=e.axes.get(t);return s||(s={mode:_.PhysicsConstraintAxisLimitMode.FREE,min:0,max:0,motor:_.PhysicsConstraintMotorType.NONE,target:0,maxForce:0,friction:0,stiffness:0,damping:0},e.axes.set(t,s)),s}_applyAxisState(e,t,s,i){let n=this._b3;if(e.plan){this._applySixDofPlan(e,t,s,i),n._bx_Joint_WakeBodies(t);return}if(this._primaryAxis(e)!==null){for(let[o,r]of e.axes){if(!this._isPrimaryAxis(e,o))continue;if(e.type===_.PhysicsConstraintType.BALL_AND_SOCKET&&o===_.PhysicsConstraintAxis.ANGULAR_X){n._bx_Joint_SetTwistLimits(t,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(t,h,c)}n._bx_Joint_EnableLimit(t,l?1:0),e.type!==_.PhysicsConstraintType.BALL_AND_SOCKET&&this._applyMotor(t,r)}e.type===_.PhysicsConstraintType.BALL_AND_SOCKET&&this._applySphericalMotor(e,t),n._bx_Joint_WakeBodies(t)}}_applySphericalMotor(e,t){let s=this._b3,i=e.axes.get(_.PhysicsConstraintAxis.ANGULAR_X),n=e.axes.get(_.PhysicsConstraintAxis.ANGULAR_Y),a=e.axes.get(_.PhysicsConstraintAxis.ANGULAR_Z),o=[i,n,a],r=m=>m?.motor===_.PhysicsConstraintMotorType.VELOCITY?m.target:0,l=o.some(m=>m?.motor===_.PhysicsConstraintMotorType.VELOCITY),h=o.some(m=>m?.motor===_.PhysicsConstraintMotorType.POSITION),c=Math.max(...o.map(m=>m?.motor===_.PhysicsConstraintMotorType.VELOCITY?m.maxForce:0));s._bx_Joint_SetSphericalMotor(t,l?1:0,r(n),r(a),r(i),c);let u=r(n)!==0||r(a)!==0||r(i)!==0;if(l&&u?this._motorJoints.add(t):this._motorJoints.delete(t),s._bx_Joint_EnableSpring(t,h?1:0),h){let m=D=>D?.motor===_.PhysicsConstraintMotorType.POSITION?D.target:0,b=this._tmpQuat[0];p.Quaternion.RotationYawPitchRollToRef(0,0,m(i),b);let d=this._tmpQuat[1];p.Quaternion.RotationYawPitchRollToRef(m(a),m(n),0,d),b.multiplyInPlace(d),s._bx_Joint_SetSpring(t,5,1),s._bx_Joint_SetSphericalTarget(t,b.x,b.y,b.z,b.w)}}_applyMotor(e,t){let s=this._b3;switch(t.motor){case _.PhysicsConstraintMotorType.VELOCITY:s._bx_Joint_EnableSpring(e,0),s._bx_Joint_EnableMotor(e,1),s._bx_Joint_SetMotorSpeed(e,t.target),s._bx_Joint_SetMaxMotorForce(e,t.maxForce);break;case _.PhysicsConstraintMotorType.POSITION:s._bx_Joint_EnableMotor(e,0),s._bx_Joint_EnableSpring(e,1),s._bx_Joint_SetSpring(e,5,1),s._bx_Joint_SetTarget(e,t.target);break;default:s._bx_Joint_EnableMotor(e,0),s._bx_Joint_EnableSpring(e,0);break}}_applySixDofPlan(e,t,s,i){let n=this._b3,a=e.plan;switch(a.jointType){case 1:a.coneAngle>=0&&n._bx_Joint_SetLimits(t,-a.coneAngle,a.coneAngle),n._bx_Joint_EnableLimit(t,a.coneAngle>=0?1:0),n._bx_Joint_SetTwistLimits(t,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(t,a.limit[0],a.limit[1]),n._bx_Joint_EnableLimit(t,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(t,0),n._bx_Joint_EnableSpring(t,1),n._bx_Joint_SetSpring(t,o.hertz,o.dampingRatio),n._bx_Joint_SetTarget(t,e.length)}else n._bx_Joint_SetLimits(t,a.limit[0],a.limit[1]),n._bx_Joint_EnableLimit(t,1),n._bx_Joint_EnableSpring(t,1),n._bx_Joint_SetSpring(t,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(t,o.hertz,o.dampingRatio)}if(a.jointType===1)this._applySphericalMotor(e,t);else if(a.primaryAxis!==null&&a.jointType!==4){let o=e.axes.get(a.primaryAxis);o&&this._applyMotor(t,o)}}_setAxis(e,t,s){let i=e._pluginData;if(!i)return;let n=this._axisState(i,t),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,t)&&this._warnOnce(`axis-motor-${t}`,`SIX_DOF: a motor on axis ${t} has no effect with the Box3D joint this constraint maps to.`);let h=i.pairs[0];if(h&&this._updateJointLength(i,e.options.maxDistance,h.parentData,h.childData),r){h&&this._buildFrames(i,e,h.parentData,h.childData),this._recreateJoints(i);return}}else if(!this._isPrimaryAxis(i,t)){this._warnOnce(`axis-${i.type}-${t}`,`axis ${t} 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(e,t,s){let i=e._pluginData;i&&(this._axisState(i,t).friction=s)}getAxisFriction(e,t){return e._pluginData?.axes.get(t)?.friction??null}setAxisMode(e,t,s){this._setAxis(e,t,i=>i.mode=s)}getAxisMode(e,t){return e._pluginData?.axes.get(t)?.mode??null}setAxisMinLimit(e,t,s){this._setAxis(e,t,i=>{i.min=s,i.mode===_.PhysicsConstraintAxisLimitMode.FREE&&(i.mode=_.PhysicsConstraintAxisLimitMode.LIMITED)})}getAxisMinLimit(e,t){return e._pluginData?.axes.get(t)?.min??null}setAxisMaxLimit(e,t,s){this._setAxis(e,t,i=>{i.max=s,i.mode===_.PhysicsConstraintAxisLimitMode.FREE&&(i.mode=_.PhysicsConstraintAxisLimitMode.LIMITED)})}getAxisMaxLimit(e,t){return e._pluginData?.axes.get(t)?.max??null}setAxisMotorType(e,t,s){this._setAxis(e,t,i=>i.motor=s)}getAxisMotorType(e,t){return e._pluginData?.axes.get(t)?.motor??null}setAxisMotorTarget(e,t,s){this._setAxis(e,t,i=>i.target=s)}getAxisMotorTarget(e,t){return e._pluginData?.axes.get(t)?.target??null}setAxisMotorMaxForce(e,t,s){this._setAxis(e,t,i=>i.maxForce=s)}getAxisMotorMaxForce(e,t){return e._pluginData?.axes.get(t)?.maxForce??null}disposeConstraint(e){let t=e._pluginData;if(t){for(let s of t.joints)s&&this._destroyJoint(s);t.joints.length=0,t.pairs.length=0}}getBodiesUsingConstraint(e){let t=e._pluginData;return t?t.pairs.map(s=>({parentBody:s.parent,parentBodyIndex:s.parentIndex,childBody:s.child,childBodyIndex:s.childIndex})):[]}addConstraint(e,t,s,i,n){this.initConstraint(s,e,t,i,n)}explode(e,t,s,i=0){this._b3._bx_World_Explode(this._worldSlot,e.x,e.y,e.z,t,i,s)}setShapeFilterGroup(e,t){let s=e._pluginData;this._b3._bx_ShapeDesc_SetGroup(s.slot,t|0),this._syncShapeUsers(s,1)}setShapeRollingResistance(e,t){let s=e._pluginData;this._b3._bx_ShapeDesc_SetRollingResistance(s.slot,t),this._syncShapeUsers(s,2)}setAllowFastRotation(e,t,s){this._applyToBodyOrInstances(e,i=>this._b3._bx_Body_AllowFastRotation(i.slot,t?1:0),s)}createWheelJoint(e,t,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(e),l=this._getPluginReference(t);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 w(this,u)}createParallelJoint(e,t,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(),u=c.getNormalToRef(new p.Vector3);return this._frameToBuffer(r,7,p.Vector3.ZeroReadOnly,u,p.Vector3.Cross(c,u),c),r.set([n,a,o],14),this._b3._bx_CreateJoint(this._worldSlot,7,this._getPluginReference(e).slot,this._getPluginReference(t).slot,1,0)}disposeExtraJoint(e){this._destroyExtraJoint(e)}_destroyExtraJoint(e){e&&this._destroyJoint(e)}_wheelCall(e,t,...s){t&&this._b3[e](t,...s)}raycast(e,t,s,i){let n=Array.isArray(s)?s:[s];for(let b of n)b.reset(e,t);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,e.x,e.y,e.z,t.x-e.x,t.y-e.y,t.z-e.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*G),u=[];for(let b=0;b<h;b++)u.push(b);if(u.sort((b,d)=>c[b*G+6]-c[d*G+6]),!n.length)for(let b=0;b<h;b++){let d=new Ce.PhysicsRaycastResult;d.reset(e,t),n.push(d)}let m=Math.min(h,n.length);for(let b=0;b<m;b++){let d=u[b]*G,D=n[b],g=this._bodies[c[d+7]];D.body=g?.body,D.bodyIndex=g?.index,D.shape=g?.body.shape??void 0,D.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]),D.calculateHitDistance()}}_readEvents(e,t,s,i){let n=new Float32Array(this._b3.HEAPF32.buffer,e,t*s).slice(),a=new Array(t*2);for(let o=0;o<t;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 e=this._b3._bx_World_GetContactEvents(this._worldSlot);if(!e)return;let{records:t,refs:s}=this._readEvents(this._b3._bx_ContactEventsPtr(),e,Be,3);for(let i=0;i<e;i++){let n=i*Be,a=t[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 b={collider:o.body,colliderIndex:o.index,collidedAgainst:r.body,collidedAgainstIndex:r.index,type:_.PhysicsEventType.COLLISION_FINISHED};this.onCollisionEndedObservable.notifyObservers(b),this._bodyCollisionEndedObservable.get(o.data.slot)?.notifyObservers(b);let d=this._bodyCollisionEndedObservable.get(r.data.slot);d&&d.notifyObservers({...b,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(t[n+5],t[n+6],t[n+7]),u=new p.Vector3(t[n+8],t[n+9],t[n+10]),m={collider:o.body,colliderIndex:o.index,collidedAgainst:r.body,collidedAgainstIndex:r.index,type:h,point:c,normal:u,distance:0,impulse:t[n+11]};if(this.onCollisionObservable.notifyObservers(m),this._bodyCollisionObservable.size){this._bodyCollisionObservable.get(o.data.slot)?.notifyObservers(m);let b=this._bodyCollisionObservable.get(r.data.slot);b&&b.notifyObservers({...m,collider:r.body,colliderIndex:r.index,collidedAgainst:o.body,collidedAgainstIndex:o.index,normal:u?u.negate():null})}}}_notifyTriggers(){let e=this._b3._bx_World_GetSensorEvents(this._worldSlot);if(!e||!this.onTriggerCollisionObservable.hasObservers())return;let{records:t,refs:s}=this._readEvents(this._b3._bx_SensorEventsPtr(),e,Ae,3);for(let i=0;i<e;i++){let n=i*Ae,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:t[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 e of this._shapes)e&&this._b3._bx_ShapeDesc_Destroy(e.slot);this._shapes.length=0,this._bodies.length=0,this._worldSlot&&(this._b3._bx_DestroyWorld(this._worldSlot),this._worldSlot=0)}};ee();function V(){if(typeof SharedArrayBuffer>"u")return!1;let x=globalThis.crossOriginIsolated;return x===void 0||x}async function Me(x={}){let{threads:e=!1,...t}=x;if(e==="auto"?V():e){if(e===!0&&!V())throw new Error('babylon-box3d: the threaded build needs SharedArrayBuffer, which this page does not have. Serve it with the Cross-Origin-Opener-Policy: same-origin and Cross-Origin-Embedder-Policy: require-corp headers, or use LoadBox3D({ threads: "auto" }) to fall back to the single threaded build.');return(await Promise.resolve().then(()=>(Ie(),Te))).default(t)}return(await Promise.resolve().then(()=>(ee(),ve))).default(t)}return Ge(Ye);})();
|
|
3
3
|
//# sourceMappingURL=babylon.box3d.min.js.map
|