MMDAnimationHelper.js 21 KB

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