MMDPhysics.js 25 KB

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