Particle.js 4.5 KB

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