AnimationAction.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. *
  3. * A clip that has been explicitly scheduled.
  4. *
  5. * @author Ben Houston / http://clara.io/
  6. * @author David Sarno / http://lighthaus.us/
  7. */
  8. THREE.AnimationAction = function ( clip, startTime, timeScale, weight, loop ) {
  9. if( clip === undefined ) throw new Error( 'clip is null' );
  10. this.clip = clip;
  11. this.startTime = startTime || 0;
  12. this.timeScale = timeScale || 1;
  13. this.weight = weight || 1;
  14. this.loop = loop || clip.loop || true;
  15. this.loopCount = 0;
  16. this.enabled = true; // allow for easy disabling of the action.
  17. this.clipTime = 0;
  18. this.propertyBindingIndices = [];
  19. };
  20. THREE.AnimationAction.prototype = {
  21. constructor: THREE.AnimationAction,
  22. updateTime: function( clipDeltaTime ) {
  23. var newClipTime = this.clipTime + clipDeltaTime;
  24. var duration = this.clip.duration;
  25. if( newClipTime <= 0 ) {
  26. if( this.loop ) {
  27. newClipTime -= Math.floor( newClipTime / duration ) * duration;
  28. this.clipTime = newClipTime % duration;
  29. this.loopCount --;
  30. this.mixer.dispatchEvent( { type: 'loop', action: this, direction: -1 } );
  31. }
  32. else {
  33. if( this.clipTime > 0 ) {
  34. this.mixer.dispatchEvent( { type: 'finished', action: this, direction: -1 } );
  35. }
  36. this.clipTime = Math.min( newClipTime, Math.max( duration, 0 ) );
  37. }
  38. }
  39. else if( newClipTime >= duration ) {
  40. if( this.loop ) {
  41. this.clipTime = newClipTime % duration;
  42. this.loopCount ++;
  43. this.mixer.dispatchEvent( { type: 'loop', action: this, direction: +1 } );
  44. }
  45. else {
  46. if( this.clipTime < duration ) {
  47. this.mixer.dispatchEvent( { type: 'finished', action: this, direction: +1 } );
  48. }
  49. this.clipTime = Math.min( newClipTime, Math.max( duration, 0 ) );
  50. }
  51. }
  52. else {
  53. this.clipTime = newClipTime;
  54. }
  55. return this.clipTime;
  56. },
  57. init: function( time ) {
  58. this.clipTime = time - this.startTime;
  59. },
  60. update: function( clipDeltaTime ) {
  61. this.updateTime( clipDeltaTime );
  62. var clipResults = this.clip.getAt( this.clipTime );
  63. return clipResults;
  64. },
  65. getTimeScaleAt: function( time ) {
  66. if( this.timeScale.getAt ) {
  67. // pass in time, not clip time, allows for fadein/fadeout across multiple loops of the clip
  68. return this.timeScale.getAt( time );
  69. }
  70. return this.timeScale;
  71. },
  72. getWeightAt: function( time ) {
  73. if( this.weight.getAt ) {
  74. // pass in time, not clip time, allows for fadein/fadeout across multiple loops of the clip
  75. return this.weight.getAt( time );
  76. }
  77. return this.weight;
  78. }
  79. };