reze-engine 0.69.2 → 0.69.4

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.
@@ -1,27 +1,4 @@
1
- /**
2
- * A material parameter over time.
3
- *
4
- * The other half of step 5: dissolve, teleport, a look that changes on a beat.
5
- * The per-material channel already exists — setStyleParam writes straight into
6
- * the style uniform — so what was missing was only a way to drive it from the
7
- * scene clock rather than from whenever a caller happened to fire.
8
- *
9
- * WHY THE SAMPLING LIVES HERE, alone in its own file: everything else in this
10
- * feature needs a GPU, and this does not. Keeping it pure is what lets the
11
- * interpolation be tested exhaustively in a headless suite, which matters
12
- * because it is the part with edge cases — the ends, a single key, keys at the
13
- * same instant.
14
- *
15
- * NOT bezier. Bone animation has curves because a VMD carries them and a body
16
- * needs ease; a material parameter is a dial, and a dial that needs shaping can
17
- * be shaped by placing more keys. If eased segments are ever wanted, they
18
- * belong as a property ON a key, not as a second sampler.
19
- *
20
- * DETERMINISTIC BY CONSTRUCTION: the value is a pure function of the scene
21
- * clock, so an offline export stepping frame by frame produces exactly what
22
- * playback did. Anything read from wall time would not, which is the same rule
23
- * the score and audio interfaces already follow.
24
- */
1
+ import { bezierInterpolate } from "./animation";
25
2
  const lerp = (a, b, k) => a + (b - a) * k;
26
3
  /**
27
4
  * The value at `t`.
@@ -59,7 +36,16 @@ export function sampleParamTrack(keys, t) {
59
36
  // by the zero span would be a NaN written into a uniform every frame.
60
37
  if (span <= 0)
61
38
  return b.v;
62
- const k = (t - a.t) / span;
39
+ if (b.e === "hold")
40
+ return a.v;
41
+ const u = (t - a.t) / span;
42
+ // Smoothstep: no jump in speed at either key, which is what makes a dial
43
+ // that eases between two values read as a move rather than as a ramp.
44
+ const k = b.b
45
+ ? bezierInterpolate(b.b[0], b.b[2], b.b[1], b.b[3], u)
46
+ : b.e === "ease"
47
+ ? u * u * (3 - 2 * u)
48
+ : u;
63
49
  if (typeof a.v === "number" && typeof b.v === "number")
64
50
  return lerp(a.v, b.v, k);
65
51
  // Mixed scalar and vector keys are an authoring mistake, not something to
@@ -45,11 +45,12 @@ export type EffectParamOption = {
45
45
  */
