reze-engine 0.69.4 → 0.69.6

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/dist/engine.js CHANGED
@@ -34,6 +34,7 @@ import { MIRROR_DOWNSAMPLE_WGSL, MIRROR_MASK_DOWNSAMPLE_WGSL, MIRROR_MAT_BYTES,
34
34
  import { packHalf } from "./hdr";
35
35
  import { evalIrradianceSH, projectIrradianceSH, gradientIrradianceSH } from "./ibl";
36
36
  import { LYRIC_ATLAS_MAX_H, LYRIC_ATLAS_MAX_W, LYRICS_FLOATS, packLyrics } from "./shaders/lyrics-api";
37
+ import { effectTextFont, effectTextWgsl, rasterEffectText, EFFECT_TEXT_PX } from "./shaders/text-api";
37
38
  import { sceneTargets as sceneTargetsFor, sceneColorFormats, setMrtIds, mrtIdsEnabled, SCENE_ID_FORMAT, } from "./shaders/passes/scene-contract";
38
39
  import { LIGHT_GRID_BASE, LIGHT_HEADER, LIGHT_STRIDE, LIGHTS_FLOATS, MAX_LIGHTS, buildLightEmitShader, hasLightEmit, } from "./shaders/lights";
39
40
  import { buildLightGrid } from "./light-grid";
@@ -683,6 +684,13 @@ const TEXTURE_EXTENSION_GUESSES = [".png", ".jpg", ".jpeg", ".bmp", ".tga", ".dd
683
684
  function paramValue(p, given) {
684
685
  if (p.kind === "bone")
685
686
  return isBoneValue(given) ? given : { bone: typeof p.value === "string" ? p.value : "", model: null };
687
+ if (p.kind === "text")
688
+ return typeof given === "string" ? given : typeof p.value === "string" ? p.value : "";
689
+ // A lag is filled from the dial it trails, every frame.
690
+ if (p.kind === "lag")
691
+ return 0;
692
+ if (typeof given === "string")
693
+ given = undefined;
686
694
  if (given !== undefined)
687
695
  return p.kind === "int" && typeof given === "number" ? Math.round(given) : given;
688
696
  if (p.kind === "color" && typeof p.value === "string") {
@@ -716,6 +724,8 @@ function castSubjectKey(subjects) {
716
724
  return subjects ? [...subjects].sort().join("\u0000") : "*";
717
725
  }
718
726
  const EFFECT_PARAMS_BINDING = 7;
727
+ /** The field module's binding for an effect's words — see text-api. */
728
+ const EFFECT_TEXT_BINDING = 33;
719
729
  const EFFECT_PARAMS_BINDING_GRID = 9;
720
730
  /**
721
731
  * How one field draw lands on the ones before it: OVER, into a premultiplied
@@ -3625,6 +3635,7 @@ export class Engine {
3625
3635
  { binding: 19, resource: { buffer: this.midiBuffer } },
3626
3636
  { binding: 24, resource: { buffer: this.lyricsBuffer } },
3627
3637
  { binding: 25, resource: this.lyricsTextureView },
3638
+ { binding: EFFECT_TEXT_BINDING, resource: owner.text?.view ?? this.trailFallbackView },
3628
3639
  // The size uniform for the pair THIS effect draws into.
3629
3640
  { binding: 14, resource: { buffer: this.fieldUniformBuffers[owner.fieldLayer] } },
3630
3641
  { binding: 22, resource: { buffer: owner.fieldClock ?? this.fieldUniformBuffers[owner.fieldLayer] } },
@@ -4041,7 +4052,7 @@ export class Engine {
4041
4052
  // reflection re-renders the scene from a folded camera, which is a pass and
4042
4053
  // not a shader, so the effect declares it and draws nothing itself.
4043
4054
  // #stepped too: it decides when a pose reaches the screen, not what is drawn.
4044
- if (!hasBackground && !hasForeground && !wantsParticles && !wantsTrails && !hasLightEmit(wgsl) && !d.mirror && !d.stepped) {
4055
+ if (!hasBackground && !hasForeground && !wantsParticles && !wantsTrails && !hasLightEmit(wgsl) && !d.mirror && !d.stepped && !d.xray) {
4045
4056
  return { ok: false, diagnostics: [
4046
4057
  "an effect must define fn background(ray: vec3f, uv: vec2f, time: f32) -> vec4f, " +
4047
4058
  "fn foreground(ray: vec3f, uv: vec2f, time: f32, depth: f32) -> vec4f, " +
@@ -4118,6 +4129,15 @@ export class Engine {
4118
4129
  }
4119
4130
  continue;
4120
4131
  }
4132
+ if (p.kind === "text") {
4133
+ // The rect of the string it shows in the effect's picture: u0, vTop,
4134
+ // u1, vBottom — read through rzText_NAME().
4135
+ const offset = Math.ceil(cursor / 16) * 16;
4136
+ layout.set(p.name, { offset: offset / 4, comps: 4 });
4137
+ fields.push(` ${p.name}: vec4f,`);
4138
+ cursor = offset + 16;
4139
+ continue;
4140
+ }
4121
4141
  const isVec = p.kind === "color" || p.kind === "vec3" || p.kind === "point";
4122
4142
  const align = isVec ? 16 : 4;
4123
4143
  const offset = Math.ceil(cursor / align) * align;
@@ -4135,6 +4155,9 @@ export class Engine {
4135
4155
  paramBindings.set(name, value);
4136
4156
  continue;
4137
4157
  }
4158
+ // Words get their rect once the picture is set, after the mounts build.
4159
+ if (typeof value === "string")
4160
+ continue;
4138
4161
  if (typeof value === "number")
4139
4162
  paramsData[slot.offset] = value;
4140
4163
  else {
@@ -4162,10 +4185,12 @@ export class Engine {
4162
4185
  * one states the number it can spare. Empty when nothing is declared: WGSL
4163
4186
  * has no empty struct, and a binding nothing reads is a layout mismatch.
4164
4187
  */
4165
- const paramsWgsl = (binding) => entries.length
4166
- ? `struct EffectParams {\n${fields.join("\n")}\n}\n@group(0) @binding(${binding}) var<uniform> params: EffectParams;\n${boneWgsl}`
4188
+ const textNames = d.params.filter((p) => p.kind === "text").map((p) => p.name);
4189
+ const paramsWgsl = (binding, text = false) => entries.length
4190
+ ? `struct EffectParams {\n${fields.join("\n")}\n}\n@group(0) @binding(${binding}) var<uniform> params: EffectParams;\n${boneWgsl}` +
4191
+ effectTextWgsl(textNames, text, EFFECT_TEXT_BINDING, "_rzGridSamp")
4167
4192
  : "";
4168
- const paramsDecl = paramsWgsl(EFFECT_PARAMS_BINDING);
4193
+ const paramsDecl = paramsWgsl(EFFECT_PARAMS_BINDING, true);
4169
4194
  // ── Compile with validation captured, not thrown at the console. Line
4170
4195
  // numbers in diagnostics are rebased to the USER's source.
4171
4196
  // The composite is STATIC: user field code compiles in its own half-res
@@ -4367,10 +4392,21 @@ export class Engine {
4367
4392
  paramLayout: layout,
4368
4393
  paramsBuffer,
4369
4394
  paramsData,
4395
+ text: textNames.length
4396
+ ? {
4397
+ font: d.font,
4398
+ strings: new Set(entries.flatMap(([, v]) => (typeof v === "string" && v ? [v] : []))),
4399
+ rects: new Map(),
4400
+ texture: null,
4401
+ view: null,
4402
+ shown: new Map(entries.flatMap(([n, v]) => (typeof v === "string" ? [[n, v]] : []))),
4403
+ }
4404
+ : null,
4370
4405
  paramBindings,
4371
4406
  paramTracks: new Map(),
4372
4407
  hasMirror: d.mirror,
4373
4408
  stepped: d.stepped,
4409
+ xray: d.xray,
4374
4410
  hasBackground,
4375
4411
  hasForeground,
4376
4412
  // The author's OWN source, not the assembled module: the assembled one
@@ -4382,9 +4418,11 @@ export class Engine {
4382
4418
  // Every route to the cast, in one test, and the author's own source for
4383
4419
  // the reason readsIds gives. `#anchor` counts: a file declaring one asks
4384
4420
  // the engine to record a bone for it, whether or not the accessor is
4385
- // spelled out in a line this matches.
4421
+ // spelled out in a line this matches. `#xray` counts too: whom its
4422
+ // window cuts is whom it is aimed at.
4386
4423
  readsCast: /\brz(?:Subject|SubjectCount|SubjectId|Anchor|Trail|TrailCount|CastDistance)\s*\(/.test(wgsl) ||
4387
- anchors.length > 0,
4424
+ anchors.length > 0 ||
4425
+ d.xray !== null,
4388
4426
  // Aimed at nobody in particular — the whole cast, which is what every
4389
4427
  // effect did before it could be aimed. A host narrows it afterwards, the
4390
4428
  // way it sets influence and a schedule.
@@ -4406,6 +4444,7 @@ export class Engine {
4406
4444
  sim: { start: null, time: null },
4407
4445
  simStep: 0,
4408
4446
  simReset: false,
4447
+ seededCast: -1,
4409
4448
  dissolve: d.dissolve ? dissolveConstants(authored) : null,
4410
4449
  ground: d.ground ? { color: new Vec3(...d.ground.color), noise: d.ground.noise } : null,
4411
4450
  // Its OWN resolution, no longer the scene's: an effect that never asked
@@ -4439,6 +4478,9 @@ export class Engine {
4439
4478
  trails,
4440
4479
  lights,
4441
4480
  };
4481
+ // Before the swap binds it, so the field's first frame already has words.
4482
+ if (instance.text)
4483
+ this.layoutEffectText(instance);
4442
4484
  return { ok: true, instance, warnings };
4443
4485
  }
4444
4486
  /**
@@ -4481,6 +4523,7 @@ export class Engine {
4481
4523
  const [identity, gamma] = await this.compositePipelines(false, false);
4482
4524
  for (const e of this.effects) {
4483
4525
  e.paramsBuffer?.destroy();
4526
+ e.text?.texture?.destroy();
4484
4527
  e.lights?.uniform.destroy();
4485
4528
  e.fieldClock?.destroy();
4486
4529
  }
@@ -4546,6 +4589,7 @@ export class Engine {
4546
4589
  // that was running is only torn down now.
4547
4590
  for (const e of this.effects) {
4548
4591
  e.paramsBuffer?.destroy();
4592
+ e.text?.texture?.destroy();
4549
4593
  e.lights?.uniform.destroy();
4550
4594
  e.fieldClock?.destroy();
4551
4595
  }
@@ -5167,14 +5211,22 @@ export class Engine {
5167
5211
  count.dispatchWorkgroups(1);
5168
5212
  count.end();
5169
5213
  }
5170
- const cp = encoder.beginComputePass({ label: "particles" });
5171
- cp.setPipeline(p.compute);
5172
- cp.setBindGroup(0, bind);
5173
- if (p.live)
5174
- cp.dispatchWorkgroupsIndirect(p.live.indirect, 0);
5175
- else
5176
- cp.dispatchWorkgroups(Math.ceil(p.count / 64));
5177
- cp.end();
5214
+ // A reset runs the pool TWICE: once to spawn every slot, once to step
5215
+ // what spawned, at dt 0. A dispatch either inits a slot or steps it, and
5216
+ // an effect that poses in its step (Pochita's bones, which its triangles
5217
+ // are drawn from) has no pose after the init alone. Playing, the next
5218
+ // frame's step supplied it; paused at a clip's start no next frame
5219
+ // steps, and the pet stayed collapsed — invisible — until play.
5220
+ for (let pass = e.simReset ? 2 : 1; pass > 0; pass--) {
5221
+ const cp = encoder.beginComputePass({ label: "particles" });
5222
+ cp.setPipeline(p.compute);
5223
+ cp.setBindGroup(0, bind);
5224
+ if (p.live)
5225
+ cp.dispatchWorkgroupsIndirect(p.live.indirect, 0);
5226
+ else
5227
+ cp.dispatchWorkgroups(Math.ceil(p.count / 64));
5228
+ cp.end();
5229
+ }
5178
5230
  }
5179
5231
  }
5180
5232
  /**
@@ -5949,7 +6001,12 @@ export class Engine {
5949
6001
  fx.paramBindings.set(name, value);
5950
6002
  return false;
5951
6003
  }
5952
- if (isBoneValue(value))
6004
+ if (decl.kind === "text")
6005
+ return typeof value === "string" ? this.showEffectText(fx, name, value) : false;
6006
+ // A lag is its source's, trailing; nothing writes it by hand.
6007
+ if (decl.kind === "lag")
6008
+ return false;
6009
+ if (isBoneValue(value) || typeof value === "string")
5953
6010
  return false;
5954
6011
  if (typeof value === "number") {
5955
6012
  fx.paramsData[slot.offset] = decl.kind === "int" ? Math.round(value) : value;
@@ -5984,8 +6041,11 @@ export class Engine {
5984
6041
  fx.paramTracks.delete(name);
5985
6042
  return true;
5986
6043
  }
5987
- const holds = decl.kind === "bool" || decl.kind === "enum" || decl.kind === "bone";
6044
+ if (decl.kind === "lag")
6045
+ return false;
6046
+ const holds = decl.kind === "bool" || decl.kind === "enum" || decl.kind === "bone" || decl.kind === "text";
5988
6047
  const bones = [];
6048
+ const texts = [];
5989
6049
  const sorted = [...keys].sort((a, b) => a.time - b.time);
5990
6050
  const track = [];
5991
6051
  for (const k of sorted) {
@@ -5995,6 +6055,13 @@ export class Engine {
5995
6055
  continue;
5996
6056
  v = bones.push(k.value) - 1;
5997
6057
  }
6058
+ else if (decl.kind === "text") {
6059
+ if (typeof k.value !== "string")
6060
+ continue;
6061
+ v = texts.push(k.value) - 1;
6062
+ }
6063
+ else if (typeof k.value === "string")
6064
+ continue;
5998
6065
  else if (typeof k.value === "number")
5999
6066
  v = k.value;
6000
6067
  else if (isBoneValue(k.value))
@@ -6008,7 +6075,11 @@ export class Engine {
6008
6075
  ...(!holds && k.curve && k.ease !== "hold" ? { b: k.curve } : {}),
6009
6076
  });
6010
6077
  }
6011
- fx.paramTracks.set(name, { keys: track, bones, last: null });
6078
+ fx.paramTracks.set(name, { keys: track, bones, texts, last: null });
6079
+ // Every word a key can show goes into the picture now, so playing past a
6080
+ // key is a rect write and never a redraw.
6081
+ if (fx.text && texts.some((t) => t && !fx.text.strings.has(t)))
6082
+ this.layoutEffectText(fx);
6012
6083
  this.evaluateEffectParams();
6013
6084
  return true;
6014
6085
  }
@@ -6036,6 +6107,11 @@ export class Engine {
6036
6107
  if (bone)
6037
6108
  fx.paramBindings.set(name, bone);
6038
6109
  }
6110
+ else if (decl?.kind === "text") {
6111
+ const words = typeof v === "number" ? track.texts[v] : undefined;
6112
+ if (words !== undefined)
6113
+ dirty = this.showEffectText(fx, name, words) || dirty;
6114
+ }
6039
6115
  else if (typeof v === "number") {
6040
6116
  dirty = this.writeEffectParam(fx, name, v) || dirty;
6041
6117
  }
@@ -6047,9 +6123,212 @@ export class Engine {
6047
6123
  }
6048
6124
  if (fx.paramBindings.size)
6049
6125
  dirty = this.resolveParamBindings(fx) || dirty;
6126
+ for (const p of fx.paramDecls) {
6127
+ if (p.kind !== "lag")
6128
+ continue;
6129
+ transport ?? (transport = this.transportTime());
6130
+ dirty = this.evaluateLag(fx, p, transport) || dirty;
6131
+ }
6050
6132
  if (dirty)
6051
6133
  this.device.queue.writeBuffer(fx.paramsBuffer, 0, fx.paramsData);
6052
6134
  }
6135
+ this.applyXray();
6136
+ }
6137
+ /**
6138
+ * A `lag` dial at time t: its source, trailing — what a game's health bar
6139
+ * draws as the chunk just lost. After the source falls it HOLDS where it was
6140
+ * for `hold` seconds, then drains at `rate` a second until it meets the
6141
+ * source again; a rise is met at once.
6142
+ *
6143
+ * Sampled from the source's KEYS, never from what the frames before this one
6144
+ * happened to see, so a scrub, a loop and a frame-by-frame export all draw
6145
+ * the same chunk. A source with no keys has no past: the lag is the source.
6146
+ */
6147
+ evaluateLag(fx, p, t) {
6148
+ const slot = fx.paramLayout.get(p.name);
6149
+ const src = p.source ? fx.paramLayout.get(p.source) : undefined;
6150
+ if (!slot || !src)
6151
+ return false;
6152
+ const d = fx.paramsData;
6153
+ let v = d[src.offset];
6154
+ const track = p.source ? fx.paramTracks.get(p.source) : undefined;
6155
+ if (track && track.keys.length > 0) {
6156
+ const hold = p.hold ?? 0;
6157
+ const rate = Math.max(p.rate ?? 1, 1e-6);
6158
+ const decl = fx.paramDecls.find((q) => q.name === p.source);
6159
+ const range = decl?.min !== undefined && decl.max !== undefined ? Math.abs(decl.max - decl.min) : 1;
6160
+ // Far enough back that anything older has drained past the full range.
6161
+ const span = hold + range / rate;
6162
+ const at = (s) => {
6163
+ const sv = sampleParamTrack(track.keys, s);
6164
+ if (typeof sv !== "number")
6165
+ return;
6166
+ v = Math.max(v, sv - rate * Math.max(0, t - s - hold));
6167
+ };
6168
+ // A fine grid, plus every key in the window exactly — a hit is a key,
6169
+ // and the instant before it is the value the chunk holds.
6170
+ const step = 1 / 120;
6171
+ for (let s = t; s >= t - span; s -= step)
6172
+ at(s);
6173
+ for (const k of track.keys) {
6174
+ if (k.t > t || k.t < t - span)
6175
+ continue;
6176
+ at(k.t);
6177
+ at(k.t - 1e-4);
6178
+ }
6179
+ }
6180
+ if (d[slot.offset] === v)
6181
+ return false;
6182
+ d[slot.offset] = v;
6183
+ return true;
6184
+ }
6185
+ /** Show `words` on a text dial: its rect into the block. A string the
6186
+ * picture has never held is set into it first, which redraws the picture
6187
+ * and writes the block itself. Returns whether the caller still has to. */
6188
+ showEffectText(fx, name, words) {
6189
+ const t = fx.text;
6190
+ if (!t)
6191
+ return false;
6192
+ t.shown.set(name, words);
6193
+ if (words && !t.strings.has(words)) {
6194
+ this.layoutEffectText(fx);
6195
+ return false;
6196
+ }
6197
+ return this.writeTextRect(fx, name, words);
6198
+ }
6199
+ writeTextRect(fx, name, words) {
6200
+ const slot = fx.paramLayout.get(name);
6201
+ if (!slot || !fx.text)
6202
+ return false;
6203
+ const r = fx.text.rects.get(words) ?? [0, 0, 0, 0];
6204
+ const d = fx.paramsData;
6205
+ let moved = false;
6206
+ for (let k = 0; k < 4; k++) {
6207
+ if (d[slot.offset + k] === r[k])
6208
+ continue;
6209
+ d[slot.offset + k] = r[k];
6210
+ moved = true;
6211
+ }
6212
+ return moved;
6213
+ }
6214
+ /**
6215
+ * Set every string an effect's text dials can show into its picture, upload
6216
+ * it and point each dial at its row. Re-binds only when the picture changes
6217
+ * SIZE. Once the face has loaded, a second pass sets it again — a web font
6218
+ * the page has not fetched yet draws in the fallback the first time.
6219
+ */
6220
+ layoutEffectText(fx, fontLoaded = false) {
6221
+ const t = fx.text;
6222
+ if (!t || !this.device)
6223
+ return;
6224
+ // What the dials show now and what their keys can show — rebuilt rather
6225
+ // than added to, so typing a name does not keep every half-typed word.
6226
+ t.strings = new Set([...t.shown.values(), ...[...fx.paramTracks.values()].flatMap((k) => k.texts)].filter((w) => w.length > 0));
6227
+ const sheet = rasterEffectText([...t.strings], t.font);
6228
+ t.rects = sheet?.rects ?? new Map();
6229
+ const w = sheet?.width ?? 1;
6230
+ const h = sheet?.height ?? 1;
6231
+ if (!t.texture || t.texture.width !== w || t.texture.height !== h) {
6232
+ t.texture?.destroy();
6233
+ t.texture = this.device.createTexture({
6234
+ label: "effect text",
6235
+ size: [w, h],
6236
+ format: "r8unorm",
6237
+ usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT,
6238
+ });
6239
+ t.view = t.texture.createView();
6240
+ if (this.effects.includes(fx))
6241
+ this.rebuildFieldBindGroup();
6242
+ }
6243
+ // premultipliedAlpha, as the lyric atlas: white glyphs on a clear canvas
6244
+ // arrive with coverage in red, antialiased edges intact.
6245
+ if (sheet) {
6246
+ this.device.queue.copyExternalImageToTexture({ source: sheet.source }, { texture: t.texture, premultipliedAlpha: true }, [w, h]);
6247
+ }
6248
+ for (const [name, words] of t.shown)
6249
+ this.writeTextRect(fx, name, words);
6250
+ if (fx.paramsBuffer)
6251
+ this.device.queue.writeBuffer(fx.paramsBuffer, 0, fx.paramsData);
6252
+ const fonts = typeof document !== "undefined" ? document.fonts : undefined;
6253
+ if (!fontLoaded && fonts && t.font) {
6254
+ const css = effectTextFont(t.font, EFFECT_TEXT_PX);
6255
+ if (!fonts.check(css, [...t.strings].join(""))) {
6256
+ fonts
6257
+ .load(css, [...t.strings].join(""))
6258
+ .then(() => {
6259
+ if (this.effects.includes(fx))
6260
+ this.layoutEffectText(fx, true);
6261
+ })
6262
+ .catch(() => { });
6263
+ }
6264
+ }
6265
+ }
6266
+ /**
6267
+ * Each `#xray` effect's window, onto every character it is aimed at: a
6268
+ * rectangle on the frame from its X, Y, WIDTH and HEIGHT dials (fractions of
6269
+ * the frame; X and Y from its centre, Y up), in pixels at the size the scene
6270
+ * draws at — so it sits in
6271
+ * the same place in a preview and a 4K export. It is whole while the effect
6272
+ * plays at all — the effect draws its own border and cannot read its fade,
6273
+ * so a window that shrank with it would part from the border it sits in. A
6274
+ * model no window is on is written closed.
6275
+ */
6276
+ applyXray() {
6277
+ const W = this.canvas.width;
6278
+ const H = this.canvas.height;
6279
+ const rects = new Map();
6280
+ for (const fx of this.effects) {
6281
+ const x = fx.xray;
6282
+ if (!x || fx.weight < 0.001)
6283
+ continue;
6284
+ const d = fx.paramsData;
6285
+ const read = (name, fallback) => {
6286
+ const s = fx.paramLayout.get(name);
6287
+ return s ? d[s.offset] : fallback;
6288
+ };
6289
+ // The frame's pixels run y down; the dial runs up from the centre.
6290
+ const cx = (0.5 + read(x.x, 0)) * W;
6291
+ const cy = (0.5 - read(x.y, 0)) * H;
6292
+ const hw = Math.abs(read(x.width, 0.25)) * 0.5 * W;
6293
+ const hh = Math.abs(read(x.height, 0.25)) * 0.5 * H;
6294
+ const rect = new Float32Array(16);
6295
+ rect[0] = cx - hw;
6296
+ rect[1] = cy - hh;
6297
+ rect[2] = cx + hw;
6298
+ rect[3] = cy + hh;
6299
+ rect[12] = 1;
6300
+ for (const name of this.castSubjectNames(fx.subjects))
6301
+ rects.set(name, rect);
6302
+ }
6303
+ for (const [name, inst] of this.modelInstances) {
6304
+ const rect = rects.get(name);
6305
+ const key = rect ? rect.join(",") : "";
6306
+ if (key === inst.xrayKey)
6307
+ continue;
6308
+ inst.xrayKey = key;
6309
+ inst.objectLight.set(rect ?? new Float32Array(16), 100);
6310
+ this.device.queue.writeBuffer(inst.lightBuffer, 100 * 4, inst.objectLight, 100, 16);
6311
+ }
6312
+ }
6313
+ /**
6314
+ * Which of a model's materials a see-through window (#xray) cuts — her
6315
+ * clothes, by PMX material name. Everything else (skin, face, hair, eyes) is
6316
+ * drawn behind the window as anywhere else. Null or empty: none.
6317
+ */
6318
+ setXrayMaterials(modelName, names) {
6319
+ const inst = this.modelInstances.get(modelName);
6320
+ if (!inst)
6321
+ return false;
6322
+ const mask = new Uint32Array(inst.objectLight.buffer, 116 * 4, 64);
6323
+ mask.fill(0);
6324
+ for (const n of names ?? []) {
6325
+ const id = inst.materialIdByName.get(n);
6326
+ if (id === undefined || id >= 512)
6327
+ continue;
6328
+ mask[id >> 5] |= 1 << (id & 31);
6329
+ }
6330
+ this.device.queue.writeBuffer(inst.lightBuffer, 116 * 4, inst.objectLight, 116, 64);
6331
+ return true;
6053
6332
  }
6054
6333
  /** Bones and followed points into the block, where they stand this frame. */
6055
6334
  resolveParamBindings(fx) {
@@ -6258,6 +6537,17 @@ export class Engine {
6258
6537
  fx.sim = sim.clock;
6259
6538
  fx.simStep = sim.step;
6260
6539
  fx.simReset = sim.reset;
6540
+ // A pool held still is seeded ONCE, against whoever was in the cast on
6541
+ // that frame. A scene opened paused at its first frame seeds before her
6542
+ // model has loaded, so an effect that spawns off the cast (chicks round
6543
+ // her feet) is born empty — and a held pool is never stepped again to
6544
+ // ask. So the seed is taken again when the cast it read has changed. The
6545
+ // cast was written earlier this frame (updateCameraUniforms).
6546
+ const cast = fx.subjectMask * 16 + fx.subjectCount;
6547
+ if (sim.clock.start !== null && !fx.simReset && fx.simStep === 0 && cast !== fx.seededCast)
6548
+ fx.simReset = true;
6549
+ if (fx.simReset || fx.simStep > 0)
6550
+ fx.seededCast = cast;
6261
6551
  // Its own clock, expressed the way the mounts read it. Every mount
6262
6552
  // derives time from the epoch against sceneClock, so this one write moves
6263
6553
  // the field, the particles, the ribbons, lightEmit and the grid together
@@ -6836,6 +7126,8 @@ export class Engine {
6836
7126
  { binding: 24, visibility: GPUShaderStage.FRAGMENT, buffer: { type: "read-only-storage" } },
6837
7127
  // The lyric line atlas, for rzLyricText.
6838
7128
  { binding: 25, visibility: GPUShaderStage.FRAGMENT, texture: { sampleType: "float", viewDimension: "2d" } },
7129
+ // This effect's own words, for rzText_NAME — a 1x1 when it has none.
7130
+ { binding: EFFECT_TEXT_BINDING, visibility: GPUShaderStage.FRAGMENT, texture: { sampleType: "float", viewDimension: "2d" } },
6839
7131
  { binding: 14, visibility: GPUShaderStage.FRAGMENT, buffer: { type: "uniform" } },
6840
7132
  // This effect's own clock — see the field shader's note on why it is
6841
7133
  // not viewU[6].x.
@@ -13530,7 +13822,9 @@ export class Engine {
13530
13822
  // layers [0..3] (y: a stage, which takes the cast's shadow —
13531
13823
  // rzRoofedCastShadow), ambient flag [4..7], SH [8..43], fill [44..47],
13532
13824
  // jelly eyes [48..95], bloom [96..99] — see materials/common.ts.
13533
- const objectLight = new Float32Array(100);
13825
+ // 100 for the lights, the jelly eyes and the bloom flag; 16 for the
13826
+ // see-through window and 64 for the materials it cuts (XRAY_WGSL).
13827
+ const objectLight = new Float32Array(180);
13534
13828
  objectLight[96] = 1;
13535
13829
  const objectLayers = new Uint32Array(objectLight.buffer);
13536
13830
  objectLayers[0] =
@@ -13616,6 +13910,8 @@ export class Engine {
13616
13910
  skinMatricesDirty: true,
13617
13911
  hiddenMaterials: new Set(),
13618
13912
  morphHiddenMaterials: new Set(),
13913
+ materialIdByName: new Map(),
13914
+ xrayKey: "",
13619
13915
  probeHiddenMaterials: new Set(),
13620
13916
  materialMorphTargets: null,
13621
13917
  materialMorphByIndex: null,
@@ -14091,6 +14387,7 @@ export class Engine {
14091
14387
  if (indexCount === 0)
14092
14388
  continue;
14093
14389
  materialId++;
14390
+ inst.materialIdByName.set(mat.name, materialId);
14094
14391
  let diffuseTexture = await loadTextureByIndex(mat.diffuseTextureIndex);
14095
14392
  if (!diffuseTexture) {
14096
14393
  // NAMING NO TEXTURE IS NOT A FAILURE. A material can legitimately have
@@ -14211,6 +14508,12 @@ export class Engine {
14211
14508
  0,
14212
14509
  0,
14213
14510
  0,
14511
+ // The material's draw index, for the see-through window's cut — the
14512
+ // colour pass's materialId — padded to the vector it sits in.
14513
+ materialId,
14514
+ 0,
14515
+ 0,
14516
+ 0,
14214
14517
  ]);
14215
14518
  const outlineUniformBuffer = this.createUniformBuffer(`${prefix}outline: ${mat.name}`, materialUniformData);
14216
14519
  inst.gpuBuffers.push(outlineUniformBuffer);
@@ -1 +1 @@
1
- {"version":3,"file":"slots.d.ts","sourceRoot":"","sources":["../../src/graph/slots.ts"],"names":[],"mappings":"AAYA,OAAO,KAAK,EAAE,SAAS,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;AAkQxE;;;;GAIG;AACH,wBAAgB,cAAc,CAC5B,WAAW,EAAE,WAAW,EACxB,SAAS,EAAE,SAAS,EACpB,MAAM,EAAE,MAAM,EACd,oBAAoB,EAAE,OAAO,EAC7B,UAAU,UAAQ,EAClB,KAAK,GAAE,UAAmB,EAC1B,UAAU,UAAO,GAChB,MAAM,CAgBR"}
1
+ {"version":3,"file":"slots.d.ts","sourceRoot":"","sources":["../../src/graph/slots.ts"],"names":[],"mappings":"AAYA,OAAO,KAAK,EAAE,SAAS,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;AAsQxE;;;;GAIG;AACH,wBAAgB,cAAc,CAC5B,WAAW,EAAE,WAAW,EACxB,SAAS,EAAE,SAAS,EACpB,MAAM,EAAE,MAAM,EACd,oBAAoB,EAAE,OAAO,EAC7B,UAAU,UAAQ,EAClB,KAAK,GAAE,UAAmB,EAC1B,UAAU,UAAO,GAChB,MAAM,CAgBR"}
@@ -94,7 +94,9 @@ function prelude(renderClass, alphaMode, blend) {
94
94
  // shade as authored.
95
95
  const flip = renderClass === "eye" ? "" : "\n n = select(-n, n, dot(n, v) >= 0.0);";
96
96
  return `@fragment fn fs(input: VertexOutput) -> FSOut {
97
- ${PLAIN_SAMPLE} // MMD alpha semantics: material alpha × texture alpha, capped at 1 as MMD
97
+ ${PLAIN_SAMPLE} // The see-through window (#xray): her clothes are not drawn inside it.
98
+ if (rz_xray_cut(input.position, material.materialId)) { discard; }
99
+ // MMD alpha semantics: material alpha × texture alpha, capped at 1 as MMD
98
100
  // does (a PMX can author alpha above 1; blended, it burns the material white).
99
101
  let alpha = saturate(material.alpha * tex_s.a);
100
102
  ${discard}
@@ -142,7 +144,9 @@ ${gate}
142
144
  // only by its own lamps — must not light it through the fallback.
143
145
  let rz_keyed = (light.dirLayers[rz_key] & objectLight.layers.x) != 0u;
144
146
  let sun = select(vec3f(0.0), light.lights[rz_key].color.xyz * light.lights[rz_key].color.w, rz_keyed);
145
- let shadow = sampleShadow(input.worldPos, n);
147
+ // Lit through the see-through window: what it reveals would otherwise wear
148
+ // the shadow of the clothes it cut, which reads as dirt rather than as cloth.
149
+ let shadow = select(sampleShadow(input.worldPos, n), 1.0, rz_xray_inside(input.position));
146
150
  // The ambient: this object's own where loaded data gave it one — the game
147
151
  // gives every renderer its own — else the world's flat colour or the HDRI's
148
152
  // irradiance at this normal.
@@ -45,11 +45,16 @@ export type EffectParamOption = {
45
45
  */
46
46
  export type EffectParamDecl = {
47
47
  name: string;
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. */
48
+ kind: "float" | "color" | "vec3" | "bool" | "enum" | "int" | "point" | "bone" | "text" | "lag";
49
+ /** Numbers for float/vec3/int/point/lag, 0 or 1 for bool, the option's index
50
+ * for enum, a bone's NAME for bone, the words for text; `#rrggbb` for a
51
+ * colour, which the host converts. */
52
52
  value: number | [number, number, number] | string;
53
+ /** lag only: the float dial it trails, how many seconds it holds after that
54
+ * dial falls, and how much it then drains per second. */
55
+ source?: string;
56
+ hold?: number;
57
+ rate?: number;
53
58
  /** float and int only, and only when the author gave a range. */
54
59
  min?: number;
55
60
  max?: number;
@@ -140,6 +145,29 @@ export type EffectDirectives = {
140
145
  * the engine's STEPPED_DIALS.
141
146
  */
142
147
  stepped: boolean;
148
+ /**
149
+ * `#xray X Y WIDTH HEIGHT` — a see-through window held in front of the
150
+ * lens, as X-ray glasses are: a rectangle on the frame, four float dials in
151
+ * fractions of the frame — X and Y its offset from the frame's centre (Y
152
+ * up), WIDTH and HEIGHT its size. Behind it, the materials the host marks on the characters it is aimed
153
+ * at (setXrayMaterials — their clothes) are not drawn, so what is under them
154
+ * shows. Whole while the effect plays at all. Null without.
155
+ */
156
+ xray: {
157
+ x: string;
158
+ y: string;
159
+ width: string;
160
+ height: string;
161
+ } | null;
162
+ /**
163
+ * `#font <css font, without the size> [tracking <em>]` — the face every
164
+ * `#param text` dial of this effect is set in, e.g. `#font 500 Georgia, serif
165
+ * tracking 0.12`. Null sets them in the platform's serif.
166
+ */
167
+ font: {
168
+ css: string;
169
+ tracking: number;
170
+ } | null;
143
171
  /**
144
172
  * How long ONE firing of this effect lasts, in seconds. 0 = undeclared.
145
173
  *
@@ -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,MA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