MMDAnimationHelper.js 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  1. import {
  2. AnimationMixer,
  3. Object3D,
  4. Quaternion,
  5. Vector3
  6. } from '../../../build/three.module.js';
  7. import { CCDIKSolver } from '../animation/CCDIKSolver.js';
  8. import { MMDPhysics } from '../animation/MMDPhysics.js';
  9. /**
  10. * MMDAnimationHelper handles animation of MMD assets loaded by MMDLoader
  11. * with MMD special features as IK, Grant, and Physics.
  12. *
  13. * Dependencies
  14. * - ammo.js https://github.com/kripken/ammo.js
  15. * - MMDPhysics
  16. * - CCDIKSolver
  17. *
  18. * TODO
  19. * - more precise grant skinning support.
  20. */
  21. class MMDAnimationHelper {
  22. /**
  23. * @param {Object} params - (optional)
  24. * @param {boolean} params.sync - Whether animation durations of added objects are synched. Default is true.
  25. * @param {Number} params.afterglow - Default is 0.0.
  26. * @param {boolean} params.resetPhysicsOnLoop - Default is true.
  27. */
  28. constructor( params = {} ) {
  29. this.meshes = [];
  30. this.camera = null;
  31. this.cameraTarget = new Object3D();
  32. this.cameraTarget.name = 'target';
  33. this.audio = null;
  34. this.audioManager = null;
  35. this.objects = new WeakMap();
  36. this.configuration = {
  37. sync: params.sync !== undefined ? params.sync : true,
  38. afterglow: params.afterglow !== undefined ? params.afterglow : 0.0,
  39. resetPhysicsOnLoop: params.resetPhysicsOnLoop !== undefined ? params.resetPhysicsOnLoop : true,
  40. pmxAnimation: params.pmxAnimation !== undefined ? params.pmxAnimation : false
  41. };
  42. this.enabled = {
  43. animation: true,
  44. ik: true,
  45. grant: true,
  46. physics: true,
  47. cameraAnimation: true
  48. };
  49. this.onBeforePhysics = function ( /* mesh */ ) {};
  50. // experimental
  51. this.sharedPhysics = false;
  52. this.masterPhysics = null;
  53. }
  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 {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 {MMDAnimationHelper}
  69. */
  70. add( object, params = {} ) {
  71. if ( object.isSkinnedMesh ) {
  72. this._addMesh( object, params );
  73. } else if ( object.isCamera ) {
  74. this._setupCamera( object, params );
  75. } else if ( object.type === 'Audio' ) {
  76. this._setupAudio( object, params );
  77. } else {
  78. throw new Error( 'THREE.MMDAnimationHelper.add: '
  79. + 'accepts only '
  80. + 'THREE.SkinnedMesh or '
  81. + 'THREE.Camera or '
  82. + 'THREE.Audio instance.' );
  83. }
  84. if ( this.configuration.sync ) this._syncDuration();
  85. return this;
  86. }
  87. /**
  88. * Removes an Three.js Object from helper.
  89. *
  90. * @param {THREE.SkinnedMesh|THREE.Camera|THREE.Audio} object
  91. * @return {MMDAnimationHelper}
  92. */
  93. remove( object ) {
  94. if ( object.isSkinnedMesh ) {
  95. this._removeMesh( object );
  96. } else if ( object.isCamera ) {
  97. this._clearCamera( object );
  98. } else if ( object.type === 'Audio' ) {
  99. this._clearAudio( object );
  100. } else {
  101. throw new Error( 'THREE.MMDAnimationHelper.remove: '
  102. + 'accepts only '
  103. + 'THREE.SkinnedMesh or '
  104. + 'THREE.Camera or '
  105. + 'THREE.Audio instance.' );
  106. }
  107. if ( this.configuration.sync ) this._syncDuration();
  108. return this;
  109. }
  110. /**
  111. * Updates the animation.
  112. *
  113. * @param {Number} delta
  114. * @return {MMDAnimationHelper}
  115. */
  116. update( delta ) {
  117. if ( this.audioManager !== null ) this.audioManager.control( delta );
  118. for ( let i = 0; i < this.meshes.length; i ++ ) {
  119. this._animateMesh( this.meshes[ i ], delta );
  120. }
  121. if ( this.sharedPhysics ) this._updateSharedPhysics( delta );
  122. if ( this.camera !== null ) this._animateCamera( this.camera, delta );
  123. return this;
  124. }
  125. /**
  126. * Changes the pose of SkinnedMesh as VPD specifies.
  127. *
  128. * @param {THREE.SkinnedMesh} mesh
  129. * @param {Object} vpd - VPD content parsed MMDParser
  130. * @param {Object} params - (optional)
  131. * @param {boolean} params.resetPose - Default is true.
  132. * @param {boolean} params.ik - Default is true.
  133. * @param {boolean} params.grant - Default is true.
  134. * @return {MMDAnimationHelper}
  135. */
  136. pose( mesh, vpd, params = {} ) {
  137. if ( params.resetPose !== false ) mesh.pose();
  138. const bones = mesh.skeleton.bones;
  139. const boneParams = vpd.bones;
  140. const boneNameDictionary = {};
  141. for ( let i = 0, il = bones.length; i < il; i ++ ) {
  142. boneNameDictionary[ bones[ i ].name ] = i;
  143. }
  144. const vector = new Vector3();
  145. const quaternion = new Quaternion();
  146. for ( let i = 0, il = boneParams.length; i < il; i ++ ) {
  147. const boneParam = boneParams[ i ];
  148. const boneIndex = boneNameDictionary[ boneParam.name ];
  149. if ( boneIndex === undefined ) continue;
  150. const bone = bones[ boneIndex ];
  151. bone.position.add( vector.fromArray( boneParam.translation ) );
  152. bone.quaternion.multiply( quaternion.fromArray( boneParam.quaternion ) );
  153. }
  154. mesh.updateMatrixWorld( true );
  155. // PMX animation system special path
  156. if ( this.configuration.pmxAnimation &&
  157. mesh.geometry.userData.MMD && mesh.geometry.userData.MMD.format === 'pmx' ) {
  158. const sortedBonesData = this._sortBoneDataArray( mesh.geometry.userData.MMD.bones.slice() );
  159. const ikSolver = params.ik !== false ? this._createCCDIKSolver( mesh ) : null;
  160. const grantSolver = params.grant !== false ? this.createGrantSolver( mesh ) : null;
  161. this._animatePMXMesh( mesh, sortedBonesData, ikSolver, grantSolver );
  162. } else {
  163. if ( params.ik !== false ) {
  164. this._createCCDIKSolver( mesh ).update();
  165. }
  166. if ( params.grant !== false ) {
  167. this.createGrantSolver( mesh ).update();
  168. }
  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( 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 ( let 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( mesh ) {
  199. return new GrantSolver( mesh, mesh.geometry.userData.MMD.grants );
  200. }
  201. // private methods
  202. _addMesh( 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( 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( 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( mesh ) {
  243. let found = false;
  244. let writeIndex = 0;
  245. for ( let 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( 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( 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( mesh, animation ) {
  281. const objects = this.objects.get( mesh );
  282. if ( animation !== undefined ) {
  283. const animations = Array.isArray( animation )
  284. ? animation : [ animation ];
  285. objects.mixer = new AnimationMixer( mesh );
  286. for ( let 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. const tracks = event.action._clip.tracks;
  292. if ( tracks.length > 0 && tracks[ 0 ].name.slice( 0, 6 ) !== '.bones' ) return;
  293. objects.looped = true;
  294. } );
  295. }
  296. objects.ikSolver = this._createCCDIKSolver( mesh );
  297. objects.grantSolver = this.createGrantSolver( mesh );
  298. return this;
  299. }
  300. _setupCameraAnimation( camera, animation ) {
  301. const animations = Array.isArray( animation )
  302. ? animation : [ animation ];
  303. const objects = this.objects.get( camera );
  304. objects.mixer = new AnimationMixer( camera );
  305. for ( let i = 0, il = animations.length; i < il; i ++ ) {
  306. objects.mixer.clipAction( animations[ i ] ).play();
  307. }
  308. }
  309. _setupMeshPhysics( mesh, params ) {
  310. const objects = this.objects.get( mesh );
  311. // shared physics is experimental
  312. if ( params.world === undefined && this.sharedPhysics ) {
  313. const masterPhysics = this._getMasterPhysics();
  314. if ( masterPhysics !== null ) world = masterPhysics.world; // eslint-disable-line no-undef
  315. }
  316. objects.physics = this._createMMDPhysics( mesh, params );
  317. if ( objects.mixer && params.animationWarmup !== false ) {
  318. this._animateMesh( mesh, 0 );
  319. objects.physics.reset();
  320. }
  321. objects.physics.warmup( params.warmup !== undefined ? params.warmup : 60 );
  322. this._optimizeIK( mesh, true );
  323. }
  324. _animateMesh( mesh, delta ) {
  325. const objects = this.objects.get( mesh );
  326. const mixer = objects.mixer;
  327. const ikSolver = objects.ikSolver;
  328. const grantSolver = objects.grantSolver;
  329. const physics = objects.physics;
  330. const looped = objects.looped;
  331. if ( mixer && this.enabled.animation ) {
  332. // alternate solution to save/restore bones but less performant?
  333. //mesh.pose();
  334. //this._updatePropertyMixersBuffer( mesh );
  335. this._restoreBones( mesh );
  336. mixer.update( delta );
  337. this._saveBones( mesh );
  338. // PMX animation system special path
  339. if ( this.configuration.pmxAnimation &&
  340. mesh.geometry.userData.MMD && mesh.geometry.userData.MMD.format === 'pmx' ) {
  341. if ( ! objects.sortedBonesData ) objects.sortedBonesData = this._sortBoneDataArray( mesh.geometry.userData.MMD.bones.slice() );
  342. this._animatePMXMesh(
  343. mesh,
  344. objects.sortedBonesData,
  345. ikSolver && this.enabled.ik ? ikSolver : null,
  346. grantSolver && this.enabled.grant ? grantSolver : null
  347. );
  348. } else {
  349. if ( ikSolver && this.enabled.ik ) {
  350. mesh.updateMatrixWorld( true );
  351. ikSolver.update();
  352. }
  353. if ( grantSolver && this.enabled.grant ) {
  354. grantSolver.update();
  355. }
  356. }
  357. }
  358. if ( looped === true && this.enabled.physics ) {
  359. if ( physics && this.configuration.resetPhysicsOnLoop ) physics.reset();
  360. objects.looped = false;
  361. }
  362. if ( physics && this.enabled.physics && ! this.sharedPhysics ) {
  363. this.onBeforePhysics( mesh );
  364. physics.update( delta );
  365. }
  366. }
  367. // Sort bones in order by 1. transformationClass and 2. bone index.
  368. // In PMX animation system, bone transformations should be processed
  369. // in this order.
  370. _sortBoneDataArray( boneDataArray ) {
  371. return boneDataArray.sort( function ( a, b ) {
  372. if ( a.transformationClass !== b.transformationClass ) {
  373. return a.transformationClass - b.transformationClass;
  374. } else {
  375. return a.index - b.index;
  376. }
  377. } );
  378. }
  379. // PMX Animation system is a bit too complex and doesn't great match to
  380. // Three.js Animation system. This method attempts to simulate it as much as
  381. // possible but doesn't perfectly simulate.
  382. // This method is more costly than the regular one so
  383. // you are recommended to set constructor parameter "pmxAnimation: true"
  384. // only if your PMX model animation doesn't work well.
  385. // If you need better method you would be required to write your own.
  386. _animatePMXMesh( mesh, sortedBonesData, ikSolver, grantSolver ) {
  387. _quaternionIndex = 0;
  388. _grantResultMap.clear();
  389. for ( let i = 0, il = sortedBonesData.length; i < il; i ++ ) {
  390. updateOne( mesh, sortedBonesData[ i ].index, ikSolver, grantSolver );
  391. }
  392. mesh.updateMatrixWorld( true );
  393. return this;
  394. }
  395. _animateCamera( camera, delta ) {
  396. const mixer = this.objects.get( camera ).mixer;
  397. if ( mixer && this.enabled.cameraAnimation ) {
  398. mixer.update( delta );
  399. camera.updateProjectionMatrix();
  400. camera.up.set( 0, 1, 0 );
  401. camera.up.applyQuaternion( camera.quaternion );
  402. camera.lookAt( this.cameraTarget.position );
  403. }
  404. }
  405. _optimizeIK( mesh, physicsEnabled ) {
  406. const iks = mesh.geometry.userData.MMD.iks;
  407. const bones = mesh.geometry.userData.MMD.bones;
  408. for ( let i = 0, il = iks.length; i < il; i ++ ) {
  409. const ik = iks[ i ];
  410. const links = ik.links;
  411. for ( let j = 0, jl = links.length; j < jl; j ++ ) {
  412. const link = links[ j ];
  413. if ( physicsEnabled === true ) {
  414. // disable IK of the bone the corresponding rigidBody type of which is 1 or 2
  415. // because its rotation will be overriden by physics
  416. link.enabled = bones[ link.index ].rigidBodyType > 0 ? false : true;
  417. } else {
  418. link.enabled = true;
  419. }
  420. }
  421. }
  422. }
  423. _createCCDIKSolver( mesh ) {
  424. if ( CCDIKSolver === undefined ) {
  425. throw new Error( 'THREE.MMDAnimationHelper: Import CCDIKSolver.' );
  426. }
  427. return new CCDIKSolver( mesh, mesh.geometry.userData.MMD.iks );
  428. }
  429. _createMMDPhysics( mesh, params ) {
  430. if ( MMDPhysics === undefined ) {
  431. throw new Error( 'THREE.MMDPhysics: Import MMDPhysics.' );
  432. }
  433. return new MMDPhysics(
  434. mesh,
  435. mesh.geometry.userData.MMD.rigidBodies,
  436. mesh.geometry.userData.MMD.constraints,
  437. params );
  438. }
  439. /*
  440. * Detects the longest duration and then sets it to them to sync.
  441. * TODO: Not to access private properties ( ._actions and ._clip )
  442. */
  443. _syncDuration() {
  444. let max = 0.0;
  445. const objects = this.objects;
  446. const meshes = this.meshes;
  447. const camera = this.camera;
  448. const audioManager = this.audioManager;
  449. // get the longest duration
  450. for ( let i = 0, il = meshes.length; i < il; i ++ ) {
  451. const mixer = this.objects.get( meshes[ i ] ).mixer;
  452. if ( mixer === undefined ) continue;
  453. for ( let j = 0; j < mixer._actions.length; j ++ ) {
  454. const clip = mixer._actions[ j ]._clip;
  455. if ( ! objects.has( clip ) ) {
  456. objects.set( clip, {
  457. duration: clip.duration
  458. } );
  459. }
  460. max = Math.max( max, objects.get( clip ).duration );
  461. }
  462. }
  463. if ( camera !== null ) {
  464. const mixer = this.objects.get( camera ).mixer;
  465. if ( mixer !== undefined ) {
  466. for ( let i = 0, il = mixer._actions.length; i < il; i ++ ) {
  467. const clip = mixer._actions[ i ]._clip;
  468. if ( ! objects.has( clip ) ) {
  469. objects.set( clip, {
  470. duration: clip.duration
  471. } );
  472. }
  473. max = Math.max( max, objects.get( clip ).duration );
  474. }
  475. }
  476. }
  477. if ( audioManager !== null ) {
  478. max = Math.max( max, objects.get( audioManager ).duration );
  479. }
  480. max += this.configuration.afterglow;
  481. // update the duration
  482. for ( let i = 0, il = this.meshes.length; i < il; i ++ ) {
  483. const mixer = this.objects.get( this.meshes[ i ] ).mixer;
  484. if ( mixer === undefined ) continue;
  485. for ( let j = 0, jl = mixer._actions.length; j < jl; j ++ ) {
  486. mixer._actions[ j ]._clip.duration = max;
  487. }
  488. }
  489. if ( camera !== null ) {
  490. const mixer = this.objects.get( camera ).mixer;
  491. if ( mixer !== undefined ) {
  492. for ( let i = 0, il = mixer._actions.length; i < il; i ++ ) {
  493. mixer._actions[ i ]._clip.duration = max;
  494. }
  495. }
  496. }
  497. if ( audioManager !== null ) {
  498. audioManager.duration = max;
  499. }
  500. }
  501. // workaround
  502. _updatePropertyMixersBuffer( mesh ) {
  503. const mixer = this.objects.get( mesh ).mixer;
  504. const propertyMixers = mixer._bindings;
  505. const accuIndex = mixer._accuIndex;
  506. for ( let i = 0, il = propertyMixers.length; i < il; i ++ ) {
  507. const propertyMixer = propertyMixers[ i ];
  508. const buffer = propertyMixer.buffer;
  509. const stride = propertyMixer.valueSize;
  510. const offset = ( accuIndex + 1 ) * stride;
  511. propertyMixer.binding.getValue( buffer, offset );
  512. }
  513. }
  514. /*
  515. * Avoiding these two issues by restore/save bones before/after mixer animation.
  516. *
  517. * 1. PropertyMixer used by AnimationMixer holds cache value in .buffer.
  518. * Calculating IK, Grant, and Physics after mixer animation can break
  519. * the cache coherency.
  520. *
  521. * 2. Applying Grant two or more times without reset the posing breaks model.
  522. */
  523. _saveBones( mesh ) {
  524. const objects = this.objects.get( mesh );
  525. const bones = mesh.skeleton.bones;
  526. let backupBones = objects.backupBones;
  527. if ( backupBones === undefined ) {
  528. backupBones = new Float32Array( bones.length * 7 );
  529. objects.backupBones = backupBones;
  530. }
  531. for ( let i = 0, il = bones.length; i < il; i ++ ) {
  532. const bone = bones[ i ];
  533. bone.position.toArray( backupBones, i * 7 );
  534. bone.quaternion.toArray( backupBones, i * 7 + 3 );
  535. }
  536. }
  537. _restoreBones( mesh ) {
  538. const objects = this.objects.get( mesh );
  539. const backupBones = objects.backupBones;
  540. if ( backupBones === undefined ) return;
  541. const bones = mesh.skeleton.bones;
  542. for ( let i = 0, il = bones.length; i < il; i ++ ) {
  543. const bone = bones[ i ];
  544. bone.position.fromArray( backupBones, i * 7 );
  545. bone.quaternion.fromArray( backupBones, i * 7 + 3 );
  546. }
  547. }
  548. // experimental
  549. _getMasterPhysics() {
  550. if ( this.masterPhysics !== null ) return this.masterPhysics;
  551. for ( let i = 0, il = this.meshes.length; i < il; i ++ ) {
  552. const physics = this.meshes[ i ].physics;
  553. if ( physics !== undefined && physics !== null ) {
  554. this.masterPhysics = physics;
  555. return this.masterPhysics;
  556. }
  557. }
  558. return null;
  559. }
  560. _updateSharedPhysics( delta ) {
  561. if ( this.meshes.length === 0 || ! this.enabled.physics || ! this.sharedPhysics ) return;
  562. const physics = this._getMasterPhysics();
  563. if ( physics === null ) return;
  564. for ( let i = 0, il = this.meshes.length; i < il; i ++ ) {
  565. const p = this.meshes[ i ].physics;
  566. if ( p !== null && p !== undefined ) {
  567. p.updateRigidBodies();
  568. }
  569. }
  570. physics.stepSimulation( delta );
  571. for ( let i = 0, il = this.meshes.length; i < il; i ++ ) {
  572. const p = this.meshes[ i ].physics;
  573. if ( p !== null && p !== undefined ) {
  574. p.updateBones();
  575. }
  576. }
  577. }
  578. }
  579. // Keep working quaternions for less GC
  580. const _quaternions = [];
  581. let _quaternionIndex = 0;
  582. function getQuaternion() {
  583. if ( _quaternionIndex >= _quaternions.length ) {
  584. _quaternions.push( new Quaternion() );
  585. }
  586. return _quaternions[ _quaternionIndex ++ ];
  587. }
  588. // Save rotation whose grant and IK are already applied
  589. // used by grant children
  590. const _grantResultMap = new Map();
  591. function updateOne( mesh, boneIndex, ikSolver, grantSolver ) {
  592. const bones = mesh.skeleton.bones;
  593. const bonesData = mesh.geometry.userData.MMD.bones;
  594. const boneData = bonesData[ boneIndex ];
  595. const bone = bones[ boneIndex ];
  596. // Return if already updated by being referred as a grant parent.
  597. if ( _grantResultMap.has( boneIndex ) ) return;
  598. const quaternion = getQuaternion();
  599. // Initialize grant result here to prevent infinite loop.
  600. // If it's referred before updating with actual result later
  601. // result without applyting IK or grant is gotten
  602. // but better than composing of infinite loop.
  603. _grantResultMap.set( boneIndex, quaternion.copy( bone.quaternion ) );
  604. // @TODO: Support global grant and grant position
  605. if ( grantSolver && boneData.grant &&
  606. ! boneData.grant.isLocal && boneData.grant.affectRotation ) {
  607. const parentIndex = boneData.grant.parentIndex;
  608. const ratio = boneData.grant.ratio;
  609. if ( ! _grantResultMap.has( parentIndex ) ) {
  610. updateOne( mesh, parentIndex, ikSolver, grantSolver );
  611. }
  612. grantSolver.addGrantRotation( bone, _grantResultMap.get( parentIndex ), ratio );
  613. }
  614. if ( ikSolver && boneData.ik ) {
  615. // @TODO: Updating world matrices every time solving an IK bone is
  616. // costly. Optimize if possible.
  617. mesh.updateMatrixWorld( true );
  618. ikSolver.updateOne( boneData.ik );
  619. // No confident, but it seems the grant results with ik links should be updated?
  620. const links = boneData.ik.links;
  621. for ( let i = 0, il = links.length; i < il; i ++ ) {
  622. const link = links[ i ];
  623. if ( link.enabled === false ) continue;
  624. const linkIndex = link.index;
  625. if ( _grantResultMap.has( linkIndex ) ) {
  626. _grantResultMap.set( linkIndex, _grantResultMap.get( linkIndex ).copy( bones[ linkIndex ].quaternion ) );
  627. }
  628. }
  629. }
  630. // Update with the actual result here
  631. quaternion.copy( bone.quaternion );
  632. }
  633. //
  634. class AudioManager {
  635. /**
  636. * @param {THREE.Audio} audio
  637. * @param {Object} params - (optional)
  638. * @param {Nuumber} params.delayTime
  639. */
  640. constructor( audio, params = {} ) {
  641. this.audio = audio;
  642. this.elapsedTime = 0.0;
  643. this.currentTime = 0.0;
  644. this.delayTime = params.delayTime !== undefined
  645. ? params.delayTime : 0.0;
  646. this.audioDuration = this.audio.buffer.duration;
  647. this.duration = this.audioDuration + this.delayTime;
  648. }
  649. /**
  650. * @param {Number} delta
  651. * @return {AudioManager}
  652. */
  653. control( delta ) {
  654. this.elapsed += delta;
  655. this.currentTime += delta;
  656. if ( this._shouldStopAudio() ) this.audio.stop();
  657. if ( this._shouldStartAudio() ) this.audio.play();
  658. return this;
  659. }
  660. // private methods
  661. _shouldStartAudio() {
  662. if ( this.audio.isPlaying ) return false;
  663. while ( this.currentTime >= this.duration ) {
  664. this.currentTime -= this.duration;
  665. }
  666. if ( this.currentTime < this.delayTime ) return false;
  667. // 'duration' can be bigger than 'audioDuration + delayTime' because of sync configuration
  668. if ( ( this.currentTime - this.delayTime ) > this.audioDuration ) return false;
  669. return true;
  670. }
  671. _shouldStopAudio() {
  672. return this.audio.isPlaying &&
  673. this.currentTime >= this.duration;
  674. }
  675. }
  676. const _q = new Quaternion();
  677. /**
  678. * Solver for Grant (Fuyo in Japanese. I just google translated because
  679. * Fuyo may be MMD specific term and may not be common word in 3D CG terms.)
  680. * Grant propagates a bone's transform to other bones transforms even if
  681. * they are not children.
  682. * @param {THREE.SkinnedMesh} mesh
  683. * @param {Array<Object>} grants
  684. */
  685. class GrantSolver {
  686. constructor( mesh, grants = [] ) {
  687. this.mesh = mesh;
  688. this.grants = grants;
  689. }
  690. /**
  691. * Solve all the grant bones
  692. * @return {GrantSolver}
  693. */
  694. update() {
  695. const grants = this.grants;
  696. for ( let i = 0, il = grants.length; i < il; i ++ ) {
  697. this.updateOne( grants[ i ] );
  698. }
  699. return this;
  700. }
  701. /**
  702. * Solve a grant bone
  703. * @param {Object} grant - grant parameter
  704. * @return {GrantSolver}
  705. */
  706. updateOne( grant ) {
  707. const bones = this.mesh.skeleton.bones;
  708. const bone = bones[ grant.index ];
  709. const parentBone = bones[ grant.parentIndex ];
  710. if ( grant.isLocal ) {
  711. // TODO: implement
  712. if ( grant.affectPosition ) {
  713. }
  714. // TODO: implement
  715. if ( grant.affectRotation ) {
  716. }
  717. } else {
  718. // TODO: implement
  719. if ( grant.affectPosition ) {
  720. }
  721. if ( grant.affectRotation ) {
  722. this.addGrantRotation( bone, parentBone.quaternion, grant.ratio );
  723. }
  724. }
  725. return this;
  726. }
  727. addGrantRotation( bone, q, ratio ) {
  728. _q.set( 0, 0, 0, 1 );
  729. _q.slerp( q, ratio );
  730. bone.quaternion.multiply( _q );
  731. return this;
  732. }
  733. }
  734. export { MMDAnimationHelper };