RapierPhysics.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. import { Clock, Vector3, Quaternion, Matrix4 } from 'three';
  2. const RAPIER_PATH = 'https://cdn.skypack.dev/@dimforge/rapier3d-compat@0.12.0';
  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. } else if ( geometry.type === 'BufferGeometry' ) {
  19. const vertices = [];
  20. const vertex = new Vector3();
  21. const position = geometry.getAttribute( 'position' );
  22. for ( let i = 0; i < position.count; i ++ ) {
  23. vertex.fromBufferAttribute( position, i );
  24. vertices.push( vertex.x, vertex.y, vertex.z );
  25. }
  26. // if the buffer is non-indexed, generate an index buffer
  27. const indices = geometry.getIndex() === null
  28. ? Uint32Array.from( Array( parseInt( vertices.length / 3 ) ).keys() )
  29. : geometry.getIndex().array;
  30. return RAPIER.ColliderDesc.trimesh( vertices, indices );
  31. }
  32. return null;
  33. }
  34. async function RapierPhysics() {
  35. if ( RAPIER === null ) {
  36. RAPIER = await import( `${RAPIER_PATH}` );
  37. await RAPIER.init();
  38. }
  39. // Docs: https://rapier.rs/docs/api/javascript/JavaScript3D/
  40. const gravity = new Vector3( 0.0, - 9.81, 0.0 );
  41. const world = new RAPIER.World( gravity );
  42. const meshes = [];
  43. const meshMap = new WeakMap();
  44. const _vector = new Vector3();
  45. const _quaternion = new Quaternion();
  46. const _matrix = new Matrix4();
  47. function addScene( scene ) {
  48. scene.traverse( function ( child ) {
  49. if ( child.isMesh ) {
  50. const physics = child.userData.physics;
  51. if ( physics ) {
  52. addMesh( child, physics.mass, physics.restitution );
  53. }
  54. }
  55. } );
  56. }
  57. function addMesh( mesh, mass = 0, restitution = 0 ) {
  58. const shape = getShape( mesh.geometry );
  59. if ( shape === null ) return;
  60. shape.setMass( mass );
  61. shape.setRestitution( restitution );
  62. const body = mesh.isInstancedMesh
  63. ? createInstancedBody( mesh, mass, shape )
  64. : createBody( mesh.position, mesh.quaternion, mass, shape );
  65. if ( mass > 0 ) {
  66. meshes.push( mesh );
  67. meshMap.set( mesh, body );
  68. }
  69. }
  70. function createInstancedBody( mesh, mass, shape ) {
  71. const array = mesh.instanceMatrix.array;
  72. const bodies = [];
  73. for ( let i = 0; i < mesh.count; i ++ ) {
  74. const position = _vector.fromArray( array, i * 16 + 12 );
  75. bodies.push( createBody( position, null, mass, shape ) );
  76. }
  77. return bodies;
  78. }
  79. function createBody( position, quaternion, mass, shape ) {
  80. const desc = mass > 0 ? RAPIER.RigidBodyDesc.dynamic() : RAPIER.RigidBodyDesc.fixed();
  81. desc.setTranslation( ...position );
  82. if ( quaternion !== null ) desc.setRotation( quaternion );
  83. const body = world.createRigidBody( desc );
  84. world.createCollider( shape, body );
  85. return body;
  86. }
  87. function setMeshPosition( mesh, position, index = 0 ) {
  88. let body = meshMap.get( mesh );
  89. if ( mesh.isInstancedMesh ) {
  90. body = body[ index ];
  91. }
  92. body.setAngvel( ZERO );
  93. body.setLinvel( ZERO );
  94. body.setTranslation( position );
  95. }
  96. function setMeshVelocity( mesh, velocity, index = 0 ) {
  97. let body = meshMap.get( mesh );
  98. if ( mesh.isInstancedMesh ) {
  99. body = body[ index ];
  100. }
  101. body.setLinvel( velocity );
  102. }
  103. //
  104. const clock = new Clock();
  105. function step() {
  106. world.timestep = clock.getDelta();
  107. world.step();
  108. //
  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 position = body.translation();
  117. _quaternion.copy( body.rotation() );
  118. _matrix.compose( position, _quaternion, _scale ).toArray( array, j * 16 );
  119. }
  120. mesh.instanceMatrix.needsUpdate = true;
  121. mesh.computeBoundingSphere();
  122. } else {
  123. const body = meshMap.get( mesh );
  124. mesh.position.copy( body.translation() );
  125. mesh.quaternion.copy( body.rotation() );
  126. }
  127. }
  128. }
  129. // animate
  130. setInterval( step, 1000 / frameRate );
  131. return {
  132. addScene: addScene,
  133. addMesh: addMesh,
  134. setMeshPosition: setMeshPosition,
  135. setMeshVelocity: setMeshVelocity
  136. };
  137. }
  138. export { RapierPhysics };
粤ICP备19079148号