BoxEmitter.js 2.8 KB

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