summit/frontend/node_modules/three-stdlib/physics/AmmoPhysics.cjs.map

1 line
12 KiB
Plaintext
Raw Normal View History

2025-12-08 16:31:30 +00:00
{"version":3,"file":"AmmoPhysics.cjs","sources":["../../src/physics/AmmoPhysics.js"],"sourcesContent":["async function AmmoPhysics() {\n if ('Ammo' in window === false) {\n console.error(\"AmmoPhysics: Couldn't find Ammo.js\")\n return\n }\n\n const AmmoLib = await Ammo()\n\n const frameRate = 60\n\n const collisionConfiguration = new AmmoLib.btDefaultCollisionConfiguration()\n const dispatcher = new AmmoLib.btCollisionDispatcher(collisionConfiguration)\n const broadphase = new AmmoLib.btDbvtBroadphase()\n const solver = new AmmoLib.btSequentialImpulseConstraintSolver()\n const world = new AmmoLib.btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration)\n world.setGravity(new AmmoLib.btVector3(0, -9.8, 0))\n\n const worldTransform = new AmmoLib.btTransform()\n\n //\n\n function getShape(geometry) {\n const parameters = geometry.parameters\n\n // TODO change type to is*\n\n if (geometry.type === 'BoxGeometry') {\n const sx = parameters.width !== undefined ? parameters.width / 2 : 0.5\n const sy = parameters.height !== undefined ? parameters.height / 2 : 0.5\n const sz = parameters.depth !== undefined ? parameters.depth / 2 : 0.5\n\n const shape = new AmmoLib.btBoxShape(new AmmoLib.btVector3(sx, sy, sz))\n shape.setMargin(0.05)\n\n return shape\n } else if (geometry.type === 'SphereGeometry' || geometry.type === 'IcosahedronGeometry') {\n const radius = parameters.radius !== undefined ? parameters.radius : 1\n\n const shape = new AmmoLib.btSphereShape(radius)\n shape.setMargin(0.05)\n\n return shape\n }\n\n return null\n }\n\n const meshes = []\n const meshMap = new WeakMap()\n\n function addMesh(mesh, mass = 0) {\n const shape = getShape(mesh.geometry)\n\n if (shape !== null) {\n if (mesh.isInstancedMesh) {\n handleInstancedMesh(mesh, mass, shape)\n } else if (mesh.isMesh) {\n handleMesh(mesh, mass, shape)\n }\n }\n }\n\n function handleMesh(mesh, mass, shape) {\n const position = mesh.position\n const quaternion = mesh.quaternion\n\n const transform = new AmmoLib.btTransform()\n transform.setIdentity()\n transform.setOrigin(new AmmoLib.btVector3(position.x, position.y, position.z))\n transform.setRotation(new AmmoLib.btQuaternion(quaternion.x, quaternion.y, quaternion.z, quaternion.w))\n\n const motionState = new AmmoLib.btDefaultMotionState(transform)\n\n const localInertia = new AmmoLib.btVector3(0, 0, 0)\n shape.calculateLocalInertia(mass, localInertia)\n\n const rbInfo = new AmmoLib.btRigidBodyConstructionInfo(mass, motionState, shape, localInertia)\n\n const body = new AmmoLib.btRigidBody(rbInfo)\n // body.setFriction( 4 );\n world.addRigidBody(body)\n\n if (mass > 0) {\n meshes.push(mesh)\n meshMap.set(mesh, body)\n }\n }\n\n function handleInstancedMesh(mesh, mass, shape) {\n const array = mesh.instanceMatrix.array\n\n const bodies = []\n\n for (let i = 0; i < mesh.count; i++) {\n const index = i * 16\n\n const transform = new AmmoLib.btTransform()\n transform.setFromOpenGLMatrix(array.slice(index, index + 16))\n\n const motionState = new AmmoLib.btDefaultMotionState(transform)\n\n const localInertia = new AmmoLib.btVector3(0, 0, 0)\n shape.calculateLocalInertia(mass, localInertia)\n\n const rbInfo = new AmmoLib.btRigidBodyConstructionInfo(mass, motionState, shape, localInertia)\n\n const body = new AmmoLib.btRigidBody(rbInfo)\n world.addRigidBody(body)\n\n bodies.push(body)\n }\n\n if (mass > 0) {\n mesh.instanceMatrix.setUsage(35048) // THREE.DynamicDrawUsage = 35048\n meshes.push(mesh)\n\n meshMap.set(mesh, bodies)\n }\n }\n\n //\n\n function setMeshPosition(mesh, position, index = 0) {\n if (mesh.isInstancedMesh) {\n const bodies = meshMap.get(mesh)\n const body = bodies[index]\n\n body.setAngularVelocity(new AmmoLib.btVector3(0, 0, 0))\n body.setLinearVelocity(new AmmoLib.btVector3(0, 0, 0))\n\