MMDAnimationHelper.js 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  1. import {
  2. AnimationMixer,
  3. Object3D,
  4. Quaternion,
  5. Vector3
  6. } from '../../../build/three.module.js';
  7. import { CCDIKSolver } from '../animation/CCDIKSolver.js';
  8. import { MMDPhysics } from '../animation/MMDPhysics.js';
  9. /**
  10. * MMDAnimationHelper handles animation of MMD assets loaded by MMDLoader
  11. * with MMD special features as IK, Grant, and Physics.
  12. *
  13. * Dependencies
  14. * - ammo.js https://github.com/kripken/ammo.js
  15. * - MMDPhysics
  16. * - CCDIKSolver
  17. *
  18. * TODO
  19. * - more precise grant skinning support.
  20. */
  21. var MMDAnimationHelper = ( function () {
  22. /**
  23. * @param {Object} params - (optional)
  24. * @param {boolean} params.sync - Whether animation durations of added objects are synched. Default is true.
  25. * @param {Number} params.afterglow - Default is 0.0.
  26. * @param {boolean} params.resetPhysicsOnLoop - Default is true.
  27. */
  28. function MMDAnimationHelper( params ) {
  29. params = params || {};
  30. this.meshes = [];
  31. this.camera = null;
  32. this.cameraTarget = new Object3D();
  33. this.cameraTarget.name = 'target';
  34. this.audio = null;
  35. this.audioManager = null;
  36. this.objects = new WeakMap();
  37. this.configuration = {
  38. sync: params.sync !== undefined
  39. ? params.sync : true,
  40. afterglow: params.afterglow !== undefined
  41. ? params.afterglow : 0.0,
  42. resetPhysicsOnLoop: params.resetPhysicsOnLoop !== undefined
  43. ? params.resetPhysicsOnLoop : true,
  44. pmxAnimation: params.pmxAnimation !== undefined
  45. ? params.pmxAnimation : false
  46. };
  47. this.enabled = {
  48. animation: true,
  49. ik: true,
  50. grant: true,
  51. physics: true,
  52. cameraAnimation: true
  53. };
  54. this.onBeforePhysics = function ( /* mesh */ ) {};
  55. // experimental
  56. this.sharedPhysics = false;
  57. this.masterPhysics = null;
  58. }
  59. MMDAnimationHelper.prototype = {
  60. constructor: MMDAnimationHelper,
  61. /**
  62. * Adds an Three.js Object to helper and setups animation.
  63. * The anmation durations of added objects are synched
  64. * if this.configuration.sync is true.
  65. *
  66. * @param {THREE.SkinnedMesh|THREE.Camera|THREE.Audio} object
  67. * @param {Object} params - (optional)
  68. * @param {THREE.AnimationClip|Array<THREE.AnimationClip>} params.animation - Only for THREE.SkinnedMesh and THREE.Camera. Default is undefined.
  69. * @param {boolean} params.physics - Only for THREE.SkinnedMesh. Default is true.
  70. * @param {Integer} params.warmup - Only for THREE.SkinnedMesh and physics is true. Default is 60.
  71. * @param {Number} params.unitStep - Only for THREE.SkinnedMesh and physics is true. Default is 1 / 65.
  72. * @param {Integer} params.maxStepNum - Only for THREE.SkinnedMesh and physics is true. Default is 3.
  73. * @param {Vector3} params.gravity - Only for THREE.SkinnedMesh and physics is true. Default ( 0, - 9.8 * 10, 0 ).
  74. * @param {Number} params.delayTime - Only for THREE.Audio. Default is 0.0.
  75. * @return {MMDAnimationHelper}
  76. */
  77. add: function ( object, params ) {
  78. params = params || {};
  79. if ( object.isSkinnedMesh ) {
  80. this._addMesh( object, params );
  81. } else if ( object.isCamera ) {
  82. this._setupCamera( object, params );
  83. } else if ( object.type === 'Audio' ) {
  84. this._setupAudio( object, params );
  85. } else {
  86. throw new Error( 'THREE.MMDAnimationHelper.add: '
  87. + 'accepts only '
  88. + 'THREE.SkinnedMesh or '
  89. + 'THREE.Camera or '
  90. + 'THREE.Audio instance.' );
  91. }
  92. if ( this.configuration.sync ) this._syncDuration();
  93. return this;
  94. },
  95. /**
  96. * Removes an Three.js Object from helper.
  97. *
  98. * @param {THREE.SkinnedMesh|THREE.Camera|THREE.Audio} object
  99. * @return {MMDAnimationHelper}
  100. */
  101. remove: function ( object ) {
  102. if ( object.isSkinnedMesh ) {
  103. this._removeMesh( object );
  104. } else if ( object.isCamera ) {
  105. this._clearCamera( object );
  106. } else if ( object.type === 'Audio' ) {
  107. this._clearAudio( object );
  108. } else {
  109. throw new Error( 'THREE.MMDAnimationHelper.remove: '
  110. + 'accepts only '
  111. + 'THREE.SkinnedMesh or '
  112. + 'THREE.Camera or '
  113. + 'THREE.Audio instance.' );
  114. }
  115. if ( this.configuration.sync ) this._syncDuration();
  116. return this;
  117. },
  118. /**
  119. * Updates the animation.
  120. *
  121. * @param {Number} delta
  122. * @return {MMDAnimationHelper}
  123. */
  124. update: function ( delta ) {
  125. if ( this.audioManager !== null ) this.audioManager.control( delta );
  126. for ( var i = 0; i < this.meshes.length; i ++ ) {
  127. this._animateMesh( this.meshes[ i ], delta );
  128. }
  129. if ( this.sharedPhysics ) this._updateSharedPhysics( delta );
  130. if ( this.camera !== null ) this._animateCamera( this.camera, delta );
  131. return this;
  132. },
  133. /**
  134. * Changes the pose of SkinnedMesh as VPD specifies.
  135. *
  136. * @param {THREE.SkinnedMesh} mesh
  137. * @param {Object} vpd - VPD content parsed MMDParser
  138. * @param {Object} params - (optional)
  139. * @param {boolean} params.resetPose - Default is true.
  140. * @param {boolean} params.ik - Default is true.
  141. * @param {boolean} params.grant - Default is true.
  142. * @return {MMDAnimationHelper}
  143. */
  144. pose: function ( mesh, vpd, params ) {
  145. params = params || {};
  146. if ( params.resetPose !== false ) mesh.pose();
  147. var bones = mesh.skeleton.bones;
  148. var boneParams = vpd.bones;
  149. var boneNameDictionary = {};
  150. for ( var i = 0, il = bones.length; i < il; i ++ ) {
  151. boneNameDictionary[ bones[ i ].name ] = i;
  152. }
  153. var vector = new Vector3();
  154. var quaternion = new Quaternion();
  155. for ( var i = 0, il = boneParams.length; i < il; i ++ ) {
  156. var boneParam = boneParams[ i ];
  157. var boneIndex = boneNameDictionary[ boneParam.name ];
  158. if ( boneIndex === undefined ) continue;
  159. var bone = bones[ boneIndex ];
  160. bone.position.add( vector.fromArray( boneParam.translation ) );
  161. bone.quaternion.multiply( quaternion.fromArray( boneParam.quaternion ) );
  162. }
  163. mesh.updateMatrixWorld( true );
  164. // PMX animation system special path
  165. if ( this.configuration.pmxAnimation &&
  166. mesh.geometry.userData.MMD && mesh.geometry.userData.MMD.format === 'pmx' ) {
  167. var sortedBonesData = this._sortBoneDataArray( mesh.geometry.userData.MMD.bones.slice() );
  168. var ikSolver = params.ik !== false ? this._createCCDIKSolver( mesh ) : null;
  169. var grantSolver = params.grant !== false ? this.createGrantSolver( mesh ) : null;
  170. this._animatePMXMesh( mesh, sortedBonesData, ikSolver, grantSolver );
  171. } else {
  172. if ( params.ik !== false ) {
  173. this._createCCDIKSolver( mesh ).update();
  174. }
  175. if ( params.grant !== false ) {
  176. this.createGrantSolver( mesh ).update();
  177. }
  178. }
  179. return this;
  180. },
  181. /**
  182. * Enabes/Disables an animation feature.
  183. *
  184. * @param {string} key
  185. * @param {boolean} enabled
  186. * @return {MMDAnimationHelper}
  187. */
  188. enable: function ( key, enabled ) {
  189. if ( this.enabled[ key ] === undefined ) {
  190. throw new Error( 'THREE.MMDAnimationHelper.enable: '
  191. + 'unknown key ' + key );
  192. }
  193. this.enabled[ key ] = enabled;
  194. if ( key === 'physics' ) {
  195. for ( var i = 0, il = this.meshes.length; i < il; i ++ ) {
  196. this._optimizeIK( this.meshes[ i ], enabled );
  197. }
  198. }
  199. return this;
  200. },
  201. /**
  202. * Creates an GrantSolver instance.
  203. *
  204. * @param {THREE.SkinnedMesh} mesh
  205. * @return {GrantSolver}
  206. */
  207. createGrantSolver: function ( mesh ) {
  208. return new GrantSolver( mesh, mesh.geometry.userData.MMD.grants );
  209. },
  210. // private methods
  211. _addMesh: function ( mesh, params ) {
  212. if ( this.meshes.indexOf( mesh ) >= 0 ) {
  213. throw new Error( 'THREE.MMDAnimationHelper._addMesh: '
  214. + 'SkinnedMesh \'' + mesh.name + '\' has already been added.' );
  215. }
  216. this.meshes.push( mesh );
  217. this.objects.set( mesh, { looped: false } );
  218. this._setupMeshAnimation( mesh, params.animation );
  219. if ( params.physics !== false ) {
  220. this._setupMeshPhysics( mesh, params );
  221. }
  222. return this;
  223. },
  224. _setupCamera: function ( camera, params ) {
  225. if ( this.camera === camera ) {
  226. throw new Error( 'THREE.MMDAnimationHelper._setupCamera: '
  227. + 'Camera \'' + camera.name + '\' has already been set.' );
  228. }
  229. if ( this.camera ) this.clearCamera( this.camera );
  230. this.camera = camera;
  231. camera.add( this.cameraTarget );
  232. this.objects.set( camera, {} );
  233. if ( params.animation !== undefined ) {
  234. this._setupCameraAnimation( camera, params.animation );
  235. }
  236. return this;
  237. },
  238. _setupAudio: function ( audio, params ) {
  239. if ( this.audio === audio ) {
  240. throw new Error( 'THREE.MMDAnimationHelper._setupAudio: '
  241. + 'Audio \'' + audio.name + '\' has already been set.' );
  242. }
  243. if ( this.audio ) this.clearAudio( this.audio );
  244. this.audio = audio;
  245. this.audioManager = new AudioManager( audio, params );
  246. this.objects.set( this.audioManager, {
  247. duration: this.audioManager.duration
  248. } );
  249. return this;
  250. },
  251. _removeMesh: function ( mesh ) {
  252. var found = false;
  253. var writeIndex = 0;
  254. for ( var i = 0, il = this.meshes.length; i < il; i ++ ) {
  255. if ( this.meshes[ i ] === mesh ) {
  256. this.objects.delete( mesh );
  257. found = true;
  258. continue;
  259. }
  260. this.meshes[ writeIndex ++ ] = this.meshes[ i ];
  261. }
  262. if ( ! found ) {
  263. throw new Error( 'THREE.MMDAnimationHelper._removeMesh: '
  264. + 'SkinnedMesh \'' + mesh.name + '\' has not been added yet.' );
  265. }
  266. this.meshes.length = writeIndex;
  267. return this;
  268. },
  269. _clearCamera: function ( camera ) {
  270. if ( camera !== this.camera ) {
  271. throw new Error( 'THREE.MMDAnimationHelper._clearCamera: '
  272. + 'Camera \'' + camera.name + '\' has not been set yet.' );
  273. }
  274. this.camera.remove( this.cameraTarget );
  275. this.objects.delete( this.camera );
  276. this.camera = null;
  277. return this;
  278. },
  279. _clearAudio: function ( audio ) {
  280. if ( audio !== this.audio ) {
  281. throw new Error( 'THREE.MMDAnimationHelper._clearAudio: '
  282. + 'Audio \'' + audio.name + '\' has not been set yet.' );
  283. }
  284. this.objects.delete( this.audioManager );
  285. this.audio = null;
  286. this.audioManager = null;
  287. return this;
  288. },
  289. _setupMeshAnimation: function ( mesh, animation ) {
  290. var objects = this.objects.get( mesh );
  291. if ( animation !== undefined ) {
  292. var animations = Array.isArray( animation )
  293. ? animation : [ animation ];
  294. objects.mixer = new AnimationMixer( mesh );
  295. for ( var i = 0, il = animations.length; i < il; i ++ ) {
  296. objects.mixer.clipAction( animations[ i ] ).play();
  297. }
  298. // TODO: find a workaround not to access ._clip looking like a private property
  299. objects.mixer.addEventListener( 'loop', function ( event ) {
  300. var tracks = event.action._clip.tracks;
  301. if ( tracks.length > 0 &&
  302. tracks[ 0 ].name.slice( 0, 6 ) !== '.bones' ) return;
  303. objects.looped = true;
  304. } );
  305. }
  306. objects.ikSolver = this._createCCDIKSolver( mesh );
  307. objects.grantSolver = this.createGrantSolver( mesh );
  308. return this;
  309. },
  310. _setupCameraAnimation: function ( camera, animation ) {
  311. var animations = Array.isArray( animation )
  312. ? animation : [ animation ];
  313. var objects = this.objects.get( camera );
  314. objects.mixer = new AnimationMixer( camera );
  315. for ( var i = 0, il = animations.length; i < il; i ++ ) {
  316. objects.mixer.clipAction( animations[ i ] ).play();
  317. }
  318. },
  319. _setupMeshPhysics: function ( mesh, params ) {
  320. var objects = this.objects.get( mesh );
  321. // shared physics is experimental
  322. if ( params.world === undefined && this.sharedPhysics ) {
  323. var masterPhysics = this._getMasterPhysics();
  324. if ( masterPhysics !== null ) world = masterPhysics.world; // eslint-disable-line no-undef
  325. }
  326. objects.physics = this._createMMDPhysics( mesh, params );
  327. if ( objects.mixer && params.animationWarmup !== false ) {
  328. this._animateMesh( mesh, 0 );
  329. objects.physics.reset();
  330. }
  331. objects.physics.warmup( params.warmup !== undefined ? params.warmup : 60 );
  332. this._optimizeIK( mesh, true );
  333. },
  334. _animateMesh: function ( mesh, delta ) {
  335. var objects = this.objects.get( mesh );
  336. var mixer = objects.mixer;
  337. var ikSolver = objects.ikSolver;
  338. var grantSolver = objects.grantSolver;
  339. var physics = objects.physics;
  340. var looped = objects.looped;
  341. if ( mixer && this.enabled.animation ) {
  342. // alternate solution to save/restore bones but less performant?
  343. //mesh.pose();
  344. //this._updatePropertyMixersBuffer( mesh );
  345. this._restoreBones( mesh );
  346. mixer.update( delta );
  347. this._saveBones( mesh );
  348. // PMX animation system special path
  349. if ( this.configuration.pmxAnimation &&
  350. mesh.geometry.userData.MMD && mesh.geometry.userData.MMD.format === 'pmx' ) {
  351. if ( ! objects.sortedBonesData ) objects.sortedBonesData = this._sortBoneDataArray( mesh.geometry.userData.MMD.bones.slice() );
  352. this._animatePMXMesh(
  353. mesh,
  354. objects.sortedBonesData,
  355. ikSolver && this.enabled.ik ? ikSolver : null,
  356. grantSolver && this.enabled.grant ? grantSolver : null
  357. );
  358. } else {
  359. if ( ikSolver && this.enabled.ik ) {
  360. mesh.updateMatrixWorld( true );
  361. ikSolver.update();
  362. }
  363. if ( grantSolver && this.enabled.grant ) {
  364. grantSolver.update();
  365. }
  366. }
  367. }
  368. if ( looped === true && this.enabled.physics ) {
  369. if ( physics && this.configuration.resetPhysicsOnLoop ) physics.reset();
  370. objects.looped = false;
  371. }
  372. if ( physics && this.enabled.physics && ! this.sharedPhysics ) {
  373. this.onBeforePhysics( mesh );
  374. physics.update( delta );
  375. }
  376. },
  377. // Sort bones in order by 1. transformationClass and 2. bone index.
  378. // In PMX animation system, bone transformations should be processed
  379. // in this order.
  380. _sortBoneDataArray: function ( boneDataArray ) {
  381. return boneDataArray.sort( function ( a, b ) {
  382. if ( a.transformationClass !== b.transformationClass ) {
  383. return a.transformationClass - b.transformationClass;
  384. } else {
  385. return a.index - b.index;
  386. }
  387. } );
  388. },
  389. // PMX Animation system is a bit too complex and doesn't great match to
  390. // Three.js Animation system. This method attempts to simulate it as much as
  391. // possible but doesn't perfectly simulate.
  392. // This method is more costly than the regular one so
  393. // you are recommended to set constructor parameter "pmxAnimation: true"
  394. // only if your PMX model animation doesn't work well.
  395. // If you need better method you would be required to write your own.
  396. _animatePMXMesh: function () {
  397. // Keep working quaternions for less GC
  398. var quaternions = [];
  399. var quaternionIndex = 0;
  400. function getQuaternion() {
  401. if ( quaternionIndex >= quaternions.length ) {
  402. quaternions.push( new Quaternion() );
  403. }
  404. return quaternions[ quaternionIndex ++ ];
  405. }
  406. // Save rotation whose grant and IK are already applied
  407. // used by grant children
  408. var grantResultMap = new Map();
  409. function updateOne( mesh, boneIndex, ikSolver, grantSolver ) {
  410. var bones = mesh.skeleton.bones;
  411. var bonesData = mesh.geometry.userData.MMD.bones;
  412. var boneData = bonesData[ boneIndex ];
  413. var bone = bones[ boneIndex ];
  414. // Return if already updated by being referred as a grant parent.
  415. if ( grantResultMap.has( boneIndex ) ) return;
  416. var quaternion = getQuaternion();
  417. // Initialize grant result here to prevent infinite loop.
  418. // If it's referred before updating with actual result later
  419. // result without applyting IK or grant is gotten
  420. // but better than composing of infinite loop.
  421. grantResultMap.set( boneIndex, quaternion.copy( bone.quaternion ) );
  422. // @TODO: Support global grant and grant position
  423. if ( grantSolver && boneData.grant &&
  424. ! boneData.grant.isLocal && boneData.grant.affectRotation ) {
  425. var parentIndex = boneData.grant.parentIndex;
  426. var ratio = boneData.grant.ratio;
  427. if ( ! grantResultMap.has( parentIndex ) ) {
  428. updateOne( mesh, parentIndex, ikSolver, grantSolver );
  429. }
  430. grantSolver.addGrantRotation( bone, grantResultMap.get( parentIndex ), ratio );
  431. }
  432. if ( ikSolver && boneData.ik ) {
  433. // @TODO: Updating world matrices every time solving an IK bone is
  434. // costly. Optimize if possible.
  435. mesh.updateMatrixWorld( true );
  436. ikSolver.updateOne( boneData.ik );
  437. // No confident, but it seems the grant results with ik links should be updated?
  438. var links = boneData.ik.links;
  439. for ( var i = 0, il = links.length; i < il; i ++ ) {
  440. var link = links[ i ];
  441. if ( link.enabled === false ) continue;
  442. var linkIndex = link.index;
  443. if ( grantResultMap.has( linkIndex ) ) {
  444. grantResultMap.set( linkIndex, grantResultMap.get( linkIndex ).copy( bones[ linkIndex ].quaternion ) );
  445. }
  446. }
  447. }
  448. // Update with the actual result here
  449. quaternion.copy( bone.quaternion );
  450. }
  451. return function ( mesh, sortedBonesData, ikSolver, grantSolver ) {
  452. quaternionIndex = 0;
  453. grantResultMap.clear();
  454. for ( var i = 0, il = sortedBonesData.length; i < il; i ++ ) {
  455. updateOne( mesh, sortedBonesData[ i ].index, ikSolver, grantSolver );
  456. }
  457. mesh.updateMatrixWorld( true );
  458. return this;
  459. };
  460. }(),
  461. _animateCamera: function ( camera, delta ) {
  462. var mixer = this.objects.get( camera ).mixer;
  463. if ( mixer && this.enabled.cameraAnimation ) {
  464. mixer.update( delta );
  465. camera.updateProjectionMatrix();
  466. camera.up.set( 0, 1, 0 );
  467. camera.up.applyQuaternion( camera.quaternion );
  468. camera.lookAt( this.cameraTarget.position );
  469. }
  470. },
  471. _optimizeIK: function ( mesh, physicsEnabled ) {
  472. var iks = mesh.geometry.userData.MMD.iks;
  473. var bones = mesh.geometry.userData.MMD.bones;
  474. for ( var i = 0, il = iks.length; i < il; i ++ ) {
  475. var ik = iks[ i ];
  476. var links = ik.links;
  477. for ( var j = 0, jl = links.length; j < jl; j ++ ) {
  478. var link = links[ j ];
  479. if ( physicsEnabled === true ) {
  480. // disable IK of the bone the corresponding rigidBody type of which is 1 or 2
  481. // because its rotation will be overriden by physics
  482. link.enabled = bones[ link.index ].rigidBodyType > 0 ? false : true;
  483. } else {
  484. link.enabled = true;
  485. }
  486. }
  487. }
  488. },
  489. _createCCDIKSolver: function ( mesh ) {
  490. if ( CCDIKSolver === undefined ) {
  491. throw new Error( 'THREE.MMDAnimationHelper: Import CCDIKSolver.' );
  492. }
  493. return new CCDIKSolver( mesh, mesh.geometry.userData.MMD.iks );
  494. },
  495. _createMMDPhysics: function ( mesh, params ) {
  496. if ( MMDPhysics === undefined ) {
  497. throw new Error( 'THREE.MMDPhysics: Import MMDPhysics.' );
  498. }
  499. return new MMDPhysics(
  500. mesh,
  501. mesh.geometry.userData.MMD.rigidBodies,
  502. mesh.geometry.userData.MMD.constraints,
  503. params );
  504. },
  505. /*
  506. * Detects the longest duration and then sets it to them to sync.
  507. * TODO: Not to access private properties ( ._actions and ._clip )
  508. */
  509. _syncDuration: function () {
  510. var max = 0.0;
  511. var objects = this.objects;
  512. var meshes = this.meshes;
  513. var camera = this.camera;
  514. var audioManager = this.audioManager;
  515. // get the longest duration
  516. for ( var i = 0, il = meshes.length; i < il; i ++ ) {
  517. var mixer = this.objects.get( meshes[ i ] ).mixer;
  518. if ( mixer === undefined ) continue;
  519. for ( var j = 0; j < mixer._actions.length; j ++ ) {
  520. var clip = mixer._actions[ j ]._clip;
  521. if ( ! objects.has( clip ) ) {
  522. objects.set( clip, {
  523. duration: clip.duration
  524. } );
  525. }
  526. max = Math.max( max, objects.get( clip ).duration );
  527. }
  528. }
  529. if ( camera !== null ) {
  530. var mixer = this.objects.get( camera ).mixer;
  531. if ( mixer !== undefined ) {
  532. for ( var i = 0, il = mixer._actions.length; i < il; i ++ ) {
  533. var clip = mixer._actions[ i ]._clip;
  534. if ( ! objects.has( clip ) ) {
  535. objects.set( clip, {
  536. duration: clip.duration
  537. } );
  538. }
  539. max = Math.max( max, objects.get( clip ).duration );
  540. }
  541. }
  542. }
  543. if ( audioManager !== null ) {
  544. max = Math.max( max, objects.get( audioManager ).duration );
  545. }
  546. max += this.configuration.afterglow;
  547. // update the duration
  548. for ( var i = 0, il = this.meshes.length; i < il; i ++ ) {
  549. var mixer = this.objects.get( this.meshes[ i ] ).mixer;
  550. if ( mixer === undefined ) continue;
  551. for ( var j = 0, jl = mixer._actions.length; j < jl; j ++ ) {
  552. mixer._actions[ j ]._clip.duration = max;
  553. }
  554. }
  555. if ( camera !== null ) {
  556. var mixer = this.objects.get( camera ).mixer;
  557. if ( mixer !== undefined ) {
  558. for ( var i = 0, il = mixer._actions.length; i < il; i ++ ) {
  559. mixer._actions[ i ]._clip.duration = max;
  560. }
  561. }
  562. }
  563. if ( audioManager !== null ) {
  564. audioManager.duration = max;
  565. }
  566. },
  567. // workaround
  568. _updatePropertyMixersBuffer: function ( mesh ) {
  569. var mixer = this.objects.get( mesh ).mixer;
  570. var propertyMixers = mixer._bindings;
  571. var accuIndex = mixer._accuIndex;
  572. for ( var i = 0, il = propertyMixers.length; i < il; i ++ ) {
  573. var propertyMixer = propertyMixers[ i ];
  574. var buffer = propertyMixer.buffer;
  575. var stride = propertyMixer.valueSize;
  576. var offset = ( accuIndex + 1 ) * stride;
  577. propertyMixer.binding.getValue( buffer, offset );
  578. }
  579. },
  580. /*
  581. * Avoiding these two issues by restore/save bones before/after mixer animation.
  582. *
  583. * 1. PropertyMixer used by AnimationMixer holds cache value in .buffer.
  584. * Calculating IK, Grant, and Physics after mixer animation can break
  585. * the cache coherency.
  586. *
  587. * 2. Applying Grant two or more times without reset the posing breaks model.
  588. */
  589. _saveBones: function ( mesh ) {
  590. var objects = this.objects.get( mesh );
  591. var bones = mesh.skeleton.bones;
  592. var backupBones = objects.backupBones;
  593. if ( backupBones === undefined ) {
  594. backupBones = new Float32Array( bones.length * 7 );
  595. objects.backupBones = backupBones;
  596. }
  597. for ( var i = 0, il = bones.length; i < il; i ++ ) {
  598. var bone = bones[ i ];
  599. bone.position.toArray( backupBones, i * 7 );
  600. bone.quaternion.toArray( backupBones, i * 7 + 3 );
  601. }
  602. },
  603. _restoreBones: function ( mesh ) {
  604. var objects = this.objects.get( mesh );
  605. var backupBones = objects.backupBones;
  606. if ( backupBones === undefined ) return;
  607. var bones = mesh.skeleton.bones;
  608. for ( var i = 0, il = bones.length; i < il; i ++ ) {
  609. var bone = bones[ i ];
  610. bone.position.fromArray( backupBones, i * 7 );
  611. bone.quaternion.fromArray( backupBones, i * 7 + 3 );
  612. }
  613. },
  614. // experimental
  615. _getMasterPhysics: function () {
  616. if ( this.masterPhysics !== null ) return this.masterPhysics;
  617. for ( var i = 0, il = this.meshes.length; i < il; i ++ ) {
  618. var physics = this.meshes[ i ].physics;
  619. if ( physics !== undefined && physics !== null ) {
  620. this.masterPhysics = physics;
  621. return this.masterPhysics;
  622. }
  623. }
  624. return null;
  625. },
  626. _updateSharedPhysics: function ( delta ) {
  627. if ( this.meshes.length === 0 || ! this.enabled.physics || ! this.sharedPhysics ) return;
  628. var physics = this._getMasterPhysics();
  629. if ( physics === null ) return;
  630. for ( var i = 0, il = this.meshes.length; i < il; i ++ ) {
  631. var p = this.meshes[ i ].physics;
  632. if ( p !== null && p !== undefined ) {
  633. p.updateRigidBodies();
  634. }
  635. }
  636. physics.stepSimulation( delta );
  637. for ( var i = 0, il = this.meshes.length; i < il; i ++ ) {
  638. var p = this.meshes[ i ].physics;
  639. if ( p !== null && p !== undefined ) {
  640. p.updateBones();
  641. }
  642. }
  643. }
  644. };
  645. //
  646. /**
  647. * @param {THREE.Audio} audio
  648. * @param {Object} params - (optional)
  649. * @param {Nuumber} params.delayTime
  650. */
  651. function AudioManager( audio, params ) {
  652. params = params || {};
  653. this.audio = audio;
  654. this.elapsedTime = 0.0;
  655. this.currentTime = 0.0;
  656. this.delayTime = params.delayTime !== undefined
  657. ? params.delayTime : 0.0;
  658. this.audioDuration = this.audio.buffer.duration;
  659. this.duration = this.audioDuration + this.delayTime;
  660. }
  661. AudioManager.prototype = {
  662. constructor: AudioManager,
  663. /**
  664. * @param {Number} delta
  665. * @return {AudioManager}
  666. */
  667. control: function ( delta ) {
  668. this.elapsed += delta;
  669. this.currentTime += delta;
  670. if ( this._shouldStopAudio() ) this.audio.stop();
  671. if ( this._shouldStartAudio() ) this.audio.play();
  672. return this;
  673. },
  674. // private methods
  675. _shouldStartAudio: function () {
  676. if ( this.audio.isPlaying ) return false;
  677. while ( this.currentTime >= this.duration ) {
  678. this.currentTime -= this.duration;
  679. }
  680. if ( this.currentTime < this.delayTime ) return false;
  681. // 'duration' can be bigger than 'audioDuration + delayTime' because of sync configuration
  682. if ( ( this.currentTime - this.delayTime ) > this.audioDuration ) return false;
  683. return true;
  684. },
  685. _shouldStopAudio: function () {
  686. return this.audio.isPlaying &&
  687. this.currentTime >= this.duration;
  688. }
  689. };
  690. /**
  691. * Solver for Grant (Fuyo in Japanese. I just google translated because
  692. * Fuyo may be MMD specific term and may not be common word in 3D CG terms.)
  693. * Grant propagates a bone's transform to other bones transforms even if
  694. * they are not children.
  695. * @param {THREE.SkinnedMesh} mesh
  696. * @param {Array<Object>} grants
  697. */
  698. function GrantSolver( mesh, grants ) {
  699. this.mesh = mesh;
  700. this.grants = grants || [];
  701. }
  702. GrantSolver.prototype = {
  703. constructor: GrantSolver,
  704. /**
  705. * Solve all the grant bones
  706. * @return {GrantSolver}
  707. */
  708. update: function () {
  709. var grants = this.grants;
  710. for ( var i = 0, il = grants.length; i < il; i ++ ) {
  711. this.updateOne( grants[ i ] );
  712. }
  713. return this;
  714. },
  715. /**
  716. * Solve a grant bone
  717. * @param {Object} grant - grant parameter
  718. * @return {GrantSolver}
  719. */
  720. updateOne: function ( grant ) {
  721. var bones = this.mesh.skeleton.bones;
  722. var bone = bones[ grant.index ];
  723. var parentBone = bones[ grant.parentIndex ];
  724. if ( grant.isLocal ) {
  725. // TODO: implement
  726. if ( grant.affectPosition ) {
  727. }
  728. // TODO: implement
  729. if ( grant.affectRotation ) {
  730. }
  731. } else {
  732. // TODO: implement
  733. if ( grant.affectPosition ) {
  734. }
  735. if ( grant.affectRotation ) {
  736. this.addGrantRotation( bone, parentBone.quaternion, grant.ratio );
  737. }
  738. }
  739. return this;
  740. },
  741. addGrantRotation: function () {
  742. var quaternion = new Quaternion();
  743. return function ( bone, q, ratio ) {
  744. quaternion.set( 0, 0, 0, 1 );
  745. quaternion.slerp( q, ratio );
  746. bone.quaternion.multiply( quaternion );
  747. return this;
  748. };
  749. }()
  750. };
  751. return MMDAnimationHelper;
  752. } )();
  753. export { MMDAnimationHelper };