OimoPhysics.js 4.7 KB

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