AnimationMixer.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. *
  3. * Mixes together the AnimationClips scheduled by AnimationActions and applies them to the root and subtree
  4. *
  5. * TODO: MUST add support for blending between AnimationActions based on their weights, right now only the last AnimationAction is applied at full weight.
  6. *
  7. * @author Ben Houston / http://clara.io/
  8. * @author David Sarno / http://lighthaus.us/
  9. */
  10. THREE.AnimationMixer = function( root ) {
  11. this.root = root;
  12. this.time = 0;
  13. this.timeScale = 1.0;
  14. this.actions = [];
  15. this.propertyBindings = {};
  16. this.propertyBindingsArray = [];
  17. };
  18. THREE.AnimationMixer.prototype = {
  19. constructor: THREE.AnimationMixer,
  20. addAction: function( action ) {
  21. //console.log( this.root.name + ".AnimationMixer.addAnimationAction( " + action.name + " )" );
  22. this.actions.push( action );
  23. var tracks = action.clip.tracks;
  24. for( var i = 0; i < tracks.length; i ++ ) {
  25. var track = tracks[ i ];
  26. if( ! this.propertyBindings[ track.name ] ) {
  27. var propertyBinding = new THREE.PropertyBinding( this.root, track.name );
  28. this.propertyBindings[ track.name ] = propertyBinding;
  29. this.propertyBindingsArray.push( propertyBinding );
  30. }
  31. }
  32. },
  33. removeAllActions: function() {
  34. this.actions = [];
  35. },
  36. removeAction: function( action ) {
  37. //console.log( this.root.name + ".AnimationMixer.addRemove( " + action.name + " )" );
  38. var index = this.actions.indexOf( action );
  39. if ( index !== - 1 ) {
  40. this.actions.splice( index, 1 );
  41. }
  42. },
  43. update: function( deltaTime ) {
  44. this.time += deltaTime * this.timeScale;
  45. //console.log( this.root.name + ".AnimationMixer.update( " + time + " )" );
  46. for( var i = 0; i < this.actions.length; i ++ ) {
  47. var action = this.actions[i];
  48. if( action.weight <= 0 || ! action.enabled ) continue;
  49. var actionResults = action.getAt( this.time );
  50. for( var name in actionResults ) {
  51. this.propertyBindings[name].accumulate( actionResults[name], action.weight );
  52. }
  53. }
  54. // apply to nodes
  55. for ( var i = 0; i < this.propertyBindingsArray.length; i ++ ) {
  56. this.propertyBindingsArray[ i ].apply();
  57. }
  58. }
  59. };