SphereEmitter.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 within a sphere.
  7. * Particles will be positioned randomly within the sphere and have initial velocities emanating from the center of the sphere.
  8. *
  9. * @alias SphereEmitter
  10. * @constructor
  11. *
  12. * @param {Number} [radius=1.0] The radius of the sphere in meters.
  13. */
  14. function SphereEmitter(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(SphereEmitter.prototype, {
  22. /**
  23. * The radius of the sphere in meters.
  24. * @memberof SphereEmitter.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 {Particle} by setting it's position and velocity.
  42. *
  43. * @private
  44. * @param {Particle} particle The particle to initialize
  45. */
  46. SphereEmitter.prototype.emit = function (particle) {
  47. const theta = CesiumMath.randomBetween(0.0, CesiumMath.TWO_PI);
  48. const phi = CesiumMath.randomBetween(0.0, CesiumMath.PI);
  49. const rad = CesiumMath.randomBetween(0.0, this._radius);
  50. const x = rad * Math.cos(theta) * Math.sin(phi);
  51. const y = rad * Math.sin(theta) * Math.sin(phi);
  52. const z = rad * Math.cos(phi);
  53. particle.position = Cartesian3.fromElements(x, y, z, particle.position);
  54. particle.velocity = Cartesian3.normalize(
  55. particle.position,
  56. particle.velocity
  57. );
  58. };
  59. export default SphereEmitter;