MMDAnimationHelper.js 21 KB

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