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/src/engine.ts CHANGED
@@ -62,6 +62,7 @@ import {
62
62
  import { packHalf, type HdrImage } from "./hdr"
63
63
  import { evalIrradianceSH, projectIrradianceSH, gradientIrradianceSH } from "./ibl"
64
64
  import { LYRIC_ATLAS_MAX_H, LYRIC_ATLAS_MAX_W, LYRICS_FLOATS, lyricsApi, packLyrics, type LyricLine, type LyricRect } from "./shaders/lyrics-api"
65
+ import { effectTextFont, effectTextWgsl, rasterEffectText, EFFECT_TEXT_PX, type EffectTextFont } from "./shaders/text-api"
65
66
  import {
66
67
  sceneTargets as sceneTargetsFor,
67
68
  sceneColorFormats,
@@ -608,10 +609,10 @@ type SunOptions = {
608
609
  shadow?: number
609
610
  }
610
611
 
611
- /** An effect param: number → f32, vector-like → vec3f (see setEffect).
612
- * Structural {x,y,z} rather than the Vec3 class so JSON-derived values (a
613
- * shared scene document's params) pass straight in. */
614
- export type EffectParamValue = number | { x: number; y: number; z: number } | EffectPointValue | EffectBoneValue
612
+ /** An effect param: number → f32, vector-like → vec3f, words → a text dial
613
+ * (see setEffect). Structural {x,y,z} rather than the Vec3 class so
614
+ * JSON-derived values (a shared scene document's params) pass straight in. */
615
+ export type EffectParamValue = number | string | { x: number; y: number; z: number } | EffectPointValue | EffectBoneValue
615
616
 
616
617
  /** A `point` dial: a world position, or — with `model` — an offset from where
617
618
  * that character stands, so the point travels with her. */
@@ -1181,6 +1182,12 @@ interface ModelInstance {
1181
1182
  * hiddenMaterials so a morph switching a part off never clobbers the user's
1182
1183
  * own visibility toggle, and vice versa. */
1183
1184
  morphHiddenMaterials: Set<string>
1185
+ /** Each PMX material's 1-based draw index (the shaders' materialId), by
1186
+ * name — what setXrayMaterials marks. */
1187
+ materialIdByName: Map<string, number>
1188
+ /** The see-through window last written into objectLight, as text, so an
1189
+ * unmoved window is not re-uploaded every frame. */
1190
+ xrayKey: string
1184
1191
  /** Materials the reflection probe leaves out (setMaterialInProbe). */
1185
1192
  probeHiddenMaterials: Set<string>
1186
1193
  /** Material-morph targets, or null when the model has no type-8 morphs. */
@@ -1658,6 +1665,10 @@ const TEXTURE_EXTENSION_GUESSES = [".png", ".jpg", ".jpeg", ".bmp", ".tga", ".dd
1658
1665
  */
1659
1666
  function paramValue(p: EffectParamDecl, given: EffectParamValue | undefined): EffectParamValue {
1660
1667
  if (p.kind === "bone") return isBoneValue(given) ? given : { bone: typeof p.value === "string" ? p.value : "", model: null }
1668
+ if (p.kind === "text") return typeof given === "string" ? given : typeof p.value === "string" ? p.value : ""
1669
+ // A lag is filled from the dial it trails, every frame.
1670
+ if (p.kind === "lag") return 0
1671
+ if (typeof given === "string") given = undefined
1661
1672
  if (given !== undefined) return p.kind === "int" && typeof given === "number" ? Math.round(given) : given
1662
1673
  if (p.kind === "color" && typeof p.value === "string") {
1663
1674
  const n = parseInt(p.value.slice(1), 16)
@@ -1691,6 +1702,8 @@ function castSubjectKey(subjects: readonly string[] | null): string {
1691
1702
  }
1692
1703
 
1693
1704
  const EFFECT_PARAMS_BINDING = 7
1705
+ /** The field module's binding for an effect's words — see text-api. */
1706
+ const EFFECT_TEXT_BINDING = 33
1694
1707
  const EFFECT_PARAMS_BINDING_GRID = 9
1695
1708
 
1696
1709
  /** How a mount receives the effect's declared dials: a generator for the struct
@@ -1699,7 +1712,8 @@ const EFFECT_PARAMS_BINDING_GRID = 9
1699
1712
  * emitted — WGSL has no empty struct, and a bound buffer nothing reads is a
1700
1713
  * layout mismatch. */
1701
1714
  type EffectParamsBinding = {
1702
- wgsl: (binding: number) => string
1715
+ /** `text` is true only for the field module, where the words are bound. */
1716
+ wgsl: (binding: number, text?: boolean) => string
1703
1717
  buffer: GPUBuffer | null
1704
1718
  }
1705
1719
 
@@ -1865,6 +1879,17 @@ const FIELD_LAYER_BLEND_ADDITIVE: GPUBlendState = {
1865
1879
  alpha: { srcFactor: "one", dstFactor: "one", operation: "add" },
1866
1880
  }
1867
1881
 
1882
+ /** An effect's text dials: every string they can show (values and keys), the
1883
+ * picture those are set in, and which one each dial shows now. */
1884
+ type EffectText = {
1885
+ font: EffectTextFont
1886
+ strings: Set<string>
1887
+ rects: Map<string, LyricRect>
1888
+ texture: GPUTexture | null
1889
+ view: GPUTextureView | null
1890
+ shown: Map<string, string>
1891
+ }
1892
+
1868
1893
  /**
1869
1894
  * One installed effect, whole.
1870
1895
  *
@@ -1886,20 +1911,25 @@ interface EffectInstance {
1886
1911
  /** One firing's length in seconds, from `#duration`. 0 = ambient. Reported
1887
1912
  * back at install so a host can place the effect at its own length. */
1888
1913
  duration: number
1889
- paramLayout: Map<string, { offset: number; comps: 1 | 3 }>
1914
+ paramLayout: Map<string, { offset: number; comps: 1 | 3 | 4 }>
1890
1915
  paramsBuffer: GPUBuffer | null
1891
1916
  paramsData: Float32Array<ArrayBuffer>
1917
+ /** The words its text dials can show, set into one picture — see text-api.
1918
+ * Null when it declares none. */
1919
+ text: EffectText | null
1892
1920
  /** The dials the engine resolves every frame rather than once: each bone
1893
1921
  * dial's bone and character, and each point dial set to follow one. */
1894
1922
  paramBindings: Map<string, EffectBoneValue | EffectPointValue>
1895
1923
  /** Dials driven by keys, by name — see setEffectParamTrack. Bone keys index
1896
1924
  * `bones`, since the sampler carries numbers. */
1897
- paramTracks: Map<string, { keys: ParamKey[]; bones: EffectBoneValue[]; last: ParamValue | null }>
1925
+ paramTracks: Map<string, { keys: ParamKey[]; bones: EffectBoneValue[]; texts: string[]; last: ParamValue | null }>
1898
1926
  /** This effect IS a mirror — see the `#mirror` directive. Draws no shader;
1899
1927
  * the engine reads its dials and folds the scene through the plane. */
1900
1928
  hasMirror: boolean
1901
1929
  /** `#stepped` — the cast it is aimed at moves on twos. */
1902
1930
  stepped: boolean
1931
+ /** `#xray` — its see-through window's four dials, or null. */
1932
+ xray: { x: string; y: string; width: string; height: string } | null
1903
1933
  /** Mounted under the scene. */
1904
1934
  hasBackground: boolean
1905
1935
  /** Mounted over the finished frame — and the reason the scene pass has to
@@ -1985,6 +2015,9 @@ interface EffectInstance {
1985
2015
  simStep: number
1986
2016
  /** Empty the particle pool and the grid before this frame's step. */
1987
2017
  simReset: boolean
2018
+ /** The cast its pool and grid were last stepped against — subject mask and
2019
+ * count. A scheduled effect held still re-seeds when this moves. */
2020
+ seededCast: number
1988
2021
  /**
1989
2022
  * The four durations this effect's `#dissolve` takes the cast apart over, as
1990
2023
  * its source wrote them. Null unless it declared one. A dial of the same name
@@ -5202,6 +5235,7 @@ export class Engine {
5202
5235
  { binding: 19, resource: { buffer: this.midiBuffer } },
5203
5236
  { binding: 24, resource: { buffer: this.lyricsBuffer } },
5204
5237
  { binding: 25, resource: this.lyricsTextureView },
5238
+ { binding: EFFECT_TEXT_BINDING, resource: owner.text?.view ?? this.trailFallbackView },
5205
5239
  // The size uniform for the pair THIS effect draws into.
5206
5240
  { binding: 14, resource: { buffer: this.fieldUniformBuffers[owner.fieldLayer] } },
5207
5241
  { binding: 22, resource: { buffer: owner.fieldClock ?? this.fieldUniformBuffers[owner.fieldLayer] } },
@@ -5637,7 +5671,7 @@ export class Engine {
5637
5671
  // reflection re-renders the scene from a folded camera, which is a pass and
5638
5672
  // not a shader, so the effect declares it and draws nothing itself.
5639
5673
  // #stepped too: it decides when a pose reaches the screen, not what is drawn.
5640
- if (!hasBackground && !hasForeground && !wantsParticles && !wantsTrails && !hasLightEmit(wgsl) && !d.mirror && !d.stepped) {
5674
+ if (!hasBackground && !hasForeground && !wantsParticles && !wantsTrails && !hasLightEmit(wgsl) && !d.mirror && !d.stepped && !d.xray) {
5641
5675
  return { ok: false, diagnostics: [
5642
5676
  "an effect must define fn background(ray: vec3f, uv: vec2f, time: f32) -> vec4f, " +
5643
5677
  "fn foreground(ray: vec3f, uv: vec2f, time: f32, depth: f32) -> vec4f, " +
@@ -5697,7 +5731,7 @@ export class Engine {
5697
5731
  // over the declared default; a name it passes that the source never declared
5698
5732
  // is ignored, because a field nothing reads is a binding nothing reads.
5699
5733
  const entries: [string, EffectParamValue][] = d.params.map((p) => [p.name, paramValue(p, params?.[p.name])])
5700
- const layout = new Map<string, { offset: number; comps: 1 | 3 }>()
5734
+ const layout = new Map<string, { offset: number; comps: 1 | 3 | 4 }>()
5701
5735
  const fields: string[] = []
5702
5736
  let cursor = 0
5703
5737
  for (const p of d.params) {
@@ -5719,6 +5753,15 @@ export class Engine {
5719
5753
  }
5720
5754
  continue
5721
5755
  }
5756
+ if (p.kind === "text") {
5757
+ // The rect of the string it shows in the effect's picture: u0, vTop,
5758
+ // u1, vBottom — read through rzText_NAME().
5759
+ const offset = Math.ceil(cursor / 16) * 16
5760
+ layout.set(p.name, { offset: offset / 4, comps: 4 })
5761
+ fields.push(` ${p.name}: vec4f,`)
5762
+ cursor = offset + 16
5763
+ continue
5764
+ }
5722
5765
  const isVec = p.kind === "color" || p.kind === "vec3" || p.kind === "point"
5723
5766
  const align = isVec ? 16 : 4
5724
5767
  const offset = Math.ceil(cursor / align) * align
@@ -5736,6 +5779,8 @@ export class Engine {
5736
5779
  paramBindings.set(name, value)
5737
5780
  continue
5738
5781
  }
5782
+ // Words get their rect once the picture is set, after the mounts build.
5783
+ if (typeof value === "string") continue
5739
5784
  if (typeof value === "number") paramsData[slot.offset] = value
5740
5785
  else {
5741
5786
  if ("model" in value && value.model) paramBindings.set(name, value)
@@ -5764,11 +5809,13 @@ export class Engine {
5764
5809
  * one states the number it can spare. Empty when nothing is declared: WGSL
5765
5810
  * has no empty struct, and a binding nothing reads is a layout mismatch.
5766
5811
  */
5767
- const paramsWgsl = (binding: number) =>
5812
+ const textNames = d.params.filter((p) => p.kind === "text").map((p) => p.name)
5813
+ const paramsWgsl = (binding: number, text = false) =>
5768
5814
  entries.length
5769
- ? `struct EffectParams {\n${fields.join("\n")}\n}\n@group(0) @binding(${binding}) var<uniform> params: EffectParams;\n${boneWgsl}`
5815
+ ? `struct EffectParams {\n${fields.join("\n")}\n}\n@group(0) @binding(${binding}) var<uniform> params: EffectParams;\n${boneWgsl}` +
5816
+ effectTextWgsl(textNames, text, EFFECT_TEXT_BINDING, "_rzGridSamp")
5770
5817
  : ""
5771
- const paramsDecl = paramsWgsl(EFFECT_PARAMS_BINDING)
5818
+ const paramsDecl = paramsWgsl(EFFECT_PARAMS_BINDING, true)
5772
5819
 
5773
5820
  // ── Compile with validation captured, not thrown at the console. Line
5774
5821
  // numbers in diagnostics are rebased to the USER's source.
@@ -5972,10 +6019,21 @@ export class Engine {
5972
6019
  paramLayout: layout,
5973
6020
  paramsBuffer,
5974
6021
  paramsData,
6022
+ text: textNames.length
6023
+ ? {
6024
+ font: d.font,
6025
+ strings: new Set(entries.flatMap(([, v]) => (typeof v === "string" && v ? [v] : []))),
6026
+ rects: new Map(),
6027
+ texture: null,
6028
+ view: null,
6029
+ shown: new Map(entries.flatMap(([n, v]) => (typeof v === "string" ? [[n, v] as [string, string]] : []))),
6030
+ }
6031
+ : null,
5975
6032
  paramBindings,
5976
6033
  paramTracks: new Map(),
5977
6034
  hasMirror: d.mirror,
5978
6035
  stepped: d.stepped,
6036
+ xray: d.xray,
5979
6037
  hasBackground,
5980
6038
  hasForeground,
5981
6039
  // The author's OWN source, not the assembled module: the assembled one
@@ -5987,9 +6045,11 @@ export class Engine {
5987
6045
  // Every route to the cast, in one test, and the author's own source for
5988
6046
  // the reason readsIds gives. `#anchor` counts: a file declaring one asks
5989
6047
  // the engine to record a bone for it, whether or not the accessor is
5990
- // spelled out in a line this matches.
6048
+ // spelled out in a line this matches. `#xray` counts too: whom its
6049
+ // window cuts is whom it is aimed at.
5991
6050
  readsCast: /\brz(?:Subject|SubjectCount|SubjectId|Anchor|Trail|TrailCount|CastDistance)\s*\(/.test(wgsl) ||
5992
- anchors.length > 0,
6051
+ anchors.length > 0 ||
6052
+ d.xray !== null,
5993
6053
  // Aimed at nobody in particular — the whole cast, which is what every
5994
6054
  // effect did before it could be aimed. A host narrows it afterwards, the
5995
6055
  // way it sets influence and a schedule.
@@ -6011,6 +6071,7 @@ export class Engine {
6011
6071
  sim: { start: null, time: null },
6012
6072
  simStep: 0,
6013
6073
  simReset: false,
6074
+ seededCast: -1,
6014
6075
  dissolve: d.dissolve ? dissolveConstants(authored) : null,
6015
6076
  ground: d.ground ? { color: new Vec3(...d.ground.color), noise: d.ground.noise } : null,
6016
6077
  // Its OWN resolution, no longer the scene's: an effect that never asked
@@ -6044,6 +6105,8 @@ export class Engine {
6044
6105
  trails,
6045
6106
  lights,
6046
6107
  }
6108
+ // Before the swap binds it, so the field's first frame already has words.
6109
+ if (instance.text) this.layoutEffectText(instance)
6047
6110
  return { ok: true, instance, warnings }
6048
6111
  }
6049
6112
 
@@ -6102,6 +6165,7 @@ export class Engine {
6102
6165
  const [identity, gamma] = await this.compositePipelines(false, false)
6103
6166
  for (const e of this.effects) {
6104
6167
  e.paramsBuffer?.destroy()
6168
+ e.text?.texture?.destroy()
6105
6169
  e.lights?.uniform.destroy()
6106
6170
  e.fieldClock?.destroy()
6107
6171
  }
@@ -6181,6 +6245,7 @@ export class Engine {
6181
6245
  // that was running is only torn down now.
6182
6246
  for (const e of this.effects) {
6183
6247
  e.paramsBuffer?.destroy()
6248
+ e.text?.texture?.destroy()
6184
6249
  e.lights?.uniform.destroy()
6185
6250
  e.fieldClock?.destroy()
6186
6251
  }
@@ -6823,12 +6888,20 @@ export class Engine {
6823
6888
  count.dispatchWorkgroups(1)
6824
6889
  count.end()
6825
6890
  }
6826
- const cp = encoder.beginComputePass({ label: "particles" })
6827
- cp.setPipeline(p.compute)
6828
- cp.setBindGroup(0, bind)
6829
- if (p.live) cp.dispatchWorkgroupsIndirect(p.live.indirect, 0)
6830
- else cp.dispatchWorkgroups(Math.ceil(p.count / 64))
6831
- cp.end()
6891
+ // A reset runs the pool TWICE: once to spawn every slot, once to step
6892
+ // what spawned, at dt 0. A dispatch either inits a slot or steps it, and
6893
+ // an effect that poses in its step (Pochita's bones, which its triangles
6894
+ // are drawn from) has no pose after the init alone. Playing, the next
6895
+ // frame's step supplied it; paused at a clip's start no next frame
6896
+ // steps, and the pet stayed collapsed — invisible — until play.
6897
+ for (let pass = e.simReset ? 2 : 1; pass > 0; pass--) {
6898
+ const cp = encoder.beginComputePass({ label: "particles" })
6899
+ cp.setPipeline(p.compute)
6900
+ cp.setBindGroup(0, bind)
6901
+ if (p.live) cp.dispatchWorkgroupsIndirect(p.live.indirect, 0)
6902
+ else cp.dispatchWorkgroups(Math.ceil(p.count / 64))
6903
+ cp.end()
6904
+ }
6832
6905
  }
6833
6906
  }
6834
6907
 
@@ -7614,7 +7687,10 @@ export class Engine {
7614
7687
  if (isBoneValue(value)) fx.paramBindings.set(name, value)
7615
7688
  return false
7616
7689
  }
7617
- if (isBoneValue(value)) return false
7690
+ if (decl.kind === "text") return typeof value === "string" ? this.showEffectText(fx, name, value) : false
7691
+ // A lag is its source's, trailing; nothing writes it by hand.
7692
+ if (decl.kind === "lag") return false
7693
+ if (isBoneValue(value) || typeof value === "string") return false
7618
7694
  if (typeof value === "number") {
7619
7695
  fx.paramsData[slot.offset] = decl.kind === "int" ? Math.round(value) : value
7620
7696
  return true
@@ -7646,8 +7722,10 @@ export class Engine {
7646
7722
  fx.paramTracks.delete(name)
7647
7723
  return true
7648
7724
  }
7649
- const holds = decl.kind === "bool" || decl.kind === "enum" || decl.kind === "bone"
7725
+ if (decl.kind === "lag") return false
7726
+ const holds = decl.kind === "bool" || decl.kind === "enum" || decl.kind === "bone" || decl.kind === "text"
7650
7727
  const bones: EffectBoneValue[] = []
7728
+ const texts: string[] = []
7651
7729
  const sorted = [...keys].sort((a, b) => a.time - b.time)
7652
7730
  const track: ParamKey[] = []
7653
7731
  for (const k of sorted) {
@@ -7655,7 +7733,11 @@ export class Engine {
7655
7733
  if (decl.kind === "bone") {
7656
7734
  if (!isBoneValue(k.value)) continue
7657
7735
  v = bones.push(k.value) - 1
7658
- } else if (typeof k.value === "number") v = k.value
7736
+ } else if (decl.kind === "text") {
7737
+ if (typeof k.value !== "string") continue
7738
+ v = texts.push(k.value) - 1
7739
+ } else if (typeof k.value === "string") continue
7740
+ else if (typeof k.value === "number") v = k.value
7659
7741
  else if (isBoneValue(k.value)) continue
7660
7742
  else v = [k.value.x, k.value.y, k.value.z]
7661
7743
  track.push({
@@ -7665,7 +7747,10 @@ export class Engine {
7665
7747
  ...(!holds && k.curve && k.ease !== "hold" ? { b: k.curve } : {}),
7666
7748
  })
7667
7749
  }
7668
- fx.paramTracks.set(name, { keys: track, bones, last: null })
7750
+ fx.paramTracks.set(name, { keys: track, bones, texts, last: null })
7751
+ // Every word a key can show goes into the picture now, so playing past a
7752
+ // key is a rect write and never a redraw.
7753
+ if (fx.text && texts.some((t) => t && !fx.text!.strings.has(t))) this.layoutEffectText(fx)
7669
7754
  this.evaluateEffectParams()
7670
7755
  return true
7671
7756
  }
@@ -7690,6 +7775,9 @@ export class Engine {
7690
7775
  if (decl?.kind === "bone") {
7691
7776
  const bone = typeof v === "number" ? track.bones[v] : undefined
7692
7777
  if (bone) fx.paramBindings.set(name, bone)
7778
+ } else if (decl?.kind === "text") {
7779
+ const words = typeof v === "number" ? track.texts[v] : undefined
7780
+ if (words !== undefined) dirty = this.showEffectText(fx, name, words) || dirty
7693
7781
  } else if (typeof v === "number") {
7694
7782
  dirty = this.writeEffectParam(fx, name, v) || dirty
7695
7783
  } else {
@@ -7699,8 +7787,202 @@ export class Engine {
7699
7787
  }
7700
7788
  }
7701
7789
  if (fx.paramBindings.size) dirty = this.resolveParamBindings(fx) || dirty
7790
+ for (const p of fx.paramDecls) {
7791
+ if (p.kind !== "lag") continue
7792
+ transport ??= this.transportTime()
7793
+ dirty = this.evaluateLag(fx, p, transport) || dirty
7794
+ }
7702
7795
  if (dirty) this.device.queue.writeBuffer(fx.paramsBuffer, 0, fx.paramsData)
7703
7796
  }
7797
+ this.applyXray()
7798
+ }
7799
+
7800
+ /**
7801
+ * A `lag` dial at time t: its source, trailing — what a game's health bar
7802
+ * draws as the chunk just lost. After the source falls it HOLDS where it was
7803
+ * for `hold` seconds, then drains at `rate` a second until it meets the
7804
+ * source again; a rise is met at once.
7805
+ *
7806
+ * Sampled from the source's KEYS, never from what the frames before this one
7807
+ * happened to see, so a scrub, a loop and a frame-by-frame export all draw
7808
+ * the same chunk. A source with no keys has no past: the lag is the source.
7809
+ */
7810
+ private evaluateLag(fx: EffectInstance, p: EffectParamDecl, t: number): boolean {
7811
+ const slot = fx.paramLayout.get(p.name)
7812
+ const src = p.source ? fx.paramLayout.get(p.source) : undefined
7813
+ if (!slot || !src) return false
7814
+ const d = fx.paramsData
7815
+ let v = d[src.offset]
7816
+ const track = p.source ? fx.paramTracks.get(p.source) : undefined
7817
+ if (track && track.keys.length > 0) {
7818
+ const hold = p.hold ?? 0
7819
+ const rate = Math.max(p.rate ?? 1, 1e-6)
7820
+ const decl = fx.paramDecls.find((q) => q.name === p.source)
7821
+ const range = decl?.min !== undefined && decl.max !== undefined ? Math.abs(decl.max - decl.min) : 1
7822
+ // Far enough back that anything older has drained past the full range.
7823
+ const span = hold + range / rate
7824
+ const at = (s: number) => {
7825
+ const sv = sampleParamTrack(track.keys, s)
7826
+ if (typeof sv !== "number") return
7827
+ v = Math.max(v, sv - rate * Math.max(0, t - s - hold))
7828
+ }
7829
+ // A fine grid, plus every key in the window exactly — a hit is a key,
7830
+ // and the instant before it is the value the chunk holds.
7831
+ const step = 1 / 120
7832
+ for (let s = t; s >= t - span; s -= step) at(s)
7833
+ for (const k of track.keys) {
7834
+ if (k.t > t || k.t < t - span) continue
7835
+ at(k.t)
7836
+ at(k.t - 1e-4)
7837
+ }
7838
+ }
7839
+ if (d[slot.offset] === v) return false
7840
+ d[slot.offset] = v
7841
+ return true
7842
+ }
7843
+
7844
+ /** Show `words` on a text dial: its rect into the block. A string the
7845
+ * picture has never held is set into it first, which redraws the picture
7846
+ * and writes the block itself. Returns whether the caller still has to. */
7847
+ private showEffectText(fx: EffectInstance, name: string, words: string): boolean {
7848
+ const t = fx.text
7849
+ if (!t) return false
7850
+ t.shown.set(name, words)
7851
+ if (words && !t.strings.has(words)) {
7852
+ this.layoutEffectText(fx)
7853
+ return false
7854
+ }
7855
+ return this.writeTextRect(fx, name, words)
7856
+ }
7857
+
7858
+ private writeTextRect(fx: EffectInstance, name: string, words: string): boolean {
7859
+ const slot = fx.paramLayout.get(name)
7860
+ if (!slot || !fx.text) return false
7861
+ const r = fx.text.rects.get(words) ?? [0, 0, 0, 0]
7862
+ const d = fx.paramsData
7863
+ let moved = false
7864
+ for (let k = 0; k < 4; k++) {
7865
+ if (d[slot.offset + k] === r[k]) continue
7866
+ d[slot.offset + k] = r[k]
7867
+ moved = true
7868
+ }
7869
+ return moved
7870
+ }
7871
+
7872
+ /**
7873
+ * Set every string an effect's text dials can show into its picture, upload
7874
+ * it and point each dial at its row. Re-binds only when the picture changes
7875
+ * SIZE. Once the face has loaded, a second pass sets it again — a web font
7876
+ * the page has not fetched yet draws in the fallback the first time.
7877
+ */
7878
+ private layoutEffectText(fx: EffectInstance, fontLoaded = false): void {
7879
+ const t = fx.text
7880
+ if (!t || !this.device) return
7881
+ // What the dials show now and what their keys can show — rebuilt rather
7882
+ // than added to, so typing a name does not keep every half-typed word.
7883
+ t.strings = new Set([...t.shown.values(), ...[...fx.paramTracks.values()].flatMap((k) => k.texts)].filter((w) => w.length > 0))
7884
+ const sheet = rasterEffectText([...t.strings], t.font)
7885
+ t.rects = sheet?.rects ?? new Map()
7886
+ const w = sheet?.width ?? 1
7887
+ const h = sheet?.height ?? 1
7888
+ if (!t.texture || t.texture.width !== w || t.texture.height !== h) {
7889
+ t.texture?.destroy()
7890
+ t.texture = this.device.createTexture({
7891
+ label: "effect text",
7892
+ size: [w, h],
7893
+ format: "r8unorm",
7894
+ usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT,
7895
+ })
7896
+ t.view = t.texture.createView()
7897
+ if (this.effects.includes(fx)) this.rebuildFieldBindGroup()
7898
+ }
7899
+ // premultipliedAlpha, as the lyric atlas: white glyphs on a clear canvas
7900
+ // arrive with coverage in red, antialiased edges intact.
7901
+ if (sheet) {
7902
+ this.device.queue.copyExternalImageToTexture(
7903
+ { source: sheet.source },
7904
+ { texture: t.texture, premultipliedAlpha: true },
7905
+ [w, h],
7906
+ )
7907
+ }
7908
+ for (const [name, words] of t.shown) this.writeTextRect(fx, name, words)
7909
+ if (fx.paramsBuffer) this.device.queue.writeBuffer(fx.paramsBuffer, 0, fx.paramsData)
7910
+ const fonts = typeof document !== "undefined" ? document.fonts : undefined
7911
+ if (!fontLoaded && fonts && t.font) {
7912
+ const css = effectTextFont(t.font, EFFECT_TEXT_PX)
7913
+ if (!fonts.check(css, [...t.strings].join(""))) {
7914
+ fonts
7915
+ .load(css, [...t.strings].join(""))
7916
+ .then(() => {
7917
+ if (this.effects.includes(fx)) this.layoutEffectText(fx, true)
7918
+ })
7919
+ .catch(() => {})
7920
+ }
7921
+ }
7922
+ }
7923
+
7924
+ /**
7925
+ * Each `#xray` effect's window, onto every character it is aimed at: a
7926
+ * rectangle on the frame from its X, Y, WIDTH and HEIGHT dials (fractions of
7927
+ * the frame; X and Y from its centre, Y up), in pixels at the size the scene
7928
+ * draws at — so it sits in
7929
+ * the same place in a preview and a 4K export. It is whole while the effect
7930
+ * plays at all — the effect draws its own border and cannot read its fade,
7931
+ * so a window that shrank with it would part from the border it sits in. A
7932
+ * model no window is on is written closed.
7933
+ */
7934
+ private applyXray(): void {
7935
+ const W = this.canvas.width
7936
+ const H = this.canvas.height
7937
+ const rects = new Map<string, Float32Array>()
7938
+ for (const fx of this.effects) {
7939
+ const x = fx.xray
7940
+ if (!x || fx.weight < 0.001) continue
7941
+ const d = fx.paramsData
7942
+ const read = (name: string, fallback: number): number => {
7943
+ const s = fx.paramLayout.get(name)
7944
+ return s ? d[s.offset] : fallback
7945
+ }
7946
+ // The frame's pixels run y down; the dial runs up from the centre.
7947
+ const cx = (0.5 + read(x.x, 0)) * W
7948
+ const cy = (0.5 - read(x.y, 0)) * H
7949
+ const hw = Math.abs(read(x.width, 0.25)) * 0.5 * W
7950
+ const hh = Math.abs(read(x.height, 0.25)) * 0.5 * H
7951
+ const rect = new Float32Array(16)
7952
+ rect[0] = cx - hw
7953
+ rect[1] = cy - hh
7954
+ rect[2] = cx + hw
7955
+ rect[3] = cy + hh
7956
+ rect[12] = 1
7957
+ for (const name of this.castSubjectNames(fx.subjects)) rects.set(name, rect)
7958
+ }
7959
+ for (const [name, inst] of this.modelInstances) {
7960
+ const rect = rects.get(name)
7961
+ const key = rect ? rect.join(",") : ""
7962
+ if (key === inst.xrayKey) continue
7963
+ inst.xrayKey = key
7964
+ inst.objectLight.set(rect ?? new Float32Array(16), 100)
7965
+ this.device.queue.writeBuffer(inst.lightBuffer, 100 * 4, inst.objectLight, 100, 16)
7966
+ }
7967
+ }
7968
+
7969
+ /**
7970
+ * Which of a model's materials a see-through window (#xray) cuts — her
7971
+ * clothes, by PMX material name. Everything else (skin, face, hair, eyes) is
7972
+ * drawn behind the window as anywhere else. Null or empty: none.
7973
+ */
7974
+ setXrayMaterials(modelName: string, names: readonly string[] | null): boolean {
7975
+ const inst = this.modelInstances.get(modelName)
7976
+ if (!inst) return false
7977
+ const mask = new Uint32Array(inst.objectLight.buffer, 116 * 4, 64)
7978
+ mask.fill(0)
7979
+ for (const n of names ?? []) {
7980
+ const id = inst.materialIdByName.get(n)
7981
+ if (id === undefined || id >= 512) continue
7982
+ mask[id >> 5] |= 1 << (id & 31)
7983
+ }
7984
+ this.device.queue.writeBuffer(inst.lightBuffer, 116 * 4, inst.objectLight, 116, 64)
7985
+ return true
7704
7986
  }
7705
7987
 
7706
7988
  /** Bones and followed points into the block, where they stand this frame. */
@@ -7908,6 +8190,15 @@ export class Engine {
7908
8190
  fx.sim = sim.clock
7909
8191
  fx.simStep = sim.step
7910
8192
  fx.simReset = sim.reset
8193
+ // A pool held still is seeded ONCE, against whoever was in the cast on
8194
+ // that frame. A scene opened paused at its first frame seeds before her
8195
+ // model has loaded, so an effect that spawns off the cast (chicks round
8196
+ // her feet) is born empty — and a held pool is never stepped again to
8197
+ // ask. So the seed is taken again when the cast it read has changed. The
8198
+ // cast was written earlier this frame (updateCameraUniforms).
8199
+ const cast = fx.subjectMask * 16 + fx.subjectCount
8200
+ if (sim.clock.start !== null && !fx.simReset && fx.simStep === 0 && cast !== fx.seededCast) fx.simReset = true
8201
+ if (fx.simReset || fx.simStep > 0) fx.seededCast = cast
7911
8202
  // Its own clock, expressed the way the mounts read it. Every mount
7912
8203
  // derives time from the epoch against sceneClock, so this one write moves
7913
8204
  // the field, the particles, the ribbons, lightEmit and the grid together
@@ -8527,6 +8818,8 @@ export class Engine {
8527
8818
  { binding: 24, visibility: GPUShaderStage.FRAGMENT, buffer: { type: "read-only-storage" } },
8528
8819
  // The lyric line atlas, for rzLyricText.
8529
8820
  { binding: 25, visibility: GPUShaderStage.FRAGMENT, texture: { sampleType: "float", viewDimension: "2d" } },
8821
+ // This effect's own words, for rzText_NAME — a 1x1 when it has none.
8822
+ { binding: EFFECT_TEXT_BINDING, visibility: GPUShaderStage.FRAGMENT, texture: { sampleType: "float", viewDimension: "2d" } },
8530
8823
  { binding: 14, visibility: GPUShaderStage.FRAGMENT, buffer: { type: "uniform" } },
8531
8824
  // This effect's own clock — see the field shader's note on why it is
8532
8825
  // not viewU[6].x.
@@ -15765,7 +16058,9 @@ export class Engine {
15765
16058
  // layers [0..3] (y: a stage, which takes the cast's shadow —
15766
16059
  // rzRoofedCastShadow), ambient flag [4..7], SH [8..43], fill [44..47],
15767
16060
  // jelly eyes [48..95], bloom [96..99] — see materials/common.ts.
15768
- const objectLight = new Float32Array(100)
16061
+ // 100 for the lights, the jelly eyes and the bloom flag; 16 for the
16062
+ // see-through window and 64 for the materials it cuts (XRAY_WGSL).
16063
+ const objectLight = new Float32Array(180)
15769
16064
  objectLight[96] = 1
15770
16065
  const objectLayers = new Uint32Array(objectLight.buffer)
15771
16066
  objectLayers[0] =
@@ -15862,6 +16157,8 @@ export class Engine {
15862
16157
  skinMatricesDirty: true,
15863
16158
  hiddenMaterials: new Set(),
15864
16159
  morphHiddenMaterials: new Set(),
16160
+ materialIdByName: new Map(),
16161
+ xrayKey: "",
15865
16162
  probeHiddenMaterials: new Set(),
15866
16163
  materialMorphTargets: null,
15867
16164
  materialMorphByIndex: null,
@@ -16431,6 +16728,7 @@ export class Engine {
16431
16728
  const indexCount = mat.vertexCount
16432
16729
  if (indexCount === 0) continue
16433
16730
  materialId++
16731
+ inst.materialIdByName.set(mat.name, materialId)
16434
16732
 
16435
16733
  let diffuseTexture = await loadTextureByIndex(mat.diffuseTextureIndex)
16436
16734
  if (!diffuseTexture) {
@@ -16567,6 +16865,12 @@ export class Engine {
16567
16865
  0,
16568
16866
  0,
16569
16867
  0,
16868
+ // The material's draw index, for the see-through window's cut — the
16869
+ // colour pass's materialId — padded to the vector it sits in.
16870
+ materialId,
16871
+ 0,
16872
+ 0,
16873
+ 0,
16570
16874
  ])
16571
16875
  const outlineUniformBuffer = this.createUniformBuffer(`${prefix}outline: ${mat.name}`, materialUniformData)
16572
16876
  inst.gpuBuffers.push(outlineUniformBuffer)
@@ -107,7 +107,9 @@ function prelude(renderClass: RenderClass, alphaMode: AlphaMode, blend: StyleBle
107
107
  const flip =
108
108
  renderClass === "eye" ? "" : "\n n = select(-n, n, dot(n, v) >= 0.0);"
109
109
  return `@fragment fn fs(input: VertexOutput) -> FSOut {
110
- ${PLAIN_SAMPLE} // MMD alpha semantics: material alpha × texture alpha, capped at 1 as MMD
110
+ ${PLAIN_SAMPLE} // The see-through window (#xray): her clothes are not drawn inside it.
111
+ if (rz_xray_cut(input.position, material.materialId)) { discard; }
112
+ // MMD alpha semantics: material alpha × texture alpha, capped at 1 as MMD
111
113
  // does (a PMX can author alpha above 1; blended, it burns the material white).
112
114
  let alpha = saturate(material.alpha * tex_s.a);
113
115
  ${discard}
@@ -155,7 +157,9 @@ ${gate}
155
157
  // only by its own lamps — must not light it through the fallback.
156
158
  let rz_keyed = (light.dirLayers[rz_key] & objectLight.layers.x) != 0u;
157
159
  let sun = select(vec3f(0.0), light.lights[rz_key].color.xyz * light.lights[rz_key].color.w, rz_keyed);
158
- let shadow = sampleShadow(input.worldPos, n);
160
+ // Lit through the see-through window: what it reveals would otherwise wear
161
+ // the shadow of the clothes it cut, which reads as dirt rather than as cloth.
162
+ let shadow = select(sampleShadow(input.worldPos, n), 1.0, rz_xray_inside(input.position));
159
163
  // The ambient: this object's own where loaded data gave it one — the game
160
164
  // gives every renderer its own — else the world's flat colour or the HDRI's
161
165
  // irradiance at this normal.