@umicat/three-sdk 1.37.0 → 1.38.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 +11 -2
- package/dist/vfx/VfxInstance.d.ts +4 -0
- package/dist/vfx/VfxInstance.js +145 -39
- package/dist/vfx/document.d.ts +30 -2
- package/dist/vfx/document.js +84 -6
- package/dist/vfx/index.d.ts +1 -0
- package/dist/vfx/index.js +1 -0
- package/dist/vfx/material.d.ts +28 -4
- package/dist/vfx/material.js +51 -19
- package/dist/vfx/mesh.d.ts +96 -0
- package/dist/vfx/mesh.js +161 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -101,8 +101,17 @@ ramp, a gradient map for colour, an optional glowing edge, and HDR emission.
|
|
|
101
101
|
`dissolve`, `threshold`, `opacity`, `emission` and each mask's `strength` /
|
|
102
102
|
`offset` are a number or `{ overLife: Curve }`. See `src/vfx/material.ts`.
|
|
103
103
|
|
|
104
|
-
**
|
|
105
|
-
|
|
104
|
+
**Meshes (version 4, 1.38.0, ADR-055 step 2).** `meshes: [...]` places Godot's
|
|
105
|
+
primitive meshes (quad, plane, sphere, cylinder/cone, torus, box, flat ring)
|
|
106
|
+
in the effect, each shown for a `start`/`life` window of the cycle with scale,
|
|
107
|
+
colour and alpha over that life and drawn with a material; `emitter.mesh`
|
|
108
|
+
draws every particle as such a mesh (`size` scales it; it stands on its
|
|
109
|
+
velocity with `align: velocity`). Materials gained rim (fresnel),
|
|
110
|
+
displacement, a cutout threshold and a band gradient; emitters gained
|
|
111
|
+
per-axis `sizeOverLifeXYZ`. Meshes draw before particles.
|
|
112
|
+
|
|
113
|
+
**Write the lowest version.** `minimumVersion(doc)` is 4 with meshes, 3 with a
|
|
114
|
+
material, else 2; the editor and the agent write that, so an effect that needs nothing
|
|
106
115
|
new stays version 2 and keeps playing on 1.36.
|
|
107
116
|
|
|
108
117
|
## What is proven, and by what
|
|
@@ -24,6 +24,7 @@ export declare class VfxInstance {
|
|
|
24
24
|
private particleLayers;
|
|
25
25
|
private byId;
|
|
26
26
|
private flipbookLayers;
|
|
27
|
+
private meshLayers;
|
|
27
28
|
private state;
|
|
28
29
|
/** Particles born so far: hashed into each particle's `seed`. */
|
|
29
30
|
private serial;
|
|
@@ -33,6 +34,9 @@ export declare class VfxInstance {
|
|
|
33
34
|
private buildParticles;
|
|
34
35
|
/** (Re)creates the per-instance buffers with room for `capacity` particles. */
|
|
35
36
|
private allocate;
|
|
37
|
+
private buildMesh;
|
|
38
|
+
/** A mesh layer's life fraction now, or -1 when it is not showing. */
|
|
39
|
+
private meshLife;
|
|
36
40
|
private buildFlipbook;
|
|
37
41
|
private random;
|
|
38
42
|
/** Back to time 0 with the original seed: a replay is identical to the first play. */
|
package/dist/vfx/VfxInstance.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import * as T from 'three';
|
|
2
2
|
import { parseVfx, pickRange, sampleCurve, sampleGradient, LIMITS, } from './document.js';
|
|
3
|
-
import { compileMaterial } from './material.js';
|
|
3
|
+
import { compileMaterial, materialDefaults } from './material.js';
|
|
4
|
+
import { meshGeometry, fixRingUv, meshLayerVertex, meshParticleVertex, meshFragment } from './mesh.js';
|
|
5
|
+
/** Closed shapes are drawn front-only, so an additive sphere does not show its back through its front. */
|
|
6
|
+
const closed = (s) => s.type === 'sphere' || s.type === 'box' || s.type === 'torus' || (s.type === 'cylinder' && s.caps);
|
|
7
|
+
const SIDE = { front: T.FrontSide, back: T.BackSide, both: T.DoubleSide };
|
|
4
8
|
const SHARED_QUAD = (() => {
|
|
5
9
|
// Corners in [0,1]^2 with y UP; the shader turns them into a cell-sized quad.
|
|
6
10
|
const g = new T.BufferGeometry();
|
|
@@ -45,16 +49,16 @@ const PARTICLE_QUAD = (() => {
|
|
|
45
49
|
})();
|
|
46
50
|
// iMisc = (size, angle radians, frame, unused)
|
|
47
51
|
const PARTICLE_VERTEX = `
|
|
48
|
-
attribute vec3 iPos; attribute vec4 iColor; attribute vec4 iMisc; attribute vec3 iVel; attribute vec2 iLife;
|
|
52
|
+
attribute vec3 iPos; attribute vec4 iColor; attribute vec4 iMisc; attribute vec3 iVel; attribute vec2 iLife; attribute vec3 iAxes;
|
|
49
53
|
uniform float uAlign; uniform float uStretch; uniform vec2 uCell; uniform mat3 uOrient; uniform vec3 uAxis;
|
|
50
54
|
varying vec2 vUv; varying vec2 vQuad; varying vec4 vColor; varying vec2 vLife;
|
|
51
55
|
void main(){
|
|
52
|
-
vec2 c = position.xy;
|
|
53
|
-
vQuad =
|
|
56
|
+
vec2 c = position.xy * iAxes.xy; // per-axis scale over life; the UVs below stay on the unscaled corner
|
|
57
|
+
vQuad = position.xy * 2.0;
|
|
54
58
|
vColor = iColor;
|
|
55
59
|
vLife = iLife;
|
|
56
60
|
float size = iMisc.x, angle = iMisc.y, frame = iMisc.z;
|
|
57
|
-
vec2 uv =
|
|
61
|
+
vec2 uv = position.xy + 0.5;
|
|
58
62
|
vUv = vec2((mod(frame, uCell.x) + uv.x) / uCell.x, 1.0 - (floor(frame / uCell.x) + 1.0 - uv.y) / uCell.y);
|
|
59
63
|
float s = sin(angle), co = cos(angle);
|
|
60
64
|
vec2 turned = vec2(c.x * co - c.y * s, c.x * s + c.y * co) * size;
|
|
@@ -121,9 +125,9 @@ ${c.code}
|
|
|
121
125
|
float vfxBayer(vec2 p){ ivec2 q = ivec2(mod(p, 4.0)); int i = q.x + q.y * 4;
|
|
122
126
|
float m[16] = float[16](0.,8.,2.,10.,12.,4.,14.,6.,3.,11.,1.,9.,15.,7.,13.,5.); return (m[i] + 0.5) / 16.0; }
|
|
123
127
|
void main(){
|
|
124
|
-
vec4 m = vfxMaterial(vQuad * 0.5 + 0.5, vLife);
|
|
128
|
+
vec4 m = vfxMaterial(vQuad * 0.5 + 0.5, vLife, 1.0);
|
|
125
129
|
float a = m.a * vColor.a;
|
|
126
|
-
${c.alphaMode === 'cutout' ? 'if (a <
|
|
130
|
+
${c.alphaMode === 'cutout' ? 'if (a < uCutoff) discard; a = 1.0;' : c.alphaMode === 'dither' ? 'if (a < vfxBayer(gl_FragCoord.xy)) discard; a = 1.0;' : 'if (a <= 0.002) discard;'}
|
|
127
131
|
gl_FragColor = vec4(m.rgb * vColor.rgb, a);
|
|
128
132
|
#include <tonemapping_fragment>
|
|
129
133
|
#include <colorspace_fragment>
|
|
@@ -160,6 +164,7 @@ export class VfxInstance {
|
|
|
160
164
|
this.particleLayers = [];
|
|
161
165
|
this.byId = new Map();
|
|
162
166
|
this.flipbookLayers = [];
|
|
167
|
+
this.meshLayers = [];
|
|
163
168
|
this.state = 1;
|
|
164
169
|
/** Particles born so far: hashed into each particle's `seed`. */
|
|
165
170
|
this.serial = 0;
|
|
@@ -176,32 +181,32 @@ export class VfxInstance {
|
|
|
176
181
|
});
|
|
177
182
|
for (const config of this.document.flipbooks ?? [])
|
|
178
183
|
this.flipbookLayers.push(this.buildFlipbook(config, resolveTexture(options.textures, config.texture)));
|
|
184
|
+
(this.document.meshes ?? []).forEach((config, i) => this.meshLayers.push(this.buildMesh(config, i, (name) => resolveTexture(options.textures, name))));
|
|
179
185
|
this.draw();
|
|
180
186
|
}
|
|
181
187
|
buildParticles(config, index, root, textureOf) {
|
|
182
188
|
const geometry = new T.InstancedBufferGeometry();
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
+
const look = config.look, solid = config.mesh;
|
|
190
|
+
if (solid) {
|
|
191
|
+
// A mesh particle: the shape's own buffers, instanced.
|
|
192
|
+
const g = fixRingUv(meshGeometry(solid), solid);
|
|
193
|
+
geometry.index = g.index;
|
|
194
|
+
for (const k of ['position', 'normal', 'uv'])
|
|
195
|
+
geometry.setAttribute(k, g.getAttribute(k));
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
geometry.index = PARTICLE_QUAD.index;
|
|
199
|
+
geometry.setAttribute('position', PARTICLE_QUAD.getAttribute('position'));
|
|
200
|
+
}
|
|
201
|
+
const texture = !solid && (look.type === 'texture' || look.type === 'sheet') ? textureOf(look.texture) : undefined;
|
|
202
|
+
const images = texture || (!solid && (look.type === 'texture' || look.type === 'sheet')) ? [texture] : [];
|
|
203
|
+
// A mesh particle always draws through a material: its look's, or a solid one.
|
|
204
|
+
const compiled = look.type === 'material' ? compileMaterial(look.material) : solid ? compileMaterial({ ...materialDefaults(), masks: [] }) : undefined;
|
|
189
205
|
const repeats = [];
|
|
190
206
|
if (compiled)
|
|
191
|
-
|
|
192
|
-
const t = textureOf(name);
|
|
193
|
-
images.push(t);
|
|
194
|
-
if (!t) {
|
|
195
|
-
compiled.uniforms[`uMask${i}`].value = null;
|
|
196
|
-
continue;
|
|
197
|
-
}
|
|
198
|
-
const copy = t.clone();
|
|
199
|
-
copy.wrapS = copy.wrapT = T.RepeatWrapping;
|
|
200
|
-
repeats.push({ source: t, copy, version: -1 });
|
|
201
|
-
compiled.uniforms[`uMask${i}`].value = copy;
|
|
202
|
-
}
|
|
207
|
+
bindImages(compiled, textureOf, images, repeats);
|
|
203
208
|
const material = new T.ShaderMaterial({
|
|
204
|
-
transparent: true, depthWrite: false, side: T.DoubleSide,
|
|
209
|
+
transparent: true, depthWrite: false, side: solid && closed(solid) ? T.FrontSide : T.DoubleSide,
|
|
205
210
|
blending: config.blend === 'additive' ? T.AdditiveBlending : T.NormalBlending,
|
|
206
211
|
uniforms: {
|
|
207
212
|
uLook: { value: LOOK_CODE[look.type] }, uSoft: { value: look.type === 'dot' ? look.softness : 0 },
|
|
@@ -212,7 +217,8 @@ export class VfxInstance {
|
|
|
212
217
|
uOrient: { value: new T.Matrix3().setFromMatrix4(new T.Matrix4().makeRotationFromQuaternion(emitterQuaternion(config))) },
|
|
213
218
|
...compiled?.uniforms,
|
|
214
219
|
},
|
|
215
|
-
vertexShader:
|
|
220
|
+
vertexShader: solid ? meshParticleVertex(compiled) : PARTICLE_VERTEX,
|
|
221
|
+
fragmentShader: solid ? meshFragment(compiled) : compiled ? materialFragment(compiled) : PARTICLE_FRAGMENT,
|
|
216
222
|
});
|
|
217
223
|
const mesh = new T.Mesh(geometry, material);
|
|
218
224
|
mesh.frustumCulled = false;
|
|
@@ -232,15 +238,56 @@ export class VfxInstance {
|
|
|
232
238
|
/** (Re)creates the per-instance buffers with room for `capacity` particles. */
|
|
233
239
|
allocate(layer, capacity) {
|
|
234
240
|
const make = (size) => new T.InstancedBufferAttribute(new Float32Array(capacity * size), size).setUsage(T.DynamicDrawUsage);
|
|
235
|
-
layer.attributes = { pos: make(3), color: make(4), misc: make(4), vel: make(3), life: make(2) };
|
|
241
|
+
layer.attributes = { pos: make(3), color: make(4), misc: make(4), vel: make(3), life: make(2), axes: make(3) };
|
|
236
242
|
const g = layer.geometry;
|
|
237
243
|
g.setAttribute('iPos', layer.attributes.pos);
|
|
238
244
|
g.setAttribute('iColor', layer.attributes.color);
|
|
239
245
|
g.setAttribute('iMisc', layer.attributes.misc);
|
|
240
246
|
g.setAttribute('iVel', layer.attributes.vel);
|
|
241
247
|
g.setAttribute('iLife', layer.attributes.life);
|
|
248
|
+
g.setAttribute('iAxes', layer.attributes.axes);
|
|
242
249
|
layer.capacity = capacity;
|
|
243
250
|
}
|
|
251
|
+
buildMesh(config, index, textureOf) {
|
|
252
|
+
const geometry = fixRingUv(meshGeometry(config.shape), config.shape);
|
|
253
|
+
const compiled = compileMaterial(config.material);
|
|
254
|
+
const images = [], repeats = [];
|
|
255
|
+
bindImages(compiled, textureOf, images, repeats);
|
|
256
|
+
const s = config.shape;
|
|
257
|
+
const material = new T.ShaderMaterial({
|
|
258
|
+
transparent: true, depthWrite: false,
|
|
259
|
+
side: config.side === 'both' && closed(s) ? T.FrontSide : SIDE[config.side],
|
|
260
|
+
blending: config.blend === 'additive' ? T.AdditiveBlending : T.NormalBlending,
|
|
261
|
+
uniforms: {
|
|
262
|
+
uLayerColor: { value: new T.Vector4(1, 1, 1, 1) }, uLayerLife: { value: new T.Vector2(0, 0) },
|
|
263
|
+
uFacing: { value: config.facing === 'camera' ? (s.type === 'quad' ? 1 : s.type === 'plane' ? 2 : 0) : 0 },
|
|
264
|
+
...compiled.uniforms,
|
|
265
|
+
},
|
|
266
|
+
vertexShader: meshLayerVertex(compiled), fragmentShader: meshFragment(compiled),
|
|
267
|
+
});
|
|
268
|
+
const mesh = new T.Mesh(geometry, material);
|
|
269
|
+
// Meshes draw BEFORE particles: an effect's meshes are its core, its ground rings and discs, which particles fly in front of.
|
|
270
|
+
mesh.name = config.id;
|
|
271
|
+
mesh.frustumCulled = false;
|
|
272
|
+
mesh.renderOrder = -100 + index;
|
|
273
|
+
mesh.visible = false;
|
|
274
|
+
mesh.position.set(...config.position);
|
|
275
|
+
mesh.rotation.set(config.rotation[0] * DEG, config.rotation[1] * DEG, config.rotation[2] * DEG, 'XYZ');
|
|
276
|
+
this.root.add(mesh);
|
|
277
|
+
return { config, mesh, geometry, material, compiled, images, repeats, seed: seedOf(this.document.seed, 100000 + index) };
|
|
278
|
+
}
|
|
279
|
+
/** A mesh layer's life fraction now, or -1 when it is not showing. */
|
|
280
|
+
meshLife(c) {
|
|
281
|
+
if (!c.enabled)
|
|
282
|
+
return -1;
|
|
283
|
+
const d = this.document.duration, life = c.life ?? Math.max(.01, d - c.start);
|
|
284
|
+
const cycleTime = this.document.loop ? this.time % d : this.time;
|
|
285
|
+
let t = (cycleTime - c.start) / life;
|
|
286
|
+
// A looping layer that outlives its cycle carries over into the next one.
|
|
287
|
+
if (this.document.loop && t < 0 && this.time >= d)
|
|
288
|
+
t = (cycleTime + d - c.start) / life;
|
|
289
|
+
return t >= 0 && t < 1 ? t : -1;
|
|
290
|
+
}
|
|
244
291
|
buildFlipbook(config, texture) {
|
|
245
292
|
const material = new T.ShaderMaterial({
|
|
246
293
|
transparent: true, depthWrite: false, side: T.DoubleSide,
|
|
@@ -284,7 +331,8 @@ export class VfxInstance {
|
|
|
284
331
|
get finished() {
|
|
285
332
|
if (this.document.loop || this.time < this.document.duration)
|
|
286
333
|
return false;
|
|
287
|
-
|
|
334
|
+
const meshesDone = this.meshLayers.every(({ config: c }) => !c.enabled || this.time >= c.start + (c.life ?? Math.max(.01, this.document.duration - c.start)));
|
|
335
|
+
return this.live === 0 && meshesDone && this.flipbookLayers.every((f) => f.done);
|
|
288
336
|
}
|
|
289
337
|
/** Kept for 1.35 callers. Particles are world-sized quads now, so the viewport no longer matters. */
|
|
290
338
|
setViewportHeight(_height) { }
|
|
@@ -551,8 +599,9 @@ export class VfxInstance {
|
|
|
551
599
|
const c = l.config, n = l.particles.length;
|
|
552
600
|
if (n > l.capacity)
|
|
553
601
|
this.allocate(l, Math.min(LIMITS.particles, Math.max(n, l.capacity * 2)));
|
|
554
|
-
const { pos, color, misc, vel, life } = l.attributes;
|
|
555
|
-
const P = pos.array, C = color.array, M = misc.array, V = vel.array, L = life.array;
|
|
602
|
+
const { pos, color, misc, vel, life, axes } = l.attributes;
|
|
603
|
+
const P = pos.array, C = color.array, M = misc.array, V = vel.array, L = life.array, A = axes.array;
|
|
604
|
+
const xyz = c.sizeOverLifeXYZ;
|
|
556
605
|
const look = c.look;
|
|
557
606
|
for (let i = 0; i < n; i++) {
|
|
558
607
|
const p = l.particles[i], t = p.age / p.life;
|
|
@@ -581,25 +630,47 @@ export class VfxInstance {
|
|
|
581
630
|
M[i * 4 + 1] = p.angle;
|
|
582
631
|
L[i * 2] = t;
|
|
583
632
|
L[i * 2 + 1] = p.seed;
|
|
633
|
+
A[i * 3] = xyz?.x ? sampleCurve(xyz.x, t) : 1;
|
|
634
|
+
A[i * 3 + 1] = xyz?.y ? sampleCurve(xyz.y, t) : 1;
|
|
635
|
+
A[i * 3 + 2] = xyz?.z ? sampleCurve(xyz.z, t) : 1;
|
|
584
636
|
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;
|
|
585
637
|
}
|
|
586
638
|
l.geometry.instanceCount = n;
|
|
587
|
-
for (const attribute of [pos, color, misc, vel, life])
|
|
639
|
+
for (const attribute of [pos, color, misc, vel, life, axes])
|
|
588
640
|
attribute.needsUpdate = true;
|
|
589
641
|
if (l.compiled)
|
|
590
642
|
l.compiled.uniforms.uTime.value = this.time;
|
|
591
|
-
|
|
592
|
-
for (const r of l.repeats)
|
|
593
|
-
if (r.version !== r.source.version && r.source.image?.width) {
|
|
594
|
-
r.copy.image = r.source.image;
|
|
595
|
-
r.copy.needsUpdate = true;
|
|
596
|
-
r.version = r.source.version;
|
|
597
|
-
}
|
|
643
|
+
syncRepeats(l.repeats);
|
|
598
644
|
// A textured look waits for its images; a missing one draws nothing.
|
|
599
645
|
l.mesh.visible = n > 0 && l.images.every((t) => !!t?.image?.width);
|
|
600
646
|
if (c.space === 'world')
|
|
601
647
|
l.mesh.matrix.copy(this.root.matrixWorld).invert();
|
|
602
648
|
}
|
|
649
|
+
for (const m of this.meshLayers) {
|
|
650
|
+
const c = m.config, t = this.meshLife(c);
|
|
651
|
+
const ready = m.images.every((x) => !!x?.image?.width);
|
|
652
|
+
m.mesh.visible = t >= 0 && ready;
|
|
653
|
+
if (!m.mesh.visible)
|
|
654
|
+
continue;
|
|
655
|
+
const k = c.scaleOverLife ? sampleCurve(c.scaleOverLife, t) : 1;
|
|
656
|
+
m.mesh.scale.set(c.scale[0] * k, c.scale[1] * k, c.scale[2] * k);
|
|
657
|
+
sampleGradient({ stops: [[0, c.color, 1]] }, 0, scratchRgba);
|
|
658
|
+
let [r, g, b] = scratchRgba, a = 1;
|
|
659
|
+
if (c.colorOverLife) {
|
|
660
|
+
sampleGradient(c.colorOverLife, t, scratchRgba);
|
|
661
|
+
r *= scratchRgba[0];
|
|
662
|
+
g *= scratchRgba[1];
|
|
663
|
+
b *= scratchRgba[2];
|
|
664
|
+
a *= scratchRgba[3];
|
|
665
|
+
}
|
|
666
|
+
if (c.alphaOverLife)
|
|
667
|
+
a *= Math.max(0, sampleCurve(c.alphaOverLife, t));
|
|
668
|
+
scratchColor.setRGB(r, g, b, T.SRGBColorSpace);
|
|
669
|
+
m.material.uniforms.uLayerColor.value.set(scratchColor.r * c.intensity, scratchColor.g * c.intensity, scratchColor.b * c.intensity, Math.min(1, a));
|
|
670
|
+
m.material.uniforms.uLayerLife.value.set(t, m.seed);
|
|
671
|
+
m.compiled.uniforms.uTime.value = this.time;
|
|
672
|
+
syncRepeats(m.repeats);
|
|
673
|
+
}
|
|
603
674
|
for (const f of this.flipbookLayers) {
|
|
604
675
|
const frame = this.frameOf(f.config);
|
|
605
676
|
if (frame < 0 && this.time >= f.config.delay)
|
|
@@ -631,14 +702,49 @@ export class VfxInstance {
|
|
|
631
702
|
}
|
|
632
703
|
for (const f of this.flipbookLayers)
|
|
633
704
|
f.material.dispose();
|
|
705
|
+
for (const m of this.meshLayers) {
|
|
706
|
+
m.geometry.dispose();
|
|
707
|
+
m.material.dispose();
|
|
708
|
+
m.compiled.dispose();
|
|
709
|
+
for (const r of m.repeats)
|
|
710
|
+
r.copy.dispose();
|
|
711
|
+
}
|
|
634
712
|
this.particleLayers = [];
|
|
635
713
|
this.flipbookLayers = [];
|
|
714
|
+
this.meshLayers = [];
|
|
636
715
|
this.byId.clear();
|
|
637
716
|
this.live = 0;
|
|
638
717
|
this.root.removeFromParent();
|
|
639
718
|
this.root.clear();
|
|
640
719
|
}
|
|
641
720
|
}
|
|
721
|
+
/**
|
|
722
|
+
* Bind a material's image masks. The shared texture is clamped for sprite
|
|
723
|
+
* sheets, so a mask gets a repeat-wrapped copy of it (same decoded image).
|
|
724
|
+
*/
|
|
725
|
+
function bindImages(compiled, textureOf, images, repeats) {
|
|
726
|
+
for (const [i, name] of compiled.images) {
|
|
727
|
+
const t = textureOf(name);
|
|
728
|
+
images.push(t);
|
|
729
|
+
if (!t) {
|
|
730
|
+
compiled.uniforms[`uMask${i}`].value = null;
|
|
731
|
+
continue;
|
|
732
|
+
}
|
|
733
|
+
const copy = t.clone();
|
|
734
|
+
copy.wrapS = copy.wrapT = T.RepeatWrapping;
|
|
735
|
+
repeats.push({ source: t, copy, version: -1 });
|
|
736
|
+
compiled.uniforms[`uMask${i}`].value = copy;
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
/** A copy made before its image decoded never uploads on its own: follow the source's version. */
|
|
740
|
+
function syncRepeats(repeats) {
|
|
741
|
+
for (const r of repeats)
|
|
742
|
+
if (r.version !== r.source.version && r.source.image?.width) {
|
|
743
|
+
r.copy.image = r.source.image;
|
|
744
|
+
r.copy.needsUpdate = true;
|
|
745
|
+
r.version = r.source.version;
|
|
746
|
+
}
|
|
747
|
+
}
|
|
642
748
|
/** A particle's material seed, 0..1, from the effect seed and its birth order. */
|
|
643
749
|
function seedOf(seed, serial) {
|
|
644
750
|
let h = Math.imul(serial + 1, 0x9e3779b1) ^ Math.imul(seed, 0x85ebca6b);
|
package/dist/vfx/document.d.ts
CHANGED
|
@@ -15,11 +15,16 @@
|
|
|
15
15
|
* Version 2. A version-1 document (the upward-firing disc emitter of 1.35) is
|
|
16
16
|
* still accepted: `parseVfx` upgrades it to the v2 emitter that plays the same.
|
|
17
17
|
*
|
|
18
|
+
* Version 4 (1.38) adds MESHES: mesh layers (`meshes`) and mesh-shaped
|
|
19
|
+
* particles (`emitter.mesh`), ADR-055 step 2. A 1.37 reader ignores unknown
|
|
20
|
+
* top-level fields, so meshes needed their own version to fail loudly there.
|
|
21
|
+
*
|
|
18
22
|
* Version 3 (1.37) adds the effect MATERIAL (`look: { type: 'material' }`,
|
|
19
23
|
* ADR-055). A document that uses none of it is still written as version 2 --
|
|
20
24
|
* `minimumVersion()` -- so games whose lockfile holds 1.36 keep playing it.
|
|
21
25
|
*/
|
|
22
26
|
import { type Material } from './material.js';
|
|
27
|
+
import { type MeshShape, type MeshLayer } from './mesh.js';
|
|
23
28
|
/** A number, or `[min, max]`: picked uniformly per particle. */
|
|
24
29
|
export type Range = number | [number, number];
|
|
25
30
|
export type Vec3 = [number, number, number];
|
|
@@ -160,6 +165,16 @@ export interface Emitter {
|
|
|
160
165
|
/** World width of one particle. */
|
|
161
166
|
size: Range;
|
|
162
167
|
sizeOverLife?: Curve;
|
|
168
|
+
/**
|
|
169
|
+
* (version 4) Per-axis scale over life on top of `size` (Godot's
|
|
170
|
+
* CurveXYZTexture scale curve): a shock ring that widens without getting
|
|
171
|
+
* taller. A quad uses x (width) and y (height). A missing axis stays 1.
|
|
172
|
+
*/
|
|
173
|
+
sizeOverLifeXYZ?: {
|
|
174
|
+
x?: Curve;
|
|
175
|
+
y?: Curve;
|
|
176
|
+
z?: Curve;
|
|
177
|
+
};
|
|
163
178
|
/** One stop: that colour. More: each particle picks a point on it at random. */
|
|
164
179
|
color: Gradient;
|
|
165
180
|
/** Multiplies `color` over life. */
|
|
@@ -180,6 +195,13 @@ export interface Emitter {
|
|
|
180
195
|
stretch: number;
|
|
181
196
|
blend: 'normal' | 'additive';
|
|
182
197
|
children: SubEmitter[];
|
|
198
|
+
/**
|
|
199
|
+
* (version 3) Draw each particle as this mesh instead of a camera-facing
|
|
200
|
+
* quad: `size` then SCALES the mesh. It stands on the emitter's up, or on
|
|
201
|
+
* its velocity / the emitter axis with align velocity / axis, turned by
|
|
202
|
+
* `angle` about that line.
|
|
203
|
+
*/
|
|
204
|
+
mesh?: MeshShape;
|
|
183
205
|
}
|
|
184
206
|
export interface Flipbook {
|
|
185
207
|
id: string;
|
|
@@ -207,7 +229,7 @@ export interface Flipbook {
|
|
|
207
229
|
blend: 'normal' | 'additive';
|
|
208
230
|
}
|
|
209
231
|
export interface VfxDocument {
|
|
210
|
-
version: 2 | 3;
|
|
232
|
+
version: 2 | 3 | 4;
|
|
211
233
|
name: string;
|
|
212
234
|
seed: number;
|
|
213
235
|
/** Seconds in one cycle. A looping effect repeats it; a one-shot stops emitting after it. */
|
|
@@ -216,6 +238,8 @@ export interface VfxDocument {
|
|
|
216
238
|
humanCurated: boolean;
|
|
217
239
|
emitters: Emitter[];
|
|
218
240
|
flipbooks?: Flipbook[];
|
|
241
|
+
/** (version 3) Meshes with a material: a shockwave ring, a fireball sphere, a ground disc. */
|
|
242
|
+
meshes?: MeshLayer[];
|
|
219
243
|
}
|
|
220
244
|
/** A version-1 emitter: a disc firing upward with linear colour/size/alpha. Read, never written. */
|
|
221
245
|
export interface EmitterV1 {
|
|
@@ -236,6 +260,7 @@ export interface EmitterV1 {
|
|
|
236
260
|
export declare const LIMITS: {
|
|
237
261
|
readonly emitters: 8;
|
|
238
262
|
readonly layers: 8;
|
|
263
|
+
readonly meshes: 8;
|
|
239
264
|
readonly particles: 4000;
|
|
240
265
|
readonly cells: 4096;
|
|
241
266
|
readonly bursts: 16;
|
|
@@ -325,7 +350,10 @@ export declare function parseVfx(value: unknown): VfxDocument;
|
|
|
325
350
|
* emitter uses the material look, else 2. The editor and the agent write
|
|
326
351
|
* this, so an effect that needs nothing new still plays on 1.36.
|
|
327
352
|
*/
|
|
328
|
-
export declare function minimumVersion(d: Pick<VfxDocument, 'emitters'>): 2 | 3;
|
|
353
|
+
export declare function minimumVersion(d: Pick<VfxDocument, 'emitters' | 'meshes'>): 2 | 3 | 4;
|
|
354
|
+
/** A new mesh layer: a soft additive ring on the ground, for the editor's "Add" and the agent. */
|
|
355
|
+
export declare function meshLayerDefaults(): Omit<MeshLayer, 'id'>;
|
|
356
|
+
export declare function meshLayer(id: string, patch?: Partial<MeshLayer>): MeshLayer;
|
|
329
357
|
/**
|
|
330
358
|
* The most particles an effect can have alive at once, estimated: per enabled
|
|
331
359
|
* emitter, rate x longest life plus every burst, and for sub-emitters, the
|
package/dist/vfx/document.js
CHANGED
|
@@ -15,12 +15,17 @@
|
|
|
15
15
|
* Version 2. A version-1 document (the upward-firing disc emitter of 1.35) is
|
|
16
16
|
* still accepted: `parseVfx` upgrades it to the v2 emitter that plays the same.
|
|
17
17
|
*
|
|
18
|
+
* Version 4 (1.38) adds MESHES: mesh layers (`meshes`) and mesh-shaped
|
|
19
|
+
* particles (`emitter.mesh`), ADR-055 step 2. A 1.37 reader ignores unknown
|
|
20
|
+
* top-level fields, so meshes needed their own version to fail loudly there.
|
|
21
|
+
*
|
|
18
22
|
* Version 3 (1.37) adds the effect MATERIAL (`look: { type: 'material' }`,
|
|
19
23
|
* ADR-055). A document that uses none of it is still written as version 2 --
|
|
20
24
|
* `minimumVersion()` -- so games whose lockfile holds 1.36 keep playing it.
|
|
21
25
|
*/
|
|
22
|
-
import { parseMaterial, materialTextures } from './material.js';
|
|
23
|
-
|
|
26
|
+
import { parseMaterial, materialTextures, materialDefaults } from './material.js';
|
|
27
|
+
import { parseMeshShape } from './mesh.js';
|
|
28
|
+
export const LIMITS = { emitters: 8, layers: 8, meshes: 8, particles: 4000, cells: 4096, bursts: 16, children: 4, curvePoints: 8, gradientStops: 16 };
|
|
24
29
|
/** Every emitter field's value when a document leaves it out. */
|
|
25
30
|
export function emitterDefaults() {
|
|
26
31
|
return {
|
|
@@ -99,6 +104,8 @@ export function vfxTextures(document) {
|
|
|
99
104
|
else if (e.look.type === 'material')
|
|
100
105
|
names.push(...materialTextures(e.look.material));
|
|
101
106
|
}
|
|
107
|
+
for (const m of document.meshes ?? [])
|
|
108
|
+
names.push(...materialTextures(m.material));
|
|
102
109
|
return [...new Set(names)];
|
|
103
110
|
}
|
|
104
111
|
// ---------------------------------------------------------------- sampling
|
|
@@ -175,8 +182,8 @@ export function parseVfx(value) {
|
|
|
175
182
|
const d = structuredClone(value);
|
|
176
183
|
const number = (v, min, max) => typeof v === 'number' && Number.isFinite(v) && v >= min && v <= max;
|
|
177
184
|
const color = (v) => typeof v === 'string' && /^#[0-9a-f]{6}$/i.test(v);
|
|
178
|
-
if (d.version !== 2 && d.version !== 3)
|
|
179
|
-
fail(`version must be 2 or
|
|
185
|
+
if (d.version !== 2 && d.version !== 3 && d.version !== 4)
|
|
186
|
+
fail(`version must be 2, 3 or 4 (or 1, which is upgraded)${typeof d.version === 'number' && d.version > 4 ? ` -- version ${d.version} was written by a newer editor: update @umicat/three-sdk` : ''}`);
|
|
180
187
|
if (typeof d.name !== 'string' || !d.name.trim() || d.name.length > 80)
|
|
181
188
|
fail('name must be 1-80 characters');
|
|
182
189
|
if (!number(d.seed, 1, 2147483646) || !Number.isInteger(d.seed))
|
|
@@ -192,8 +199,12 @@ export function parseVfx(value) {
|
|
|
192
199
|
if (d.flipbooks !== undefined && !Array.isArray(d.flipbooks))
|
|
193
200
|
fail('flipbooks must be a list');
|
|
194
201
|
const flipbooks = d.flipbooks ?? [];
|
|
202
|
+
if (d.meshes !== undefined && (!Array.isArray(d.meshes) || d.meshes.length > LIMITS.meshes))
|
|
203
|
+
fail(`meshes must be a list of 0-${LIMITS.meshes}`);
|
|
204
|
+
if (d.meshes !== undefined && d.version < 4)
|
|
205
|
+
fail('meshes are version 4: set "version": 4 (needs @umicat/three-sdk 1.38 or later)');
|
|
195
206
|
const layers = d.emitters.length + flipbooks.length;
|
|
196
|
-
if (layers
|
|
207
|
+
if (layers > LIMITS.layers || layers + (d.meshes?.length ?? 0) < 1)
|
|
197
208
|
fail(`an effect needs 1-${LIMITS.layers} layers (emitters + flipbooks), it has ${layers}`);
|
|
198
209
|
const ids = new Set();
|
|
199
210
|
const checkId = (id, at) => {
|
|
@@ -245,7 +256,7 @@ export function parseVfx(value) {
|
|
|
245
256
|
fail(`${at} is not an object`);
|
|
246
257
|
checkId(input.id, at);
|
|
247
258
|
emitterIds.add(input.id);
|
|
248
|
-
const known = new Set(['id', ...Object.keys(emitterDefaults()), ...CURVE_FIELDS, 'colorOverLife']);
|
|
259
|
+
const known = new Set(['id', ...Object.keys(emitterDefaults()), ...CURVE_FIELDS, 'colorOverLife', 'mesh', 'sizeOverLifeXYZ']);
|
|
249
260
|
for (const key of Object.keys(input))
|
|
250
261
|
if (!known.has(key))
|
|
251
262
|
fail(`${at}.${key} is not an emitter field (a version-1 field? set "version": 1, or see emitterDefaults())`);
|
|
@@ -342,6 +353,23 @@ export function parseVfx(value) {
|
|
|
342
353
|
fail(`${lat}.type must be dot, glow, star, texture, sheet or material`);
|
|
343
354
|
if (!['camera', 'velocity', 'ground', 'axis'].includes(e.align))
|
|
344
355
|
fail(`${at}.align must be "camera", "velocity", "ground" or "axis"`);
|
|
356
|
+
if (e.sizeOverLifeXYZ !== undefined) {
|
|
357
|
+
if (d.version < 4)
|
|
358
|
+
fail(`${at}.sizeOverLifeXYZ is version 4: set "version": 4 (needs @umicat/three-sdk 1.38 or later)`);
|
|
359
|
+
const xyz = e.sizeOverLifeXYZ;
|
|
360
|
+
if (!xyz || typeof xyz !== 'object' || Array.isArray(xyz))
|
|
361
|
+
fail(`${at}.sizeOverLifeXYZ must be { x?, y?, z? } curves`);
|
|
362
|
+
for (const k of Object.keys(xyz)) {
|
|
363
|
+
if (!['x', 'y', 'z'].includes(k))
|
|
364
|
+
fail(`${at}.sizeOverLifeXYZ.${k} is not an axis (x, y, z)`);
|
|
365
|
+
checkCurve(xyz[k], `${at}.sizeOverLifeXYZ.${k}`);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
if (e.mesh !== undefined) {
|
|
369
|
+
if (d.version < 4)
|
|
370
|
+
fail(`${at}.mesh is version 4: set "version": 4 (needs @umicat/three-sdk 1.38 or later)`);
|
|
371
|
+
e.mesh = parseMeshShape(e.mesh, `${at}.mesh`, fail);
|
|
372
|
+
}
|
|
345
373
|
if (!['normal', 'additive'].includes(e.blend))
|
|
346
374
|
fail(`${at}.blend must be "normal" or "additive"`);
|
|
347
375
|
if (!Array.isArray(e.children) || e.children.length > LIMITS.children)
|
|
@@ -382,6 +410,45 @@ export function parseVfx(value) {
|
|
|
382
410
|
const budget = particleBudget(d);
|
|
383
411
|
if (budget > LIMITS.particles)
|
|
384
412
|
fail(`too many live particles: the most this effect can have alive at once is about ${Math.ceil(budget)} (rate x lifetime + bursts, times sub-emitters), the limit is ${LIMITS.particles}`);
|
|
413
|
+
if (d.meshes)
|
|
414
|
+
d.meshes = d.meshes.map((raw, i) => {
|
|
415
|
+
const at = `meshes[${i}]`;
|
|
416
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw))
|
|
417
|
+
fail(`${at} is not an object`);
|
|
418
|
+
checkId(raw.id, at);
|
|
419
|
+
const base = meshLayerDefaults();
|
|
420
|
+
for (const k of Object.keys(raw))
|
|
421
|
+
if (!(k in base) && !['id', 'scaleOverLife', 'colorOverLife', 'alphaOverLife'].includes(k))
|
|
422
|
+
fail(`${at}.${k} is not a mesh-layer field (expected id, ${Object.keys(base).join(', ')}, scaleOverLife, colorOverLife, alphaOverLife)`);
|
|
423
|
+
const m = { ...base, ...raw };
|
|
424
|
+
if (typeof m.enabled !== 'boolean')
|
|
425
|
+
fail(`${at}.enabled must be a boolean`);
|
|
426
|
+
m.shape = parseMeshShape(m.shape, `${at}.shape`, fail);
|
|
427
|
+
checkVec(m.position, `${at}.position`, -100, 100);
|
|
428
|
+
checkVec(m.rotation, `${at}.rotation`, -360, 360);
|
|
429
|
+
checkVec(m.scale, `${at}.scale`, -100, 100);
|
|
430
|
+
if (m.facing !== 'none' && m.facing !== 'camera')
|
|
431
|
+
fail(`${at}.facing must be "none" or "camera"`);
|
|
432
|
+
if (!number(m.start, 0, 60))
|
|
433
|
+
fail(`${at}.start must be 0-60`);
|
|
434
|
+
if (m.life !== null && !number(m.life, .01, 60))
|
|
435
|
+
fail(`${at}.life must be null (to the end of the cycle) or 0.01-60`);
|
|
436
|
+
for (const k of ['scaleOverLife', 'alphaOverLife'])
|
|
437
|
+
if (m[k] !== undefined)
|
|
438
|
+
checkCurve(m[k], `${at}.${k}`);
|
|
439
|
+
if (!color(m.color))
|
|
440
|
+
fail(`${at}.color must be #rrggbb`);
|
|
441
|
+
if (m.colorOverLife !== undefined)
|
|
442
|
+
checkGradient(m.colorOverLife, `${at}.colorOverLife`);
|
|
443
|
+
if (!number(m.intensity, 0, 16))
|
|
444
|
+
fail(`${at}.intensity must be 0-16`);
|
|
445
|
+
if (m.blend !== 'normal' && m.blend !== 'additive')
|
|
446
|
+
fail(`${at}.blend must be "normal" or "additive"`);
|
|
447
|
+
if (!['front', 'back', 'both'].includes(m.side))
|
|
448
|
+
fail(`${at}.side must be "front", "back" or "both"`);
|
|
449
|
+
m.material = parseMaterial(m.material, `${at}.material`, fail, { checkCurve, checkGradient, texturePattern: TEXTURE_PATTERN });
|
|
450
|
+
return m;
|
|
451
|
+
});
|
|
385
452
|
flipbooks.forEach((f, i) => {
|
|
386
453
|
const at = `flipbooks[${i}]`;
|
|
387
454
|
if (!f || typeof f !== 'object')
|
|
@@ -416,8 +483,19 @@ export function parseVfx(value) {
|
|
|
416
483
|
* this, so an effect that needs nothing new still plays on 1.36.
|
|
417
484
|
*/
|
|
418
485
|
export function minimumVersion(d) {
|
|
486
|
+
if (d.emitters.some((e) => e.mesh !== undefined || e.sizeOverLifeXYZ !== undefined) || d.meshes !== undefined)
|
|
487
|
+
return 4;
|
|
419
488
|
return d.emitters.some((e) => e.look?.type === 'material') ? 3 : 2;
|
|
420
489
|
}
|
|
490
|
+
/** A new mesh layer: a soft additive ring on the ground, for the editor's "Add" and the agent. */
|
|
491
|
+
export function meshLayerDefaults() {
|
|
492
|
+
return {
|
|
493
|
+
enabled: true, shape: { type: 'ring', inner: .8, outer: 1 }, position: [0, .02, 0], rotation: [0, 0, 0], scale: [1, 1, 1],
|
|
494
|
+
facing: 'none', start: 0, life: null, color: '#ffffff', intensity: 1, blend: 'additive', side: 'both',
|
|
495
|
+
material: { ...materialDefaults(), masks: [] },
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
export function meshLayer(id, patch = {}) { return { id, ...meshLayerDefaults(), ...patch }; }
|
|
421
499
|
/**
|
|
422
500
|
* The most particles an effect can have alive at once, estimated: per enabled
|
|
423
501
|
* emitter, rate x longest life plus every burst, and for sub-emitters, the
|
package/dist/vfx/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ export * from './document.js';
|
|
|
2
2
|
export * from './templates.js';
|
|
3
3
|
export { VfxInstance } from './VfxInstance.js';
|
|
4
4
|
export * from './material.js';
|
|
5
|
+
export * from './mesh.js';
|
|
5
6
|
export type { VfxInstanceOptions, VfxTextureSource } from './VfxInstance.js';
|
|
6
7
|
export { loadVfx, VfxEffect, siblingUrl, prepareSheetTexture } from './load.js';
|
|
7
8
|
export type { LoadVfxOptions } from './load.js';
|
package/dist/vfx/index.js
CHANGED
package/dist/vfx/material.d.ts
CHANGED
|
@@ -47,12 +47,15 @@ export interface NoiseSource {
|
|
|
47
47
|
export type MaskSource = NoiseSource
|
|
48
48
|
/**
|
|
49
49
|
* linear: 0 at the left edge to 1 at the right. radial: 1 at `center` to 0
|
|
50
|
-
* half a tile away. square: the same, square.
|
|
50
|
+
* half a tile away. square: the same, square. band: a stripe across u,
|
|
51
|
+
* 1 at center.x falling to 0 `width` away (turn it with the mask's
|
|
52
|
+
* rotation; a shock ring's glowing stripe). `center` is in UVs, v up.
|
|
51
53
|
*/
|
|
52
54
|
| {
|
|
53
55
|
type: 'gradient';
|
|
54
|
-
fill: 'linear' | 'radial' | 'square';
|
|
56
|
+
fill: 'linear' | 'radial' | 'square' | 'band';
|
|
55
57
|
center: [number, number];
|
|
58
|
+
width: number;
|
|
56
59
|
}
|
|
57
60
|
/** The part-1 particle shapes, plus a ring (`radius` and `width` as fractions of the half-size). */
|
|
58
61
|
| {
|
|
@@ -101,6 +104,7 @@ export interface Mask {
|
|
|
101
104
|
strength: Animated;
|
|
102
105
|
}
|
|
103
106
|
export interface Material {
|
|
107
|
+
/** 0-3. None: the whole surface, solid (a mesh layer's default). */
|
|
104
108
|
masks: Mask[];
|
|
105
109
|
/** Taken off the mask value: 0 = whole, 1 = gone. Animate it over life to erode a particle away. */
|
|
106
110
|
dissolve: Animated;
|
|
@@ -124,8 +128,26 @@ export interface Material {
|
|
|
124
128
|
opacity: Animated;
|
|
125
129
|
/** Brightness; above 1 is HDR. Multiplies the emitter's `intensity`. */
|
|
126
130
|
emission: Animated;
|
|
127
|
-
/** blend: soft alpha. cutout:
|
|
131
|
+
/** blend: soft alpha. cutout: opaque above `cutoff`, gone below (Godot's alpha scissor). dither: a stipple instead of transparency. */
|
|
128
132
|
alphaMode: 'blend' | 'cutout' | 'dither';
|
|
133
|
+
cutoff: number;
|
|
134
|
+
/**
|
|
135
|
+
* Meshes: fade by how squarely a surface faces the viewer (fresnel).
|
|
136
|
+
* edge keeps the silhouette and thins the middle (a shield, a bubble);
|
|
137
|
+
* center keeps the middle and fades the silhouette (a soft fireball).
|
|
138
|
+
* strength 0 = off. A camera-facing quad always faces the viewer, so it
|
|
139
|
+
* has no effect on flat particles.
|
|
140
|
+
*/
|
|
141
|
+
rim: {
|
|
142
|
+
strength: number;
|
|
143
|
+
power: number;
|
|
144
|
+
mode: 'edge' | 'center';
|
|
145
|
+
};
|
|
146
|
+
/** Meshes: push each vertex along its normal by (mask 1 - offset) x amount -- lumpy smoke, a wobbling sphere. 0 = off; animate it to settle. */
|
|
147
|
+
displace: {
|
|
148
|
+
amount: Animated;
|
|
149
|
+
offset: number;
|
|
150
|
+
};
|
|
129
151
|
}
|
|
130
152
|
export declare const MATERIAL_LIMITS: {
|
|
131
153
|
readonly masks: 3;
|
|
@@ -135,7 +157,7 @@ export declare const ANIMATED_FIELDS: {
|
|
|
135
157
|
readonly dissolve: readonly [-1, 2];
|
|
136
158
|
readonly threshold: readonly [-1, 2];
|
|
137
159
|
readonly opacity: readonly [0, 1];
|
|
138
|
-
readonly emission: readonly [0,
|
|
160
|
+
readonly emission: readonly [0, 32];
|
|
139
161
|
};
|
|
140
162
|
export declare function noiseDefaults(): NoiseSource;
|
|
141
163
|
export declare function maskDefaults(source?: MaskSource): Mask;
|
|
@@ -180,6 +202,8 @@ export interface CompiledMaterial {
|
|
|
180
202
|
/** Image filenames, by mask index, the caller binds into `uMask<i>`. */
|
|
181
203
|
images: Map<number, string>;
|
|
182
204
|
alphaMode: Material['alphaMode'];
|
|
205
|
+
/** The vertex shader pushes along the normal by `vfxDisplace(uv, life)` (defined in `code`). */
|
|
206
|
+
displace: boolean;
|
|
183
207
|
dispose(): void;
|
|
184
208
|
}
|
|
185
209
|
export declare function compileMaterial(m: Material): CompiledMaterial;
|
package/dist/vfx/material.js
CHANGED
|
@@ -24,7 +24,7 @@ import { sampleCurve, sampleGradient } from './document.js';
|
|
|
24
24
|
/** Binbun's twelve (`util/blend.gdshaderinc`), in its order. dodge is a / (1 - b); linearBurn a + b - 1. */
|
|
25
25
|
export const MASK_BLENDS = ['add', 'subtract', 'multiply', 'min', 'max', 'screen', 'overlay', 'hardLight', 'softLight', 'dodge', 'burn', 'linearBurn'];
|
|
26
26
|
export const MATERIAL_LIMITS = { masks: 3, curves: 8 };
|
|
27
|
-
export const ANIMATED_FIELDS = { dissolve: [-1, 2], threshold: [-1, 2], opacity: [0, 1], emission: [0,
|
|
27
|
+
export const ANIMATED_FIELDS = { dissolve: [-1, 2], threshold: [-1, 2], opacity: [0, 1], emission: [0, 32] };
|
|
28
28
|
export function noiseDefaults() {
|
|
29
29
|
return { type: 'noise', kind: 'perlin', fractal: 'fbm', octaves: 4, frequency: 4, seed: 1, warp: 0, pingpong: 2, size: 256 };
|
|
30
30
|
}
|
|
@@ -39,7 +39,8 @@ export function materialDefaults() {
|
|
|
39
39
|
return {
|
|
40
40
|
masks: [maskDefaults(shapeSource('dot')), { ...maskDefaults(noiseDefaults()), blend: 'multiply', randomOffset: true }],
|
|
41
41
|
dissolve: 0, threshold: 0, softness: .3, ramp: 'smooth', power: 1, edge: { width: 0, color: '#ffffff', emission: 2 },
|
|
42
|
-
color: { stops: [[0, '#ffffff', 1]] }, opacity: 1, emission: 1, alphaMode: 'blend',
|
|
42
|
+
color: { stops: [[0, '#ffffff', 1]] }, opacity: 1, emission: 1, alphaMode: 'blend', cutoff: .5,
|
|
43
|
+
rim: { strength: 0, power: 2, mode: 'edge' }, displace: { amount: 0, offset: .5 },
|
|
43
44
|
};
|
|
44
45
|
}
|
|
45
46
|
const isCurve = (a) => typeof a === 'object' && a !== null;
|
|
@@ -95,8 +96,23 @@ export function parseMaterial(input, at, fail, h) {
|
|
|
95
96
|
h.checkGradient(m.color, `${at}.color`);
|
|
96
97
|
if (!['blend', 'cutout', 'dither'].includes(m.alphaMode))
|
|
97
98
|
fail(`${at}.alphaMode must be "blend", "cutout" or "dither"`);
|
|
98
|
-
|
|
99
|
-
|
|
99
|
+
m.rim = { ...base.rim, ...m.rim };
|
|
100
|
+
known(m.rim, ['strength', 'power', 'mode'], `${at}.rim`, fail);
|
|
101
|
+
if (!num(m.rim.strength, 0, 1))
|
|
102
|
+
fail(`${at}.rim.strength must be 0-1`);
|
|
103
|
+
if (!num(m.rim.power, .25, 8))
|
|
104
|
+
fail(`${at}.rim.power must be 0.25-8`);
|
|
105
|
+
if (m.rim.mode !== 'edge' && m.rim.mode !== 'center')
|
|
106
|
+
fail(`${at}.rim.mode must be "edge" or "center"`);
|
|
107
|
+
m.displace = { ...base.displace, ...m.displace };
|
|
108
|
+
known(m.displace, ['amount', 'offset'], `${at}.displace`, fail);
|
|
109
|
+
animated(m.displace.amount, `${at}.displace.amount`, -5, 5);
|
|
110
|
+
if (!num(m.cutoff, 0, 1))
|
|
111
|
+
fail(`${at}.cutoff must be 0-1`);
|
|
112
|
+
if (!num(m.displace.offset, 0, 1))
|
|
113
|
+
fail(`${at}.displace.offset must be 0-1`);
|
|
114
|
+
if (!Array.isArray(m.masks) || m.masks.length > MATERIAL_LIMITS.masks)
|
|
115
|
+
fail(`${at}.masks must be a list of 0-${MATERIAL_LIMITS.masks} (none = solid)`);
|
|
100
116
|
m.masks = m.masks.map((raw, i) => {
|
|
101
117
|
const mat = `${at}.masks[${i}]`;
|
|
102
118
|
if (!raw || typeof raw !== 'object' || Array.isArray(raw))
|
|
@@ -158,10 +174,12 @@ function parseSource(s, at, fail, texturePattern) {
|
|
|
158
174
|
return n;
|
|
159
175
|
}
|
|
160
176
|
if (type === 'gradient') {
|
|
161
|
-
const g = { fill: 'radial', center: [.5, .5], ...s };
|
|
162
|
-
known(g, ['type', 'fill', 'center'], at, fail);
|
|
163
|
-
if (!['linear', 'radial', 'square'].includes(g.fill))
|
|
164
|
-
fail(`${at}.fill must be linear, radial or
|
|
177
|
+
const g = { fill: 'radial', center: [.5, .5], width: .25, ...s };
|
|
178
|
+
known(g, ['type', 'fill', 'center', 'width'], at, fail);
|
|
179
|
+
if (!['linear', 'radial', 'square', 'band'].includes(g.fill))
|
|
180
|
+
fail(`${at}.fill must be linear, radial, square or band`);
|
|
181
|
+
if (!num(g.width, .001, 1))
|
|
182
|
+
fail(`${at}.width must be 0.001-1`);
|
|
165
183
|
if (!Array.isArray(g.center) || g.center.length !== 2 || !g.center.every((x) => num(x, -1, 2)))
|
|
166
184
|
fail(`${at}.center must be [u, v], each -1-2`);
|
|
167
185
|
return g;
|
|
@@ -197,6 +215,8 @@ function curveFields(m) {
|
|
|
197
215
|
if (isCurve(v))
|
|
198
216
|
out.push({ key: k, value: v });
|
|
199
217
|
}
|
|
218
|
+
if (isCurve(m.displace.amount))
|
|
219
|
+
out.push({ key: 'displace', value: m.displace.amount });
|
|
200
220
|
m.masks.forEach((k, i) => {
|
|
201
221
|
if (isCurve(k.strength))
|
|
202
222
|
out.push({ key: `strength${i}`, value: k.strength });
|
|
@@ -362,6 +382,8 @@ function sourceCode(s, i, lod) {
|
|
|
362
382
|
case 'image': return s.channel === 'r' ? `${tex('uv')}.r` : s.channel === 'a' ? `${tex('uv')}.a` : `(dot(${tex('uv')}.rgb, vec3(0.2126, 0.7152, 0.0722)) * ${tex('uv')}.a)`;
|
|
363
383
|
case 'gradient': {
|
|
364
384
|
const c = `vec2(${glslFloat(s.center[0])}, ${glslFloat(s.center[1])})`;
|
|
385
|
+
if (s.fill === 'band')
|
|
386
|
+
return `clamp(1.0 - abs(fract(uv.x) - ${glslFloat(s.center[0])}) / ${glslFloat(s.width)}, 0.0, 1.0)`;
|
|
365
387
|
return s.fill === 'linear' ? 'fract(uv.x)' : s.fill === 'radial' ? `(1.0 - clamp(length(fract(uv) - ${c}) * 2.0, 0.0, 1.0))` : `(1.0 - clamp(max(abs(fract(uv.x) - ${c}.x), abs(fract(uv.y) - ${c}.y)) * 2.0, 0.0, 1.0))`;
|
|
366
388
|
}
|
|
367
389
|
case 'shape': return `vfxShape${s.shape[0].toUpperCase()}${s.shape.slice(1)}(fract(uv) * 2.0 - 1.0, uShape${i})`;
|
|
@@ -390,11 +412,15 @@ export function compileMaterial(m) {
|
|
|
390
412
|
uDissolve: { value: isCurve(m.dissolve) ? 0 : m.dissolve }, uThreshold: { value: isCurve(m.threshold) ? 0 : m.threshold },
|
|
391
413
|
uSoftness: { value: m.softness }, uPower: { value: m.power }, uOpacity: { value: isCurve(m.opacity) ? 1 : m.opacity }, uEmission: { value: isCurve(m.emission) ? 1 : m.emission },
|
|
392
414
|
uEdge: { value: new T.Vector3(m.edge.width, m.edge.emission, 0) }, uEdgeColor: { value: new T.Color(m.edge.color) },
|
|
415
|
+
uRim: { value: new T.Vector3(m.rim.strength, m.rim.power, m.rim.mode === 'edge' ? 1 : 0) },
|
|
416
|
+
uDisplace: { value: new T.Vector2(isCurve(m.displace.amount) ? 1 : m.displace.amount, m.displace.offset) },
|
|
417
|
+
uCutoff: { value: m.cutoff },
|
|
393
418
|
};
|
|
394
419
|
const owned = [uniforms.uRamp.value, uniforms.uCurves.value];
|
|
395
420
|
const noise = [];
|
|
396
421
|
const images = new Map();
|
|
397
422
|
const decl = [];
|
|
423
|
+
const fns = [];
|
|
398
424
|
const body = [];
|
|
399
425
|
m.masks.forEach((k, i) => {
|
|
400
426
|
const s = k.source;
|
|
@@ -423,21 +449,26 @@ export function compileMaterial(m) {
|
|
|
423
449
|
}
|
|
424
450
|
const polar = k.polar ? `{ vec2 c = uv0 - 0.5; uv = vec2(atan(c.x, c.y) / 6.2831853 + 0.5, length(c) * 2.0); }` : '';
|
|
425
451
|
const random = k.randomOffset ? ` + life.y * vec2(17.31, 7.97)` : '';
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
452
|
+
// One function per mask, so a vertex shader can call mask 0 for displacement.
|
|
453
|
+
fns.push(`float vfxMask${i}(vec2 uv0, vec2 life){ vec2 uv = uv0; ${polar}
|
|
454
|
+
uv = uv * uScale${i} - uScroll${i} * uTime${random};
|
|
455
|
+
float sa = sin(uSpin${i} * uTime + uTurn${i}), ca = cos(uSpin${i} * uTime + uTurn${i}); uv = mat2(ca, sa, -sa, ca) * (uv - 0.5) + 0.5;
|
|
456
|
+
float b = ${sourceCode(s, i, k.polar)};${k.invert ? ' b = 1.0 - b;' : ''}${k.levels ? ` b = smoothstep(uLevels${i}.x, uLevels${i}.y, b);` : ''}${isCurve(k.offset) || k.offset !== 0 ? ` b = clamp(b + ${value(`offset${i}`, `uOffset${i}`)}, 0.0, 1.0);` : ''}
|
|
457
|
+
return b; }`);
|
|
458
|
+
body.push(i === 0 ? ' m = vfxMask0(uv0, life);' : ` { float a = m, b = vfxMask${i}(uv0, life); m = mix(a, ${BLEND_CODE[k.blend]}, clamp(${value(`strength${i}`, `uStrength${i}`)}, 0.0, 1.0)); }`);
|
|
431
459
|
});
|
|
432
460
|
const code = `
|
|
433
461
|
uniform float uTime; uniform sampler2D uRamp; uniform sampler2D uCurves;
|
|
434
462
|
uniform float uDissolve; uniform float uThreshold; uniform float uSoftness; uniform float uPower; uniform float uOpacity; uniform float uEmission;
|
|
435
|
-
uniform vec3 uEdge; uniform vec3 uEdgeColor;
|
|
463
|
+
uniform vec3 uEdge; uniform vec3 uEdgeColor; uniform vec3 uRim; uniform vec2 uDisplace; uniform float uCutoff;
|
|
436
464
|
${decl.join('\n')}
|
|
437
465
|
${SHAPES}
|
|
438
466
|
float vfxCurve(float row, float t){ return texture2D(uCurves, vec2((clamp(t, 0.0, 1.0) * 255.0 + 0.5) / 256.0, (row + 0.5) / ${glslFloat(rows)})).r; }
|
|
439
|
-
|
|
440
|
-
|
|
467
|
+
${fns.join('\n')}
|
|
468
|
+
${m.masks.length ? `float vfxDisplace(vec2 uv, vec2 life){ return (vfxMask0(uv, life) - uDisplace.y) * ${value('displace', 'uDisplace.x')}; }` : 'float vfxDisplace(vec2 uv, vec2 life){ return 0.0; }'}
|
|
469
|
+
/** facing: |dot(normal, view)|, 1 for a camera-facing quad. */
|
|
470
|
+
vec4 vfxMaterial(vec2 uv0, vec2 life, float facing){
|
|
471
|
+
float m = ${m.masks.length ? '0.0' : '1.0'};
|
|
441
472
|
${body.join('\n')}
|
|
442
473
|
m = clamp(m, 0.0, 1.0);
|
|
443
474
|
float v = m - ${value('dissolve', 'uDissolve')};
|
|
@@ -446,10 +477,11 @@ ${body.join('\n')}
|
|
|
446
477
|
vec4 ramp = texture2D(uRamp, vec2(clamp(v, 0.0, 1.0) * (255.0 / 256.0) + 0.5 / 256.0, 0.5));
|
|
447
478
|
vec3 rgb = ramp.rgb;
|
|
448
479
|
if (uEdge.x > 0.0) { float e = 1.0 - smoothstep(th, th + uEdge.x, v); rgb = mix(rgb, uEdgeColor * uEdge.y, e * step(th, v)); }
|
|
480
|
+
if (uRim.x > 0.0) { float f = pow(1.0 - clamp(facing, 0.0, 1.0), uRim.y); alpha *= mix(1.0, uRim.z > 0.5 ? f : 1.0 - f, uRim.x); }
|
|
449
481
|
return vec4(rgb * max(0.0, ${value('emission', 'uEmission')}), ramp.a * alpha * clamp(${value('opacity', 'uOpacity')}, 0.0, 1.0));
|
|
450
482
|
}`;
|
|
451
483
|
return {
|
|
452
|
-
code, uniforms, images, alphaMode: m.alphaMode,
|
|
484
|
+
code, uniforms, images, alphaMode: m.alphaMode, displace: m.masks.length > 0 && (isCurve(m.displace.amount) || m.displace.amount !== 0),
|
|
453
485
|
dispose() { for (const t of owned)
|
|
454
486
|
t.dispose(); for (const t of noise)
|
|
455
487
|
releaseNoiseTexture(t); },
|
|
@@ -470,14 +502,14 @@ export const MATERIAL_PRESETS = {
|
|
|
470
502
|
masks: [
|
|
471
503
|
mask(noise({ kind: 'perlin', fractal: 'fbm', octaves: 3, frequency: 6, warp: .25 }), { scale: [.6, .6], scroll: [0, .2], randomOffset: true }),
|
|
472
504
|
mask(noise({ kind: 'perlin', fractal: 'pingpong', octaves: 1, frequency: 10, pingpong: 1, seed: 3 }), { invert: true, scale: [.4, .5], scroll: [0, -.1], randomOffset: true, blend: 'dodge', strength: .76 }),
|
|
473
|
-
mask({ type: 'gradient', fill: 'radial', center: [.5, .5] }),
|
|
505
|
+
mask({ type: 'gradient', fill: 'radial', center: [.5, .5], width: .25 }),
|
|
474
506
|
],
|
|
475
507
|
threshold: 0, softness: 1, ramp: 'linear', power: 3, emission: 1.5, color: { stops: [[0, '#4b4b4b', 1]] },
|
|
476
508
|
}),
|
|
477
509
|
fireCore: () => ({
|
|
478
510
|
...materialDefaults(),
|
|
479
511
|
masks: [
|
|
480
|
-
mask({ type: 'gradient', fill: 'radial', center: [.5, .5] }, { levels: [-.2, 1] }),
|
|
512
|
+
mask({ type: 'gradient', fill: 'radial', center: [.5, .5], width: .25 }, { levels: [-.2, 1] }),
|
|
481
513
|
mask(noise({ kind: 'perlin', fractal: 'ridged', octaves: 3, frequency: 6, warp: .35 }), { scale: [.15, .6], scroll: [0, .4], randomOffset: true }),
|
|
482
514
|
],
|
|
483
515
|
threshold: .3, softness: .2, emission: curve([0, 4], [.3, 1.4], [1, 1]),
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mesh layers and mesh-shaped particles (format v3, umicat-design ADR-055
|
|
3
|
+
* step 2). The shapes are Godot's primitive meshes with Godot's parameters,
|
|
4
|
+
* so a rebuild copies the numbers: QuadMesh faces +Z, PlaneMesh faces +Y,
|
|
5
|
+
* Cylinder / Sphere / Torus stand on the Y axis.
|
|
6
|
+
*/
|
|
7
|
+
import * as T from 'three';
|
|
8
|
+
import type { Curve, Gradient, Vec3 } from './document.js';
|
|
9
|
+
import type { CompiledMaterial, Material } from './material.js';
|
|
10
|
+
export type MeshShape =
|
|
11
|
+
/** Upright card in the XY plane, facing +Z. */
|
|
12
|
+
{
|
|
13
|
+
type: 'quad';
|
|
14
|
+
size: [number, number];
|
|
15
|
+
}
|
|
16
|
+
/** Flat on the ground (XZ), facing up. */
|
|
17
|
+
| {
|
|
18
|
+
type: 'plane';
|
|
19
|
+
size: [number, number];
|
|
20
|
+
}
|
|
21
|
+
/** `height` other than 2 x radius squashes it (Godot SphereMesh). */
|
|
22
|
+
| {
|
|
23
|
+
type: 'sphere';
|
|
24
|
+
radius: number;
|
|
25
|
+
height: number;
|
|
26
|
+
hemisphere: boolean;
|
|
27
|
+
}
|
|
28
|
+
/** top 0 is a cone. `caps` false leaves it an open tube (most effect rings and columns). */
|
|
29
|
+
| {
|
|
30
|
+
type: 'cylinder';
|
|
31
|
+
top: number;
|
|
32
|
+
bottom: number;
|
|
33
|
+
height: number;
|
|
34
|
+
caps: boolean;
|
|
35
|
+
}
|
|
36
|
+
/** Lies flat, around Y (Godot TorusMesh). */
|
|
37
|
+
| {
|
|
38
|
+
type: 'torus';
|
|
39
|
+
inner: number;
|
|
40
|
+
outer: number;
|
|
41
|
+
} | {
|
|
42
|
+
type: 'box';
|
|
43
|
+
size: Vec3;
|
|
44
|
+
}
|
|
45
|
+
/** A flat annulus on the ground: a shockwave, a target circle. */
|
|
46
|
+
| {
|
|
47
|
+
type: 'ring';
|
|
48
|
+
inner: number;
|
|
49
|
+
outer: number;
|
|
50
|
+
};
|
|
51
|
+
export interface MeshLayer {
|
|
52
|
+
id: string;
|
|
53
|
+
enabled: boolean;
|
|
54
|
+
shape: MeshShape;
|
|
55
|
+
position: Vec3;
|
|
56
|
+
rotation: Vec3;
|
|
57
|
+
scale: Vec3;
|
|
58
|
+
/** none: as placed. camera: a quad or plane turned to face the viewer, keeping its position and size (other shapes ignore it). */
|
|
59
|
+
facing: 'none' | 'camera';
|
|
60
|
+
/** Seconds into each cycle it appears, and for how long (null: to the end of the cycle). */
|
|
61
|
+
start: number;
|
|
62
|
+
life: number | null;
|
|
63
|
+
/** Over the layer's own life (0 = start, 1 = end). Multiplies `scale`. */
|
|
64
|
+
scaleOverLife?: Curve;
|
|
65
|
+
color: string;
|
|
66
|
+
colorOverLife?: Gradient;
|
|
67
|
+
alphaOverLife?: Curve;
|
|
68
|
+
intensity: number;
|
|
69
|
+
blend: 'normal' | 'additive';
|
|
70
|
+
side: 'front' | 'back' | 'both';
|
|
71
|
+
material: Material;
|
|
72
|
+
}
|
|
73
|
+
export declare const MESH_SHAPES: Record<MeshShape['type'], () => MeshShape>;
|
|
74
|
+
type Fail = (why: string) => never;
|
|
75
|
+
/** Fills a shape's missing parameters from its defaults and checks them. */
|
|
76
|
+
export declare function parseMeshShape(raw: unknown, at: string, fail: Fail): MeshShape;
|
|
77
|
+
/**
|
|
78
|
+
* The geometry, built once per layer. Enough segments for vertex
|
|
79
|
+
* displacement to read as a surface (Binbun subdivides its cards 16x16 for the
|
|
80
|
+
* same reason).
|
|
81
|
+
*/
|
|
82
|
+
export declare function meshGeometry(s: MeshShape): T.BufferGeometry;
|
|
83
|
+
/** Ring UVs from three.js are planar; an effect ring wants u around and v across, like a cylinder's. */
|
|
84
|
+
export declare function fixRingUv(g: T.BufferGeometry, s: MeshShape): T.BufferGeometry<T.NormalBufferAttributes, T.BufferGeometryEventMap>;
|
|
85
|
+
/** A mesh layer: one mesh, its colour and life from uniforms. */
|
|
86
|
+
export declare const meshLayerVertex: (c: CompiledMaterial) => string;
|
|
87
|
+
/**
|
|
88
|
+
* A mesh particle: the mesh, scaled by the particle's size, stood on a basis
|
|
89
|
+
* whose Y is the particle's velocity (align velocity), the emitter's axis
|
|
90
|
+
* (axis) or the emitter's own up (otherwise), and turned by its angle about
|
|
91
|
+
* that Y -- Godot's transform_align / rotate_y.
|
|
92
|
+
*/
|
|
93
|
+
export declare const meshParticleVertex: (c: CompiledMaterial) => string;
|
|
94
|
+
/** Shared by mesh layers and mesh particles: the material, times the colour, with the alpha mode. */
|
|
95
|
+
export declare const meshFragment: (c: CompiledMaterial) => string;
|
|
96
|
+
export {};
|
package/dist/vfx/mesh.js
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mesh layers and mesh-shaped particles (format v3, umicat-design ADR-055
|
|
3
|
+
* step 2). The shapes are Godot's primitive meshes with Godot's parameters,
|
|
4
|
+
* so a rebuild copies the numbers: QuadMesh faces +Z, PlaneMesh faces +Y,
|
|
5
|
+
* Cylinder / Sphere / Torus stand on the Y axis.
|
|
6
|
+
*/
|
|
7
|
+
import * as T from 'three';
|
|
8
|
+
export const MESH_SHAPES = {
|
|
9
|
+
quad: () => ({ type: 'quad', size: [1, 1] }),
|
|
10
|
+
plane: () => ({ type: 'plane', size: [2, 2] }),
|
|
11
|
+
sphere: () => ({ type: 'sphere', radius: .5, height: 1, hemisphere: false }),
|
|
12
|
+
cylinder: () => ({ type: 'cylinder', top: .5, bottom: .5, height: 1, caps: false }),
|
|
13
|
+
torus: () => ({ type: 'torus', inner: .4, outer: .5 }),
|
|
14
|
+
box: () => ({ type: 'box', size: [1, 1, 1] }),
|
|
15
|
+
ring: () => ({ type: 'ring', inner: .8, outer: 1 }),
|
|
16
|
+
};
|
|
17
|
+
const num = (v, min, max) => typeof v === 'number' && Number.isFinite(v) && v >= min && v <= max;
|
|
18
|
+
/** Fills a shape's missing parameters from its defaults and checks them. */
|
|
19
|
+
export function parseMeshShape(raw, at, fail) {
|
|
20
|
+
const type = raw?.type;
|
|
21
|
+
if (!raw || typeof raw !== 'object' || !(type in MESH_SHAPES))
|
|
22
|
+
return fail(`${at}.type must be one of ${Object.keys(MESH_SHAPES).join(', ')}`);
|
|
23
|
+
const base = MESH_SHAPES[type]();
|
|
24
|
+
for (const k of Object.keys(raw))
|
|
25
|
+
if (!(k in base))
|
|
26
|
+
fail(`${at}.${k} is not a ${type} field (expected ${Object.keys(base).join(', ')})`);
|
|
27
|
+
const s = { ...base, ...raw };
|
|
28
|
+
const pos = (k, v, max = 100) => { if (!num(v, 0, max))
|
|
29
|
+
fail(`${at}.${k} must be 0-${max}`); };
|
|
30
|
+
const vec = (k, v, n) => { if (!Array.isArray(v) || v.length !== n || !v.every((x) => num(x, 0, 100)))
|
|
31
|
+
fail(`${at}.${k} must be [${n === 2 ? 'w, h' : 'x, y, z'}], each 0-100`); };
|
|
32
|
+
switch (s.type) {
|
|
33
|
+
case 'quad':
|
|
34
|
+
case 'plane':
|
|
35
|
+
vec('size', s.size, 2);
|
|
36
|
+
break;
|
|
37
|
+
case 'box':
|
|
38
|
+
vec('size', s.size, 3);
|
|
39
|
+
break;
|
|
40
|
+
case 'sphere':
|
|
41
|
+
pos('radius', s.radius);
|
|
42
|
+
pos('height', s.height, 200);
|
|
43
|
+
if (typeof s.hemisphere !== 'boolean')
|
|
44
|
+
fail(`${at}.hemisphere must be a boolean`);
|
|
45
|
+
break;
|
|
46
|
+
case 'cylinder':
|
|
47
|
+
pos('top', s.top);
|
|
48
|
+
pos('bottom', s.bottom);
|
|
49
|
+
pos('height', s.height);
|
|
50
|
+
if (typeof s.caps !== 'boolean')
|
|
51
|
+
fail(`${at}.caps must be a boolean`);
|
|
52
|
+
if (s.top === 0 && s.bottom === 0)
|
|
53
|
+
fail(`${at} needs top or bottom above 0`);
|
|
54
|
+
break;
|
|
55
|
+
case 'torus':
|
|
56
|
+
case 'ring':
|
|
57
|
+
pos('outer', s.outer);
|
|
58
|
+
if (!num(s.inner, 0, s.outer))
|
|
59
|
+
fail(`${at}.inner must be 0-outer (${s.outer})`);
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
return s;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The geometry, built once per layer. Enough segments for vertex
|
|
66
|
+
* displacement to read as a surface (Binbun subdivides its cards 16x16 for the
|
|
67
|
+
* same reason).
|
|
68
|
+
*/
|
|
69
|
+
export function meshGeometry(s) {
|
|
70
|
+
switch (s.type) {
|
|
71
|
+
case 'quad': return new T.PlaneGeometry(s.size[0], s.size[1], 16, 16);
|
|
72
|
+
case 'plane': return new T.PlaneGeometry(s.size[0], s.size[1], 16, 16).rotateX(-Math.PI / 2);
|
|
73
|
+
case 'sphere': {
|
|
74
|
+
const g = new T.SphereGeometry(Math.max(s.radius, 1e-4), 32, s.hemisphere ? 8 : 16, 0, Math.PI * 2, 0, s.hemisphere ? Math.PI / 2 : Math.PI);
|
|
75
|
+
return s.radius > 0 && Math.abs(s.height - 2 * s.radius) > 1e-6 ? g.scale(1, s.height / (2 * s.radius), 1) : g;
|
|
76
|
+
}
|
|
77
|
+
case 'cylinder': return new T.CylinderGeometry(s.top, s.bottom, s.height, 32, 8, !s.caps);
|
|
78
|
+
case 'torus': return new T.TorusGeometry((s.inner + s.outer) / 2, Math.max(1e-4, (s.outer - s.inner) / 2), 16, 48).rotateX(-Math.PI / 2);
|
|
79
|
+
case 'box': return new T.BoxGeometry(s.size[0], s.size[1], s.size[2]);
|
|
80
|
+
case 'ring': return new T.RingGeometry(s.inner, s.outer, 64, 1).rotateX(-Math.PI / 2);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/** Ring UVs from three.js are planar; an effect ring wants u around and v across, like a cylinder's. */
|
|
84
|
+
export function fixRingUv(g, s) {
|
|
85
|
+
if (s.type !== 'ring')
|
|
86
|
+
return g;
|
|
87
|
+
const p = g.getAttribute('position'), uv = g.getAttribute('uv');
|
|
88
|
+
for (let i = 0; i < p.count; i++) {
|
|
89
|
+
const x = p.getX(i), z = p.getZ(i), r = Math.hypot(x, z);
|
|
90
|
+
uv.setXY(i, Math.atan2(z, x) / (Math.PI * 2) + .5, s.outer > s.inner ? (r - s.inner) / (s.outer - s.inner) : 0);
|
|
91
|
+
}
|
|
92
|
+
uv.needsUpdate = true;
|
|
93
|
+
return g;
|
|
94
|
+
}
|
|
95
|
+
const DISPLACE = (c, life) => c.displace ? `p += n * vfxDisplace(uv, ${life});` : '';
|
|
96
|
+
/** A mesh layer: one mesh, its colour and life from uniforms. */
|
|
97
|
+
export const meshLayerVertex = (c) => `
|
|
98
|
+
uniform vec4 uLayerColor; uniform vec2 uLayerLife; uniform float uFacing;
|
|
99
|
+
varying vec2 vUv; varying vec4 vColor; varying vec2 vLife; varying vec3 vN; varying vec3 vV;
|
|
100
|
+
${c.code}
|
|
101
|
+
void main(){
|
|
102
|
+
vec3 p = position, n = normal;
|
|
103
|
+
vUv = uv; vColor = uLayerColor; vLife = uLayerLife;
|
|
104
|
+
${DISPLACE(c, 'uLayerLife')}
|
|
105
|
+
if (uFacing > 0.5) {
|
|
106
|
+
// Turned to face the viewer, keeping its position and its own size.
|
|
107
|
+
vec4 c0 = modelViewMatrix * vec4(0.0, 0.0, 0.0, 1.0);
|
|
108
|
+
// uFacing 1: a quad (its XY), 2: a plane (its XZ, z toward the viewer = down on screen).
|
|
109
|
+
c0.xy += uFacing < 1.5 ? vec2(p.x * length(modelMatrix[0].xyz), p.y * length(modelMatrix[1].xyz))
|
|
110
|
+
: vec2(p.x * length(modelMatrix[0].xyz), -p.z * length(modelMatrix[2].xyz));
|
|
111
|
+
vV = -c0.xyz; vN = vec3(0.0, 0.0, 1.0);
|
|
112
|
+
gl_Position = projectionMatrix * c0;
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
vec4 mv = modelViewMatrix * vec4(p, 1.0);
|
|
116
|
+
vV = -mv.xyz; vN = normalize(normalMatrix * n);
|
|
117
|
+
gl_Position = projectionMatrix * mv;
|
|
118
|
+
}`;
|
|
119
|
+
/**
|
|
120
|
+
* A mesh particle: the mesh, scaled by the particle's size, stood on a basis
|
|
121
|
+
* whose Y is the particle's velocity (align velocity), the emitter's axis
|
|
122
|
+
* (axis) or the emitter's own up (otherwise), and turned by its angle about
|
|
123
|
+
* that Y -- Godot's transform_align / rotate_y.
|
|
124
|
+
*/
|
|
125
|
+
export const meshParticleVertex = (c) => `
|
|
126
|
+
attribute vec3 iPos; attribute vec4 iColor; attribute vec4 iMisc; attribute vec3 iVel; attribute vec2 iLife; attribute vec3 iAxes;
|
|
127
|
+
uniform float uAlign; uniform mat3 uOrient; uniform vec3 uAxis;
|
|
128
|
+
varying vec2 vUv; varying vec4 vColor; varying vec2 vLife; varying vec3 vN; varying vec3 vV;
|
|
129
|
+
${c.code}
|
|
130
|
+
void main(){
|
|
131
|
+
vec3 p = position, n = normal;
|
|
132
|
+
vUv = uv; vColor = iColor; vLife = iLife;
|
|
133
|
+
${DISPLACE(c, 'iLife')}
|
|
134
|
+
vec3 up = uOrient * vec3(0.0, 1.0, 0.0);
|
|
135
|
+
if (uAlign > 0.5 && uAlign < 1.5 && dot(iVel, iVel) > 1e-10) up = normalize(iVel);
|
|
136
|
+
else if (uAlign > 2.5) up = normalize(uAxis);
|
|
137
|
+
vec3 ref = abs(up.y) < 0.99 ? vec3(0.0, 1.0, 0.0) : vec3(1.0, 0.0, 0.0);
|
|
138
|
+
vec3 side = normalize(cross(ref, up)), fwd = cross(side, up);
|
|
139
|
+
if (uAlign < 0.5 || (uAlign > 1.5 && uAlign < 2.5)) { side = uOrient * vec3(1.0, 0.0, 0.0); fwd = uOrient * vec3(0.0, 0.0, 1.0); }
|
|
140
|
+
float s = sin(iMisc.y), co = cos(iMisc.y);
|
|
141
|
+
vec3 sx = side * co - fwd * s, sz = side * s + fwd * co;
|
|
142
|
+
mat3 basis = mat3(sx, up, sz);
|
|
143
|
+
vec4 mv = modelViewMatrix * vec4(iPos + basis * (p * iMisc.x * iAxes), 1.0);
|
|
144
|
+
vV = -mv.xyz; vN = normalize((modelViewMatrix * vec4(basis * (n / max(iAxes, vec3(1e-4))), 0.0)).xyz);
|
|
145
|
+
gl_Position = projectionMatrix * mv;
|
|
146
|
+
}`;
|
|
147
|
+
/** Shared by mesh layers and mesh particles: the material, times the colour, with the alpha mode. */
|
|
148
|
+
export const meshFragment = (c) => `
|
|
149
|
+
varying vec2 vUv; varying vec4 vColor; varying vec2 vLife; varying vec3 vN; varying vec3 vV;
|
|
150
|
+
${c.code}
|
|
151
|
+
float vfxBayer(vec2 p){ ivec2 q = ivec2(mod(p, 4.0)); int i = q.x + q.y * 4;
|
|
152
|
+
float m[16] = float[16](0.,8.,2.,10.,12.,4.,14.,6.,3.,11.,1.,9.,15.,7.,13.,5.); return (m[i] + 0.5) / 16.0; }
|
|
153
|
+
void main(){
|
|
154
|
+
float facing = abs(dot(normalize(vN), normalize(vV)));
|
|
155
|
+
vec4 m = vfxMaterial(vUv, vLife, facing);
|
|
156
|
+
float a = m.a * vColor.a;
|
|
157
|
+
${c.alphaMode === 'cutout' ? 'if (a < uCutoff) discard; a = 1.0;' : c.alphaMode === 'dither' ? 'if (a < vfxBayer(gl_FragCoord.xy)) discard; a = 1.0;' : 'if (a <= 0.002) discard;'}
|
|
158
|
+
gl_FragColor = vec4(m.rgb * vColor.rgb, a);
|
|
159
|
+
#include <tonemapping_fragment>
|
|
160
|
+
#include <colorspace_fragment>
|
|
161
|
+
}`;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umicat/three-sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.38.0",
|
|
4
4
|
"description": "Three.js runtime for Umicat games: the scene3d design format, its loader with physics, a kinematic character controller, and the Umicat platform via @umicat/platform-sdk.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|