AnimationMixer.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.actions = [];
  13. this.propertyBindings = {};
  14. };
  15. THREE.AnimationMixer.prototype = {
  16. constructor: THREE.AnimationMixer,
  17. addAction: function( action ) {
  18. console.log( this.root.name + ".AnimationMixer.addAnimationAction( " + action.name + " )" );
  19. this.actions.push( action );
  20. for( var track in action.tracks ) {
  21. if( ! this.propertyBindings[ track.name ] ) {
  22. this.propertyBindings[ track.name ] = new THREE.PropertyBinding( this.root, track.name );
  23. }
  24. }
  25. },
  26. removeAction: function( action ) {
  27. console.log( this.root.name + ".AnimationMixer.addRemove( " + action.name + " )" );
  28. var index = this.actions.indexOf( action );
  29. if ( index !== - 1 ) {
  30. this.actions.splice( index, 1 );
  31. }
  32. },
  33. update: function( time ) {
  34. console.log( this.root.name + ".AnimationMixer.update( " + time + " )" );
  35. var mixerResults = {};
  36. for( var i = 0; i < this.actions.length; i ++ ) {
  37. var action = this.actions[i];
  38. if( action.weight <= 0 || ! action.enabled ) continue;
  39. var actionResults = action.getAt( time );
  40. console.log( ' actionResults', actionResults );
  41. for( var name in actionResults ) {
  42. var mixerResult = mixerResults[name];
  43. var actionResult = actionResults[name];
  44. console.log( ' name', name );
  45. console.log( ' mixerResult', mixerResult );
  46. console.log( ' actionResult', actionResult );
  47. if( ! mixerResult ) {
  48. mixerResults[name] = {
  49. cumulativeValue: actionResult,
  50. cumulativeWeight: action.weight
  51. };
  52. }
  53. else {
  54. var lerpAlpha = action.weight / ( mixerResult.cumulativeWeight + action.weight );
  55. mixerResult.cumulativeValue = THREE.AnimationUtils.lerp( mixerResult.cumulativeValue, actionResult, lerpAlpha );
  56. mixerResult.cumulativeWeight += action.weight;
  57. }
  58. console.log( ' mixerResults[name]', mixerResults[name] );
  59. }
  60. }
  61. console.log( " mixerResults: ", mixerResults );
  62. // apply to nodes
  63. for ( var name in mixerResults ) {
  64. console.log( ' track:' + name );
  65. var mixerResult = mixerResults[ name ];
  66. console.log( ' mixerResult:', mixerResult );
  67. var propertyBinding = this.propertyBindings[ name ];
  68. propertyBinding.set( mixerResult.cumulativeValue );
  69. }
  70. }
  71. };