ConeEmitter.js 1.8 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. const defaultAngle = CesiumMath.toRadians(30.0);
  6. /**
  7. * A ParticleEmitter that emits particles within a cone.
  8. * Particles will be positioned at the tip of the cone and have initial velocities going towards the base.
  9. *
  10. * @alias ConeEmitter
  11. * @constructor
  12. *
  13. * @param {Number} [angle=Cesium.Math.toRadians(30.0)] The angle of the cone in radians.
  14. */
  15. function ConeEmitter(angle) {
  16. this._angle = defaultValue(angle, defaultAngle);
  17. }
  18. Object.defineProperties(ConeEmitter.prototype, {
  19. /**
  20. * The angle of the cone in radians.
  21. * @memberof CircleEmitter.prototype
  22. * @type {Number}
  23. * @default Cesium.Math.toRadians(30.0)
  24. */
  25. angle: {
  26. get: function () {
  27. return this._angle;
  28. },
  29. set: function (value) {
  30. //>>includeStart('debug', pragmas.debug);
  31. Check.typeOf.number("value", value);
  32. //>>includeEnd('debug');
  33. this._angle = value;
  34. },
  35. },
  36. });
  37. /**
  38. * Initializes the given {Particle} by setting it's position and velocity.
  39. *
  40. * @private
  41. * @param {Particle} particle The particle to initialize
  42. */
  43. ConeEmitter.prototype.emit = function (particle) {
  44. const radius = Math.tan(this._angle);
  45. // Compute a random point on the cone's base
  46. const theta = CesiumMath.randomBetween(0.0, CesiumMath.TWO_PI);
  47. const rad = CesiumMath.randomBetween(0.0, radius);
  48. const x = rad * Math.cos(theta);
  49. const y = rad * Math.sin(theta);
  50. const z = 1.0;
  51. particle.velocity = Cartesian3.fromElements(x, y, z, particle.velocity);
  52. Cartesian3.normalize(particle.velocity, particle.velocity);
  53. particle.position = Cartesian3.clone(Cartesian3.ZERO, particle.position);
  54. };
  55. export default ConeEmitter;