AmmoPhysics.js 6.6 KB

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