@umicat/three-sdk 1.35.0 → 1.37.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 +32 -9
- package/dist/vfx/VfxInstance.d.ts +28 -8
- package/dist/vfx/VfxInstance.js +505 -60
- package/dist/vfx/document.d.ts +263 -19
- package/dist/vfx/document.js +375 -24
- package/dist/vfx/index.d.ts +1 -0
- package/dist/vfx/index.js +1 -0
- package/dist/vfx/material.d.ts +191 -0
- package/dist/vfx/material.js +500 -0
- package/dist/vfx/templates.js +9 -7
- package/package.json +1 -1
|
@@ -0,0 +1,500 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The effect MATERIAL (format v3, umicat-design ADR-055): how a particle (and,
|
|
3
|
+
* later, a mesh layer) is drawn. Plain data, compiled into one shader.
|
|
4
|
+
*
|
|
5
|
+
* Modelled on the general material the Binbun packs ship
|
|
6
|
+
* (`shared/shader/transparent.gdshader`, their most-used shader):
|
|
7
|
+
*
|
|
8
|
+
* 1. up to three MASKS -- procedural noise, a gradient, a shape or an image,
|
|
9
|
+
* each scaled / scrolled / spun / optionally in polar UVs -- combined
|
|
10
|
+
* into one value m (0..1);
|
|
11
|
+
* 2. DISSOLVE d is taken off m: v = m - d;
|
|
12
|
+
* 3. alpha = smoothstep(threshold, threshold + softness, v);
|
|
13
|
+
* 4. the colour is the GRADIENT MAP `color` at v (a colour ramp indexed by
|
|
14
|
+
* the mask -- the white-hot core of a flame), optionally with a glowing
|
|
15
|
+
* EDGE where v is just above the threshold;
|
|
16
|
+
* 5. x `emission` (HDR) x the particle's own colour and alpha.
|
|
17
|
+
*
|
|
18
|
+
* Numbers marked `Animated` are a constant or `{ overLife: Curve }` --
|
|
19
|
+
* sampled at the particle's age. That is what Binbun's shaders do by reading
|
|
20
|
+
* the particle COLOR as data; here the file says what the number is for.
|
|
21
|
+
*/
|
|
22
|
+
import * as T from 'three';
|
|
23
|
+
import { sampleCurve, sampleGradient } from './document.js';
|
|
24
|
+
/** Binbun's twelve (`util/blend.gdshaderinc`), in its order. dodge is a / (1 - b); linearBurn a + b - 1. */
|
|
25
|
+
export const MASK_BLENDS = ['add', 'subtract', 'multiply', 'min', 'max', 'screen', 'overlay', 'hardLight', 'softLight', 'dodge', 'burn', 'linearBurn'];
|
|
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, 16] };
|
|
28
|
+
export function noiseDefaults() {
|
|
29
|
+
return { type: 'noise', kind: 'perlin', fractal: 'fbm', octaves: 4, frequency: 4, seed: 1, warp: 0, pingpong: 2, size: 256 };
|
|
30
|
+
}
|
|
31
|
+
export function maskDefaults(source = noiseDefaults()) {
|
|
32
|
+
return { source, scale: [1, 1], scroll: [0, 0], spin: 0, rotation: 0, polar: false, randomOffset: false, invert: false, levels: null, offset: 0, blend: 'multiply', strength: 1 };
|
|
33
|
+
}
|
|
34
|
+
export function shapeSource(shape = 'dot') {
|
|
35
|
+
return { type: 'shape', shape, softness: .5, points: 4, sharpness: .5, radius: .7, width: .2 };
|
|
36
|
+
}
|
|
37
|
+
/** A soft round particle with a noise texture: a blank material that already reads as something. */
|
|
38
|
+
export function materialDefaults() {
|
|
39
|
+
return {
|
|
40
|
+
masks: [maskDefaults(shapeSource('dot')), { ...maskDefaults(noiseDefaults()), blend: 'multiply', randomOffset: true }],
|
|
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',
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
const isCurve = (a) => typeof a === 'object' && a !== null;
|
|
46
|
+
/** The value of an Animated number at life t (0..1). */
|
|
47
|
+
export function sampleAnimated(a, t) { return isCurve(a) ? sampleCurve(a.overLife, t) : a; }
|
|
48
|
+
/** Every image filename a material reads. */
|
|
49
|
+
export function materialTextures(m) {
|
|
50
|
+
return m.masks.flatMap((k) => k.source.type === 'image' ? [k.source.texture] : []);
|
|
51
|
+
}
|
|
52
|
+
const num = (v, min, max) => typeof v === 'number' && Number.isFinite(v) && v >= min && v <= max;
|
|
53
|
+
const known = (o, keys, at, fail) => { for (const k of Object.keys(o))
|
|
54
|
+
if (!keys.includes(k))
|
|
55
|
+
fail(`${at}.${k} is not a field here (expected ${keys.join(', ')})`); };
|
|
56
|
+
/**
|
|
57
|
+
* Fills omitted fields from the defaults, refuses unknown ones, checks
|
|
58
|
+
* ranges. `checkCurve` / `checkGradient` / `texturePattern` come from the
|
|
59
|
+
* document parser so the messages read the same.
|
|
60
|
+
*/
|
|
61
|
+
export function parseMaterial(input, at, fail, h) {
|
|
62
|
+
if (!input || typeof input !== 'object' || Array.isArray(input))
|
|
63
|
+
return fail(`${at} must be an object`);
|
|
64
|
+
const base = materialDefaults();
|
|
65
|
+
known(input, Object.keys(base), at, fail);
|
|
66
|
+
const m = { ...base, ...input };
|
|
67
|
+
const animated = (v, a, min, max) => {
|
|
68
|
+
if (num(v, min, max))
|
|
69
|
+
return;
|
|
70
|
+
if (v && typeof v === 'object' && !Array.isArray(v)) {
|
|
71
|
+
known(v, ['overLife'], a, fail);
|
|
72
|
+
h.checkCurve(v.overLife, `${a}.overLife`);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
fail(`${a} must be a number ${min}-${max}, or { overLife: { points: [[t, value], ...] } }`);
|
|
76
|
+
};
|
|
77
|
+
for (const [k, [min, max]] of Object.entries(ANIMATED_FIELDS))
|
|
78
|
+
animated(m[k], `${at}.${k}`, min, max);
|
|
79
|
+
if (!num(m.softness, 0, 1))
|
|
80
|
+
fail(`${at}.softness must be 0-1`);
|
|
81
|
+
if (m.ramp !== 'smooth' && m.ramp !== 'linear')
|
|
82
|
+
fail(`${at}.ramp must be "smooth" or "linear"`);
|
|
83
|
+
if (!num(m.power, .1, 8))
|
|
84
|
+
fail(`${at}.power must be 0.1-8`);
|
|
85
|
+
if (!m.edge || typeof m.edge !== 'object')
|
|
86
|
+
fail(`${at}.edge must be { width, color, emission }`);
|
|
87
|
+
m.edge = { ...base.edge, ...m.edge };
|
|
88
|
+
known(m.edge, ['width', 'color', 'emission'], `${at}.edge`, fail);
|
|
89
|
+
if (!num(m.edge.width, 0, 1))
|
|
90
|
+
fail(`${at}.edge.width must be 0-1`);
|
|
91
|
+
if (typeof m.edge.color !== 'string' || !/^#[0-9a-f]{6}$/i.test(m.edge.color))
|
|
92
|
+
fail(`${at}.edge.color must be #rrggbb`);
|
|
93
|
+
if (!num(m.edge.emission, 0, 16))
|
|
94
|
+
fail(`${at}.edge.emission must be 0-16`);
|
|
95
|
+
h.checkGradient(m.color, `${at}.color`);
|
|
96
|
+
if (!['blend', 'cutout', 'dither'].includes(m.alphaMode))
|
|
97
|
+
fail(`${at}.alphaMode must be "blend", "cutout" or "dither"`);
|
|
98
|
+
if (!Array.isArray(m.masks) || m.masks.length < 1 || m.masks.length > MATERIAL_LIMITS.masks)
|
|
99
|
+
fail(`${at}.masks must be a list of 1-${MATERIAL_LIMITS.masks}`);
|
|
100
|
+
m.masks = m.masks.map((raw, i) => {
|
|
101
|
+
const mat = `${at}.masks[${i}]`;
|
|
102
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw))
|
|
103
|
+
fail(`${mat} must be an object`);
|
|
104
|
+
const mb = maskDefaults();
|
|
105
|
+
known(raw, Object.keys(mb), mat, fail);
|
|
106
|
+
const k = { ...mb, ...raw };
|
|
107
|
+
const pair = (v, a, min, max) => { if (!Array.isArray(v) || v.length !== 2 || !v.every((x) => num(x, min, max)))
|
|
108
|
+
fail(`${a} must be [u, v], each ${min}-${max}`); };
|
|
109
|
+
pair(k.scale, `${mat}.scale`, -100, 100);
|
|
110
|
+
pair(k.scroll, `${mat}.scroll`, -100, 100);
|
|
111
|
+
if (!num(k.spin, -3600, 3600))
|
|
112
|
+
fail(`${mat}.spin must be -3600-3600`);
|
|
113
|
+
if (!num(k.rotation, -360, 360))
|
|
114
|
+
fail(`${mat}.rotation must be -360-360`);
|
|
115
|
+
animated(k.offset, `${mat}.offset`, -2, 2);
|
|
116
|
+
for (const b of ['polar', 'randomOffset', 'invert'])
|
|
117
|
+
if (typeof k[b] !== 'boolean')
|
|
118
|
+
fail(`${mat}.${b} must be a boolean`);
|
|
119
|
+
if (k.levels !== null) {
|
|
120
|
+
pair(k.levels, `${mat}.levels`, -2, 3);
|
|
121
|
+
if (k.levels[0] === k.levels[1])
|
|
122
|
+
fail(`${mat}.levels must be [lo, hi] with lo != hi, or null`);
|
|
123
|
+
}
|
|
124
|
+
if (!MASK_BLENDS.includes(k.blend))
|
|
125
|
+
fail(`${mat}.blend must be one of ${MASK_BLENDS.join(', ')}`);
|
|
126
|
+
animated(k.strength, `${mat}.strength`, 0, 1);
|
|
127
|
+
k.source = parseSource(k.source, `${mat}.source`, fail, h.texturePattern);
|
|
128
|
+
return k;
|
|
129
|
+
});
|
|
130
|
+
const curves = curveFields(m).length;
|
|
131
|
+
if (curves > MATERIAL_LIMITS.curves)
|
|
132
|
+
fail(`${at} animates ${curves} numbers over life, the limit is ${MATERIAL_LIMITS.curves}`);
|
|
133
|
+
return m;
|
|
134
|
+
}
|
|
135
|
+
function parseSource(s, at, fail, texturePattern) {
|
|
136
|
+
if (!s || typeof s !== 'object' || Array.isArray(s))
|
|
137
|
+
return fail(`${at} must be an object with a type`);
|
|
138
|
+
const type = s.type;
|
|
139
|
+
if (type === 'noise') {
|
|
140
|
+
const n = { ...noiseDefaults(), ...s };
|
|
141
|
+
known(n, Object.keys(noiseDefaults()), at, fail);
|
|
142
|
+
if (!['perlin', 'value', 'cellular', 'cellEdges'].includes(n.kind))
|
|
143
|
+
fail(`${at}.kind must be perlin, value, cellular or cellEdges`);
|
|
144
|
+
if (!['none', 'fbm', 'ridged', 'pingpong'].includes(n.fractal))
|
|
145
|
+
fail(`${at}.fractal must be none, fbm, ridged or pingpong`);
|
|
146
|
+
if (!num(n.octaves, 1, 6) || !Number.isInteger(n.octaves))
|
|
147
|
+
fail(`${at}.octaves must be a whole number 1-6`);
|
|
148
|
+
if (!num(n.frequency, 1, 64) || !Number.isInteger(n.frequency))
|
|
149
|
+
fail(`${at}.frequency must be a whole number 1-64 (cells across the tile; whole so it tiles)`);
|
|
150
|
+
if (!num(n.seed, 0, 1e6) || !Number.isInteger(n.seed))
|
|
151
|
+
fail(`${at}.seed must be a whole number 0-1000000`);
|
|
152
|
+
if (!num(n.pingpong, .5, 8))
|
|
153
|
+
fail(`${at}.pingpong must be 0.5-8`);
|
|
154
|
+
if (!num(n.warp, 0, 4))
|
|
155
|
+
fail(`${at}.warp must be 0-4`);
|
|
156
|
+
if (![64, 128, 256, 512].includes(n.size))
|
|
157
|
+
fail(`${at}.size must be 64, 128, 256 or 512`);
|
|
158
|
+
return n;
|
|
159
|
+
}
|
|
160
|
+
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 square`);
|
|
165
|
+
if (!Array.isArray(g.center) || g.center.length !== 2 || !g.center.every((x) => num(x, -1, 2)))
|
|
166
|
+
fail(`${at}.center must be [u, v], each -1-2`);
|
|
167
|
+
return g;
|
|
168
|
+
}
|
|
169
|
+
if (type === 'shape') {
|
|
170
|
+
const g = { ...shapeSource(), ...s };
|
|
171
|
+
known(g, Object.keys(shapeSource()), at, fail);
|
|
172
|
+
if (!['dot', 'glow', 'star', 'ring'].includes(g.shape))
|
|
173
|
+
fail(`${at}.shape must be dot, glow, star or ring`);
|
|
174
|
+
for (const k of ['softness', 'sharpness', 'radius', 'width'])
|
|
175
|
+
if (!num(g[k], 0, 1))
|
|
176
|
+
fail(`${at}.${k} must be 0-1`);
|
|
177
|
+
if (!num(g.points, 3, 12) || !Number.isInteger(g.points))
|
|
178
|
+
fail(`${at}.points must be a whole number 3-12`);
|
|
179
|
+
return g;
|
|
180
|
+
}
|
|
181
|
+
if (type === 'image') {
|
|
182
|
+
const g = { channel: 'luma', ...s };
|
|
183
|
+
known(g, ['type', 'texture', 'channel'], at, fail);
|
|
184
|
+
if (typeof g.texture !== 'string' || !texturePattern.test(g.texture))
|
|
185
|
+
fail(`${at}.texture must be the filename of a png, webp or jpg image in the same folder (no path)`);
|
|
186
|
+
if (!['luma', 'r', 'a'].includes(g.channel))
|
|
187
|
+
fail(`${at}.channel must be luma, r or a`);
|
|
188
|
+
return g;
|
|
189
|
+
}
|
|
190
|
+
return fail(`${at}.type must be noise, gradient, shape or image`);
|
|
191
|
+
}
|
|
192
|
+
/** The animated numbers that are curves, in a fixed order: each gets one row of the curve texture. */
|
|
193
|
+
function curveFields(m) {
|
|
194
|
+
const out = [];
|
|
195
|
+
for (const k of Object.keys(ANIMATED_FIELDS)) {
|
|
196
|
+
const v = m[k];
|
|
197
|
+
if (isCurve(v))
|
|
198
|
+
out.push({ key: k, value: v });
|
|
199
|
+
}
|
|
200
|
+
m.masks.forEach((k, i) => {
|
|
201
|
+
if (isCurve(k.strength))
|
|
202
|
+
out.push({ key: `strength${i}`, value: k.strength });
|
|
203
|
+
if (isCurve(k.offset))
|
|
204
|
+
out.push({ key: `offset${i}`, value: k.offset });
|
|
205
|
+
});
|
|
206
|
+
return out;
|
|
207
|
+
}
|
|
208
|
+
// ------------------------------------------------------------------ noise
|
|
209
|
+
function hash(x, y, seed) {
|
|
210
|
+
let h = Math.imul(x, 0x27d4eb2d) ^ Math.imul(y, 0x165667b1) ^ Math.imul(seed + 1, 0x9e3779b1);
|
|
211
|
+
h = Math.imul(h ^ (h >>> 15), 0x85ebca6b);
|
|
212
|
+
h ^= h >>> 13;
|
|
213
|
+
h = Math.imul(h, 0xc2b2ae35);
|
|
214
|
+
h ^= h >>> 16;
|
|
215
|
+
return (h >>> 0) / 4294967296;
|
|
216
|
+
}
|
|
217
|
+
const fade = (t) => t * t * t * (t * (t * 6 - 15) + 10);
|
|
218
|
+
const wrap = (i, n) => ((i % n) + n) % n;
|
|
219
|
+
/** One octave at (x, y) in cell units, periodic in `period` cells. Returns -1..1 (perlin) or 0..1 (the rest). */
|
|
220
|
+
function octave(kind, x, y, period, seed) {
|
|
221
|
+
const ix = Math.floor(x), iy = Math.floor(y), fx = x - ix, fy = y - iy;
|
|
222
|
+
if (kind === 'value' || kind === 'perlin') {
|
|
223
|
+
const corner = (cx, cy) => {
|
|
224
|
+
const hx = wrap(ix + cx, period), hy = wrap(iy + cy, period);
|
|
225
|
+
if (kind === 'value')
|
|
226
|
+
return hash(hx, hy, seed);
|
|
227
|
+
const a = hash(hx, hy, seed) * Math.PI * 2;
|
|
228
|
+
return Math.cos(a) * (fx - cx) + Math.sin(a) * (fy - cy);
|
|
229
|
+
};
|
|
230
|
+
const u = fade(fx), v = fade(fy);
|
|
231
|
+
const a = corner(0, 0) + (corner(1, 0) - corner(0, 0)) * u;
|
|
232
|
+
const b = corner(0, 1) + (corner(1, 1) - corner(0, 1)) * u;
|
|
233
|
+
// perlin: scaled to Godot's simplex spread (sd ~0.54, measured against an exported
|
|
234
|
+
// NoiseTexture2D) so a pingpong / ridged fold lands where Godot's does.
|
|
235
|
+
return kind === 'perlin' ? Math.max(-1, Math.min(1, (a + (b - a) * v) * Math.SQRT2 * 1.75)) : a + (b - a) * v;
|
|
236
|
+
}
|
|
237
|
+
let f1 = 9, f2 = 9;
|
|
238
|
+
for (let oy = -1; oy <= 1; oy++)
|
|
239
|
+
for (let ox = -1; ox <= 1; ox++) {
|
|
240
|
+
const hx = wrap(ix + ox, period), hy = wrap(iy + oy, period);
|
|
241
|
+
const px = ox + hash(hx, hy, seed) - fx, py = oy + hash(hx, hy, seed + 7919) - fy;
|
|
242
|
+
const d = Math.hypot(px, py);
|
|
243
|
+
if (d < f1) {
|
|
244
|
+
f2 = f1;
|
|
245
|
+
f1 = d;
|
|
246
|
+
}
|
|
247
|
+
else if (d < f2)
|
|
248
|
+
f2 = d;
|
|
249
|
+
}
|
|
250
|
+
return kind === 'cellular' ? Math.min(1, f1) : Math.min(1, f2 - f1);
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* The noise as `size * size` bytes, normalised to the full 0..255 range (as
|
|
254
|
+
* Godot's NoiseTexture2D does by default). Tileable: every octave is periodic
|
|
255
|
+
* in a whole number of cells. Deterministic per source.
|
|
256
|
+
*/
|
|
257
|
+
export function generateNoise(n) {
|
|
258
|
+
const size = n.size, out = new Float32Array(size * size);
|
|
259
|
+
const octaves = n.fractal === 'none' ? 1 : n.octaves;
|
|
260
|
+
let lo = Infinity, hi = -Infinity;
|
|
261
|
+
for (let py = 0; py < size; py++)
|
|
262
|
+
for (let px = 0; px < size; px++) {
|
|
263
|
+
let sum = 0, amp = 1;
|
|
264
|
+
// Warp: offset the sample point by a periodic perlin pair at the base frequency, so it still tiles.
|
|
265
|
+
let ux = (px + .5) / size, uy = (py + .5) / size;
|
|
266
|
+
if (n.warp > 0) {
|
|
267
|
+
// Two octaves (2f, 4f): one octave alone leaves perlin's zero-at-every-lattice-point grid visible as pinches.
|
|
268
|
+
const f = n.frequency * 2, w = (seed) => octave('perlin', ux * f, uy * f, f, seed) + .5 * octave('perlin', ux * f * 2, uy * f * 2, f * 2, seed + 1);
|
|
269
|
+
const wx = w(n.seed + 5003), wy = w(n.seed + 9001);
|
|
270
|
+
ux += wx * n.warp / n.frequency;
|
|
271
|
+
uy += wy * n.warp / n.frequency;
|
|
272
|
+
}
|
|
273
|
+
for (let o = 0; o < octaves; o++) {
|
|
274
|
+
const period = n.frequency << o;
|
|
275
|
+
let v = octave(n.kind, ux * period, uy * period, period, n.seed + o * 101);
|
|
276
|
+
if (n.kind !== 'perlin')
|
|
277
|
+
v = v * 2 - 1; // all -1..1 from here
|
|
278
|
+
if (n.fractal === 'ridged')
|
|
279
|
+
v = 1 - Math.abs(v) * 2;
|
|
280
|
+
else if (n.fractal === 'pingpong') {
|
|
281
|
+
const t = (v + 1) * n.pingpong;
|
|
282
|
+
const p = t - Math.floor(t * .5) * 2;
|
|
283
|
+
v = (p < 1 ? p : 2 - p) * 2 - 1;
|
|
284
|
+
}
|
|
285
|
+
sum += v * amp;
|
|
286
|
+
amp *= .5;
|
|
287
|
+
}
|
|
288
|
+
out[py * size + px] = sum;
|
|
289
|
+
if (sum < lo)
|
|
290
|
+
lo = sum;
|
|
291
|
+
if (sum > hi)
|
|
292
|
+
hi = sum;
|
|
293
|
+
}
|
|
294
|
+
const bytes = new Uint8Array(size * size), k = hi > lo ? 255 / (hi - lo) : 0;
|
|
295
|
+
for (let i = 0; i < out.length; i++)
|
|
296
|
+
bytes[i] = Math.round((out[i] - lo) * k);
|
|
297
|
+
return bytes;
|
|
298
|
+
}
|
|
299
|
+
// Shared across instances: a texture per distinct noise, freed when the last user lets go.
|
|
300
|
+
const noiseCache = new Map();
|
|
301
|
+
export function acquireNoiseTexture(n) {
|
|
302
|
+
const key = JSON.stringify([n.kind, n.fractal, n.octaves, n.frequency, n.seed, n.warp, n.pingpong, n.size]);
|
|
303
|
+
let hit = noiseCache.get(key);
|
|
304
|
+
if (!hit) {
|
|
305
|
+
const t = new T.DataTexture(generateNoise(n), n.size, n.size, T.RedFormat, T.UnsignedByteType);
|
|
306
|
+
t.wrapS = t.wrapT = T.RepeatWrapping;
|
|
307
|
+
t.magFilter = T.LinearFilter;
|
|
308
|
+
t.minFilter = T.LinearMipmapLinearFilter;
|
|
309
|
+
t.generateMipmaps = true;
|
|
310
|
+
t.needsUpdate = true;
|
|
311
|
+
t.name = `noise:${key}`;
|
|
312
|
+
hit = { texture: t, users: 0 };
|
|
313
|
+
noiseCache.set(key, hit);
|
|
314
|
+
}
|
|
315
|
+
hit.users++;
|
|
316
|
+
return hit.texture;
|
|
317
|
+
}
|
|
318
|
+
export function releaseNoiseTexture(t) {
|
|
319
|
+
for (const [key, hit] of noiseCache)
|
|
320
|
+
if (hit.texture === t) {
|
|
321
|
+
if (--hit.users <= 0) {
|
|
322
|
+
t.dispose();
|
|
323
|
+
noiseCache.delete(key);
|
|
324
|
+
}
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
/** For tests: how many distinct noise textures are alive. */
|
|
329
|
+
export function noiseTexturesAlive() { return noiseCache.size; }
|
|
330
|
+
// ------------------------------------------------------------------ shader
|
|
331
|
+
/** 256 x 1 sRGB ramp of the gradient map. */
|
|
332
|
+
function gradientTexture(g) {
|
|
333
|
+
const w = 256, data = new Uint8Array(w * 4), c = [0, 0, 0, 0];
|
|
334
|
+
for (let i = 0; i < w; i++) {
|
|
335
|
+
sampleGradient(g, i / (w - 1), c);
|
|
336
|
+
for (let k = 0; k < 4; k++)
|
|
337
|
+
data[i * 4 + k] = Math.round(Math.min(1, Math.max(0, c[k])) * 255);
|
|
338
|
+
}
|
|
339
|
+
const t = new T.DataTexture(data, w, 1, T.RGBAFormat, T.UnsignedByteType);
|
|
340
|
+
t.colorSpace = T.SRGBColorSpace;
|
|
341
|
+
t.magFilter = t.minFilter = T.LinearFilter;
|
|
342
|
+
t.wrapS = t.wrapT = T.ClampToEdgeWrapping;
|
|
343
|
+
t.needsUpdate = true;
|
|
344
|
+
return t;
|
|
345
|
+
}
|
|
346
|
+
/** Every curve as a 256-wide row, sampled at the row's centre texels. */
|
|
347
|
+
function curveTexture(curves) {
|
|
348
|
+
const w = 256, rows = Math.max(1, curves.length), data = new Float32Array(w * rows);
|
|
349
|
+
curves.forEach((c, r) => { for (let i = 0; i < w; i++)
|
|
350
|
+
data[r * w + i] = sampleCurve(c.overLife, i / (w - 1)); });
|
|
351
|
+
const t = new T.DataTexture(data, w, rows, T.RedFormat, T.FloatType);
|
|
352
|
+
t.magFilter = t.minFilter = T.NearestFilter;
|
|
353
|
+
t.needsUpdate = true;
|
|
354
|
+
return t;
|
|
355
|
+
}
|
|
356
|
+
const glslFloat = (x) => { const s = String(x); return /[.e]/.test(s) ? s : s + '.0'; };
|
|
357
|
+
/** GLSL for one mask's raw value (before invert) at uv. */
|
|
358
|
+
function sourceCode(s, i, lod) {
|
|
359
|
+
const tex = (expr) => lod ? `textureLod(uMask${i}, ${expr}, 0.0)` : `texture2D(uMask${i}, ${expr})`;
|
|
360
|
+
switch (s.type) {
|
|
361
|
+
case 'noise': return `${tex('uv')}.r`;
|
|
362
|
+
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
|
+
case 'gradient': {
|
|
364
|
+
const c = `vec2(${glslFloat(s.center[0])}, ${glslFloat(s.center[1])})`;
|
|
365
|
+
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
|
+
}
|
|
367
|
+
case 'shape': return `vfxShape${s.shape[0].toUpperCase()}${s.shape.slice(1)}(fract(uv) * 2.0 - 1.0, uShape${i})`;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
const BLEND_CODE = {
|
|
371
|
+
add: 'a + b', subtract: 'a - b', multiply: 'a * b', min: 'min(a, b)', max: 'max(a, b)',
|
|
372
|
+
screen: '1.0 - (1.0 - a) * (1.0 - b)', overlay: '(a < 0.5 ? 2.0 * a * b : 1.0 - 2.0 * (1.0 - a) * (1.0 - b))',
|
|
373
|
+
hardLight: '(b < 0.5 ? 2.0 * a * b : 1.0 - 2.0 * (1.0 - a) * (1.0 - b))',
|
|
374
|
+
softLight: '(b < 0.5 ? 2.0 * a * b + a * a * (1.0 - 2.0 * b) : sqrt(max(a, 0.0)) * (2.0 * b - 1.0) + 2.0 * a * (1.0 - b))',
|
|
375
|
+
dodge: 'a / max(1.0 - b, 1e-3)', burn: '1.0 - (1.0 - a) / max(b, 1e-3)', linearBurn: 'a + b - 1.0',
|
|
376
|
+
};
|
|
377
|
+
const SHAPES = `
|
|
378
|
+
// p in -1..1; params = (softness, points, sharpness, radius) and the ring width in uShapeW.
|
|
379
|
+
float vfxShapeDot(vec2 p, vec4 k){ float r = length(p); return r >= 1.0 ? 0.0 : pow(1.0 - r, max(k.x * 4.0, 1e-3)); }
|
|
380
|
+
float vfxShapeGlow(vec2 p, vec4 k){ float r = length(p); if (r >= 1.0) return 0.0; float core = 1.0 - smoothstep(0.0, 0.3, r); return min(1.0, max(core, pow(1.0 - r, 3.0) * 0.6) * (1.0 + core)); }
|
|
381
|
+
float vfxShapeStar(vec2 p, vec4 k){ float r = length(p); if (r >= 1.0) return 0.0; float e = mix(0.12, 1.0, pow(abs(cos(k.y * atan(p.y, p.x) * 0.5)), 1.0 + k.z * 12.0)); return max(1.0 - smoothstep(e * 0.5, e, r), (1.0 - smoothstep(0.0, 0.25, r)) * 0.8); }
|
|
382
|
+
float vfxShapeRing(vec2 p, vec4 k){ float d = abs(length(p) - k.w); float w = max(k.y, 1e-3); return 1.0 - smoothstep(w * (1.0 - k.x), w, d); }
|
|
383
|
+
`;
|
|
384
|
+
export function compileMaterial(m) {
|
|
385
|
+
const curves = curveFields(m), row = new Map(curves.map((c, i) => [c.key, i]));
|
|
386
|
+
const rows = Math.max(1, curves.length);
|
|
387
|
+
const value = (key, uniform) => row.has(key) ? `vfxCurve(${glslFloat(row.get(key))}, life.x)` : uniform;
|
|
388
|
+
const uniforms = {
|
|
389
|
+
uTime: { value: 0 }, uRamp: { value: gradientTexture(m.color) }, uCurves: { value: curveTexture(curves.map((c) => c.value)) },
|
|
390
|
+
uDissolve: { value: isCurve(m.dissolve) ? 0 : m.dissolve }, uThreshold: { value: isCurve(m.threshold) ? 0 : m.threshold },
|
|
391
|
+
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
|
+
uEdge: { value: new T.Vector3(m.edge.width, m.edge.emission, 0) }, uEdgeColor: { value: new T.Color(m.edge.color) },
|
|
393
|
+
};
|
|
394
|
+
const owned = [uniforms.uRamp.value, uniforms.uCurves.value];
|
|
395
|
+
const noise = [];
|
|
396
|
+
const images = new Map();
|
|
397
|
+
const decl = [];
|
|
398
|
+
const body = [];
|
|
399
|
+
m.masks.forEach((k, i) => {
|
|
400
|
+
const s = k.source;
|
|
401
|
+
uniforms[`uScale${i}`] = { value: new T.Vector2(...k.scale) };
|
|
402
|
+
uniforms[`uScroll${i}`] = { value: new T.Vector2(...k.scroll) };
|
|
403
|
+
uniforms[`uSpin${i}`] = { value: k.spin * Math.PI / 180 };
|
|
404
|
+
uniforms[`uTurn${i}`] = { value: k.rotation * Math.PI / 180 };
|
|
405
|
+
uniforms[`uOffset${i}`] = { value: isCurve(k.offset) ? 0 : k.offset };
|
|
406
|
+
uniforms[`uStrength${i}`] = { value: isCurve(k.strength) ? 1 : k.strength };
|
|
407
|
+
uniforms[`uLevels${i}`] = { value: new T.Vector2(...(k.levels ?? [0, 1])) };
|
|
408
|
+
decl.push(`uniform vec2 uScale${i}; uniform vec2 uScroll${i}; uniform float uSpin${i}; uniform float uStrength${i}; uniform vec2 uLevels${i}; uniform float uTurn${i}; uniform float uOffset${i};`);
|
|
409
|
+
if (s.type === 'noise') {
|
|
410
|
+
const t = acquireNoiseTexture(s);
|
|
411
|
+
noise.push(t);
|
|
412
|
+
uniforms[`uMask${i}`] = { value: t };
|
|
413
|
+
decl.push(`uniform sampler2D uMask${i};`);
|
|
414
|
+
}
|
|
415
|
+
else if (s.type === 'image') {
|
|
416
|
+
uniforms[`uMask${i}`] = { value: null };
|
|
417
|
+
images.set(i, s.texture);
|
|
418
|
+
decl.push(`uniform sampler2D uMask${i};`);
|
|
419
|
+
}
|
|
420
|
+
else if (s.type === 'shape') {
|
|
421
|
+
uniforms[`uShape${i}`] = { value: new T.Vector4(s.softness, s.shape === 'ring' ? s.width : s.points, s.sharpness, s.radius) };
|
|
422
|
+
decl.push(`uniform vec4 uShape${i};`);
|
|
423
|
+
}
|
|
424
|
+
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
|
+
const random = k.randomOffset ? ` + life.y * vec2(17.31, 7.97)` : '';
|
|
426
|
+
body.push(` { vec2 uv = uv0; ${polar}
|
|
427
|
+
uv = uv * uScale${i} - uScroll${i} * uTime${random};
|
|
428
|
+
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;
|
|
429
|
+
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);` : ''}
|
|
430
|
+
${i === 0 ? 'm = b;' : `float a = m; m = mix(a, ${BLEND_CODE[k.blend]}, clamp(${value(`strength${i}`, `uStrength${i}`)}, 0.0, 1.0));`} }`);
|
|
431
|
+
});
|
|
432
|
+
const code = `
|
|
433
|
+
uniform float uTime; uniform sampler2D uRamp; uniform sampler2D uCurves;
|
|
434
|
+
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;
|
|
436
|
+
${decl.join('\n')}
|
|
437
|
+
${SHAPES}
|
|
438
|
+
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
|
+
vec4 vfxMaterial(vec2 uv0, vec2 life){
|
|
440
|
+
float m = 0.0;
|
|
441
|
+
${body.join('\n')}
|
|
442
|
+
m = clamp(m, 0.0, 1.0);
|
|
443
|
+
float v = m - ${value('dissolve', 'uDissolve')};
|
|
444
|
+
float th = ${value('threshold', 'uThreshold')};
|
|
445
|
+
float alpha = pow(${m.ramp === 'linear' ? 'clamp((v - th) / max(uSoftness, 1e-4), 0.0, 1.0)' : 'smoothstep(th, th + max(uSoftness, 1e-4), v)'}, uPower);
|
|
446
|
+
vec4 ramp = texture2D(uRamp, vec2(clamp(v, 0.0, 1.0) * (255.0 / 256.0) + 0.5 / 256.0, 0.5));
|
|
447
|
+
vec3 rgb = ramp.rgb;
|
|
448
|
+
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)); }
|
|
449
|
+
return vec4(rgb * max(0.0, ${value('emission', 'uEmission')}), ramp.a * alpha * clamp(${value('opacity', 'uOpacity')}, 0.0, 1.0));
|
|
450
|
+
}`;
|
|
451
|
+
return {
|
|
452
|
+
code, uniforms, images, alphaMode: m.alphaMode,
|
|
453
|
+
dispose() { for (const t of owned)
|
|
454
|
+
t.dispose(); for (const t of noise)
|
|
455
|
+
releaseNoiseTexture(t); },
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
// ------------------------------------------------------------------ presets
|
|
459
|
+
const noise = (p) => ({ ...noiseDefaults(), ...p });
|
|
460
|
+
const mask = (source, p = {}) => ({ ...maskDefaults(source), ...p });
|
|
461
|
+
const curve = (...points) => ({ overLife: { points, smooth: true } });
|
|
462
|
+
/**
|
|
463
|
+
* Starting points, so nobody begins from a blank material. Each is what one
|
|
464
|
+
* of the reference rebuilds needed (ADR-055), with the numbers rounded.
|
|
465
|
+
*/
|
|
466
|
+
export const MATERIAL_PRESETS = {
|
|
467
|
+
softGlow: () => ({ ...materialDefaults(), masks: [mask(shapeSource('glow'))], emission: 2, color: { stops: [[0, '#ffffff', 1]] } }),
|
|
468
|
+
noiseSmoke: () => ({
|
|
469
|
+
...materialDefaults(),
|
|
470
|
+
masks: [
|
|
471
|
+
mask(noise({ kind: 'perlin', fractal: 'fbm', octaves: 3, frequency: 6, warp: .25 }), { scale: [.6, .6], scroll: [0, .2], randomOffset: true }),
|
|
472
|
+
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] }),
|
|
474
|
+
],
|
|
475
|
+
threshold: 0, softness: 1, ramp: 'linear', power: 3, emission: 1.5, color: { stops: [[0, '#4b4b4b', 1]] },
|
|
476
|
+
}),
|
|
477
|
+
fireCore: () => ({
|
|
478
|
+
...materialDefaults(),
|
|
479
|
+
masks: [
|
|
480
|
+
mask({ type: 'gradient', fill: 'radial', center: [.5, .5] }, { levels: [-.2, 1] }),
|
|
481
|
+
mask(noise({ kind: 'perlin', fractal: 'ridged', octaves: 3, frequency: 6, warp: .35 }), { scale: [.15, .6], scroll: [0, .4], randomOffset: true }),
|
|
482
|
+
],
|
|
483
|
+
threshold: .3, softness: .2, emission: curve([0, 4], [.3, 1.4], [1, 1]),
|
|
484
|
+
color: { stops: [[0, '#ff3a1a', 1], [.6, '#ff8a1a', 1], [1, '#ffe08a', 1]] },
|
|
485
|
+
}),
|
|
486
|
+
dissolveBurst: () => ({
|
|
487
|
+
...materialDefaults(),
|
|
488
|
+
masks: [mask(shapeSource('dot')), mask(noise({ kind: 'cellular', fractal: 'fbm', octaves: 3, frequency: 5 }), { randomOffset: true, blend: 'multiply' })],
|
|
489
|
+
dissolve: curve([0, 0], [1, .9]), softness: .08, edge: { width: .12, color: '#ffb347', emission: 4 },
|
|
490
|
+
color: { stops: [[0, '#5a2a10', 1], [1, '#ffe2b0', 1]] }, emission: 1.5,
|
|
491
|
+
}),
|
|
492
|
+
energyRing: () => ({
|
|
493
|
+
...materialDefaults(),
|
|
494
|
+
masks: [
|
|
495
|
+
mask({ type: 'shape', shape: 'ring', softness: .6, points: 4, sharpness: .5, radius: .7, width: .25 }),
|
|
496
|
+
mask(noise({ kind: 'perlin', fractal: 'ridged', octaves: 2, frequency: 4 }), { polar: true, scroll: [0, -.8], blend: 'multiply', strength: .7 }),
|
|
497
|
+
],
|
|
498
|
+
emission: 3, color: { stops: [[0, '#2a6cff', 1], [1, '#d6f0ff', 1]] },
|
|
499
|
+
}),
|
|
500
|
+
};
|
package/dist/vfx/templates.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { campfire } from './document.js';
|
|
1
|
+
import { campfire, upgradeV1, emitterLayer } from './document.js';
|
|
2
2
|
/**
|
|
3
3
|
* Starting points for "New effect". They ship WITH the editor rather than
|
|
4
4
|
* coming from the Store, so creating an effect never depends on somebody
|
|
@@ -11,28 +11,30 @@ import { campfire } from './document.js';
|
|
|
11
11
|
* that falls back, not a sphere.
|
|
12
12
|
*/
|
|
13
13
|
const layer = (id, e) => ({ id, enabled: true, ...e });
|
|
14
|
+
// The ready-made ones were written in the version-1 model and are upgraded on the way out.
|
|
15
|
+
const v1 = (d) => upgradeV1({ version: 1, ...d });
|
|
14
16
|
const TEMPLATES = {
|
|
15
17
|
// Blank is ONE plain layer, not zero: a document needs at least one emitter,
|
|
16
18
|
// and something on screen to change is a better start than an empty view.
|
|
17
|
-
blank: () => ({ version:
|
|
18
|
-
|
|
19
|
+
blank: () => ({ version: 2, name: 'New Effect', seed: 7, duration: 3, loop: true, humanCurated: false, emitters: [
|
|
20
|
+
emitterLayer('layer-1', { size: .3, sizeOverLife: { points: [[0, 1], [1, .35]] }, color: { stops: [[0, '#ffffff', .8]] }, colorOverLife: { stops: [[0, '#ffffff', 1], [1, '#9aa3b2', 1]] } })
|
|
19
21
|
] }),
|
|
20
22
|
campfire,
|
|
21
|
-
smoke: () => ({
|
|
23
|
+
smoke: () => v1({ name: 'Smoke', seed: 23, duration: 4, loop: true, humanCurated: false, emitters: [
|
|
22
24
|
layer('puff', { rate: 14, lifetime: 4, speed: .6, spread: .35, gravity: .05, size: .25, endSize: 1.2, opacity: .35, color: '#8a8f99', endColor: '#3d424b', blend: 'normal' }),
|
|
23
25
|
layer('wisp', { rate: 6, lifetime: 3, speed: .9, spread: .2, gravity: 0, size: .12, endSize: .6, opacity: .2, color: '#b4b8c0', endColor: '#5b616b', blend: 'normal' })
|
|
24
26
|
] }),
|
|
25
|
-
sparks: () => ({
|
|
27
|
+
sparks: () => v1({ name: 'Sparks', seed: 31, duration: 3, loop: true, humanCurated: false, emitters: [
|
|
26
28
|
layer('sparks', { rate: 80, lifetime: .9, speed: 3.2, spread: 2.4, gravity: -6, size: .04, endSize: .01, opacity: 1, color: '#fff3b0', endColor: '#ff7a1a', blend: 'additive' }),
|
|
27
29
|
layer('glow', { rate: 20, lifetime: .25, speed: .3, spread: .3, gravity: 0, size: .35, endSize: .1, opacity: .5, color: '#ffd27a', endColor: '#ff8a2a', blend: 'additive' })
|
|
28
30
|
] }),
|
|
29
|
-
explosion: () => ({
|
|
31
|
+
explosion: () => v1({ name: 'Explosion', seed: 41, duration: .15, loop: false, humanCurated: false, emitters: [
|
|
30
32
|
layer('flash', { rate: 300, lifetime: .35, speed: 1.2, spread: 4, gravity: 0, size: .9, endSize: .2, opacity: .9, color: '#fff2c4', endColor: '#ff6a1a', blend: 'additive' }),
|
|
31
33
|
layer('fire', { rate: 300, lifetime: .8, speed: 2.5, spread: 5, gravity: -1.5, size: .45, endSize: .1, opacity: .8, color: '#ffb347', endColor: '#d4290f', blend: 'additive' }),
|
|
32
34
|
layer('debris', { rate: 300, lifetime: 1.4, speed: 4, spread: 5, gravity: -7, size: .05, endSize: .02, opacity: 1, color: '#ffe08a', endColor: '#ff4d00', blend: 'additive' }),
|
|
33
35
|
layer('smoke', { rate: 200, lifetime: 2.5, speed: .8, spread: 2, gravity: .4, size: .4, endSize: 1.4, opacity: .25, color: '#5a5550', endColor: '#2a2724', blend: 'normal' })
|
|
34
36
|
] }),
|
|
35
|
-
magic: () => ({
|
|
37
|
+
magic: () => v1({ name: 'Magic Aura', seed: 53, duration: 4, loop: true, humanCurated: false, emitters: [
|
|
36
38
|
layer('motes', { rate: 40, lifetime: 2, speed: .6, spread: .9, gravity: .2, size: .08, endSize: .01, opacity: .9, color: '#b98cff', endColor: '#3fd3ff', blend: 'additive' }),
|
|
37
39
|
layer('core', { rate: 12, lifetime: 1.2, speed: .2, spread: .2, gravity: .4, size: .5, endSize: .15, opacity: .35, color: '#7a5cff', endColor: '#2bb8ff', blend: 'additive' }),
|
|
38
40
|
layer('sparkle', { rate: 18, lifetime: .6, speed: 1.4, spread: 1.2, gravity: -.5, size: .05, endSize: .005, opacity: 1, color: '#ffffff', endColor: '#c9a8ff', blend: 'additive' })
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umicat/three-sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.37.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",
|