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