-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.ts
375 lines (324 loc) · 12.4 KB
/
index.ts
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
import {
Mesh,
InstancedBufferGeometry,
PlaneBufferGeometry,
ShaderMaterial,
Vector3,
Color,
InstancedBufferAttribute,
MathUtils,
AddEquation,
Texture,
BufferAttribute,
RawShaderMaterial,
Matrix4,
UniformsUtils,
UniformsLib,
DynamicDrawUsage
} from "three";
import * as EasingFunctions from "@mozillareality/easing-functions";
export function lerp(start: number, end: number, value: number) {
return (end - start) * value + start;
}
export function clamp(min: number, max: number, value: number) {
if (value < min) {
value = min;
}
if (value > max) {
value = max;
}
return value;
}
function flipV(geometry: PlaneBufferGeometry) {
// Three.js seems to assume texture flipY is true for all its built in geometry
// but we turn this off on our texture loader since createImageBitmap in Firefox
// does not support flipping. Then we flip the v of uv for flipY = false texture.
// @TODO: There is a similar function in Hubs client core. Should we reuse it?
const uv = geometry.getAttribute("uv");
for (let i = 0; i < uv.count; i++) {
uv.setY(i, 1.0 - uv.getY(i));
}
return geometry;
}
const vertexShader = `
#include <common>
attribute vec4 particlePosition;
attribute vec4 particleColor;
attribute float particleAngle;
varying vec4 vColor;
varying vec2 vUV;
uniform mat4 emitterMatrix;
#include <fog_pars_vertex>
void main() {
vUV = uv;
vColor = particleColor;
float particleScale = particlePosition.w;
vec4 mvPosition = viewMatrix * emitterMatrix * vec4(particlePosition.xyz, 1.0);
vec3 rotatedPosition = position;
rotatedPosition.x = cos( particleAngle ) * position.x - sin( particleAngle ) * position.y;
rotatedPosition.y = sin( particleAngle ) * position.x + cos( particleAngle ) * position.y;
mvPosition.xyz += rotatedPosition * particleScale;
gl_Position = projectionMatrix * mvPosition;
#include <fog_vertex>
}
`;
const fragmentShader = `
#include <common>
#include <fog_pars_fragment>
uniform sampler2D map;
varying vec2 vUV;
varying vec4 vColor;
void main() {
gl_FragColor = texture2D(map, vUV) * vColor;
#include <fog_fragment>
}
`;
interface ParticleEmitterGeometry extends InstancedBufferGeometry {
attributes: {
position: BufferAttribute;
uv: BufferAttribute;
particlePosition: InstancedBufferAttribute;
particleColor: InstancedBufferAttribute;
particleAngle: InstancedBufferAttribute;
}
}
export class ParticleEmitter extends Mesh {
initialPositions: number[];
initialAges: number[];
startSize: number;
endSize: number;
sizeRandomness: number;
startVelocity: Vector3;
endVelocity: Vector3;
angularVelocity: number;
particleCount: number;
lifetime: number;
lifetimes: number[];
lifetimeRandomness: number;
particleSizeRandomness: number[];
ageRandomness: number;
ages: number[];
colors: number[];
endColor: Color;
middleColor: Color;
startColor: Color;
startOpacity: number;
middleOpacity: number;
endOpacity: number;
colorCurve: string;
velocityCurve: string;
sizeCurve: string;
worldScale: Vector3;
inverseWorldScale: Vector3;
constructor(texture: Texture) {
const planeGeometry = new PlaneBufferGeometry(1, 1, 1, 1);
if (texture && !texture.flipY) {
flipV(planeGeometry);
}
const geometry = new InstancedBufferGeometry();
geometry.index = planeGeometry.index;
geometry.attributes = planeGeometry.attributes;
const material = new ShaderMaterial({
uniforms: UniformsUtils.merge([{
map: { value: texture },
emitterMatrix: { value: new Matrix4() }
}, UniformsLib.fog]),
vertexShader,
fragmentShader,
transparent: true,
depthWrite: false,
// TODO: Resolve the root issue. fog property seems to have
// been removed from Three.js ShaderMaterial at some point.
// @ts-ignore
fog: true,
blendEquation: AddEquation
});
super(geometry, material);
this.frustumCulled = false;
this.initialPositions = [];
this.startSize = 0.25;
this.endSize = 0.25;
this.sizeRandomness = 0;
this.startVelocity = new Vector3(0, 0, 0.5);
this.endVelocity = new Vector3(0, 0, 0.5);
this.angularVelocity = 0;
this.particleCount = 100;
this.lifetime = 5;
this.lifetimes = [];
this.lifetimeRandomness = 5;
this.particleSizeRandomness = [];
this.ageRandomness = 10;
this.initialAges = [];
this.ages = [];
this.colors = [];
this.endColor = new Color();
this.middleColor = new Color();
this.startColor = new Color();
this.startOpacity = 1;
this.middleOpacity = 1;
this.endOpacity = 1;
this.colorCurve = "linear";
this.velocityCurve = "linear";
this.sizeCurve = "linear";
this.worldScale = new Vector3();
this.inverseWorldScale = new Vector3();
this.updateParticles();
}
updateParticles() {
const texture = (this.material as ShaderMaterial).uniforms.map.value;
const planeGeometry = new PlaneBufferGeometry(1, 1, 1, 1);
if (texture && !texture.flipY) {
flipV(planeGeometry);
}
const tempGeo = new InstancedBufferGeometry();
tempGeo.index = planeGeometry.index;
tempGeo.attributes = planeGeometry.attributes;
const positions = [];
const colors = [];
const lifetimes = [];
const ages = [];
const initialAges = [];
const initialPositions = [];
const particleSizeRandomness = [];
const angles = [];
this.getWorldScale(this.worldScale);
for (let i = 0; i < this.particleCount; i++) {
initialAges[i] = Math.random() * this.ageRandomness - this.ageRandomness;
lifetimes[i] = this.lifetime + Math.random() * 2 * this.lifetimeRandomness;
ages[i] = initialAges[i];
initialPositions[i * 3] = Math.random() * 2 - 1; // X
initialPositions[i * 3 + 1] = Math.random() * 2 - 1; // Y
initialPositions[i * 3 + 2] = 0; // Z
particleSizeRandomness[i] = Math.random() * this.sizeRandomness;
positions.push(initialPositions[i * 3] * this.worldScale.x);
positions.push(initialPositions[i * 3 + 1] * this.worldScale.y);
positions.push(initialPositions[i * 3 + 2]);
positions.push(this.startSize + particleSizeRandomness[i]);
angles.push(0);
colors.push(this.startColor.r, this.startColor.g, this.startColor.b, 0);
}
tempGeo.setAttribute(
"particlePosition",
new InstancedBufferAttribute(new Float32Array(positions), 4).setUsage(DynamicDrawUsage)
);
tempGeo.setAttribute("particleColor", new InstancedBufferAttribute(new Float32Array(colors), 4).setUsage(DynamicDrawUsage));
tempGeo.setAttribute("particleAngle", new InstancedBufferAttribute(new Float32Array(angles), 1).setUsage(DynamicDrawUsage));
this.geometry = tempGeo as ParticleEmitterGeometry;
this.initialPositions = initialPositions;
this.particleSizeRandomness = particleSizeRandomness;
this.ages = ages;
this.initialAges = initialAges;
this.lifetimes = lifetimes;
this.colors = colors;
}
update(dt: number) {
const geometry = this.geometry as ParticleEmitterGeometry;
const particlePosition = geometry.attributes.particlePosition.array as Float32Array;
const particleColor = geometry.attributes.particleColor.array as Float32Array;
const particleAngle = geometry.attributes.particleAngle.array as Float32Array;
this.getWorldScale(this.worldScale);
this.inverseWorldScale.set(
1 / this.worldScale.x,
1 / this.worldScale.y,
1 / this.worldScale.z
);
const material = this.material as ShaderMaterial;
const emitterMatrix: Matrix4 = material.uniforms.emitterMatrix.value;
emitterMatrix.copy(this.matrixWorld);
emitterMatrix.scale(this.inverseWorldScale);
for (let i = 0; i < this.particleCount; i++) {
const prevAge = this.ages[i];
const curAge = (this.ages[i] += dt);
// Particle is dead
if (curAge < 0) {
continue;
}
// // Particle became alive
if (curAge > 0 && prevAge <= 0) {
particleColor[i * 4 + 3] = this.startOpacity;
particlePosition[i * 4] = this.initialPositions[i * 3] * this.worldScale.x;
particlePosition[i * 4 + 1] = this.initialPositions[i * 3 + 1] * this.worldScale.y;
particlePosition[i * 4 + 2] = 0;
particlePosition[i * 4 + 3] = this.startSize + this.particleSizeRandomness[i];
particleColor[i * 4] = this.startColor.r;
particleColor[i * 4 + 1] = this.startColor.g;
particleColor[i * 4 + 2] = this.startColor.b;
continue;
}
// Particle died
if (curAge > this.lifetimes[i]) {
this.ages[i] = this.initialAges[i];
particleColor[i * 4 + 3] = 0; // Set opacity to zero
continue;
}
const normalizedAge = clamp(0, 1, this.ages[i] / this.lifetimes[i]);
const _EasingFunctions = EasingFunctions as { [name: string]: (k: number) => number };
if (!_EasingFunctions[this.velocityCurve]) {
console.warn(`Unknown velocity curve type ${this.velocityCurve} in particle emitter. Falling back to linear.`)
this.velocityCurve = "linear";
}
if (!_EasingFunctions[this.sizeCurve]) {
console.warn(`Unknown size curve type ${this.sizeCurve} in particle emitter. Falling back to linear.`)
this.sizeCurve = "linear";
}
if (!_EasingFunctions[this.colorCurve]) {
console.warn(`Unknown color curve type ${this.colorCurve} in particle emitter. Falling back to linear.`)
this.colorCurve = "linear";
}
const velFactor = _EasingFunctions[this.velocityCurve](normalizedAge);
const sizeFactor = _EasingFunctions[this.sizeCurve](normalizedAge);
const colorFactor = _EasingFunctions[this.colorCurve](normalizedAge);
particlePosition[i * 4] += lerp(this.startVelocity.x, this.endVelocity.x, velFactor) * dt;
particlePosition[i * 4 + 1] += lerp(this.startVelocity.y, this.endVelocity.y, velFactor) * dt;
particlePosition[i * 4 + 2] += lerp(this.startVelocity.z, this.endVelocity.z, velFactor) * dt;
particlePosition[i * 4 + 3] = lerp(
this.startSize + this.particleSizeRandomness[i],
this.endSize + this.particleSizeRandomness[i],
sizeFactor
);
particleAngle[i] += this.angularVelocity * MathUtils.DEG2RAD * dt;
if (colorFactor <= 0.5) {
const colorFactor1 = colorFactor / 0.5;
particleColor[i * 4] = lerp(this.startColor.r, this.middleColor.r, colorFactor1);
particleColor[i * 4 + 1] = lerp(this.startColor.g, this.middleColor.g, colorFactor1);
particleColor[i * 4 + 2] = lerp(this.startColor.b, this.middleColor.b, colorFactor1);
particleColor[i * 4 + 3] = lerp(this.startOpacity, this.middleOpacity, colorFactor1);
} else if (colorFactor > 0.5) {
const colorFactor2 = (colorFactor - 0.5) / 0.5;
particleColor[i * 4] = lerp(this.middleColor.r, this.endColor.r, colorFactor2);
particleColor[i * 4 + 1] = lerp(this.middleColor.g, this.endColor.g, colorFactor2);
particleColor[i * 4 + 2] = lerp(this.middleColor.b, this.endColor.b, colorFactor2);
particleColor[i * 4 + 3] = lerp(this.middleOpacity, this.endOpacity, colorFactor2);
}
}
geometry.attributes.particlePosition.needsUpdate = true;
geometry.attributes.particleColor.needsUpdate = true;
geometry.attributes.particleAngle.needsUpdate = true;
}
copy(source: this, recursive = true) {
super.copy(source, recursive);
const material = this.material as RawShaderMaterial;
const sourceMaterial = source.material as RawShaderMaterial;
material.uniforms.map.value = sourceMaterial.uniforms.map.value;
this.startColor.copy(source.startColor);
this.middleColor.copy(source.middleColor);
this.endColor.copy(source.endColor);
this.startOpacity = source.startOpacity;
this.middleOpacity = source.middleOpacity;
this.endOpacity = source.endOpacity;
this.colorCurve = source.colorCurve;
this.sizeCurve = source.sizeCurve;
this.startSize = source.startSize;
this.endSize = source.endSize;
this.sizeRandomness = source.sizeRandomness;
this.ageRandomness = source.ageRandomness;
this.lifetime = source.lifetime;
this.lifetimeRandomness = source.lifetimeRandomness;
this.particleCount = source.particleCount;
this.startVelocity.copy(source.startVelocity);
this.endVelocity.copy(source.endVelocity);
this.velocityCurve = source.velocityCurve;
this.angularVelocity = source.angularVelocity;
return this;
}
}