ugly-game 0.15.26 → 0.15.27
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/gpuShadow.d.ts +65 -4
- package/dist/gpuShadow.js +203 -33
- package/package.json +1 -1
package/dist/gpuShadow.d.ts
CHANGED
|
@@ -456,6 +456,45 @@ export declare function carveFace(mask: number, corner: readonly [number, number
|
|
|
456
456
|
export declare function bevelClass(mask: number, bevel: number, concave: number,
|
|
457
457
|
/** Subdivision — the section boundaries move with it. See buildBevelCube on the shadowing. */
|
|
458
458
|
BEVEL_SEG?: number): number;
|
|
459
|
+
/**
|
|
460
|
+
* ── FACE CULLING ────────────────────────────────────────────────────────────────────────────────
|
|
461
|
+
*
|
|
462
|
+
* A surface voxel draws all six of its faces today — 36 vertices — and on a heightfield five of them
|
|
463
|
+
* are buried in solid rock. Measured at N=256 in all-mine-sky, terrain is 61 % of the frame (14.6 ms
|
|
464
|
+
* of 24.1) while being only ~17 % of its triangles, so the cost tracks VERTEX COUNT rather than
|
|
465
|
+
* triangle count, and five sixths of that vertex count is invisible.
|
|
466
|
+
*
|
|
467
|
+
* This drops the buried faces. It needs the neighbour mask instances already carry for AO, so it is
|
|
468
|
+
* free on the GPU and NOT free in the mesher — computing `occ` is the trade the caller opts into.
|
|
469
|
+
*
|
|
470
|
+
* ⚠️ THE FACE ORDER IS buildCube's, AND IT IS LOAD-BEARING. Vertices 0..35 are six faces of six
|
|
471
|
+
* corners in the order +X, −X, +Y, −Y, +Z, −Z, so vertex `vi` belongs to face `vi / 6`. The remap
|
|
472
|
+
* below rewrites `vi` to the vi/6-th EXPOSED face, which only works because that layout is a flat
|
|
473
|
+
* face-major array. See buildCube's "FACE QUADS ARE EMITTED FIRST" note.
|
|
474
|
+
*
|
|
475
|
+
* ⚠️ AND IT IS MUTUALLY EXCLUSIVE WITH THE BEVEL. A beveled cube's bands, facets and fillets span
|
|
476
|
+
* the EDGE BETWEEN two faces, so "drop face 3" is not a well-defined operation on it — the geometry
|
|
477
|
+
* that rounds an edge belongs to neither face alone. `frame()` ignores face culling whenever a bevel
|
|
478
|
+
* is set rather than silently producing holes.
|
|
479
|
+
*/
|
|
480
|
+
export declare const FACE_DIRS: readonly (readonly [number, number, number])[];
|
|
481
|
+
/** Is face `d` open to air? A SET bit means the neighbour is SOLID — see occAt. */
|
|
482
|
+
export declare function faceExposed(mask: number, d: number): boolean;
|
|
483
|
+
/** How many faces this instance actually has to draw. mask 0 is the "unknown" sentinel ⇒ all six. */
|
|
484
|
+
export declare function exposedFaceCount(mask: number): number;
|
|
485
|
+
/**
|
|
486
|
+
* Vertex counts for the three face-cull buckets, and the classifier that picks one.
|
|
487
|
+
*
|
|
488
|
+
* ⚠️ SAME ASYMMETRY AS bevelClass: TOO HIGH ONLY WASTES WORK, TOO LOW DELETES GEOMETRY. A bucket
|
|
489
|
+
* whose count is above the instance's need draws degenerate (centre-collapsed) triangles; one below
|
|
490
|
+
* it silently drops a visible face. `faceCull.test.mts` holds the boundary.
|
|
491
|
+
*
|
|
492
|
+
* 6 / 18 / 36 rather than an even split because the distribution is not even: on a heightfield the
|
|
493
|
+
* overwhelming majority of surface voxels expose exactly ONE face, so the cheapest bucket is worth
|
|
494
|
+
* making as cheap as it can be — a 6x cut for the common case.
|
|
495
|
+
*/
|
|
496
|
+
export declare const FACE_CULL_BUCKET_VERTS: readonly number[];
|
|
497
|
+
export declare function faceCullClass(mask: number): number;
|
|
459
498
|
export declare function bevelVertex(corner: readonly [number, number, number], home: readonly [number, number, number], mask: number, bevel: number, role?: number, axis?: readonly [number, number, number],
|
|
460
499
|
/** The CONCAVE radius. Separate from `bevel` (the convex one) so the two halves of the rounding
|
|
461
500
|
* can be turned up, down and off independently — the only way to tell which one a defect is in.
|
|
@@ -549,6 +588,7 @@ export declare class GpuShadowRenderer {
|
|
|
549
588
|
private matCutoff;
|
|
550
589
|
private eye;
|
|
551
590
|
private bevel;
|
|
591
|
+
private faceCull;
|
|
552
592
|
private fillet;
|
|
553
593
|
private alphaPipe;
|
|
554
594
|
private alphaBGL;
|
|
@@ -684,12 +724,33 @@ export declare class GpuShadowRenderer {
|
|
|
684
724
|
* without a mask (`occ` absent ⇒ 0, the "unknown" sentinel) is never beveled, which is why the far
|
|
685
725
|
* field, sky-body shells, ships, creatures and props stay sharp with no work on their part.
|
|
686
726
|
*
|
|
687
|
-
* ⚠️ NOT FREE WHILE IT IS ON
|
|
688
|
-
* the CAMERA pass
|
|
689
|
-
*
|
|
690
|
-
*
|
|
727
|
+
* ⚠️ NOT FREE WHILE IT IS ON, AND THE OLD "132 VERTICES" NUMBER HERE IS DEAD. A non-zero amount
|
|
728
|
+
* moves the CAMERA pass off the 36-vertex sharp cube and onto three bucketed indirect draws of
|
|
729
|
+
* 180 / 420 / 588 vertices — 60, 140 and 196 triangles — with `bevelClass` sorting each instance
|
|
730
|
+
* into the cheapest bucket that still contains all of its geometry. On a terrain heightfield that
|
|
731
|
+
* puts ~45 % in the first bucket and ~9 % in the last, so the mean is ~9x the sharp cube rather
|
|
732
|
+
* than the 16x the worst bucket would suggest. The shadow pass keeps the sharp cube either way.
|
|
733
|
+
* At 0 all three buckets collapse back to 36, so leaving this alone costs exactly what it cost
|
|
734
|
+
* before the feature existed — and that is what an unrelated lab measuring triangle counts is
|
|
735
|
+
* actually drawing, which is worth knowing before attributing a frame to "beveled cubes".
|
|
691
736
|
*/
|
|
692
737
|
setBevel(amount: number, concave?: number): void;
|
|
738
|
+
/**
|
|
739
|
+
* DRAW ONLY THE FACES THAT ARE OPEN TO AIR, per instance, from the `occ` neighbour mask.
|
|
740
|
+
*
|
|
741
|
+
* A surface voxel costs 36 vertices today and needs 6 of them on a flat heightfield: the other
|
|
742
|
+
* five faces are buried in solid rock and rasterize to nothing. With this on the cull pass sorts
|
|
743
|
+
* instances into 6 / 18 / 36-vertex buckets by how many faces are actually exposed.
|
|
744
|
+
*
|
|
745
|
+
* ⚠️ IT NEEDS A REAL `occ` MASK AND SILENTLY DOES NOTHING WITHOUT ONE. `occ` absent ⇒ 0 ⇒ the
|
|
746
|
+
* "unknown" sentinel ⇒ all six faces, exactly as today — which is the safe direction, and also
|
|
747
|
+
* means a caller who turns this on without teaching their mesher to fill `occ` will measure no
|
|
748
|
+
* change and conclude the feature does not work. Computing that mask is not free; it is the
|
|
749
|
+
* trade.
|
|
750
|
+
*
|
|
751
|
+
* ⚠️ AND IT IS IGNORED WHILE A BEVEL IS SET. See `frame()` — the two are mutually exclusive.
|
|
752
|
+
*/
|
|
753
|
+
setFaceCull(on: boolean): void;
|
|
693
754
|
setAtlas(tex: GPUTexture, layout: AtlasLayout): void;
|
|
694
755
|
/** Static scene: sizes the buffer to exactly these instances. */
|
|
695
756
|
setScene(instances: CubeInstance[]): void;
|
package/dist/gpuShadow.js
CHANGED
|
@@ -283,8 +283,9 @@ export const ROLE_BASE = 8; // where ONE convex edge lands among TWO concave one
|
|
|
283
283
|
// that touch still meet exactly as they did before this existed.
|
|
284
284
|
//
|
|
285
285
|
// FREE WHEN UNKNOWN — occ == 0 (every non-terrain caller: the far field, sky bodies, the ship,
|
|
286
|
-
// creatures, props, the place ghost) makes every gate false, so all
|
|
287
|
-
// sharp corners and the geometry is identical to buildCube()'s.
|
|
286
|
+
// creatures, props, the place ghost) makes every gate false, so all BEVEL_VERTS land on the 8
|
|
287
|
+
// sharp corners and the geometry is identical to buildCube()'s. (It said "132" while the cube was
|
|
288
|
+
// a chamfer only; the fillet, carve and base sections have taken it to 588 since.)
|
|
288
289
|
//
|
|
289
290
|
// ⚠️ FACE QUADS ARE EMITTED FIRST, AND THAT ORDERING IS LOAD-BEARING. Vertices 0..35 are byte-for-
|
|
290
291
|
// byte buildCube()'s cube, which is what lets the camera pass draw 36 instead of 132 whenever the
|
|
@@ -1011,6 +1012,67 @@ BEVEL_SEG = bevelSegDefault) {
|
|
|
1011
1012
|
return S.facets;
|
|
1012
1013
|
return S.fans;
|
|
1013
1014
|
}
|
|
1015
|
+
/**
|
|
1016
|
+
* ── FACE CULLING ────────────────────────────────────────────────────────────────────────────────
|
|
1017
|
+
*
|
|
1018
|
+
* A surface voxel draws all six of its faces today — 36 vertices — and on a heightfield five of them
|
|
1019
|
+
* are buried in solid rock. Measured at N=256 in all-mine-sky, terrain is 61 % of the frame (14.6 ms
|
|
1020
|
+
* of 24.1) while being only ~17 % of its triangles, so the cost tracks VERTEX COUNT rather than
|
|
1021
|
+
* triangle count, and five sixths of that vertex count is invisible.
|
|
1022
|
+
*
|
|
1023
|
+
* This drops the buried faces. It needs the neighbour mask instances already carry for AO, so it is
|
|
1024
|
+
* free on the GPU and NOT free in the mesher — computing `occ` is the trade the caller opts into.
|
|
1025
|
+
*
|
|
1026
|
+
* ⚠️ THE FACE ORDER IS buildCube's, AND IT IS LOAD-BEARING. Vertices 0..35 are six faces of six
|
|
1027
|
+
* corners in the order +X, −X, +Y, −Y, +Z, −Z, so vertex `vi` belongs to face `vi / 6`. The remap
|
|
1028
|
+
* below rewrites `vi` to the vi/6-th EXPOSED face, which only works because that layout is a flat
|
|
1029
|
+
* face-major array. See buildCube's "FACE QUADS ARE EMITTED FIRST" note.
|
|
1030
|
+
*
|
|
1031
|
+
* ⚠️ AND IT IS MUTUALLY EXCLUSIVE WITH THE BEVEL. A beveled cube's bands, facets and fillets span
|
|
1032
|
+
* the EDGE BETWEEN two faces, so "drop face 3" is not a well-defined operation on it — the geometry
|
|
1033
|
+
* that rounds an edge belongs to neither face alone. `frame()` ignores face culling whenever a bevel
|
|
1034
|
+
* is set rather than silently producing holes.
|
|
1035
|
+
*/
|
|
1036
|
+
export const FACE_DIRS = [
|
|
1037
|
+
[1, 0, 0],
|
|
1038
|
+
[-1, 0, 0],
|
|
1039
|
+
[0, 1, 0],
|
|
1040
|
+
[0, -1, 0],
|
|
1041
|
+
[0, 0, 1],
|
|
1042
|
+
[0, 0, -1],
|
|
1043
|
+
];
|
|
1044
|
+
/** Is face `d` open to air? A SET bit means the neighbour is SOLID — see occAt. */
|
|
1045
|
+
export function faceExposed(mask, d) {
|
|
1046
|
+
const o = FACE_DIRS[d];
|
|
1047
|
+
const idx = o[0] + 1 + (o[1] + 1) * 3 + (o[2] + 1) * 9;
|
|
1048
|
+
return ((mask >>> idx) & 1) === 0;
|
|
1049
|
+
}
|
|
1050
|
+
/** How many faces this instance actually has to draw. mask 0 is the "unknown" sentinel ⇒ all six. */
|
|
1051
|
+
export function exposedFaceCount(mask) {
|
|
1052
|
+
if (mask === 0)
|
|
1053
|
+
return 6;
|
|
1054
|
+
let n = 0;
|
|
1055
|
+
for (let d = 0; d < 6; d++)
|
|
1056
|
+
if (faceExposed(mask, d))
|
|
1057
|
+
n++;
|
|
1058
|
+
return n;
|
|
1059
|
+
}
|
|
1060
|
+
/**
|
|
1061
|
+
* Vertex counts for the three face-cull buckets, and the classifier that picks one.
|
|
1062
|
+
*
|
|
1063
|
+
* ⚠️ SAME ASYMMETRY AS bevelClass: TOO HIGH ONLY WASTES WORK, TOO LOW DELETES GEOMETRY. A bucket
|
|
1064
|
+
* whose count is above the instance's need draws degenerate (centre-collapsed) triangles; one below
|
|
1065
|
+
* it silently drops a visible face. `faceCull.test.mts` holds the boundary.
|
|
1066
|
+
*
|
|
1067
|
+
* 6 / 18 / 36 rather than an even split because the distribution is not even: on a heightfield the
|
|
1068
|
+
* overwhelming majority of surface voxels expose exactly ONE face, so the cheapest bucket is worth
|
|
1069
|
+
* making as cheap as it can be — a 6x cut for the common case.
|
|
1070
|
+
*/
|
|
1071
|
+
export const FACE_CULL_BUCKET_VERTS = [6, 18, 36];
|
|
1072
|
+
export function faceCullClass(mask) {
|
|
1073
|
+
const n = exposedFaceCount(mask);
|
|
1074
|
+
return n <= 1 ? 6 : n <= 3 ? 18 : 36;
|
|
1075
|
+
}
|
|
1014
1076
|
// THE RIM TURN IS GONE, AND ITS ABSENCE IS THE POINT. rimTurn() clipped a face's corner and grew a
|
|
1015
1077
|
// one-axis facet wherever a surface had an L-shaped bite taken out of it — a pit's rim turning a
|
|
1016
1078
|
// corner. That configuration is a CONCAVE corner, and concave corners are now FILLED rather than
|
|
@@ -1511,6 +1573,39 @@ fn occAt(mask: u32, o: vec3<f32>) -> f32 {
|
|
|
1511
1573
|
let idx = u32((o.x + 1.0) + (o.y + 1.0) * 3.0 + (o.z + 1.0) * 9.0);
|
|
1512
1574
|
return f32((mask >> idx) & 1u);
|
|
1513
1575
|
}
|
|
1576
|
+
// ── FACE CULLING ────────────────────────────────────────────────────────────────────────────────
|
|
1577
|
+
// Mirrors faceExposed / exposedFaceCount / faceCullRemap in TS — ONE source, spliced into both the
|
|
1578
|
+
// draw and cull shaders, so there is no third copy. See the FACE CULLING block above buildCube.
|
|
1579
|
+
const FACE_N = array<vec3<f32>, 6>(
|
|
1580
|
+
vec3(1.0, 0.0, 0.0), vec3(-1.0, 0.0, 0.0),
|
|
1581
|
+
vec3(0.0, 1.0, 0.0), vec3(0.0, -1.0, 0.0),
|
|
1582
|
+
vec3(0.0, 0.0, 1.0), vec3(0.0, 0.0, -1.0),
|
|
1583
|
+
);
|
|
1584
|
+
fn faceExposedW(mask: u32, d: u32) -> bool {
|
|
1585
|
+
return occAt(mask, FACE_N[d]) < 0.5; // a SET bit is a SOLID neighbour
|
|
1586
|
+
}
|
|
1587
|
+
fn exposedFaceCountW(mask: u32) -> u32 {
|
|
1588
|
+
if (mask == 0u) { return 6u; } // unknown ⇒ draw everything, same sentinel as the bevel
|
|
1589
|
+
var n = 0u;
|
|
1590
|
+
for (var d = 0u; d < 6u; d = d + 1u) { if (faceExposedW(mask, d)) { n = n + 1u; } }
|
|
1591
|
+
return n;
|
|
1592
|
+
}
|
|
1593
|
+
// Rewrite a sharp-cube vertex index onto the (vi / 6)-th EXPOSED face, keeping its corner. Returns
|
|
1594
|
+
// 0xffffffff when this slot has no face left — the draw's bucket is sized for the WORST instance in
|
|
1595
|
+
// it, so most instances have slots to spare and the vertex shader collapses those to a point.
|
|
1596
|
+
fn faceCullRemap(mask: u32, vi: u32) -> u32 {
|
|
1597
|
+
let slot = vi / 6u;
|
|
1598
|
+
let corner = vi % 6u;
|
|
1599
|
+
var seen = 0u;
|
|
1600
|
+
for (var d = 0u; d < 6u; d = d + 1u) {
|
|
1601
|
+
if (faceExposedW(mask, d)) {
|
|
1602
|
+
if (seen == slot) { return d * 6u + corner; }
|
|
1603
|
+
seen = seen + 1u;
|
|
1604
|
+
}
|
|
1605
|
+
}
|
|
1606
|
+
return 0xffffffffu;
|
|
1607
|
+
}
|
|
1608
|
+
|
|
1514
1609
|
// ── ROUNDED EDGES ───────────────────────────────────────────────────────────────────────────────
|
|
1515
1610
|
// Is the edge between faces a and b exposed to air or water — and so beveled? The two faces AND
|
|
1516
1611
|
// the cell diagonally between them must all be clear.
|
|
@@ -2082,11 +2177,26 @@ fn cull(@builtin(global_invocation_id) gid: vec3<u32>) {
|
|
|
2082
2177
|
// ${V} vertices — see CULL_BUCKETS.
|
|
2083
2178
|
//
|
|
2084
2179
|
// ⚠️ ROUNDING UP IS FREE; ROUNDING DOWN DELETES TRIANGLES. bevelClass.test.mts is what holds that.
|
|
2180
|
+
// cc.lod.y SELECTS THE BUCKETING MODE: 0 none, 1 bevel sections, 2 exposed faces. One dial rather
|
|
2181
|
+
// than two flags because the modes are mutually exclusive — a beveled cube cannot drop a face,
|
|
2182
|
+
// since the geometry that rounds an edge belongs to two of them and to neither alone.
|
|
2183
|
+
//
|
|
2184
|
+
// ⚠️ TEST 2 BEFORE 1. Written as ascending thresholds the bevel branch swallows mode 2 (2 is also
|
|
2185
|
+
// greater than 0.5), and face culling silently runs the bevel classifier instead — which returns
|
|
2186
|
+
// section counts, so the buckets would be sized 180/420/588 for a 36-vertex cube.
|
|
2187
|
+
//
|
|
2188
|
+
// ⚠️ ROUNDING UP IS FREE; ROUNDING DOWN DELETES TRIANGLES. bevelClass.test.mts and
|
|
2189
|
+
// faceCull.test.mts are what hold that, one per mode.
|
|
2085
2190
|
var b = 0u;
|
|
2086
2191
|
if (cc.lod.y > 0.5) {
|
|
2087
2192
|
let mask = select(u32(inst.aux2.x) | (u32(inst.aux2.y) << 14u), 0u, inst.aux.x < -1.5);
|
|
2088
|
-
|
|
2089
|
-
|
|
2193
|
+
if (cc.lod.y > 1.5) {
|
|
2194
|
+
let n = exposedFaceCountW(mask);
|
|
2195
|
+
if (n > 3u) { b = 2u; } else if (n > 1u) { b = 1u; }
|
|
2196
|
+
} else {
|
|
2197
|
+
let cls = bevelClass(mask, cc.lod.z, cc.lod.w);
|
|
2198
|
+
if (cls > ${S.fillet}u) { b = 2u; } else if (cls > ${S.fans}u) { b = 1u; }
|
|
2199
|
+
}
|
|
2090
2200
|
}
|
|
2091
2201
|
let slot = atomicAdd(&args[b].ic, 1u);
|
|
2092
2202
|
visible[b * u32(cc.lod.x) + slot] = i;
|
|
@@ -2193,33 +2303,54 @@ fn faceVec(code: f32) -> vec3<f32> {
|
|
|
2193
2303
|
// THE BEVELED CUBE. cam.eye.w is the bevel amount; at 0 — every caller that has not opted in —
|
|
2194
2304
|
// bevelPos returns the sharp corner and this is buildCube()'s geometry exactly. The draw is only
|
|
2195
2305
|
// 36 vertices long in that case anyway (see the indirect args), so the tail is never even reached.
|
|
2196
|
-
|
|
2306
|
+
// ── FACE CULLING: REWRITE THE VERTEX INDEX ONTO AN EXPOSED FACE ──────────────────────────────
|
|
2307
|
+
//
|
|
2308
|
+
// cam.csm.w is the switch. Gated on the bevel being OFF as well, because the bevel's bands and
|
|
2309
|
+
// facets span the edge BETWEEN two faces and cannot be assigned to either — frame() already
|
|
2310
|
+
// refuses to combine them, and repeating the condition here means a caller who reaches past it
|
|
2311
|
+
// gets today's cube rather than a cube with holes in it.
|
|
2312
|
+
//
|
|
2313
|
+
// ⚠️ vv, NOT vi, FROM HERE DOWN. Every table lookup — corner, home, role, axis — has to use
|
|
2314
|
+
// the rewritten index or the position comes from one face and its normal from another.
|
|
2315
|
+
var vv = vi;
|
|
2316
|
+
var faceless = false; // this slot has no exposed face left → collapse it below
|
|
2317
|
+
if (cam.csm.w > 0.5 && cam.eye.w <= 0.0 && cam.csm.z <= 0.0 && mask != 0u) {
|
|
2318
|
+
let r = faceCullRemap(mask, vi);
|
|
2319
|
+
if (r == 0xffffffffu) { faceless = true; } else { vv = r; }
|
|
2320
|
+
}
|
|
2321
|
+
let sharp = BEV_CORNER[vv];
|
|
2197
2322
|
// ⚠️ THE CORNER CARVE TAKES ITS home FROM THE MASK, not from the table. Its three vertices belong
|
|
2198
2323
|
// to whichever of the three faces at that corner is the EXPOSED one, and the table cannot know
|
|
2199
2324
|
// which — so BEV_HOME holds a placeholder and carveFace() resolves it here, before anything reads
|
|
2200
2325
|
// it. Left as the placeholder it would texture a patch on a grass top with the dirt SIDE tile at
|
|
2201
2326
|
// every notch corner, which is the "doesn't match the surrounding textures" report all over again.
|
|
2202
2327
|
// All three vertices resolve the SAME face, so objN stays flat and the patch keeps one tile.
|
|
2203
|
-
var home = BEV_HOME[
|
|
2328
|
+
var home = BEV_HOME[vv];
|
|
2204
2329
|
// ⚠️ RESOLVED ONCE, USED TWICE. Both of these are needed again further down for the SHADING
|
|
2205
2330
|
// normal, and each walks up to 27 neighbours of the mask through branchy per-axis loops — the
|
|
2206
2331
|
// most expensive thing this shader does. Computing them a second time down there cost the carve
|
|
2207
2332
|
// and base sections about a third of their time (tools/bevelBench.mjs).
|
|
2208
2333
|
var cf = vec3(0.0, 0.0, 0.0);
|
|
2209
2334
|
var gb = vec3(0.0, 0.0, 0.0);
|
|
2210
|
-
if (BEV_ROLE[
|
|
2211
|
-
if (BEV_ROLE[
|
|
2212
|
-
if (BEV_ROLE[
|
|
2335
|
+
if (BEV_ROLE[vv] == ${ROLE_CARVE}.0) { cf = carveFace(mask, sharp); }
|
|
2336
|
+
if (BEV_ROLE[vv] == ${ROLE_BASE}.0) { gb = baseOwner(mask, sharp); }
|
|
2337
|
+
if (BEV_ROLE[vv] == ${ROLE_CARVE}.0) {
|
|
2213
2338
|
if (length(cf) > 0.5) { home = cf; }
|
|
2214
2339
|
}
|
|
2215
2340
|
// ⚠️ AND THE BASE PATCH TOO. It belongs to the FLAT SURFACE it grows out of, whose outward normal
|
|
2216
2341
|
// is -g, and the table cannot know which axis that is. Left as the placeholder it shipped as black
|
|
2217
2342
|
// wedges once already.
|
|
2218
|
-
if (BEV_ROLE[
|
|
2343
|
+
if (BEV_ROLE[vv] == ${ROLE_BASE}.0) {
|
|
2219
2344
|
if (length(gb) > 0.5) { home = -gb; } // the TEXTURE is the flat surface's; the NORMAL is below
|
|
2220
2345
|
}
|
|
2221
2346
|
|
|
2222
|
-
|
|
2347
|
+
// ⚠️ COLLAPSED TO THE CENTRE, NOT DISCARDED. A face-cull bucket is sized for the WORST instance
|
|
2348
|
+
// in it — a 3-face bucket still runs 18 vertices for a 2-face cube — so the spare slots have to
|
|
2349
|
+
// go somewhere harmless. Putting all six of a slot's corners on the cube centre makes its two
|
|
2350
|
+
// triangles zero-area, which the rasterizer drops for free and which cannot show a seam. There is
|
|
2351
|
+
// no discard available in a vertex shader, and clipping them out by w would cost more.
|
|
2352
|
+
var local = bevelPos(mask, sharp, home, cam.eye.w, BEV_ROLE[vv], BEV_AXIS[vv], cam.csm.z);
|
|
2353
|
+
if (faceless) { local = vec3(0.0, 0.0, 0.0); }
|
|
2223
2354
|
let world = bodyPos(bd, inst.pos.xyz + qrot(inst.rot, local * inst.scl.xyz));
|
|
2224
2355
|
var o: VOut;
|
|
2225
2356
|
o.clip = cam.camVP * vec4(world, 1.0);
|
|
@@ -2237,9 +2368,9 @@ fn faceVec(code: f32) -> vec3<f32> {
|
|
|
2237
2368
|
// ⚠️ THE FILLET'S NORMAL SWEEPS THE ARC. At the foot it is the face's own normal and at the crest
|
|
2238
2369
|
// the bisector — which is what one flat quad's two rows used to carry. Subdivided, every row in
|
|
2239
2370
|
// between needs its own or the strip is a curve shaded as a fan of flat facets.
|
|
2240
|
-
if (BEV_ROLE[
|
|
2241
|
-
let fa3 = filletArc(BEV_AXIS[
|
|
2242
|
-
shade = normalize(home * fa3.y - sign(BEV_AXIS[
|
|
2371
|
+
if (BEV_ROLE[vv] == ${ROLE_FMID}.0 || BEV_ROLE[vv] == ${ROLE_FRIM}.0) {
|
|
2372
|
+
let fa3 = filletArc(BEV_AXIS[vv], ${seg}.0);
|
|
2373
|
+
shade = normalize(home * fa3.y - sign(BEV_AXIS[vv]) * fa3.x);
|
|
2243
2374
|
}
|
|
2244
2375
|
// ⚠️ A BAND'S NORMAL SWEEPS ACROSS THE STRIP. That sweep is the only reason a flat 45° chamfer
|
|
2245
2376
|
// reads as a cylinder — the geometry is a plane and the shading is a curve. It used to happen for
|
|
@@ -2257,7 +2388,7 @@ fn faceVec(code: f32) -> vec3<f32> {
|
|
|
2257
2388
|
// the line that declares it — WGSL use-before-declaration, the shader failed to compile, the
|
|
2258
2389
|
// pipeline never built and the screen went black. bevelParity.spec.ts could not see it: it
|
|
2259
2390
|
// compiles COMMON_WGSL into a COMPUTE shader and never builds the vertex stage at all.
|
|
2260
|
-
if (BEV_ROLE[
|
|
2391
|
+
if (BEV_ROLE[vv] == ${ROLE_BASE}.0) {
|
|
2261
2392
|
let gb2 = gb; // resolved once at the top; see there
|
|
2262
2393
|
if (length(gb2) > 0.5) {
|
|
2263
2394
|
let hb = -gb2;
|
|
@@ -2272,7 +2403,7 @@ fn faceVec(code: f32) -> vec3<f32> {
|
|
|
2272
2403
|
if (dot(cross(b1, b2), hb) < 0.0) { fa2 = b2; fb2 = b1; }
|
|
2273
2404
|
// The arc points sweep their normal from one wall to the other, exactly as the band they meet
|
|
2274
2405
|
// does — a chord's two end normals across a subdivided arc is the flat-shaded facet again.
|
|
2275
|
-
let ib = i32(BEV_AXIS[
|
|
2406
|
+
let ib = i32(BEV_AXIS[vv].x + 0.5);
|
|
2276
2407
|
if (ib == 1) { shade = normalize(hb + fb2); }
|
|
2277
2408
|
else if (ib >= 2 && ib <= ${2 + seg}) {
|
|
2278
2409
|
let tb = (f32(ib - 2) / ${seg}.0) * 1.5707963267948966;
|
|
@@ -2282,9 +2413,9 @@ fn faceVec(code: f32) -> vec3<f32> {
|
|
|
2282
2413
|
else { shade = hb; }
|
|
2283
2414
|
}
|
|
2284
2415
|
}
|
|
2285
|
-
if (BEV_ROLE[
|
|
2286
|
-
let pb = sign(BEV_AXIS[
|
|
2287
|
-
let stb = round(max(max(abs(BEV_AXIS[
|
|
2416
|
+
if (BEV_ROLE[vv] == ${ROLE_BAND}.0) {
|
|
2417
|
+
let pb = sign(BEV_AXIS[vv]);
|
|
2418
|
+
let stb = round(max(max(abs(BEV_AXIS[vv].x), abs(BEV_AXIS[vv].y)), abs(BEV_AXIS[vv].z))) - 1.0;
|
|
2288
2419
|
let thb = (stb / ${seg}.0) * 1.5707963267948966;
|
|
2289
2420
|
shade = normalize(home * cos(thb) + pb * sin(thb));
|
|
2290
2421
|
}
|
|
@@ -2292,7 +2423,7 @@ fn faceVec(code: f32) -> vec3<f32> {
|
|
|
2292
2423
|
// TEXTURE, and at one segment those three normals sweeping across one triangle is what made a flat
|
|
2293
2424
|
// chamfer corner read round. Once the patch is a real octant, the honest normal is the sphere's —
|
|
2294
2425
|
// the point's own direction from the centre — or the geometry is round and the shading is not.
|
|
2295
|
-
if (BEV_ROLE[
|
|
2426
|
+
if (BEV_ROLE[vv] == ${ROLE_FACET}.0 && cam.eye.w > 0.0) {
|
|
2296
2427
|
let ctr = sharp - sign(sharp) * cam.eye.w;
|
|
2297
2428
|
let dv = local - ctr;
|
|
2298
2429
|
if (length(dv) > 1e-5) { shade = normalize(dv); }
|
|
@@ -2307,7 +2438,7 @@ fn faceVec(code: f32) -> vec3<f32> {
|
|
|
2307
2438
|
// Each vertex takes the normal of the thing it MEETS, so the patch is continuous with its
|
|
2308
2439
|
// neighbours: the two on the exposed face take that face; the two band ends take the side face
|
|
2309
2440
|
// their band grows from; the crest takes the bisector, which is what the fillet's own crest uses.
|
|
2310
|
-
if (BEV_ROLE[
|
|
2441
|
+
if (BEV_ROLE[vv] == ${ROLE_CARVE}.0) {
|
|
2311
2442
|
if (length(cf) > 0.5) { // resolved once at the top; see there
|
|
2312
2443
|
let ipc = sign(sharp) * (vec3(1.0, 1.0, 1.0) - abs(cf));
|
|
2313
2444
|
var e1 = vec3(0.0, 0.0, 0.0);
|
|
@@ -2318,7 +2449,7 @@ fn faceVec(code: f32) -> vec3<f32> {
|
|
|
2318
2449
|
var fa = e1;
|
|
2319
2450
|
var fb = e2;
|
|
2320
2451
|
if (dot(cross(e1, e2), cf) > 0.0) { fa = e2; fb = e1; }
|
|
2321
|
-
let ax = BEV_AXIS[
|
|
2452
|
+
let ax = BEV_AXIS[vv];
|
|
2322
2453
|
// sweep from the exposed face at the rim to the side face the neighbour's band grows out of,
|
|
2323
2454
|
// which is what makes the patch continuous with what surrounds it instead of a flat facet
|
|
2324
2455
|
if (ax.z > 0.5) { shade = normalize(fa + fb); }
|
|
@@ -2764,6 +2895,7 @@ export class GpuShadowRenderer {
|
|
|
2764
2895
|
matCutoff = 28; // world units; parallax fades out before here
|
|
2765
2896
|
eye = new Float32Array(3);
|
|
2766
2897
|
bevel = 0; // see setBevel — 0 keeps the sharp cube exactly, and draws only 36 vertices
|
|
2898
|
+
faceCull = false; // see setFaceCull — off is today's all-six-faces cube, bit for bit
|
|
2767
2899
|
fillet = 0; // the CONCAVE radius, set separately so the two halves can be judged apart
|
|
2768
2900
|
alphaPipe;
|
|
2769
2901
|
alphaBGL; // kept so the overlay buffer can grow and rebind on demand
|
|
@@ -3406,15 +3538,38 @@ export class GpuShadowRenderer {
|
|
|
3406
3538
|
* without a mask (`occ` absent ⇒ 0, the "unknown" sentinel) is never beveled, which is why the far
|
|
3407
3539
|
* field, sky-body shells, ships, creatures and props stay sharp with no work on their part.
|
|
3408
3540
|
*
|
|
3409
|
-
* ⚠️ NOT FREE WHILE IT IS ON
|
|
3410
|
-
* the CAMERA pass
|
|
3411
|
-
*
|
|
3412
|
-
*
|
|
3541
|
+
* ⚠️ NOT FREE WHILE IT IS ON, AND THE OLD "132 VERTICES" NUMBER HERE IS DEAD. A non-zero amount
|
|
3542
|
+
* moves the CAMERA pass off the 36-vertex sharp cube and onto three bucketed indirect draws of
|
|
3543
|
+
* 180 / 420 / 588 vertices — 60, 140 and 196 triangles — with `bevelClass` sorting each instance
|
|
3544
|
+
* into the cheapest bucket that still contains all of its geometry. On a terrain heightfield that
|
|
3545
|
+
* puts ~45 % in the first bucket and ~9 % in the last, so the mean is ~9x the sharp cube rather
|
|
3546
|
+
* than the 16x the worst bucket would suggest. The shadow pass keeps the sharp cube either way.
|
|
3547
|
+
* At 0 all three buckets collapse back to 36, so leaving this alone costs exactly what it cost
|
|
3548
|
+
* before the feature existed — and that is what an unrelated lab measuring triangle counts is
|
|
3549
|
+
* actually drawing, which is worth knowing before attributing a frame to "beveled cubes".
|
|
3413
3550
|
*/
|
|
3414
3551
|
setBevel(amount, concave = amount) {
|
|
3415
3552
|
this.bevel = Math.max(0, amount);
|
|
3416
3553
|
this.fillet = Math.max(0, concave);
|
|
3417
3554
|
}
|
|
3555
|
+
/**
|
|
3556
|
+
* DRAW ONLY THE FACES THAT ARE OPEN TO AIR, per instance, from the `occ` neighbour mask.
|
|
3557
|
+
*
|
|
3558
|
+
* A surface voxel costs 36 vertices today and needs 6 of them on a flat heightfield: the other
|
|
3559
|
+
* five faces are buried in solid rock and rasterize to nothing. With this on the cull pass sorts
|
|
3560
|
+
* instances into 6 / 18 / 36-vertex buckets by how many faces are actually exposed.
|
|
3561
|
+
*
|
|
3562
|
+
* ⚠️ IT NEEDS A REAL `occ` MASK AND SILENTLY DOES NOTHING WITHOUT ONE. `occ` absent ⇒ 0 ⇒ the
|
|
3563
|
+
* "unknown" sentinel ⇒ all six faces, exactly as today — which is the safe direction, and also
|
|
3564
|
+
* means a caller who turns this on without teaching their mesher to fill `occ` will measure no
|
|
3565
|
+
* change and conclude the feature does not work. Computing that mask is not free; it is the
|
|
3566
|
+
* trade.
|
|
3567
|
+
*
|
|
3568
|
+
* ⚠️ AND IT IS IGNORED WHILE A BEVEL IS SET. See `frame()` — the two are mutually exclusive.
|
|
3569
|
+
*/
|
|
3570
|
+
setFaceCull(on) {
|
|
3571
|
+
this.faceCull = on;
|
|
3572
|
+
}
|
|
3418
3573
|
setAtlas(tex, layout) {
|
|
3419
3574
|
this.atlasView = tex.createView({ dimension: '2d-array' });
|
|
3420
3575
|
const ids = Object.keys(layout.tiles).map(Number);
|
|
@@ -3610,7 +3765,7 @@ export class GpuShadowRenderer {
|
|
|
3610
3765
|
mp.end();
|
|
3611
3766
|
}
|
|
3612
3767
|
/** Write a frustum's cull uniform: 6 planes + count + this frustum's VP + occlusion params (occ on the camera pass). */
|
|
3613
|
-
writeCull(buf, vp, occlude, bucket =
|
|
3768
|
+
writeCull(buf, vp, occlude, bucket = 0) {
|
|
3614
3769
|
const planes = frustumPlanes(vp);
|
|
3615
3770
|
const u = new Float32Array(52); // planes[24] + cnt[4] + camVP[16] + occ[4] + lod[4]
|
|
3616
3771
|
for (let p = 0; p < 6; p++) {
|
|
@@ -3629,7 +3784,7 @@ export class GpuShadowRenderer {
|
|
|
3629
3784
|
// sort them by, and with bucketing off every survivor lands in bucket 0 — which is the whole
|
|
3630
3785
|
// list, exactly as before.
|
|
3631
3786
|
u[48] = this.visStride() / 4;
|
|
3632
|
-
u[49] = bucket
|
|
3787
|
+
u[49] = bucket; // 0 none · 1 bevel sections · 2 exposed faces
|
|
3633
3788
|
u[50] = this.bevel;
|
|
3634
3789
|
u[51] = this.fillet;
|
|
3635
3790
|
this.device.queue.writeBuffer(buf, 0, u);
|
|
@@ -3767,6 +3922,8 @@ export class GpuShadowRenderer {
|
|
|
3767
3922
|
this.cam[S + 25] = this.eye[1];
|
|
3768
3923
|
this.cam[S + 26] = this.eye[2];
|
|
3769
3924
|
this.cam[S + 22] = this.fillet; // csm.z — the CONCAVE radius, see setBevel
|
|
3925
|
+
this.cam[S + 23] =
|
|
3926
|
+
this.faceCull && !(this.bevel > 0 || this.fillet > 0) ? 1 : 0; // csm.w — face culling, see setFaceCull
|
|
3770
3927
|
this.cam[S + 27] = this.bevel; // eye.w — the CONVEX radius, see setBevel
|
|
3771
3928
|
this.device.queue.writeBuffer(this.camBuf, 0, this.cam);
|
|
3772
3929
|
// Each cascade's shadow pass reads its OWN copy with that cascade's matrix parked at lightVP[0].
|
|
@@ -3782,14 +3939,27 @@ export class GpuShadowRenderer {
|
|
|
3782
3939
|
// — main pass vs the camera frustum (+ Hi-Z occlusion), shadow pass vs the light frustum. Each dispatch atomically
|
|
3783
3940
|
// compacts its survivors into a visible list + counts them in instanceCount, which the matching drawIndirect consumes.
|
|
3784
3941
|
// ⚠️ TWO RESETS, NOT ONE, AND THE VERTEX COUNTS DIFFER ON PURPOSE. The camera pass draws the
|
|
3785
|
-
// beveled cube's
|
|
3786
|
-
// which buildBevelCube guarantees ARE the sharp cube. The
|
|
3787
|
-
// is invisible in a shadow map and there is no reason to
|
|
3942
|
+
// beveled cube's bucketed 180/420/588 vertices only while a bevel is actually set; at 0 all
|
|
3943
|
+
// three buckets draw the first 36, which buildBevelCube guarantees ARE the sharp cube. The
|
|
3944
|
+
// shadow pass always draws 36 — a 6% cut is invisible in a shadow map and there is no reason to
|
|
3945
|
+
// multiply the cascades' vertex work.
|
|
3946
|
+
//
|
|
3947
|
+
// ⚠️ SO "HOW MANY TRIANGLES IS A BOX" HAS TWO ANSWERS AND THIS LINE PICKS BETWEEN THEM. A caller
|
|
3948
|
+
// that never sets a bevel — which is every lab but terrain-relief — draws 12 triangles a box,
|
|
3949
|
+
// not the 60–196 the section table implies.
|
|
3788
3950
|
const camBevel = this.bevel > 0 || this.fillet > 0;
|
|
3951
|
+
// ⚠️ THE BEVEL WINS. Face culling drops whole faces; the bevel's bands and facets span the edge
|
|
3952
|
+
// between two of them and belong to neither, so combining the two would cut geometry that is
|
|
3953
|
+
// still visible. Rather than let a caller ask for both and get holes, a set bevel simply turns
|
|
3954
|
+
// face culling off for that frame — and the VS repeats the same condition so the two cannot
|
|
3955
|
+
// disagree about which mode is live.
|
|
3956
|
+
const faceCull = this.faceCull && !camBevel;
|
|
3789
3957
|
// With the bevel off there is nothing to classify: one bucket of 36, the other two empty.
|
|
3790
3958
|
const bucketVerts = camBevel
|
|
3791
3959
|
? [this.bev.sec.fans, this.bev.sec.fillet, this.bev.verts]
|
|
3792
|
-
:
|
|
3960
|
+
: faceCull
|
|
3961
|
+
? [...FACE_CULL_BUCKET_VERTS]
|
|
3962
|
+
: [SHARP_VERTS, SHARP_VERTS, SHARP_VERTS];
|
|
3793
3963
|
const camReset = new Uint32Array(4 * CULL_BUCKETS);
|
|
3794
3964
|
for (let b = 0; b < CULL_BUCKETS; b++)
|
|
3795
3965
|
camReset[b * 4] = bucketVerts[b];
|
|
@@ -3798,7 +3968,7 @@ export class GpuShadowRenderer {
|
|
|
3798
3968
|
lightReset[0] = SHARP_VERTS;
|
|
3799
3969
|
for (const a of this.lightArgs)
|
|
3800
3970
|
this.device.queue.writeBuffer(a, 0, lightReset);
|
|
3801
|
-
this.writeCull(this.camCull, camVP, occludeNow, camBevel);
|
|
3971
|
+
this.writeCull(this.camCull, camVP, occludeNow, camBevel ? 1 : faceCull ? 2 : 0);
|
|
3802
3972
|
for (let i = 0; i < nc; i++)
|
|
3803
3973
|
this.writeCull(this.lightCull[i], mats[i], false);
|
|
3804
3974
|
// ⚠️ THE PASS OPENS FOR EITHER REASON. It used to be gated on `this.count > 0` alone, which is
|