CircleEmitter.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import Cartesian3 from "../Core/Cartesian3.js";
  2. import Check from "../Core/Check.js";
  3. import defaultValue from "../Core/defaultValue.js";
  4. import CesiumMath from "../Core/Math.js";
  5. /**
  6. * A ParticleEmitter that emits particles from a circle.
  7. * Particles will be positioned within a circle and have initial velocities going along the z vector.
  8. *
  9. * @alias CircleEmitter
  10. * @constructor
  11. *
  12. * @param {number} [radius=1.0] The radius of the circle in meters.
  13. */
  14. function CircleEmitter(radius) {
  15. radius = defaultValue(radius, 1.0);
  16. //>>includeStart('debug', pragmas.debug);
  17. Check.typeOf.number.greaterThan("radius", radius, 0.0);
  18. //>>includeEnd('debug');
  19. this._radius = defaultValue(radius, 1.0);
  20. }
  21. Object.defineProperties(CircleEmitter.prototype, {
  22. /**
  23. * The radius of the circle in meters.
  24. * @memberof CircleEmitter.prototype
  25. * @type {number}
  26. * @default 1.0
  27. */
  28. radius: {
  29. get: function () {
  30. return this._radius;
  31. },
  32. set: function (value) {
  33. //>>includeStart('debug', pragmas.debug);
  34. Check.typeOf.number.greaterThan("value", value, 0.0);
  35. //>>includeEnd('debug');
  36. this._radius = value;
  37. },
  38. },
  39. });
  40. /**
  41. * Initializes the given {@link Particle} by setting it's position and velocity.
  42. *
  43. * @private
  44. * @param {Particle} particle The particle to initialize.
  45. */
  46. CircleEmitter.prototype.emit = function (particle) {
  47. const theta = CesiumMath.randomBetween(0.0, CesiumMath.TWO_PI);
  48. const rad = CesiumMath.randomBetween(0.0, this._radius);
  49. const x = rad * Math.cos(theta);
  50. const y = rad * Math.sin(theta);
  51. const z = 0.0;
  52. particle.position = Cartesian3.fromElements(x, y, z, particle.position);
  53. particle.velocity = Cartesian3.clone(Cartesian3.UNIT_Z, particle.velocity);
  54. };
  55. export default CircleEmitter;