ParticleSystemFrameset.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /**
  2. * @author Mark Kellogg - http://www.github.com/mkkellogg
  3. */
  4. THREE.Particles = THREE.Particles || {};
  5. THREE.Particles.FrameSet = function( timeFrames, valueFrames, isScalar ) {
  6. this.timeFrames = timeFrames || [];
  7. this.valueFrames = valueFrames || [];
  8. }
  9. THREE.Particles.FrameSet.prototype.findNextFrameForTimeValue = function( t ) {
  10. var frameIndex = 0;
  11. while ( frameIndex < this.timeFrames.length && this.timeFrames[ frameIndex ] < t ) {
  12. frameIndex = frameIndex + 1;
  13. }
  14. return frameIndex;
  15. }
  16. THREE.Particles.FrameSet.prototype.calculateFraction = function( a, b, z ) {
  17. return ( z - a ) / ( b - a );
  18. }
  19. THREE.Particles.FrameSet.prototype.interpolateFrameValues = function( t, target ) {
  20. var nextFrameIndex = this.findNextFrameForTimeValue( t );
  21. var currentFrameIndex = nextFrameIndex - 1;
  22. if ( nextFrameIndex == 0 ) {
  23. target.copy( this.valueFrames[ 0 ] );
  24. } else if ( nextFrameIndex == this.timeFrames.length ) {
  25. target.copy( this.valueFrames[ currentFrameIndex ] );
  26. } else {
  27. var fraction = this.calculateFraction( this.timeFrames[ currentFrameIndex ], this.timeFrames[ nextFrameIndex ], t );
  28. target.copy( this.valueFrames[ currentFrameIndex ] );
  29. target.lerp( this.valueFrames[ nextFrameIndex ], fraction );
  30. }
  31. }