MMDAnimationHelper.js 21 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  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} enabled
  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; // eslint-disable-line no-undef
  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.userData.MMD.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. // 'duration' can be bigger than 'audioDuration + delayTime' because of sync configuration
  569. if ( ( this.currentTime - this.delayTime ) > this.audioDuration ) return false;
  570. return true;
  571. },
  572. _shouldStopAudio: function () {
  573. return this.audio.isPlaying &&
  574. this.currentTime >= this.duration;
  575. }
  576. };
  577. /**
  578. * @param {THREE.SkinnedMesh} mesh
  579. * @param {Array<Object>} grants
  580. */
  581. function GrantSolver( mesh, grants ) {
  582. this.mesh = mesh;
  583. this.grants = grants || [];
  584. }
  585. GrantSolver.prototype = {
  586. constructor: GrantSolver,
  587. /**
  588. * @return {GrantSolver}
  589. */
  590. update: function () {
  591. var quaternion = new THREE.Quaternion();
  592. return function () {
  593. var bones = this.mesh.skeleton.bones;
  594. var grants = this.grants;
  595. for ( var i = 0, il = grants.length; i < il; i ++ ) {
  596. var grant = grants[ i ];
  597. var bone = bones[ grant.index ];
  598. var parentBone = bones[ grant.parentIndex ];
  599. if ( grant.isLocal ) {
  600. // TODO: implement
  601. if ( grant.affectPosition ) {
  602. }
  603. // TODO: implement
  604. if ( grant.affectRotation ) {
  605. }
  606. } else {
  607. // TODO: implement
  608. if ( grant.affectPosition ) {
  609. }
  610. if ( grant.affectRotation ) {
  611. quaternion.set( 0, 0, 0, 1 );
  612. quaternion.slerp( parentBone.quaternion, grant.ratio );
  613. bone.quaternion.multiply( quaternion );
  614. }
  615. }
  616. }
  617. return this;
  618. };
  619. }()
  620. };
  621. return MMDAnimationHelper;
  622. } )();