2
0

MMDAnimationHelper.js 21 KB

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