reze-engine 0.69.1 → 0.69.2

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.
@@ -61,6 +61,21 @@ const LINE = DIRECTIVE_LINE;
61
61
  * thing to write, so it has to be the accepted thing to write.
62
62
  */
63
63
  export const DIRECTIVE_NOTE = /(?:^|\s)(?:—|--|\/\/|#)\s/;
64
+ /** The note after the arguments, or "" — see DIRECTIVE_NOTE. */
65
+ function noteOf(rest) {
66
+ const m = DIRECTIVE_NOTE.exec(rest);
67
+ return m ? rest.slice(m.index + m[0].length).trim() : "";
68
+ }
69
+ /** A dial's labels from its note, when the note is written `English | 中文`. */
70
+ function labelsOf(rest) {
71
+ const note = noteOf(rest);
72
+ const bar = note.indexOf("|");
73
+ if (bar < 0)
74
+ return {};
75
+ const label = note.slice(0, bar).trim();
76
+ const labelZh = note.slice(bar + 1).trim();
77
+ return { ...(label ? { label } : {}), ...(labelZh ? { labelZh } : {}) };
78
+ }
64
79
  function argsOf(rest) {
65
80
  // `^` as well as `\s`: the tag's own trailing space is eaten by LINE, so a
66
81
  // note can begin at the very first character — which is what
@@ -166,12 +181,47 @@ export function parseDirectives(wgsl) {
166
181
  errors.push(`${at}: #param needs a WGSL identifier for a name`);
167
182
  return;
168
183
  }
184
+ const labels = labelsOf(m[2]);
185
+ if (kind === "bool") {
186
+ const v = rest[0]?.toLowerCase();
187
+ const on = v === "true" || v === "1" || v === "on";
188
+ const off = v === "false" || v === "0" || v === "off";
189
+ if (rest.length !== 1 || (!on && !off)) {
190
+ errors.push(`${at}: #param bool ${name} needs a default, true or false`);
191
+ return;
192
+ }
193
+ d.params.push({ name, kind: "bool", value: on ? 1 : 0, ...labels });
194
+ return;
195
+ }
196
+ if (kind === "enum") {
197
+ // Each choice is `id` or `id|中文`; a leading `*` marks the default,
198
+ // else the first is.
199
+ const options = [];
200
+ let value = 0;
201
+ for (const raw of rest) {
202
+ const star = raw.startsWith("*");
203
+ const [id, zh] = (star ? raw.slice(1) : raw).split("|");
204
+ if (!id) {
205
+ errors.push(`${at}: #param enum ${name} has an empty choice`);
206
+ return;
207
+ }
208
+ if (star)
209
+ value = options.length;
210
+ options.push({ id, ...(zh ? { labelZh: zh } : {}) });
211
+ }
212
+ if (options.length < 2) {
213
+ errors.push(`${at}: #param enum ${name} needs at least two choices, like "rain|数字雨 dots|点阵"`);
214
+ return;
215
+ }
216
+ d.params.push({ name, kind: "enum", value, options, ...labels });
217
+ return;
218
+ }
169
219
  if (kind === "color") {
170
220
  if (!/^#[0-9a-fA-F]{6}$/.test(rest[0] ?? "")) {
171
221
  errors.push(`${at}: #param color ${name} needs a default like #3b82f6`);
172
222
  return;
173
223
  }
174
- d.params.push({ name, kind: "color", value: rest[0] });
224
+ d.params.push({ name, kind: "color", value: rest[0], ...labels });
175
225
  return;
176
226
  }
177
227
  if (kind === "vec3") {
@@ -180,7 +230,7 @@ export function parseDirectives(wgsl) {
180
230
  errors.push(`${at}: #param vec3 ${name} needs three numbers`);
181
231
  return;
182
232
  }
183
- d.params.push({ name, kind: "vec3", value: v });
233
+ d.params.push({ name, kind: "vec3", value: v, ...labels });
184
234
  return;
185
235
  }
186
236
  if (kind === "float") {
@@ -197,10 +247,10 @@ export function parseDirectives(wgsl) {
197
247
  errors.push(`${at}: #param float ${name} takes both a min and a max, or neither`);
198
248
  return;
199
249
  }
200
- d.params.push({ name, kind: "float", value: v, ...(lo !== null && hi !== null ? { min: lo, max: hi } : {}) });
250
+ d.params.push({ name, kind: "float", value: v, ...(lo !== null && hi !== null ? { min: lo, max: hi } : {}), ...labels });
201
251
  return;
202
252
  }
203
- errors.push(`${at}: #param kind must be float, color or vec3`);
253
+ errors.push(`${at}: #param kind must be float, color, vec3, bool or enum`);
204
254
  return;
205
255
  }
206
256
  case "halfres":
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "reze-engine",
3
- "version": "0.69.1",
3
+ "version": "0.69.2",
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
@@ -1049,6 +1049,17 @@ interface ModelInstance {
1049
1049
  * writes ONE float into each of them and needs no other reason to hold a
1050
1050
  * block: the whole 16-float copy exists only for materials that morph. */
1051
1051
  materialUniformBuffers: GPUBuffer[]
1052
+ /** The material each of `materialUniformBuffers` belongs to, by index. Names,
1053
+ * not PMX indices: a material with no faces draws nothing and has no buffer,
1054
+ * so the two lists do not line up with the PMX's own. */
1055
+ materialNames: string[]
1056
+ /** How much of each named material is still there, as setMaterialDissolve
1057
+ * last asked. Absent = whole. A material wears the lower of this and the
1058
+ * model's own `dissolve`. */
1059
+ materialDissolve: Map<string, number>
1060
+ /** What each material buffer was last written, so a frame that changes
1061
+ * nothing writes nothing. */
1062
+ dissolveWritten: number[]
1052
1063
  /** The outline hulls' own uniforms, for the materials that have them.
1053
1064
  *
1054
1065
  * A SEPARATE LIST because they are a separate buffer: the hull pass binds 32
@@ -5662,7 +5673,7 @@ export class Engine {
5662
5673
  // KIND comes from the declaration, never from the runtime type of the
5663
5674
  // value: a caller handing a number to a colour must not silently turn a
5664
5675
  // vec3f field into an f32 and shift every field after it.
5665
- const isVec = p.kind !== "float"
5676
+ const isVec = p.kind === "color" || p.kind === "vec3"
5666
5677
  const align = isVec ? 16 : 4
5667
5678
  const offset = Math.ceil(cursor / align) * align
5668
5679
  layout.set(p.name, { offset: offset / 4, comps: isVec ? 3 : 1 })
@@ -15629,6 +15640,9 @@ export class Engine {
15629
15640
  objectId: (this.lastObjectId = this.lastObjectId >= GROUND_OBJECT_ID - 1 ? 1 : this.lastObjectId + 1),
15630
15641
  dissolve: 1,
15631
15642
  materialUniformBuffers: [],
15643
+ materialNames: [],
15644
+ materialDissolve: new Map(),
15645
+ dissolveWritten: [],
15632
15646
  outlineUniformBuffers: [],
15633
15647
  outlineByMaterial: new Map(),
15634
15648
  cullModelIndex: 0,
@@ -16243,6 +16257,8 @@ export class Engine {
16243
16257
  )
16244
16258
  inst.gpuBuffers.push(materialUniformBuffer)
16245
16259
  inst.materialUniformBuffers.push(materialUniformBuffer)
16260
+ inst.materialNames.push(mat.name)
16261
+ inst.dissolveWritten.push(1)
16246
16262
  if (morphedMaterials.has(pmxMaterialIndex)) {
16247
16263
  const base = this.materialUniformData(mat, sphereMode, headBoneIndex, materialId, modelId)
16248
16264
  morphTargets.push({
@@ -18540,31 +18556,75 @@ export class Engine {
18540
18556
  const v = Math.min(1, Math.max(0, value))
18541
18557
  if (inst.dissolve === v) return true
18542
18558
  inst.dissolve = v
18543
- // Offset 60: the sixteenth float of MaterialUniforms. One four-byte write
18544
- // per material rather than the whole block — the block only exists as a
18545
- // copy for materials that morph.
18546
- const one = new Float32Array([v])
18547
- for (const buffer of inst.materialUniformBuffers) {
18548
- this.device.queue.writeBuffer(buffer, 60, one)
18559
+ this.writeDissolve(inst)
18560
+ return true
18561
+ }
18562
+
18563
+ /**
18564
+ * Take some of one model's MATERIALS apart: the stockings, the jacket, a hat
18565
+ * — by material name, 1 whole, 0 gone. The same dissolve the whole model
18566
+ * comes apart by (setModelDissolve), with the same burning front, honoured by
18567
+ * the colour pass, the depth prepass, the shadow map and the outline; it is
18568
+ * only narrowed to the materials named. Each replaces the last set: a name
18569
+ * left out is whole again, and null makes them all whole.
18570
+ *
18571
+ * A material wears the lower of this and the model's own dissolve, so a
18572
+ * model coming apart takes these with it.
18573
+ */
18574
+ setMaterialDissolve(modelName: string, values: Readonly<Record<string, number>> | null): boolean {
18575
+ const inst = this.modelInstances.get(modelName)
18576
+ if (!inst) return false
18577
+ const next = new Map<string, number>()
18578
+ if (values) {
18579
+ for (const [name, value] of Object.entries(values)) {
18580
+ const v = Math.min(1, Math.max(0, Number.isFinite(value) ? value : 1))
18581
+ if (v < 1) next.set(name, v)
18582
+ }
18549
18583
  }
18550
- // And the hulls, which bind their own 32 bytes of edge data rather than the
18551
- // material block — so this is a different buffer at a different offset, and
18552
- // missing it left a dissolved character standing in her own outline.
18553
- for (const buffer of inst.outlineUniformBuffers) {
18554
- this.device.queue.writeBuffer(buffer, RZ_OUTLINE_DISSOLVE_OFFSET, one)
18584
+ inst.materialDissolve = next
18585
+ this.writeDissolve(inst)
18586
+ return true
18587
+ }
18588
+
18589
+ /** What setMaterialDissolve last set for one material, 1 when whole. */
18590
+ getMaterialDissolve(modelName: string, materialName: string): number {
18591
+ return this.modelInstances.get(modelName)?.materialDissolve.get(materialName) ?? 1
18592
+ }
18593
+
18594
+ /**
18595
+ * Each material's dissolve into its buffers: the lower of the model's and its
18596
+ * own. Only buffers whose value moved are written.
18597
+ *
18598
+ * Offset 60 is the sixteenth float of MaterialUniforms, which the colour pass,
18599
+ * the depth prepass and the shadow map all bind. The hulls bind their own 32
18600
+ * bytes of edge data, so they are a different buffer at a different offset —
18601
+ * missing them left a dissolved character standing in her own outline.
18602
+ */
18603
+ private writeDissolve(inst: ModelInstance): void {
18604
+ const one = new Float32Array(1)
18605
+ let moved = false
18606
+ for (let i = 0; i < inst.materialNames.length; i++) {
18607
+ const name = inst.materialNames[i]
18608
+ const v = Math.min(inst.dissolve, inst.materialDissolve.get(name) ?? 1)
18609
+ if (inst.dissolveWritten[i] === v) continue
18610
+ inst.dissolveWritten[i] = v
18611
+ moved = true
18612
+ one[0] = v
18613
+ this.device.queue.writeBuffer(inst.materialUniformBuffers[i], 60, one)
18614
+ const hull = inst.outlineByMaterial.get(name)
18615
+ if (hull) this.device.queue.writeBuffer(hull.buffer, RZ_OUTLINE_DISSOLVE_OFFSET, one)
18555
18616
  }
18556
18617
  // The morph path rebuilds a material's block from its `base` copy and
18557
18618
  // uploads it whole, which would put the old value straight back. Patching
18558
18619
  // `base` is what keeps a face that is morphing while she dissolves from
18559
18620
  // coming back solid for those frames; `last` is cleared so the next
18560
18621
  // comparison genuinely re-uploads rather than deciding nothing moved.
18561
- if (inst.materialMorphTargets) {
18622
+ if (moved && inst.materialMorphTargets) {
18562
18623
  for (const t of inst.materialMorphTargets) {
18563
- t.base[15] = v
18624
+ t.base[15] = Math.min(inst.dissolve, inst.materialDissolve.get(t.materialName) ?? 1)
18564
18625
  t.last[15] = Number.NaN
18565
18626
  }
18566
18627
  }
18567
- return true
18568
18628
  }
18569
18629
 
18570
18630
  /**
package/src/index.ts CHANGED
@@ -110,6 +110,7 @@ export {
110
110
  DIRECTIVE_NOTE,
111
111
  type EffectDirectives,
112
112
  type EffectParamDecl,
113
+ type EffectParamOption,
113
114
  } from "./shaders/directives"
114
115
  export {
115
116
  Model,
@@ -52,15 +52,34 @@
52
52
  */
53
53
  export type EffectAnchor = { bone: string; trail: boolean; along?: number; step?: number }
54
54
 
55
- /** A knob an effect exposes, for a host to build a control from. */
55
+ /** One choice of an `enum` dial: its id, and a Chinese label after a `|`. */
56
+ export type EffectParamOption = { id: string; labelZh?: string }
57
+
58
+ /**
59
+ * A knob an effect exposes, for a host to build a control from.
60
+ *
61
+ * `bool` and `enum` are one f32 in the shader like `float`: a switch reads 0 or
62
+ * 1, a choice reads its option's index. They differ only in the control a host
63
+ * draws — a switch, a list — which is the whole reason to declare them: a
64
+ * slider from 0 to 3 that means "rain, dots, circuit, grid" is a dial nobody
65
+ * can read.
66
+ *
67
+ * `label`/`labelZh` come from a note written `— English | 中文`. A note without
68
+ * the `|` is only a note, as it always was.
69
+ */
56
70
  export type EffectParamDecl = {
57
71
  name: string
58
- kind: "float" | "color" | "vec3"
59
- /** Numbers for float/vec3; `#rrggbb` for a colour, which the host converts. */
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. */
60
75
  value: number | [number, number, number] | string
61
76
  /** float only, and only when the author gave a range. */
62
77
  min?: number
63
78
  max?: number
79
+ /** enum only: the choices, in index order. */
80
+ options?: EffectParamOption[]
81
+ label?: string
82
+ labelZh?: string
64
83
  }
65
84
 
66
85
  export type EffectDirectives = {
@@ -196,6 +215,22 @@ const LINE = DIRECTIVE_LINE
196
215
  */
197
216
  export const DIRECTIVE_NOTE = /(?:^|\s)(?:—|--|\/\/|#)\s/
198
217
 
218
+ /** The note after the arguments, or "" — see DIRECTIVE_NOTE. */
219
+ function noteOf(rest: string): string {
220
+ const m = DIRECTIVE_NOTE.exec(rest)
221
+ return m ? rest.slice(m.index + m[0].length).trim() : ""
222
+ }
223
+
224
+ /** A dial's labels from its note, when the note is written `English | 中文`. */
225
+ function labelsOf(rest: string): { label?: string; labelZh?: string } {
226
+ const note = noteOf(rest)
227
+ const bar = note.indexOf("|")
228
+ if (bar < 0) return {}
229
+ const label = note.slice(0, bar).trim()
230
+ const labelZh = note.slice(bar + 1).trim()
231
+ return { ...(label ? { label } : {}), ...(labelZh ? { labelZh } : {}) }
232
+ }
233
+
199
234
  function argsOf(rest: string): string[] {
200
235
  // `^` as well as `\s`: the tag's own trailing space is eaten by LINE, so a
201
236
  // note can begin at the very first character — which is what
@@ -304,12 +339,46 @@ export function parseDirectives(wgsl: string): DirectiveResult {
304
339
  errors.push(`${at}: #param needs a WGSL identifier for a name`)
305
340
  return
306
341
  }
342
+ const labels = labelsOf(m[2])
343
+ if (kind === "bool") {
344
+ const v = rest[0]?.toLowerCase()
345
+ const on = v === "true" || v === "1" || v === "on"
346
+ const off = v === "false" || v === "0" || v === "off"
347
+ if (rest.length !== 1 || (!on && !off)) {
348
+ errors.push(`${at}: #param bool ${name} needs a default, true or false`)
349
+ return
350
+ }
351
+ d.params.push({ name, kind: "bool", value: on ? 1 : 0, ...labels })
352
+ return
353
+ }
354
+ if (kind === "enum") {
355
+ // Each choice is `id` or `id|中文`; a leading `*` marks the default,
356
+ // else the first is.
357
+ const options: EffectParamOption[] = []
358
+ let value = 0
359
+ for (const raw of rest) {
360
+ const star = raw.startsWith("*")
361
+ const [id, zh] = (star ? raw.slice(1) : raw).split("|")
362
+ if (!id) {
363
+ errors.push(`${at}: #param enum ${name} has an empty choice`)
364
+ return
365
+ }
366
+ if (star) value = options.length
367
+ options.push({ id, ...(zh ? { labelZh: zh } : {}) })
368
+ }
369
+ if (options.length < 2) {
370
+ errors.push(`${at}: #param enum ${name} needs at least two choices, like "rain|数字雨 dots|点阵"`)
371
+ return
372
+ }
373
+ d.params.push({ name, kind: "enum", value, options, ...labels })
374
+ return
375
+ }
307
376
  if (kind === "color") {
308
377
  if (!/^#[0-9a-fA-F]{6}$/.test(rest[0] ?? "")) {
309
378
  errors.push(`${at}: #param color ${name} needs a default like #3b82f6`)
310
379
  return
311
380
  }
312
- d.params.push({ name, kind: "color", value: rest[0] })
381
+ d.params.push({ name, kind: "color", value: rest[0], ...labels })
313
382
  return
314
383
  }
315
384
  if (kind === "vec3") {
@@ -318,7 +387,7 @@ export function parseDirectives(wgsl: string): DirectiveResult {
318
387
  errors.push(`${at}: #param vec3 ${name} needs three numbers`)
319
388
  return
320
389
  }
321
- d.params.push({ name, kind: "vec3", value: v as [number, number, number] })
390
+ d.params.push({ name, kind: "vec3", value: v as [number, number, number], ...labels })
322
391
  return
323
392
  }
324
393
  if (kind === "float") {
@@ -335,10 +404,10 @@ export function parseDirectives(wgsl: string): DirectiveResult {
335
404
  errors.push(`${at}: #param float ${name} takes both a min and a max, or neither`)
336
405
  return
337
406
  }
338
- d.params.push({ name, kind: "float", value: v, ...(lo !== null && hi !== null ? { min: lo, max: hi } : {}) })
407
+ d.params.push({ name, kind: "float", value: v, ...(lo !== null && hi !== null ? { min: lo, max: hi } : {}), ...labels })
339
408
  return
340
409
  }
341
- errors.push(`${at}: #param kind must be float, color or vec3`)
410
+ errors.push(`${at}: #param kind must be float, color, vec3, bool or enum`)
342
411
  return
343
412
  }
344
413
  case "halfres":