CannonPhysics.js 4.5 KB

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