/
BoxEmitter.js
84 lines (73 loc) · 2.88 KB
/
BoxEmitter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
define([
'../Core/Cartesian3',
'../Core/Check',
'../Core/defaultValue',
'../Core/defineProperties',
'../Core/Math'
], function(
Cartesian3,
Check,
defaultValue,
defineProperties,
CesiumMath) {
'use strict';
var defaultDimensions = new Cartesian3(1.0, 1.0, 1.0);
/**
* A ParticleEmitter that emits particles within a box.
* Particles will be positioned randomly within the box and have initial velocities emanating from the center of the box.
*
* @alias BoxEmitter
* @constructor
*
* @param {Cartesian3} dimensions The width, height and depth dimensions of the box.
*/
function BoxEmitter(dimensions) {
dimensions = defaultValue(dimensions, defaultDimensions);
//>>includeStart('debug', pragmas.debug);
Check.defined('dimensions', dimensions);
Check.typeOf.number.greaterThanOrEquals('dimensions.x', dimensions.x, 0.0);
Check.typeOf.number.greaterThanOrEquals('dimensions.y', dimensions.y, 0.0);
Check.typeOf.number.greaterThanOrEquals('dimensions.z', dimensions.z, 0.0);
//>>includeEnd('debug');
this._dimensions = Cartesian3.clone(dimensions);
}
defineProperties(BoxEmitter.prototype, {
/**
* The width, height and depth dimensions of the box in meters.
* @memberof BoxEmitter.prototype
* @type {Cartesian3}
* @default new Cartesian3(1.0, 1.0, 1.0)
*/
dimensions : {
get : function() {
return this._dimensions;
},
set : function(value) {
//>>includeStart('debug', pragmas.debug);
Check.defined('value', value);
Check.typeOf.number.greaterThanOrEquals('value.x', value.x, 0.0);
Check.typeOf.number.greaterThanOrEquals('value.y', value.y, 0.0);
Check.typeOf.number.greaterThanOrEquals('value.z', value.z, 0.0);
//>>includeEnd('debug');
Cartesian3.clone(value, this._dimensions);
}
}
});
var scratchHalfDim = new Cartesian3();
/**
* Initializes the given {Particle} by setting it's position and velocity.
*
* @private
* @param {Particle} particle The particle to initialize.
*/
BoxEmitter.prototype.emit = function(particle) {
var dim = this._dimensions;
var halfDim = Cartesian3.multiplyByScalar(dim, 0.5, scratchHalfDim);
var x = CesiumMath.randomBetween(-halfDim.x, halfDim.x);
var y = CesiumMath.randomBetween(-halfDim.y, halfDim.y);
var z = CesiumMath.randomBetween(-halfDim.z, halfDim.z);
particle.position = Cartesian3.fromElements(x, y, z, particle.position);
particle.velocity = Cartesian3.normalize(particle.position, particle.velocity);
};
return BoxEmitter;
});