46
46
  export type EffectParamDecl = {
47
47
  name: string;
48
- kind: "float" | "color" | "vec3" | "bool" | "enum";
49
- /** Numbers for float/vec3, 0 or 1 for bool, the option's index for enum;
50
- * `#rrggbb` for a colour, which the host converts. */
48
+ kind: "float" | "color" | "vec3" | "bool" | "enum" | "int" | "point" | "bone";
49
+ /** Numbers for float/vec3/int/point, 0 or 1 for bool, the option's index for
50
+ * enum, a bone's NAME for bone; `#rrggbb` for a colour, which the host
51
+ * converts. */
51
52
  value: number | [number, number, number] | string;
52
- /** float only, and only when the author gave a range. */
53
+ /** float and int only, and only when the author gave a range. */
53
54
  min?: number;
54
55
  max?: number;
55
56
  /** enum only: the choices, in index order. */
@@ -1 +1 @@
1
- {"version":3,"file":"directives.d.ts","sourceRoot":"","sources":["../../src/shaders/directives.ts"],"names":[],"mappings":"AA8BA;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,MAAM,YAAY,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,OAAO,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAA;CAAE,CAAA;AAE1F,6EAA6E;AAC7E,MAAM,MAAM,iBAAiB,GAAG;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,MAAM,CAAA;CAAE,CAAA;AAEhE;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,eAAe,GAAG;IAC5B,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,OAAO,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,CAAA;IAClD;2DACuD;IACvD,KAAK,EAAE,MAAM,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,GAAG,MAAM,CAAA;IACjD,yDAAyD;IACzD,GAA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QAA8B,CAAA;AAmCzD,MAAM,MAAM,eAAe,GAAG;IAAE,UAAU,EAAE,gBAAgB,CAAC;IAAC,MAAM,EAAE,MAAM,EAAE,CAAA;CAAE,CAAA;AAEhF,2EAA2E;AAC3E,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,eAAe,CAmT7D;AAED;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAKpD"}
@@ -193,6 +193,45 @@ export function parseDirectives(wgsl) {
193
193
  d.params.push({ name, kind: "bool", value: on ? 1 : 0, ...labels });
194
194
  return;
195
195
  }
196
+ if (kind === "int") {
197
+ // A count: strands, rows, petals. One f32 in the shader like a
198
+ // slider, but whole — the host steps it and a key never lands between.
199
+ const v = num(rest[0]);
200
+ if (v === null || !Number.isInteger(v)) {
201
+ errors.push(`${at}: #param int ${name} needs a whole-number default`);
202
+ return;
203
+ }
204
+ const lo = num(rest[1]);
205
+ const hi = num(rest[2]);
206
+ if ((rest[1] !== undefined) !== (rest[2] !== undefined) || (rest[1] !== undefined && (lo === null || hi === null))) {
207
+ errors.push(`${at}: #param int ${name} takes both a min and a max, or neither`);
208
+ return;
209
+ }
210
+ d.params.push({ name, kind: "int", value: v, ...(lo !== null && hi !== null ? { min: lo, max: hi } : {}), ...labels });
211
+ return;
212
+ }
213
+ if (kind === "point") {
214
+ // A place in the world, in world units — set by dragging it in the
215
+ // scene, and able to follow a character. One vec3f in the shader.
216
+ const v = rest.slice(0, 3).map(num);
217
+ if (rest.length !== 3 || v.some((x) => x === null)) {
218
+ errors.push(`${at}: #param point ${name} needs three numbers, a position like "0 10 0"`);
219
+ return;
220
+ }
221
+ d.params.push({ name, kind: "point", value: v, ...labels });
222
+ return;
223
+ }
224
+ if (kind === "bone") {
225
+ // A bone of a character, picked by the host: read as rzBone_NAME(),
226
+ // a position, a forward and whether that rig has it. The default is
227
+ // the bone's name; whose bone it is, the host decides.
228
+ if (rest.length !== 1) {
229
+ errors.push(`${at}: #param bone ${name} needs one bone name, like 右手首`);
230
+ return;
231
+ }
232
+ d.params.push({ name, kind: "bone", value: rest[0], ...labels });
233
+ return;
234
+ }
196
235
  if (kind === "enum") {
197
236
  // Each choice is `id` or `id|中文`; a leading `*` marks the default,
198
237
  // else the first is.
@@ -250,7 +289,7 @@ export function parseDirectives(wgsl) {
250
289
  d.params.push({ name, kind: "float", value: v, ...(lo !== null && hi !== null ? { min: lo, max: hi } : {}), ...labels });
251
290
  return;
252
291
  }
253
- errors.push(`${at}: #param kind must be float, color, vec3, bool or enum`);
292
+ errors.push(`${at}: #param kind must be float, int, color, vec3, point, bone, bool or enum`);
254
293
  return;
255
294
  }
256
295
  case "halfres":
@@ -1,2 +1,2 @@
1
- export declare const MORPH_COMPUTE_WGSL = "\nstruct Params { vertexCount: u32 };\n\n@group(0) @binding(0) var<storage, read> basePos: array<f32>; // vertexCount * 3\n@group(0) @binding(1) var<storage, read> rowStart: array<u32>; // vertexCount + 1\n@group(0) @binding(2) var<storage, read> colMorph: array<u32>; // entryCount\n@group(0) @binding(3) var<storage, read> colOffset: array<f32>; // entryCount * 3\n@group(0) @binding(4) var<storage, read> weights: array<f32>; // morphCount (effective)\n@group(0) @binding(5) var<storage, read_write> vtx: array<f32>; // vertexCount * 8\n@group(0) @binding(6) var<uniform> params: Params;\n\n@compute @workgroup_size(64)\nfn cs(@builtin(global_invocation_id) gid: vec3<u32>) {\n let v = gid.x;\n if (v >= params.vertexCount) { return; }\n\n let b3 = v * 3u;\n var px = basePos[b3];\n var py = basePos[b3 + 1u];\n var pz = basePos[b3 + 2u];\n\n let s = rowStart[v];\n let e = rowStart[v + 1u];\n for (var i = s; i < e; i = i + 1u) {\n let w = weights[colMorph[i]];\n if (w != 0.0) {\n let o3 = i * 3u;\n px = px + w * colOffset[o3];\n py = py + w * colOffset[o3 + 1u];\n pz = pz + w * colOffset[o3 + 2u];\n }\n }\n\n let vo = v * 8u; // VERTEX_STRIDE \u2014 write only the position slots\n vtx[vo] = px;\n vtx[vo + 1u] = py;\n vtx[vo + 2u] = pz;\n}\n";
1
+ export declare const MORPH_COMPUTE_WGSL = "\nstruct Params { vertexCount: u32 };\n\n@group(0) @binding(0) var<storage, read> base: array<f32>; // vertexCount * 5 (xyz uv)\n@group(0) @binding(1) var<storage, read> rowStart: array<u32>; // vertexCount + 1\n@group(0) @binding(2) var<storage, read> colMorph: array<u32>; // entryCount\n@group(0) @binding(3) var<storage, read> colOffset: array<f32>; // entryCount * 5 (xyz uv)\n@group(0) @binding(4) var<storage, read> weights: array<f32>; // morphCount (effective)\n@group(0) @binding(5) var<storage, read_write> vtx: array<f32>; // vertexCount * 8\n@group(0) @binding(6) var<uniform> params: Params;\n\n@compute @workgroup_size(64)\nfn cs(@builtin(global_invocation_id) gid: vec3<u32>) {\n let v = gid.x;\n if (v >= params.vertexCount) { return; }\n\n let b5 = v * 5u;\n var px = base[b5];\n var py = base[b5 + 1u];\n var pz = base[b5 + 2u];\n var tu = base[b5 + 3u];\n var tv = base[b5 + 4u];\n\n let s = rowStart[v];\n let e = rowStart[v + 1u];\n for (var i = s; i < e; i = i + 1u) {\n let w = weights[colMorph[i]];\n if (w != 0.0) {\n let o5 = i * 5u;\n px = px + w * colOffset[o5];\n py = py + w * colOffset[o5 + 1u];\n pz = pz + w * colOffset[o5 + 2u];\n tu = tu + w * colOffset[o5 + 3u];\n tv = tv + w * colOffset[o5 + 4u];\n }\n }\n\n let vo = v * 8u; // VERTEX_STRIDE \u2014 the position and UV slots; the normal is left alone\n vtx[vo] = px;\n vtx[vo + 1u] = py;\n vtx[vo + 2u] = pz;\n vtx[vo + 6u] = tu;\n vtx[vo + 7u] = tv;\n}\n";
2
2
  //# sourceMappingURL=morph.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"morph.d.ts","sourceRoot":"","sources":["../../../src/shaders/passes/morph.ts"],"names":[],"mappings":"AAWA,eAAO,MAAM,kBAAkB,2yCAsC9B,CAAA"}
1
+ {"version":3,"file":"morph.d.ts","sourceRoot":"","sources":["../../../src/shaders/passes/morph.ts"],"names":[],"mappings":"AAWA,eAAO,MAAM,kBAAkB,8/CA4C9B,CAAA"}
@@ -1,20 +1,20 @@
1
- // GPU vertex-morph compute pass. Produces per-vertex morphed positions and writes them
2
- // straight into the interleaved vertex buffer (stride 8: pos3 + normal3 + uv2), so every
3
- // downstream pass (main / shadow / outline / pick) sees the morphed geometry with no VS
4
- // change. Runs once per frame only when a model's morph weights changed.
1
+ // GPU vertex-morph compute pass. Produces per-vertex morphed positions and UVs and writes
2
+ // them straight into the interleaved vertex buffer (stride 8: pos3 + normal3 + uv2), so
3
+ // every downstream pass (main / shadow / outline / pick) sees the morphed geometry with no
4
+ // VS change. Runs once per frame only when a model's morph weights changed.
5
5
  //
6
- // Morph data is a CSR (compressed sparse row) inversion of the per-morph vertex-offset
7
- // lists: for vertex v, its entries live in [rowStart[v], rowStart[v+1]); each entry is a
8
- // (morphIndex, xyz-offset) pair, ordered by ascending morph index — the same accumulation
9
- // order the CPU path used, so results match. Group morphs are resolved to effective
10
- // weights on the CPU and uploaded in `weights`.
6
+ // Morph data is a CSR (compressed sparse row) inversion of the per-morph offset lists: for
7
+ // vertex v, its entries live in [rowStart[v], rowStart[v+1]); each entry is a morph index
8
+ // and five offsets — xyz for a vertex morph, uv for a UV morph (type 3) — ordered by
9
+ // ascending morph index, the same accumulation order the CPU path uses, so results match.
10
+ // Group morphs are resolved to effective weights on the CPU and uploaded in `weights`.
11
11
  export const MORPH_COMPUTE_WGSL = /* wgsl */ `
12
12
  struct Params { vertexCount: u32 };
13
13
 
14
- @group(0) @binding(0) var<storage, read> basePos: array<f32>; // vertexCount * 3
14
+ @group(0) @binding(0) var<storage, read> base: array<f32>; // vertexCount * 5 (xyz uv)
15
15
  @group(0) @binding(1) var<storage, read> rowStart: array<u32>; // vertexCount + 1
16
16
  @group(0) @binding(2) var<storage, read> colMorph: array<u32>; // entryCount
17
- @group(0) @binding(3) var<storage, read> colOffset: array<f32>; // entryCount * 3
17
+ @group(0) @binding(3) var<storage, read> colOffset: array<f32>; // entryCount * 5 (xyz uv)
18
18
  @group(0) @binding(4) var<storage, read> weights: array<f32>; // morphCount (effective)
19
19
  @group(0) @binding(5) var<storage, read_write> vtx: array<f32>; // vertexCount * 8
20
20
  @group(0) @binding(6) var<uniform> params: Params;
@@ -24,26 +24,32 @@ fn cs(@builtin(global_invocation_id) gid: vec3<u32>) {
24
24
  let v = gid.x;
25
25
  if (v >= params.vertexCount) { return; }
26
26
 
27
- let b3 = v * 3u;
28
- var px = basePos[b3];
29
- var py = basePos[b3 + 1u];
30
- var pz = basePos[b3 + 2u];
27
+ let b5 = v * 5u;
28
+ var px = base[b5];
29
+ var py = base[b5 + 1u];
30
+ var pz = base[b5 + 2u];
31
+ var tu = base[b5 + 3u];
32
+ var tv = base[b5 + 4u];
31
33
 
32
34
  let s = rowStart[v];
33
35
  let e = rowStart[v + 1u];
34
36
  for (var i = s; i < e; i = i + 1u) {
35
37
  let w = weights[colMorph[i]];
36
38
  if (w != 0.0) {
37
- let o3 = i * 3u;
38
- px = px + w * colOffset[o3];
39
- py = py + w * colOffset[o3 + 1u];
40
- pz = pz + w * colOffset[o3 + 2u];
39
+ let o5 = i * 5u;
40
+ px = px + w * colOffset[o5];
41
+ py = py + w * colOffset[o5 + 1u];
42
+ pz = pz + w * colOffset[o5 + 2u];
43
+ tu = tu + w * colOffset[o5 + 3u];
44
+ tv = tv + w * colOffset[o5 + 4u];
41
45
  }
42
46
  }
43
47
 
44
- let vo = v * 8u; // VERTEX_STRIDE — write only the position slots
48
+ let vo = v * 8u; // VERTEX_STRIDE — the position and UV slots; the normal is left alone
45
49
  vtx[vo] = px;
46
50
  vtx[vo + 1u] = py;
47
51
  vtx[vo + 2u] = pz;
52
+ vtx[vo + 6u] = tu;
53
+ vtx[vo + 7u] = tv;
48
54
  }
49
55
  `;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "reze-engine",
3
- "version": "0.69.2",
3
+ "version": "0.69.4",
4
4
  "description": "A lightweight WebGPU engine for real-time 3D MMD/PMX model rendering",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
package/src/engine.ts CHANGED
@@ -611,7 +611,31 @@ type SunOptions = {
611
611
  /** An effect param: number → f32, vector-like → vec3f (see setEffect).
612
612
  * Structural {x,y,z} rather than the Vec3 class so JSON-derived values (a
613
613
  * shared scene document's params) pass straight in. */
614
- export type EffectParamValue = number | { x: number; y: number; z: number }
614
+ export type EffectParamValue = number | { x: number; y: number; z: number } | EffectPointValue | EffectBoneValue
615
+
616
+ /** A `point` dial: a world position, or — with `model` — an offset from where
617
+ * that character stands, so the point travels with her. */
618
+ export type EffectPointValue = { x: number; y: number; z: number; model?: string | null }
619
+
620
+ /** A `bone` dial: a bone by name, on `model`, or on the effect's first
621
+ * character when it names none. */
622
+ export type EffectBoneValue = { bone: string; model?: string | null }
623
+
624
+ /** One key of a dial's track. `time` is seconds on the transport — the clock an
625
+ * effect's strips run on. `ease` shapes the way to the NEXT key; switches,
626
+ * lists and bones always hold, whatever it says. */
627
+ export type EffectParamKey = {
628
+ time: number
629
+ value: EffectParamValue
630
+ ease?: "linear" | "ease" | "hold"
631
+ /** The way INTO this key from the previous one, as a bezier — x1, y1, x2, y2
632
+ * in 0..1, the curve a VMD key carries. `ease` is that segment too.
633
+ * Ignored by a hold, and by dials that always hold. */
634
+ curve?: [number, number, number, number]
635
+ }
636
+
637
+ const isBoneValue = (v: EffectParamValue | undefined): v is EffectBoneValue =>
638
+ typeof v === "object" && v !== null && "bone" in v
615
639
 
616
640
  /** A vector by shape rather than by class — Vec3 satisfies it, and so does a
617
641
  * JSON object out of a scene document or a literal typed into a console. */
@@ -1633,7 +1657,8 @@ const TEXTURE_EXTENSION_GUESSES = [".png", ".jpg", ".jpeg", ".bmp", ".tga", ".dd
1633
1657
  * darken every authored colour by the transfer curve twice.
1634
1658
  */
1635
1659
  function paramValue(p: EffectParamDecl, given: EffectParamValue | undefined): EffectParamValue {
1636
- if (given !== undefined) return given
1660
+ if (p.kind === "bone") return isBoneValue(given) ? given : { bone: typeof p.value === "string" ? p.value : "", model: null }
1661
+ if (given !== undefined) return p.kind === "int" && typeof given === "number" ? Math.round(given) : given
1637
1662
  if (p.kind === "color" && typeof p.value === "string") {
1638
1663
  const n = parseInt(p.value.slice(1), 16)
1639
1664
  return { x: ((n >> 16) & 255) / 255, y: ((n >> 8) & 255) / 255, z: (n & 255) / 255 }
@@ -1864,6 +1889,12 @@ interface EffectInstance {
1864
1889
  paramLayout: Map<string, { offset: number; comps: 1 | 3 }>
1865
1890
  paramsBuffer: GPUBuffer | null
1866
1891
  paramsData: Float32Array<ArrayBuffer>
1892
+ /** The dials the engine resolves every frame rather than once: each bone
1893
+ * dial's bone and character, and each point dial set to follow one. */
1894
+ paramBindings: Map<string, EffectBoneValue | EffectPointValue>
1895
+ /** Dials driven by keys, by name — see setEffectParamTrack. Bone keys index
1896
+ * `bones`, since the sampler carries numbers. */
1897
+ paramTracks: Map<string, { keys: ParamKey[]; bones: EffectBoneValue[]; last: ParamValue | null }>
1867
1898
  /** This effect IS a mirror — see the `#mirror` directive. Draws no shader;
1868
1899
  * the engine reads its dials and folds the scene through the plane. */
1869
1900
  hasMirror: boolean
@@ -1930,6 +1961,9 @@ interface EffectInstance {
1930
1961
  * permanently half-strength effect and a scheduled one are the same dial.
1931
1962
  */
1932
1963
  influence: number
1964
+ /** `influence` over time, keyed on the transport — a fade is two keys.
1965
+ * Null holds `influence`. */
1966
+ influenceTrack: ParamKey[] | null
1933
1967
  /** Its strips, in scene seconds — a LANE, so one effect can fire more than
1934
1968
  * once. Null or empty = on for the whole scene, which is what applying an
1935
1969
  * effect does until someone places it. */
@@ -5673,7 +5707,19 @@ export class Engine {
5673
5707
  // KIND comes from the declaration, never from the runtime type of the
5674
5708
  // value: a caller handing a number to a colour must not silently turn a
5675
5709
  // vec3f field into an f32 and shift every field after it.
5676
- const isVec = p.kind === "color" || p.kind === "vec3"
5710
+ if (p.kind === "bone") {
5711
+ // Three fields the engine fills each frame — where the bone is, the way
5712
+ // it points, and whether this rig has it — read through rzBone_NAME().
5713
+ for (const [field, vec] of [[p.name, true], [`${p.name}__fwd`, true], [`${p.name}__ok`, false]] as const) {
5714
+ const align = vec ? 16 : 4
5715
+ const offset = Math.ceil(cursor / align) * align
5716
+ layout.set(field, { offset: offset / 4, comps: vec ? 3 : 1 })
5717
+ fields.push(` ${field}: ${vec ? "vec3f" : "f32"},`)
5718
+ cursor = offset + (vec ? 12 : 4)
5719
+ }
5720
+ continue
5721
+ }
5722
+ const isVec = p.kind === "color" || p.kind === "vec3" || p.kind === "point"
5677
5723
  const align = isVec ? 16 : 4
5678
5724
  const offset = Math.ceil(cursor / align) * align
5679
5725
  layout.set(p.name, { offset: offset / 4, comps: isVec ? 3 : 1 })
@@ -5681,15 +5727,35 @@ export class Engine {
5681
5727
  cursor = offset + (isVec ? 12 : 4)
5682
5728
  }
5683
5729
  const paramsData = new Float32Array(Math.max(4, Math.ceil(cursor / 16) * 4))
5730
+ const paramBindings = new Map<string, EffectBoneValue | EffectPointValue>()
5684
5731
  for (const [name, value] of entries) {
5685
5732
  const slot = layout.get(name)!
5733
+ // A bone, and a point that follows someone, are resolved every frame;
5734
+ // their fields start empty and the first evaluation fills them.
5735
+ if (isBoneValue(value)) {
5736
+ paramBindings.set(name, value)
5737
+ continue
5738
+ }
5686
5739
  if (typeof value === "number") paramsData[slot.offset] = value
5687
5740
  else {
5741
+ if ("model" in value && value.model) paramBindings.set(name, value)
5688
5742
  paramsData[slot.offset] = value.x
5689
5743
  paramsData[slot.offset + 1] = value.y
5690
5744
  paramsData[slot.offset + 2] = value.z
5691
5745
  }
5692
5746
  }
5747
+ // rzBone_NAME() for each bone dial: its own struct rather than RzAnchor, so
5748
+ // the accessor stands in every module a params block is spliced into.
5749
+ const bones = d.params.filter((p) => p.kind === "bone")
5750
+ const boneWgsl = bones.length
5751
+ ? "struct RzBoneParam { pos: vec3f, fwd: vec3f, valid: bool, }\n" +
5752
+ bones
5753
+ .map(
5754
+ (p) =>
5755
+ `fn rzBone_${p.name}() -> RzBoneParam { return RzBoneParam(params.${p.name}, params.${p.name}__fwd, params.${p.name}__ok > 0.5); }\n`,
5756
+ )
5757
+ .join("")
5758
+ : ""
5693
5759
  /**
5694
5760
  * The params block, at whatever binding the pass asking has free.
5695
5761
  *
@@ -5700,7 +5766,7 @@ export class Engine {
5700
5766
  */
5701
5767
  const paramsWgsl = (binding: number) =>
5702
5768
  entries.length
5703
- ? `struct EffectParams {\n${fields.join("\n")}\n}\n@group(0) @binding(${binding}) var<uniform> params: EffectParams;\n`
5769
+ ? `struct EffectParams {\n${fields.join("\n")}\n}\n@group(0) @binding(${binding}) var<uniform> params: EffectParams;\n${boneWgsl}`
5704
5770
  : ""
5705
5771
  const paramsDecl = paramsWgsl(EFFECT_PARAMS_BINDING)
5706
5772
 
@@ -5906,6 +5972,8 @@ export class Engine {
5906
5972
  paramLayout: layout,
5907
5973
  paramsBuffer,
5908
5974
  paramsData,
5975
+ paramBindings,
5976
+ paramTracks: new Map(),
5909
5977
  hasMirror: d.mirror,
5910
5978
  stepped: d.stepped,
5911
5979
  hasBackground,
@@ -5937,6 +6005,7 @@ export class Engine {
5937
6005
  // scheduling it is something a caller does afterwards, and an install
5938
6006
  // that silently began at zero would look like a compile that failed.
5939
6007
  influence: 1,
6008
+ influenceTrack: null,
5940
6009
  window: null,
5941
6010
  weight: 1,
5942
6011
  sim: { start: null, time: null },
@@ -7525,15 +7594,158 @@ export class Engine {
7525
7594
  setEffectParam(index: number, name: string, value: EffectParamValue): void {
7526
7595
  const fx = this.effects[index]
7527
7596
  if (!fx || !fx.paramsBuffer) return
7597
+ // A keyed dial is its track's: the frame writes it only when the sampled
7598
+ // value changes, so a plain value written over it would stick until then.
7599
+ if (fx.paramTracks.has(name)) return
7600
+ if (this.writeEffectParam(fx, name, value)) this.device.queue.writeBuffer(fx.paramsBuffer, 0, fx.paramsData)
7601
+ // A bone, or a point that now follows someone, lands on the next frame's
7602
+ // evaluation; resolving it now as well means a paused scene shows it too.
7603
+ if (fx.paramBindings.has(name)) this.evaluateEffectParams()
7604
+ }
7605
+
7606
+ /** One dial into the instance's block, by its declared kind. Returns whether
7607
+ * the block changed — a bone changes only its binding, which the frame's
7608
+ * evaluation writes. */
7609
+ private writeEffectParam(fx: EffectInstance, name: string, value: EffectParamValue): boolean {
7610
+ const decl = fx.paramDecls.find((p) => p.name === name)
7528
7611
  const slot = fx.paramLayout.get(name)
7529
- if (!slot) return
7530
- if (typeof value === "number") fx.paramsData[slot.offset] = value
7531
- else {
7532
- fx.paramsData[slot.offset] = value.x
7533
- fx.paramsData[slot.offset + 1] = value.y
7534
- fx.paramsData[slot.offset + 2] = value.z
7612
+ if (!decl || !slot) return false
7613
+ if (decl.kind === "bone") {
7614
+ if (isBoneValue(value)) fx.paramBindings.set(name, value)
7615
+ return false
7616
+ }
7617
+ if (isBoneValue(value)) return false
7618
+ if (typeof value === "number") {
7619
+ fx.paramsData[slot.offset] = decl.kind === "int" ? Math.round(value) : value
7620
+ return true
7621
+ }
7622
+ if (decl.kind === "point" && "model" in value && value.model) fx.paramBindings.set(name, value)
7623
+ else fx.paramBindings.delete(name)
7624
+ fx.paramsData[slot.offset] = value.x
7625
+ fx.paramsData[slot.offset + 1] = value.y
7626
+ fx.paramsData[slot.offset + 2] = value.z
7627
+ return true
7628
+ }
7629
+
7630
+ /**
7631
+ * Drive one dial from keys on the transport — the clock the effect's strips
7632
+ * run on, so a key at a clip's start lands at the clip's start in the editor,
7633
+ * a scrub and an export alike. Null, or no keys, gives the dial back to
7634
+ * setEffectParam.
7635
+ *
7636
+ * Numbers, whole numbers, colours, vectors and points travel between keys,
7637
+ * straight or eased; switches, lists and bones hold until the next key. A
7638
+ * track lives on the instance, so an install builds fresh ones and the host
7639
+ * pushes its tracks again, as it does a schedule.
7640
+ */
7641
+ setEffectParamTrack(index: number, name: string, keys: readonly EffectParamKey[] | null): boolean {
7642
+ const fx = this.effects[index]
7643
+ const decl = fx?.paramDecls.find((p) => p.name === name)
7644
+ if (!fx || !decl) return false
7645
+ if (!keys || keys.length === 0) {
7646
+ fx.paramTracks.delete(name)
7647
+ return true
7648
+ }
7649
+ const holds = decl.kind === "bool" || decl.kind === "enum" || decl.kind === "bone"
7650
+ const bones: EffectBoneValue[] = []
7651
+ const sorted = [...keys].sort((a, b) => a.time - b.time)
7652
+ const track: ParamKey[] = []
7653
+ for (const k of sorted) {
7654
+ let v: ParamValue
7655
+ if (decl.kind === "bone") {
7656
+ if (!isBoneValue(k.value)) continue
7657
+ v = bones.push(k.value) - 1
7658
+ } else if (typeof k.value === "number") v = k.value
7659
+ else if (isBoneValue(k.value)) continue
7660
+ else v = [k.value.x, k.value.y, k.value.z]
7661
+ track.push({
7662
+ t: k.time,
7663
+ v,
7664
+ e: holds ? "hold" : (k.ease ?? "linear"),
7665
+ ...(!holds && k.curve && k.ease !== "hold" ? { b: k.curve } : {}),
7666
+ })
7667
+ }
7668
+ fx.paramTracks.set(name, { keys: track, bones, last: null })
7669
+ this.evaluateEffectParams()
7670
+ return true
7671
+ }
7672
+
7673
+ /**
7674
+ * Every effect's keyed dials at this transport time, then every bone and
7675
+ * followed point where it stands now — once a frame, after the schedules, so
7676
+ * each mount reads this frame's values. One upload per effect, and only for
7677
+ * one whose block moved.
7678
+ */
7679
+ private evaluateEffectParams(): void {
7680
+ let transport: number | null = null
7681
+ for (const fx of this.effects) {
7682
+ if (!fx.paramsBuffer) continue
7683
+ let dirty = false
7684
+ for (const [name, track] of fx.paramTracks) {
7685
+ transport ??= this.transportTime()
7686
+ const v = sampleParamTrack(track.keys, transport)
7687
+ if (v === null || !paramChanged(v, track.last)) continue
7688
+ track.last = v
7689
+ const decl = fx.paramDecls.find((p) => p.name === name)
7690
+ if (decl?.kind === "bone") {
7691
+ const bone = typeof v === "number" ? track.bones[v] : undefined
7692
+ if (bone) fx.paramBindings.set(name, bone)
7693
+ } else if (typeof v === "number") {
7694
+ dirty = this.writeEffectParam(fx, name, v) || dirty
7695
+ } else {
7696
+ const held = fx.paramBindings.get(name)
7697
+ const model = held && !isBoneValue(held) ? held.model : undefined
7698
+ dirty = this.writeEffectParam(fx, name, { x: v[0], y: v[1], z: v[2], ...(model ? { model } : {}) }) || dirty
7699
+ }
7700
+ }
7701
+ if (fx.paramBindings.size) dirty = this.resolveParamBindings(fx) || dirty
7702
+ if (dirty) this.device.queue.writeBuffer(fx.paramsBuffer, 0, fx.paramsData)
7703
+ }
7704
+ }
7705
+
7706
+ /** Bones and followed points into the block, where they stand this frame. */
7707
+ private resolveParamBindings(fx: EffectInstance): boolean {
7708
+ const d = fx.paramsData
7709
+ let moved = false
7710
+ const put = (offset: number, x: number, y: number, z: number) => {
7711
+ if (d[offset] === x && d[offset + 1] === y && d[offset + 2] === z) return
7712
+ d[offset] = x
7713
+ d[offset + 1] = y
7714
+ d[offset + 2] = z
7715
+ moved = true
7716
+ }
7717
+ const first = () => this.castSubjectNames(fx.subjects)[0] ?? null
7718
+ for (const [name, value] of fx.paramBindings) {
7719
+ const slot = fx.paramLayout.get(name)
7720
+ if (!slot) continue
7721
+ const inst = this.modelInstances.get((value.model ?? first()) ?? "")
7722
+ if (isBoneValue(value)) {
7723
+ const fwdSlot = fx.paramLayout.get(`${name}__fwd`)
7724
+ const okSlot = fx.paramLayout.get(`${name}__ok`)
7725
+ const pos = inst?.model.getBoneWorldPosition(value.bone) ?? null
7726
+ const ok = pos ? 1 : 0
7727
+ if (okSlot && d[okSlot.offset] !== ok) {
7728
+ d[okSlot.offset] = ok
7729
+ moved = true
7730
+ }
7731
+ if (!inst || !pos) continue
7732
+ const m = inst.model
7733
+ // Model space into the world, as the cast's own anchors go.
7734
+ pos.setXYZ(pos.x * m.scale, pos.y * m.scale, pos.z * m.scale)
7735
+ Quat.rotateVecInto(m.rotation, pos, pos)
7736
+ put(slot.offset, pos.x + m.position.x, pos.y + m.position.y, pos.z + m.position.z)
7737
+ const fwd = m.getBoneWorldForward(value.bone)
7738
+ if (fwd && fwdSlot) {
7739
+ Quat.rotateVecInto(m.rotation, fwd, fwd)
7740
+ put(fwdSlot.offset, fwd.x, fwd.y, fwd.z)
7741
+ }
7742
+ } else if (inst) {
7743
+ // A followed point: the offset from where she stands.
7744
+ const m = inst.model
7745
+ put(slot.offset, m.position.x + value.x, m.position.y + value.y, m.position.z + value.z)
7746
+ }
7535
7747
  }
7536
- this.device.queue.writeBuffer(fx.paramsBuffer, 0, fx.paramsData)
7748
+ return moved
7537
7749
  }
7538
7750
 
7539
7751
  /**
@@ -7571,6 +7783,28 @@ export class Engine {
7571
7783
  fx.influence = Math.min(1, Math.max(0, influence))
7572
7784
  }
7573
7785
 
7786
+ /**
7787
+ * Influence from keys on the transport, the way a dial is keyed: a fade in is
7788
+ * a key at 0 and a key at 1, eased by its curve. Null or no keys gives it
7789
+ * back to setEffectInfluence. It shapes the level the strips ramp toward.
7790
+ */
7791
+ setEffectInfluenceTrack(index: number, keys: readonly EffectParamKey[] | null): void {
7792
+ const fx = this.effects[index]
7793
+ if (!fx) return
7794
+ const track: ParamKey[] = []
7795
+ for (const k of keys ?? []) {
7796
+ if (typeof k.value !== "number") continue
7797
+ track.push({
7798
+ t: k.time,
7799
+ v: k.value,
7800
+ ...(k.ease ? { e: k.ease } : {}),
7801
+ ...(k.curve && k.ease !== "hold" ? { b: k.curve } : {}),
7802
+ })
7803
+ }
7804
+ track.sort((a, b) => a.t - b.t)
7805
+ fx.influenceTrack = track.length ? track : null
7806
+ }
7807
+
7574
7808
  getEffectInfluence(index: number): number {
7575
7809
  return this.effects[index]?.influence ?? 0
7576
7810
  }
@@ -7659,11 +7893,13 @@ export class Engine {
7659
7893
  const transport = this.transportTime()
7660
7894
  for (const fx of this.effects) {
7661
7895
  fx.simReset = false
7896
+ const keyed = fx.influenceTrack ? sampleParamTrack(fx.influenceTrack, transport) : null
7897
+ const level = typeof keyed === "number" ? Math.min(1, Math.max(0, keyed)) : fx.influence
7662
7898
  if (!fx.window || fx.window.length === 0) {
7663
- fx.weight = fx.influence
7899
+ fx.weight = level
7664
7900
  continue
7665
7901
  }
7666
- const at = effectState(fx.window, fx.influence, transport)
7902
+ const at = effectState(fx.window, level, transport)
7667
7903
  fx.weight = at.weight
7668
7904
  // The particles and the grid carry state from frame to frame, so they get
7669
7905
  // more than a clock: a restart on entering a window or going back in one,
@@ -9298,7 +9534,7 @@ export class Engine {
9298
9534
  )
9299
9535
 
9300
9536
  // GPU vertex-morph compute pipeline (shared by all models; per-model bind groups).
9301
- // Bindings: 0-4 read-only storage (base pos, CSR rowStart/colMorph/colOffset, weights),
9537
+ // Bindings: 0-4 read-only storage (base pos+uv, CSR rowStart/colMorph/colOffset, weights),
9302
9538
  // 5 read-write storage (vertex buffer), 6 uniform (params).
9303
9539
  const roStorage = { type: "read-only-storage" as const }
9304
9540
  this.morphComputeBindGroupLayout = this.device.createBindGroupLayout({
@@ -13499,7 +13735,8 @@ export class Engine {
13499
13735
  scratch[1] = position[1]
13500
13736
  scratch[2] = position[2]
13501
13737
  this.device.queue.writeBuffer(inst.vertexBuffer, index * 32, scratch)
13502
- if (inst.gpuMorph) this.device.queue.writeBuffer(inst.gpuMorph.baseBuf, index * 12, scratch)
13738
+ // The compute base is five floats a vertex (xyz uv): the position is its first three.
13739
+ if (inst.gpuMorph) this.device.queue.writeBuffer(inst.gpuMorph.baseBuf, index * 20, scratch)
13503
13740
  }
13504
13741
  }
13505
13742
 
@@ -15724,7 +15961,7 @@ export class Engine {
15724
15961
  return buf
15725
15962
  }
15726
15963
 
15727
- const baseBuf = mkStorage("basePositions", data.basePositions)
15964
+ const baseBuf = mkStorage("base", data.base)
15728
15965
  const rowBuf = mkStorage("rowStart", data.rowStart)
15729
15966
  const colMorphBuf = mkStorage("colMorph", data.colMorph)
15730
15967
  const colOffsetBuf = mkStorage("colOffset", data.colOffset)
@@ -18087,6 +18324,9 @@ export class Engine {
18087
18324
  // caller so that playback, the export loop and a warm-up pass cannot
18088
18325
  // disagree about when an effect is alive — none of them has to remember it.
18089
18326
  this.evaluateEffectSchedules()
18327
+ // The dials' keys and the bones and points they follow, on this frame —
18328
+ // after the schedules, before anything reads an effect's block.
18329
+ this.evaluateEffectParams()
18090
18330
  this.stepSim(encoder, deltaTime)
18091
18331
  this.stepParticles(encoder, deltaTime)
18092
18332
  // Before the scene pass, which READS the slots this writes. Same buffer,
package/src/index.ts CHANGED
@@ -32,6 +32,9 @@ export {
32
32
  type GizmoDragKind,
33
33
  type DissolveCycle,
34
34
  type EffectParamValue,
35
+ type EffectPointValue,
36
+ type EffectBoneValue,
37
+ type EffectParamKey,
35
38
  type EffectResult,
36
39
  type ViewLut,
37
40
  type CullDiagnostics,