SphereEmitter.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. /**
  7. * A ParticleEmitter that emits particles within a sphere.
  8. * Particles will be positioned randomly within the sphere and have initial velocities emanating from the center of the sphere.
  9. *
  10. * @alias SphereEmitter
  11. * @constructor
  12. *
  13. * @param {Number} [radius=1.0] The radius of the sphere in meters.
  14. */
  15. function SphereEmitter(radius) {
  16. radius = defaultValue(radius, 1.0);
  17. //>>includeStart('debug', pragmas.debug);
  18. Check.typeOf.number.greaterThan('radius', radius, 0.0);
  19. //>>includeEnd('debug');
  20. this._radius = defaultValue(radius, 1.0);
  21. }
  22. defineProperties(SphereEmitter.prototype, {
  23. /**
  24. * The radius of the sphere in meters.
  25. * @memberof SphereEmitter.prototype
  26. * @type {Number}
  27. * @default 1.0
  28. */
  29. radius : {
  30. get : function() {
  31. return this._radius;
  32. },
  33. set : function(value) {
  34. //>>includeStart('debug', pragmas.debug);
  35. Check.typeOf.number.greaterThan('value', value, 0.0);
  36. //>>includeEnd('debug');
  37. this._radius = value;
  38. }
  39. }
  40. });
  41. /**
  42. * Initializes the given {Particle} by setting it's position and velocity.
  43. *
  44. * @private
  45. * @param {Particle} particle The particle to initialize
  46. */
  47. SphereEmitter.prototype.emit = function(particle) {
  48. var theta = CesiumMath.randomBetween(0.0, CesiumMath.TWO_PI);
  49. var phi = CesiumMath.randomBetween(0.0, CesiumMath.PI);
  50. var rad = CesiumMath.randomBetween(0.0, this._radius);
  51. var x = rad * Math.cos(theta) * Math.sin(phi);
  52. var y = rad * Math.sin(theta) * Math.sin(phi);
  53. var z = rad * Math.cos(phi);
  54. particle.position = Cartesian3.fromElements(x, y, z, particle.position);
  55. particle.velocity = Cartesian3.normalize(particle.position, particle.velocity);
  56. };
  57. export default SphereEmitter;