OimoPhysics.js 4.8 KB

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