BoxEmitter.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 defaultDimensions = new Cartesian3(1.0, 1.0, 1.0);
  6. /**
  7. * A ParticleEmitter that emits particles within a box.
  8. * Particles will be positioned randomly within the box and have initial velocities emanating from the center of the box.
  9. *
  10. * @alias BoxEmitter
  11. * @constructor
  12. *
  13. * @param {Cartesian3} dimensions The width, height and depth dimensions of the box.
  14. */
  15. function BoxEmitter(dimensions) {
  16. dimensions = defaultValue(dimensions, defaultDimensions);
  17. //>>includeStart('debug', pragmas.debug);
  18. Check.defined("dimensions", dimensions);
  19. Check.typeOf.number.greaterThanOrEquals("dimensions.x", dimensions.x, 0.0);
  20. Check.typeOf.number.greaterThanOrEquals("dimensions.y", dimensions.y, 0.0);
  21. Check.typeOf.number.greaterThanOrEquals("dimensions.z", dimensions.z, 0.0);
  22. //>>includeEnd('debug');
  23. this._dimensions = Cartesian3.clone(dimensions);
  24. }
  25. Object.defineProperties(BoxEmitter.prototype, {
  26. /**
  27. * The width, height and depth dimensions of the box in meters.
  28. * @memberof BoxEmitter.prototype
  29. * @type {Cartesian3}
  30. * @default new Cartesian3(1.0, 1.0, 1.0)
  31. */
  32. dimensions: {
  33. get: function () {
  34. return this._dimensions;
  35. },
  36. set: function (value) {
  37. //>>includeStart('debug', pragmas.debug);
  38. Check.defined("value", value);
  39. Check.typeOf.number.greaterThanOrEquals("value.x", value.x, 0.0);
  40. Check.typeOf.number.greaterThanOrEquals("value.y", value.y, 0.0);
  41. Check.typeOf.number.greaterThanOrEquals("value.z", value.z, 0.0);
  42. //>>includeEnd('debug');
  43. Cartesian3.clone(value, this._dimensions);
  44. },
  45. },
  46. });
  47. const scratchHalfDim = new Cartesian3();
  48. /**
  49. * Initializes the given {Particle} by setting it's position and velocity.
  50. *
  51. * @private
  52. * @param {Particle} particle The particle to initialize.
  53. */
  54. BoxEmitter.prototype.emit = function (particle) {
  55. const dim = this._dimensions;
  56. const halfDim = Cartesian3.multiplyByScalar(dim, 0.5, scratchHalfDim);
  57. const x = CesiumMath.randomBetween(-halfDim.x, halfDim.x);
  58. const y = CesiumMath.randomBetween(-halfDim.y, halfDim.y);
  59. const z = CesiumMath.randomBetween(-halfDim.z, halfDim.z);
  60. particle.position = Cartesian3.fromElements(x, y, z, particle.position);
  61. particle.velocity = Cartesian3.normalize(
  62. particle.position,
  63. particle.velocity
  64. );
  65. };
  66. export default BoxEmitter;