RapierPhysics.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. async function RapierPhysics() {
  2. const RAPIER = await import( 'https://cdn.skypack.dev/@dimforge/[email protected]' );
  3. await RAPIER.init();
  4. const frameRate = 60;
  5. // Docs: https://rapier.rs/docs/api/javascript/JavaScript3D/
  6. const gravity = { x: 0.0, y: - 9.81, z: 0.0 };
  7. const world = new RAPIER.World( gravity );
  8. function getCollider( geometry ) {
  9. const parameters = geometry.parameters;
  10. // TODO change type to is*
  11. if ( geometry.type === 'BoxGeometry' ) {
  12. const sx = parameters.width !== undefined ? parameters.width / 2 : 0.5;
  13. const sy = parameters.height !== undefined ? parameters.height / 2 : 0.5;
  14. const sz = parameters.depth !== undefined ? parameters.depth / 2 : 0.5;
  15. return RAPIER.ColliderDesc.cuboid( sx, sy, sz );
  16. } else if ( geometry.type === 'SphereGeometry' || geometry.type === 'IcosahedronGeometry' ) {
  17. const radius = parameters.radius !== undefined ? parameters.radius : 1;
  18. return RAPIER.ColliderDesc.ball( radius );
  19. }
  20. return null;
  21. }
  22. const meshes = [];
  23. const meshMap = new WeakMap();
  24. function addMesh( mesh, mass = 0, restitution = 0 ) {
  25. const shape = getCollider( mesh.geometry );
  26. if ( shape !== null ) {
  27. shape.setMass( mass );
  28. shape.setRestitution( restitution );
  29. if ( mesh.isInstancedMesh ) {
  30. handleInstancedMesh( mesh, mass, shape );
  31. } else if ( mesh.isMesh ) {
  32. handleMesh( mesh, mass, shape );
  33. }
  34. }
  35. }
  36. function handleMesh( mesh, mass, shape ) {
  37. const position = mesh.position;
  38. const quaternion = mesh.quaternion;
  39. const desc = mass > 0 ? RAPIER.RigidBodyDesc.dynamic() : RAPIER.RigidBodyDesc.fixed();
  40. desc.setTranslation( position.x, position.y, position.z );
  41. desc.setRotation( quaternion );
  42. const body = world.createRigidBody( desc );
  43. world.createCollider( shape, body );
  44. if ( mass > 0 ) {
  45. meshes.push( mesh );
  46. meshMap.set( mesh, body );
  47. }
  48. }
  49. function handleInstancedMesh( mesh, mass, shape ) {
  50. const array = mesh.instanceMatrix.array;
  51. const bodies = [];
  52. for ( let i = 0; i < mesh.count; i ++ ) {
  53. const index = i * 16;
  54. const desc = mass > 0 ? RAPIER.RigidBodyDesc.dynamic() : RAPIER.RigidBodyDesc.fixed();
  55. desc.setTranslation( array[ index + 12 ], array[ index + 13 ], array[ index + 14 ] );
  56. const body = world.createRigidBody( desc );
  57. world.createCollider( shape, body );
  58. bodies.push( body );
  59. }
  60. if ( mass > 0 ) {
  61. meshes.push( mesh );
  62. meshMap.set( mesh, bodies );
  63. }
  64. }
  65. const vector = { x: 0, y: 0, z: 0 };
  66. function setMeshPosition( mesh, position, index = 0 ) {
  67. if ( mesh.isInstancedMesh ) {
  68. const bodies = meshMap.get( mesh );
  69. const body = bodies[ index ];
  70. body.setAngvel( vector );
  71. body.setLinvel( vector );
  72. body.setTranslation( position );
  73. } else if ( mesh.isMesh ) {
  74. const body = meshMap.get( mesh );
  75. body.setAngvel( vector );
  76. body.setLinvel( vector );
  77. body.setTranslation( position );
  78. }
  79. }
  80. //
  81. let lastTime = 0;
  82. function step() {
  83. const time = performance.now();
  84. if ( lastTime > 0 ) {
  85. const delta = ( time - lastTime ) / 1000;
  86. world.timestep = delta;
  87. world.step();
  88. //
  89. for ( let i = 0, l = meshes.length; i < l; i ++ ) {
  90. const mesh = meshes[ i ];
  91. if ( mesh.isInstancedMesh ) {
  92. const array = mesh.instanceMatrix.array;
  93. const bodies = meshMap.get( mesh );
  94. for ( let j = 0; j < bodies.length; j ++ ) {
  95. const body = bodies[ j ];
  96. const position = body.translation();
  97. const quaternion = body.rotation();
  98. compose( position, quaternion, array, j * 16 );
  99. }
  100. mesh.instanceMatrix.needsUpdate = true;
  101. mesh.computeBoundingSphere();
  102. } else if ( mesh.isMesh ) {
  103. const body = meshMap.get( mesh );
  104. mesh.position.copy( body.translation() );
  105. mesh.quaternion.copy( body.rotation() );
  106. }
  107. }
  108. }
  109. lastTime = time;
  110. }
  111. // animate
  112. setInterval( step, 1000 / frameRate );
  113. return {
  114. addMesh: addMesh,
  115. setMeshPosition: setMeshPosition
  116. };
  117. }
  118. function compose( position, quaternion, array, index ) {
  119. const x = quaternion.x, y = quaternion.y, z = quaternion.z, w = quaternion.w;
  120. const x2 = x + x, y2 = y + y, z2 = z + z;
  121. const xx = x * x2, xy = x * y2, xz = x * z2;
  122. const yy = y * y2, yz = y * z2, zz = z * z2;
  123. const wx = w * x2, wy = w * y2, wz = w * z2;
  124. array[ index + 0 ] = ( 1 - ( yy + zz ) );
  125. array[ index + 1 ] = ( xy + wz );
  126. array[ index + 2 ] = ( xz - wy );
  127. array[ index + 3 ] = 0;
  128. array[ index + 4 ] = ( xy - wz );
  129. array[ index + 5 ] = ( 1 - ( xx + zz ) );
  130. array[ index + 6 ] = ( yz + wx );
  131. array[ index + 7 ] = 0;
  132. array[ index + 8 ] = ( xz + wy );
  133. array[ index + 9 ] = ( yz - wx );
  134. array[ index + 10 ] = ( 1 - ( xx + yy ) );
  135. array[ index + 11 ] = 0;
  136. array[ index + 12 ] = position.x;
  137. array[ index + 13 ] = position.y;
  138. array[ index + 14 ] = position.z;
  139. array[ index + 15 ] = 1;
  140. }
  141. export { RapierPhysics };