AnimationHandler.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**
  2. * @author mikael emtinger / http://gomo.se/
  3. */
  4. THREE.AnimationHandler = {
  5. LINEAR: 0,
  6. CATMULLROM: 1,
  7. CATMULLROM_FORWARD: 2,
  8. //
  9. add: function () {
  10. console.warn( 'THREE.AnimationHandler.add() has been deprecated.' );
  11. },
  12. get: function () {
  13. console.warn( 'THREE.AnimationHandler.get() has been deprecated.' );
  14. },
  15. remove: function () {
  16. console.warn( 'THREE.AnimationHandler.remove() has been deprecated.' );
  17. },
  18. //
  19. animations: [],
  20. init: function ( data ) {
  21. if ( data.initialized === true ) return data;
  22. // loop through all keys
  23. for ( var h = 0; h < data.hierarchy.length; h ++ ) {
  24. for ( var k = 0; k < data.hierarchy[ h ].keys.length; k ++ ) {
  25. // remove minus times
  26. if ( data.hierarchy[ h ].keys[ k ].time < 0 ) {
  27. data.hierarchy[ h ].keys[ k ].time = 0;
  28. }
  29. // create quaternions
  30. if ( data.hierarchy[ h ].keys[ k ].rot !== undefined &&
  31. ! ( data.hierarchy[ h ].keys[ k ].rot instanceof THREE.Quaternion ) ) {
  32. var quat = data.hierarchy[ h ].keys[ k ].rot;
  33. data.hierarchy[ h ].keys[ k ].rot = new THREE.Quaternion().fromArray( quat );
  34. }
  35. }
  36. // prepare morph target keys
  37. if ( data.hierarchy[ h ].keys.length && data.hierarchy[ h ].keys[ 0 ].morphTargets !== undefined ) {
  38. // get all used
  39. var usedMorphTargets = {};
  40. for ( var k = 0; k < data.hierarchy[ h ].keys.length; k ++ ) {
  41. for ( var m = 0; m < data.hierarchy[ h ].keys[ k ].morphTargets.length; m ++ ) {
  42. var morphTargetName = data.hierarchy[ h ].keys[ k ].morphTargets[ m ];
  43. usedMorphTargets[ morphTargetName ] = - 1;
  44. }
  45. }
  46. data.hierarchy[ h ].usedMorphTargets = usedMorphTargets;
  47. // set all used on all frames
  48. for ( var k = 0; k < data.hierarchy[ h ].keys.length; k ++ ) {
  49. var influences = {};
  50. for ( var morphTargetName in usedMorphTargets ) {
  51. for ( var m = 0; m < data.hierarchy[ h ].keys[ k ].morphTargets.length; m ++ ) {
  52. if ( data.hierarchy[ h ].keys[ k ].morphTargets[ m ] === morphTargetName ) {
  53. influences[ morphTargetName ] = data.hierarchy[ h ].keys[ k ].morphTargetsInfluences[ m ];
  54. break;
  55. }
  56. }
  57. if ( m === data.hierarchy[ h ].keys[ k ].morphTargets.length ) {
  58. influences[ morphTargetName ] = 0;
  59. }
  60. }
  61. data.hierarchy[ h ].keys[ k ].morphTargetsInfluences = influences;
  62. }
  63. }
  64. // remove all keys that are on the same time
  65. for ( var k = 1; k < data.hierarchy[ h ].keys.length; k ++ ) {
  66. if ( data.hierarchy[ h ].keys[ k ].time === data.hierarchy[ h ].keys[ k - 1 ].time ) {
  67. data.hierarchy[ h ].keys.splice( k, 1 );
  68. k --;
  69. }
  70. }
  71. // set index
  72. for ( var k = 0; k < data.hierarchy[ h ].keys.length; k ++ ) {
  73. data.hierarchy[ h ].keys[ k ].index = k;
  74. }
  75. }
  76. data.initialized = true;
  77. return data;
  78. },
  79. parse: function ( root ) {
  80. var parseRecurseHierarchy = function ( root, hierarchy ) {
  81. hierarchy.push( root );
  82. for ( var c = 0; c < root.children.length; c ++ )
  83. parseRecurseHierarchy( root.children[ c ], hierarchy );
  84. };
  85. // setup hierarchy
  86. var hierarchy = [];
  87. if ( root instanceof THREE.SkinnedMesh ) {
  88. for ( var b = 0; b < root.skeleton.bones.length; b ++ ) {
  89. hierarchy.push( root.skeleton.bones[ b ] );
  90. }
  91. } else {
  92. parseRecurseHierarchy( root, hierarchy );
  93. }
  94. return hierarchy;
  95. },
  96. play: function ( animation ) {
  97. if ( this.animations.indexOf( animation ) === - 1 ) {
  98. this.animations.push( animation );
  99. }
  100. },
  101. stop: function ( animation ) {
  102. var index = this.animations.indexOf( animation );
  103. if ( index !== - 1 ) {
  104. this.animations.splice( index, 1 );
  105. }
  106. },
  107. update: function ( deltaTimeMS ) {
  108. for ( var i = 0; i < this.animations.length; i ++ ) {
  109. this.animations[ i ].resetBlendWeights();
  110. }
  111. for ( var i = 0; i < this.animations.length; i ++ ) {
  112. this.animations[ i ].update( deltaTimeMS );
  113. }
  114. }
  115. };