MMDPhysics.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  1. /**
  2. * @author takahiro / https://github.com/takahirox
  3. *
  4. * Dependencies
  5. * - Ammo.js https://github.com/kripken/ammo.js
  6. *
  7. * MMD specific Physics class.
  8. *
  9. * See THREE.MMDLoader for the passed parameter list of RigidBody/Constraint.
  10. *
  11. * Requirement:
  12. * - don't change object's scale from (1,1,1) after setting physics to object
  13. *
  14. * TODO
  15. * - optimize for the performance
  16. * - use Physijs http://chandlerprall.github.io/Physijs/
  17. * and improve the performance by making use of Web worker.
  18. * - if possible, make this class being non-MMD specific.
  19. * - object scale change support
  20. */
  21. THREE.MMDPhysics = function ( mesh, params ) {
  22. if ( params === undefined ) params = {};
  23. this.mesh = mesh;
  24. this.helper = new THREE.MMDPhysics.ResourceHelper();
  25. this.unitStep = ( params.unitStep !== undefined ) ? params.unitStep : 1 / 60;
  26. this.maxStepNum = ( params.maxStepNum !== undefined ) ? params.maxStepNum : 3;
  27. this.world = null;
  28. this.bodies = [];
  29. this.constraints = [];
  30. this.init( mesh );
  31. };
  32. THREE.MMDPhysics.prototype = {
  33. constructor: THREE.MMDPhysics,
  34. init: function ( mesh ) {
  35. var parent = mesh.parent;
  36. if ( parent !== null ) {
  37. parent.remove( mesh );
  38. }
  39. var currentPosition = mesh.position.clone();
  40. var currentRotation = mesh.rotation.clone();
  41. var currentScale = mesh.scale.clone();
  42. mesh.position.set( 0, 0, 0 );
  43. mesh.rotation.set( 0, 0, 0 );
  44. mesh.scale.set( 1, 1, 1 );
  45. mesh.updateMatrixWorld( true );
  46. this.initWorld();
  47. this.initRigidBodies();
  48. this.initConstraints();
  49. if ( parent !== null ) {
  50. parent.add( mesh );
  51. }
  52. mesh.position.copy( currentPosition );
  53. mesh.rotation.copy( currentRotation );
  54. mesh.scale.copy( currentScale );
  55. mesh.updateMatrixWorld( true );
  56. this.reset();
  57. },
  58. initWorld: function () {
  59. var config = new Ammo.btDefaultCollisionConfiguration();
  60. var dispatcher = new Ammo.btCollisionDispatcher( config );
  61. var cache = new Ammo.btDbvtBroadphase();
  62. var solver = new Ammo.btSequentialImpulseConstraintSolver();
  63. var world = new Ammo.btDiscreteDynamicsWorld( dispatcher, cache, solver, config );
  64. world.setGravity( new Ammo.btVector3( 0, -9.8 * 10, 0 ) );
  65. this.world = world;
  66. },
  67. initRigidBodies: function () {
  68. var bodies = this.mesh.geometry.rigidBodies;
  69. for ( var i = 0; i < bodies.length; i++ ) {
  70. var b = new THREE.MMDPhysics.RigidBody( this.mesh, this.world, bodies[ i ], this.helper );
  71. this.bodies.push( b );
  72. }
  73. },
  74. initConstraints: function () {
  75. var constraints = this.mesh.geometry.constraints;
  76. for ( var i = 0; i < constraints.length; i++ ) {
  77. var params = constraints[ i ];
  78. var bodyA = this.bodies[ params.rigidBodyIndex1 ];
  79. var bodyB = this.bodies[ params.rigidBodyIndex2 ];
  80. var c = new THREE.MMDPhysics.Constraint( this.mesh, this.world, bodyA, bodyB, params, this.helper );
  81. this.constraints.push( c );
  82. }
  83. },
  84. update: function ( delta ) {
  85. var unitStep = this.unitStep;
  86. var stepTime = delta;
  87. var maxStepNum = ( ( delta / unitStep ) | 0 ) + 1;
  88. if ( stepTime < unitStep ) {
  89. stepTime = unitStep;
  90. maxStepNum = 1;
  91. }
  92. if ( maxStepNum > this.maxStepNum ) {
  93. maxStepNum = this.maxStepNum;
  94. }
  95. this.updateRigidBodies();
  96. this.world.stepSimulation( stepTime, maxStepNum, unitStep );
  97. this.updateBones();
  98. },
  99. updateRigidBodies: function () {
  100. for ( var i = 0; i < this.bodies.length; i++ ) {
  101. this.bodies[ i ].updateFromBone();
  102. }
  103. },
  104. updateBones: function () {
  105. for ( var i = 0; i < this.bodies.length; i++ ) {
  106. this.bodies[ i ].updateBone();
  107. }
  108. },
  109. reset: function () {
  110. for ( var i = 0; i < this.bodies.length; i++ ) {
  111. this.bodies[ i ].reset();
  112. }
  113. },
  114. warmup: function ( cycles ) {
  115. for ( var i = 0; i < cycles; i++ ) {
  116. this.update( 1 / 60 );
  117. }
  118. }
  119. };
  120. /**
  121. * This helper class responsibilies are
  122. *
  123. * 1. manage Ammo.js and Three.js object resources and
  124. * improve the performance and the memory consumption by
  125. * reusing objects.
  126. *
  127. * 2. provide simple Ammo object operations.
  128. */
  129. THREE.MMDPhysics.ResourceHelper = function () {
  130. // for Three.js
  131. this.threeVector3s = [];
  132. this.threeMatrix4s = [];
  133. this.threeQuaternions = [];
  134. this.threeEulers = [];
  135. // for Ammo.js
  136. this.transforms = [];
  137. this.quaternions = [];
  138. this.vector3s = [];
  139. };
  140. THREE.MMDPhysics.ResourceHelper.prototype = {
  141. allocThreeVector3: function () {
  142. return ( this.threeVector3s.length > 0 ) ? this.threeVector3s.pop() : new THREE.Vector3();
  143. },
  144. freeThreeVector3: function ( v ) {
  145. this.threeVector3s.push( v );
  146. },
  147. allocThreeMatrix4: function () {
  148. return ( this.threeMatrix4s.length > 0 ) ? this.threeMatrix4s.pop() : new THREE.Matrix4();
  149. },
  150. freeThreeMatrix4: function ( m ) {
  151. this.threeMatrix4s.push( m );
  152. },
  153. allocThreeQuaternion: function () {
  154. return ( this.threeQuaternions.length > 0 ) ? this.threeQuaternions.pop() : new THREE.Quaternion();
  155. },
  156. freeThreeQuaternion: function ( q ) {
  157. this.threeQuaternions.push( q );
  158. },
  159. allocThreeEuler: function () {
  160. return ( this.threeEulers.length > 0 ) ? this.threeEulers.pop() : new THREE.Euler();
  161. },
  162. freeThreeEuler: function ( e ) {
  163. this.threeEulers.push( e );
  164. },
  165. allocTransform: function () {
  166. return ( this.transforms.length > 0 ) ? this.transforms.pop() : new Ammo.btTransform();
  167. },
  168. freeTransform: function ( t ) {
  169. this.transforms.push( t );
  170. },
  171. allocQuaternion: function () {
  172. return ( this.quaternions.length > 0 ) ? this.quaternions.pop() : new Ammo.btQuaternion();
  173. },
  174. freeQuaternion: function ( q ) {
  175. this.quaternions.push( q );
  176. },
  177. allocVector3: function () {
  178. return ( this.vector3s.length > 0 ) ? this.vector3s.pop() : new Ammo.btVector3();
  179. },
  180. freeVector3: function ( v ) {
  181. this.vector3s.push( v );
  182. },
  183. setIdentity: function ( t ) {
  184. t.setIdentity();
  185. },
  186. getBasis: function ( t ) {
  187. var q = this.allocQuaternion();
  188. t.getBasis().getRotation( q );
  189. return q;
  190. },
  191. getBasisAsMatrix3: function ( t ) {
  192. var q = this.getBasis( t );
  193. var m = this.quaternionToMatrix3( q );
  194. this.freeQuaternion( q );
  195. return m;
  196. },
  197. getOrigin: function( t ) {
  198. return t.getOrigin();
  199. },
  200. setOrigin: function( t, v ) {
  201. t.getOrigin().setValue( v.x(), v.y(), v.z() );
  202. },
  203. copyOrigin: function( t1, t2 ) {
  204. var o = t2.getOrigin();
  205. this.setOrigin( t1, o );
  206. },
  207. setBasis: function( t, q ) {
  208. t.setRotation( q );
  209. },
  210. setBasisFromMatrix3: function( t, m ) {
  211. var q = this.matrix3ToQuaternion( m );
  212. this.setBasis( t, q );
  213. this.freeQuaternion( q );
  214. },
  215. setOriginFromArray3: function ( t, a ) {
  216. t.getOrigin().setValue( a[ 0 ], a[ 1 ], a[ 2 ] );
  217. },
  218. setBasisFromArray3: function ( t, a ) {
  219. var thQ = this.allocThreeQuaternion();
  220. var thE = this.allocThreeEuler();
  221. thE.set( a[ 0 ], a[ 1 ], a[ 2 ] );
  222. this.setBasisFromArray4( t, thQ.setFromEuler( thE ).toArray() );
  223. this.freeThreeEuler( thE );
  224. this.freeThreeQuaternion( thQ );
  225. },
  226. setBasisFromArray4: function ( t, a ) {
  227. var q = this.array4ToQuaternion( a );
  228. this.setBasis( t, q );
  229. this.freeQuaternion( q );
  230. },
  231. array4ToQuaternion: function( a ) {
  232. var q = this.allocQuaternion();
  233. q.setX( a[ 0 ] );
  234. q.setY( a[ 1 ] );
  235. q.setZ( a[ 2 ] );
  236. q.setW( a[ 3 ] );
  237. return q;
  238. },
  239. multiplyTransforms: function ( t1, t2 ) {
  240. var t = this.allocTransform();
  241. this.setIdentity( t );
  242. var m1 = this.getBasisAsMatrix3( t1 );
  243. var m2 = this.getBasisAsMatrix3( t2 );
  244. var o1 = this.getOrigin( t1 );
  245. var o2 = this.getOrigin( t2 );
  246. var v1 = this.multiplyMatrix3ByVector3( m1, o2 );
  247. var v2 = this.addVector3( v1, o1 );
  248. this.setOrigin( t, v2 );
  249. var m3 = this.multiplyMatrices3( m1, m2 );
  250. this.setBasisFromMatrix3( t, m3 );
  251. this.freeVector3( v1 );
  252. this.freeVector3( v2 );
  253. return t;
  254. },
  255. inverseTransform: function ( t ) {
  256. var t2 = this.allocTransform();
  257. var m1 = this.getBasisAsMatrix3( t );
  258. var o = this.getOrigin( t );
  259. var m2 = this.transposeMatrix3( m1 );
  260. var v1 = this.negativeVector3( o );
  261. var v2 = this.multiplyMatrix3ByVector3( m2, v1 );
  262. this.setOrigin( t2, v2 );
  263. this.setBasisFromMatrix3( t2, m2 );
  264. this.freeVector3( v1 );
  265. this.freeVector3( v2 );
  266. return t2;
  267. },
  268. multiplyMatrices3: function ( m1, m2 ) {
  269. var m3 = [];
  270. var v10 = this.rowOfMatrix3( m1, 0 );
  271. var v11 = this.rowOfMatrix3( m1, 1 );
  272. var v12 = this.rowOfMatrix3( m1, 2 );
  273. var v20 = this.columnOfMatrix3( m2, 0 );
  274. var v21 = this.columnOfMatrix3( m2, 1 );
  275. var v22 = this.columnOfMatrix3( m2, 2 );
  276. m3[ 0 ] = this.dotVectors3( v10, v20 );
  277. m3[ 1 ] = this.dotVectors3( v10, v21 );
  278. m3[ 2 ] = this.dotVectors3( v10, v22 );
  279. m3[ 3 ] = this.dotVectors3( v11, v20 );
  280. m3[ 4 ] = this.dotVectors3( v11, v21 );
  281. m3[ 5 ] = this.dotVectors3( v11, v22 );
  282. m3[ 6 ] = this.dotVectors3( v12, v20 );
  283. m3[ 7 ] = this.dotVectors3( v12, v21 );
  284. m3[ 8 ] = this.dotVectors3( v12, v22 );
  285. this.freeVector3( v10 );
  286. this.freeVector3( v11 );
  287. this.freeVector3( v12 );
  288. this.freeVector3( v20 );
  289. this.freeVector3( v21 );
  290. this.freeVector3( v22 );
  291. return m3;
  292. },
  293. addVector3: function( v1, v2 ) {
  294. var v = this.allocVector3();
  295. v.setValue( v1.x() + v2.x(), v1.y() + v2.y(), v1.z() + v2.z() );
  296. return v;
  297. },
  298. dotVectors3: function( v1, v2 ) {
  299. return v1.x() * v2.x() + v1.y() * v2.y() + v1.z() * v2.z();
  300. },
  301. rowOfMatrix3: function( m, i ) {
  302. var v = this.allocVector3();
  303. v.setValue( m[ i * 3 + 0 ], m[ i * 3 + 1 ], m[ i * 3 + 2 ] );
  304. return v;
  305. },
  306. columnOfMatrix3: function( m, i ) {
  307. var v = this.allocVector3();
  308. v.setValue( m[ i + 0 ], m[ i + 3 ], m[ i + 6 ] );
  309. return v;
  310. },
  311. negativeVector3: function( v ) {
  312. var v2 = this.allocVector3();
  313. v2.setValue( -v.x(), -v.y(), -v.z() );
  314. return v2;
  315. },
  316. multiplyMatrix3ByVector3: function ( m, v ) {
  317. var v4 = this.allocVector3();
  318. var v0 = this.rowOfMatrix3( m, 0 );
  319. var v1 = this.rowOfMatrix3( m, 1 );
  320. var v2 = this.rowOfMatrix3( m, 2 );
  321. var x = this.dotVectors3( v0, v );
  322. var y = this.dotVectors3( v1, v );
  323. var z = this.dotVectors3( v2, v );
  324. v4.setValue( x, y, z );
  325. this.freeVector3( v0 );
  326. this.freeVector3( v1 );
  327. this.freeVector3( v2 );
  328. return v4;
  329. },
  330. transposeMatrix3: function( m ) {
  331. var m2 = [];
  332. m2[ 0 ] = m[ 0 ];
  333. m2[ 1 ] = m[ 3 ];
  334. m2[ 2 ] = m[ 6 ];
  335. m2[ 3 ] = m[ 1 ];
  336. m2[ 4 ] = m[ 4 ];
  337. m2[ 5 ] = m[ 7 ];
  338. m2[ 6 ] = m[ 2 ];
  339. m2[ 7 ] = m[ 5 ];
  340. m2[ 8 ] = m[ 8 ];
  341. return m2;
  342. },
  343. quaternionToMatrix3: function ( q ) {
  344. var m = [];
  345. var x = q.x();
  346. var y = q.y();
  347. var z = q.z();
  348. var w = q.w();
  349. var xx = x * x;
  350. var yy = y * y;
  351. var zz = z * z;
  352. var xy = x * y;
  353. var yz = y * z;
  354. var zx = z * x;
  355. var xw = x * w;
  356. var yw = y * w;
  357. var zw = z * w;
  358. m[ 0 ] = 1 - 2 * ( yy + zz );
  359. m[ 1 ] = 2 * ( xy - zw );
  360. m[ 2 ] = 2 * ( zx + yw );
  361. m[ 3 ] = 2 * ( xy + zw );
  362. m[ 4 ] = 1 - 2 * ( zz + xx );
  363. m[ 5 ] = 2 * ( yz - xw );
  364. m[ 6 ] = 2 * ( zx - yw );
  365. m[ 7 ] = 2 * ( yz + xw );
  366. m[ 8 ] = 1 - 2 * ( xx + yy );
  367. return m;
  368. },
  369. matrix3ToQuaternion: function( m ) {
  370. var t = m[ 0 ] + m[ 4 ] + m[ 8 ];
  371. var s, x, y, z, w;
  372. if( t > 0 ) {
  373. s = Math.sqrt( t + 1.0 ) * 2;
  374. w = 0.25 * s;
  375. x = ( m[ 7 ] - m[ 5 ] ) / s;
  376. y = ( m[ 2 ] - m[ 6 ] ) / s;
  377. z = ( m[ 3 ] - m[ 1 ] ) / s;
  378. } else if( ( m[ 0 ] > m[ 4 ] ) && ( m[ 0 ] > m[ 8 ] ) ) {
  379. s = Math.sqrt( 1.0 + m[ 0 ] - m[ 4 ] - m[ 8 ] ) * 2;
  380. w = ( m[ 7 ] - m[ 5 ] ) / s;
  381. x = 0.25 * s;
  382. y = ( m[ 1 ] + m[ 3 ] ) / s;
  383. z = ( m[ 2 ] + m[ 6 ] ) / s;
  384. } else if( m[ 4 ] > m[ 8 ] ) {
  385. s = Math.sqrt( 1.0 + m[ 4 ] - m[ 0 ] - m[ 8 ] ) * 2;
  386. w = ( m[ 2 ] - m[ 6 ] ) / s;
  387. x = ( m[ 1 ] + m[ 3 ] ) / s;
  388. y = 0.25 * s;
  389. z = ( m[ 5 ] + m[ 7 ] ) / s;
  390. } else {
  391. s = Math.sqrt( 1.0 + m[ 8 ] - m[ 0 ] - m[ 4 ] ) * 2;
  392. w = ( m[ 3 ] - m[ 1 ] ) / s;
  393. x = ( m[ 2 ] + m[ 6 ] ) / s;
  394. y = ( m[ 5 ] + m[ 7 ] ) / s;
  395. z = 0.25 * s;
  396. }
  397. var q = this.allocQuaternion();
  398. q.setX( x );
  399. q.setY( y );
  400. q.setZ( z );
  401. q.setW( w );
  402. return q;
  403. },
  404. };
  405. THREE.MMDPhysics.RigidBody = function ( mesh, world, params, helper ) {
  406. this.mesh = mesh;
  407. this.world = world;
  408. this.params = params;
  409. this.helper = helper;
  410. this.body = null;
  411. this.bone = null;
  412. this.boneOffsetForm = null;
  413. this.boneOffsetFormInverse = null;
  414. this.init();
  415. };
  416. THREE.MMDPhysics.RigidBody.prototype = {
  417. constructor: THREE.MMDPhysics.RigidBody,
  418. init: function () {
  419. function generateShape( p ) {
  420. switch( p.shapeType ) {
  421. case 0:
  422. return new Ammo.btSphereShape( p.width );
  423. case 1:
  424. return new Ammo.btBoxShape( new Ammo.btVector3( p.width, p.height, p.depth ) );
  425. case 2:
  426. return new Ammo.btCapsuleShape( p.width, p.height );
  427. default:
  428. throw 'unknown shape type ' + p.shapeType;
  429. }
  430. };
  431. var helper = this.helper;
  432. var params = this.params;
  433. var bones = this.mesh.skeleton.bones;
  434. var bone = ( params.boneIndex === -1 ) ? new THREE.Bone() : bones[ params.boneIndex ];
  435. var shape = generateShape( params );
  436. var weight = ( params.type === 0 ) ? 0 : params.weight;
  437. var localInertia = helper.allocVector3();
  438. localInertia.setValue( 0, 0, 0 );
  439. if( weight !== 0 ) {
  440. shape.calculateLocalInertia( weight, localInertia );
  441. }
  442. var boneOffsetForm = helper.allocTransform();
  443. helper.setIdentity( boneOffsetForm );
  444. helper.setOriginFromArray3( boneOffsetForm, params.position );
  445. helper.setBasisFromArray3( boneOffsetForm, params.rotation );
  446. var boneForm = helper.allocTransform();
  447. helper.setIdentity( boneForm );
  448. helper.setOriginFromArray3( boneForm, bone.getWorldPosition().toArray() );
  449. var form = helper.multiplyTransforms( boneForm, boneOffsetForm );
  450. var state = new Ammo.btDefaultMotionState( form );
  451. var info = new Ammo.btRigidBodyConstructionInfo( weight, state, shape, localInertia );
  452. info.set_m_friction( params.friction );
  453. info.set_m_restitution( params.restitution );
  454. var body = new Ammo.btRigidBody( info );
  455. if ( params.type === 0 ) {
  456. body.setCollisionFlags( body.getCollisionFlags() | 2 );
  457. /*
  458. * It'd be better to comment out this line though in general I should call this method
  459. * because I'm not sure why but physics will be more like MMD's
  460. * if I comment out.
  461. */
  462. body.setActivationState( 4 );
  463. }
  464. body.setDamping( params.positionDamping, params.rotationDamping );
  465. body.setSleepingThresholds( 0, 0 );
  466. this.world.addRigidBody( body, 1 << params.groupIndex, params.groupTarget );
  467. this.body = body;
  468. this.bone = bone;
  469. this.boneOffsetForm = boneOffsetForm;
  470. this.boneOffsetFormInverse = helper.inverseTransform( boneOffsetForm );
  471. helper.freeVector3( localInertia );
  472. helper.freeTransform( form );
  473. helper.freeTransform( boneForm );
  474. },
  475. reset: function () {
  476. this.setTransformFromBone();
  477. },
  478. updateFromBone: function () {
  479. if ( this.params.boneIndex === -1 ) {
  480. return;
  481. }
  482. if ( this.params.type === 0 ) {
  483. this.setTransformFromBone();
  484. }
  485. },
  486. updateBone: function () {
  487. if ( this.params.type === 0 || this.params.boneIndex === -1 ) {
  488. return;
  489. }
  490. this.updateBoneRotation();
  491. if ( this.params.type === 1 ) {
  492. this.updateBonePosition();
  493. }
  494. this.bone.updateMatrixWorld( true );
  495. if ( this.params.type === 2 ) {
  496. this.setPositionFromBone();
  497. }
  498. },
  499. getBoneTransform: function () {
  500. var helper = this.helper;
  501. var p = this.bone.getWorldPosition();
  502. var q = this.bone.getWorldQuaternion();
  503. var tr = helper.allocTransform();
  504. helper.setOriginFromArray3( tr, p.toArray() );
  505. helper.setBasisFromArray4( tr, q.toArray() );
  506. var form = helper.multiplyTransforms( tr, this.boneOffsetForm );
  507. helper.freeTransform( tr );
  508. return form;
  509. },
  510. getWorldTransformForBone: function () {
  511. var helper = this.helper;
  512. var tr = helper.allocTransform();
  513. this.body.getMotionState().getWorldTransform( tr );
  514. var tr2 = helper.multiplyTransforms( tr, this.boneOffsetFormInverse );
  515. helper.freeTransform( tr );
  516. return tr2;
  517. },
  518. setTransformFromBone: function () {
  519. var helper = this.helper;
  520. var form = this.getBoneTransform();
  521. // TODO: check the most appropriate way to set
  522. //this.body.setWorldTransform( form );
  523. this.body.setCenterOfMassTransform( form );
  524. this.body.getMotionState().setWorldTransform( form );
  525. helper.freeTransform( form );
  526. },
  527. setPositionFromBone: function () {
  528. var helper = this.helper;
  529. var form = this.getBoneTransform();
  530. var tr = helper.allocTransform();
  531. this.body.getMotionState().getWorldTransform( tr );
  532. helper.copyOrigin( tr, form );
  533. // TODO: check the most appropriate way to set
  534. //this.body.setWorldTransform( tr );
  535. this.body.setCenterOfMassTransform( tr );
  536. this.body.getMotionState().setWorldTransform( tr );
  537. helper.freeTransform( tr );
  538. helper.freeTransform( form );
  539. },
  540. updateBoneRotation: function () {
  541. this.bone.updateMatrixWorld( true );
  542. var helper = this.helper;
  543. var tr = this.getWorldTransformForBone();
  544. var q = helper.getBasis( tr );
  545. var thQ = helper.allocThreeQuaternion();
  546. var thQ2 = helper.allocThreeQuaternion();
  547. var thQ3 = helper.allocThreeQuaternion();
  548. thQ.set( q.x(), q.y(), q.z(), q.w() );
  549. thQ2.setFromRotationMatrix( this.bone.matrixWorld );
  550. thQ2.conjugate()
  551. thQ2.multiply( thQ );
  552. //this.bone.quaternion.multiply( thQ2 );
  553. thQ3.setFromRotationMatrix( this.bone.matrix );
  554. this.bone.quaternion.copy( thQ2.multiply( thQ3 ) );
  555. helper.freeThreeQuaternion( thQ );
  556. helper.freeThreeQuaternion( thQ2 );
  557. helper.freeThreeQuaternion( thQ3 );
  558. helper.freeQuaternion( q );
  559. helper.freeTransform( tr );
  560. },
  561. updateBonePosition: function () {
  562. var helper = this.helper;
  563. var tr = this.getWorldTransformForBone();
  564. var thV = helper.allocThreeVector3();
  565. var o = helper.getOrigin( tr );
  566. thV.set( o.x(), o.y(), o.z() );
  567. var v = this.bone.worldToLocal( thV );
  568. this.bone.position.add( v );
  569. helper.freeThreeVector3( thV );
  570. helper.freeTransform( tr );
  571. }
  572. };
  573. THREE.MMDPhysics.Constraint = function ( mesh, world, bodyA, bodyB, params, helper ) {
  574. this.mesh = mesh;
  575. this.world = world;
  576. this.bodyA = bodyA;
  577. this.bodyB = bodyB;
  578. this.params = params;
  579. this.helper = helper;
  580. this.constraint = null;
  581. this.init();
  582. };
  583. THREE.MMDPhysics.Constraint.prototype = {
  584. constructor: THREE.MMDPhysics.Constraint,
  585. init: function () {
  586. var helper = this.helper;
  587. var params = this.params;
  588. var bodyA = this.bodyA;
  589. var bodyB = this.bodyB;
  590. var form = helper.allocTransform();
  591. helper.setIdentity( form );
  592. helper.setOriginFromArray3( form, params.position );
  593. helper.setBasisFromArray3( form, params.rotation );
  594. var formA = helper.allocTransform();
  595. var formB = helper.allocTransform();
  596. bodyA.body.getMotionState().getWorldTransform( formA );
  597. bodyB.body.getMotionState().getWorldTransform( formB );
  598. var formInverseA = helper.inverseTransform( formA );
  599. var formInverseB = helper.inverseTransform( formB );
  600. var formA2 = helper.multiplyTransforms( formInverseA, form );
  601. var formB2 = helper.multiplyTransforms( formInverseB, form );
  602. var constraint = new Ammo.btGeneric6DofSpringConstraint( bodyA.body, bodyB.body, formA2, formB2, true );
  603. var lll = helper.allocVector3();
  604. var lul = helper.allocVector3();
  605. var all = helper.allocVector3();
  606. var aul = helper.allocVector3();
  607. lll.setValue( params.translationLimitation1[ 0 ],
  608. params.translationLimitation1[ 1 ],
  609. params.translationLimitation1[ 2 ] );
  610. lul.setValue( params.translationLimitation2[ 0 ],
  611. params.translationLimitation2[ 1 ],
  612. params.translationLimitation2[ 2 ] );
  613. all.setValue( params.rotationLimitation1[ 0 ],
  614. params.rotationLimitation1[ 1 ],
  615. params.rotationLimitation1[ 2 ] );
  616. aul.setValue( params.rotationLimitation2[ 0 ],
  617. params.rotationLimitation2[ 1 ],
  618. params.rotationLimitation2[ 2 ] );
  619. constraint.setLinearLowerLimit( lll );
  620. constraint.setLinearUpperLimit( lul );
  621. constraint.setAngularLowerLimit( all );
  622. constraint.setAngularUpperLimit( aul );
  623. for ( var i = 0; i < 3; i++ ) {
  624. if( params.springPosition[ i ] !== 0 ) {
  625. constraint.enableSpring( i, true );
  626. constraint.setStiffness( i, params.springPosition[ i ] );
  627. }
  628. }
  629. for ( var i = 0; i < 3; i++ ) {
  630. if( params.springRotation[ i ] !== 0 ) {
  631. constraint.enableSpring( i + 3, true );
  632. constraint.setStiffness( i + 3, params.springRotation[ i ] );
  633. }
  634. }
  635. /*
  636. * Currently(10/31/2016) official ammo.js doesn't support
  637. * btGeneric6DofSpringConstraint.setParam method.
  638. * You need custom ammo.js (add the method into idl) if you wanna use.
  639. * By setting this parameter, physics will be more like MMD's
  640. */
  641. if ( constraint.setParam !== undefined ) {
  642. for ( var i = 0; i < 6; i ++ ) {
  643. // this parameter is from http://www20.atpages.jp/katwat/three.js_r58/examples/mytest37/mmd.three.js
  644. constraint.setParam( 2, 0.475, i );
  645. }
  646. }
  647. this.world.addConstraint( constraint, true );
  648. this.constraint = constraint;
  649. helper.freeTransform( form );
  650. helper.freeTransform( formA );
  651. helper.freeTransform( formB );
  652. helper.freeTransform( formInverseA );
  653. helper.freeTransform( formInverseB );
  654. helper.freeTransform( formA2 );
  655. helper.freeTransform( formB2 );
  656. helper.freeVector3( lll );
  657. helper.freeVector3( lul );
  658. helper.freeVector3( all );
  659. helper.freeVector3( aul );
  660. }
  661. };
  662. /*
  663. * This helper displays rigid bodies of mesh for debug.
  664. * It should be under Scene because it just sets world position/roration to meshes
  665. * for simplicity.
  666. */
  667. THREE.MMDPhysicsHelper = function ( mesh ) {
  668. if ( mesh.physics === undefined || mesh.geometry.rigidBodies === undefined ) {
  669. throw 'THREE.MMDPhysicsHelper requires physics in mesh and rigidBodies in mesh.geometry.';
  670. }
  671. THREE.Object3D.call( this );
  672. this.mesh = mesh;
  673. this._init();
  674. };
  675. THREE.MMDPhysicsHelper.prototype = Object.create( THREE.Object3D.prototype );
  676. THREE.MMDPhysicsHelper.prototype.constructor = THREE.MMDPhysicsHelper;
  677. THREE.MMDPhysicsHelper.prototype._init = function () {
  678. function createGeometry( param ) {
  679. switch ( param.shapeType ) {
  680. case 0:
  681. return new THREE.SphereBufferGeometry( param.width, 16, 8 );
  682. case 1:
  683. return new THREE.BoxBufferGeometry( param.width * 2, param.height * 2, param.depth * 2, 8, 8, 8);
  684. case 2:
  685. return new createCapsuleGeometry( param.width, param.height, 16, 8 );
  686. default:
  687. return null;
  688. }
  689. }
  690. // copy from http://www20.atpages.jp/katwat/three.js_r58/examples/mytest37/mytest37.js?ver=20160815
  691. function createCapsuleGeometry( radius, cylinderHeight, segmentsRadius, segmentsHeight ) {
  692. var geometry = new THREE.CylinderBufferGeometry( radius, radius, cylinderHeight, segmentsRadius, segmentsHeight, true );
  693. var upperSphere = new THREE.Mesh( new THREE.SphereBufferGeometry( radius, segmentsRadius, segmentsHeight, 0, Math.PI * 2, 0, Math.PI / 2 ) );
  694. var lowerSphere = new THREE.Mesh( new THREE.SphereBufferGeometry( radius, segmentsRadius, segmentsHeight, 0, Math.PI * 2, Math.PI / 2, Math.PI / 2 ) );
  695. upperSphere.position.set( 0, cylinderHeight / 2, 0 );
  696. lowerSphere.position.set( 0, -cylinderHeight / 2, 0 );
  697. upperSphere.updateMatrix();
  698. lowerSphere.updateMatrix();
  699. geometry.merge( upperSphere.geometry, upperSphere.matrix );
  700. geometry.merge( lowerSphere.geometry, lowerSphere.matrix );
  701. return geometry;
  702. }
  703. function createMaterial( param ) {
  704. var color;
  705. switch ( param.type ) {
  706. case 0:
  707. color = 0xff8888;
  708. break;
  709. case 1:
  710. color = 0x88ff88;
  711. break;
  712. case 2:
  713. color = 0x8888ff;
  714. break;
  715. default:
  716. color = 0x000000;
  717. break;
  718. }
  719. return new THREE.MeshBasicMaterial( {
  720. color: new THREE.Color( color ),
  721. wireframe: true,
  722. depthTest: false,
  723. depthWrite: false,
  724. opacity: 0.25,
  725. transparent: true
  726. } );
  727. }
  728. for ( var i = 0, il = this.mesh.geometry.rigidBodies.length; i < il; i ++ ) {
  729. var param = this.mesh.geometry.rigidBodies[ i ];
  730. var mesh = new THREE.Mesh( createGeometry( param ), createMaterial( param ) );
  731. this.add( mesh );
  732. }
  733. };
  734. THREE.MMDPhysicsHelper.prototype.update = function () {
  735. for ( var i = 0, il = this.mesh.geometry.rigidBodies.length; i < il; i ++ ) {
  736. var body = this.mesh.physics.bodies[ i ].body;
  737. var mesh = this.children[ i ];
  738. var tr = body.getCenterOfMassTransform();
  739. var o = tr.getOrigin();
  740. var r = tr.getRotation();
  741. mesh.position.set( o.x(), o.y(), o.z() );
  742. mesh.quaternion.set( r.x(), r.y(), r.z(), r.w() );
  743. }
  744. };