-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticleMaterial.js
More file actions
472 lines (385 loc) · 16.3 KB
/
Copy pathParticleMaterial.js
File metadata and controls
472 lines (385 loc) · 16.3 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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
import { Material } from '../core/Material.js';
import { Shader } from '../core/Shader.js';
// Simple Texture placeholder
class Texture {
constructor() {
this.image = null;
this.needsUpdate = false;
}
}
/**
* Custom shader material for particle rendering
* Supports GPU instancing, sprite rendering, and custom effects
*/
export class ParticleMaterial extends Material {
constructor(parameters = {}) {
super();
// Default shader uniforms
this.uniforms = {
time: { value: 0 },
particleSize: { value: 1.0 },
perspectiveScale: { value: 1.0 },
colorMap: { value: null },
alphaMap: { value: null },
blending: { value: parameters.blending || 'additive' },
opacity: { value: parameters.opacity || 1.0 }
};
// Merge with provided parameters
Object.assign(this.uniforms, parameters.uniforms || {});
// Default material properties
this.transparent = parameters.transparent !== false;
this.depthWrite = parameters.depthWrite !== undefined ? parameters.depthWrite : false;
this.depthTest = parameters.depthTest !== undefined ? parameters.depthTest : true;
this.opacity = parameters.opacity || 1.0;
this.blending = parameters.blending;
// GPU instancing support
this.vertexColors = parameters.vertexColors !== false;
this.sizeAttenuation = parameters.sizeAttenuation !== false;
// Custom shaders
this.vertexShader = parameters.vertexShader || this.getDefaultVertexShader();
this.fragmentShader = parameters.fragmentShader || this.getDefaultFragmentShader();
// Compile the shader
this.needsUpdate = true;
}
/**
* Default vertex shader for particle rendering
*/
getDefaultVertexShader() {
return `
attribute vec3 instancePosition;
attribute vec3 instanceScale;
attribute vec3 instanceVelocity;
attribute vec3 instanceAcceleration;
attribute vec4 instanceColor;
attribute float instanceLifetime;
attribute float instanceAge;
attribute float instanceRotation;
attribute float instanceRotationSpeed;
attribute float instanceTextureIndex;
attribute float instanceActive;
uniform float time;
uniform float particleSize;
uniform float perspectiveScale;
varying vec2 vUv;
varying vec4 vColor;
varying float vAge;
varying float vLifetime;
varying vec3 vVelocity;
void main() {
// Only render active particles
if (instanceActive < 0.5) {
gl_Position = vec4(2.0, 2.0, 2.0, 2.0); // Move off-screen
return;
}
// Pass UV coordinates
vUv = uv;
// Pass color and life data
vColor = instanceColor;
vAge = instanceAge;
vLifetime = instanceLifetime;
vVelocity = instanceVelocity;
// Calculate age progress (0-1)
float ageProgress = instanceAge / instanceLifetime;
// Apply rotation
float rotation = instanceRotation + instanceRotationSpeed * time;
mat2 rotationMatrix = mat2(
cos(rotation), -sin(rotation),
sin(rotation), cos(rotation)
);
vec2 rotatedPosition = rotationMatrix * position.xy;
// Scale particle based on size and life
float lifeScale = 1.0;
// Optional: size fade based on age
// lifeScale *= (1.0 - ageProgress * 0.5); // Fade to 50% at end of life
// Optional: velocity-based scaling
float velocityMagnitude = length(instanceVelocity);
lifeScale *= (1.0 + velocityMagnitude * 0.1);
vec2 scaledPosition = rotatedPosition * instanceScale.xy * particleSize * lifeScale;
// Particle billboard rotation to face camera
vec4 worldPosition = vec4(instancePosition + vec3(scaledPosition, 0.0), 1.0);
vec4 mvPosition = modelViewMatrix * worldPosition;
// Size attenuation for perspective
if (sizeAttenuation) {
gl_PointSize = particleSize * instanceScale.x * lifeScale * (perspectiveScale / -mvPosition.z);
} else {
gl_PointSize = particleSize * instanceScale.x * lifeScale;
}
// Pass age progress to fragment shader
vAge = ageProgress;
gl_Position = projectionMatrix * mvPosition;
}
`;
}
/**
* Default fragment shader for particle rendering
*/
getDefaultFragmentShader() {
return `
uniform sampler2D colorMap;
uniform sampler2D alphaMap;
uniform float time;
uniform float opacity;
varying vec2 vUv;
varying vec4 vColor;
varying float vAge;
varying float vLifetime;
varying vec3 vVelocity;
void main() {
// Get base color from texture or use vertex color
vec4 texColor = texture2D(colorMap, vUv);
vec4 finalColor = texColor;
// Apply vertex color
finalColor *= vColor;
// Apply alpha texture if available
if (alphaMap != 0) {
float alphaValue = texture2D(alphaMap, vUv).r;
finalColor.a *= alphaValue;
}
// Life-based fade
float lifeFactor = 1.0 - vAge;
finalColor.a *= lifeFactor;
// Velocity-based effects
float velocityGlow = length(vVelocity) * 0.1;
finalColor.rgb += vec3(velocityGlow);
// Alpha test for soft particles
if (finalColor.a < 0.01) {
discard;
}
gl_FragColor = vec4(finalColor.rgb, finalColor.a * opacity);
}
`;
}
/**
* Fire/Explosion shader
*/
getFireShader() {
return {
vertexShader: this.getDefaultVertexShader(),
fragmentShader: `
uniform float time;
uniform sampler2D noiseMap;
varying vec2 vUv;
varying vec4 vColor;
varying float vAge;
varying vec3 vVelocity;
float random(vec2 st) {
return fract(sin(dot(st.xy, vec2(12.9898, 78.233))) * 43758.5453123);
}
float noise(vec2 st) {
vec2 i = floor(st);
vec2 f = fract(st);
float a = random(i);
float b = random(i + vec2(1.0, 0.0));
float c = random(i + vec2(0.0, 1.0));
float d = random(i + vec2(1.0, 1.0));
vec2 u = f * f * (3.0 - 2.0 * f);
return mix(a, b, u.x) +
(c - a) * u.y * (1.0 - u.x) +
(d - b) * u.x * u.y;
}
void main() {
// Create fire-like gradient
vec2 center = vUv - 0.5;
float dist = length(center);
float radial = 1.0 - smoothstep(0.0, 0.5, dist);
// Fire colors based on age and height
vec3 fireColor;
if (radial > 0.7) {
fireColor = vec3(1.0, 0.8, 0.2); // Yellow
} else if (radial > 0.4) {
fireColor = vec3(1.0, 0.4, 0.1); // Orange
} else {
fireColor = vec3(1.0, 0.1, 0.0); // Red
}
// Add noise for fire flicker
float noiseValue = noise(vUv * 10.0 + time * 2.0);
fireColor *= (0.5 + noiseValue * 0.5);
// Alpha based on radial falloff and age
float alpha = radial * (1.0 - vAge);
alpha *= (0.5 + noiseValue * 0.5);
// Apply vertex color
fireColor *= vColor.rgb;
if (alpha < 0.01) {
discard;
}
gl_FragColor = vec4(fireColor, alpha);
}
`
};
}
/**
* Water/Smoke shader
*/
getWaterShader() {
return {
vertexShader: this.getDefaultVertexShader(),
fragmentShader: `
uniform float time;
uniform sampler2D noiseMap;
varying vec2 vUv;
varying vec4 vColor;
varying float vAge;
varying vec3 vVelocity;
void main() {
// Create smooth, flowing appearance
vec2 distortedUv = vUv + vVelocity.xy * 0.1 * time;
float noiseValue = texture2D(noiseMap, distortedUv).r;
float smoothNoise = smoothstep(0.3, 0.7, noiseValue);
// Water-like blue color
vec3 waterColor = vec3(0.2, 0.5, 0.8);
waterColor += vec3(0.1) * smoothNoise;
// Alpha with soft edges
float alpha = (1.0 - vAge) * smoothNoise;
alpha *= (1.0 - length(vUv - 0.5));
// Apply vertex color
waterColor *= vColor.rgb;
if (alpha < 0.01) {
discard;
}
gl_FragColor = vec4(waterColor, alpha);
}
`
};
}
/**
* Spark/Trail shader
*/
getSparkShader() {
return {
vertexShader: this.getDefaultVertexShader(),
fragmentShader: `
uniform float time;
varying vec2 vUv;
varying vec4 vColor;
varying float vAge;
varying vec3 vVelocity;
void main() {
// Create elongated spark shape
vec2 center = vUv - 0.5;
center.x *= 2.0; // Make sparks longer
float dist = length(center);
float spark = 1.0 - smoothstep(0.0, 0.5, dist);
// Bright white/yellow core
vec3 sparkColor = vec3(1.0, 0.9, 0.6);
sparkColor *= 2.0; // Make it bright
// Alpha based on shape and life
float alpha = spark * (1.0 - vAge * 0.5);
// Apply vertex color
sparkColor *= vColor.rgb;
if (alpha < 0.01) {
discard;
}
gl_FragColor = vec4(sparkColor, alpha);
}
`
};
}
/**
* Magic/Particle effects shader
*/
getMagicShader() {
return {
vertexShader: this.getDefaultVertexShader(),
fragmentShader: `
uniform float time;
uniform sampler2D noiseMap;
varying vec2 vUv;
varying vec4 vColor;
varying float vAge;
varying vec3 vVelocity;
// HSL to RGB conversion
vec3 hsl2rgb(vec3 hsl) {
vec3 rgb = clamp(abs(mod(hsl.x*6.0 + vec3(0,4,2), 6.0)-3.0)-1.0, 0.0, 1.0);
rgb = rgb*rgb*(3.0-2.0*rgb);
return hsl.z + hsl.y*(rgb-0.5)*(1.0-abs(2.0*hsl.z-1.0));
}
void main() {
// Create magical particle effect
float dist = length(vUv - 0.5);
// Magic circle pattern
float angle = atan(vUv.y - 0.5, vUv.x - 0.5);
float circlePattern = sin(angle * 8.0 + time * 3.0) * 0.5 + 0.5;
// Magic color based on time and position
float hue = fract(time * 0.2 + dist * 2.0 + circlePattern * 0.3);
vec3 magicColor = hsl2rgb(vec3(hue, 0.8, 0.6));
// Add sparkle effect
float sparkle = step(0.95, fract(sin(vUv.x * 100.0 + time * 50.0) * 43758.5453));
magicColor += vec3(sparkle) * 2.0;
// Alpha with magic glow
float alpha = (1.0 - dist) * (1.0 - vAge) * 0.8;
alpha += sparkle * 0.5;
// Apply vertex color
magicColor *= vColor.rgb;
if (alpha < 0.01) {
discard;
}
gl_FragColor = vec4(magicColor, alpha);
}
`
};
}
/**
* Apply custom shader effects
*/
applyShader(shaderType) {
switch (shaderType) {
case 'fire':
const fireShader = this.getFireShader();
this.vertexShader = fireShader.vertexShader;
this.fragmentShader = fireShader.fragmentShader;
break;
case 'water':
const waterShader = this.getWaterShader();
this.vertexShader = waterShader.vertexShader;
this.fragmentShader = waterShader.fragmentShader;
break;
case 'spark':
const sparkShader = this.getSparkShader();
this.vertexShader = sparkShader.vertexShader;
this.fragmentShader = sparkShader.fragmentShader;
break;
case 'magic':
const magicShader = this.getMagicShader();
this.vertexShader = magicShader.vertexShader;
this.fragmentShader = magicShader.fragmentShader;
break;
}
this.needsUpdate = true;
}
/**
* Set particle size
*/
setParticleSize(size) {
this.uniforms.particleSize.value = size;
}
/**
* Set texture maps
*/
setTextures(colorMap, alphaMap) {
if (colorMap) this.uniforms.colorMap.value = colorMap;
if (alphaMap) this.uniforms.alphaMap.value = alphaMap;
}
/**
* Set blending mode
*/
setBlending(mode) {
this.blending = mode;
}
/**
* Clone this material
*/
clone() {
const material = new ParticleMaterial({
uniforms: this.uniforms,
vertexShader: this.vertexShader,
fragmentShader: this.fragmentShader,
transparent: this.transparent,
depthWrite: this.depthWrite,
depthTest: this.depthTest,
blending: this.blending,
vertexColors: this.vertexColors,
sizeAttenuation: this.sizeAttenuation
});
return material;
}
}