-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticle.js
More file actions
423 lines (351 loc) · 11.9 KB
/
Copy pathParticle.js
File metadata and controls
423 lines (351 loc) · 11.9 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
import { Vector3 } from '../core/math/Vector3.js';
import { Color } from '../core/math/Color.js';
/**
* Individual particle class
* Manages particle state, physics, and lifecycle
*/
export class Particle {
constructor(index, particleSystem) {
this.index = index;
this.particleSystem = particleSystem;
// Particle properties
this.position = new Vector3();
this.velocity = new Vector3();
this.acceleration = new Vector3();
this.size = new Vector3(1, 1, 1);
this.color = new Color();
// Lifecycle
this.age = 0;
this.lifetime = 1;
this.isAlive = false;
// Physics
this.rotation = 0;
this.rotationSpeed = 0;
this.mass = 1;
this.drag = 1;
this.bounce = 0;
// Forces applied to this particle
this.forces = [];
// Custom user data
this.userData = {};
// Reference to emitter that spawned this particle
this.emitter = null;
// Performance tracking
this.updateCount = 0;
}
/**
* Reset particle to initial state
*/
reset(position, velocity, color, size, lifetime, emitter) {
this.position.copy(position);
this.velocity.copy(velocity);
this.acceleration.set(0, 0, 0);
if (size instanceof Vector3) {
this.size.copy(size);
} else {
this.size.set(size, size, size);
}
this.color.copy(color);
this.age = 0;
this.lifetime = lifetime;
this.isAlive = true;
this.rotation = 0;
this.rotationSpeed = 0;
this.mass = emitter ? emitter.mass || 1 : 1;
this.drag = emitter ? emitter.drag || 1 : 1;
this.bounce = emitter ? emitter.bounce || 0 : 0;
this.forces = [];
this.userData = {};
this.emitter = emitter;
this.updateCount = 0;
}
/**
* Reset particle to inactive state
*/
reset() {
this.isAlive = false;
this.age = 0;
this.lifetime = 0;
this.forces = [];
this.userData = {};
this.emitter = null;
}
/**
* Update particle physics and state
*/
update(deltaTime) {
if (!this.isAlive) return;
this.updateCount++;
this.age += deltaTime;
// Check if particle should die
if (this.age >= this.lifetime) {
this.die();
return;
}
// Apply forces
this.applyForces(deltaTime);
// Update physics
this.updatePhysics(deltaTime);
// Update rotation
this.updateRotation(deltaTime);
// Update GPU buffer data
this.updateGPUData();
}
/**
* Apply all forces to the particle
*/
applyForces(deltaTime) {
// Clear acceleration
this.acceleration.set(0, 0, 0);
// Apply custom forces
for (const force of this.forces) {
this.acceleration.add(force.clone().multiplyScalar(this.mass));
}
// Apply emitter acceleration
if (this.emitter && this.emitter.acceleration) {
this.acceleration.add(this.emitter.acceleration);
}
// Apply particle system gravity
if (this.particleSystem.gravity) {
this.acceleration.add(this.particleSystem.gravity);
}
// Apply wind
if (this.particleSystem.wind) {
this.acceleration.add(this.particleSystem.wind);
}
// Apply global velocity as acceleration
if (this.particleSystem.globalVelocity) {
const globalVel = this.particleSystem.globalVelocity.clone();
this.acceleration.add(globalVel.multiplyScalar(0.1)); // Mild acceleration
}
}
/**
* Update particle physics (position, velocity)
*/
updatePhysics(deltaTime) {
// Update velocity with acceleration
this.velocity.add(this.acceleration.clone().multiplyScalar(deltaTime));
// Apply drag
const dragFactor = Math.pow(this.drag, deltaTime * 60); // Frame-rate independent drag
this.velocity.multiplyScalar(dragFactor);
// Apply particle system global drag
if (this.particleSystem.drag) {
const systemDrag = Math.pow(this.particleSystem.drag, deltaTime * 60);
this.velocity.multiplyScalar(systemDrag);
}
// Update position
this.position.add(this.velocity.clone().multiplyScalar(deltaTime));
// Handle collisions if enabled
if (this.particleSystem.enableCollisions) {
this.handleCollisions();
}
}
/**
* Update particle rotation
*/
updateRotation(deltaTime) {
this.rotation += this.rotationSpeed * deltaTime;
// Keep rotation in reasonable range
if (this.rotation > Math.PI * 2) {
this.rotation -= Math.PI * 2;
} else if (this.rotation < 0) {
this.rotation += Math.PI * 2;
}
}
/**
* Handle collisions with other particles or environment
*/
handleCollisions() {
// Get nearby particles using spatial grid
const nearbyParticles = this.particleSystem.getParticlesInRegion(this.position, 5);
for (const other of nearbyParticles) {
if (other === this || !other.isAlive) continue;
const distance = this.position.distanceTo(other.position);
const collisionDistance = this.getCollisionRadius() + other.getCollisionRadius();
if (distance < collisionDistance) {
this.resolveCollision(other, distance);
}
}
}
/**
* Resolve collision with another particle
*/
resolveCollision(other, distance) {
// Simple elastic collision
const normal = this.position.clone().sub(other.position).normalize();
const relativeVelocity = this.velocity.clone().sub(other.velocity);
const velocityAlongNormal = relativeVelocity.dot(normal);
if (velocityAlongNormal > 0) return; // Already separating
const restitution = 0.5; // Bounciness
const impulse = -(1 + restitution) * velocityAlongNormal;
const impulseVector = normal.clone().multiplyScalar(impulse / this.mass);
this.velocity.add(impulseVector);
other.velocity.sub(impulseVector.clone().multiplyScalar(other.mass / this.mass));
// Separate particles
const separation = (this.getCollisionRadius() + other.getCollisionRadius() - distance) * 0.5;
this.position.add(normal.clone().multiplyScalar(separation));
other.position.sub(normal.clone().multiplyScalar(separation));
}
/**
* Get collision radius based on particle size
*/
getCollisionRadius() {
return Math.max(this.size.x, this.size.y, this.size.z) * 0.5;
}
/**
* Update GPU buffer data for this particle
*/
updateGPUData() {
const index = this.index;
const posOffset = index * 3;
const colOffset = index * 4;
// Update position and scale in GPU buffers
this.particleSystem.positions[posOffset] = this.position.x;
this.particleSystem.positions[posOffset + 1] = this.position.y;
this.particleSystem.positions[posOffset + 2] = this.position.z;
this.particleSystem.scales[posOffset] = this.size.x;
this.particleSystem.scales[posOffset + 1] = this.size.y;
this.particleSystem.scales[posOffset + 2] = this.size.z;
// Update color with life-based interpolation
this.updateColor();
this.particleSystem.colors[colOffset] = this.color.r;
this.particleSystem.colors[colOffset + 1] = this.color.g;
this.particleSystem.colors[colOffset + 2] = this.color.b;
this.particleSystem.colors[colOffset + 3] = this.color.a || 1.0;
// Update rotation and age
this.particleSystem.rotationAngles[index] = this.rotation;
this.particleSystem.ages[index] = this.age;
}
/**
* Update particle color based on life progress
*/
updateColor() {
if (!this.emitter) return;
const lifeProgress = this.age / this.lifetime;
// Interpolate color
const startColor = this.emitter.colorStart;
const endColor = this.emitter.colorEnd;
this.color.copy(startColor).lerp(endColor, lifeProgress);
// Interpolate alpha
const alpha = this.emitter.alphaStart + (this.emitter.alphaEnd - this.emitter.alphaStart) * lifeProgress;
this.color.alpha = Math.max(0, Math.min(1, alpha));
}
/**
* Add a force to this particle
*/
addForce(force) {
if (force instanceof Vector3) {
this.forces.push(force.clone());
}
}
/**
* Remove a force from this particle
*/
removeForce(force) {
const index = this.forces.indexOf(force);
if (index !== -1) {
this.forces.splice(index, 1);
}
}
/**
* Clear all forces
*/
clearForces() {
this.forces = [];
}
/**
* Apply velocity change
*/
addVelocity(velocity) {
this.velocity.add(velocity);
}
/**
* Apply impulse (instantaneous velocity change)
*/
applyImpulse(impulse) {
this.velocity.add(impulse.clone().multiplyScalar(this.mass));
}
/**
* Set particle size
*/
setSize(size) {
if (size instanceof Vector3) {
this.size.copy(size);
} else {
this.size.set(size, size, size);
}
}
/**
* Set particle color
*/
setColor(color) {
this.color.copy(color);
}
/**
* Kill this particle
*/
die() {
this.isAlive = false;
this.particleSystem.killParticle(this);
}
/**
* Respawn this particle (reset with new values)
*/
respawn(position, velocity, color, size, lifetime, emitter) {
this.reset(position, velocity, color, size, lifetime, emitter);
}
/**
* Get particle life progress (0-1)
*/
getLifeProgress() {
return Math.min(1, this.age / this.lifetime);
}
/**
* Get remaining life time
*/
getRemainingLife() {
return Math.max(0, this.lifetime - this.age);
}
/**
* Check if particle is still alive
*/
isDead() {
return !this.isAlive || this.age >= this.lifetime;
}
/**
* Clone this particle (for advanced effects)
*/
clone() {
const cloned = new Particle(this.index, this.particleSystem);
cloned.position.copy(this.position);
cloned.velocity.copy(this.velocity);
cloned.acceleration.copy(this.acceleration);
cloned.size.copy(this.size);
cloned.color.copy(this.color);
cloned.rotation = this.rotation;
cloned.rotationSpeed = this.rotationSpeed;
cloned.lifetime = this.lifetime;
cloned.emitter = this.emitter;
return cloned;
}
/**
* Get particle statistics
*/
getStats() {
return {
index: this.index,
age: this.age,
lifetime: this.lifetime,
lifeProgress: this.getLifeProgress(),
position: this.position.clone(),
velocity: this.velocity.clone(),
size: this.size.clone(),
color: this.color.clone(),
rotation: this.rotation,
rotationSpeed: this.rotationSpeed,
isAlive: this.isAlive,
forces: this.forces.length,
updateCount: this.updateCount
};
}
}