ugly-game 0.14.20 → 0.14.22
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/assets/gpuCrowd.d.ts +57 -1
- package/dist/assets/gpuCrowd.js +171 -13
- package/package.json +1 -1
|
@@ -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
|
|
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
|
}
|
package/dist/assets/gpuCrowd.js
CHANGED
|
@@ -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>;
|
|
@@ -902,13 +925,15 @@ const LUMA_GAIN: f32 = 1.2428;
|
|
|
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
927
|
// b.w is the PROJECTION MODE for the material texture: 0 = triplanar, 1 = cylindrical about the
|
|
905
|
-
// body's long axis. c.x is how far FRESNEL drives the output ALPHA
|
|
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.
|
|
906
931
|
const MATS = array<vec4f, 15>(
|
|
907
|
-
vec4f(0.00, 1.0, 0.00, 0.0), vec4f(0.00, -1.0, 1.000, 0.0), vec4f(0.00, 0.
|
|
908
|
-
vec4f(0.35, 64.0, 0.50, 0.0), vec4f(0.60, -1.0, 1.000, 0.0), vec4f(0.00, 0.
|
|
909
|
-
vec4f(0.90, 128.0, 0.80, 1.0), vec4f(1.00, 0.0, 0.333, 0.0), vec4f(0.00, 0.
|
|
910
|
-
vec4f(0.90, 256.0, 1.00, 0.0), vec4f(1.00, -1.0, 1.000, 0.0), vec4f(1.00, 0.
|
|
911
|
-
vec4f(0.08, 16.0, 0.15, 0.0), vec4f(1.00, 1.0, 0.300, 1.0), vec4f(0.00, 0.
|
|
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),
|
|
912
937
|
);
|
|
913
938
|
// ⚠️ Wood's tile scale is 0.22, not 0.5, and that was set by LOOKING. A shipped human stands ~16.6
|
|
914
939
|
// kit units, so 0.5 repeats the grain about eight times head to foot — fine enough that at any
|
|
@@ -1056,8 +1081,54 @@ fn cylindrical(layer: i32, p: vec3f, dx: vec3f, dy: vec3f) -> vec3f {
|
|
|
1056
1081
|
// c.x is 0 for every other row, so alpha is exactly tint.w for them - and material 0 is
|
|
1057
1082
|
// untouched, which is the invariant the whole table is built around.
|
|
1058
1083
|
let rim = select(0.0, fres / max(a.z, 1e-4), a.z > 0.0);
|
|
1059
|
-
|
|
1060
|
-
|
|
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
|
+
// ⚠️ NORMALIZED, and it was not at first. viewProj applied to a DIRECTION (w = 0) has no
|
|
1107
|
+
// meaningful magnitude - it carries the projection's scale terms and the rebased world's units,
|
|
1108
|
+
// so it came out in the tens. Times a 0.045 screen offset that is a displacement of whole
|
|
1109
|
+
// screens, which clamp() then pinned to a CORNER of the copy: every glass fragment sampled the
|
|
1110
|
+
// same corner texel, and face-on - where the rim term is near zero and the background is
|
|
1111
|
+
// essentially all you see - the body rendered as a flat dark shape. Only the direction is wanted
|
|
1112
|
+
// here; c.y alone sets how far.
|
|
1113
|
+
let ndRaw = (u.viewProj * vec4f(n, 0.0)).xy;
|
|
1114
|
+
let ndLen = length(ndRaw);
|
|
1115
|
+
let nd = select(vec2f(0.0), ndRaw / ndLen, ndLen > 1e-6);
|
|
1116
|
+
let off = vec2f(nd.x, -nd.y) * c.y;
|
|
1117
|
+
// Clamped so a grazing fragment cannot reach outside the copy and smear its edge texel across
|
|
1118
|
+
// the body.
|
|
1119
|
+
let suv = clamp(here + off, vec2f(0.0), vec2f(1.0));
|
|
1120
|
+
let behind = textureSampleLevel(sceneTex, skinSamp, suv, 0.0).rgb;
|
|
1121
|
+
// The transmitted background takes the glass's own colour, then the lit surface is layered over
|
|
1122
|
+
// it by the SAME fresnel rim that drove the alpha: clear in the middle, bright at the edge.
|
|
1123
|
+
outCol = mix(behind * base, lit, clamp(c.x * rim, 0.0, 1.0));
|
|
1124
|
+
// ⚠️ ALPHA 1 once we have composited the background OURSELVES. The blended pipeline would
|
|
1125
|
+
// otherwise mix our result with the UNDISTORTED scene underneath and half the refraction would
|
|
1126
|
+
// be cancelled straight back out. The record still carries tint.w < 1, so splitByAlpha still
|
|
1127
|
+
// routes glass to that pipeline and still gets its back-face cull and depth-write-off - this
|
|
1128
|
+
// only makes the blend itself a pass-through.
|
|
1129
|
+
alpha = 1.0;
|
|
1130
|
+
}
|
|
1131
|
+
return vec4f(mix(outCol, u.fog.xyz, f), alpha);
|
|
1061
1132
|
}`;
|
|
1062
1133
|
const INITIAL_MERGE_ENTRIES = 4096;
|
|
1063
1134
|
export class GpuCrowdRenderer {
|
|
@@ -1108,6 +1179,17 @@ export class GpuCrowdRenderer {
|
|
|
1108
1179
|
*/
|
|
1109
1180
|
matTex = null;
|
|
1110
1181
|
ownsMat = false;
|
|
1182
|
+
/**
|
|
1183
|
+
* The 1×1 stand-in bound at group 2 binding 3 when no scene copy is supplied, and the view the
|
|
1184
|
+
* `matBG` currently holds — group 2 is rebuilt only when that view actually changes, so a steady
|
|
1185
|
+
* frame does no bind-group work.
|
|
1186
|
+
*/
|
|
1187
|
+
dummyScene = null;
|
|
1188
|
+
dummySceneView = null;
|
|
1189
|
+
lastSceneView = null;
|
|
1190
|
+
skinArrayView = null;
|
|
1191
|
+
/** Whether any packed instance refracts — see `needsScene`. Recomputed on every setInstances. */
|
|
1192
|
+
refracting = false;
|
|
1111
1193
|
plan = null;
|
|
1112
1194
|
/** The CPU-side morph library. It never reaches the GPU — see `mergeTargets`. */
|
|
1113
1195
|
targets = [];
|
|
@@ -1334,19 +1416,56 @@ export class GpuCrowdRenderer {
|
|
|
1334
1416
|
// group 2 is the kit's atlas, so it changes exactly when the kit does. ARRAY-dimensioned even
|
|
1335
1417
|
// for a single-layer texture: the shader samples texture_2d_array unconditionally, and a plain
|
|
1336
1418
|
// 2d view against that binding is a validation error rather than a fallback.
|
|
1337
|
-
|
|
1419
|
+
// The 1×1 stand-in for the scene copy. Created once per kit and destroyed with it; `csm.w` is 0
|
|
1420
|
+
// whenever it is what is bound, so the shader never reads it.
|
|
1421
|
+
if (!this.dummyScene) {
|
|
1422
|
+
const t = dev.createTexture({
|
|
1423
|
+
size: { width: 1, height: 1 },
|
|
1424
|
+
format: 'rgba8unorm',
|
|
1425
|
+
usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST,
|
|
1426
|
+
});
|
|
1427
|
+
dev.queue.writeTexture({ texture: t }, new Uint8Array([0, 0, 0, 255]), { bytesPerRow: 4 }, { width: 1, height: 1 });
|
|
1428
|
+
this.dummyScene = t;
|
|
1429
|
+
this.dummySceneView = t.createView();
|
|
1430
|
+
}
|
|
1431
|
+
this.skinArrayView = a.skin.createView({ dimension: '2d-array' });
|
|
1432
|
+
this.buildMatBG(this.dummySceneView);
|
|
1433
|
+
this.rebuildLodBindGroups();
|
|
1434
|
+
this.frameBGDirty = true;
|
|
1435
|
+
}
|
|
1436
|
+
/**
|
|
1437
|
+
* Group 2 — the two atlases and the scene copy.
|
|
1438
|
+
*
|
|
1439
|
+
* Rebuilt when the SCENE VIEW changes and not otherwise: the two atlases belong to the kit, so a
|
|
1440
|
+
* steady frame with a stable scene texture does no bind-group work at all.
|
|
1441
|
+
*/
|
|
1442
|
+
buildMatBG(sceneView) {
|
|
1443
|
+
if (!this.skinArrayView || !this.matTex)
|
|
1444
|
+
return;
|
|
1445
|
+
this.matBG = this.device.createBindGroup({
|
|
1338
1446
|
layout: this.drawBGL[2],
|
|
1339
1447
|
entries: [
|
|
1340
|
-
{ binding: 0, resource:
|
|
1448
|
+
{ binding: 0, resource: this.skinArrayView },
|
|
1341
1449
|
{ binding: 1, resource: this.skinSamp },
|
|
1342
1450
|
{
|
|
1343
1451
|
binding: 2,
|
|
1344
1452
|
resource: this.matTex.createView({ dimension: '2d-array' }),
|
|
1345
1453
|
},
|
|
1454
|
+
{ binding: 3, resource: sceneView },
|
|
1346
1455
|
],
|
|
1347
1456
|
});
|
|
1348
|
-
this.
|
|
1349
|
-
|
|
1457
|
+
this.lastSceneView = sceneView;
|
|
1458
|
+
}
|
|
1459
|
+
/**
|
|
1460
|
+
* Does any instance currently packed actually REFRACT?
|
|
1461
|
+
*
|
|
1462
|
+
* ⚠️ THE WHOLE POINT OF THIS METHOD is to let a caller skip the scene-colour copy. A copy is a
|
|
1463
|
+
* full-screen blit; paying for one every frame so that a crowd of skin can ignore it is exactly
|
|
1464
|
+
* the cost that must not be added. Read straight off the records the renderer holds, so it cannot
|
|
1465
|
+
* disagree with what is about to be drawn.
|
|
1466
|
+
*/
|
|
1467
|
+
needsScene() {
|
|
1468
|
+
return this.refracting;
|
|
1350
1469
|
}
|
|
1351
1470
|
allocMergeBuffers() {
|
|
1352
1471
|
const dev = this.device, U = GPUBufferUsage;
|
|
@@ -1468,6 +1587,13 @@ export class GpuCrowdRenderer {
|
|
|
1468
1587
|
this.matTex?.destroy();
|
|
1469
1588
|
this.matTex = null;
|
|
1470
1589
|
this.ownsMat = false;
|
|
1590
|
+
// Ours, always — the caller never supplies the stand-in. Not freeing it leaks one 1×1 texture
|
|
1591
|
+
// per kit swap, which is small and still a leak.
|
|
1592
|
+
this.dummyScene?.destroy();
|
|
1593
|
+
this.dummyScene = null;
|
|
1594
|
+
this.dummySceneView = null;
|
|
1595
|
+
this.skinArrayView = null;
|
|
1596
|
+
this.lastSceneView = null;
|
|
1471
1597
|
}
|
|
1472
1598
|
/**
|
|
1473
1599
|
* Shape one character into its pool slot. Called at spawn, on a LOD change, and on a slider drag
|
|
@@ -1532,6 +1658,15 @@ export class GpuCrowdRenderer {
|
|
|
1532
1658
|
const total = validateBuckets(records, byLod, this.lodGpu.length);
|
|
1533
1659
|
this.buckets = byLod.filter((b) => b.count > 0);
|
|
1534
1660
|
this.draws = splitByAlpha(records, this.buckets);
|
|
1661
|
+
// ⚠️ Computed HERE, once per upload, not per `needsScene` call — that is asked every frame and
|
|
1662
|
+
// the record set only changes when this runs. One pass over the records the renderer is about to
|
|
1663
|
+
// hold, so the answer cannot disagree with what gets drawn.
|
|
1664
|
+
this.refracting = false;
|
|
1665
|
+
for (let i = 0; i < total; i++)
|
|
1666
|
+
if (REFRACTING_MATERIALS.has(records[i * CROWD_RECORD_FLOATS + CROWD_REC_MATERIAL] ?? 0)) {
|
|
1667
|
+
this.refracting = true;
|
|
1668
|
+
break;
|
|
1669
|
+
}
|
|
1535
1670
|
if (!total)
|
|
1536
1671
|
return;
|
|
1537
1672
|
const dev = this.device;
|
|
@@ -1584,7 +1719,16 @@ export class GpuCrowdRenderer {
|
|
|
1584
1719
|
frame(enc, colorView, depthView, camVP, sunDir, sunCol, amb, camPos, fog, fogDensity, clear, load = false,
|
|
1585
1720
|
// Optional sun shadow-receive: pass the terrain's cascade matrices + shadow-map view +
|
|
1586
1721
|
// comparison sampler and characters sit in the same shadow. Omit all three ⇒ no shadowing.
|
|
1587
|
-
lightVP, shadowView, shadowSamp
|
|
1722
|
+
lightVP, shadowView, shadowSamp,
|
|
1723
|
+
/**
|
|
1724
|
+
* A COPY of the colour target as it stood before this pass, plus its pixel size, so refracting
|
|
1725
|
+
* materials can sample what is behind a body.
|
|
1726
|
+
*
|
|
1727
|
+
* ⚠️ ONLY WORTH SUPPLYING WHEN `needsScene()` IS TRUE. A copy is a full-screen blit; taking one
|
|
1728
|
+
* every frame for a crowd with no glass in it is exactly the cost that must not be paid. Omit
|
|
1729
|
+
* this and refraction is off — glass falls back to its fresnel rim, which is what 0.14.20 did.
|
|
1730
|
+
*/
|
|
1731
|
+
scene) {
|
|
1588
1732
|
const dev = this.device;
|
|
1589
1733
|
const ready = this.ensureFrameBindGroups() && this.draws.length > 0;
|
|
1590
1734
|
// ── SKIN. One dispatch per bucket, BEFORE the render pass. WebGPU orders passes within an
|
|
@@ -1616,6 +1760,20 @@ export class GpuCrowdRenderer {
|
|
|
1616
1760
|
// csm.x = how many cascades are live. Zero ⇒ shadowFactor's loop exits at once and returns
|
|
1617
1761
|
// fully lit, which is exactly the no-shadow path.
|
|
1618
1762
|
u[36] = live;
|
|
1763
|
+
// csm.yz = the viewport in pixels, csm.w = whether the scene copy is real. All three ride spare
|
|
1764
|
+
// components of an existing vec4, so CROWD_U_FLOATS does not move and no caller's uniform
|
|
1765
|
+
// layout changes.
|
|
1766
|
+
u[37] = scene?.width ?? 0;
|
|
1767
|
+
u[38] = scene?.height ?? 0;
|
|
1768
|
+
u[39] = scene ? 1 : 0;
|
|
1769
|
+
// Rebind group 2 only when the scene view actually changed — a steady frame, with or without a
|
|
1770
|
+
// copy, does no bind-group work here.
|
|
1771
|
+
// ⚠️ The dummy VIEW is cached, not recreated. createView() returns a fresh object every call, so
|
|
1772
|
+
// building one here would never equal lastSceneView and would rebuild group 2 on every frame —
|
|
1773
|
+
// the precise per-frame cost this gating exists to avoid.
|
|
1774
|
+
const wantScene = scene?.view ?? this.dummySceneView;
|
|
1775
|
+
if (wantScene && wantScene !== this.lastSceneView)
|
|
1776
|
+
this.buildMatBG(wantScene);
|
|
1619
1777
|
for (let i = 0; i < live; i++)
|
|
1620
1778
|
u.set(mats[i], 40 + i * 16);
|
|
1621
1779
|
dev.queue.writeBuffer(this.uBuf, 0, u);
|