MMDAnimationHelper.js 25 KB

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