AmmoPhysics.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. ( function () {
  2. async function AmmoPhysics() {
  3. if ( 'Ammo' in window === false ) {
  4. console.error( 'AmmoPhysics: Couldn\'t find Ammo.js' );
  5. return;
  6. }
  7. const AmmoLib = await Ammo(); // eslint-disable-line no-undef
  8. const frameRate = 60;
  9. const collisionConfiguration = new AmmoLib.btDefaultCollisionConfiguration();
  10. const dispatcher = new AmmoLib.btCollisionDispatcher( collisionConfiguration );
  11. const broadphase = new AmmoLib.btDbvtBroadphase();
  12. const solver = new AmmoLib.btSequentialImpulseConstraintSolver();
  13. const world = new AmmoLib.btDiscreteDynamicsWorld( dispatcher, broadphase, solver, collisionConfiguration );
  14. world.setGravity( new AmmoLib.btVector3( 0, - 9.8, 0 ) );
  15. const worldTransform = new AmmoLib.btTransform(); //
  16. function getShape( geometry ) {
  17. const parameters = geometry.parameters; // TODO change type to is*
  18. if ( geometry.type === 'BoxGeometry' ) {
  19. const sx = parameters.width !== undefined ? parameters.width / 2 : 0.5;
  20. const sy = parameters.height !== undefined ? parameters.height / 2 : 0.5;
  21. const sz = parameters.depth !== undefined ? parameters.depth / 2 : 0.5;
  22. const shape = new AmmoLib.btBoxShape( new AmmoLib.btVector3( sx, sy, sz ) );
  23. shape.setMargin( 0.05 );
  24. return shape;
  25. } else if ( geometry.type === 'SphereGeometry' || geometry.type === 'IcosahedronGeometry' ) {
  26. const radius = parameters.radius !== undefined ? parameters.radius : 1;
  27. const shape = new AmmoLib.btSphereShape( radius );
  28. shape.setMargin( 0.05 );
  29. return shape;
  30. }
  31. return null;
  32. }
  33. const meshes = [];
  34. const meshMap = new WeakMap();
  35. function addMesh( mesh, mass = 0 ) {
  36. const shape = getShape( mesh.geometry );
  37. if ( shape !== null ) {
  38. if ( mesh.isInstancedMesh ) {
  39. handleInstancedMesh( mesh, mass, shape );
  40. } else if ( mesh.isMesh ) {
  41. handleMesh( mesh, mass, shape );
  42. }
  43. }
  44. }
  45. function handleMesh( mesh, mass, shape ) {
  46. const position = mesh.position;
  47. const quaternion = mesh.quaternion;
  48. const transform = new AmmoLib.btTransform();
  49. transform.setIdentity();
  50. transform.setOrigin( new AmmoLib.btVector3( position.x, position.y, position.z ) );
  51. transform.setRotation( new AmmoLib.btQuaternion( quaternion.x, quaternion.y, quaternion.z, quaternion.w ) );
  52. const motionState = new AmmoLib.btDefaultMotionState( transform );
  53. const localInertia = new AmmoLib.btVector3( 0, 0, 0 );
  54. shape.calculateLocalInertia( mass, localInertia );
  55. const rbInfo = new AmmoLib.btRigidBodyConstructionInfo( mass, motionState, shape, localInertia );
  56. const body = new AmmoLib.btRigidBody( rbInfo ); // body.setFriction( 4 );
  57. world.addRigidBody( body );
  58. if ( mass > 0 ) {
  59. meshes.push( mesh );
  60. meshMap.set( mesh, body );
  61. }
  62. }
  63. function handleInstancedMesh( mesh, mass, shape ) {
  64. const array = mesh.instanceMatrix.array;
  65. const bodies = [];
  66. for ( let i = 0; i < mesh.count; i ++ ) {
  67. const index = i * 16;
  68. const transform = new AmmoLib.btTransform();
  69. transform.setFromOpenGLMatrix( array.slice( index, index + 16 ) );
  70. const motionState = new AmmoLib.btDefaultMotionState( transform );
  71. const localInertia = new AmmoLib.btVector3( 0, 0, 0 );
  72. shape.calculateLocalInertia( mass, localInertia );
  73. const rbInfo = new AmmoLib.btRigidBodyConstructionInfo( mass, motionState, shape, localInertia );
  74. const body = new AmmoLib.btRigidBody( rbInfo );
  75. world.addRigidBody( body );
  76. bodies.push( body );
  77. }
  78. if ( mass > 0 ) {
  79. meshes.push( mesh );
  80. meshMap.set( mesh, bodies );
  81. }
  82. } //
  83. function setMeshPosition( mesh, position, index = 0 ) {
  84. if ( mesh.isInstancedMesh ) {
  85. const bodies = meshMap.get( mesh );
  86. const body = bodies[ index ];
  87. body.setAngularVelocity( new AmmoLib.btVector3( 0, 0, 0 ) );
  88. body.setLinearVelocity( new AmmoLib.btVector3( 0, 0, 0 ) );
  89. worldTransform.setIdentity();
  90. worldTransform.setOrigin( new AmmoLib.btVector3( position.x, position.y, position.z ) );
  91. body.setWorldTransform( worldTransform );
  92. } else if ( mesh.isMesh ) {
  93. const body = meshMap.get( mesh );
  94. body.setAngularVelocity( new AmmoLib.btVector3( 0, 0, 0 ) );
  95. body.setLinearVelocity( new AmmoLib.btVector3( 0, 0, 0 ) );
  96. worldTransform.setIdentity();
  97. worldTransform.setOrigin( new AmmoLib.btVector3( position.x, position.y, position.z ) );
  98. body.setWorldTransform( worldTransform );
  99. }
  100. } //
  101. let lastTime = 0;
  102. function step() {
  103. const time = performance.now();
  104. if ( lastTime > 0 ) {
  105. const delta = ( time - lastTime ) / 1000; // console.time( 'world.step' );
  106. world.stepSimulation( delta, 10 ); // console.timeEnd( 'world.step' );
  107. }
  108. lastTime = time; //
  109. for ( let i = 0, l = meshes.length; i < l; i ++ ) {
  110. const mesh = meshes[ i ];
  111. if ( mesh.isInstancedMesh ) {
  112. const array = mesh.instanceMatrix.array;
  113. const bodies = meshMap.get( mesh );
  114. for ( let j = 0; j < bodies.length; j ++ ) {
  115. const body = bodies[ j ];
  116. const motionState = body.getMotionState();
  117. motionState.getWorldTransform( worldTransform );
  118. const position = worldTransform.getOrigin();
  119. const quaternion = worldTransform.getRotation();
  120. compose( position, quaternion, array, j * 16 );
  121. }
  122. mesh.instanceMatrix.needsUpdate = true;
  123. } else if ( mesh.isMesh ) {
  124. const body = meshMap.get( mesh );
  125. const motionState = body.getMotionState();
  126. motionState.getWorldTransform( worldTransform );
  127. const position = worldTransform.getOrigin();
  128. const quaternion = worldTransform.getRotation();
  129. mesh.position.set( position.x(), position.y(), position.z() );
  130. mesh.quaternion.set( quaternion.x(), quaternion.y(), quaternion.z(), quaternion.w() );
  131. }
  132. }
  133. } // animate
  134. setInterval( step, 1000 / frameRate );
  135. return {
  136. addMesh: addMesh,
  137. setMeshPosition: setMeshPosition // addCompoundMesh
  138. };
  139. }
  140. function compose( position, quaternion, array, index ) {
  141. const x = quaternion.x(),
  142. y = quaternion.y(),
  143. z = quaternion.z(),
  144. w = quaternion.w();
  145. const x2 = x + x,
  146. y2 = y + y,
  147. z2 = z + z;
  148. const xx = x * x2,
  149. xy = x * y2,
  150. xz = x * z2;
  151. const yy = y * y2,
  152. yz = y * z2,
  153. zz = z * z2;
  154. const wx = w * x2,
  155. wy = w * y2,
  156. wz = w * z2;
  157. array[ index + 0 ] = 1 - ( yy + zz );
  158. array[ index + 1 ] = xy + wz;
  159. array[ index + 2 ] = xz - wy;
  160. array[ index + 3 ] = 0;
  161. array[ index + 4 ] = xy - wz;
  162. array[ index + 5 ] = 1 - ( xx + zz );
  163. array[ index + 6 ] = yz + wx;
  164. array[ index + 7 ] = 0;
  165. array[ index + 8 ] = xz + wy;
  166. array[ index + 9 ] = yz - wx;
  167. array[ index + 10 ] = 1 - ( xx + yy );
  168. array[ index + 11 ] = 0;
  169. array[ index + 12 ] = position.x();
  170. array[ index + 13 ] = position.y();
  171. array[ index + 14 ] = position.z();
  172. array[ index + 15 ] = 1;
  173. }
  174. THREE.AmmoPhysics = AmmoPhysics;
  175. } )();