@umicat/three-sdk 1.34.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 +31 -0
- package/dist/vfx/VfxInstance.d.ts +71 -0
- package/dist/vfx/VfxInstance.js +635 -0
- package/dist/vfx/document.d.ts +332 -0
- package/dist/vfx/document.js +461 -0
- package/dist/vfx/index.d.ts +6 -0
- package/dist/vfx/index.js +9 -0
- package/dist/vfx/load.d.ts +42 -0
- package/dist/vfx/load.js +79 -0
- package/dist/vfx/templates.d.ts +15 -0
- package/dist/vfx/templates.js +46 -0
- package/package.json +5 -1
|
@@ -0,0 +1,635 @@
|
|
|
1
|
+
import * as T from 'three';
|
|
2
|
+
import { parseVfx, pickRange, sampleCurve, sampleGradient, LIMITS, } from './document.js';
|
|
3
|
+
const SHARED_QUAD = (() => {
|
|
4
|
+
// Corners in [0,1]^2 with y UP; the shader turns them into a cell-sized quad.
|
|
5
|
+
const g = new T.BufferGeometry();
|
|
6
|
+
g.setAttribute('position', new T.BufferAttribute(new Float32Array([0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0]), 3));
|
|
7
|
+
g.setIndex([0, 1, 2, 0, 2, 3]);
|
|
8
|
+
return g;
|
|
9
|
+
})();
|
|
10
|
+
const FLIPBOOK_VERTEX = `
|
|
11
|
+
uniform vec2 uSize; uniform vec2 uOrigin; uniform vec3 uCell; uniform float uGround;
|
|
12
|
+
varying vec2 vUv;
|
|
13
|
+
void main(){
|
|
14
|
+
vec2 c = position.xy;
|
|
15
|
+
float top = 1.0 - c.y; // 0 at the cell's top edge
|
|
16
|
+
vec2 offset = vec2((c.x - uOrigin.x) * uSize.x, (uOrigin.y - top) * uSize.y);
|
|
17
|
+
float frame = uCell.z, cols = uCell.x, rows = uCell.y;
|
|
18
|
+
float col = mod(frame, cols), row = floor(frame / cols);
|
|
19
|
+
vUv = vec2((col + c.x) / cols, 1.0 - (row + top) / rows);
|
|
20
|
+
if (uGround > 0.5) {
|
|
21
|
+
gl_Position = projectionMatrix * modelViewMatrix * vec4(offset.x, 0.0, -offset.y, 1.0);
|
|
22
|
+
} else {
|
|
23
|
+
float scale = length(modelMatrix[0].xyz);
|
|
24
|
+
vec4 center = modelViewMatrix * vec4(0.0, 0.0, 0.0, 1.0);
|
|
25
|
+
center.xy += offset * scale;
|
|
26
|
+
gl_Position = projectionMatrix * center;
|
|
27
|
+
}
|
|
28
|
+
}`;
|
|
29
|
+
const FLIPBOOK_FRAGMENT = `
|
|
30
|
+
uniform sampler2D uMap; uniform vec3 uTint; uniform float uOpacity;
|
|
31
|
+
varying vec2 vUv;
|
|
32
|
+
void main(){
|
|
33
|
+
vec4 texel = texture2D(uMap, vUv);
|
|
34
|
+
gl_FragColor = vec4(texel.rgb * uTint, texel.a * uOpacity);
|
|
35
|
+
#include <tonemapping_fragment>
|
|
36
|
+
#include <colorspace_fragment>
|
|
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;
|
|
118
|
+
function resolveTexture(source, name) {
|
|
119
|
+
if (!source)
|
|
120
|
+
return undefined;
|
|
121
|
+
if (typeof source === 'function')
|
|
122
|
+
return source(name);
|
|
123
|
+
if (source instanceof Map)
|
|
124
|
+
return source.get(name);
|
|
125
|
+
return Object.prototype.hasOwnProperty.call(source, name) ? source[name] : undefined;
|
|
126
|
+
}
|
|
127
|
+
const scratchColor = new T.Color();
|
|
128
|
+
const scratchRgba = [0, 0, 0, 0];
|
|
129
|
+
const scratchVec = new T.Vector3();
|
|
130
|
+
/**
|
|
131
|
+
* One playing copy of an effect. Shared by the VFX Editor and games: it owns
|
|
132
|
+
* no renderer, no DOM and no animation loop -- add `root` to a scene and call
|
|
133
|
+
* `update(seconds)` from your own frame loop.
|
|
134
|
+
*
|
|
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.
|
|
138
|
+
*/
|
|
139
|
+
export class VfxInstance {
|
|
140
|
+
constructor(document, options = {}) {
|
|
141
|
+
this.root = new T.Group();
|
|
142
|
+
/** Seconds of simulated time since start (or the last reset). */
|
|
143
|
+
this.time = 0;
|
|
144
|
+
this.particleLayers = [];
|
|
145
|
+
this.byId = new Map();
|
|
146
|
+
this.flipbookLayers = [];
|
|
147
|
+
this.state = 1;
|
|
148
|
+
this.accumulator = 0;
|
|
149
|
+
this.live = 0;
|
|
150
|
+
this.document = parseVfx(document);
|
|
151
|
+
this.state = this.document.seed;
|
|
152
|
+
this.root.name = this.document.name;
|
|
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
|
+
});
|
|
161
|
+
for (const config of this.document.flipbooks ?? [])
|
|
162
|
+
this.flipbookLayers.push(this.buildFlipbook(config, resolveTexture(options.textures, config.texture)));
|
|
163
|
+
this.draw();
|
|
164
|
+
}
|
|
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;
|
|
170
|
+
const material = new T.ShaderMaterial({
|
|
171
|
+
transparent: true, depthWrite: false, side: T.DoubleSide,
|
|
172
|
+
blending: config.blend === 'additive' ? T.AdditiveBlending : T.NormalBlending,
|
|
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,
|
|
182
|
+
});
|
|
183
|
+
const mesh = new T.Mesh(geometry, material);
|
|
184
|
+
mesh.frustumCulled = false;
|
|
185
|
+
mesh.name = config.id;
|
|
186
|
+
mesh.renderOrder = index;
|
|
187
|
+
if (config.space === 'world')
|
|
188
|
+
mesh.matrixAutoUpdate = false;
|
|
189
|
+
this.root.add(mesh);
|
|
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;
|
|
208
|
+
}
|
|
209
|
+
buildFlipbook(config, texture) {
|
|
210
|
+
const material = new T.ShaderMaterial({
|
|
211
|
+
transparent: true, depthWrite: false, side: T.DoubleSide,
|
|
212
|
+
blending: config.blend === 'additive' ? T.AdditiveBlending : T.NormalBlending,
|
|
213
|
+
uniforms: {
|
|
214
|
+
uMap: { value: texture ?? null }, uSize: { value: new T.Vector2(config.size, config.size) },
|
|
215
|
+
uOrigin: { value: new T.Vector2(config.originU, config.originV) }, uCell: { value: new T.Vector3(config.cols, config.rows, 0) },
|
|
216
|
+
uGround: { value: config.facing === 'ground' ? 1 : 0 }, uTint: { value: new T.Color(config.color) }, uOpacity: { value: config.opacity },
|
|
217
|
+
},
|
|
218
|
+
vertexShader: FLIPBOOK_VERTEX, fragmentShader: FLIPBOOK_FRAGMENT,
|
|
219
|
+
});
|
|
220
|
+
const mesh = new T.Mesh(SHARED_QUAD, material);
|
|
221
|
+
mesh.frustumCulled = false;
|
|
222
|
+
mesh.name = config.id;
|
|
223
|
+
mesh.visible = false;
|
|
224
|
+
this.root.add(mesh);
|
|
225
|
+
return { config, mesh, material, texture, done: !config.enabled };
|
|
226
|
+
}
|
|
227
|
+
random() { this.state = this.state * 16807 % 2147483647; return (this.state - 1) / 2147483646; }
|
|
228
|
+
/** Back to time 0 with the original seed: a replay is identical to the first play. */
|
|
229
|
+
reset() {
|
|
230
|
+
this.time = 0;
|
|
231
|
+
this.accumulator = 0;
|
|
232
|
+
this.state = this.document.seed;
|
|
233
|
+
this.live = 0;
|
|
234
|
+
for (const l of this.particleLayers) {
|
|
235
|
+
l.particles = [];
|
|
236
|
+
l.fraction = 0;
|
|
237
|
+
l.cycle = 0;
|
|
238
|
+
l.burstEmitted.fill(0);
|
|
239
|
+
}
|
|
240
|
+
for (const f of this.flipbookLayers)
|
|
241
|
+
f.done = !f.config.enabled;
|
|
242
|
+
this.draw();
|
|
243
|
+
}
|
|
244
|
+
get particleCount() { return this.live; }
|
|
245
|
+
/** True once a NON-looping effect has stopped emitting, every particle has
|
|
246
|
+
* died and every flipbook has played out -- the moment to `dispose()` a
|
|
247
|
+
* one-shot. Always false for a looping effect. */
|
|
248
|
+
get finished() {
|
|
249
|
+
if (this.document.loop || this.time < this.document.duration)
|
|
250
|
+
return false;
|
|
251
|
+
return this.live === 0 && this.flipbookLayers.every((f) => f.done);
|
|
252
|
+
}
|
|
253
|
+
/** Kept for 1.35 callers. Particles are world-sized quads now, so the viewport no longer matters. */
|
|
254
|
+
setViewportHeight(_height) { }
|
|
255
|
+
/** Advance by `delta` seconds. Gaps over 250 ms are capped, so a backgrounded tab does not dump a burst. */
|
|
256
|
+
update(delta) {
|
|
257
|
+
if (!Number.isFinite(delta) || delta < 0)
|
|
258
|
+
return;
|
|
259
|
+
this.accumulator += Math.min(delta, .25);
|
|
260
|
+
while (this.accumulator >= 1 / 60) {
|
|
261
|
+
this.step(1 / 60);
|
|
262
|
+
this.accumulator -= 1 / 60;
|
|
263
|
+
}
|
|
264
|
+
this.draw();
|
|
265
|
+
}
|
|
266
|
+
/** Whether the effect is still starting new particles / new flipbook cycles. */
|
|
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
|
+
}
|
|
274
|
+
step(dt) {
|
|
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;
|
|
278
|
+
this.time += dt;
|
|
279
|
+
this.root.updateWorldMatrix(true, false);
|
|
280
|
+
const origin = new T.Vector3();
|
|
281
|
+
for (const l of this.particleLayers) {
|
|
282
|
+
const c = l.config;
|
|
283
|
+
this.origin(l, origin);
|
|
284
|
+
const kept = [];
|
|
285
|
+
for (const p of l.particles) {
|
|
286
|
+
p.age += dt;
|
|
287
|
+
if (p.age >= p.life) {
|
|
288
|
+
this.live--;
|
|
289
|
+
this.spawnChildren(l, p, 'death', dt);
|
|
290
|
+
continue;
|
|
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;
|
|
338
|
+
}
|
|
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
|
+
});
|
|
492
|
+
}
|
|
493
|
+
/** The frame a flipbook shows now, or -1 for none. A looping flipbook keeps
|
|
494
|
+
* cycling while the effect runs and, once it stops, finishes its current
|
|
495
|
+
* cycle -- the flipbook counterpart of particles draining. */
|
|
496
|
+
frameOf(f) {
|
|
497
|
+
const t = this.time - f.delay;
|
|
498
|
+
if (!f.enabled || t < 0)
|
|
499
|
+
return -1;
|
|
500
|
+
const n = Math.floor(t * f.fps);
|
|
501
|
+
if (!f.loop)
|
|
502
|
+
return n < f.frames ? n : -1;
|
|
503
|
+
if (this.running)
|
|
504
|
+
return n % f.frames;
|
|
505
|
+
// Stopped: the cycle that was playing when the effect stopped may finish.
|
|
506
|
+
const stoppedAt = Math.floor(Math.max(0, this.document.duration - f.delay) * f.fps);
|
|
507
|
+
const cycleEnd = (Math.floor(stoppedAt / f.frames) + 1) * f.frames;
|
|
508
|
+
return n < cycleEnd ? n % f.frames : -1;
|
|
509
|
+
}
|
|
510
|
+
draw() {
|
|
511
|
+
if (this.particleLayers.some((l) => l.config.space === 'world'))
|
|
512
|
+
this.root.updateWorldMatrix(true, false);
|
|
513
|
+
for (const l of this.particleLayers) {
|
|
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])
|
|
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();
|
|
555
|
+
}
|
|
556
|
+
for (const f of this.flipbookLayers) {
|
|
557
|
+
const frame = this.frameOf(f.config);
|
|
558
|
+
if (frame < 0 && this.time >= f.config.delay)
|
|
559
|
+
f.done = true;
|
|
560
|
+
if (frame >= 0)
|
|
561
|
+
f.done = false;
|
|
562
|
+
const image = f.texture?.image;
|
|
563
|
+
const ready = !!(image && image.width && image.height);
|
|
564
|
+
f.mesh.visible = frame >= 0 && ready;
|
|
565
|
+
if (!f.mesh.visible)
|
|
566
|
+
continue;
|
|
567
|
+
const c = f.config, u = f.material.uniforms;
|
|
568
|
+
// Height follows the CELL's aspect, read from the sheet itself, so a
|
|
569
|
+
// non-square sheet or grid is never stretched.
|
|
570
|
+
const cellAspect = (image.height / c.rows) / (image.width / c.cols);
|
|
571
|
+
u.uSize.value.set(c.size, c.size * cellAspect);
|
|
572
|
+
u.uCell.value.z = frame;
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
/** Frees this instance's GPU buffers. Textures are NOT disposed: they are
|
|
576
|
+
* shared between instances and belong to whoever loaded them (`VfxEffect`). */
|
|
577
|
+
dispose() {
|
|
578
|
+
for (const l of this.particleLayers) {
|
|
579
|
+
l.geometry.dispose();
|
|
580
|
+
l.material.dispose();
|
|
581
|
+
}
|
|
582
|
+
for (const f of this.flipbookLayers)
|
|
583
|
+
f.material.dispose();
|
|
584
|
+
this.particleLayers = [];
|
|
585
|
+
this.flipbookLayers = [];
|
|
586
|
+
this.byId.clear();
|
|
587
|
+
this.live = 0;
|
|
588
|
+
this.root.removeFromParent();
|
|
589
|
+
this.root.clear();
|
|
590
|
+
}
|
|
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
|
+
}
|