ugly-game 0.14.19 → 0.14.21

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.
@@ -147,6 +147,15 @@ export declare const CROWD_MATERIALS: {
147
147
  };
148
148
  /** Rows in the WGSL `MATS` table — the bound `writeCrowdRecord` checks a material against. */
149
149
  export declare const CROWD_MATERIAL_COUNT = 5;
150
+ /**
151
+ * Materials whose shader row has a non-zero REFRACTION strength (`c.y` in `MATS`).
152
+ *
153
+ * ⚠️ Must agree with that table, and `gpuCrowd.gpu.mjs` checks the two against each other on a real
154
+ * device. It exists so `needsScene` can answer without the CPU restating the shader's numbers, and
155
+ * so a material given refraction later cannot be forgotten here — which would leave it sampling a
156
+ * 1×1 dummy and looking merely flat.
157
+ */
158
+ export declare const REFRACTING_MATERIALS: ReadonlySet<number>;
150
159
  /** Which LOD bucket a contiguous run of instance records belongs to. */
151
160
  export interface CrowdBucket {
152
161
  lod: number;
@@ -477,6 +486,13 @@ export declare const CROWD_BINDINGS: {
477
486
  readonly sampleType: "float";
478
487
  readonly viewDimension: "2d-array";
479
488
  };
489
+ }, {
490
+ readonly binding: 3;
491
+ readonly visibility: 2;
492
+ readonly texture: {
493
+ readonly sampleType: "float";
494
+ readonly viewDimension: "2d";
495
+ };
480
496
  }];
481
497
  /**
482
498
  * group 3 — the pooled geometry and the instance records, per rung.
@@ -615,6 +631,17 @@ export declare class GpuCrowdRenderer {
615
631
  */
616
632
  private matTex;
617
633
  private ownsMat;
634
+ /**
635
+ * The 1×1 stand-in bound at group 2 binding 3 when no scene copy is supplied, and the view the
636
+ * `matBG` currently holds — group 2 is rebuilt only when that view actually changes, so a steady
637
+ * frame does no bind-group work.
638
+ */
639
+ private dummyScene;
640
+ private dummySceneView;
641
+ private lastSceneView;
642
+ private skinArrayView;
643
+ /** Whether any packed instance refracts — see `needsScene`. Recomputed on every setInstances. */
644
+ private refracting;
618
645
  private plan;
619
646
  /** The CPU-side morph library. It never reaches the GPU — see `mergeTargets`. */
620
647
  private targets;
@@ -668,6 +695,22 @@ export declare class GpuCrowdRenderer {
668
695
  * per-character or per-frame; a character is a slot plus a record, and that is the whole point.
669
696
  */
670
697
  setAssets(a: CrowdAssets): void;
698
+ /**
699
+ * Group 2 — the two atlases and the scene copy.
700
+ *
701
+ * Rebuilt when the SCENE VIEW changes and not otherwise: the two atlases belong to the kit, so a
702
+ * steady frame with a stable scene texture does no bind-group work at all.
703
+ */
704
+ private buildMatBG;
705
+ /**
706
+ * Does any instance currently packed actually REFRACT?
707
+ *
708
+ * ⚠️ THE WHOLE POINT OF THIS METHOD is to let a caller skip the scene-colour copy. A copy is a
709
+ * full-screen blit; paying for one every frame so that a crowd of skin can ignore it is exactly
710
+ * the cost that must not be added. Read straight off the records the renderer holds, so it cannot
711
+ * disagree with what is about to be drawn.
712
+ */
713
+ needsScene(): boolean;
671
714
  private allocMergeBuffers;
672
715
  private rebuildLodBindGroups;
673
716
  /**
@@ -714,5 +757,18 @@ export declare class GpuCrowdRenderer {
714
757
  * fauna standing next to it. `load = true` composites over whatever is already there; `false`
715
758
  * clears to `clear`.
716
759
  */
717
- frame(enc: GPUCommandEncoder, colorView: GPUTextureView, depthView: GPUTextureView, camVP: Float32Array, sunDir: [number, number, number], sunCol: [number, number, number], amb: [number, number, number], camPos: [number, number, number], fog: [number, number, number], fogDensity: number, clear: [number, number, number], load?: boolean, lightVP?: Float32Array | Float32Array[], shadowView?: GPUTextureView, shadowSamp?: GPUSampler): void;
760
+ frame(enc: GPUCommandEncoder, colorView: GPUTextureView, depthView: GPUTextureView, camVP: Float32Array, sunDir: [number, number, number], sunCol: [number, number, number], amb: [number, number, number], camPos: [number, number, number], fog: [number, number, number], fogDensity: number, clear: [number, number, number], load?: boolean, lightVP?: Float32Array | Float32Array[], shadowView?: GPUTextureView, shadowSamp?: GPUSampler,
761
+ /**
762
+ * A COPY of the colour target as it stood before this pass, plus its pixel size, so refracting
763
+ * materials can sample what is behind a body.
764
+ *
765
+ * ⚠️ ONLY WORTH SUPPLYING WHEN `needsScene()` IS TRUE. A copy is a full-screen blit; taking one
766
+ * every frame for a crowd with no glass in it is exactly the cost that must not be paid. Omit
767
+ * this and refraction is off — glass falls back to its fresnel rim, which is what 0.14.20 did.
768
+ */
769
+ scene?: {
770
+ view: GPUTextureView;
771
+ width: number;
772
+ height: number;
773
+ }): void;
718
774
  }
