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.
package/src/model.ts CHANGED
@@ -260,10 +260,13 @@ export interface Morphing {
260
260
 
261
261
  // CSR inversion of vertex-morph offsets for the GPU compute pass (built once at load).
262
262
  interface MorphComputeData {
263
- basePositions: Float32Array // vertexCount * 3
263
+ /** vertexCount * 5: each vertex's rest position (xyz) and UV (uv). */
264
+ base: Float32Array
264
265
  rowStart: Uint32Array // vertexCount + 1 (prefix offsets into the entry arrays)
265
266
  colMorph: Uint32Array // entryCount (morph index per entry)
266
- colOffset: Float32Array // entryCount * 3 (xyz offset per entry)
267
+ /** entryCount * 5: a position offset (xyz) and a UV offset (uv) per entry —
268
+ * a vertex morph's carries no UV, a UV morph's no position. */
269
+ colOffset: Float32Array
267
270
  morphCount: number
268
271
  vertexCount: number
269
272
  entryCount: number
@@ -1027,7 +1030,9 @@ export class Model {
1027
1030
 
1028
1031
  /** A PMX additional UV channel (1–4) as loaded, four floats a vertex — or
1029
1032
  * null when the file has fewer. Its morphs (types 4–7) are in getMorphing(),
1030
- * with uvOffsets; the engine applies neither, a native look reads both. */
1033
+ * with uvOffsets; the engine applies neither, a native look reads both. The
1034
+ * base UV's own morphs (type 3) the engine does apply, with the vertex
1035
+ * morphs. */
1031
1036
  getAdditionalUv(channel: number): Float32Array | null {
1032
1037
  return this.additionalUvs[channel - 1] ?? null
1033
1038
  }
@@ -1826,6 +1831,25 @@ export class Model {
1826
1831
  }
1827
1832
  }
1828
1833
 
1834
+ // UV morphs: the base UV moved, the same way a vertex morph moves the
1835
+ // position, into the same touched range.
1836
+ for (let morphIdx = 0; morphIdx < morphCount; morphIdx++) {
1837
+ const effectiveWeight = effectiveWeights[morphIdx]
1838
+ if (effectiveWeight < 0.0001) continue
1839
+ const morph = this.morphing.morphs[morphIdx]
1840
+ if (morph.type !== 3 || !morph.uvOffsets) continue
1841
+ for (const o of morph.uvOffsets) {
1842
+ const vIdx = o.vertexIndex
1843
+ if (vIdx < 0 || vIdx >= vertexCount) continue
1844
+ if (Math.abs(o.offset[0]) < 0.0001 && Math.abs(o.offset[1]) < 0.0001) continue
1845
+ const vertexIdx = vIdx * VERTEX_STRIDE
1846
+ this.vertexData[vertexIdx + 6] += o.offset[0] * effectiveWeight
1847
+ this.vertexData[vertexIdx + 7] += o.offset[1] * effectiveWeight
1848
+ if (curMinVert < 0 || vIdx < curMinVert) curMinVert = vIdx
1849
+ if (vIdx > curMaxVert) curMaxVert = vIdx
1850
+ }
1851
+ }
1852
+
1829
1853
  let dirtyMin = curMinVert
1830
1854
  let dirtyMax = curMaxVert
1831
1855
  if (this.morphPrevMaxVert >= 0) {
@@ -1879,12 +1903,16 @@ export class Model {
1879
1903
  (Math.abs(o.positionOffset[0]) >= EPS ||
1880
1904
  Math.abs(o.positionOffset[1]) >= EPS ||
1881
1905
  Math.abs(o.positionOffset[2]) >= EPS)
1906
+ // A UV morph (type 3) moves the base UV: a scrolling or spinning texture,
1907
+ // a pattern swapped by sliding to another part of the sheet.
1908
+ const isLiveUv = (o: UvMorphOffset): boolean =>
1909
+ o.vertexIndex >= 0 && o.vertexIndex < V && (Math.abs(o.offset[0]) >= EPS || Math.abs(o.offset[1]) >= EPS)
1882
1910
 
1883
1911
  const counts = new Uint32Array(V)
1884
1912
  for (let m = 0; m < M; m++) {
1885
1913
  const morph = morphs[m]
1886
- if (morph.type !== 1) continue
1887
- for (const o of morph.vertexOffsets) if (isLive(o)) counts[o.vertexIndex]++
1914
+ if (morph.type === 1) for (const o of morph.vertexOffsets) if (isLive(o)) counts[o.vertexIndex]++
1915
+ if (morph.type === 3) for (const o of morph.uvOffsets ?? []) if (isLiveUv(o)) counts[o.vertexIndex]++
1888
1916
  }
1889
1917
 
1890
1918
  const rowStart = new Uint32Array(V + 1)
@@ -1898,31 +1926,43 @@ export class Model {
1898
1926
  if (E === 0) return null
1899
1927
 
1900
1928
  const colMorph = new Uint32Array(E)
1901
- const colOffset = new Float32Array(E * 3)
1929
+ const colOffset = new Float32Array(E * 5)
1902
1930
  const fill = new Uint32Array(V)
1903
1931
  for (let m = 0; m < M; m++) {
1904
1932
  const morph = morphs[m]
1905
- if (morph.type !== 1) continue
1906
- for (const o of morph.vertexOffsets) {
1907
- if (!isLive(o)) continue
1908
- const v = o.vertexIndex
1909
- const p = rowStart[v] + fill[v]++
1910
- colMorph[p] = m
1911
- colOffset[p * 3] = o.positionOffset[0]
1912
- colOffset[p * 3 + 1] = o.positionOffset[1]
1913
- colOffset[p * 3 + 2] = o.positionOffset[2]
1933
+ if (morph.type === 1) {
1934
+ for (const o of morph.vertexOffsets) {
1935
+ if (!isLive(o)) continue
1936
+ const v = o.vertexIndex
1937
+ const p = rowStart[v] + fill[v]++
1938
+ colMorph[p] = m
1939
+ colOffset[p * 5] = o.positionOffset[0]
1940
+ colOffset[p * 5 + 1] = o.positionOffset[1]
1941
+ colOffset[p * 5 + 2] = o.positionOffset[2]
1942
+ }
1943
+ } else if (morph.type === 3) {
1944
+ for (const o of morph.uvOffsets ?? []) {
1945
+ if (!isLiveUv(o)) continue
1946
+ const v = o.vertexIndex
1947
+ const p = rowStart[v] + fill[v]++
1948
+ colMorph[p] = m
1949
+ colOffset[p * 5 + 3] = o.offset[0]
1950
+ colOffset[p * 5 + 4] = o.offset[1]
1951
+ }
1914
1952
  }
1915
1953
  }
1916
1954
 
1917
- const basePositions = new Float32Array(V * 3)
1955
+ const base = new Float32Array(V * 5)
1918
1956
  for (let v = 0; v < V; v++) {
1919
1957
  const vi = v * VERTEX_STRIDE
1920
- basePositions[v * 3] = this.baseVertexData[vi]
1921
- basePositions[v * 3 + 1] = this.baseVertexData[vi + 1]
1922
- basePositions[v * 3 + 2] = this.baseVertexData[vi + 2]
1958
+ base[v * 5] = this.baseVertexData[vi]
1959
+ base[v * 5 + 1] = this.baseVertexData[vi + 1]
1960
+ base[v * 5 + 2] = this.baseVertexData[vi + 2]
1961
+ base[v * 5 + 3] = this.baseVertexData[vi + 6]
1962
+ base[v * 5 + 4] = this.baseVertexData[vi + 7]
1923
1963
  }
1924
1964
 
1925
- return { basePositions, rowStart, colMorph, colOffset, morphCount: M, vertexCount: V, entryCount: E }
1965
+ return { base, rowStart, colMorph, colOffset, morphCount: M, vertexCount: V, entryCount: E }
1926
1966
  }
1927
1967
 
1928
1968
  // Consume the pending morph vertex-upload range for the engine. Returns null when a
@@ -1,3 +1,5 @@
1
+ import { bezierInterpolate } from "./animation"
2
+
1
3
  /**
2
4
  * A material parameter over time.
3
5
  *
@@ -26,10 +28,20 @@
26
28
  /** A parameter value: a scalar, or a vector for the vec3 params. */
27
29
  export type ParamValue = number | [number, number, number]
28
30
 
31
+ /** How the way INTO a key runs: straight, eased in and out, or held at the
32
+ * previous key's value and stepped on arrival. Absent is linear. */
33
+ export type ParamEase = "linear" | "ease" | "hold"
34
+
29
35
  export type ParamKey = {
30
36
  /** Seconds on the SCENE clock — the same clock an export steps. */
31
37
  t: number
32
38
  v: ParamValue
39
+ /** The segment from the previous key to this one — VMD's rule, where a key
40
+ * carries the curve that arrives at it. */
41
+ e?: ParamEase
42
+ /** That segment as a bezier — x1, y1, x2, y2 in 0..1, VMD's own curve — in
43
+ * place of `e` when it is not a hold. */
44
+ b?: [number, number, number, number]
33
45
  }
34
46
 
35
47
  const lerp = (a: number, b: number, k: number): number => a + (b - a) * k
@@ -65,7 +77,15 @@ export function sampleParamTrack(keys: ParamKey[], t: number): ParamValue | null
65
77
  // Two keys at the same instant are a STEP, and the later one wins. Dividing
66
78
  // by the zero span would be a NaN written into a uniform every frame.
67
79
  if (span <= 0) return b.v
68
- const k = (t - a.t) / span
80
+ if (b.e === "hold") return a.v
81
+ const u = (t - a.t) / span
82
+ // Smoothstep: no jump in speed at either key, which is what makes a dial
83
+ // that eases between two values read as a move rather than as a ramp.
84
+ const k = b.b
85
+ ? bezierInterpolate(b.b[0], b.b[2], b.b[1], b.b[3], u)
86
+ : b.e === "ease"
87
+ ? u * u * (3 - 2 * u)
88
+ : u
69
89
  if (typeof a.v === "number" && typeof b.v === "number") return lerp(a.v, b.v, k)
70
90
  // Mixed scalar and vector keys are an authoring mistake, not something to
71
91
  // guess at: hold the segment's start rather than inventing a conversion.
@@ -69,11 +69,12 @@ export type EffectParamOption = { id: string; labelZh?: string }
69
69
  */
70
70
  export type EffectParamDecl = {
71
71
  name: string
72
- kind: "float" | "color" | "vec3" | "bool" | "enum"
73
- /** Numbers for float/vec3, 0 or 1 for bool, the option's index for enum;
74
- * `#rrggbb` for a colour, which the host converts. */
72
+ kind: "float" | "color" | "vec3" | "bool" | "enum" | "int" | "point" | "bone"
73
+ /** Numbers for float/vec3/int/point, 0 or 1 for bool, the option's index for
74
+ * enum, a bone's NAME for bone; `#rrggbb` for a colour, which the host
75
+ * converts. */
75
76
  value: number | [number, number, number] | string
76
- /** float only, and only when the author gave a range. */
77
+ /** float and int only, and only when the author gave a range. */
77
78
  min?: number
78
79
  max?: number
79
80
  /** enum only: the choices, in index order. */
@@ -351,6 +352,45 @@ export function parseDirectives(wgsl: string): DirectiveResult {
351
352
  d.params.push({ name, kind: "bool", value: on ? 1 : 0, ...labels })
352
353
  return
353
354
  }
355
+ if (kind === "int") {
356
+ // A count: strands, rows, petals. One f32 in the shader like a
357
+ // slider, but whole — the host steps it and a key never lands between.
358
+ const v = num(rest[0])
359
+ if (v === null || !Number.isInteger(v)) {
360
+ errors.push(`${at}: #param int ${name} needs a whole-number default`)
361
+ return
362
+ }
363
+ const lo = num(rest[1])
364
+ const hi = num(rest[2])
365
+ if ((rest[1] !== undefined) !== (rest[2] !== undefined) || (rest[1] !== undefined && (lo === null || hi === null))) {
366
+ errors.push(`${at}: #param int ${name} takes both a min and a max, or neither`)
367
+ return
368
+ }
369
+ d.params.push({ name, kind: "int", value: v, ...(lo !== null && hi !== null ? { min: lo, max: hi } : {}), ...labels })
370
+ return
371
+ }
372
+ if (kind === "point") {
373
+ // A place in the world, in world units — set by dragging it in the
374
+ // scene, and able to follow a character. One vec3f in the shader.
375
+ const v = rest.slice(0, 3).map(num)
376
+ if (rest.length !== 3 || v.some((x) => x === null)) {
377
+ errors.push(`${at}: #param point ${name} needs three numbers, a position like "0 10 0"`)
378
+ return
379
+ }
380
+ d.params.push({ name, kind: "point", value: v as [number, number, number], ...labels })
381
+ return
382
+ }
383
+ if (kind === "bone") {
384
+ // A bone of a character, picked by the host: read as rzBone_NAME(),
385
+ // a position, a forward and whether that rig has it. The default is
386
+ // the bone's name; whose bone it is, the host decides.
387
+ if (rest.length !== 1) {
388
+ errors.push(`${at}: #param bone ${name} needs one bone name, like 右手首`)
389
+ return
390
+ }
391
+ d.params.push({ name, kind: "bone", value: rest[0], ...labels })
392
+ return
393
+ }
354
394
  if (kind === "enum") {
355
395
  // Each choice is `id` or `id|中文`; a leading `*` marks the default,
356
396
  // else the first is.
@@ -407,7 +447,7 @@ export function parseDirectives(wgsl: string): DirectiveResult {
407
447
  d.params.push({ name, kind: "float", value: v, ...(lo !== null && hi !== null ? { min: lo, max: hi } : {}), ...labels })
408
448
  return
409
449
  }
410
- errors.push(`${at}: #param kind must be float, color, vec3, bool or enum`)
450
+ errors.push(`${at}: #param kind must be float, int, color, vec3, point, bone, bool or enum`)
411
451
  return
412
452
  }
413
453
  case "halfres":
@@ -1,21 +1,21 @@
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
 
12
12
  export const MORPH_COMPUTE_WGSL = /* wgsl */ `
13
13
  struct Params { vertexCount: u32 };
14
14
 
15
- @group(0) @binding(0) var<storage, read> basePos: array<f32>; // vertexCount * 3
15
+ @group(0) @binding(0) var<storage, read> base: array<f32>; // vertexCount * 5 (xyz uv)
16
16
  @group(0) @binding(1) var<storage, read> rowStart: array<u32>; // vertexCount + 1
17
17
  @group(0) @binding(2) var<storage, read> colMorph: array<u32>; // entryCount
18
- @group(0) @binding(3) var<storage, read> colOffset: array<f32>; // entryCount * 3
18
+ @group(0) @binding(3) var<storage, read> colOffset: array<f32>; // entryCount * 5 (xyz uv)
19
19
  @group(0) @binding(4) var<storage, read> weights: array<f32>; // morphCount (effective)
20
20
  @group(0) @binding(5) var<storage, read_write> vtx: array<f32>; // vertexCount * 8
21
21
  @group(0) @binding(6) var<uniform> params: Params;
@@ -25,26 +25,32 @@ fn cs(@builtin(global_invocation_id) gid: vec3<u32>) {
25
25
  let v = gid.x;
26
26
  if (v >= params.vertexCount) { return; }
27
27
 
28
- let b3 = v * 3u;
29
- var px = basePos[b3];
30
- var py = basePos[b3 + 1u];
31
- var pz = basePos[b3 + 2u];
28
+ let b5 = v * 5u;
29
+ var px = base[b5];
30
+ var py = base[b5 + 1u];
31
+ var pz = base[b5 + 2u];
32
+ var tu = base[b5 + 3u];
33
+ var tv = base[b5 + 4u];
32
34
 
33
35
  let s = rowStart[v];
34
36
  let e = rowStart[v + 1u];
35
37
  for (var i = s; i < e; i = i + 1u) {
36
38
  let w = weights[colMorph[i]];
37
39
  if (w != 0.0) {
38
- let o3 = i * 3u;
39
- px = px + w * colOffset[o3];
40
- py = py + w * colOffset[o3 + 1u];
41
- pz = pz + w * colOffset[o3 + 2u];
40
+ let o5 = i * 5u;
41
+ px = px + w * colOffset[o5];
42
+ py = py + w * colOffset[o5 + 1u];
43
+ pz = pz + w * colOffset[o5 + 2u];
44
+ tu = tu + w * colOffset[o5 + 3u];
45
+ tv = tv + w * colOffset[o5 + 4u];
42
46
  }
43
47
  }
44
48
 
45
- let vo = v * 8u; // VERTEX_STRIDE — write only the position slots
49
+ let vo = v * 8u; // VERTEX_STRIDE — the position and UV slots; the normal is left alone
46
50
  vtx[vo] = px;
47
51
  vtx[vo + 1u] = py;
48
52
  vtx[vo + 2u] = pz;
53
+ vtx[vo + 6u] = tu;
54
+ vtx[vo + 7u] = tv;
49
55
  }
50
56
  `