MMDAnimationHelper.js 21 KB

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