@@ -72,6 +72,17 @@ export const CROWD_MATERIALS = {
72
72
  };
73
73
  /** Rows in the WGSL `MATS` table — the bound `writeCrowdRecord` checks a material against. */
74
74
  export const CROWD_MATERIAL_COUNT = 5;
75
+ /**
76
+ * Materials whose shader row has a non-zero REFRACTION strength (`c.y` in `MATS`).
77
+ *
78
+ * ⚠️ Must agree with that table, and `gpuCrowd.gpu.mjs` checks the two against each other on a real
79
+ * device. It exists so `needsScene` can answer without the CPU restating the shader's numbers, and
80
+ * so a material given refraction later cannot be forgotten here — which would leave it sampling a
81
+ * 1×1 dummy and looking merely flat.
82
+ */
83
+ export const REFRACTING_MATERIALS = new Set([
84
+ CROWD_MATERIALS.glass,
85
+ ]);
75
86
  // ─────────────────────────────────────────────────────────────────────────────
76
87
  // PURE ARITHMETIC. Everything below this line down to the WGSL is a plain function over plain
77
88
  // arrays, with no GPUDevice anywhere — because there is no GPU in CI and "the proof is a picture"
@@ -604,6 +615,15 @@ export const CROWD_BINDINGS = {
604
615
  visibility: VIS_FRAGMENT,
605
616
  texture: { sampleType: 'float', viewDimension: '2d-array' },
606
617
  },
618
+ // A COPY of the scene colour as it stood before this pass, for refraction. Plain 2d, not an
619
+ // array. Bound to a 1×1 dummy when the caller supplies none, and then `csm.w` is 0 and the
620
+ // shader never samples it — see `needsScene`, which is how a frame with no glass in it pays
621
+ // for none of this.
622
+ {
623
+ binding: 3,
624
+ visibility: VIS_FRAGMENT,
625
+ texture: { sampleType: 'float', viewDimension: '2d' },
626
+ },
607
627
  ],
