2
0

MMDPhysics.js 26 KB

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