@umicat/three-sdk 1.35.0 → 1.36.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -9
- package/dist/vfx/VfxInstance.d.ts +26 -8
- package/dist/vfx/VfxInstance.js +447 -60
- package/dist/vfx/document.d.ts +247 -19
- package/dist/vfx/document.js +351 -24
- package/dist/vfx/templates.js +9 -7
- package/package.json +1 -1
package/dist/vfx/VfxInstance.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as T from 'three';
|
|
2
|
-
import { parseVfx } from './document.js';
|
|
2
|
+
import { parseVfx, pickRange, sampleCurve, sampleGradient, LIMITS, } from './document.js';
|
|
3
3
|
const SHARED_QUAD = (() => {
|
|
4
4
|
// Corners in [0,1]^2 with y UP; the shader turns them into a cell-sized quad.
|
|
5
5
|
const g = new T.BufferGeometry();
|
|
@@ -35,6 +35,86 @@ void main(){
|
|
|
35
35
|
#include <tonemapping_fragment>
|
|
36
36
|
#include <colorspace_fragment>
|
|
37
37
|
}`;
|
|
38
|
+
/** The particle quad: corners at +-0.5, one instance per particle. */
|
|
39
|
+
const PARTICLE_QUAD = (() => {
|
|
40
|
+
const g = new T.InstancedBufferGeometry();
|
|
41
|
+
g.setAttribute('position', new T.BufferAttribute(new Float32Array([-.5, -.5, 0, .5, -.5, 0, .5, .5, 0, -.5, .5, 0]), 3));
|
|
42
|
+
g.setIndex([0, 1, 2, 0, 2, 3]);
|
|
43
|
+
return g;
|
|
44
|
+
})();
|
|
45
|
+
// iMisc = (size, angle radians, frame, unused)
|
|
46
|
+
const PARTICLE_VERTEX = `
|
|
47
|
+
attribute vec3 iPos; attribute vec4 iColor; attribute vec4 iMisc; attribute vec3 iVel;
|
|
48
|
+
uniform float uAlign; uniform float uStretch; uniform vec2 uCell; uniform mat3 uOrient; uniform vec3 uAxis;
|
|
49
|
+
varying vec2 vUv; varying vec2 vQuad; varying vec4 vColor;
|
|
50
|
+
void main(){
|
|
51
|
+
vec2 c = position.xy;
|
|
52
|
+
vQuad = c * 2.0;
|
|
53
|
+
vColor = iColor;
|
|
54
|
+
float size = iMisc.x, angle = iMisc.y, frame = iMisc.z;
|
|
55
|
+
vec2 uv = c + 0.5;
|
|
56
|
+
vUv = vec2((mod(frame, uCell.x) + uv.x) / uCell.x, 1.0 - (floor(frame / uCell.x) + 1.0 - uv.y) / uCell.y);
|
|
57
|
+
float s = sin(angle), co = cos(angle);
|
|
58
|
+
vec2 turned = vec2(c.x * co - c.y * s, c.x * s + c.y * co) * size;
|
|
59
|
+
if (uAlign > 1.5 && uAlign < 2.5) {
|
|
60
|
+
gl_Position = projectionMatrix * modelViewMatrix * vec4(iPos + uOrient * vec3(turned.x, 0.0, -turned.y), 1.0);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
float scale = length(modelMatrix[0].xyz);
|
|
64
|
+
vec4 center = modelViewMatrix * vec4(iPos, 1.0);
|
|
65
|
+
vec3 offset = vec3(turned, 0.0);
|
|
66
|
+
if (uAlign > 0.5) {
|
|
67
|
+
// An axial billboard: the long side lies along a 3D direction (the
|
|
68
|
+
// particle's velocity, or the emitter's axis) and foreshortens with it;
|
|
69
|
+
// the quad only spins about that line to face the viewer.
|
|
70
|
+
vec3 along = (modelViewMatrix * vec4(uAlign > 2.5 ? uAxis : iVel, 0.0)).xyz;
|
|
71
|
+
float l = length(along);
|
|
72
|
+
if (l > 1e-6) {
|
|
73
|
+
vec3 dir = along / l;
|
|
74
|
+
vec3 side = cross(dir, normalize(-center.xyz));
|
|
75
|
+
float sl = length(side);
|
|
76
|
+
if (sl > 1e-4) {
|
|
77
|
+
side /= sl;
|
|
78
|
+
vec2 r = vec2(c.x * co - c.y * s, c.x * s + c.y * co);
|
|
79
|
+
offset = side * (r.x * size) + dir * (r.y * size * (1.0 + uStretch * length(iVel)));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
center.xyz += offset * scale;
|
|
84
|
+
gl_Position = projectionMatrix * center;
|
|
85
|
+
}`;
|
|
86
|
+
const PARTICLE_FRAGMENT = `
|
|
87
|
+
uniform float uLook; uniform float uSoft; uniform float uPoints; uniform float uSharp; uniform sampler2D uMap;
|
|
88
|
+
varying vec2 vUv; varying vec2 vQuad; varying vec4 vColor;
|
|
89
|
+
void main(){
|
|
90
|
+
float r = length(vQuad);
|
|
91
|
+
vec3 rgb = vColor.rgb;
|
|
92
|
+
float a;
|
|
93
|
+
if (uLook < 0.5) { // dot
|
|
94
|
+
if (r >= 1.0) discard;
|
|
95
|
+
a = pow(1.0 - r, uSoft * 4.0);
|
|
96
|
+
} else if (uLook < 1.5) { // glow: a hot core and a long halo
|
|
97
|
+
if (r >= 1.0) discard;
|
|
98
|
+
float core = 1.0 - smoothstep(0.0, 0.3, r);
|
|
99
|
+
a = max(core, pow(1.0 - r, 3.0) * 0.6);
|
|
100
|
+
rgb *= 1.0 + core;
|
|
101
|
+
} else if (uLook < 2.5) { // star
|
|
102
|
+
if (r >= 1.0) discard;
|
|
103
|
+
float k = abs(cos(uPoints * atan(vQuad.y, vQuad.x) * 0.5));
|
|
104
|
+
float edge = mix(0.12, 1.0, pow(k, 1.0 + uSharp * 12.0));
|
|
105
|
+
a = max(1.0 - smoothstep(edge * 0.5, edge, r), (1.0 - smoothstep(0.0, 0.25, r)) * 0.8);
|
|
106
|
+
} else { // texture / sheet
|
|
107
|
+
vec4 texel = texture2D(uMap, vUv);
|
|
108
|
+
rgb *= texel.rgb;
|
|
109
|
+
a = texel.a;
|
|
110
|
+
}
|
|
111
|
+
gl_FragColor = vec4(rgb, vColor.a * a);
|
|
112
|
+
#include <tonemapping_fragment>
|
|
113
|
+
#include <colorspace_fragment>
|
|
114
|
+
}`;
|
|
115
|
+
const LOOK_CODE = { dot: 0, glow: 1, star: 2, texture: 3, sheet: 3 };
|
|
116
|
+
const ALIGN_CODE = { camera: 0, velocity: 1, ground: 2, axis: 3 };
|
|
117
|
+
const TAU = Math.PI * 2, DEG = Math.PI / 180;
|
|
38
118
|
function resolveTexture(source, name) {
|
|
39
119
|
if (!source)
|
|
40
120
|
return undefined;
|
|
@@ -44,13 +124,17 @@ function resolveTexture(source, name) {
|
|
|
44
124
|
return source.get(name);
|
|
45
125
|
return Object.prototype.hasOwnProperty.call(source, name) ? source[name] : undefined;
|
|
46
126
|
}
|
|
127
|
+
const scratchColor = new T.Color();
|
|
128
|
+
const scratchRgba = [0, 0, 0, 0];
|
|
129
|
+
const scratchVec = new T.Vector3();
|
|
47
130
|
/**
|
|
48
131
|
* One playing copy of an effect. Shared by the VFX Editor and games: it owns
|
|
49
132
|
* no renderer, no DOM and no animation loop -- add `root` to a scene and call
|
|
50
133
|
* `update(seconds)` from your own frame loop.
|
|
51
134
|
*
|
|
52
|
-
* Simulation is fixed-step (1/60 s) and
|
|
53
|
-
* same way at any frame rate, and `reset()` replays it
|
|
135
|
+
* Simulation is fixed-step (1/60 s), seeded and on the CPU, so the same
|
|
136
|
+
* document plays the same way at any frame rate, and `reset()` replays it
|
|
137
|
+
* exactly. Each particle layer draws as one instanced quad mesh.
|
|
54
138
|
*/
|
|
55
139
|
export class VfxInstance {
|
|
56
140
|
constructor(document, options = {}) {
|
|
@@ -58,43 +142,69 @@ export class VfxInstance {
|
|
|
58
142
|
/** Seconds of simulated time since start (or the last reset). */
|
|
59
143
|
this.time = 0;
|
|
60
144
|
this.particleLayers = [];
|
|
145
|
+
this.byId = new Map();
|
|
61
146
|
this.flipbookLayers = [];
|
|
62
147
|
this.state = 1;
|
|
63
148
|
this.accumulator = 0;
|
|
149
|
+
this.live = 0;
|
|
64
150
|
this.document = parseVfx(document);
|
|
65
151
|
this.state = this.document.seed;
|
|
66
152
|
this.root.name = this.document.name;
|
|
67
|
-
|
|
68
|
-
|
|
153
|
+
const children = new Set(this.document.emitters.flatMap((e) => e.children.map((c) => c.emitter)));
|
|
154
|
+
this.document.emitters.forEach((config, index) => {
|
|
155
|
+
const look = config.look;
|
|
156
|
+
const texture = look.type === 'texture' || look.type === 'sheet' ? resolveTexture(options.textures, look.texture) : undefined;
|
|
157
|
+
const layer = this.buildParticles(config, index, !children.has(config.id), texture);
|
|
158
|
+
this.particleLayers.push(layer);
|
|
159
|
+
this.byId.set(config.id, layer);
|
|
160
|
+
});
|
|
69
161
|
for (const config of this.document.flipbooks ?? [])
|
|
70
162
|
this.flipbookLayers.push(this.buildFlipbook(config, resolveTexture(options.textures, config.texture)));
|
|
71
163
|
this.draw();
|
|
72
164
|
}
|
|
73
|
-
buildParticles(config) {
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
geometry.setAttribute(name, new T.BufferAttribute(array, itemSize).setUsage(T.DynamicDrawUsage));
|
|
79
|
-
}
|
|
80
|
-
geometry.setDrawRange(0, 0);
|
|
165
|
+
buildParticles(config, index, root, texture) {
|
|
166
|
+
const geometry = new T.InstancedBufferGeometry();
|
|
167
|
+
geometry.index = PARTICLE_QUAD.index;
|
|
168
|
+
geometry.setAttribute('position', PARTICLE_QUAD.getAttribute('position'));
|
|
169
|
+
const look = config.look;
|
|
81
170
|
const material = new T.ShaderMaterial({
|
|
82
|
-
transparent: true, depthWrite: false,
|
|
171
|
+
transparent: true, depthWrite: false, side: T.DoubleSide,
|
|
83
172
|
blending: config.blend === 'additive' ? T.AdditiveBlending : T.NormalBlending,
|
|
84
|
-
uniforms: {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
}
|
|
173
|
+
uniforms: {
|
|
174
|
+
uLook: { value: LOOK_CODE[look.type] }, uSoft: { value: look.type === 'dot' ? look.softness : 0 },
|
|
175
|
+
uPoints: { value: look.type === 'star' ? look.points : 5 }, uSharp: { value: look.type === 'star' ? look.sharpness : 0 },
|
|
176
|
+
uMap: { value: texture ?? null }, uCell: { value: new T.Vector2(look.type === 'sheet' ? look.cols : 1, look.type === 'sheet' ? look.rows : 1) },
|
|
177
|
+
uAlign: { value: ALIGN_CODE[config.align] }, uStretch: { value: config.stretch },
|
|
178
|
+
uAxis: { value: new T.Vector3(...config.direction).applyQuaternion(emitterQuaternion(config)).normalize() },
|
|
179
|
+
uOrient: { value: new T.Matrix3().setFromMatrix4(new T.Matrix4().makeRotationFromQuaternion(emitterQuaternion(config))) },
|
|
180
|
+
},
|
|
181
|
+
vertexShader: PARTICLE_VERTEX, fragmentShader: PARTICLE_FRAGMENT,
|
|
92
182
|
});
|
|
93
|
-
const mesh = new T.
|
|
183
|
+
const mesh = new T.Mesh(geometry, material);
|
|
94
184
|
mesh.frustumCulled = false;
|
|
95
185
|
mesh.name = config.id;
|
|
186
|
+
mesh.renderOrder = index;
|
|
187
|
+
if (config.space === 'world')
|
|
188
|
+
mesh.matrixAutoUpdate = false;
|
|
96
189
|
this.root.add(mesh);
|
|
97
|
-
|
|
190
|
+
const layer = {
|
|
191
|
+
config, index, particles: [], root, fraction: 0, cycle: 0, burstEmitted: config.bursts.map(() => 0),
|
|
192
|
+
orient: emitterQuaternion(config), mesh, geometry, material, capacity: 0, attributes: undefined,
|
|
193
|
+
texture, needsTexture: look.type === 'texture' || look.type === 'sheet',
|
|
194
|
+
};
|
|
195
|
+
this.allocate(layer, 64);
|
|
196
|
+
return layer;
|
|
197
|
+
}
|
|
198
|
+
/** (Re)creates the per-instance buffers with room for `capacity` particles. */
|
|
199
|
+
allocate(layer, capacity) {
|
|
200
|
+
const make = (size) => new T.InstancedBufferAttribute(new Float32Array(capacity * size), size).setUsage(T.DynamicDrawUsage);
|
|
201
|
+
layer.attributes = { pos: make(3), color: make(4), misc: make(4), vel: make(3) };
|
|
202
|
+
const g = layer.geometry;
|
|
203
|
+
g.setAttribute('iPos', layer.attributes.pos);
|
|
204
|
+
g.setAttribute('iColor', layer.attributes.color);
|
|
205
|
+
g.setAttribute('iMisc', layer.attributes.misc);
|
|
206
|
+
g.setAttribute('iVel', layer.attributes.vel);
|
|
207
|
+
layer.capacity = capacity;
|
|
98
208
|
}
|
|
99
209
|
buildFlipbook(config, texture) {
|
|
100
210
|
const material = new T.ShaderMaterial({
|
|
@@ -120,26 +230,28 @@ void main(){float r=length(gl_PointCoord-.5)*2.;if(r>=1.)discard;gl_FragColor=ve
|
|
|
120
230
|
this.time = 0;
|
|
121
231
|
this.accumulator = 0;
|
|
122
232
|
this.state = this.document.seed;
|
|
233
|
+
this.live = 0;
|
|
123
234
|
for (const l of this.particleLayers) {
|
|
124
235
|
l.particles = [];
|
|
125
236
|
l.fraction = 0;
|
|
237
|
+
l.cycle = 0;
|
|
238
|
+
l.burstEmitted.fill(0);
|
|
126
239
|
}
|
|
127
240
|
for (const f of this.flipbookLayers)
|
|
128
241
|
f.done = !f.config.enabled;
|
|
129
242
|
this.draw();
|
|
130
243
|
}
|
|
131
|
-
get particleCount() { return this.
|
|
244
|
+
get particleCount() { return this.live; }
|
|
132
245
|
/** True once a NON-looping effect has stopped emitting, every particle has
|
|
133
246
|
* died and every flipbook has played out -- the moment to `dispose()` a
|
|
134
247
|
* one-shot. Always false for a looping effect. */
|
|
135
248
|
get finished() {
|
|
136
249
|
if (this.document.loop || this.time < this.document.duration)
|
|
137
250
|
return false;
|
|
138
|
-
return this.
|
|
251
|
+
return this.live === 0 && this.flipbookLayers.every((f) => f.done);
|
|
139
252
|
}
|
|
140
|
-
/**
|
|
141
|
-
setViewportHeight(
|
|
142
|
-
l.mesh.material.uniforms.viewportHeight.value = height; }
|
|
253
|
+
/** Kept for 1.35 callers. Particles are world-sized quads now, so the viewport no longer matters. */
|
|
254
|
+
setViewportHeight(_height) { }
|
|
143
255
|
/** Advance by `delta` seconds. Gaps over 250 ms are capped, so a backgrounded tab does not dump a burst. */
|
|
144
256
|
update(delta) {
|
|
145
257
|
if (!Number.isFinite(delta) || delta < 0)
|
|
@@ -153,32 +265,230 @@ void main(){float r=length(gl_PointCoord-.5)*2.;if(r>=1.)discard;gl_FragColor=ve
|
|
|
153
265
|
}
|
|
154
266
|
/** Whether the effect is still starting new particles / new flipbook cycles. */
|
|
155
267
|
get running() { return this.document.loop || this.time < this.document.duration; }
|
|
268
|
+
/** The emitter's origin in its own simulation space. */
|
|
269
|
+
origin(layer, out) {
|
|
270
|
+
if (layer.config.space === 'world')
|
|
271
|
+
return out.setFromMatrixPosition(this.root.matrixWorld);
|
|
272
|
+
return out.set(0, 0, 0);
|
|
273
|
+
}
|
|
156
274
|
step(dt) {
|
|
157
|
-
const
|
|
275
|
+
const running = this.running;
|
|
276
|
+
const cycleTime = this.document.loop ? this.time % this.document.duration : this.time;
|
|
277
|
+
const cycle = this.document.loop ? Math.floor(this.time / this.document.duration) : 0;
|
|
158
278
|
this.time += dt;
|
|
279
|
+
this.root.updateWorldMatrix(true, false);
|
|
280
|
+
const origin = new T.Vector3();
|
|
159
281
|
for (const l of this.particleLayers) {
|
|
160
282
|
const c = l.config;
|
|
161
|
-
l
|
|
283
|
+
this.origin(l, origin);
|
|
284
|
+
const kept = [];
|
|
285
|
+
for (const p of l.particles) {
|
|
162
286
|
p.age += dt;
|
|
163
|
-
if (p.age >= p.life)
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
p.x += p.vx * dt;
|
|
167
|
-
p.y += p.vy * dt;
|
|
168
|
-
p.z += p.vz * dt;
|
|
169
|
-
return true;
|
|
170
|
-
});
|
|
171
|
-
if (!emit || !c.enabled)
|
|
172
|
-
continue;
|
|
173
|
-
l.fraction += c.rate * dt;
|
|
174
|
-
while (l.fraction >= 1) {
|
|
175
|
-
l.fraction--;
|
|
176
|
-
const a = this.random() * Math.PI * 2, r = this.random() * .15;
|
|
177
|
-
if (l.particles.length >= l.capacity)
|
|
287
|
+
if (p.age >= p.life) {
|
|
288
|
+
this.live--;
|
|
289
|
+
this.spawnChildren(l, p, 'death', dt);
|
|
178
290
|
continue;
|
|
179
|
-
|
|
291
|
+
}
|
|
292
|
+
this.move(c, p, dt, origin);
|
|
293
|
+
this.spawnChildren(l, p, 'life', dt);
|
|
294
|
+
kept.push(p);
|
|
295
|
+
}
|
|
296
|
+
l.particles = kept;
|
|
297
|
+
if (!running || !c.enabled || !l.root)
|
|
298
|
+
continue;
|
|
299
|
+
// Continuous emission inside [start, stop).
|
|
300
|
+
if (cycleTime >= c.start && (c.stop === null || cycleTime < c.stop)) {
|
|
301
|
+
l.fraction += c.rate * dt;
|
|
302
|
+
while (l.fraction >= 1) {
|
|
303
|
+
l.fraction--;
|
|
304
|
+
this.spawn(l, null);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
// Bursts, counted per cycle so a looping effect repeats them.
|
|
308
|
+
if (cycle !== l.cycle) {
|
|
309
|
+
l.cycle = cycle;
|
|
310
|
+
l.burstEmitted.fill(0);
|
|
311
|
+
}
|
|
312
|
+
c.bursts.forEach((b, i) => {
|
|
313
|
+
// Particle i of a spread burst is born at time + i * over / count, as
|
|
314
|
+
// Godot does (its first particle at the START of the window, not after
|
|
315
|
+
// one slice -- a 1-particle burst "over" its lifetime appears at once).
|
|
316
|
+
const due = cycleTime < b.time ? 0 : b.over > 0 ? Math.min(b.count, Math.floor(b.count * (cycleTime - b.time) / b.over + 1e-9) + 1) : b.count;
|
|
317
|
+
while (l.burstEmitted[i] < due) {
|
|
318
|
+
l.burstEmitted[i]++;
|
|
319
|
+
this.spawn(l, null);
|
|
320
|
+
}
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
move(c, p, dt, origin) {
|
|
325
|
+
const t = p.age / p.life;
|
|
326
|
+
let rx = p.x - origin.x, ry = p.y - origin.y, rz = p.z - origin.z;
|
|
327
|
+
const rl = Math.hypot(rx, ry, rz);
|
|
328
|
+
let ax = c.gravity[0], ay = c.gravity[1], az = c.gravity[2];
|
|
329
|
+
if (rl > 1e-6) {
|
|
330
|
+
const radial = p.radialAccel * (c.radialAccelOverLife ? sampleCurve(c.radialAccelOverLife, t) : 1);
|
|
331
|
+
ax += rx / rl * radial;
|
|
332
|
+
ay += ry / rl * radial;
|
|
333
|
+
az += rz / rl * radial;
|
|
334
|
+
const hl = Math.hypot(rx, rz);
|
|
335
|
+
if (hl > 1e-6 && p.tangentialAccel) {
|
|
336
|
+
ax += -rz / hl * p.tangentialAccel;
|
|
337
|
+
az += rx / hl * p.tangentialAccel;
|
|
180
338
|
}
|
|
181
339
|
}
|
|
340
|
+
p.vx += ax * dt;
|
|
341
|
+
p.vy += ay * dt;
|
|
342
|
+
p.vz += az * dt;
|
|
343
|
+
let speed = Math.hypot(p.vx, p.vy, p.vz);
|
|
344
|
+
if (p.damping > 0 && speed > 0) {
|
|
345
|
+
const k = Math.max(0, speed - p.damping * dt) / speed;
|
|
346
|
+
p.vx *= k;
|
|
347
|
+
p.vy *= k;
|
|
348
|
+
p.vz *= k;
|
|
349
|
+
speed *= k;
|
|
350
|
+
}
|
|
351
|
+
if (c.speedLimit && speed > 0) {
|
|
352
|
+
const lim = Math.max(0, sampleCurve(c.speedLimit, t));
|
|
353
|
+
if (speed > lim) {
|
|
354
|
+
const k = lim / speed;
|
|
355
|
+
p.vx *= k;
|
|
356
|
+
p.vy *= k;
|
|
357
|
+
p.vz *= k;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
p.x += p.vx * dt;
|
|
361
|
+
p.y += p.vy * dt;
|
|
362
|
+
p.z += p.vz * dt;
|
|
363
|
+
if (p.radialSpeed && rl > 1e-6) {
|
|
364
|
+
const k = p.radialSpeed * dt / rl;
|
|
365
|
+
p.x += rx * k;
|
|
366
|
+
p.y += ry * k;
|
|
367
|
+
p.z += rz * k;
|
|
368
|
+
}
|
|
369
|
+
const orbit = p.orbit * (c.orbitOverLife ? sampleCurve(c.orbitOverLife, t) : 1);
|
|
370
|
+
if (orbit) {
|
|
371
|
+
rx = p.x - origin.x;
|
|
372
|
+
rz = p.z - origin.z;
|
|
373
|
+
const a = orbit * TAU * dt, s = Math.sin(a), co = Math.cos(a);
|
|
374
|
+
p.x = origin.x + rx * co - rz * s;
|
|
375
|
+
p.z = origin.z + rx * s + rz * co;
|
|
376
|
+
}
|
|
377
|
+
p.angle += p.spin * dt;
|
|
378
|
+
}
|
|
379
|
+
/** A new particle on `layer`, from its own emission shape -- around a parent particle when `at` is given. */
|
|
380
|
+
spawn(layer, at) {
|
|
381
|
+
if (this.live >= LIMITS.particles)
|
|
382
|
+
return;
|
|
383
|
+
const c = layer.config, r = () => this.random();
|
|
384
|
+
// Shape (Godot's emission shapes, then scale, then offset).
|
|
385
|
+
let x = 0, y = 0, z = 0;
|
|
386
|
+
const s = c.shape;
|
|
387
|
+
if (s.type === 'sphere') {
|
|
388
|
+
const u = r() * 2 - 1, th = r() * TAU, k = s.surface ? 1 : r(), ring = s.radius * Math.sqrt(1 - u * u);
|
|
389
|
+
x = ring * Math.cos(th) * k;
|
|
390
|
+
y = ring * Math.sin(th) * k;
|
|
391
|
+
z = s.radius * u * k;
|
|
392
|
+
}
|
|
393
|
+
else if (s.type === 'box') {
|
|
394
|
+
x = (r() * 2 - 1) * s.extents[0];
|
|
395
|
+
y = (r() * 2 - 1) * s.extents[1];
|
|
396
|
+
z = (r() * 2 - 1) * s.extents[2];
|
|
397
|
+
}
|
|
398
|
+
else if (s.type === 'ring') {
|
|
399
|
+
const th = r() * TAU, rad = Math.sqrt(r() * (s.radius ** 2 - s.inner ** 2) + s.inner ** 2), h = (r() - .5) * s.height;
|
|
400
|
+
const axis = scratchVec.set(...s.axis).normalize();
|
|
401
|
+
const ortho = Math.abs(axis.x) < .9 ? new T.Vector3(1, 0, 0) : new T.Vector3(0, 1, 0);
|
|
402
|
+
const u = ortho.cross(axis).normalize(), v = axis.clone().cross(u);
|
|
403
|
+
x = (u.x * Math.cos(th) + v.x * Math.sin(th)) * rad + axis.x * h;
|
|
404
|
+
y = (u.y * Math.cos(th) + v.y * Math.sin(th)) * rad + axis.y * h;
|
|
405
|
+
z = (u.z * Math.cos(th) + v.z * Math.sin(th)) * rad + axis.z * h;
|
|
406
|
+
}
|
|
407
|
+
x = x * c.shapeScale[0] + c.shapeOffset[0];
|
|
408
|
+
y = y * c.shapeScale[1] + c.shapeOffset[1];
|
|
409
|
+
z = z * c.shapeScale[2] + c.shapeOffset[2];
|
|
410
|
+
// Direction cone (Godot's spread + flatness).
|
|
411
|
+
const [dx, dy, dz] = coneDirection(c.direction, c.spread * DEG, c.flatness, r(), r());
|
|
412
|
+
const speed = pickRange(c.speed, r());
|
|
413
|
+
let vx = dx * speed + (r() * 2 - 1) * c.randomVelocity[0];
|
|
414
|
+
let vy = dy * speed + (r() * 2 - 1) * c.randomVelocity[1];
|
|
415
|
+
let vz = dz * speed + (r() * 2 - 1) * c.randomVelocity[2];
|
|
416
|
+
if (layer.orient.w < 1 - 1e-12) {
|
|
417
|
+
const p = scratchVec.set(x, y, z).applyQuaternion(layer.orient);
|
|
418
|
+
x = p.x;
|
|
419
|
+
y = p.y;
|
|
420
|
+
z = p.z;
|
|
421
|
+
const v = scratchVec.set(vx, vy, vz).applyQuaternion(layer.orient);
|
|
422
|
+
vx = v.x;
|
|
423
|
+
vy = v.y;
|
|
424
|
+
vz = v.z;
|
|
425
|
+
}
|
|
426
|
+
if (at) {
|
|
427
|
+
x += at.x;
|
|
428
|
+
y += at.y;
|
|
429
|
+
z += at.z;
|
|
430
|
+
vx += at.vx;
|
|
431
|
+
vy += at.vy;
|
|
432
|
+
vz += at.vz;
|
|
433
|
+
}
|
|
434
|
+
else if (c.space === 'world') {
|
|
435
|
+
const m = this.root.matrixWorld;
|
|
436
|
+
const p = scratchVec.set(x, y, z).applyMatrix4(m);
|
|
437
|
+
x = p.x;
|
|
438
|
+
y = p.y;
|
|
439
|
+
z = p.z;
|
|
440
|
+
const v = scratchVec.set(vx, vy, vz).transformDirection(m).multiplyScalar(Math.hypot(vx, vy, vz));
|
|
441
|
+
vx = v.x;
|
|
442
|
+
vy = v.y;
|
|
443
|
+
vz = v.z;
|
|
444
|
+
}
|
|
445
|
+
sampleGradient(c.color, c.color.stops.length > 1 ? r() : 0, scratchRgba);
|
|
446
|
+
const particle = {
|
|
447
|
+
age: 0, life: pickRange(c.lifetime, r()), x, y, z, vx, vy, vz,
|
|
448
|
+
angle: pickRange(c.angle, r()) * DEG, spin: pickRange(c.spin, r()) * DEG, size: pickRange(c.size, r()),
|
|
449
|
+
r: scratchRgba[0], g: scratchRgba[1], b: scratchRgba[2], a: scratchRgba[3],
|
|
450
|
+
radialSpeed: pickRange(c.radialSpeed, r()), radialAccel: pickRange(c.radialAccel, r()),
|
|
451
|
+
tangentialAccel: pickRange(c.tangentialAccel, r()), orbit: pickRange(c.orbit, r()), damping: pickRange(c.damping, r()),
|
|
452
|
+
owed: c.children.map(() => 0),
|
|
453
|
+
};
|
|
454
|
+
layer.particles.push(particle);
|
|
455
|
+
this.live++;
|
|
456
|
+
this.spawnChildren(layer, particle, 'birth', 0);
|
|
457
|
+
}
|
|
458
|
+
spawnChildren(layer, p, when, dt) {
|
|
459
|
+
layer.config.children.forEach((sub, i) => {
|
|
460
|
+
if (sub.when !== when)
|
|
461
|
+
return;
|
|
462
|
+
const child = this.byId.get(sub.emitter);
|
|
463
|
+
if (!child || !child.config.enabled)
|
|
464
|
+
return;
|
|
465
|
+
let n = sub.count;
|
|
466
|
+
if (when === 'life') {
|
|
467
|
+
p.owed[i] += sub.rate * dt;
|
|
468
|
+
n = Math.floor(p.owed[i]);
|
|
469
|
+
p.owed[i] -= n;
|
|
470
|
+
}
|
|
471
|
+
if (n <= 0)
|
|
472
|
+
return;
|
|
473
|
+
// The parent's position (and velocity) in the CHILD's space.
|
|
474
|
+
let x = p.x, y = p.y, z = p.z, vx = sub.inheritVelocity ? p.vx : 0, vy = sub.inheritVelocity ? p.vy : 0, vz = sub.inheritVelocity ? p.vz : 0;
|
|
475
|
+
if (layer.config.space !== child.config.space) {
|
|
476
|
+
const m = child.config.space === 'world' ? this.root.matrixWorld : this.root.matrixWorld.clone().invert();
|
|
477
|
+
const pos = scratchVec.set(x, y, z).applyMatrix4(m);
|
|
478
|
+
x = pos.x;
|
|
479
|
+
y = pos.y;
|
|
480
|
+
z = pos.z;
|
|
481
|
+
const len = Math.hypot(vx, vy, vz);
|
|
482
|
+
if (len > 0) {
|
|
483
|
+
const v = scratchVec.set(vx, vy, vz).transformDirection(m).multiplyScalar(len);
|
|
484
|
+
vx = v.x;
|
|
485
|
+
vy = v.y;
|
|
486
|
+
vz = v.z;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
for (let k = 0; k < n; k++)
|
|
490
|
+
this.spawn(child, { x, y, z, vx, vy, vz });
|
|
491
|
+
});
|
|
182
492
|
}
|
|
183
493
|
/** The frame a flipbook shows now, or -1 for none. A looping flipbook keeps
|
|
184
494
|
* cycling while the effect runs and, once it stops, finishes its current
|
|
@@ -198,19 +508,50 @@ void main(){float r=length(gl_PointCoord-.5)*2.;if(r>=1.)discard;gl_FragColor=ve
|
|
|
198
508
|
return n < cycleEnd ? n % f.frames : -1;
|
|
199
509
|
}
|
|
200
510
|
draw() {
|
|
511
|
+
if (this.particleLayers.some((l) => l.config.space === 'world'))
|
|
512
|
+
this.root.updateWorldMatrix(true, false);
|
|
201
513
|
for (const l of this.particleLayers) {
|
|
202
|
-
const c = l.config,
|
|
203
|
-
l.
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
l.
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
514
|
+
const c = l.config, n = l.particles.length;
|
|
515
|
+
if (n > l.capacity)
|
|
516
|
+
this.allocate(l, Math.min(LIMITS.particles, Math.max(n, l.capacity * 2)));
|
|
517
|
+
const { pos, color, misc, vel } = l.attributes;
|
|
518
|
+
const P = pos.array, C = color.array, M = misc.array, V = vel.array;
|
|
519
|
+
const look = c.look;
|
|
520
|
+
for (let i = 0; i < n; i++) {
|
|
521
|
+
const p = l.particles[i], t = p.age / p.life;
|
|
522
|
+
let r = p.r, g = p.g, b = p.b, a = p.a;
|
|
523
|
+
if (c.colorOverLife) {
|
|
524
|
+
sampleGradient(c.colorOverLife, t, scratchRgba);
|
|
525
|
+
r *= scratchRgba[0];
|
|
526
|
+
g *= scratchRgba[1];
|
|
527
|
+
b *= scratchRgba[2];
|
|
528
|
+
a *= scratchRgba[3];
|
|
529
|
+
}
|
|
530
|
+
if (c.alphaOverLife)
|
|
531
|
+
a *= Math.max(0, sampleCurve(c.alphaOverLife, t));
|
|
532
|
+
scratchColor.setRGB(r, g, b, T.SRGBColorSpace);
|
|
533
|
+
P[i * 3] = p.x;
|
|
534
|
+
P[i * 3 + 1] = p.y;
|
|
535
|
+
P[i * 3 + 2] = p.z;
|
|
536
|
+
V[i * 3] = p.vx;
|
|
537
|
+
V[i * 3 + 1] = p.vy;
|
|
538
|
+
V[i * 3 + 2] = p.vz;
|
|
539
|
+
C[i * 4] = scratchColor.r * c.intensity;
|
|
540
|
+
C[i * 4 + 1] = scratchColor.g * c.intensity;
|
|
541
|
+
C[i * 4 + 2] = scratchColor.b * c.intensity;
|
|
542
|
+
C[i * 4 + 3] = Math.min(1, a);
|
|
543
|
+
M[i * 4] = p.size * (c.sizeOverLife ? Math.max(0, sampleCurve(c.sizeOverLife, t)) : 1);
|
|
544
|
+
M[i * 4 + 1] = p.angle;
|
|
545
|
+
M[i * 4 + 2] = look.type !== 'sheet' ? 0 : look.fps === 'life' ? Math.min(look.frames - 1, Math.floor(t * look.frames)) : Math.floor(p.age * look.fps) % look.frames;
|
|
546
|
+
}
|
|
547
|
+
l.geometry.instanceCount = n;
|
|
548
|
+
for (const attribute of [pos, color, misc, vel])
|
|
213
549
|
attribute.needsUpdate = true;
|
|
550
|
+
// A textured look waits for its image; a missing one draws nothing.
|
|
551
|
+
const image = l.texture?.image;
|
|
552
|
+
l.mesh.visible = n > 0 && (!l.needsTexture || !!image?.width);
|
|
553
|
+
if (c.space === 'world')
|
|
554
|
+
l.mesh.matrix.copy(this.root.matrixWorld).invert();
|
|
214
555
|
}
|
|
215
556
|
for (const f of this.flipbookLayers) {
|
|
216
557
|
const frame = this.frameOf(f.config);
|
|
@@ -235,14 +576,60 @@ void main(){float r=length(gl_PointCoord-.5)*2.;if(r>=1.)discard;gl_FragColor=ve
|
|
|
235
576
|
* shared between instances and belong to whoever loaded them (`VfxEffect`). */
|
|
236
577
|
dispose() {
|
|
237
578
|
for (const l of this.particleLayers) {
|
|
238
|
-
l.
|
|
239
|
-
l.
|
|
579
|
+
l.geometry.dispose();
|
|
580
|
+
l.material.dispose();
|
|
240
581
|
}
|
|
241
582
|
for (const f of this.flipbookLayers)
|
|
242
583
|
f.material.dispose();
|
|
243
584
|
this.particleLayers = [];
|
|
244
585
|
this.flipbookLayers = [];
|
|
586
|
+
this.byId.clear();
|
|
587
|
+
this.live = 0;
|
|
245
588
|
this.root.removeFromParent();
|
|
246
589
|
this.root.clear();
|
|
247
590
|
}
|
|
248
591
|
}
|
|
592
|
+
/** An emitter's `rotation` (XYZ Euler degrees) as a quaternion. */
|
|
593
|
+
function emitterQuaternion(e) {
|
|
594
|
+
return new T.Quaternion().setFromEuler(new T.Euler(e.rotation[0] * DEG, e.rotation[1] * DEG, e.rotation[2] * DEG, 'XYZ'));
|
|
595
|
+
}
|
|
596
|
+
/**
|
|
597
|
+
* A random direction inside the cone around `direction` with half-angle
|
|
598
|
+
* `spread` (radians), following Godot's ParticleProcessMaterial: one angle in
|
|
599
|
+
* the cone's first plane, a second one scaled down by `flatness`, so
|
|
600
|
+
* flatness 1 is a flat fan.
|
|
601
|
+
*/
|
|
602
|
+
export function coneDirection(direction, spread, flatness, u1, u2) {
|
|
603
|
+
const a1 = (u1 * 2 - 1) * spread, a2 = (u2 * 2 - 1) * spread * (1 - flatness);
|
|
604
|
+
let sx = Math.sin(a1) * Math.cos(a2), sy = Math.sin(a2), sz = Math.cos(a1) * Math.cos(a2);
|
|
605
|
+
const sl = Math.hypot(sx, sy, sz) || 1;
|
|
606
|
+
sx /= sl;
|
|
607
|
+
sy /= sl;
|
|
608
|
+
sz /= sl;
|
|
609
|
+
let [nx, ny, nz] = direction;
|
|
610
|
+
const nl = Math.hypot(nx, ny, nz);
|
|
611
|
+
if (nl < 1e-9) {
|
|
612
|
+
nx = 0;
|
|
613
|
+
ny = 0;
|
|
614
|
+
nz = 1;
|
|
615
|
+
}
|
|
616
|
+
else {
|
|
617
|
+
nx /= nl;
|
|
618
|
+
ny /= nl;
|
|
619
|
+
nz /= nl;
|
|
620
|
+
}
|
|
621
|
+
// binormal = Y x n (or Z when n is vertical); normal = binormal x n
|
|
622
|
+
let bx = nz, by = 0, bz = -nx;
|
|
623
|
+
let bl = Math.hypot(bx, by, bz);
|
|
624
|
+
if (bl < 1e-4) {
|
|
625
|
+
bx = 0;
|
|
626
|
+
by = 0;
|
|
627
|
+
bz = 1;
|
|
628
|
+
bl = 1;
|
|
629
|
+
}
|
|
630
|
+
bx /= bl;
|
|
631
|
+
by /= bl;
|
|
632
|
+
bz /= bl;
|
|
633
|
+
const mx = by * nz - bz * ny, my = bz * nx - bx * nz, mz = bx * ny - by * nx;
|
|
634
|
+
return [bx * sx + mx * sy + nx * sz, by * sx + my * sy + ny * sz, bz * sx + mz * sy + nz * sz];
|
|
635
|
+
}
|