OimoPhysics.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import { oimo } from '../libs/OimoPhysics.js';
  2. async function OimoPhysics() {
  3. const Vec3 = oimo.common.Vec3;
  4. const World = oimo.dynamics.World;
  5. const RigidBodyType = oimo.dynamics.rigidbody.RigidBodyType;
  6. const RigidBodyConfig = oimo.dynamics.rigidbody.RigidBodyConfig;
  7. const ShapeConfig = oimo.dynamics.rigidbody.ShapeConfig;
  8. const RigidBody = oimo.dynamics.rigidbody.RigidBody;
  9. const Shape = oimo.dynamics.rigidbody.Shape;
  10. const OBoxGeometry = oimo.collision.geometry.BoxGeometry;
  11. const OSphereGeometry = oimo.collision.geometry.SphereGeometry;
  12. const frameRate = 60;
  13. const world = new World( 2, new Vec3( 0, - 9.8, 0 ) );
  14. //
  15. function getShape( geometry ) {
  16. const parameters = geometry.parameters;
  17. // TODO change type to is*
  18. if ( geometry.type === 'BoxGeometry' ) {
  19. const sx = parameters.width !== undefined ? parameters.width / 2 : 0.5;
  20. const sy = parameters.height !== undefined ? parameters.height / 2 : 0.5;
  21. const sz = parameters.depth !== undefined ? parameters.depth / 2 : 0.5;
  22. return new OBoxGeometry( new Vec3( sx, sy, sz ) );
  23. } else if ( geometry.type === 'SphereGeometry' || geometry.type === 'IcosahedronGeometry' ) {
  24. const radius = parameters.radius !== undefined ? parameters.radius : 1;
  25. return new OSphereGeometry( radius );
  26. }
  27. return null;
  28. }
  29. const meshes = [];
  30. const meshMap = new WeakMap();
  31. function addMesh( mesh, mass = 0 ) {
  32. const shape = getShape( mesh.geometry );
  33. if ( shape !== null ) {
  34. if ( mesh.isInstancedMesh ) {
  35. handleInstancedMesh( mesh, mass, shape );
  36. } else if ( mesh.isMesh ) {
  37. handleMesh( mesh, mass, shape );
  38. }
  39. }
  40. }
  41. function handleMesh( mesh, mass, shape ) {
  42. const shapeConfig = new ShapeConfig();
  43. shapeConfig.geometry = shape;
  44. const bodyConfig = new RigidBodyConfig();
  45. bodyConfig.type = mass === 0 ? RigidBodyType.STATIC : RigidBodyType.DYNAMIC;
  46. bodyConfig.position = new Vec3( mesh.position.x, mesh.position.y, mesh.position.z );
  47. const body = new RigidBody( bodyConfig );
  48. body.addShape( new Shape( shapeConfig ) );
  49. world.addRigidBody( body );
  50. if ( mass > 0 ) {
  51. meshes.push( mesh );
  52. meshMap.set( mesh, body );
  53. }
  54. }
  55. function handleInstancedMesh( mesh, mass, shape ) {
  56. const array = mesh.instanceMatrix.array;
  57. const bodies = [];
  58. for ( let i = 0; i < mesh.count; i ++ ) {
  59. const index = i * 16;
  60. const shapeConfig = new ShapeConfig();
  61. shapeConfig.geometry = shape;
  62. const bodyConfig = new RigidBodyConfig();
  63. bodyConfig.type = mass === 0 ? RigidBodyType.STATIC : RigidBodyType.DYNAMIC;
  64. bodyConfig.position = new Vec3( array[ index + 12 ], array[ index + 13 ], array[ index + 14 ] );
  65. const body = new RigidBody( bodyConfig );
  66. body.addShape( new Shape( shapeConfig ) );
  67. world.addRigidBody( body );
  68. bodies.push( body );
  69. }
  70. if ( mass > 0 ) {
  71. meshes.push( mesh );
  72. meshMap.set( mesh, bodies );
  73. }
  74. }
  75. //
  76. function setMeshPosition( mesh, position, index = 0 ) {
  77. if ( mesh.isInstancedMesh ) {
  78. const bodies = meshMap.get( mesh );
  79. const body = bodies[ index ];
  80. body.setPosition( new Vec3( position.x, position.y, position.z ) );
  81. } else if ( mesh.isMesh ) {
  82. const body = meshMap.get( mesh );
  83. body.setPosition( new Vec3( position.x, position.y, position.z ) );
  84. }
  85. }
  86. //
  87. let lastTime = 0;
  88. function step() {
  89. const time = performance.now();
  90. if ( lastTime > 0 ) {
  91. const delta = ( time - lastTime ) / 1000;
  92. // console.time( 'world.step' );
  93. world.step( delta );
  94. // console.timeEnd( 'world.step' );
  95. }
  96. lastTime = time;
  97. //
  98. for ( let i = 0, l = meshes.length; i < l; i ++ ) {
  99. const mesh = meshes[ i ];
  100. if ( mesh.isInstancedMesh ) {
  101. const array = mesh.instanceMatrix.array;
  102. const bodies = meshMap.get( mesh );
  103. for ( let j = 0; j < bodies.length; j ++ ) {
  104. const body = bodies[ j ];
  105. compose( body.getPosition(), body.getOrientation(), array, j * 16 );
  106. }
  107. mesh.instanceMatrix.needsUpdate = true;
  108. } else if ( mesh.isMesh ) {
  109. const body = meshMap.get( mesh );
  110. mesh.position.copy( body.getPosition() );
  111. mesh.quaternion.copy( body.getOrientation() );
  112. }
  113. }
  114. }
  115. // animate
  116. setInterval( step, 1000 / frameRate );
  117. return {
  118. addMesh: addMesh,
  119. setMeshPosition: setMeshPosition
  120. // addCompoundMesh
  121. };
  122. }
  123. function compose( position, quaternion, array, index ) {
  124. const x = quaternion.x, y = quaternion.y, z = quaternion.z, w = quaternion.w;
  125. const x2 = x + x, y2 = y + y, z2 = z + z;
  126. const xx = x * x2, xy = x * y2, xz = x * z2;
  127. const yy = y * y2, yz = y * z2, zz = z * z2;
  128. const wx = w * x2, wy = w * y2, wz = w * z2;
  129. array[ index + 0 ] = ( 1 - ( yy + zz ) );
  130. array[ index + 1 ] = ( xy + wz );
  131. array[ index + 2 ] = ( xz - wy );
  132. array[ index + 3 ] = 0;
  133. array[ index + 4 ] = ( xy - wz );
  134. array[ index + 5 ] = ( 1 - ( xx + zz ) );
  135. array[ index + 6 ] = ( yz + wx );
  136. array[ index + 7 ] = 0;
  137. array[ index + 8 ] = ( xz + wy );
  138. array[ index + 9 ] = ( yz - wx );
  139. array[ index + 10 ] = ( 1 - ( xx + yy ) );
  140. array[ index + 11 ] = 0;
  141. array[ index + 12 ] = position.x;
  142. array[ index + 13 ] = position.y;
  143. array[ index + 14 ] = position.z;
  144. array[ index + 15 ] = 1;
  145. }
  146. export { OimoPhysics };