608
628
  /**
609
629
  * group 3 — the pooled geometry and the instance records, per rung.
@@ -803,6 +823,9 @@ struct U { viewProj: mat4x4f, sun: vec4f, sunCol: vec4f, amb: vec4f, cam: vec4f,
803
823
  // white array is bound when a caller supplies none, so white x colour = colour and the untextured
804
824
  // path is byte-identical to before this existed.
805
825
  @group(2) @binding(2) var matTex: texture_2d_array<f32>;
826
+ // The scene as it stood BEFORE this pass, for refraction. u.csm.w is 1 when it is real and 0 when
827
+ // it is the 1x1 dummy; u.csm.yz is the viewport in pixels, so a fragment can find itself in it.
828
+ @group(2) @binding(3) var sceneTex: texture_2d<f32>;
806
829
  @group(3) @binding(0) var<storage,read> def: array<vec4f>;
807
830
  @group(3) @binding(1) var<storage,read> defN: array<vec4f>;
808
831
  @group(3) @binding(2) var<storage,read> uvs: array<vec2f>;
@@ -901,17 +924,24 @@ const LUMA_GAIN: f32 = 1.2428;
901
924
  //
902
925
  // exp and scale are written as 1 on the rows that do not use them: a WGSL const array has no empty
903
926
  // cell, and pow(0, 0) is a value nobody should have to reason about.
904
- const MATS = array<vec4f, 10>(
905
- vec4f(0.00, 1.0, 0.00, 0.0), vec4f(0.00, -1.0, 1.000, 0.0),
906
- vec4f(0.35, 64.0, 0.50, 0.0), vec4f(0.60, -1.0, 1.000, 0.0),
907
- vec4f(0.90, 128.0, 0.80, 1.0), vec4f(1.00, 0.0, 0.333, 0.0),
908
- vec4f(0.90, 256.0, 1.00, 0.0), vec4f(1.00, -1.0, 1.000, 0.0),
909
- vec4f(0.08, 16.0, 0.15, 0.0), vec4f(1.00, 1.0, 0.220, 0.0),
927
+ // b.w is the PROJECTION MODE for the material texture: 0 = triplanar, 1 = cylindrical about the
928
+ // body's long axis. c.x is how far FRESNEL drives the output ALPHA and c.y is REFRACTION strength in
929
+ // screen widths - see the glass note in fs. c.y is 0 on every row but glass, and it is the flag that
930
+ // keeps every other material out of the scene-colour sample entirely.
931
+ const MATS = array<vec4f, 15>(
932
+ vec4f(0.00, 1.0, 0.00, 0.0), vec4f(0.00, -1.0, 1.000, 0.0), vec4f(0.00, 0.000, 0.0, 0.0),
933
+ vec4f(0.35, 64.0, 0.50, 0.0), vec4f(0.60, -1.0, 1.000, 0.0), vec4f(0.00, 0.000, 0.0, 0.0),
934
+ vec4f(0.90, 128.0, 0.80, 1.0), vec4f(1.00, 0.0, 0.333, 0.0), vec4f(0.00, 0.000, 0.0, 0.0),
935
+ vec4f(0.90, 256.0, 1.00, 0.0), vec4f(1.00, -1.0, 1.000, 0.0), vec4f(1.00, 0.045, 0.0, 0.0),
936
+ vec4f(0.08, 16.0, 0.15, 0.0), vec4f(1.00, 1.0, 0.300, 1.0), vec4f(0.00, 0.000, 0.0, 0.0),
910
937
  );
911
938
  // ⚠️ Wood's tile scale is 0.22, not 0.5, and that was set by LOOKING. A shipped human stands ~16.6
912
939
  // kit units, so 0.5 repeats the grain about eight times head to foot — fine enough that at any
913
940
  // normal camera distance it averages into a flat tan and the character reads as bare skin. 0.22 is
914
- // closer to four repeats, which is a plank rather than a veneer.
941
+ // closer to four repeats, which is a plank rather than a veneer. It then moved again to 0.30 when
942
+ // wood went CYLINDRICAL: the scale now multiplies a RADIUS rather than a linear coordinate, and a
943
+ // torso is only ~1.5 kit units thick, so the value that gave four bands across a body gave almost
944
+ // no variation at all across its radius and the rings disappeared.
915
945
  fn luma(c: vec3f) -> f32 { return dot(c, vec3f(0.2126, 0.7152, 0.0722)); }
916
946
  // TRIPLANAR: three samples of the same tiling map, blended by the bind-space normal.
917
947
  //
@@ -937,9 +967,35 @@ fn triplanar(layer: i32, p: vec3f, dx: vec3f, dy: vec3f, n: vec3f) -> vec3f {
937
967
  let z = textureSampleGrad(matTex, skinSamp, p.xy, layer, dx.xy, dy.xy).rgb;
938
968
  return x * w.x + y * w.y + z * w.z;
939
969
  }
970
+ // CYLINDRICAL, about the body's own long axis - the projection a thing CARVED FROM A LOG needs.
971
+ //
972
+ // ⚠️ Triplanar is wrong for wood, and it looked it. A 2D tile whose rings run along texture-V maps
973
+ // V to HEIGHT on a chest (the +Z plane wins there), so the rings came out as horizontal bands
974
+ // stacked up the torso - a figure made of discs - and then flipped direction on the arms, where the
975
+ // +X plane wins and V maps to depth instead. Direction that changes with which way a surface
976
+ // happens to face is the one thing a grain must never do.
977
+ //
978
+ // Radius from the axis drives the RINGS and height drives the FIBRE, so a growth ring is a shell
979
+ // around the trunk and the fibre runs the length of it, exactly as in the log the figure came from.
980
+ // Same bind-space trick the hair's uvs2 uses, and for the same reason.
981
+ fn cylindrical(layer: i32, p: vec3f, dx: vec3f, dy: vec3f) -> vec3f {
982
+ let r = length(p.xz);
983
+ // ⚠️ Gradients carried through by the chain rule rather than dropped. d(r)/dx is the radial
984
+ // direction dotted with the position derivative; without it this needs implicit derivatives and
985
+ // is back in the uniform-control-flow trap textureSampleGrad exists to avoid.
986
+ let radial = select(vec2f(1.0, 0.0), p.xz / max(r, 1e-6), r > 1e-6);
987
+ let drx = dot(dx.xz, radial);
988
+ let dry = dot(dy.xz, radial);
989
+ return textureSampleGrad(
990
+ matTex, skinSamp,
991
+ vec2f(p.y, r), layer,
992
+ vec2f(dx.y, drx), vec2f(dy.y, dry),
993
+ ).rgb;
994
+ }
940
995
  @fragment fn fs(i: VOut) -> @location(0) vec4f {
941
- let a = MATS[i.mat * 2u];
942
- let b = MATS[i.mat * 2u + 1u];
996
+ let a = MATS[i.mat * 3u];
997
+ let b = MATS[i.mat * 3u + 1u];
998
+ let c = MATS[i.mat * 3u + 2u];
943
999
  let n = normalize(i.n);
944
1000
  let l = -normalize(u.sun.xyz);
945
1001
  let d = clamp(dot(n, l), 0.0, 1.0);
@@ -970,7 +1026,13 @@ fn triplanar(layer: i32, p: vec3f, dx: vec3f, dy: vec3f, n: vec3f) -> vec3f {
970
1026
  let crl = length(cr);
971
1027
  let bn = select(vec3f(0.0, 0.0, 1.0), cr / crl, crl > 1e-20);
972
1028
  var matC = vec3f(1.0);
973
- if (i32(b.y) >= 0) { matC = triplanar(i32(b.y), i.rest * b.z, rdx * b.z, rdy * b.z, bn); }
1029
+ if (i32(b.y) >= 0) {
1030
+ if (b.w > 0.5) {
1031
+ matC = cylindrical(i32(b.y), i.rest * b.z, rdx * b.z, rdy * b.z);
1032
+ } else {
1033
+ matC = triplanar(i32(b.y), i.rest * b.z, rdx * b.z, rdy * b.z, bn);
1034
+ }
1035
+ }
974
1036
  let base = detail * matC * i.tint.rgb;
975
1037
 
976
1038
  let v = normalize(u.cam.xyz - i.world);
@@ -1004,9 +1066,60 @@ fn triplanar(layer: i32, p: vec3f, dx: vec3f, dy: vec3f, n: vec3f) -> vec3f {
1004
1066
 
1005
1067
  let dist = length(i.world - u.cam.xyz);
1006
1068
  let f = clamp(1.0 - exp(-dist * u.fog.w), 0.0, 1.0);
1007
- // tint.w is the opacity. It is 1 for everyone on the opaque pipeline, where the target has no
1008
- // blend state and the channel is ignored, so the two pipelines run the SAME fragment shader.
1009
- return vec4f(mix(lit, u.fog.xyz, f), i.tint.w);
1069
+ // ⚠️ GLASS GETS ITS ALPHA FROM FRESNEL, and a flat alpha is why the first cut read as a ghost
1070
+ // rather than as glass. A sheet of glass is nearly CLEAR face-on and nearly OPAQUE at a grazing
1071
+ // angle; that falloff is most of what the eye uses to recognise the material, and with a constant
1072
+ // 0.35 everywhere the body simply faded out evenly and disappeared into the grass.
1073
+ //
1074
+ // The raw Schlick term is reused rather than recomputed - fres above is already
1075
+ // fresnel x pow(1 - n.v, 5), so dividing out the strength recovers the geometric part.
1076
+ //
1077
+ // ⚠️ NOT refraction. Nothing here bends what is behind the body: this pass composites into the
1078
+ // scene colour with loadOp 'load' and never receives a sampleable copy of it, so there is nothing
1079
+ // to distort. A rim is what is reachable without that copy.
1080
+ //
1081
+ // c.x is 0 for every other row, so alpha is exactly tint.w for them - and material 0 is
1082
+ // untouched, which is the invariant the whole table is built around.
1083
+ let rim = select(0.0, fres / max(a.z, 1e-4), a.z > 0.0);
1084
+ var alpha = clamp(mix(i.tint.w, 1.0, c.x * rim), 0.0, 1.0);
1085
+ var outCol = lit;
1086
+
1087
+ // ── REFRACTION ────────────────────────────────────────────────────────────
1088
+ //
1089
+ // ⚠️ GATED ON c.y, WHICH IS ZERO ON EVERY ROW BUT GLASS. Nothing else reaches the sample, so a
1090
+ // crowd of skin costs exactly what it did; and the caller only copies the scene at all when
1091
+ // needsScene() says a refracting instance is actually on screen, so a frame with no glass in it
1092
+ // pays for neither the copy nor the bind.
1093
+ //
1094
+ // ⚠️ textureSampleLevel, not textureSample. A screen-space lookup wants mip 0 anyway, and an
1095
+ // explicit level takes no implicit derivatives - which is what makes it legal inside this
1096
+ // non-uniform branch. Same constraint the material atlas hit, solved the cheaper way because
1097
+ // there is no mip chain to want here.
1098
+ if (c.y > 0.0 && u.csm.w > 0.5) {
1099
+ let vp = vec2f(max(u.csm.y, 1.0), max(u.csm.z, 1.0));
1100
+ // pos is in FRAMEBUFFER pixels, so this is already the sample's own place in the copy.
1101
+ let here = i.pos.xy / vp;
1102
+ // The surface normal's direction ON SCREEN. viewProj applied to a direction (w = 0) gives its
1103
+ // clip-space heading; the y is negated because clip space runs +y UP and a texture's v runs
1104
+ // DOWN. Getting that flip wrong bends the background the wrong way, which reads as glass with
1105
+ // an odd tilt rather than as anything obviously broken.
1106
+ let nd = (u.viewProj * vec4f(n, 0.0)).xy;
1107
+ let off = vec2f(nd.x, -nd.y) * c.y;
1108
+ // Clamped so a grazing fragment cannot reach outside the copy and smear its edge texel across
1109
+ // the body.
1110
+ let suv = clamp(here + off, vec2f(0.0), vec2f(1.0));
1111
+ let behind = textureSampleLevel(sceneTex, skinSamp, suv, 0.0).rgb;
1112
+ // The transmitted background takes the glass's own colour, then the lit surface is layered over
1113
+ // it by the SAME fresnel rim that drove the alpha: clear in the middle, bright at the edge.
1114
+ outCol = mix(behind * base, lit, clamp(c.x * rim, 0.0, 1.0));
1115
+ // ⚠️ ALPHA 1 once we have composited the background OURSELVES. The blended pipeline would
1116
+ // otherwise mix our result with the UNDISTORTED scene underneath and half the refraction would
1117
+ // be cancelled straight back out. The record still carries tint.w < 1, so splitByAlpha still
1118
+ // routes glass to that pipeline and still gets its back-face cull and depth-write-off - this
1119
+ // only makes the blend itself a pass-through.
1120
+ alpha = 1.0;
1121
+ }
1122
+ return vec4f(mix(outCol, u.fog.xyz, f), alpha);
1010
1123
  }`;
1011
1124
  const INITIAL_MERGE_ENTRIES = 4096;
1012
1125
  export class GpuCrowdRenderer {
@@ -1057,6 +1170,17 @@ export class GpuCrowdRenderer {
1057
1170
  */
