/
Particle.js
168 lines (155 loc) · 5.43 KB
/
Particle.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
define([
'../Core/Cartesian2',
'../Core/Cartesian3',
'../Core/Color',
'../Core/defaultValue',
'../Core/defined',
'../Core/defineProperties'
], function(
Cartesian2,
Cartesian3,
Color,
defaultValue,
defined,
defineProperties) {
'use strict';
var defaultSize = new Cartesian2(1.0, 1.0);
/**
* A particle emitted by a {@link ParticleSystem}.
*
* @alias Particle
* @constructor
*
* @param {Object} options An object with the following properties:
* @param {Number} [options.mass=1.0] The mass of particles in kilograms.
* @param {Cartesian3} [options.position=Cartesian3.ZERO] The initial position of the particle in world coordinates.
* @param {Cartesian3} [options.velocity=Cartesian3.ZERO] The velocity vector of the particle in world coordinates.
* @param {Number} [options.life=Number.MAX_VALUE] The life of particles in seconds.
* @param {Object} [options.image] The URI, HTMLImageElement, or HTMLCanvasElement to use for the billboard.
* @param {Color} [options.startColor=Color.WHITE] The color of a particle when it is born.
* @param {Color} [options.endColor=Color.WHITE] The color of a particle when it dies.
* @param {Number} [options.startScale=1.0] The scale of the particle when it is born.
* @param {Number} [options.endScale=1.0] The scale of the particle when it dies.
* @param {Cartesian2} [options.size=new Cartesian2(1.0, 1.0)] The dimensions of particles in pixels.
*/
function Particle(options) {
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
/**
* The mass of the particle in kilograms.
* @type {Number}
* @default 1.0
*/
this.mass = defaultValue(options.mass, 1.0);
/**
* The positon of the particle in world coordinates.
* @type {Cartesian3}
* @default Cartesian3.ZERO
*/
this.position = Cartesian3.clone(defaultValue(options.position, Cartesian3.ZERO));
/**
* The velocity of the particle in world coordinates.
* @type {Cartesian3}
* @default Cartesian3.ZERO
*/
this.velocity = Cartesian3.clone(defaultValue(options.velocity, Cartesian3.ZERO));
/**
* The life of the particle in seconds.
* @type {Number}
* @default Number.MAX_VALUE
*/
this.life = defaultValue(options.life, Number.MAX_VALUE);
/**
* The image to use for the particle.
* @type {Object}
* @default undefined
*/
this.image = options.image;
/**
* The color of the particle when it is born.
* @type {Color}
* @default Color.WHITE
*/
this.startColor = Color.clone(defaultValue(options.startColor, Color.WHITE));
/**
* The color of the particle when it dies.
* @type {Color}
* @default Color.WHITE
*/
this.endColor = Color.clone(defaultValue(options.endColor, Color.WHITE));
/**
* the scale of the particle when it is born.
* @type {Number}
* @default 1.0
*/
this.startScale = defaultValue(options.startScale, 1.0);
/**
* The scale of the particle when it dies.
* @type {Number}
* @default 1.0
*/
this.endScale = defaultValue(options.endScale, 1.0);
/**
* The dimensions of the particle in pixels.
* @type {Cartesian2}
* @default new Cartesian(1.0, 1.0)
*/
this.size = Cartesian2.clone(defaultValue(options.size, defaultSize));
this._age = 0.0;
this._normalizedAge = 0.0;
// used by ParticleSystem
this._billboard = undefined;
}
defineProperties(Particle.prototype, {
/**
* Gets the age of the particle in seconds.
* @memberof Particle.prototype
* @type {Number}
*/
age : {
get : function() {
return this._age;
}
},
/**
* Gets the age normalized to a value in the range [0.0, 1.0].
* @memberof Particle.prototype
* @type {Number}
*/
normalizedAge : {
get : function() {
return this._normalizedAge;
}
}
});
var deltaScratch = new Cartesian3();
/**
* @private
*/
Particle.prototype.update = function(dt, forces) {
// Apply the velocity
Cartesian3.multiplyByScalar(this.velocity, dt, deltaScratch);
Cartesian3.add(this.position, deltaScratch, this.position);
// Update any forces.
if (defined(forces)) {
var length = forces.length;
for (var i = 0; i < length; ++i) {
var force = forces[i];
if (typeof force === 'function') {
// Force is just a simple callback function.
force(this, dt);
}
}
}
// Age the particle
this._age += dt;
// Compute the normalized age.
if (this.life === Number.MAX_VALUE) {
this._normalizedAge = 0.0;
} else {
this._normalizedAge = this._age / this.life;
}
// If this particle is older than it's lifespan then die.
return this._age <= this.life;
};
return Particle;
});