MMDAnimationHelper.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  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.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. if ( params.animation !== undefined ) {
  202. this._setupMeshAnimation( mesh, params.animation );
  203. }
  204. if ( params.physics !== false ) {
  205. this._setupMeshPhysics( mesh, params );
  206. }
  207. return this;
  208. },
  209. _setupCamera: function ( camera, params ) {
  210. if ( this.camera === camera ) {
  211. throw new Error( 'THREE.MMDAnimationHelper._setupCamera: '
  212. + 'Camera \'' + camera.name + '\' has already been set.' );
  213. }
  214. if ( this.camera ) this.clearCamera( this.camera );
  215. this.camera = camera;
  216. camera.add( this.cameraTarget );
  217. this.objects.set( camera, {} );
  218. if ( params.animation !== undefined ) {
  219. this._setupCameraAnimation( camera, params.animation )
  220. }
  221. return this;
  222. },
  223. _setupAudio: function ( audio, params ) {
  224. if ( this.audio === audio ) {
  225. throw new Error( 'THREE.MMDAnimationHelper._setupAudio: '
  226. + 'Audio \'' + audio.name + '\' has already been set.' );
  227. }
  228. if ( this.audio ) this.clearAudio( this.audio );
  229. this.audio = audio;
  230. this.audioManager = new AudioManager( audio, params );
  231. this.objects.set( this.audioManager, {
  232. duration: this.audioManager.duration
  233. } );
  234. return this;
  235. },
  236. _removeMesh: function ( mesh ) {
  237. var found = false;
  238. var writeIndex = 0;
  239. for ( var i = 0, il = this.meshes.length; i < il; i ++ ) {
  240. if ( this.meshes[ i ] === mesh ) {
  241. this.objects.delete( mesh );
  242. found = true;
  243. continue;
  244. }
  245. this.meshes[ writeIndex ++ ] = this.meshes[ i ];
  246. }
  247. if ( ! found ) {
  248. throw new Error( 'THREE.MMDAnimationHelper._removeMesh: '
  249. + 'SkinnedMesh \'' + mesh.name + '\' has not been added yet.' );
  250. }
  251. this.meshes.length = writeIndex;
  252. return this;
  253. },
  254. _clearCamera: function ( camera ) {
  255. if ( camera !== this.camera ) {
  256. throw new Error( 'THREE.MMDAnimationHelper._clearCamera: '
  257. + 'Camera \'' + camera.name + '\' has not been set yet.' );
  258. }
  259. this.camera.remove( this.cameraTarget );
  260. this.objects.delete( this.camera );
  261. this.camera = null;
  262. return this;
  263. },
  264. _clearAudio: function ( audio ) {
  265. if ( audio !== this.audio ) {
  266. throw new Error( 'THREE.MMDAnimationHelper._clearAudio: '
  267. + 'Audio \'' + audio.name + '\' has not been set yet.' );
  268. }
  269. this.objects.delete( this.audioManager );
  270. this.audio = null;
  271. this.audioManager = null;
  272. return this;
  273. },
  274. _setupMeshAnimation: function ( mesh, animation ) {
  275. var animations = Array.isArray( animation )
  276. ? animation : [ animation ];
  277. var objects = this.objects.get( mesh );
  278. objects.mixer = new THREE.AnimationMixer( mesh );
  279. for ( var i = 0, il = animations.length; i < il; i ++ ) {
  280. objects.mixer.clipAction( animations[ i ] ).play();
  281. }
  282. // TODO: find a workaround not to access ._clip looking like a private property
  283. objects.mixer.addEventListener( 'loop', function ( event ) {
  284. var tracks = event.action._clip.tracks;
  285. if ( tracks.length > 0 &&
  286. tracks[ 0 ].name.slice( 0, 6 ) !== '.bones' ) return;
  287. objects.looped = true;
  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. if ( mixer && this.enabled.animation ) {
  325. this._restoreBones( mesh );
  326. // another solution but less performant?
  327. //mesh.pose();
  328. //this._updatePropertyMixersBuffer( 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.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.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, mesh.geometry.rigidBodies, mesh.geometry.constraints, params );
  388. },
  389. /*
  390. * Detects the longest duration and then sets it to them to sync.
  391. * TODO: Not to access private properties ( ._actions and ._clip )
  392. */
  393. _syncDuration: function () {
  394. var max = 0.0;
  395. var objects = this.objects;
  396. var meshes = this.meshes;
  397. var camera = this.camera;
  398. var audioManager = this.audioManager;
  399. // get the longest duration
  400. for ( var i = 0, il = meshes.length; i < il; i ++ ) {
  401. var mixer = this.objects.get( meshes[ i ] ).mixer;
  402. if ( mixer === undefined ) continue;
  403. for ( var j = 0; j < mixer._actions.length; j ++ ) {
  404. var clip = mixer._actions[ j ]._clip;
  405. if ( ! objects.has( clip ) ) {
  406. objects.set( clip, {
  407. duration: clip.duration
  408. } )
  409. }
  410. max = Math.max( max, objects.get( clip ).duration );
  411. }
  412. }
  413. if ( camera !== null ) {
  414. var mixer = this.objects.get( camera ).mixer;
  415. if ( mixer !== undefined ) {
  416. for ( var i = 0, il = mixer._actions.length; i < il; i ++ ) {
  417. var clip = mixer._actions[ i ]._clip;
  418. if ( ! objects.has( clip ) ) {
  419. objects.set( clip, {
  420. duration: clip.duration
  421. } )
  422. }
  423. max = Math.max( max, objects.get( clip ).duration );
  424. }
  425. }
  426. }
  427. if ( audioManager !== null ) {
  428. max = Math.max( max, objects.get( audioManager ).duration );
  429. }
  430. max += this.configuration.afterglow;
  431. // update the duration
  432. for ( var i = 0, il = this.meshes.length; i < il; i ++ ) {
  433. var mixer = this.objects.get( this.meshes[ i ] ).mixer;
  434. if ( mixer === undefined ) continue;
  435. for ( var j = 0, jl = mixer._actions.length; j < jl; j ++ ) {
  436. mixer._actions[ j ]._clip.duration = max;
  437. }
  438. }
  439. if ( camera !== null ) {
  440. var mixer = this.objects.get( camera ).mixer;
  441. if ( mixer !== undefined ) {
  442. for ( var i = 0, il = mixer._actions.length; i < il; i ++ ) {
  443. mixer._actions[ i ]._clip.duration = max;
  444. }
  445. }
  446. }
  447. if ( audioManager !== null ) {
  448. audioManager.duration = max;
  449. }
  450. },
  451. // workaround
  452. _updatePropertyMixersBuffer: function ( mesh ) {
  453. var mixer = this.objects.get( mesh ).mixer;
  454. var propertyMixers = mixer._bindings;
  455. var accuIndex = mixer._accuIndex;
  456. for ( var i = 0, il = propertyMixers.length; i < il; i ++ ) {
  457. var propertyMixer = propertyMixers[ i ];
  458. var buffer = propertyMixer.buffer;
  459. var stride = propertyMixer.valueSize;
  460. var offset = ( accuIndex + 1 ) * stride;
  461. propertyMixer.binding.getValue( buffer, offset );
  462. }
  463. },
  464. /*
  465. * Avoiding these two issues by restore/save bones before/after mixer animation.
  466. *
  467. * 1. PropertyMixer used by AnimationMixer holds cache value in .buffer.
  468. * Calculating IK, Grant, and Physics after mixer animation can break
  469. * the cache coherency.
  470. *
  471. * 2. Applying Grant two or more times without reset the posing breaks model.
  472. */
  473. _saveBones: function ( mesh ) {
  474. var objects = this.objects.get( mesh );
  475. var bones = mesh.skeleton.bones;
  476. var backupBones = objects.backupBones;
  477. if ( backupBones === undefined ) {
  478. backupBones = new Float32Array( bones.length * 7 );
  479. objects.backupBones = backupBones;
  480. }
  481. for ( var i = 0, il = bones.length; i < il; i ++ ) {
  482. var bone = bones[ i ];
  483. bone.position.toArray( backupBones, i * 7 );
  484. bone.quaternion.toArray( backupBones, i * 7 + 3 );
  485. }
  486. },
  487. _restoreBones: function ( mesh ) {
  488. var objects = this.objects.get( mesh );
  489. var backupBones = objects.backupBones;
  490. if ( backupBones === undefined ) return;
  491. var bones = mesh.skeleton.bones;
  492. for ( var i = 0, il = bones.length; i < il; i ++ ) {
  493. var bone = bones[ i ];
  494. bone.position.fromArray( backupBones, i * 7 );
  495. bone.quaternion.fromArray( backupBones, i * 7 + 3 );
  496. }
  497. },
  498. // experimental
  499. _getMasterPhysics: function () {
  500. if ( this.masterPhysics !== null ) return this.masterPhysics;
  501. for ( var i = 0, il = this.meshes.length; i < il; i ++ ) {
  502. var physics = this.meshes[ i ].physics;
  503. if ( physics !== undefined && physics !== null ) {
  504. this.masterPhysics = physics;
  505. return this.masterPhysics;
  506. }
  507. }
  508. return null;
  509. },
  510. _updateSharedPhysics: function ( delta ) {
  511. if ( this.meshes.length === 0 || ! this.enabled.physics || ! this.sharedPhysics ) return;
  512. var physics = this._getMasterPhysics();
  513. if ( physics === null ) return;
  514. for ( var i = 0, il = this.meshes.length; i < il; i ++ ) {
  515. var p = this.meshes[ i ].physics;
  516. if ( p !== null && p !== undefined ) {
  517. p.updateRigidBodies();
  518. }
  519. }
  520. physics.stepSimulation( delta );
  521. for ( var i = 0, il = this.meshes.length; i < il; i ++ ) {
  522. var p = this.meshes[ i ].physics;
  523. if ( p !== null && p !== undefined ) {
  524. p.updateBones();
  525. }
  526. }
  527. }
  528. };
  529. //
  530. /**
  531. * @param {THREE.Audio} audio
  532. * @param {Object} params - (optional)
  533. * @param {Nuumber} params.delayTime
  534. */
  535. function AudioManager( audio, params ) {
  536. params = params || {};
  537. this.audio = audio;
  538. this.elapsedTime = 0.0;
  539. this.currentTime = 0.0;
  540. this.delayTime = params.delayTime !== undefined
  541. ? params.delayTime : 0.0;
  542. this.audioDuration = this.audio.buffer.duration;
  543. this.duration = this.audioDuration + this.delayTime;
  544. }
  545. AudioManager.prototype = {
  546. constructor: AudioManager,
  547. /**
  548. * @param {Number} delta
  549. * @return {AudioManager}
  550. */
  551. control: function ( delta ) {
  552. this.elapsed += delta;
  553. this.currentTime += delta;
  554. if ( this._shouldStopAudio() ) this.audio.stop();
  555. if ( this._shouldStartAudio() ) this.audio.play();
  556. return this;
  557. },
  558. // private methods
  559. _shouldStartAudio: function () {
  560. if ( this.audio.isPlaying ) return false;
  561. while ( this.currentTime >= this.duration ) {
  562. this.currentTime -= this.duration;
  563. }
  564. if ( this.currentTime < this.delayTime ) return false;
  565. this.audio.startTime = this.currentTime - this.delayTime;
  566. return true;
  567. },
  568. _shouldStopAudio: function () {
  569. return this.audio.isPlaying &&
  570. this.currentTime >= this.duration;
  571. }
  572. };
  573. /**
  574. * @param {THREE.SkinnedMesh} mesh
  575. * @param {Array<Object>} grants
  576. */
  577. function GrantSolver( mesh, grants ) {
  578. this.mesh = mesh;
  579. this.grants = grants || [];
  580. }
  581. GrantSolver.prototype = {
  582. constructor: GrantSolver,
  583. /**
  584. * @return {GrantSolver}
  585. */
  586. update: function () {
  587. var quaternion = new THREE.Quaternion();
  588. return function () {
  589. var bones = this.mesh.skeleton.bones;
  590. var grants = this.grants;
  591. for ( var i = 0, il = grants.length; i < il; i ++ ) {
  592. var grant = grants[ i ];
  593. var bone = bones[ grant.index ];
  594. var parentBone = bones[ grant.parentIndex ];
  595. if ( grant.isLocal ) {
  596. // TODO: implement
  597. if ( grant.affectPosition ) {
  598. }
  599. // TODO: implement
  600. if ( grant.affectRotation ) {
  601. }
  602. } else {
  603. // TODO: implement
  604. if ( grant.affectPosition ) {
  605. }
  606. if ( grant.affectRotation ) {
  607. quaternion.set( 0, 0, 0, 1 );
  608. quaternion.slerp( parentBone.quaternion, grant.ratio );
  609. bone.quaternion.multiply( quaternion );
  610. }
  611. }
  612. }
  613. return this;
  614. };
  615. }()
  616. };
  617. return MMDAnimationHelper;
  618. } )();