1058
1171
  matTex = null;
1059
1172
  ownsMat = false;
1173
+ /**
1174
+ * The 1×1 stand-in bound at group 2 binding 3 when no scene copy is supplied, and the view the
1175
+ * `matBG` currently holds — group 2 is rebuilt only when that view actually changes, so a steady
1176
+ * frame does no bind-group work.
1177
+ */
1178
+ dummyScene = null;
1179
+ dummySceneView = null;
1180
+ lastSceneView = null;
1181
+ skinArrayView = null;
1182
+ /** Whether any packed instance refracts — see `needsScene`. Recomputed on every setInstances. */
1183
+ refracting = false;
1060
1184
  plan = null;
1061
1185
  /** The CPU-side morph library. It never reaches the GPU — see `mergeTargets`. */
1062
1186
  targets = [];
@@ -1283,19 +1407,56 @@ export class GpuCrowdRenderer {
1283
1407
  // group 2 is the kit's atlas, so it changes exactly when the kit does. ARRAY-dimensioned even
1284
1408
  // for a single-layer texture: the shader samples texture_2d_array unconditionally, and a plain
1285
1409
  // 2d view against that binding is a validation error rather than a fallback.
1286
- this.matBG = dev.createBindGroup({
1410
+ // The 1×1 stand-in for the scene copy. Created once per kit and destroyed with it; `csm.w` is 0
1411
+ // whenever it is what is bound, so the shader never reads it.
1412
+ if (!this.dummyScene) {
1413
+ const t = dev.createTexture({
1414
+ size: { width: 1, height: 1 },
1415
+ format: 'rgba8unorm',
1416
+ usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST,
1417
+ });
1418
+ dev.queue.writeTexture({ texture: t }, new Uint8Array([0, 0, 0, 255]), { bytesPerRow: 4 }, { width: 1, height: 1 });
1419
+ this.dummyScene = t;
1420
+ this.dummySceneView = t.createView();
1421
+ }
1422
+ this.skinArrayView = a.skin.createView({ dimension: '2d-array' });
1423
+ this.buildMatBG(this.dummySceneView);
1424
+ this.rebuildLodBindGroups();
1425
+ this.frameBGDirty = true;
1426
+ }
1427
+ /**
1428
+ * Group 2 — the two atlases and the scene copy.
1429
+ *
1430
+ * Rebuilt when the SCENE VIEW changes and not otherwise: the two atlases belong to the kit, so a
1431
+ * steady frame with a stable scene texture does no bind-group work at all.
1432
+ */
1433
+ buildMatBG(sceneView) {
1434
+ if (!this.skinArrayView || !this.matTex)
1435
+ return;
1436
+ this.matBG = this.device.createBindGroup({
1287
1437
  layout: this.drawBGL[2],
1288
1438
  entries: [
1289
- { binding: 0, resource: a.skin.createView({ dimension: '2d-array' }) },
1439
+ { binding: 0, resource: this.skinArrayView },
1290
1440
  { binding: 1, resource: this.skinSamp },
1291
1441
  {
1292
1442
  binding: 2,
1293
1443
  resource: this.matTex.createView({ dimension: '2d-array' }),
1294
1444
  },
1445
+ { binding: 3, resource: sceneView },
1295
1446
  ],
1296
1447
  });
1297
- this.rebuildLodBindGroups();
1298
- this.frameBGDirty = true;
1448
+ this.lastSceneView = sceneView;
1449
+ }
1450
+ /**
1451
+ * Does any instance currently packed actually REFRACT?
1452
+ *
1453
+ * ⚠️ THE WHOLE POINT OF THIS METHOD is to let a caller skip the scene-colour copy. A copy is a
1454
+ * full-screen blit; paying for one every frame so that a crowd of skin can ignore it is exactly
1455
+ * the cost that must not be added. Read straight off the records the renderer holds, so it cannot
1456
+ * disagree with what is about to be drawn.
1457
+ */
1458
+ needsScene() {
1459
+ return this.refracting;
1299
1460
  }
1300
1461
  allocMergeBuffers() {
1301
1462
  const dev = this.device, U = GPUBufferUsage;
@@ -1417,6 +1578,13 @@ export class GpuCrowdRenderer {
1417
1578
  this.matTex?.destroy();
1418
1579
  this.matTex = null;
1419
1580
  this.ownsMat = false;
1581
+ // Ours, always — the caller never supplies the stand-in. Not freeing it leaks one 1×1 texture
1582
+ // per kit swap, which is small and still a leak.
1583
+ this.dummyScene?.destroy();
1584
+ this.dummyScene = null;
1585
+ this.dummySceneView = null;
1586
+ this.skinArrayView = null;
1587
+ this.lastSceneView = null;
1420
1588
  }
1421
1589
  /**
1422
1590
  * Shape one character into its pool slot. Called at spawn, on a LOD change, and on a slider drag
@@ -1481,6 +1649,15 @@ export class GpuCrowdRenderer {
1481
1649
  const total = validateBuckets(records, byLod, this.lodGpu.length);
1482
1650
  this.buckets = byLod.filter((b) => b.count > 0);
1483
1651
  this.draws = splitByAlpha(records, this.buckets);
1652
+ // ⚠️ Computed HERE, once per upload, not per `needsScene` call — that is asked every frame and
1653
+ // the record set only changes when this runs. One pass over the records the renderer is about to
1654
+ // hold, so the answer cannot disagree with what gets drawn.
1655
+ this.refracting = false;
1656
+ for (let i = 0; i < total; i++)
1657
+ if (REFRACTING_MATERIALS.has(records[i * CROWD_RECORD_FLOATS + CROWD_REC_MATERIAL] ?? 0)) {
1658
+ this.refracting = true;
1659
+ break;
1660
+ }
1484
1661
  if (!total)
1485
1662
  return;
1486
1663
  const dev = this.device;
@@ -1533,7 +1710,16 @@ export class GpuCrowdRenderer {
1533
1710
  frame(enc, colorView, depthView, camVP, sunDir, sunCol, amb, camPos, fog, fogDensity, clear, load = false,
1534
1711
  // Optional sun shadow-receive: pass the terrain's cascade matrices + shadow-map view +
1535
1712
  // comparison sampler and characters sit in the same shadow. Omit all three ⇒ no shadowing.
1536
- lightVP, shadowView, shadowSamp) {
1713
+ lightVP, shadowView, shadowSamp,
1714
+ /**
1715
+ * A COPY of the colour target as it stood before this pass, plus its pixel size, so refracting
1716
+ * materials can sample what is behind a body.
1717
+ *
1718
+ * ⚠️ ONLY WORTH SUPPLYING WHEN `needsScene()` IS TRUE. A copy is a full-screen blit; taking one
1719
+ * every frame for a crowd with no glass in it is exactly the cost that must not be paid. Omit
1720
+ * this and refraction is off — glass falls back to its fresnel rim, which is what 0.14.20 did.
1721
+ */
1722
+ scene) {
1537
1723
  const dev = this.device;
1538
1724
  const ready = this.ensureFrameBindGroups() && this.draws.length > 0;
1539
1725
  // ── SKIN. One dispatch per bucket, BEFORE the render pass. WebGPU orders passes within an
@@ -1565,6 +1751,20 @@ export class GpuCrowdRenderer {
1565
1751
  // csm.x = how many cascades are live. Zero ⇒ shadowFactor's loop exits at once and returns
1566
1752
  // fully lit, which is exactly the no-shadow path.
1567
1753
  u[36] = live;
1754
+ // csm.yz = the viewport in pixels, csm.w = whether the scene copy is real. All three ride spare
1755
+ // components of an existing vec4, so CROWD_U_FLOATS does not move and no caller's uniform
1756
+ // layout changes.
1757
+ u[37] = scene?.width ?? 0;
1758
+ u[38] = scene?.height ?? 0;
1759
+ u[39] = scene ? 1 : 0;
1760
+ // Rebind group 2 only when the scene view actually changed — a steady frame, with or without a
1761
+ // copy, does no bind-group work here.
1762
+ // ⚠️ The dummy VIEW is cached, not recreated. createView() returns a fresh object every call, so
1763
+ // building one here would never equal lastSceneView and would rebuild group 2 on every frame —
1764
+ // the precise per-frame cost this gating exists to avoid.
1765
+ const wantScene = scene?.view ?? this.dummySceneView;
1766
+ if (wantScene && wantScene !== this.lastSceneView)
1767
+ this.buildMatBG(wantScene);
1568
1768
  for (let i = 0; i < live; i++)
1569
1769
  u.set(mats[i], 40 + i * 16);
1570
1770
  dev.queue.writeBuffer(this.uBuf, 0, u);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ugly-game",
3
- "version": "0.14.19",
3
+ "version": "0.14.21",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {