Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mamayaya1
GitHub Repository: mamayaya1/game
Path: blob/main/projects/HexGL/bkcore/threejs/Particles.js
4627 views
1
/*!
2
* @class bkcore.threejs.Particles
3
*
4
* Particle system wrapper/helper
5
*
6
* @author Thibaut 'BKcore' Despoulain <http://bkcore.com>
7
*/
8
9
/*!
10
* @package bkcore.threejs
11
*/
12
var bkcore = bkcore || {};
13
bkcore.threejs = bkcore.threejs || {};
14
15
/**
16
* Creates a new particle system using given parameters
17
* @param {Object{max, spawnRate, spawn, velocity, randomness, force, spawnRadius, life, friction, color, color2, tint, texture, size, blending, depthTest, transparent, opacity}} opts
18
*/
19
bkcore.threejs.Particles = function(opts)
20
{
21
this.black = new THREE.Color(0x000000);
22
this.white = new THREE.Color(0xffffff);
23
24
this.material = new THREE.ParticleBasicMaterial({
25
color: opts.tint == undefined ? 0xffffff : opts.tint,
26
map: opts.texture == undefined ? null : opts.texture,
27
size: opts.size == undefined ? 4 : opts.size,
28
blending: opts.blending == undefined ? THREE.AdditiveBlending : opts.blending,
29
depthTest: opts.depthTest == undefined ? false : opts.depthTest,
30
transparent: opts.transparent == undefined ? true : opts.transparent,
31
vertexColors: true,
32
opacity: opts.opacity == undefined ? 1.0 : opts.opacity,
33
sizeAttenuation: true
34
});
35
36
this.max = opts.max == undefined ? 1000 : opts.max;
37
this.spawnRate = opts.spawnRate == undefined ? 0 : opts.spawnRate;
38
39
this.spawn = opts.spawn == undefined ? new THREE.Vector3() : opts.spawn;
40
this.velocity = opts.velocity == undefined ? new THREE.Vector3() : opts.velocity;
41
this.randomness = opts.randomness == undefined ? new THREE.Vector3() : opts.randomness;
42
this.force = opts.force == undefined ? new THREE.Vector3() : opts.force;
43
this.spawnRadius = opts.spawnRadius == undefined ? new THREE.Vector3() : opts.spawnRadius;
44
this.life = opts.life == undefined ? 60 : opts.life;
45
this.ageing = 1 / this.life;
46
this.friction = opts.friction == undefined ? 1.0 : opts.friction;
47
this.color = new THREE.Color(opts.color == undefined ? 0xffffff : opts.color);
48
this.color2 = opts.color2 == undefined ? null : new THREE.Color(opts.color2);
49
50
this.position = opts.position == undefined ? new THREE.Vector3() : opts.position;
51
this.rotation = opts.rotation == undefined ? new THREE.Vector3() : opts.rotation;
52
this.sort = opts.sort == undefined ? false : opts.sort;
53
54
this.pool = [];
55
this.buffer = [];
56
this.geometry = null;
57
this.system = null;
58
59
this.build();
60
}
61
62
bkcore.threejs.Particles.prototype.build = function()
63
{
64
this.geometry = new THREE.Geometry();
65
this.geometry.dynamic = true;
66
67
this.pool = [];
68
this.buffer = [];
69
70
for(var i = 0; i < this.max; ++i)
71
{
72
var p = new bkcore.threejs.Particle();
73
this.pool.push(p);
74
this.buffer.push(p);
75
this.geometry.vertices.push(p.position);
76
this.geometry.colors.push(p.color);
77
}
78
79
this.system = new THREE.ParticleSystem(this.geometry, this.material);
80
this.system.position = this.position;
81
this.system.rotation = this.rotation;
82
this.system.sort = this.sort;
83
}
84
85
/**
86
* Emits given number of particles
87
* @param int count
88
*/
89
bkcore.threejs.Particles.prototype.emit = function(count)
90
{
91
var emitable = Math.min(count, this.pool.length);
92
for(var i = 0; i < emitable; ++i)
93
{
94
var p = this.pool.pop();
95
p.available = false;
96
p.position.copy(this.spawn)
97
.addSelf(
98
this.randomVector()
99
.multiplySelf(this.spawnRadius)
100
);
101
p.velocity.copy(this.velocity)
102
.addSelf(
103
this.randomVector()
104
.multiplySelf(this.randomness)
105
);
106
p.force.copy(this.force);
107
p.basecolor.copy(this.color);
108
if(this.color2 != undefined) p.basecolor.lerpSelf(this.color2, Math.random());
109
p.life = 1.0;
110
}
111
}
112
113
bkcore.threejs.Particles.prototype.randomVector = function()
114
{
115
return new THREE.Vector3(
116
Math.random()*2-1,
117
Math.random()*2-1,
118
Math.random()*2-1
119
);
120
}
121
122
/**
123
* Updates particles (should be call in a RAF loop)
124
* @param float dt time delta ~1.0
125
*/
126
bkcore.threejs.Particles.prototype.update = function(dt)
127
{
128
var p, l;
129
var df = new THREE.Vector3();
130
var dv = new THREE.Vector3();
131
for(var i = 0; i < this.buffer.length; ++i)
132
{
133
134
p = this.buffer[i];
135
136
if(p.available) continue;
137
138
p.life -= this.ageing;
139
140
if(p.life <= 0 && !p.available)
141
{
142
p.reset();
143
this.pool.push(p);
144
continue;
145
}
146
147
l = p.life > 0.5 ? 1.0 : p.life + 0.5;
148
p.color.setRGB(
149
l * p.basecolor.r,
150
l * p.basecolor.g,
151
l * p.basecolor.b
152
);
153
154
if(this.friction != 1.0)
155
p.velocity.multiplyScalar(this.friction);
156
157
df.copy(p.force).multiplyScalar(dt);
158
p.velocity.addSelf(df);
159
160
dv.copy(p.velocity).multiplyScalar(dt);
161
p.position.addSelf(dv);
162
}
163
164
if(this.spawnRate > 0)
165
this.emit(this.spawnRate);
166
167
this.geometry.verticesNeedUpdate = true;
168
this.geometry.colorsNeedUpdate = true;
169
}
170
171
bkcore.threejs.Particle = function()
172
{
173
this.position = new THREE.Vector3(-10000,-10000,-10000);
174
this.velocity = new THREE.Vector3();
175
this.force = new THREE.Vector3();
176
this.color = new THREE.Color(0x000000);
177
this.basecolor = new THREE.Color(0x000000);
178
this.life = 0.0;
179
this.available = true;
180
}
181
182
bkcore.threejs.Particle.prototype.reset = function()
183
{
184
this.position.set(0,-100000,0);
185
this.velocity.set(0,0,0);
186
this.force.set(0,0,0);
187
this.color.setRGB(0,0,0);
188
this.basecolor.setRGB(0,0,0);
189
this.life = 0.0;
190
this.available = true;
191
}
192