MorphAnimation.js 1.3 KB

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