RapierPhysics.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 getShape( 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 addScene( scene ) {
  35. scene.traverse( function ( child ) {
  36. if ( child.isMesh ) {
  37. const physics = child.userData.physics;
  38. if ( physics ) {
  39. addMesh( child, physics.mass, physics.restitution );
  40. }
  41. }
  42. } );
  43. }
  44. function addMesh( mesh, mass = 0, restitution = 0 ) {
  45. const shape = getShape( mesh.geometry );
  46. if ( shape === null ) return;
  47. shape.setMass( mass );
  48. shape.setRestitution( restitution );
  49. const body = mesh.isInstancedMesh
  50. ? createInstancedBody( mesh, mass, shape )
  51. : createBody( mesh.position, mesh.quaternion, mass, shape );
  52. if ( mass > 0 ) {
  53. meshes.push( mesh );
  54. meshMap.set( mesh, body );
  55. }
  56. }
  57. function createInstancedBody( mesh, mass, shape ) {
  58. const array = mesh.instanceMatrix.array;
  59. const bodies = [];
  60. for ( let i = 0; i < mesh.count; i ++ ) {
  61. const position = _vector.fromArray( array, i * 16 + 12 );
  62. bodies.push( createBody( position, null, mass, shape ) );
  63. }
  64. return bodies;
  65. }
  66. function createBody( position, quaternion, mass, shape ) {
  67. const desc = mass > 0 ? RAPIER.RigidBodyDesc.dynamic() : RAPIER.RigidBodyDesc.fixed();
  68. desc.setTranslation( ...position );
  69. if ( quaternion !== null ) desc.setRotation( quaternion );
  70. const body = world.createRigidBody( desc );
  71. world.createCollider( shape, body );
  72. return body;
  73. }
  74. function setMeshPosition( mesh, position, index = 0 ) {
  75. let body = meshMap.get( mesh );
  76. if ( mesh.isInstancedMesh ) {
  77. body = body[ index ];
  78. }
  79. body.setAngvel( ZERO );
  80. body.setLinvel( ZERO );
  81. body.setTranslation( position );
  82. }
  83. function setMeshVelocity( mesh, velocity, index = 0 ) {
  84. let body = meshMap.get( mesh );
  85. if ( mesh.isInstancedMesh ) {
  86. body = body[ index ];
  87. }
  88. body.setLinvel( velocity );
  89. }
  90. //
  91. const clock = new Clock();
  92. function step() {
  93. world.timestep = clock.getDelta();
  94. world.step();
  95. //
  96. for ( let i = 0, l = meshes.length; i < l; i ++ ) {
  97. const mesh = meshes[ i ];
  98. if ( mesh.isInstancedMesh ) {
  99. const array = mesh.instanceMatrix.array;
  100. const bodies = meshMap.get( mesh );
  101. for ( let j = 0; j < bodies.length; j ++ ) {
  102. const body = bodies[ j ];
  103. const position = body.translation();
  104. _quaternion.copy( body.rotation() );
  105. _matrix.compose( position, _quaternion, _scale ).toArray( array, j * 16 );
  106. }
  107. mesh.instanceMatrix.needsUpdate = true;
  108. mesh.computeBoundingSphere();
  109. } else {
  110. const body = meshMap.get( mesh );
  111. mesh.position.copy( body.translation() );
  112. mesh.quaternion.copy( body.rotation() );
  113. }
  114. }
  115. }
  116. // animate
  117. setInterval( step, 1000 / frameRate );
  118. return {
  119. addScene: addScene,
  120. addMesh: addMesh,
  121. setMeshPosition: setMeshPosition,
  122. setMeshVelocity: setMeshVelocity
  123. };
  124. }
  125. export { RapierPhysics };