CannonPhysics.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import CANNON from "../libs/cannon.module.min.js";
  2. /**
  3. * @author mrdoob / http://mrdoob.com/
  4. */
  5. function compose( position, quaternion, array, index ) {
  6. var x = quaternion.x, y = quaternion.y, z = quaternion.z, w = quaternion.w;
  7. var x2 = x + x, y2 = y + y, z2 = z + z;
  8. var xx = x * x2, xy = x * y2, xz = x * z2;
  9. var yy = y * y2, yz = y * z2, zz = z * z2;
  10. var wx = w * x2, wy = w * y2, wz = w * z2;
  11. array[ index + 0 ] = ( 1 - ( yy + zz ) );
  12. array[ index + 1 ] = ( xy + wz );
  13. array[ index + 2 ] = ( xz - wy );
  14. array[ index + 3 ] = 0;
  15. array[ index + 4 ] = ( xy - wz );
  16. array[ index + 5 ] = ( 1 - ( xx + zz ) );
  17. array[ index + 6 ] = ( yz + wx );
  18. array[ index + 7 ] = 0;
  19. array[ index + 8 ] = ( xz + wy );
  20. array[ index + 9 ] = ( yz - wx );
  21. array[ index + 10 ] = ( 1 - ( xx + yy ) );
  22. array[ index + 11 ] = 0;
  23. array[ index + 12 ] = position.x;
  24. array[ index + 13 ] = position.y;
  25. array[ index + 14 ] = position.z;
  26. array[ index + 15 ] = 1;
  27. }
  28. function CannonPhysics() {
  29. var frameRate = 60;
  30. var frameTime = 1 / frameRate;
  31. var world = new CANNON.World();
  32. world.gravity.set( 0, - 9.8, 0 );
  33. world.broadphase = new CANNON.SAPBroadphase( world );
  34. // world.solver.iterations = 20;
  35. // world.solver.tolerance = 0.001;
  36. // world.allowSleep = true;
  37. //
  38. function getShape( geometry ) {
  39. var parameters = geometry.parameters;
  40. // TODO change type to is*
  41. switch ( geometry.type ) {
  42. case 'BoxBufferGeometry':
  43. var halfExtents = new CANNON.Vec3();
  44. halfExtents.x = parameters.width !== undefined ? parameters.width / 2 : 0.5;
  45. halfExtents.y = parameters.height !== undefined ? parameters.height / 2 : 0.5;
  46. halfExtents.z = parameters.depth !== undefined ? parameters.depth / 2 : 0.5;
  47. return new CANNON.Box( halfExtents );
  48. case 'PlaneBufferGeometry':
  49. return new CANNON.Plane();
  50. case 'SphereBufferGeometry':
  51. var radius = parameters.radius;
  52. return new CANNON.Sphere( radius );
  53. }
  54. return null;
  55. }
  56. var meshes = [];
  57. var meshMap = new WeakMap();
  58. function addMesh( mesh, mass = 0 ) {
  59. var shape = getShape( mesh.geometry );
  60. if ( shape !== null ) {
  61. if ( mesh.isInstancedMesh ) {
  62. handleInstancedMesh( mesh, mass, shape );
  63. } else if ( mesh.isMesh ) {
  64. handleMesh( mesh, mass, shape );
  65. }
  66. }
  67. }
  68. function handleMesh( mesh, mass, shape ) {
  69. var position = new CANNON.Vec3();
  70. position.copy( mesh.position );
  71. var quaternion = new CANNON.Quaternion();
  72. quaternion.copy( mesh.quaternion );
  73. var body = new CANNON.Body( {
  74. position: position,
  75. quaternion: quaternion,
  76. mass: mass,
  77. shape: shape
  78. } );
  79. world.addBody( body );
  80. if ( mass > 0 ) {
  81. meshes.push( mesh );
  82. meshMap.set( mesh, body );
  83. }
  84. }
  85. function handleInstancedMesh( mesh, mass, shape ) {
  86. var array = mesh.instanceMatrix.array;
  87. var bodies = [];
  88. for ( var i = 0; i < mesh.count; i ++ ) {
  89. var index = i * 16;
  90. var position = new CANNON.Vec3();
  91. position.set( array[ index + 12 ], array[ index + 13 ], array[ index + 14 ] );
  92. var body = new CANNON.Body( {
  93. position: position,
  94. mass: mass,
  95. shape: shape
  96. } );
  97. world.addBody( body );
  98. bodies.push( body );
  99. }
  100. if ( mass > 0 ) {
  101. mesh.instanceMatrix.setUsage( 35048 ); // THREE.DynamicDrawUsage = 35048
  102. meshes.push( mesh );
  103. meshMap.set( mesh, bodies );
  104. }
  105. }
  106. //
  107. function setMeshPosition( mesh, position, index = 0 ) {
  108. if ( mesh.isInstancedMesh ) {
  109. var bodies = meshMap.get( mesh );
  110. bodies[ index ].position.copy( position );
  111. } else if ( mesh.isMesh ) {
  112. var body = meshMap.get( mesh );
  113. body.position.copy( position );
  114. }
  115. }
  116. //
  117. var lastTime = 0;
  118. function step() {
  119. var time = performance.now();
  120. if ( lastTime > 0 ) {
  121. var delta = ( time - lastTime ) / 1000;
  122. // console.time( 'world.step' );
  123. world.step( frameTime, delta, frameRate );
  124. // console.timeEnd( 'world.step' );
  125. }
  126. lastTime = time;
  127. //
  128. for ( var i = 0, l = meshes.length; i < l; i ++ ) {
  129. var mesh = meshes[ i ];
  130. if ( mesh.isInstancedMesh ) {
  131. var array = mesh.instanceMatrix.array;
  132. var bodies = meshMap.get( mesh );
  133. for ( var j = 0; j < bodies.length; j ++ ) {
  134. var body = bodies[ j ];
  135. compose( body.position, body.quaternion, array, j * 16 );
  136. }
  137. mesh.instanceMatrix.needsUpdate = true;
  138. } else if ( mesh.isMesh ) {
  139. var body = meshMap.get( mesh );
  140. mesh.position.copy( body.position );
  141. mesh.quaternion.copy( body.quaternion );
  142. }
  143. }
  144. }
  145. // animate
  146. setInterval( step, 1000 / frameRate );
  147. return {
  148. addMesh: addMesh,
  149. setMeshPosition: setMeshPosition
  150. // addCompoundMesh
  151. };
  152. }
  153. export { CannonPhysics };