-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticleObject.js
More file actions
125 lines (99 loc) · 3.33 KB
/
ParticleObject.js
File metadata and controls
125 lines (99 loc) · 3.33 KB
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
const lifeDecay = 0.01;
const ParticleObject = function (mesh, lifetime, speed, acceleration, direction, startColor, endColor) {
// Sphere mesh
this.mesh = mesh;
// Total Lifetime of this object
this.lifetime = lifetime;
// Lifetime left of this object
this.lifeLeft = lifetime;
// Speed of object
this.speed = speed;
// Acceleration of object
this.acceleration = acceleration;
// Direction of object
this.direction = direction;
// The color the particle spawns as (THREE.Color)
this.startColor = startColor;
// The color the particle interpolates to throughout its lifetime
this.endColor = endColor;
// The color change in each update
this.deltaR = parseFloat((endColor.r - startColor.r) / lifetime * lifeDecay);
this.deltaG = parseFloat((endColor.g - startColor.g) / lifetime * lifeDecay);
this.deltaB = parseFloat((endColor.b - startColor.b) / lifetime * lifeDecay);
};
ParticleObject.prototype.update = function(){
this.updatePosition();
this.updateLifeLeft();
this.adjustSpeedByAcceleration();
this.updateColor();
};
ParticleObject.prototype.updatePosition = function() {
this.mesh.position.set(
this.mesh.position.x + (this.direction.elements[0] * this.speed),
this.mesh.position.y + (this.direction.elements[1] * this.speed),
this.mesh.position.z + (this.direction.elements[2] * this.speed)
);
};
ParticleObject.prototype.updateColor = function() {
this.mesh.material.color = new THREE.Color(
parseFloat(this.mesh.material.color.r) + this.deltaR,
parseFloat(this.mesh.material.color.g) + this.deltaG,
parseFloat(this.mesh.material.color.b) + this.deltaB
);
};
ParticleObject.prototype.updateLifeLeft = function() {
this.lifeLeft = this.lifeLeft - lifeDecay;
};
ParticleObject.prototype.adjustSpeedByAcceleration = function() {
this.speed = this.speed * this.acceleration;
};
ParticleObject.prototype.resetColor = function() {
this.mesh.material.color = this.startColor;
};
ParticleObject.prototype.swapColor = function(newStartColor, newEndColor) {
this.startColor = newStartColor;
this.endColor = newEndColor;
this.deltaR = parseFloat((newEndColor.r - newStartColor.r) / this.lifetime * lifeDecay);
this.deltaG = parseFloat((newEndColor.g - newStartColor.g) / this.lifetime * lifeDecay);
this.deltaB = parseFloat((newEndColor.b - newStartColor.b) / this.lifetime * lifeDecay);
};
ParticleObject.prototype.setPosition = function(x, y, z) {
this.mesh.position.set(x,y,z);
};
ParticleObject.prototype.getMesh = function()
{
return this.mesh;
};
ParticleObject.prototype.getLifeLeft = function()
{
return this.lifeLeft;
};
ParticleObject.prototype.setLifetime = function(lifetime)
{
this.lifetime = lifetime;
this.lifeLeft = lifetime;
};
ParticleObject.prototype.setSize = function(size)
{
this.mesh.scale.setScalar(size);
};
ParticleObject.prototype.getSpeed = function()
{
return this.speed;
};
ParticleObject.prototype.setSpeed = function(speed)
{
this.speed = speed;
};
ParticleObject.prototype.getAcceleration = function()
{
return this.acceleration;
};
ParticleObject.prototype.setAcceleration = function(acceleration)
{
this.acceleration = acceleration;
};
ParticleObject.prototype.getDirection = function()
{
return this.direction;
};