reze-engine 0.69.3 → 0.69.5

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
@@ -1181,6 +1181,12 @@ interface ModelInstance {
1181
1181
  * hiddenMaterials so a morph switching a part off never clobbers the user's
1182
1182
  * own visibility toggle, and vice versa. */
1183
1183
  morphHiddenMaterials: Set<string>
1184
+ /** Each PMX material's 1-based draw index (the shaders' materialId), by
1185
+ * name — what setXrayMaterials marks. */
1186
+ materialIdByName: Map<string, number>
1187
+ /** The see-through window last written into objectLight, as text, so an
1188
+ * unmoved window is not re-uploaded every frame. */
1189
+ xrayKey: string
1184
1190
  /** Materials the reflection probe leaves out (setMaterialInProbe). */
1185
1191
  probeHiddenMaterials: Set<string>
1186
1192
  /** Material-morph targets, or null when the model has no type-8 morphs. */
@@ -1900,6 +1906,8 @@ interface EffectInstance {
1900
1906
  hasMirror: boolean
1901
1907
  /** `#stepped` — the cast it is aimed at moves on twos. */
1902
1908
  stepped: boolean
1909
+ /** `#xray` — its see-through window's four dials, or null. */
1910
+ xray: { x: string; y: string; width: string; height: string } | null
1903
1911
  /** Mounted under the scene. */
1904
1912
  hasBackground: boolean
1905
1913
  /** Mounted over the finished frame — and the reason the scene pass has to
@@ -5637,7 +5645,7 @@ export class Engine {
5637
5645
  // reflection re-renders the scene from a folded camera, which is a pass and
5638
5646
  // not a shader, so the effect declares it and draws nothing itself.
5639
5647
  // #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) {
5648
+ if (!hasBackground && !hasForeground && !wantsParticles && !wantsTrails && !hasLightEmit(wgsl) && !d.mirror && !d.stepped && !d.xray) {
5641
5649
  return { ok: false, diagnostics: [
5642
5650
  "an effect must define fn background(ray: vec3f, uv: vec2f, time: f32) -> vec4f, " +
5643
5651
  "fn foreground(ray: vec3f, uv: vec2f, time: f32, depth: f32) -> vec4f, " +
@@ -5976,6 +5984,7 @@ export class Engine {
5976
5984
  paramTracks: new Map(),
5977
5985
  hasMirror: d.mirror,
5978
5986
  stepped: d.stepped,
5987
+ xray: d.xray,
5979
5988
  hasBackground,
5980
5989
  hasForeground,
5981
5990
  // The author's OWN source, not the assembled module: the assembled one
@@ -5987,9 +5996,11 @@ export class Engine {
5987
5996
  // Every route to the cast, in one test, and the author's own source for
5988
5997
  // the reason readsIds gives. `#anchor` counts: a file declaring one asks
5989
5998
  // the engine to record a bone for it, whether or not the accessor is
5990
- // spelled out in a line this matches.
5999
+ // spelled out in a line this matches. `#xray` counts too: whom its
6000
+ // window cuts is whom it is aimed at.
5991
6001
  readsCast: /\brz(?:Subject|SubjectCount|SubjectId|Anchor|Trail|TrailCount|CastDistance)\s*\(/.test(wgsl) ||
5992
- anchors.length > 0,
6002
+ anchors.length > 0 ||
6003
+ d.xray !== null,
5993
6004
  // Aimed at nobody in particular — the whole cast, which is what every
5994
6005
  // effect did before it could be aimed. A host narrows it afterwards, the
5995
6006
  // way it sets influence and a schedule.
@@ -7701,6 +7712,71 @@ export class Engine {
7701
7712
  if (fx.paramBindings.size) dirty = this.resolveParamBindings(fx) || dirty
7702
7713
  if (dirty) this.device.queue.writeBuffer(fx.paramsBuffer, 0, fx.paramsData)
7703
7714
  }
7715
+ this.applyXray()
7716
+ }
7717
+
7718
+ /**
7719
+ * Each `#xray` effect's window, onto every character it is aimed at: a
7720
+ * rectangle on the frame from its X, Y, WIDTH and HEIGHT dials (fractions of
7721
+ * the frame; X and Y from its centre, Y up), in pixels at the size the scene
7722
+ * draws at — so it sits in
7723
+ * the same place in a preview and a 4K export. It is whole while the effect
7724
+ * plays at all — the effect draws its own border and cannot read its fade,
7725
+ * so a window that shrank with it would part from the border it sits in. A
7726
+ * model no window is on is written closed.
7727
+ */
7728
+ private applyXray(): void {
7729
+ const W = this.canvas.width
7730
+ const H = this.canvas.height
7731
+ const rects = new Map<string, Float32Array>()
7732
+ for (const fx of this.effects) {
7733
+ const x = fx.xray
7734
+ if (!x || fx.weight < 0.001) continue
7735
+ const d = fx.paramsData
7736
+ const read = (name: string, fallback: number): number => {
7737
+ const s = fx.paramLayout.get(name)
7738
+ return s ? d[s.offset] : fallback
7739
+ }
7740
+ // The frame's pixels run y down; the dial runs up from the centre.
7741
+ const cx = (0.5 + read(x.x, 0)) * W
7742
+ const cy = (0.5 - read(x.y, 0)) * H
7743
+ const hw = Math.abs(read(x.width, 0.25)) * 0.5 * W
7744
+ const hh = Math.abs(read(x.height, 0.25)) * 0.5 * H
7745
+ const rect = new Float32Array(16)
7746
+ rect[0] = cx - hw
7747
+ rect[1] = cy - hh
7748
+ rect[2] = cx + hw
7749
+ rect[3] = cy + hh
7750
+ rect[12] = 1
7751
+ for (const name of this.castSubjectNames(fx.subjects)) rects.set(name, rect)
7752
+ }
7753
+ for (const [name, inst] of this.modelInstances) {
7754
+ const rect = rects.get(name)
7755
+ const key = rect ? rect.join(",") : ""
7756
+ if (key === inst.xrayKey) continue
7757
+ inst.xrayKey = key
7758
+ inst.objectLight.set(rect ?? new Float32Array(16), 100)
7759
+ this.device.queue.writeBuffer(inst.lightBuffer, 100 * 4, inst.objectLight, 100, 16)
7760
+ }
7761
+ }
7762
+
7763
+ /**
7764
+ * Which of a model's materials a see-through window (#xray) cuts — her
7765
+ * clothes, by PMX material name. Everything else (skin, face, hair, eyes) is
7766
+ * drawn behind the window as anywhere else. Null or empty: none.
7767
+ */
7768
+ setXrayMaterials(modelName: string, names: readonly string[] | null): boolean {
7769
+ const inst = this.modelInstances.get(modelName)
7770
+ if (!inst) return false
7771
+ const mask = new Uint32Array(inst.objectLight.buffer, 116 * 4, 64)
7772
+ mask.fill(0)
7773
+ for (const n of names ?? []) {
7774
+ const id = inst.materialIdByName.get(n)
7775
+ if (id === undefined || id >= 512) continue
7776
+ mask[id >> 5] |= 1 << (id & 31)
7777
+ }
7778
+ this.device.queue.writeBuffer(inst.lightBuffer, 116 * 4, inst.objectLight, 116, 64)
7779
+ return true
7704
7780
  }
7705
7781
 
7706
7782
  /** Bones and followed points into the block, where they stand this frame. */
@@ -9534,7 +9610,7 @@ export class Engine {
9534
9610
  )
9535
9611
 
9536
9612
  // GPU vertex-morph compute pipeline (shared by all models; per-model bind groups).
9537
- // Bindings: 0-4 read-only storage (base pos, CSR rowStart/colMorph/colOffset, weights),
9613
+ // Bindings: 0-4 read-only storage (base pos+uv, CSR rowStart/colMorph/colOffset, weights),
9538
9614
  // 5 read-write storage (vertex buffer), 6 uniform (params).
9539
9615
  const roStorage = { type: "read-only-storage" as const }
9540
9616
  this.morphComputeBindGroupLayout = this.device.createBindGroupLayout({
@@ -13735,7 +13811,8 @@ export class Engine {
13735
13811
  scratch[1] = position[1]
13736
13812
  scratch[2] = position[2]
13737
13813
  this.device.queue.writeBuffer(inst.vertexBuffer, index * 32, scratch)
13738
- if (inst.gpuMorph) this.device.queue.writeBuffer(inst.gpuMorph.baseBuf, index * 12, scratch)
13814
+ // The compute base is five floats a vertex (xyz uv): the position is its first three.
13815
+ if (inst.gpuMorph) this.device.queue.writeBuffer(inst.gpuMorph.baseBuf, index * 20, scratch)
13739
13816
  }
13740
13817
  }
13741
13818
 
@@ -15764,7 +15841,9 @@ export class Engine {
15764
15841
  // layers [0..3] (y: a stage, which takes the cast's shadow —
15765
15842
  // rzRoofedCastShadow), ambient flag [4..7], SH [8..43], fill [44..47],
15766
15843
  // jelly eyes [48..95], bloom [96..99] — see materials/common.ts.
15767
- const objectLight = new Float32Array(100)
15844
+ // 100 for the lights, the jelly eyes and the bloom flag; 16 for the
15845
+ // see-through window and 64 for the materials it cuts (XRAY_WGSL).
15846
+ const objectLight = new Float32Array(180)
15768
15847
  objectLight[96] = 1
15769
15848
  const objectLayers = new Uint32Array(objectLight.buffer)
15770
15849
  objectLayers[0] =
@@ -15861,6 +15940,8 @@ export class Engine {
15861
15940
  skinMatricesDirty: true,
15862
15941
  hiddenMaterials: new Set(),
15863
15942
  morphHiddenMaterials: new Set(),
15943
+ materialIdByName: new Map(),
15944
+ xrayKey: "",
15864
15945
  probeHiddenMaterials: new Set(),
15865
15946
  materialMorphTargets: null,
15866
15947
  materialMorphByIndex: null,
@@ -15960,7 +16041,7 @@ export class Engine {
15960
16041
  return buf
15961
16042
  }
15962
16043
 
15963
- const baseBuf = mkStorage("basePositions", data.basePositions)
16044
+ const baseBuf = mkStorage("base", data.base)
15964
16045
  const rowBuf = mkStorage("rowStart", data.rowStart)
15965
16046
  const colMorphBuf = mkStorage("colMorph", data.colMorph)
15966
16047
  const colOffsetBuf = mkStorage("colOffset", data.colOffset)
@@ -16430,6 +16511,7 @@ export class Engine {
16430
16511
  const indexCount = mat.vertexCount
16431
16512
  if (indexCount === 0) continue
16432
16513
  materialId++
16514
+ inst.materialIdByName.set(mat.name, materialId)
16433
16515
 
16434
16516
  let diffuseTexture = await loadTextureByIndex(mat.diffuseTextureIndex)
16435
16517
  if (!diffuseTexture) {
@@ -16566,6 +16648,12 @@ export class Engine {
16566
16648
  0,
16567
16649
  0,
16568
16650
  0,
16651
+ // The material's draw index, for the see-through window's cut — the
16652
+ // colour pass's materialId — padded to the vector it sits in.
16653
+ materialId,
16654
+ 0,
16655
+ 0,
16656
+ 0,
16569
16657
  ])
16570
16658
  const outlineUniformBuffer = this.createUniformBuffer(`${prefix}outline: ${mat.name}`, materialUniformData)
16571
16659
  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.
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
@@ -162,6 +162,15 @@ export type EffectDirectives = {
162
162
  * the engine's STEPPED_DIALS.
163
163
  */
164
164
  stepped: boolean
165
+ /**
166
+ * `#xray X Y WIDTH HEIGHT` — a see-through window held in front of the
167
+ * lens, as X-ray glasses are: a rectangle on the frame, four float dials in
168
+ * fractions of the frame — X and Y its offset from the frame's centre (Y
169
+ * up), WIDTH and HEIGHT its size. Behind it, the materials the host marks on the characters it is aimed
170
+ * at (setXrayMaterials — their clothes) are not drawn, so what is under them
171
+ * shows. Whole while the effect plays at all. Null without.
172
+ */
173
+ xray: { x: string; y: string; width: string; height: string } | null
165
174
  /**
166
175
  * How long ONE firing of this effect lasts, in seconds. 0 = undeclared.
167
176
  *
@@ -196,6 +205,7 @@ const SPEC = {
196
205
  dissolve: 0,
197
206
  mirror: 0,
198
207
  stepped: 0,
208
+ xray: 4,
199
209
  duration: 1,
200
210
  ground: "rest",
201
211
  } as const
@@ -275,6 +285,7 @@ export function parseDirectives(wgsl: string): DirectiveResult {
275
285
  ground: null,
276
286
  mirror: false,
277
287
  stepped: false,
288
+ xray: null,
278
289
  duration: 0,
279
290
  }
280
291
  const errors: string[] = []
@@ -522,6 +533,9 @@ export function parseDirectives(wgsl: string): DirectiveResult {
522
533
  case "stepped":
523
534
  d.stepped = true
524
535
  return
536
+ case "xray":
537
+ d.xray = { x: args[0], y: args[1], width: args[2], height: args[3] }
538
+ return
525
539
  case "duration": {
526
540
  // SECONDS, like every other time a directive states (see #dissolve).
527
541
  // The document above works in frames and converts once; an author
@@ -550,6 +564,13 @@ export function parseDirectives(wgsl: string): DirectiveResult {
550
564
  }
551
565
  })
552
566
 
567
+ // #xray names four dials, which have to be there and be numbers.
568
+ if (d.xray) {
569
+ for (const name of [d.xray.x, d.xray.y, d.xray.width, d.xray.height]) {
570
+ if (d.params.find((p) => p.name === name)?.kind !== "float") errors.push(`#xray: ${name} has to be a #param float`)
571
+ }
572
+ }
573
+
553
574
  // Duplicates are an author editing one line and forgetting another; last-wins
554
575
  // is a coin toss they never see resolved.
555
576
  const names = new Set<string>()
@@ -96,7 +96,30 @@ fn rzJellyVertex(p: vec3f, joints: vec4<u32>, nw: vec4f) -> vec3f {
96
96
  /** A model's ObjectLight as WGSL declares it (see the layout below). One copy
97
97
  * for every pass that binds it, so their layouts cannot drift. */
98
98
  export const OBJECT_LIGHT_STRUCT_WGSL =
99
- "struct ObjectLight { layers: vec4u, ambient: vec4f, sh: array<vec4f, 9>, fill: vec4f, jelly: array<vec4f, 12>, bloom: vec4f }"
99
+ "struct ObjectLight { layers: vec4u, ambient: vec4f, sh: array<vec4f, 9>, fill: vec4f, jelly: array<vec4f, 12>, bloom: vec4f, xray: array<vec4f, 4>, xrayMask: array<vec4u, 16> }"
100
+
101
+ /**
102
+ * The see-through window (#xray): a rectangle on the frame, in pixels —
103
+ * xray[0] is (left, top, right, bottom), xray[3].x is 1 while a window is open
104
+ * on this model — and which of the model's materials it cuts: a bit a
105
+ * material, by its 1-based draw index (materialId), up to 512. Tested against
106
+ * the fragment's own position, so it sits where the lens is, whatever moves
107
+ * behind it. Declared after the pass's own `objectLight` binding.
108
+ */
109
+ export const XRAY_WGSL = /* wgsl */ `
110
+ fn rz_xray_inside(fp: vec4f) -> bool {
111
+ if (objectLight.xray[3].x < 0.5) { return false; }
112
+ let r = objectLight.xray[0];
113
+ return fp.x >= r.x && fp.y >= r.y && fp.x <= r.z && fp.y <= r.w;
114
+ }
115
+ fn rz_xray_cut(fp: vec4f, materialId: f32) -> bool {
116
+ let i = u32(materialId);
117
+ if (i >= 512u) { return false; }
118
+ let word = objectLight.xrayMask[i / 128u][(i / 32u) % 4u];
119
+ if (((word >> (i % 32u)) & 1u) == 0u) { return false; }
120
+ return rz_xray_inside(fp);
121
+ }
122
+ `
100
123
 
101
124
  export const COMMON_BINDINGS_WGSL = /* wgsl */ `
102
125
 
@@ -297,6 +320,7 @@ fn _rzCastShadow(wp: vec3f) -> f32 {
297
320
  // outlines write.
298
321
  ${OBJECT_LIGHT_STRUCT_WGSL}
299
322
  @group(1) @binding(1) var<uniform> objectLight: ObjectLight;
323
+ ${XRAY_WGSL}
300
324
 
301
325
  ${JELLY_VERTEX_WGSL}
302
326
  /** The light arriving at a surface of this object facing n: its own ambient
@@ -27,7 +27,7 @@
27
27
  // attachment, and that answer does not exist at import time. The constant could
28
28
  // not have taken the outputs at all, which is most of why it did not have them.
29
29
 
30
- import { DISSOLVE_WGSL, JELLY_VERTEX_WGSL } from "../materials/common"
30
+ import { DISSOLVE_WGSL, JELLY_VERTEX_WGSL, OBJECT_LIGHT_STRUCT_WGSL, XRAY_WGSL } from "../materials/common"
31
31
  import { sceneFsOutWgsl, sceneIdPadWgsl } from "./scene-contract"
32
32
 
33
33
  export function transparentDepthPrepassWgsl(): string {
@@ -55,8 +55,9 @@ struct MaterialUniforms {
55
55
  @group(1) @binding(0) var<storage, read> skinMats: array<mat4x4f>;
56
56
  // The model's ObjectLight, for its jelly eyes: depth goes down where the colour
57
57
  // pass will draw the iris.
58
- struct ObjectLight { layers: vec4u, ambient: vec4f, sh: array<vec4f, 9>, fill: vec4f, jelly: array<vec4f, 12> }
58
+ ${OBJECT_LIGHT_STRUCT_WGSL}
59
59
  @group(1) @binding(1) var<uniform> objectLight: ObjectLight;
60
+ ${XRAY_WGSL}
60
61
  ${JELLY_VERTEX_WGSL}
61
62
  @group(2) @binding(0) var diffuseTexture: texture_2d<f32>;
62
63
  @group(2) @binding(1) var<uniform> material: MaterialUniforms;
@@ -118,6 +119,7 @@ override CUTOFF: f32 = 0.5;
118
119
  // her: you would see sky through her, which is the failure this line exists
119
120
  // to prevent.
120
121
  if (material.dissolve < 0.9995 && rz_dissolve_threshold(in.restPos) > material.dissolve) { discard; }
122
+ if (rz_xray_cut(in.position, material.materialId)) { discard; }
121
123
  var out: PrepassOut;
122
124
  out.color = vec4f(0.0);
123
125
  out.mask = vec4f(0.0);
@@ -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
  `
@@ -17,7 +17,7 @@
17
17
  // a string baked at import cannot know what the device said.
18
18
 
19
19
  import { sceneFsOutWgsl, sceneIdPadWgsl } from "./scene-contract"
20
- import { DISSOLVE_WGSL, OBJECT_LIGHT_STRUCT_WGSL } from "../materials/common"
20
+ import { DISSOLVE_WGSL, OBJECT_LIGHT_STRUCT_WGSL, XRAY_WGSL } from "../materials/common"
21
21
 
22
22
  /**
23
23
  * Where `dissolve` sits in the outline's own uniform, in bytes.
@@ -79,6 +79,9 @@ struct MaterialUniforms {
79
79
  * material's own alpha still holds — a line its author made sheer stays so. */
80
80
  colorOverride: vec4f,
81
81
  shadowColor: vec4f,
82
+ /** x: the material's 1-based draw index, as the colour pass's materialId —
83
+ * what lets the see-through window cut a garment's line and keep hers. */
84
+ xray: vec4f,
82
85
  };
83
86
 
84
87
  struct OutlineLight { direction: vec4f, color: vec4f, };
@@ -91,6 +94,7 @@ struct OutlineLights { ambientColor: vec4f, lights: array<OutlineLight, 4>, };
91
94
  @group(1) @binding(0) var<storage, read> skinMats: array<mat4x4f>;
92
95
  ${OBJECT_LIGHT_STRUCT_WGSL}
93
96
  @group(1) @binding(1) var<uniform> objectLight: ObjectLight;
97
+ ${XRAY_WGSL}
94
98
  @group(2) @binding(0) var<uniform> material: MaterialUniforms;
95
99
  @group(2) @binding(1) var diffuseTexture: texture_2d<f32>;
96
100
  /** A look's outline tint (ShaderGraph.outline.tint): one of its images, white
@@ -269,6 +273,8 @@ ${sceneFsOutWgsl({ name: "FSOut", aux: "mask" })}
269
273
  // bind-pose position — three passes, one rule, or they disagree about which
270
274
  // pieces are still there.
271
275
  if (material.dissolve < 0.9995 && rz_dissolve_threshold(input.restPos) > material.dissolve) { discard; }
276
+ // The see-through window cuts the lines of what it cuts, and keeps hers.
277
+ if (rz_xray_cut(input.position, material.xray.x)) { discard; }
272
278
  let tex = textureSample(diffuseTexture, edgeSampler, input.uv);
273
279
  let texA = tex.a;
274
280
  if (texA < 0.05) {