ConeEmitter.js 2.1 KB

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