feat: initialize project with core dependencies and game entry point
This commit is contained in:
359
node_modules/three/examples/jsm/physics/AmmoPhysics.js
generated
vendored
Normal file
359
node_modules/three/examples/jsm/physics/AmmoPhysics.js
generated
vendored
Normal file
@@ -0,0 +1,359 @@
|
||||
/**
|
||||
* @classdesc Can be used to include Ammo.js as a Physics engine into
|
||||
* `three.js` apps. Make sure to include `ammo.wasm.js` first:
|
||||
* ```
|
||||
* <script src="jsm/libs/ammo.wasm.js"></script>
|
||||
* ```
|
||||
* It is then possible to initialize the API via:
|
||||
* ```js
|
||||
* const physics = await AmmoPhysics();
|
||||
* ```
|
||||
*
|
||||
* @name AmmoPhysics
|
||||
* @class
|
||||
* @hideconstructor
|
||||
* @three_import import { AmmoPhysics } from 'three/addons/physics/AmmoPhysics.js';
|
||||
*/
|
||||
async function AmmoPhysics() {
|
||||
|
||||
if ( 'Ammo' in window === false ) {
|
||||
|
||||
console.error( 'AmmoPhysics: Couldn\'t find Ammo.js' );
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
const AmmoLib = await Ammo();
|
||||
|
||||
const frameRate = 60;
|
||||
|
||||
const collisionConfiguration = new AmmoLib.btDefaultCollisionConfiguration();
|
||||
const dispatcher = new AmmoLib.btCollisionDispatcher( collisionConfiguration );
|
||||
const broadphase = new AmmoLib.btDbvtBroadphase();
|
||||
const solver = new AmmoLib.btSequentialImpulseConstraintSolver();
|
||||
const world = new AmmoLib.btDiscreteDynamicsWorld( dispatcher, broadphase, solver, collisionConfiguration );
|
||||
world.setGravity( new AmmoLib.btVector3( 0, - 9.8, 0 ) );
|
||||
|
||||
const worldTransform = new AmmoLib.btTransform();
|
||||
|
||||
//
|
||||
|
||||
function getShape( geometry ) {
|
||||
|
||||
const parameters = geometry.parameters;
|
||||
|
||||
// TODO change type to is*
|
||||
|
||||
if ( geometry.type === 'BoxGeometry' ) {
|
||||
|
||||
const sx = parameters.width !== undefined ? parameters.width / 2 : 0.5;
|
||||
const sy = parameters.height !== undefined ? parameters.height / 2 : 0.5;
|
||||
const sz = parameters.depth !== undefined ? parameters.depth / 2 : 0.5;
|
||||
|
||||
const shape = new AmmoLib.btBoxShape( new AmmoLib.btVector3( sx, sy, sz ) );
|
||||
shape.setMargin( 0.05 );
|
||||
|
||||
return shape;
|
||||
|
||||
} else if ( geometry.type === 'SphereGeometry' || geometry.type === 'IcosahedronGeometry' ) {
|
||||
|
||||
const radius = parameters.radius !== undefined ? parameters.radius : 1;
|
||||
|
||||
const shape = new AmmoLib.btSphereShape( radius );
|
||||
shape.setMargin( 0.05 );
|
||||
|
||||
return shape;
|
||||
|
||||
}
|
||||
|
||||
console.error( 'AmmoPhysics: Unsupported geometry type:', geometry.type );
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
const meshes = [];
|
||||
const meshMap = new WeakMap();
|
||||
|
||||
function addScene( scene ) {
|
||||
|
||||
scene.traverse( function ( child ) {
|
||||
|
||||
if ( child.isMesh ) {
|
||||
|
||||
const physics = child.userData.physics;
|
||||
|
||||
if ( physics ) {
|
||||
|
||||
addMesh( child, physics.mass, physics.restitution );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
}
|
||||
|
||||
function addMesh( mesh, mass = 0, restitution = 0 ) {
|
||||
|
||||
const shape = getShape( mesh.geometry );
|
||||
|
||||
if ( shape !== null ) {
|
||||
|
||||
if ( mesh.isInstancedMesh ) {
|
||||
|
||||
handleInstancedMesh( mesh, shape, mass, restitution );
|
||||
|
||||
} else if ( mesh.isMesh ) {
|
||||
|
||||
handleMesh( mesh, shape, mass, restitution );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function handleMesh( mesh, shape, mass, restitution ) {
|
||||
|
||||
const position = mesh.position;
|
||||
const quaternion = mesh.quaternion;
|
||||
|
||||
const transform = new AmmoLib.btTransform();
|
||||
transform.setIdentity();
|
||||
transform.setOrigin( new AmmoLib.btVector3( position.x, position.y, position.z ) );
|
||||
transform.setRotation( new AmmoLib.btQuaternion( quaternion.x, quaternion.y, quaternion.z, quaternion.w ) );
|
||||
|
||||
const motionState = new AmmoLib.btDefaultMotionState( transform );
|
||||
|
||||
const localInertia = new AmmoLib.btVector3( 0, 0, 0 );
|
||||
shape.calculateLocalInertia( mass, localInertia );
|
||||
|
||||
const rbInfo = new AmmoLib.btRigidBodyConstructionInfo( mass, motionState, shape, localInertia );
|
||||
rbInfo.set_m_restitution( restitution );
|
||||
|
||||
const body = new AmmoLib.btRigidBody( rbInfo );
|
||||
// body.setFriction( 4 );
|
||||
world.addRigidBody( body );
|
||||
|
||||
if ( mass > 0 ) {
|
||||
|
||||
meshes.push( mesh );
|
||||
meshMap.set( mesh, body );
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
function handleInstancedMesh( mesh, shape, mass, restitution ) {
|
||||
|
||||
const array = mesh.instanceMatrix.array;
|
||||
|
||||
const bodies = [];
|
||||
|
||||
for ( let i = 0; i < mesh.count; i ++ ) {
|
||||
|
||||
const index = i * 16;
|
||||
|
||||
const transform = new AmmoLib.btTransform();
|
||||
transform.setFromOpenGLMatrix( array.slice( index, index + 16 ) );
|
||||
|
||||
const motionState = new AmmoLib.btDefaultMotionState( transform );
|
||||
|
||||
const localInertia = new AmmoLib.btVector3( 0, 0, 0 );
|
||||
shape.calculateLocalInertia( mass, localInertia );
|
||||
|
||||
const rbInfo = new AmmoLib.btRigidBodyConstructionInfo( mass, motionState, shape, localInertia );
|
||||
rbInfo.set_m_restitution( restitution );
|
||||
|
||||
const body = new AmmoLib.btRigidBody( rbInfo );
|
||||
world.addRigidBody( body );
|
||||
|
||||
bodies.push( body );
|
||||
|
||||
}
|
||||
|
||||
if ( mass > 0 ) {
|
||||
|
||||
meshes.push( mesh );
|
||||
|
||||
meshMap.set( mesh, bodies );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
function setMeshPosition( mesh, position, index = 0 ) {
|
||||
|
||||
if ( mesh.isInstancedMesh ) {
|
||||
|
||||
const bodies = meshMap.get( mesh );
|
||||
const body = bodies[ index ];
|
||||
|
||||
body.setAngularVelocity( new AmmoLib.btVector3( 0, 0, 0 ) );
|
||||
body.setLinearVelocity( new AmmoLib.btVector3( 0, 0, 0 ) );
|
||||
|
||||
worldTransform.setIdentity();
|
||||
worldTransform.setOrigin( new AmmoLib.btVector3( position.x, position.y, position.z ) );
|
||||
body.setWorldTransform( worldTransform );
|
||||
|
||||
} else if ( mesh.isMesh ) {
|
||||
|
||||
const body = meshMap.get( mesh );
|
||||
|
||||
body.setAngularVelocity( new AmmoLib.btVector3( 0, 0, 0 ) );
|
||||
body.setLinearVelocity( new AmmoLib.btVector3( 0, 0, 0 ) );
|
||||
|
||||
worldTransform.setIdentity();
|
||||
worldTransform.setOrigin( new AmmoLib.btVector3( position.x, position.y, position.z ) );
|
||||
body.setWorldTransform( worldTransform );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
let lastTime = 0;
|
||||
|
||||
function step() {
|
||||
|
||||
const time = performance.now();
|
||||
|
||||
if ( lastTime > 0 ) {
|
||||
|
||||
const delta = ( time - lastTime ) / 1000;
|
||||
|
||||
world.stepSimulation( delta, 10 );
|
||||
|
||||
//
|
||||
|
||||
for ( let i = 0, l = meshes.length; i < l; i ++ ) {
|
||||
|
||||
const mesh = meshes[ i ];
|
||||
|
||||
if ( mesh.isInstancedMesh ) {
|
||||
|
||||
const array = mesh.instanceMatrix.array;
|
||||
const bodies = meshMap.get( mesh );
|
||||
|
||||
for ( let j = 0; j < bodies.length; j ++ ) {
|
||||
|
||||
const body = bodies[ j ];
|
||||
|
||||
const motionState = body.getMotionState();
|
||||
motionState.getWorldTransform( worldTransform );
|
||||
|
||||
const position = worldTransform.getOrigin();
|
||||
const quaternion = worldTransform.getRotation();
|
||||
|
||||
compose( position, quaternion, array, j * 16 );
|
||||
|
||||
}
|
||||
|
||||
mesh.instanceMatrix.needsUpdate = true;
|
||||
mesh.computeBoundingSphere();
|
||||
|
||||
} else if ( mesh.isMesh ) {
|
||||
|
||||
const body = meshMap.get( mesh );
|
||||
|
||||
const motionState = body.getMotionState();
|
||||
motionState.getWorldTransform( worldTransform );
|
||||
|
||||
const position = worldTransform.getOrigin();
|
||||
const quaternion = worldTransform.getRotation();
|
||||
mesh.position.set( position.x(), position.y(), position.z() );
|
||||
mesh.quaternion.set( quaternion.x(), quaternion.y(), quaternion.z(), quaternion.w() );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
lastTime = time;
|
||||
|
||||
}
|
||||
|
||||
// animate
|
||||
|
||||
setInterval( step, 1000 / frameRate );
|
||||
|
||||
return {
|
||||
/**
|
||||
* Adds the given scene to this physics simulation. Only meshes with a
|
||||
* `physics` object in their {@link Object3D#userData} field will be honored.
|
||||
* The object can be used to store the mass of the mesh. E.g.:
|
||||
* ```js
|
||||
* box.userData.physics = { mass: 1 };
|
||||
* ```
|
||||
*
|
||||
* @method
|
||||
* @name AmmoPhysics#addScene
|
||||
* @param {Object3D} scene The scene or any type of 3D object to add.
|
||||
*/
|
||||
addScene: addScene,
|
||||
|
||||
/**
|
||||
* Adds the given mesh to this physics simulation.
|
||||
*
|
||||
* @method
|
||||
* @name AmmoPhysics#addMesh
|
||||
* @param {Mesh} mesh The mesh to add.
|
||||
* @param {number} [mass=0] The mass in kg of the mesh.
|
||||
* @param {number} [restitution=0] The restitution of the mesh, usually from 0 to 1. Represents how "bouncy" objects are when they collide with each other.
|
||||
*/
|
||||
addMesh: addMesh,
|
||||
|
||||
/**
|
||||
* Set the position of the given mesh which is part of the physics simulation. Calling this
|
||||
* method will reset the current simulated velocity of the mesh.
|
||||
*
|
||||
* @method
|
||||
* @name AmmoPhysics#setMeshPosition
|
||||
* @param {Mesh} mesh The mesh to update the position for.
|
||||
* @param {Vector3} position - The new position.
|
||||
* @param {number} [index=0] - If the mesh is instanced, the index represents the instanced ID.
|
||||
*/
|
||||
setMeshPosition: setMeshPosition
|
||||
// addCompoundMesh
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
function compose( position, quaternion, array, index ) {
|
||||
|
||||
const x = quaternion.x(), y = quaternion.y(), z = quaternion.z(), w = quaternion.w();
|
||||
const x2 = x + x, y2 = y + y, z2 = z + z;
|
||||
const xx = x * x2, xy = x * y2, xz = x * z2;
|
||||
const yy = y * y2, yz = y * z2, zz = z * z2;
|
||||
const wx = w * x2, wy = w * y2, wz = w * z2;
|
||||
|
||||
array[ index + 0 ] = ( 1 - ( yy + zz ) );
|
||||
array[ index + 1 ] = ( xy + wz );
|
||||
array[ index + 2 ] = ( xz - wy );
|
||||
array[ index + 3 ] = 0;
|
||||
|
||||
array[ index + 4 ] = ( xy - wz );
|
||||
array[ index + 5 ] = ( 1 - ( xx + zz ) );
|
||||
array[ index + 6 ] = ( yz + wx );
|
||||
array[ index + 7 ] = 0;
|
||||
|
||||
array[ index + 8 ] = ( xz + wy );
|
||||
array[ index + 9 ] = ( yz - wx );
|
||||
array[ index + 10 ] = ( 1 - ( xx + yy ) );
|
||||
array[ index + 11 ] = 0;
|
||||
|
||||
array[ index + 12 ] = position.x();
|
||||
array[ index + 13 ] = position.y();
|
||||
array[ index + 14 ] = position.z();
|
||||
array[ index + 15 ] = 1;
|
||||
|
||||
}
|
||||
|
||||
export { AmmoPhysics };
|
||||
332
node_modules/three/examples/jsm/physics/JoltPhysics.js
generated
vendored
Normal file
332
node_modules/three/examples/jsm/physics/JoltPhysics.js
generated
vendored
Normal file
@@ -0,0 +1,332 @@
|
||||
import { Clock, Vector3, Quaternion, Matrix4 } from 'three';
|
||||
|
||||
const JOLT_PATH = 'https://cdn.jsdelivr.net/npm/jolt-physics@0.23.0/dist/jolt-physics.wasm-compat.js';
|
||||
|
||||
const frameRate = 60;
|
||||
|
||||
let Jolt = null;
|
||||
|
||||
function getShape( geometry ) {
|
||||
|
||||
const parameters = geometry.parameters;
|
||||
|
||||
// TODO change type to is*
|
||||
|
||||
if ( geometry.type === 'BoxGeometry' ) {
|
||||
|
||||
const sx = parameters.width !== undefined ? parameters.width / 2 : 0.5;
|
||||
const sy = parameters.height !== undefined ? parameters.height / 2 : 0.5;
|
||||
const sz = parameters.depth !== undefined ? parameters.depth / 2 : 0.5;
|
||||
|
||||
return new Jolt.BoxShape( new Jolt.Vec3( sx, sy, sz ), 0.05 * Math.min( sx, sy, sz ), null );
|
||||
|
||||
} else if ( geometry.type === 'SphereGeometry' || geometry.type === 'IcosahedronGeometry' ) {
|
||||
|
||||
const radius = parameters.radius !== undefined ? parameters.radius : 1;
|
||||
|
||||
return new Jolt.SphereShape( radius, null );
|
||||
|
||||
}
|
||||
|
||||
console.error( 'JoltPhysics: Unsupported geometry type:', geometry.type );
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
// Object layers
|
||||
const LAYER_NON_MOVING = 0;
|
||||
const LAYER_MOVING = 1;
|
||||
const NUM_OBJECT_LAYERS = 2;
|
||||
|
||||
function setupCollisionFiltering( settings ) {
|
||||
|
||||
const objectFilter = new Jolt.ObjectLayerPairFilterTable( NUM_OBJECT_LAYERS );
|
||||
objectFilter.EnableCollision( LAYER_NON_MOVING, LAYER_MOVING );
|
||||
objectFilter.EnableCollision( LAYER_MOVING, LAYER_MOVING );
|
||||
|
||||
const BP_LAYER_NON_MOVING = new Jolt.BroadPhaseLayer( 0 );
|
||||
const BP_LAYER_MOVING = new Jolt.BroadPhaseLayer( 1 );
|
||||
const NUM_BROAD_PHASE_LAYERS = 2;
|
||||
|
||||
const bpInterface = new Jolt.BroadPhaseLayerInterfaceTable( NUM_OBJECT_LAYERS, NUM_BROAD_PHASE_LAYERS );
|
||||
bpInterface.MapObjectToBroadPhaseLayer( LAYER_NON_MOVING, BP_LAYER_NON_MOVING );
|
||||
bpInterface.MapObjectToBroadPhaseLayer( LAYER_MOVING, BP_LAYER_MOVING );
|
||||
|
||||
settings.mObjectLayerPairFilter = objectFilter;
|
||||
settings.mBroadPhaseLayerInterface = bpInterface;
|
||||
settings.mObjectVsBroadPhaseLayerFilter = new Jolt.ObjectVsBroadPhaseLayerFilterTable( settings.mBroadPhaseLayerInterface, NUM_BROAD_PHASE_LAYERS, settings.mObjectLayerPairFilter, NUM_OBJECT_LAYERS );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @classdesc Can be used to include Jolt as a Physics engine into
|
||||
* `three.js` apps. The API can be initialized via:
|
||||
* ```js
|
||||
* const physics = await JoltPhysics();
|
||||
* ```
|
||||
* The component automatically imports Jolt from a CDN so make sure
|
||||
* to use the component with an active Internet connection.
|
||||
*
|
||||
* @name JoltPhysics
|
||||
* @class
|
||||
* @hideconstructor
|
||||
* @three_import import { JoltPhysics } from 'three/addons/physics/JoltPhysics.js';
|
||||
*/
|
||||
async function JoltPhysics() {
|
||||
|
||||
if ( Jolt === null ) {
|
||||
|
||||
const { default: initJolt } = await import( `${JOLT_PATH}` );
|
||||
Jolt = await initJolt();
|
||||
|
||||
}
|
||||
|
||||
const settings = new Jolt.JoltSettings();
|
||||
setupCollisionFiltering( settings );
|
||||
|
||||
const jolt = new Jolt.JoltInterface( settings );
|
||||
Jolt.destroy( settings );
|
||||
|
||||
const physicsSystem = jolt.GetPhysicsSystem();
|
||||
const bodyInterface = physicsSystem.GetBodyInterface();
|
||||
|
||||
const meshes = [];
|
||||
const meshMap = new WeakMap();
|
||||
|
||||
const _position = new Vector3();
|
||||
const _quaternion = new Quaternion();
|
||||
const _scale = new Vector3( 1, 1, 1 );
|
||||
|
||||
const _matrix = new Matrix4();
|
||||
|
||||
function addScene( scene ) {
|
||||
|
||||
scene.traverse( function ( child ) {
|
||||
|
||||
if ( child.isMesh ) {
|
||||
|
||||
const physics = child.userData.physics;
|
||||
|
||||
if ( physics ) {
|
||||
|
||||
addMesh( child, physics.mass, physics.restitution );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
}
|
||||
|
||||
function addMesh( mesh, mass = 0, restitution = 0 ) {
|
||||
|
||||
const shape = getShape( mesh.geometry );
|
||||
|
||||
if ( shape === null ) return;
|
||||
|
||||
const body = mesh.isInstancedMesh
|
||||
? createInstancedBody( mesh, mass, restitution, shape )
|
||||
: createBody( mesh.position, mesh.quaternion, mass, restitution, shape );
|
||||
|
||||
if ( mass > 0 ) {
|
||||
|
||||
meshes.push( mesh );
|
||||
meshMap.set( mesh, body );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function createInstancedBody( mesh, mass, restitution, shape ) {
|
||||
|
||||
const array = mesh.instanceMatrix.array;
|
||||
|
||||
const bodies = [];
|
||||
|
||||
for ( let i = 0; i < mesh.count; i ++ ) {
|
||||
|
||||
const position = _position.fromArray( array, i * 16 + 12 );
|
||||
const quaternion = _quaternion.setFromRotationMatrix( _matrix.fromArray( array, i * 16 ) ); // TODO Copilot did this
|
||||
bodies.push( createBody( position, quaternion, mass, restitution, shape ) );
|
||||
|
||||
}
|
||||
|
||||
return bodies;
|
||||
|
||||
}
|
||||
|
||||
function createBody( position, rotation, mass, restitution, shape ) {
|
||||
|
||||
const pos = new Jolt.Vec3( position.x, position.y, position.z );
|
||||
const rot = new Jolt.Quat( rotation.x, rotation.y, rotation.z, rotation.w );
|
||||
|
||||
const motion = mass > 0 ? Jolt.EMotionType_Dynamic : Jolt.EMotionType_Static;
|
||||
const layer = mass > 0 ? LAYER_MOVING : LAYER_NON_MOVING;
|
||||
|
||||
const creationSettings = new Jolt.BodyCreationSettings( shape, pos, rot, motion, layer );
|
||||
creationSettings.mRestitution = restitution;
|
||||
|
||||
const body = bodyInterface.CreateBody( creationSettings );
|
||||
|
||||
bodyInterface.AddBody( body.GetID(), Jolt.EActivation_Activate );
|
||||
|
||||
Jolt.destroy( creationSettings );
|
||||
|
||||
return body;
|
||||
|
||||
}
|
||||
|
||||
function setMeshPosition( mesh, position, index = 0 ) {
|
||||
|
||||
if ( mesh.isInstancedMesh ) {
|
||||
|
||||
const bodies = meshMap.get( mesh );
|
||||
|
||||
const body = bodies[ index ];
|
||||
|
||||
bodyInterface.RemoveBody( body.GetID() );
|
||||
bodyInterface.DestroyBody( body.GetID() );
|
||||
|
||||
const physics = mesh.userData.physics;
|
||||
|
||||
const shape = body.GetShape();
|
||||
const body2 = createBody( position, { x: 0, y: 0, z: 0, w: 1 }, physics.mass, physics.restitution, shape );
|
||||
|
||||
bodies[ index ] = body2;
|
||||
|
||||
} else {
|
||||
|
||||
// TODO: Implement this
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function setMeshVelocity( mesh, velocity, index = 0 ) {
|
||||
|
||||
/*
|
||||
let body = meshMap.get( mesh );
|
||||
|
||||
if ( mesh.isInstancedMesh ) {
|
||||
|
||||
body = body[ index ];
|
||||
|
||||
}
|
||||
|
||||
body.setLinvel( velocity );
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
const clock = new Clock();
|
||||
|
||||
function step() {
|
||||
|
||||
let deltaTime = clock.getDelta();
|
||||
|
||||
// Don't go below 30 Hz to prevent spiral of death
|
||||
deltaTime = Math.min( deltaTime, 1.0 / 30.0 );
|
||||
|
||||
// When running below 55 Hz, do 2 steps instead of 1
|
||||
const numSteps = deltaTime > 1.0 / 55.0 ? 2 : 1;
|
||||
|
||||
// Step the physics world
|
||||
jolt.Step( deltaTime, numSteps );
|
||||
|
||||
//
|
||||
|
||||
for ( let i = 0, l = meshes.length; i < l; i ++ ) {
|
||||
|
||||
const mesh = meshes[ i ];
|
||||
|
||||
if ( mesh.isInstancedMesh ) {
|
||||
|
||||
const array = mesh.instanceMatrix.array;
|
||||
const bodies = meshMap.get( mesh );
|
||||
|
||||
for ( let j = 0; j < bodies.length; j ++ ) {
|
||||
|
||||
const body = bodies[ j ];
|
||||
|
||||
const position = body.GetPosition();
|
||||
const quaternion = body.GetRotation();
|
||||
|
||||
_position.set( position.GetX(), position.GetY(), position.GetZ() );
|
||||
_quaternion.set( quaternion.GetX(), quaternion.GetY(), quaternion.GetZ(), quaternion.GetW() );
|
||||
|
||||
_matrix.compose( _position, _quaternion, _scale ).toArray( array, j * 16 );
|
||||
|
||||
}
|
||||
|
||||
mesh.instanceMatrix.needsUpdate = true;
|
||||
mesh.computeBoundingSphere();
|
||||
|
||||
} else {
|
||||
|
||||
const body = meshMap.get( mesh );
|
||||
|
||||
const position = body.GetPosition();
|
||||
const rotation = body.GetRotation();
|
||||
|
||||
mesh.position.set( position.GetX(), position.GetY(), position.GetZ() );
|
||||
mesh.quaternion.set( rotation.GetX(), rotation.GetY(), rotation.GetZ(), rotation.GetW() );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// animate
|
||||
|
||||
setInterval( step, 1000 / frameRate );
|
||||
|
||||
return {
|
||||
/**
|
||||
* Adds the given scene to this physics simulation. Only meshes with a
|
||||
* `physics` object in their {@link Object3D#userData} field will be honored.
|
||||
* The object can be used to store the mass and restitution of the mesh. E.g.:
|
||||
* ```js
|
||||
* box.userData.physics = { mass: 1, restitution: 0 };
|
||||
* ```
|
||||
*
|
||||
* @method
|
||||
* @name JoltPhysics#addScene
|
||||
* @param {Object3D} scene The scene or any type of 3D object to add.
|
||||
*/
|
||||
addScene: addScene,
|
||||
|
||||
/**
|
||||
* Adds the given mesh to this physics simulation.
|
||||
*
|
||||
* @method
|
||||
* @name JoltPhysics#addMesh
|
||||
* @param {Mesh} mesh The mesh to add.
|
||||
* @param {number} [mass=0] The mass in kg of the mesh.
|
||||
* @param {number} [restitution=0] The restitution of the mesh, usually from 0 to 1. Represents how "bouncy" objects are when they collide with each other.
|
||||
*/
|
||||
addMesh: addMesh,
|
||||
|
||||
/**
|
||||
* Set the position of the given mesh which is part of the physics simulation. Calling this
|
||||
* method will reset the current simulated velocity of the mesh.
|
||||
*
|
||||
* @method
|
||||
* @name JoltPhysics#setMeshPosition
|
||||
* @param {Mesh} mesh The mesh to update the position for.
|
||||
* @param {Vector3} position - The new position.
|
||||
* @param {number} [index=0] - If the mesh is instanced, the index represents the instanced ID.
|
||||
*/
|
||||
setMeshPosition: setMeshPosition,
|
||||
|
||||
// NOOP
|
||||
setMeshVelocity: setMeshVelocity
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
export { JoltPhysics };
|
||||
434
node_modules/three/examples/jsm/physics/RapierPhysics.js
generated
vendored
Normal file
434
node_modules/three/examples/jsm/physics/RapierPhysics.js
generated
vendored
Normal file
@@ -0,0 +1,434 @@
|
||||
import { Clock, Vector3, Quaternion, Matrix4 } from 'three';
|
||||
|
||||
const RAPIER_PATH = 'https://cdn.skypack.dev/@dimforge/rapier3d-compat@0.17.3';
|
||||
|
||||
const frameRate = 60;
|
||||
|
||||
const _scale = new Vector3( 1, 1, 1 );
|
||||
const ZERO = new Vector3();
|
||||
|
||||
let RAPIER = null;
|
||||
|
||||
function getShape( geometry ) {
|
||||
|
||||
const parameters = geometry.parameters;
|
||||
|
||||
// TODO change type to is*
|
||||
|
||||
if ( geometry.type === 'RoundedBoxGeometry' ) {
|
||||
|
||||
const sx = parameters.width !== undefined ? parameters.width / 2 : 0.5;
|
||||
const sy = parameters.height !== undefined ? parameters.height / 2 : 0.5;
|
||||
const sz = parameters.depth !== undefined ? parameters.depth / 2 : 0.5;
|
||||
const radius = parameters.radius !== undefined ? parameters.radius : 0.1;
|
||||
|
||||
return RAPIER.ColliderDesc.roundCuboid( sx - radius, sy - radius, sz - radius, radius );
|
||||
|
||||
} else if ( geometry.type === 'BoxGeometry' ) {
|
||||
|
||||
const sx = parameters.width !== undefined ? parameters.width / 2 : 0.5;
|
||||
const sy = parameters.height !== undefined ? parameters.height / 2 : 0.5;
|
||||
const sz = parameters.depth !== undefined ? parameters.depth / 2 : 0.5;
|
||||
|
||||
return RAPIER.ColliderDesc.cuboid( sx, sy, sz );
|
||||
|
||||
} else if ( geometry.type === 'SphereGeometry' || geometry.type === 'IcosahedronGeometry' ) {
|
||||
|
||||
const radius = parameters.radius !== undefined ? parameters.radius : 1;
|
||||
return RAPIER.ColliderDesc.ball( radius );
|
||||
|
||||
} else if ( geometry.type === 'CylinderGeometry' ) {
|
||||
|
||||
const radius = parameters.radiusBottom !== undefined ? parameters.radiusBottom : 0.5;
|
||||
const length = parameters.height !== undefined ? parameters.height : 0.5;
|
||||
|
||||
return RAPIER.ColliderDesc.cylinder( length / 2, radius );
|
||||
|
||||
} else if ( geometry.type === 'CapsuleGeometry' ) {
|
||||
|
||||
const radius = parameters.radius !== undefined ? parameters.radius : 0.5;
|
||||
const length = parameters.height !== undefined ? parameters.height : 0.5;
|
||||
|
||||
return RAPIER.ColliderDesc.capsule( length / 2, radius );
|
||||
|
||||
} else if ( geometry.type === 'BufferGeometry' ) {
|
||||
|
||||
const vertices = [];
|
||||
const vertex = new Vector3();
|
||||
const position = geometry.getAttribute( 'position' );
|
||||
|
||||
for ( let i = 0; i < position.count; i ++ ) {
|
||||
|
||||
vertex.fromBufferAttribute( position, i );
|
||||
vertices.push( vertex.x, vertex.y, vertex.z );
|
||||
|
||||
}
|
||||
|
||||
// if the buffer is non-indexed, generate an index buffer
|
||||
const indices = geometry.getIndex() === null
|
||||
? Uint32Array.from( Array( parseInt( vertices.length / 3 ) ).keys() )
|
||||
: geometry.getIndex().array;
|
||||
|
||||
return RAPIER.ColliderDesc.trimesh( vertices, indices );
|
||||
|
||||
}
|
||||
|
||||
console.error( 'RapierPhysics: Unsupported geometry type:', geometry.type );
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @classdesc Can be used to include Rapier as a Physics engine into
|
||||
* `three.js` apps. The API can be initialized via:
|
||||
* ```js
|
||||
* const physics = await RapierPhysics();
|
||||
* ```
|
||||
* The component automatically imports Rapier from a CDN so make sure
|
||||
* to use the component with an active Internet connection.
|
||||
*
|
||||
* @name RapierPhysics
|
||||
* @class
|
||||
* @hideconstructor
|
||||
* @three_import import { RapierPhysics } from 'three/addons/physics/RapierPhysics.js';
|
||||
*/
|
||||
async function RapierPhysics() {
|
||||
|
||||
if ( RAPIER === null ) {
|
||||
|
||||
RAPIER = await import( `${RAPIER_PATH}` );
|
||||
await RAPIER.init();
|
||||
|
||||
}
|
||||
|
||||
// Docs: https://rapier.rs/docs/api/javascript/JavaScript3D/
|
||||
|
||||
const gravity = new Vector3( 0.0, - 9.81, 0.0 );
|
||||
const world = new RAPIER.World( gravity );
|
||||
|
||||
const meshes = [];
|
||||
const meshMap = new WeakMap();
|
||||
|
||||
const _vector = new Vector3();
|
||||
const _quaternion = new Quaternion();
|
||||
const _matrix = new Matrix4();
|
||||
|
||||
function addScene( scene ) {
|
||||
|
||||
scene.traverse( function ( child ) {
|
||||
|
||||
if ( child.isMesh ) {
|
||||
|
||||
const physics = child.userData.physics;
|
||||
|
||||
if ( physics ) {
|
||||
|
||||
addMesh( child, physics.mass, physics.restitution );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
}
|
||||
|
||||
function addMesh( mesh, mass = 0, restitution = 0 ) {
|
||||
|
||||
const shape = getShape( mesh.geometry );
|
||||
|
||||
if ( shape === null ) return;
|
||||
|
||||
shape.setMass( mass );
|
||||
shape.setRestitution( restitution );
|
||||
|
||||
const { body, collider } = mesh.isInstancedMesh
|
||||
? createInstancedBody( mesh, mass, shape )
|
||||
: createBody( mesh.position, mesh.quaternion, mass, shape );
|
||||
|
||||
if ( ! mesh.userData.physics ) mesh.userData.physics = {};
|
||||
|
||||
mesh.userData.physics.body = body;
|
||||
mesh.userData.physics.collider = collider;
|
||||
|
||||
if ( mass > 0 ) {
|
||||
|
||||
meshes.push( mesh );
|
||||
meshMap.set( mesh, { body, collider } );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function removeMesh( mesh ) {
|
||||
|
||||
const index = meshes.indexOf( mesh );
|
||||
|
||||
if ( index !== - 1 ) {
|
||||
|
||||
meshes.splice( index, 1 );
|
||||
meshMap.delete( mesh );
|
||||
|
||||
if ( ! mesh.userData.physics ) return;
|
||||
|
||||
const body = mesh.userData.physics.body;
|
||||
const collider = mesh.userData.physics.collider;
|
||||
|
||||
if ( body ) removeBody( body );
|
||||
if ( collider ) removeCollider( collider );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function createInstancedBody( mesh, mass, shape ) {
|
||||
|
||||
const array = mesh.instanceMatrix.array;
|
||||
|
||||
const bodies = [];
|
||||
const colliders = [];
|
||||
|
||||
for ( let i = 0; i < mesh.count; i ++ ) {
|
||||
|
||||
const position = _vector.fromArray( array, i * 16 + 12 );
|
||||
const { body, collider } = createBody( position, null, mass, shape );
|
||||
bodies.push( body );
|
||||
colliders.push( collider );
|
||||
|
||||
}
|
||||
|
||||
return { body: bodies, collider: colliders };
|
||||
|
||||
}
|
||||
|
||||
function createBody( position, quaternion, mass, shape ) {
|
||||
|
||||
const desc = mass > 0 ? RAPIER.RigidBodyDesc.dynamic() : RAPIER.RigidBodyDesc.fixed();
|
||||
desc.setTranslation( ...position );
|
||||
if ( quaternion !== null ) desc.setRotation( quaternion );
|
||||
|
||||
const body = world.createRigidBody( desc );
|
||||
const collider = world.createCollider( shape, body );
|
||||
|
||||
return { body, collider };
|
||||
|
||||
}
|
||||
|
||||
function removeBody( body ) {
|
||||
|
||||
if ( Array.isArray( body ) ) {
|
||||
|
||||
for ( let i = 0; i < body.length; i ++ ) {
|
||||
|
||||
world.removeRigidBody( body[ i ] );
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
world.removeRigidBody( body );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function removeCollider( collider ) {
|
||||
|
||||
if ( Array.isArray( collider ) ) {
|
||||
|
||||
for ( let i = 0; i < collider.length; i ++ ) {
|
||||
|
||||
world.removeCollider( collider[ i ] );
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
world.removeCollider( collider );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function setMeshPosition( mesh, position, index = 0 ) {
|
||||
|
||||
let { body } = meshMap.get( mesh );
|
||||
|
||||
if ( mesh.isInstancedMesh ) {
|
||||
|
||||
body = body[ index ];
|
||||
|
||||
}
|
||||
|
||||
body.setAngvel( ZERO );
|
||||
body.setLinvel( ZERO );
|
||||
body.setTranslation( position );
|
||||
|
||||
}
|
||||
|
||||
function setMeshVelocity( mesh, velocity, index = 0 ) {
|
||||
|
||||
let { body } = meshMap.get( mesh );
|
||||
|
||||
if ( mesh.isInstancedMesh ) {
|
||||
|
||||
body = body[ index ];
|
||||
|
||||
}
|
||||
|
||||
body.setLinvel( velocity );
|
||||
|
||||
}
|
||||
|
||||
function addHeightfield( mesh, width, depth, heights, scale ) {
|
||||
|
||||
const shape = RAPIER.ColliderDesc.heightfield( width, depth, heights, scale );
|
||||
|
||||
const bodyDesc = RAPIER.RigidBodyDesc.fixed();
|
||||
bodyDesc.setTranslation( mesh.position.x, mesh.position.y, mesh.position.z );
|
||||
bodyDesc.setRotation( mesh.quaternion );
|
||||
|
||||
const body = world.createRigidBody( bodyDesc );
|
||||
world.createCollider( shape, body );
|
||||
|
||||
if ( ! mesh.userData.physics ) mesh.userData.physics = {};
|
||||
mesh.userData.physics.body = body;
|
||||
|
||||
return body;
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
const clock = new Clock();
|
||||
|
||||
function step() {
|
||||
|
||||
world.timestep = clock.getDelta();
|
||||
world.step();
|
||||
|
||||
//
|
||||
|
||||
for ( let i = 0, l = meshes.length; i < l; i ++ ) {
|
||||
|
||||
const mesh = meshes[ i ];
|
||||
|
||||
if ( mesh.isInstancedMesh ) {
|
||||
|
||||
const array = mesh.instanceMatrix.array;
|
||||
const { body: bodies } = meshMap.get( mesh );
|
||||
|
||||
for ( let j = 0; j < bodies.length; j ++ ) {
|
||||
|
||||
const body = bodies[ j ];
|
||||
|
||||
const position = body.translation();
|
||||
_quaternion.copy( body.rotation() );
|
||||
|
||||
_matrix.compose( position, _quaternion, _scale ).toArray( array, j * 16 );
|
||||
|
||||
}
|
||||
|
||||
mesh.instanceMatrix.needsUpdate = true;
|
||||
mesh.computeBoundingSphere();
|
||||
|
||||
} else {
|
||||
|
||||
const { body } = meshMap.get( mesh );
|
||||
|
||||
mesh.position.copy( body.translation() );
|
||||
mesh.quaternion.copy( body.rotation() );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// animate
|
||||
|
||||
setInterval( step, 1000 / frameRate );
|
||||
|
||||
return {
|
||||
RAPIER,
|
||||
world,
|
||||
/**
|
||||
* Adds the given scene to this physics simulation. Only meshes with a
|
||||
* `physics` object in their {@link Object3D#userData} field will be honored.
|
||||
* The object can be used to store the mass and restitution of the mesh. E.g.:
|
||||
* ```js
|
||||
* box.userData.physics = { mass: 1, restitution: 0 };
|
||||
* ```
|
||||
*
|
||||
* @method
|
||||
* @name RapierPhysics#addScene
|
||||
* @param {Object3D} scene The scene or any type of 3D object to add.
|
||||
*/
|
||||
addScene: addScene,
|
||||
|
||||
/**
|
||||
* Adds the given mesh to this physics simulation.
|
||||
*
|
||||
* @method
|
||||
* @name RapierPhysics#addMesh
|
||||
* @param {Mesh} mesh The mesh to add.
|
||||
* @param {number} [mass=0] The mass in kg of the mesh.
|
||||
* @param {number} [restitution=0] The restitution of the mesh, usually from 0 to 1. Represents how "bouncy" objects are when they collide with each other.
|
||||
*/
|
||||
addMesh: addMesh,
|
||||
|
||||
/**
|
||||
* Removes the given mesh from this physics simulation.
|
||||
*
|
||||
* @method
|
||||
* @name RapierPhysics#removeMesh
|
||||
* @param {Mesh} mesh The mesh to remove.
|
||||
*/
|
||||
removeMesh: removeMesh,
|
||||
|
||||
/**
|
||||
* Set the position of the given mesh which is part of the physics simulation. Calling this
|
||||
* method will reset the current simulated velocity of the mesh.
|
||||
*
|
||||
* @method
|
||||
* @name RapierPhysics#setMeshPosition
|
||||
* @param {Mesh} mesh The mesh to update the position for.
|
||||
* @param {Vector3} position - The new position.
|
||||
* @param {number} [index=0] - If the mesh is instanced, the index represents the instanced ID.
|
||||
*/
|
||||
setMeshPosition: setMeshPosition,
|
||||
|
||||
/**
|
||||
* Set the velocity of the given mesh which is part of the physics simulation.
|
||||
*
|
||||
* @method
|
||||
* @name RapierPhysics#setMeshVelocity
|
||||
* @param {Mesh} mesh The mesh to update the velocity for.
|
||||
* @param {Vector3} velocity - The new velocity.
|
||||
* @param {number} [index=0] - If the mesh is instanced, the index represents the instanced ID.
|
||||
*/
|
||||
setMeshVelocity: setMeshVelocity,
|
||||
|
||||
/**
|
||||
* Adds a heightfield terrain to the physics simulation.
|
||||
*
|
||||
* @method
|
||||
* @name RapierPhysics#addHeightfield
|
||||
* @param {Mesh} mesh - The Three.js mesh representing the terrain.
|
||||
* @param {number} width - The number of vertices along the width (x-axis) of the heightfield.
|
||||
* @param {number} depth - The number of vertices along the depth (z-axis) of the heightfield.
|
||||
* @param {Float32Array} heights - Array of height values for each vertex in the heightfield.
|
||||
* @param {Object} scale - Scale factors for the heightfield dimensions.
|
||||
* @param {number} scale.x - Scale factor for width.
|
||||
* @param {number} scale.y - Scale factor for height.
|
||||
* @param {number} scale.z - Scale factor for depth.
|
||||
* @returns {RigidBody} The created Rapier rigid body for the heightfield.
|
||||
*/
|
||||
addHeightfield: addHeightfield
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
export { RapierPhysics };
|
||||
Reference in New Issue
Block a user