123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- import CANNON from "../libs/cannon.module.min.js";
- // import * as CANNON from "https://unpkg.com/cannon-es/dist/cannon-es.js";
- 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;
- }
- function CannonPhysics() {
- const frameRate = 60;
- const frameTime = 1 / frameRate;
- const world = new CANNON.World();
- world.gravity.set( 0, - 9.8, 0 );
- world.broadphase = new CANNON.SAPBroadphase( world );
- // world.solver.iterations = 20;
- // world.solver.tolerance = 0.001;
- // world.allowSleep = true;
- //
- function getShape( geometry ) {
- const parameters = geometry.parameters;
- // TODO change type to is*
- switch ( geometry.type ) {
- case 'BoxBufferGeometry':
- const halfExtents = new CANNON.Vec3();
- halfExtents.x = parameters.width !== undefined ? parameters.width / 2 : 0.5;
- halfExtents.y = parameters.height !== undefined ? parameters.height / 2 : 0.5;
- halfExtents.z = parameters.depth !== undefined ? parameters.depth / 2 : 0.5;
- return new CANNON.Box( halfExtents );
- case 'PlaneBufferGeometry':
- return new CANNON.Plane();
- case 'SphereBufferGeometry':
- const radius = parameters.radius;
- return new CANNON.Sphere( radius );
- }
- return null;
- }
- const meshes = [];
- const meshMap = new WeakMap();
- function addMesh( mesh, mass = 0 ) {
- const shape = getShape( mesh.geometry );
- if ( shape !== null ) {
- if ( mesh.isInstancedMesh ) {
- handleInstancedMesh( mesh, mass, shape );
- } else if ( mesh.isMesh ) {
- handleMesh( mesh, mass, shape );
- }
- }
- }
- function handleMesh( mesh, mass, shape ) {
- const position = new CANNON.Vec3();
- position.copy( mesh.position );
- const quaternion = new CANNON.Quaternion();
- quaternion.copy( mesh.quaternion );
- const body = new CANNON.Body( {
- position: position,
- quaternion: quaternion,
- mass: mass,
- shape: shape
- } );
- world.addBody( body );
- if ( mass > 0 ) {
- meshes.push( mesh );
- meshMap.set( mesh, body );
- }
- }
- function handleInstancedMesh( mesh, mass, shape ) {
- const array = mesh.instanceMatrix.array;
- const bodies = [];
- for ( let i = 0; i < mesh.count; i ++ ) {
- const index = i * 16;
- const position = new CANNON.Vec3();
- position.set( array[ index + 12 ], array[ index + 13 ], array[ index + 14 ] );
- const body = new CANNON.Body( {
- position: position,
- mass: mass,
- shape: shape
- } );
- world.addBody( body );
- bodies.push( body );
- }
- if ( mass > 0 ) {
- mesh.instanceMatrix.setUsage( 35048 ); // THREE.DynamicDrawUsage = 35048
- meshes.push( mesh );
- meshMap.set( mesh, bodies );
- }
- }
- //
- function setMeshPosition( mesh, position, index = 0 ) {
- if ( mesh.isInstancedMesh ) {
- const bodies = meshMap.get( mesh );
- bodies[ index ].position.copy( position );
- } else if ( mesh.isMesh ) {
- const body = meshMap.get( mesh );
- body.position.copy( position );
- }
- }
- //
- let lastTime = 0;
- function step() {
- const time = performance.now();
- if ( lastTime > 0 ) {
- const delta = ( time - lastTime ) / 1000;
- // console.time( 'world.step' );
- world.step( frameTime, delta );
- // console.timeEnd( 'world.step' );
- }
- lastTime = time;
- //
- 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 ];
- compose( body.position, body.quaternion, array, j * 16 );
- }
- mesh.instanceMatrix.needsUpdate = true;
- } else if ( mesh.isMesh ) {
- const body = meshMap.get( mesh );
- mesh.position.copy( body.position );
- mesh.quaternion.copy( body.quaternion );
- }
- }
- }
- // animate
- setInterval( step, 1000 / frameRate );
- return {
- addMesh: addMesh,
- setMeshPosition: setMeshPosition
- // addCompoundMesh
- };
- }
- export { CannonPhysics };
|