Particle.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import Cartesian2 from '../Core/Cartesian2.js';
  2. import Cartesian3 from '../Core/Cartesian3.js';
  3. import Color from '../Core/Color.js';
  4. import defaultValue from '../Core/defaultValue.js';
  5. import defined from '../Core/defined.js';
  6. import defineProperties from '../Core/defineProperties.js';
  7. var defaultSize = new Cartesian2(1.0, 1.0);
  8. /**
  9. * A particle emitted by a {@link ParticleSystem}.
  10. *
  11. * @alias Particle
  12. * @constructor
  13. *
  14. * @param {Object} options An object with the following properties:
  15. * @param {Number} [options.mass=1.0] The mass of the particle in kilograms.
  16. * @param {Cartesian3} [options.position=Cartesian3.ZERO] The initial position of the particle in world coordinates.
  17. * @param {Cartesian3} [options.velocity=Cartesian3.ZERO] The velocity vector of the particle in world coordinates.
  18. * @param {Number} [options.life=Number.MAX_VALUE] The life of the particle in seconds.
  19. * @param {Object} [options.image] The URI, HTMLImageElement, or HTMLCanvasElement to use for the billboard.
  20. * @param {Color} [options.startColor=Color.WHITE] The color of a particle when it is born.
  21. * @param {Color} [options.endColor=Color.WHITE] The color of a particle when it dies.
  22. * @param {Number} [options.startScale=1.0] The scale of the particle when it is born.
  23. * @param {Number} [options.endScale=1.0] The scale of the particle when it dies.
  24. * @param {Cartesian2} [options.imageSize=new Cartesian2(1.0, 1.0)] The dimensions, width by height, to scale the particle image in pixels.
  25. */
  26. function Particle(options) {
  27. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  28. /**
  29. * The mass of the particle in kilograms.
  30. * @type {Number}
  31. * @default 1.0
  32. */
  33. this.mass = defaultValue(options.mass, 1.0);
  34. /**
  35. * The positon of the particle in world coordinates.
  36. * @type {Cartesian3}
  37. * @default Cartesian3.ZERO
  38. */
  39. this.position = Cartesian3.clone(defaultValue(options.position, Cartesian3.ZERO));
  40. /**
  41. * The velocity of the particle in world coordinates.
  42. * @type {Cartesian3}
  43. * @default Cartesian3.ZERO
  44. */
  45. this.velocity = Cartesian3.clone(defaultValue(options.velocity, Cartesian3.ZERO));
  46. /**
  47. * The life of the particle in seconds.
  48. * @type {Number}
  49. * @default Number.MAX_VALUE
  50. */
  51. this.life = defaultValue(options.life, Number.MAX_VALUE);
  52. /**
  53. * The image to use for the particle.
  54. * @type {Object}
  55. * @default undefined
  56. */
  57. this.image = options.image;
  58. /**
  59. * The color of the particle when it is born.
  60. * @type {Color}
  61. * @default Color.WHITE
  62. */
  63. this.startColor = Color.clone(defaultValue(options.startColor, Color.WHITE));
  64. /**
  65. * The color of the particle when it dies.
  66. * @type {Color}
  67. * @default Color.WHITE
  68. */
  69. this.endColor = Color.clone(defaultValue(options.endColor, Color.WHITE));
  70. /**
  71. * the scale of the particle when it is born.
  72. * @type {Number}
  73. * @default 1.0
  74. */
  75. this.startScale = defaultValue(options.startScale, 1.0);
  76. /**
  77. * The scale of the particle when it dies.
  78. * @type {Number}
  79. * @default 1.0
  80. */
  81. this.endScale = defaultValue(options.endScale, 1.0);
  82. /**
  83. * The dimensions, width by height, to scale the particle image in pixels.
  84. * @type {Cartesian2}
  85. * @default new Cartesian(1.0, 1.0)
  86. */
  87. this.imageSize = Cartesian2.clone(defaultValue(options.imageSize, defaultSize));
  88. this._age = 0.0;
  89. this._normalizedAge = 0.0;
  90. // used by ParticleSystem
  91. this._billboard = undefined;
  92. }
  93. defineProperties(Particle.prototype, {
  94. /**
  95. * Gets the age of the particle in seconds.
  96. * @memberof Particle.prototype
  97. * @type {Number}
  98. */
  99. age : {
  100. get : function() {
  101. return this._age;
  102. }
  103. },
  104. /**
  105. * Gets the age normalized to a value in the range [0.0, 1.0].
  106. * @memberof Particle.prototype
  107. * @type {Number}
  108. */
  109. normalizedAge : {
  110. get : function() {
  111. return this._normalizedAge;
  112. }
  113. }
  114. });
  115. var deltaScratch = new Cartesian3();
  116. /**
  117. * @private
  118. */
  119. Particle.prototype.update = function(dt, particleUpdateFunction) {
  120. // Apply the velocity
  121. Cartesian3.multiplyByScalar(this.velocity, dt, deltaScratch);
  122. Cartesian3.add(this.position, deltaScratch, this.position);
  123. // Update any forces.
  124. if (defined(particleUpdateFunction)) {
  125. particleUpdateFunction(this, dt);
  126. }
  127. // Age the particle
  128. this._age += dt;
  129. // Compute the normalized age.
  130. if (this.life === Number.MAX_VALUE) {
  131. this._normalizedAge = 0.0;
  132. } else {
  133. this._normalizedAge = this._age / this.life;
  134. }
  135. // If this particle is older than it's lifespan then die.
  136. return this._age <= this.life;
  137. };
  138. export default Particle;