RapierPhysics.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import { Clock, Vector3, Quaternion, Matrix4 } from 'three';
  2. const RAPIER_PATH = 'https://cdn.skypack.dev/@dimforge/[email protected]';
  3. const frameRate = 60;
  4. const _scale = new Vector3( 1, 1, 1 );
  5. const ZERO = new Vector3();
  6. let RAPIER = null;
  7. function getCollider( geometry ) {
  8. const parameters = geometry.parameters;
  9. // TODO change type to is*
  10. if ( geometry.type === 'BoxGeometry' ) {
  11. const sx = parameters.width !== undefined ? parameters.width / 2 : 0.5;
  12. const sy = parameters.height !== undefined ? parameters.height / 2 : 0.5;
  13. const sz = parameters.depth !== undefined ? parameters.depth / 2 : 0.5;
  14. return RAPIER.ColliderDesc.cuboid( sx, sy, sz );
  15. } else if ( geometry.type === 'SphereGeometry' || geometry.type === 'IcosahedronGeometry' ) {
  16. const radius = parameters.radius !== undefined ? parameters.radius : 1;
  17. return RAPIER.ColliderDesc.ball( radius );
  18. }
  19. return null;
  20. }
  21. async function RapierPhysics() {
  22. if ( RAPIER === null ) {
  23. RAPIER = await import( RAPIER_PATH );
  24. await RAPIER.init();
  25. }
  26. // Docs: https://rapier.rs/docs/api/javascript/JavaScript3D/
  27. const gravity = new Vector3( 0.0, - 9.81, 0.0 );
  28. const world = new RAPIER.World( gravity );
  29. const meshes = [];
  30. const meshMap = new WeakMap();
  31. const _vector = new Vector3();
  32. const _quaternion = new Quaternion();
  33. const _matrix = new Matrix4();
  34. function addMesh( mesh, mass = 0, restitution = 0 ) {
  35. const shape = getCollider( mesh.geometry );
  36. if ( shape === null ) return;
  37. shape.setMass( mass );
  38. shape.setRestitution( restitution );
  39. const body = mesh.isInstancedMesh
  40. ? createInstancedBody( mesh, mass, shape )
  41. : createBody( mesh.position, mesh.quaternion, mass, shape );
  42. if ( mass > 0 ) {
  43. meshes.push( mesh );
  44. meshMap.set( mesh, body );
  45. }
  46. }
  47. function createInstancedBody( mesh, mass, shape ) {
  48. const array = mesh.instanceMatrix.array;
  49. const bodies = [];
  50. for ( let i = 0; i < mesh.count; i ++ ) {
  51. const position = _vector.fromArray( array, i * 16 + 12 );
  52. bodies.push( createBody( position, null, mass, shape ) );
  53. }
  54. return bodies;
  55. }
  56. function createBody( position, quaternion, mass, shape ) {
  57. const desc = mass > 0 ? RAPIER.RigidBodyDesc.dynamic() : RAPIER.RigidBodyDesc.fixed();
  58. desc.setTranslation( ...position );
  59. if ( quaternion !== null ) desc.setRotation( quaternion );
  60. const body = world.createRigidBody( desc );
  61. world.createCollider( shape, body );
  62. return body;
  63. }
  64. function setMeshPosition( mesh, position, index = 0 ) {
  65. let body = meshMap.get( mesh );
  66. if ( mesh.isInstancedMesh ) {
  67. body = body[ index ];
  68. }
  69. body.setAngvel( ZERO );
  70. body.setLinvel( ZERO );
  71. body.setTranslation( position );
  72. }
  73. function setMeshVelocity( mesh, velocity, index = 0 ) {
  74. let body = meshMap.get( mesh );
  75. if ( mesh.isInstancedMesh ) {
  76. body = body[ index ];
  77. }
  78. body.setLinvel( velocity );
  79. }
  80. //
  81. const clock = new Clock();
  82. function step() {
  83. world.timestep = clock.getDelta();
  84. world.step();
  85. //
  86. for ( let i = 0, l = meshes.length; i < l; i ++ ) {
  87. const mesh = meshes[ i ];
  88. if ( mesh.isInstancedMesh ) {
  89. const array = mesh.instanceMatrix.array;
  90. const bodies = meshMap.get( mesh );
  91. for ( let j = 0; j < bodies.length; j ++ ) {
  92. const body = bodies[ j ];
  93. const position = body.translation();
  94. _quaternion.copy( body.rotation() );
  95. _matrix.compose( position, _quaternion, _scale ).toArray( array, j * 16 );
  96. }
  97. mesh.instanceMatrix.needsUpdate = true;
  98. mesh.computeBoundingSphere();
  99. } else {
  100. const body = meshMap.get( mesh );
  101. mesh.position.copy( body.translation() );
  102. mesh.quaternion.copy( body.rotation() );
  103. }
  104. }
  105. }
  106. // animate
  107. setInterval( step, 1000 / frameRate );
  108. return {
  109. addMesh: addMesh,
  110. setMeshPosition: setMeshPosition,
  111. setMeshVelocity: setMeshVelocity
  112. };
  113. }
  114. export { RapierPhysics };