MMDAnimationHelper.js 21 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  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. this.audio.startTime = this.currentTime - this.delayTime;
  579. return true;
  580. },
  581. _shouldStopAudio: function () {
  582. return this.audio.isPlaying &&
  583. this.currentTime >= this.duration;
  584. }
  585. };
  586. /**
  587. * @param {THREE.SkinnedMesh} mesh
  588. * @param {Array<Object>} grants
  589. */
  590. function GrantSolver( mesh, grants ) {
  591. this.mesh = mesh;
  592. this.grants = grants || [];
  593. }
  594. GrantSolver.prototype = {
  595. constructor: GrantSolver,
  596. /**
  597. * @return {GrantSolver}
  598. */
  599. update: function () {
  600. var quaternion = new Quaternion();
  601. return function () {
  602. var bones = this.mesh.skeleton.bones;
  603. var grants = this.grants;
  604. for ( var i = 0, il = grants.length; i < il; i ++ ) {
  605. var grant = grants[ i ];
  606. var bone = bones[ grant.index ];
  607. var parentBone = bones[ grant.parentIndex ];
  608. if ( grant.isLocal ) {
  609. // TODO: implement
  610. if ( grant.affectPosition ) {
  611. }
  612. // TODO: implement
  613. if ( grant.affectRotation ) {
  614. }
  615. } else {
  616. // TODO: implement
  617. if ( grant.affectPosition ) {
  618. }
  619. if ( grant.affectRotation ) {
  620. quaternion.set( 0, 0, 0, 1 );
  621. quaternion.slerp( parentBone.quaternion, grant.ratio );
  622. bone.quaternion.multiply( quaternion );
  623. }
  624. }
  625. }
  626. return this;
  627. };
  628. }()
  629. };
  630. return MMDAnimationHelper;
  631. } )();
  632. export { MMDAnimationHelper };