MorphAnimation.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * @author mrdoob / http://mrdoob.com
  3. * @author willy-vvu / http://willy-vvu.github.io
  4. */
  5. THREE.MorphAnimation = function ( mesh ) {
  6. this.mesh = mesh;
  7. this.frames = mesh.morphTargetInfluences.length;
  8. this.currentTime = 0;
  9. this.duration = 1000;
  10. this.loop = true;
  11. this.lastFrame = 0;
  12. this.currentFrame = 0;
  13. this.isPlaying = false;
  14. };
  15. THREE.MorphAnimation.prototype = {
  16. constructor: THREE.MorphAnimation,
  17. play: function () {
  18. this.isPlaying = true;
  19. },
  20. pause: function () {
  21. this.isPlaying = false;
  22. },
  23. update: function ( delta ) {
  24. if ( this.isPlaying === false ) return;
  25. this.currentTime += delta;
  26. if ( this.loop === true && this.currentTime > this.duration ) {
  27. this.currentTime %= this.duration;
  28. }
  29. this.currentTime = Math.min( this.currentTime, this.duration );
  30. var frameTime = this.duration / this.frames;
  31. var frame = Math.floor( this.currentTime / frameTime );
  32. var influences = this.mesh.morphTargetInfluences;
  33. if ( frame !== this.currentFrame ) {
  34. influences[ this.lastFrame ] = 0;
  35. influences[ this.currentFrame ] = 1;
  36. influences[ frame ] = 0;
  37. this.lastFrame = this.currentFrame;
  38. this.currentFrame = frame;
  39. }
  40. var mix = ( this.currentTime % frameTime ) / frameTime;
  41. influences[ frame ] = mix;
  42. influences[ this.lastFrame ] = 1 - mix;
  43. }
  44. };