AnimationMixer.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /**
  2. *
  3. * Mixes together the AnimationClips scheduled by AnimationActions and applies them to the root and subtree
  4. *
  5. *
  6. * @author Ben Houston / http://clara.io/
  7. * @author David Sarno / http://lighthaus.us/
  8. */
  9. THREE.AnimationMixer = function( root ) {
  10. this.root = root;
  11. this.time = 0;
  12. this.timeScale = 1.0;
  13. this.actions = [];
  14. this.propertyBindings = [];
  15. this.propertyBindingNamesToIndex = {};
  16. };
  17. THREE.AnimationMixer.prototype = {
  18. constructor: THREE.AnimationMixer,
  19. addAction: function( action ) {
  20. // TODO: check for duplicate action names? Or provide each action with a UUID?
  21. this.actions.push( action );
  22. action.init( this.time );
  23. action.mixer = this;
  24. var tracks = action.clip.tracks;
  25. var root = action.localRoot || this.root;
  26. for( var i = 0; i < tracks.length; i ++ ) {
  27. var track = tracks[ i ];
  28. var j = this.getPropertyBindingIndex( root, track.name )
  29. var propertyBinding;
  30. if( j < 0 ) {
  31. propertyBinding = new THREE.PropertyBinding( root, track.name );
  32. this.propertyBindings.push( propertyBinding );
  33. this.propertyBindingNamesToIndex[ root.uuid + '-' + track.name ] = this.propertyBindings.length - 1;
  34. }
  35. else {
  36. propertyBinding = this.propertyBindings[ j ];
  37. }
  38. // track usages of shared property bindings, because if we leave too many around, the mixer can get slow
  39. propertyBinding.referenceCount += 1;
  40. }
  41. this.propertyBindingIndicesDirty = true;
  42. },
  43. getPropertyBindingIndex: function( rootNode, trackName ) {
  44. var index = this.propertyBindingNamesToIndex[ rootNode.uuid + '-' + trackName ];
  45. return ( index !== undefined ) ? index : -1;
  46. },
  47. updatePropertyBindingIndices: function() {
  48. if( this.propertyBindingIndicesDirty ) {
  49. for( var i = 0; i < this.actions.length; i++ ) {
  50. var action = this.actions[i];
  51. var root = action.localRoot || this.root;
  52. var propertyBindingIndices = [];
  53. for( var j = 0; j < action.clip.tracks.length; j ++ ) {
  54. var trackName = action.clip.tracks[j].name;
  55. propertyBindingIndices.push( this.getPropertyBindingIndex( root, trackName ) );
  56. }
  57. action.propertyBindingIndices = propertyBindingIndices;
  58. }
  59. this.propertyBindingIndicesDirty = false;
  60. }
  61. },
  62. removeAllActions: function() {
  63. for( var i = 0; i < this.actions.length; i ++ ) {
  64. this.actions[i].mixer = null;
  65. }
  66. // unbind all property bindings
  67. for( var i = 0; i < this.propertyBindings.length; i ++ ) {
  68. this.propertyBindings[i].unbind();
  69. }
  70. this.actions = [];
  71. this.propertyBindings = [];
  72. this.propertyBindingNamesToIndex = {};
  73. return this;
  74. },
  75. removeAction: function( action ) {
  76. var index = this.actions.indexOf( action );
  77. if ( index !== - 1 ) {
  78. this.actions.splice( index, 1 );
  79. action.mixer = null;
  80. }
  81. // remove unused property bindings because if we leave them around the mixer can get slow
  82. var root = action.localRoot || this.root;
  83. var tracks = action.clip.tracks;
  84. for( var i = 0; i < tracks.length; i ++ ) {
  85. var track = tracks[ i ];
  86. var propertyBindingIndex = this.getPropertyBindingIndex( root, track.name );
  87. var propertyBinding = this.propertyBindings[ propertyBindingIndex ];
  88. propertyBinding.referenceCount -= 1;
  89. if( propertyBinding.referenceCount <= 0 ) {
  90. propertyBinding.unbind();
  91. this.propertyBindings.splice( this.propertyBindings.indexOf( propertyBinding ), 1 );
  92. }
  93. }
  94. this.propertyBindingIndicesDirty = true;
  95. return this;
  96. },
  97. // can be optimized if needed
  98. findActionByName: function( name ) {
  99. for( var i = 0; i < this.actions.length; i ++ ) {
  100. if( this.actions[i].name === name ) return this.actions[i];
  101. }
  102. return null;
  103. },
  104. play: function( action, optionalFadeInDuration ) {
  105. action.startTime = this.time;
  106. this.addAction( action );
  107. return this;
  108. },
  109. fadeOut: function( action, duration ) {
  110. var keys = [];
  111. keys.push( { time: this.time, value: 1 } );
  112. keys.push( { time: this.time + duration, value: 0 } );
  113. action.weight = new THREE.NumberKeyframeTrack( "weight", keys );
  114. return this;
  115. },
  116. fadeIn: function( action, duration ) {
  117. var keys = [];
  118. keys.push( { time: this.time, value: 0 } );
  119. keys.push( { time: this.time + duration, value: 1 } );
  120. action.weight = new THREE.NumberKeyframeTrack( "weight", keys );
  121. return this;
  122. },
  123. warp: function( action, startTimeScale, endTimeScale, duration ) {
  124. var keys = [];
  125. keys.push( { time: this.time, value: startTimeScale } );
  126. keys.push( { time: this.time + duration, value: endTimeScale } );
  127. action.timeScale = new THREE.NumberKeyframeTrack( "timeScale", keys );
  128. return this;
  129. },
  130. crossFade: function( fadeOutAction, fadeInAction, duration, warp ) {
  131. this.fadeOut( fadeOutAction, duration );
  132. this.fadeIn( fadeInAction, duration );
  133. if( warp ) {
  134. var startEndRatio = fadeOutAction.clip.duration / fadeInAction.clip.duration;
  135. var endStartRatio = 1.0 / startEndRatio;
  136. this.warp( fadeOutAction, 1.0, startEndRatio, duration );
  137. this.warp( fadeInAction, endStartRatio, 1.0, duration );
  138. }
  139. return this;
  140. },
  141. update: function( deltaTime ) {
  142. this.updatePropertyBindingIndices();
  143. var mixerDeltaTime = deltaTime * this.timeScale;
  144. this.time += mixerDeltaTime;
  145. for( var i = 0; i < this.actions.length; i ++ ) {
  146. var action = this.actions[i];
  147. var weight = action.getWeightAt( this.time );
  148. var actionTimeScale = action.getTimeScaleAt( this.time );
  149. var actionDeltaTime = mixerDeltaTime * actionTimeScale;
  150. var actionResults = action.update( actionDeltaTime );
  151. if( action.weight <= 0 || ! action.enabled ) continue;
  152. for( var j = 0; j < actionResults.length; j ++ ) {
  153. var name = action.clip.tracks[j].name;
  154. this.propertyBindings[ action.propertyBindingIndices[ j ] ].accumulate( actionResults[j], weight );
  155. }
  156. }
  157. // apply to nodes
  158. for ( var i = 0; i < this.propertyBindings.length; i ++ ) {
  159. this.propertyBindings[ i ].apply();
  160. }
  161. return this;
  162. }
  163. };
  164. THREE.EventDispatcher.prototype.apply( THREE.AnimationMixer.prototype );
粤ICP备19079148号