MMDAnimationHelper.js 21 KB

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