智慧水务管理系统 - 精河县供水工程综合管理平台

Particle.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import Cartesian2 from "../Core/Cartesian2.js";
  2. import Cartesian3 from "../Core/Cartesian3.js";
  3. import Color from "../Core/Color.js";
  4. import Frozen from "../Core/Frozen.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 = options ?? Frozen.EMPTY_OBJECT;
  27. /**
  28. * The mass of the particle in kilograms.
  29. * @type {number}
  30. * @default 1.0
  31. */
  32. this.mass = 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(options.position ?? Cartesian3.ZERO);
  39. /**
  40. * The velocity of the particle in world coordinates.
  41. * @type {Cartesian3}
  42. * @default Cartesian3.ZERO
  43. */
  44. this.velocity = Cartesian3.clone(options.velocity ?? Cartesian3.ZERO);
  45. /**
  46. * The life of the particle in seconds.
  47. * @type {number}
  48. * @default Number.MAX_VALUE
  49. */
  50. this.life = options.life ?? Number.MAX_VALUE;
  51. /**
  52. * The image to use for the particle.
  53. * @type {object}
  54. * @default undefined
  55. */
  56. this.image = options.image;
  57. /**
  58. * The color of the particle when it is born.
  59. * @type {Color}
  60. * @default Color.WHITE
  61. */
  62. this.startColor = Color.clone(options.startColor ?? Color.WHITE);
  63. /**
  64. * The color of the particle when it dies.
  65. * @type {Color}
  66. * @default Color.WHITE
  67. */
  68. this.endColor = Color.clone(options.endColor ?? Color.WHITE);
  69. /**
  70. * the scale of the particle when it is born.
  71. * @type {number}
  72. * @default 1.0
  73. */
  74. this.startScale = options.startScale ?? 1.0;
  75. /**
  76. * The scale of the particle when it dies.
  77. * @type {number}
  78. * @default 1.0
  79. */
  80. this.endScale = options.endScale ?? 1.0;
  81. /**
  82. * The dimensions, width by height, to scale the particle image in pixels.
  83. * @type {Cartesian2}
  84. * @default new Cartesian(1.0, 1.0)
  85. */
  86. this.imageSize = Cartesian2.clone(options.imageSize ?? defaultSize);
  87. this._age = 0.0;
  88. this._normalizedAge = 0.0;
  89. // used by ParticleSystem
  90. this._billboard = undefined;
  91. }
  92. Object.defineProperties(Particle.prototype, {
  93. /**
  94. * Gets the age of the particle in seconds.
  95. * @memberof Particle.prototype
  96. * @type {number}
  97. */
  98. age: {
  99. get: function () {
  100. return this._age;
  101. },
  102. },
  103. /**
  104. * Gets the age normalized to a value in the range [0.0, 1.0].
  105. * @memberof Particle.prototype
  106. * @type {number}
  107. */
  108. normalizedAge: {
  109. get: function () {
  110. return this._normalizedAge;
  111. },
  112. },
  113. });
  114. const deltaScratch = new Cartesian3();
  115. /**
  116. * @private
  117. */
  118. Particle.prototype.update = function (dt, particleUpdateFunction) {
  119. // Apply the velocity
  120. Cartesian3.multiplyByScalar(this.velocity, dt, deltaScratch);
  121. Cartesian3.add(this.position, deltaScratch, this.position);
  122. // Update any forces.
  123. if (defined(particleUpdateFunction)) {
  124. particleUpdateFunction(this, dt);
  125. }
  126. // Age the particle
  127. this._age += dt;
  128. // Compute the normalized age.
  129. if (this.life === Number.MAX_VALUE) {
  130. this._normalizedAge = 0.0;
  131. } else {
  132. this._normalizedAge = this._age / this.life;
  133. }
  134. // If this particle is older than it's lifespan then die.
  135. return this._age <= this.life;
  136. };
  137. export default Particle;