MMDAnimationHelper.js 25 KB

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