MMDAnimationHelper.js 21 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. /**
  2. * MMDAnimationHelper handles animation of MMD assets loaded by MMDLoader
  3. * with MMD special features as IK, Grant, and Physics.
  4. *
  5. * Dependencies
  6. * - ammo.js https://github.com/kripken/ammo.js
  7. * - THREE.MMDPhysics
  8. * - THREE.CCDIKSolver
  9. *
  10. * TODO
  11. * - more precise grant skinning support.
  12. */
  13. THREE.MMDAnimationHelper = ( function () {
  14. /**
  15. * @param {Object} params - (optional)
  16. * @param {boolean} params.sync - Whether animation durations of added objects are synched. Default is true.
  17. * @param {Number} params.afterglow - Default is 0.0.
  18. * @param {boolean} params.resetPhysicsOnLoop - Default is true.
  19. */
  20. function MMDAnimationHelper( params ) {
  21. params = params || {};
  22. this.meshes = [];
  23. this.camera = null;
  24. this.cameraTarget = new THREE.Object3D();
  25. this.cameraTarget.name = 'target';
  26. this.audio = null;
  27. this.audioManager = null;
  28. this.objects = new WeakMap();
  29. this.configuration = {
  30. sync: params.sync !== undefined
  31. ? params.sync : true,
  32. afterglow: params.afterglow !== undefined
  33. ? params.afterglow : 0.0,
  34. resetPhysicsOnLoop: params.resetPhysicsOnLoop !== undefined
  35. ? params.resetPhysicsOnLoop : true
  36. };
  37. this.enabled = {
  38. animation: true,
  39. ik: true,
  40. grant: true,
  41. physics: true,
  42. cameraAnimation: true
  43. };
  44. this.onBeforePhysics = function ( /* mesh */ ) {};
  45. // experimental
  46. this.sharedPhysics = false;
  47. this.masterPhysics = null;
  48. }
  49. MMDAnimationHelper.prototype = {
  50. constructor: MMDAnimationHelper,
  51. /**
  52. * Adds an Three.js Object to helper and setups animation.
  53. * The anmation durations of added objects are synched
  54. * if this.configuration.sync is true.
  55. *
  56. * @param {THREE.SkinnedMesh|THREE.Camera|THREE.Audio} object
  57. * @param {Object} params - (optional)
  58. * @param {THREE.AnimationClip|Array<THREE.AnimationClip>} params.animation - Only for THREE.SkinnedMesh and THREE.Camera. Default is undefined.
  59. * @param {boolean} params.physics - Only for THREE.SkinnedMesh. Default is true.
  60. * @param {Integer} params.warmup - Only for THREE.SkinnedMesh and physics is true. Default is 60.
  61. * @param {Number} params.unitStep - Only for THREE.SkinnedMesh and physics is true. Default is 1 / 65.
  62. * @param {Integer} params.maxStepNum - Only for THREE.SkinnedMesh and physics is true. Default is 3.
  63. * @param {THREE.Vector3} params.gravity - Only for THREE.SkinnedMesh and physics is true. Default ( 0, - 9.8 * 10, 0 ).
  64. * @param {Number} params.delayTime - Only for THREE.Audio. Default is 0.0.
  65. * @return {THREE.MMDAnimationHelper}
  66. */
  67. add: function ( object, params ) {
  68. params = params || {};
  69. if ( object.isSkinnedMesh ) {
  70. this._addMesh( object, params );
  71. } else if ( object.isCamera ) {
  72. this._setupCamera( object, params );
  73. } else if ( object.type === 'Audio' ) {
  74. this._setupAudio( object, params );
  75. } else {
  76. throw new Error( 'THREE.MMDAnimationHelper.add: '
  77. + 'accepts only '
  78. + 'THREE.SkinnedMesh or '
  79. + 'THREE.Camera or '
  80. + 'THREE.Audio instance.' );
  81. }
  82. if ( this.configuration.sync ) this._syncDuration();
  83. return this;
  84. },
  85. /**
  86. * Removes an Three.js Object from helper.
  87. *
  88. * @param {THREE.SkinnedMesh|THREE.Camera|THREE.Audio} object
  89. * @return {THREE.MMDAnimationHelper}
  90. */
  91. remove: function ( object ) {
  92. if ( object.isSkinnedMesh ) {
  93. this._removeMesh( object );
  94. } else if ( object.isCamera ) {
  95. this._clearCamera( object );
  96. } else if ( object.type === 'Audio' ) {
  97. this._clearAudio( object );
  98. } else {
  99. throw new Error( 'THREE.MMDAnimationHelper.remove: '
  100. + 'accepts only '
  101. + 'THREE.SkinnedMesh or '
  102. + 'THREE.Camera or '
  103. + 'THREE.Audio instance.' );
  104. }
  105. if ( this.configuration.sync ) this._syncDuration();
  106. return this;
  107. },
  108. /**
  109. * Updates the animation.
  110. *
  111. * @param {Number} delta
  112. * @return {THREE.MMDAnimationHelper}
  113. */
  114. update: function ( delta ) {
  115. if ( this.audioManager !== null ) this.audioManager.control( delta );
  116. for ( var i = 0; i < this.meshes.length; i ++ ) {
  117. this._animateMesh( this.meshes[ i ], delta );
  118. }
  119. if ( this.sharedPhysics ) this._updateSharedPhysics( delta );
  120. if ( this.camera !== null ) this._animateCamera( this.camera, delta );
  121. return this;
  122. },
  123. /**
  124. * Changes the pose of SkinnedMesh as VPD specifies.
  125. *
  126. * @param {THREE.SkinnedMesh} mesh
  127. * @param {Object} vpd - VPD content parsed MMDParser
  128. * @param {Object} params - (optional)
  129. * @param {boolean} params.resetPose - Default is true.
  130. * @param {boolean} params.ik - Default is true.
  131. * @param {boolean} params.grant - Default is true.
  132. * @return {THREE.MMDAnimationHelper}
  133. */
  134. pose: function ( mesh, vpd, params ) {
  135. params = params || {};
  136. if ( params.resetPose !== false ) mesh.pose();
  137. var bones = mesh.skeleton.bones;
  138. var boneParams = vpd.bones;
  139. var boneNameDictionary = {};
  140. for ( var i = 0, il = bones.length; i < il; i ++ ) {
  141. boneNameDictionary[ bones[ i ].name ] = i;
  142. }
  143. var vector = new THREE.Vector3();
  144. var quaternion = new THREE.Quaternion();
  145. for ( var i = 0, il = boneParams.length; i < il; i ++ ) {
  146. var boneParam = boneParams[ i ];
  147. var boneIndex = boneNameDictionary[ boneParam.name ];
  148. if ( boneIndex === undefined ) continue;
  149. var bone = bones[ boneIndex ];
  150. bone.position.add( vector.fromArray( boneParam.translation ) );
  151. bone.quaternion.multiply( quaternion.fromArray( boneParam.quaternion ) );
  152. }
  153. mesh.updateMatrixWorld( true );
  154. if ( params.ik !== false ) {
  155. this._createCCDIKSolver( mesh ).update( params.saveOriginalBonesBeforeIK ); // this param is experimental
  156. }
  157. if ( params.grant !== false ) {
  158. this.createGrantSolver( mesh ).update();
  159. }
  160. return this;
  161. },
  162. /**
  163. * Enabes/Disables an animation feature.
  164. *
  165. * @param {string} key
  166. * @param {boolean} enabled
  167. * @return {THREE.MMDAnimationHelper}
  168. */
  169. enable: function ( key, enabled ) {
  170. if ( this.enabled[ key ] === undefined ) {
  171. throw new Error( 'THREE.MMDAnimationHelper.enable: '
  172. + 'unknown key ' + key );
  173. }
  174. this.enabled[ key ] = enabled;
  175. if ( key === 'physics' ) {
  176. for ( var i = 0, il = this.meshes.length; i < il; i ++ ) {
  177. this._optimizeIK( this.meshes[ i ], enabled );
  178. }
  179. }
  180. return this;
  181. },
  182. /**
  183. * Creates an GrantSolver instance.
  184. *
  185. * @param {THREE.SkinnedMesh} mesh
  186. * @return {GrantSolver}
  187. */
  188. createGrantSolver: function ( mesh ) {
  189. return new GrantSolver( mesh, mesh.geometry.userData.MMD.grants );
  190. },
  191. // private methods
  192. _addMesh: function ( mesh, params ) {
  193. if ( this.meshes.indexOf( mesh ) >= 0 ) {
  194. throw new Error( 'THREE.MMDAnimationHelper._addMesh: '
  195. + 'SkinnedMesh \'' + mesh.name + '\' has already been added.' );
  196. }
  197. this.meshes.push( mesh );
  198. this.objects.set( mesh, { looped: false } );
  199. this._setupMeshAnimation( mesh, params.animation );
  200. if ( params.physics !== false ) {
  201. this._setupMeshPhysics( mesh, params );
  202. }
  203. return this;
  204. },
  205. _setupCamera: function ( camera, params ) {
  206. if ( this.camera === camera ) {
  207. throw new Error( 'THREE.MMDAnimationHelper._setupCamera: '
  208. + 'Camera \'' + camera.name + '\' has already been set.' );
  209. }
  210. if ( this.camera ) this.clearCamera( this.camera );
  211. this.camera = camera;
  212. camera.add( this.cameraTarget );
  213. this.objects.set( camera, {} );
  214. if ( params.animation !== undefined ) {
  215. this._setupCameraAnimation( camera, params.animation );
  216. }
  217. return this;
  218. },
  219. _setupAudio: function ( audio, params ) {
  220. if ( this.audio === audio ) {
  221. throw new Error( 'THREE.MMDAnimationHelper._setupAudio: '
  222. + 'Audio \'' + audio.name + '\' has already been set.' );
  223. }
  224. if ( this.audio ) this.clearAudio( this.audio );
  225. this.audio = audio;
  226. this.audioManager = new AudioManager( audio, params );
  227. this.objects.set( this.audioManager, {
  228. duration: this.audioManager.duration
  229. } );
  230. return this;
  231. },
  232. _removeMesh: function ( mesh ) {
  233. var found = false;
  234. var writeIndex = 0;
  235. for ( var i = 0, il = this.meshes.length; i < il; i ++ ) {
  236. if ( this.meshes[ i ] === mesh ) {
  237. this.objects.delete( mesh );
  238. found = true;
  239. continue;
  240. }
  241. this.meshes[ writeIndex ++ ] = this.meshes[ i ];
  242. }
  243. if ( ! found ) {
  244. throw new Error( 'THREE.MMDAnimationHelper._removeMesh: '
  245. + 'SkinnedMesh \'' + mesh.name + '\' has not been added yet.' );
  246. }
  247. this.meshes.length = writeIndex;
  248. return this;
  249. },
  250. _clearCamera: function ( camera ) {
  251. if ( camera !== this.camera ) {
  252. throw new Error( 'THREE.MMDAnimationHelper._clearCamera: '
  253. + 'Camera \'' + camera.name + '\' has not been set yet.' );
  254. }
  255. this.camera.remove( this.cameraTarget );
  256. this.objects.delete( this.camera );
  257. this.camera = null;
  258. return this;
  259. },
  260. _clearAudio: function ( audio ) {
  261. if ( audio !== this.audio ) {
  262. throw new Error( 'THREE.MMDAnimationHelper._clearAudio: '
  263. + 'Audio \'' + audio.name + '\' has not been set yet.' );
  264. }
  265. this.objects.delete( this.audioManager );
  266. this.audio = null;
  267. this.audioManager = null;
  268. return this;
  269. },
  270. _setupMeshAnimation: function ( mesh, animation ) {
  271. var objects = this.objects.get( mesh );
  272. if ( animation !== undefined ) {
  273. var animations = Array.isArray( animation )
  274. ? animation : [ animation ];
  275. objects.mixer = new THREE.AnimationMixer( mesh );
  276. for ( var i = 0, il = animations.length; i < il; i ++ ) {
  277. objects.mixer.clipAction( animations[ i ] ).play();
  278. }
  279. // TODO: find a workaround not to access ._clip looking like a private property
  280. objects.mixer.addEventListener( 'loop', function ( event ) {
  281. var tracks = event.action._clip.tracks;
  282. if ( tracks.length > 0 &&
  283. tracks[ 0 ].name.slice( 0, 6 ) !== '.bones' ) return;
  284. objects.looped = true;
  285. } );
  286. }
  287. objects.ikSolver = this._createCCDIKSolver( mesh );
  288. objects.grantSolver = this.createGrantSolver( mesh );
  289. return this;
  290. },
  291. _setupCameraAnimation: function ( camera, animation ) {
  292. var animations = Array.isArray( animation )
  293. ? animation : [ animation ];
  294. var objects = this.objects.get( camera );
  295. objects.mixer = new THREE.AnimationMixer( camera );
  296. for ( var i = 0, il = animations.length; i < il; i ++ ) {
  297. objects.mixer.clipAction( animations[ i ] ).play();
  298. }
  299. },
  300. _setupMeshPhysics: function ( mesh, params ) {
  301. var objects = this.objects.get( mesh );
  302. // shared physics is experimental
  303. if ( params.world === undefined && this.sharedPhysics ) {
  304. var masterPhysics = this._getMasterPhysics();
  305. if ( masterPhysics !== null ) world = masterPhysics.world; // eslint-disable-line no-undef
  306. }
  307. objects.physics = this._createMMDPhysics( mesh, params );
  308. if ( objects.mixer && params.animationWarmup !== false ) {
  309. this._animateMesh( mesh, 0 );
  310. objects.physics.reset();
  311. }
  312. objects.physics.warmup( params.warmup !== undefined ? params.warmup : 60 );
  313. this._optimizeIK( mesh, true );
  314. },
  315. _animateMesh: function ( mesh, delta ) {
  316. var objects = this.objects.get( mesh );
  317. var mixer = objects.mixer;
  318. var ikSolver = objects.ikSolver;
  319. var grantSolver = objects.grantSolver;
  320. var physics = objects.physics;
  321. var looped = objects.looped;
  322. // alternate solution to save/restore bones but less performant?
  323. //mesh.pose();
  324. //this._updatePropertyMixersBuffer( mesh );
  325. if ( mixer && this.enabled.animation ) {
  326. this._restoreBones( mesh );
  327. mixer.update( delta );
  328. this._saveBones( mesh );
  329. if ( ikSolver && this.enabled.ik ) {
  330. mesh.updateMatrixWorld( true );
  331. ikSolver.update();
  332. }
  333. if ( grantSolver && this.enabled.grant ) {
  334. grantSolver.update();
  335. }
  336. }
  337. if ( looped === true && this.enabled.physics ) {
  338. if ( physics && this.configuration.resetPhysicsOnLoop ) physics.reset();
  339. objects.looped = false;
  340. }
  341. if ( physics && this.enabled.physics && ! this.sharedPhysics ) {
  342. this.onBeforePhysics( mesh );
  343. physics.update( delta );
  344. }
  345. },
  346. _animateCamera: function ( camera, delta ) {
  347. var mixer = this.objects.get( camera ).mixer;
  348. if ( mixer && this.enabled.cameraAnimation ) {
  349. mixer.update( delta );
  350. camera.updateProjectionMatrix();
  351. camera.up.set( 0, 1, 0 );
  352. camera.up.applyQuaternion( camera.quaternion );
  353. camera.lookAt( this.cameraTarget.position );
  354. }
  355. },
  356. _optimizeIK: function ( mesh, physicsEnabled ) {
  357. var iks = mesh.geometry.userData.MMD.iks;
  358. var bones = mesh.geometry.userData.MMD.bones;
  359. for ( var i = 0, il = iks.length; i < il; i ++ ) {
  360. var ik = iks[ i ];
  361. var links = ik.links;
  362. for ( var j = 0, jl = links.length; j < jl; j ++ ) {
  363. var link = links[ j ];
  364. if ( physicsEnabled === true ) {
  365. // disable IK of the bone the corresponding rigidBody type of which is 1 or 2
  366. // because its rotation will be overriden by physics
  367. link.enabled = bones[ link.index ].rigidBodyType > 0 ? false : true;
  368. } else {
  369. link.enabled = true;
  370. }
  371. }
  372. }
  373. },
  374. _createCCDIKSolver: function ( mesh ) {
  375. if ( THREE.CCDIKSolver === undefined ) {
  376. throw new Error( 'THREE.MMDAnimationHelper: Import THREE.CCDIKSolver.' );
  377. }
  378. return new THREE.CCDIKSolver( mesh, mesh.geometry.userData.MMD.iks );
  379. },
  380. _createMMDPhysics: function ( mesh, params ) {
  381. if ( THREE.MMDPhysics === undefined ) {
  382. throw new Error( 'THREE.MMDPhysics: Import THREE.MMDPhysics.' );
  383. }
  384. return new THREE.MMDPhysics(
  385. mesh,
  386. mesh.geometry.userData.MMD.rigidBodies,
  387. mesh.geometry.userData.MMD.constraints,
  388. params );
  389. },
  390. /*
  391. * Detects the longest duration and then sets it to them to sync.
  392. * TODO: Not to access private properties ( ._actions and ._clip )
  393. */
  394. _syncDuration: function () {
  395. var max = 0.0;
  396. var objects = this.objects;
  397. var meshes = this.meshes;
  398. var camera = this.camera;
  399. var audioManager = this.audioManager;
  400. // get the longest duration
  401. for ( var i = 0, il = meshes.length; i < il; i ++ ) {
  402. var mixer = this.objects.get( meshes[ i ] ).mixer;
  403. if ( mixer === undefined ) continue;
  404. for ( var j = 0; j < mixer._actions.length; j ++ ) {
  405. var clip = mixer._actions[ j ]._clip;
  406. if ( ! objects.has( clip ) ) {
  407. objects.set( clip, {
  408. duration: clip.duration
  409. } );
  410. }
  411. max = Math.max( max, objects.get( clip ).duration );
  412. }
  413. }
  414. if ( camera !== null ) {
  415. var mixer = this.objects.get( camera ).mixer;
  416. if ( mixer !== undefined ) {
  417. for ( var i = 0, il = mixer._actions.length; i < il; i ++ ) {
  418. var clip = mixer._actions[ i ]._clip;
  419. if ( ! objects.has( clip ) ) {
  420. objects.set( clip, {
  421. duration: clip.duration
  422. } );
  423. }
  424. max = Math.max( max, objects.get( clip ).duration );
  425. }
  426. }
  427. }
  428. if ( audioManager !== null ) {
  429. max = Math.max( max, objects.get( audioManager ).duration );
  430. }
  431. max += this.configuration.afterglow;
  432. // update the duration
  433. for ( var i = 0, il = this.meshes.length; i < il; i ++ ) {
  434. var mixer = this.objects.get( this.meshes[ i ] ).mixer;
  435. if ( mixer === undefined ) continue;
  436. for ( var j = 0, jl = mixer._actions.length; j < jl; j ++ ) {
  437. mixer._actions[ j ]._clip.duration = max;
  438. }
  439. }
  440. if ( camera !== null ) {
  441. var mixer = this.objects.get( camera ).mixer;
  442. if ( mixer !== undefined ) {
  443. for ( var i = 0, il = mixer._actions.length; i < il; i ++ ) {
  444. mixer._actions[ i ]._clip.duration = max;
  445. }
  446. }
  447. }
  448. if ( audioManager !== null ) {
  449. audioManager.duration = max;
  450. }
  451. },
  452. // workaround
  453. _updatePropertyMixersBuffer: function ( mesh ) {
  454. var mixer = this.objects.get( mesh ).mixer;
  455. var propertyMixers = mixer._bindings;
  456. var accuIndex = mixer._accuIndex;
  457. for ( var i = 0, il = propertyMixers.length; i < il; i ++ ) {
  458. var propertyMixer = propertyMixers[ i ];
  459. var buffer = propertyMixer.buffer;
  460. var stride = propertyMixer.valueSize;
  461. var offset = ( accuIndex + 1 ) * stride;
  462. propertyMixer.binding.getValue( buffer, offset );
  463. }
  464. },
  465. /*
  466. * Avoiding these two issues by restore/save bones before/after mixer animation.
  467. *
  468. * 1. PropertyMixer used by AnimationMixer holds cache value in .buffer.
  469. * Calculating IK, Grant, and Physics after mixer animation can break
  470. * the cache coherency.
  471. *
  472. * 2. Applying Grant two or more times without reset the posing breaks model.
  473. */
  474. _saveBones: function ( mesh ) {
  475. var objects = this.objects.get( mesh );
  476. var bones = mesh.skeleton.bones;
  477. var backupBones = objects.backupBones;
  478. if ( backupBones === undefined ) {
  479. backupBones = new Float32Array( bones.length * 7 );
  480. objects.backupBones = backupBones;
  481. }
  482. for ( var i = 0, il = bones.length; i < il; i ++ ) {
  483. var bone = bones[ i ];
  484. bone.position.toArray( backupBones, i * 7 );
  485. bone.quaternion.toArray( backupBones, i * 7 + 3 );
  486. }
  487. },
  488. _restoreBones: function ( mesh ) {
  489. var objects = this.objects.get( mesh );
  490. var backupBones = objects.backupBones;
  491. if ( backupBones === undefined ) return;
  492. var bones = mesh.skeleton.bones;
  493. for ( var i = 0, il = bones.length; i < il; i ++ ) {
  494. var bone = bones[ i ];
  495. bone.position.fromArray( backupBones, i * 7 );
  496. bone.quaternion.fromArray( backupBones, i * 7 + 3 );
  497. }
  498. },
  499. // experimental
  500. _getMasterPhysics: function () {
  501. if ( this.masterPhysics !== null ) return this.masterPhysics;
  502. for ( var i = 0, il = this.meshes.length; i < il; i ++ ) {
  503. var physics = this.meshes[ i ].physics;
  504. if ( physics !== undefined && physics !== null ) {
  505. this.masterPhysics = physics;
  506. return this.masterPhysics;
  507. }
  508. }
  509. return null;
  510. },
  511. _updateSharedPhysics: function ( delta ) {
  512. if ( this.meshes.length === 0 || ! this.enabled.physics || ! this.sharedPhysics ) return;
  513. var physics = this._getMasterPhysics();
  514. if ( physics === null ) return;
  515. for ( var i = 0, il = this.meshes.length; i < il; i ++ ) {
  516. var p = this.meshes[ i ].physics;
  517. if ( p !== null && p !== undefined ) {
  518. p.updateRigidBodies();
  519. }
  520. }
  521. physics.stepSimulation( delta );
  522. for ( var i = 0, il = this.meshes.length; i < il; i ++ ) {
  523. var p = this.meshes[ i ].physics;
  524. if ( p !== null && p !== undefined ) {
  525. p.updateBones();
  526. }
  527. }
  528. }
  529. };
  530. //
  531. /**
  532. * @param {THREE.Audio} audio
  533. * @param {Object} params - (optional)
  534. * @param {Nuumber} params.delayTime
  535. */
  536. function AudioManager( audio, params ) {
  537. params = params || {};
  538. this.audio = audio;
  539. this.elapsedTime = 0.0;
  540. this.currentTime = 0.0;
  541. this.delayTime = params.delayTime !== undefined
  542. ? params.delayTime : 0.0;
  543. this.audioDuration = this.audio.buffer.duration;
  544. this.duration = this.audioDuration + this.delayTime;
  545. }
  546. AudioManager.prototype = {
  547. constructor: AudioManager,
  548. /**
  549. * @param {Number} delta
  550. * @return {AudioManager}
  551. */
  552. control: function ( delta ) {
  553. this.elapsed += delta;
  554. this.currentTime += delta;
  555. if ( this._shouldStopAudio() ) this.audio.stop();
  556. if ( this._shouldStartAudio() ) this.audio.play();
  557. return this;
  558. },
  559. // private methods
  560. _shouldStartAudio: function () {
  561. if ( this.audio.isPlaying ) return false;
  562. while ( this.currentTime >= this.duration ) {
  563. this.currentTime -= this.duration;
  564. }
  565. if ( this.currentTime < this.delayTime ) return false;
  566. // 'duration' can be bigger than 'audioDuration + delayTime' because of sync configuration
  567. if ( ( this.currentTime - this.delayTime ) > this.audioDuration ) return false;
  568. return true;
  569. },
  570. _shouldStopAudio: function () {
  571. return this.audio.isPlaying &&
  572. this.currentTime >= this.duration;
  573. }
  574. };
  575. /**
  576. * @param {THREE.SkinnedMesh} mesh
  577. * @param {Array<Object>} grants
  578. */
  579. function GrantSolver( mesh, grants ) {
  580. this.mesh = mesh;
  581. this.grants = grants || [];
  582. }
  583. GrantSolver.prototype = {
  584. constructor: GrantSolver,
  585. /**
  586. * @return {GrantSolver}
  587. */
  588. update: function () {
  589. var quaternion = new THREE.Quaternion();
  590. return function () {
  591. var bones = this.mesh.skeleton.bones;
  592. var grants = this.grants;
  593. for ( var i = 0, il = grants.length; i < il; i ++ ) {
  594. var grant = grants[ i ];
  595. var bone = bones[ grant.index ];
  596. var parentBone = bones[ grant.parentIndex ];
  597. if ( grant.isLocal ) {
  598. // TODO: implement
  599. if ( grant.affectPosition ) {
  600. }
  601. // TODO: implement
  602. if ( grant.affectRotation ) {
  603. }
  604. } else {
  605. // TODO: implement
  606. if ( grant.affectPosition ) {
  607. }
  608. if ( grant.affectRotation ) {
  609. quaternion.set( 0, 0, 0, 1 );
  610. quaternion.slerp( parentBone.quaternion, grant.ratio );
  611. bone.quaternion.multiply( quaternion );
  612. }
  613. }
  614. }
  615. return this;
  616. };
  617. }()
  618. };
  619. return MMDAnimationHelper;
  620. } )();