All files / engine/Source/Scene Particle.js

96.55% Statements 28/29
95.83% Branches 23/24
100% Functions 4/4
96.55% Lines 28/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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