ParticleModifiers.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * @author Mark Kellogg - http://www.github.com/mkkellogg
  3. */
  4. THREE.Particles = THREE.Particles || {};
  5. //=======================================
  6. // Base Modifier
  7. //=======================================
  8. THREE.Particles.Modifier = function() {
  9. }
  10. //=======================================
  11. // Random Modifier
  12. //=======================================
  13. THREE.Particles.RandomModifier = function( params ) {
  14. THREE.Particles.Modifier.call( this );
  15. if ( ! params ) params = {};
  16. if ( ! params.range ) {
  17. throw "Particles.RandomModifier: No range specified.";
  18. }
  19. if ( ! params.offset ) {
  20. throw "Particles.RandomModifier: No offset specified.";
  21. }
  22. this.range = params.range;
  23. this.offset = params.offset;
  24. this.rangeType = params.rangeType || THREE.Particles.RangeType.Cube;
  25. this.rangeEdgeClamp = params.rangeEdgeClamp !== undefined && params.rangeEdgeClamp !== null ? params.rangeEdgeClamp : false ;
  26. }
  27. THREE.Particles.RandomModifier.prototype = Object.create( THREE.Particles.Modifier.prototype );
  28. THREE.Particles.RandomModifier.prototype.update = function( particle, target ) {
  29. if ( this.rangeType == THREE.Particles.RangeType.Cube ) {
  30. THREE.Particles.Random.getRandomVectorCube( target, this.offset, this.range, this.rangeEdgeClamp );
  31. } else if ( this.rangeType == THREE.Particles.RangeType.Sphere ) {
  32. THREE.Particles.Random.getRandomVectorSphere( target, this.offset, this.range, this.rangeEdgeClamp );
  33. }
  34. }
  35. //=======================================
  36. // FrameSet Modifier
  37. //=======================================
  38. THREE.Particles.FrameSetModifier = function( frameset ) {
  39. THREE.Particles.Modifier.call( this );
  40. this.frameset = frameset;
  41. }
  42. THREE.Particles.FrameSetModifier.prototype = Object.create( THREE.Particles.Modifier.prototype );
  43. THREE.Particles.FrameSetModifier.prototype.update = function( particle, target ) {
  44. this.frameset.interpolateFrameValues( particle.age, target );
  45. }
  46. //=======================================
  47. // EvenIntervalIndex Modifier
  48. //=======================================
  49. THREE.Particles.EvenIntervalIndexModifier = function( totalSteps ) {
  50. THREE.Particles.Modifier.call( this );
  51. this.totalSteps = Math.floor( totalSteps || 1 );
  52. }
  53. THREE.Particles.EvenIntervalIndexModifier.prototype = Object.create( THREE.Particles.Modifier.prototype );
  54. THREE.Particles.EvenIntervalIndexModifier.prototype.update = function( particle, target ) {
  55. var fraction = particle.age / particle.lifeSpan;
  56. var step = Math.floor( fraction * this.totalSteps );
  57. if ( step == this.totalSteps && step > 0 ) step --;
  58. target.set( step, step, step );
  59. }