ugly-game 0.15.28 → 0.15.29

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.
@@ -9,6 +9,18 @@ export interface CubeInstance {
9
9
  hue: number;
10
10
  emissive?: number;
11
11
  rot?: [number, number, number, number];
12
+ /**
13
+ * WHICH FACES TO DRAW: bit d, in FACE_DIRS order (+X −X +Y −Y +Z −Z). See FACE_MASK_ALL.
14
+ *
15
+ * ⚠️ 0 IS "UNKNOWN ⇒ DRAW ALL SIX", NOT "DRAW NOTHING" — the same sentinel `occ` uses, and what
16
+ * makes this safe to adopt one producer at a time. "Draw nothing" never needs expressing: a cube
17
+ * with no visible face is dropped by the mesher and never reaches the buffer.
18
+ *
19
+ * ⚠️ NOT A SUBSTITUTE FOR `occ`, WHICH IS STILL REQUIRED IN FULL. `occ` is 27-bit OCCUPANCY and
20
+ * vertex AO reads its DIAGONALS; this is only the six axial answers culling needs, shipped rather
21
+ * than re-derived per vertex. Ignored in faceMats/patchwork mode, where the slots carry materials.
22
+ */
23
+ faceMask?: number;
12
24
  /**
13
25
  * ATLAS TILE ID, or a sentinel.
14
26
  *
@@ -489,10 +501,33 @@ BEVEL_SEG?: number): number;
489
501
  * is set rather than silently producing holes.
490
502
  */
491
503
  export declare const FACE_DIRS: readonly (readonly [number, number, number])[];
492
- /** Is face `d` open to air? A SET bit means the neighbour is SOLID — see occAt. */
493
- export declare function faceExposed(mask: number, d: number): boolean;
494
- /** How many faces this instance actually has to draw. mask 0 is the "unknown" sentinel ⇒ all six. */
495
- export declare function exposedFaceCount(mask: number): number;
504
+ /**
505
+ * THE FACE MASK: BIT d MEANS "DRAW FACE d". Six bits, in FACE_DIRS order.
506
+ *
507
+ * ⚠️ THIS IS NOT `occ`, AND THE TWO MUST NOT BE CONFLATED. `occ` is a 27-bit OCCUPANCY mask — a set
508
+ * bit is a SOLID neighbour — and it is still required in full, because vertex AO reads the DIAGONAL
509
+ * neighbours (`cornerAOAt` samples n+a1, n+a2 and n+a1+a2) and the bevel shapes edges from the same
510
+ * neighbourhood. Face culling needs only the six axial answers, and the producer already knows them:
511
+ * deriving them again per vertex in the shader meant a six-iteration loop calling `occAt`, which
512
+ * recomputes a 3D neighbour index every time, in the hottest shader in the engine.
513
+ *
514
+ * So the mesher ships what it already computed. `occ` keeps its 27 bits for AO and bevel; this
515
+ * carries the six that culling needs.
516
+ *
517
+ * ⚠️ ZERO IS "UNKNOWN ⇒ DRAW ALL SIX", NOT "DRAW NOTHING", which is the same sentinel `occ` uses.
518
+ * That is what makes the change safe to land incrementally: a producer that has not been taught to
519
+ * fill this in writes 0 and keeps its whole cube, rather than silently vanishing. "Draw nothing"
520
+ * never needs expressing — a cube with no visible face is dropped by the mesher and never reaches
521
+ * the buffer at all.
522
+ */
523
+ export declare const FACE_MASK_ALL = 63;
524
+ /** Is face `d` drawn? Bit d set ⇒ yes. 0 is the unknown sentinel ⇒ every face. */
525
+ export declare function faceExposed(faceMask: number, d: number): boolean;
526
+ /** How many faces this instance actually has to draw. 0 is the "unknown" sentinel ⇒ all six. */
527
+ export declare function exposedFaceCount(faceMask: number): number;
528
+ /** The six axial bits of a 27-bit occupancy mask, inverted into a face mask. For producers that
529
+ * already hold an `occ` — a solid neighbour hides its face. */
530
+ export declare function faceMaskFromOcc(occ: number): number;
496
531
  /**
497
532
  * Vertex counts for the three face-cull buckets, and the classifier that picks one.
498
533
  *
@@ -505,7 +540,7 @@ export declare function exposedFaceCount(mask: number): number;
505
540
  * making as cheap as it can be — a 6x cut for the common case.
506
541
  */
507
542
  export declare const FACE_CULL_BUCKET_VERTS: readonly number[];
508
- export declare function faceCullClass(mask: number): number;
543
+ export declare function faceCullClass(faceMask: number): number;
509
544
  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],
510
545
  /** The CONCAVE radius. Separate from `bevel` (the convex one) so the two halves of the rounding
511
546
  * can be turned up, down and off independently — the only way to tell which one a defect is in.
package/dist/gpuShadow.js CHANGED
@@ -1052,22 +1052,54 @@ export const FACE_DIRS = [
1052
1052
  [0, 0, 1],
1053
1053
  [0, 0, -1],
1054
1054
  ];
1055
- /** Is face `d` open to air? A SET bit means the neighbour is SOLID — see occAt. */
1056
- export function faceExposed(mask, d) {
1057
- const o = FACE_DIRS[d];
1058
- const idx = o[0] + 1 + (o[1] + 1) * 3 + (o[2] + 1) * 9;
1059
- return ((mask >>> idx) & 1) === 0;
1055
+ /**
1056
+ * THE FACE MASK: BIT d MEANS "DRAW FACE d". Six bits, in FACE_DIRS order.
1057
+ *
1058
+ * ⚠️ THIS IS NOT `occ`, AND THE TWO MUST NOT BE CONFLATED. `occ` is a 27-bit OCCUPANCY mask — a set
1059
+ * bit is a SOLID neighbour — and it is still required in full, because vertex AO reads the DIAGONAL
1060
+ * neighbours (`cornerAOAt` samples n+a1, n+a2 and n+a1+a2) and the bevel shapes edges from the same
1061
+ * neighbourhood. Face culling needs only the six axial answers, and the producer already knows them:
1062
+ * deriving them again per vertex in the shader meant a six-iteration loop calling `occAt`, which
1063
+ * recomputes a 3D neighbour index every time, in the hottest shader in the engine.
1064
+ *
1065
+ * So the mesher ships what it already computed. `occ` keeps its 27 bits for AO and bevel; this
1066
+ * carries the six that culling needs.
1067
+ *
1068
+ * ⚠️ ZERO IS "UNKNOWN ⇒ DRAW ALL SIX", NOT "DRAW NOTHING", which is the same sentinel `occ` uses.
1069
+ * That is what makes the change safe to land incrementally: a producer that has not been taught to
1070
+ * fill this in writes 0 and keeps its whole cube, rather than silently vanishing. "Draw nothing"
1071
+ * never needs expressing — a cube with no visible face is dropped by the mesher and never reaches
1072
+ * the buffer at all.
1073
+ */
1074
+ export const FACE_MASK_ALL = 0b111111;
1075
+ /** Is face `d` drawn? Bit d set ⇒ yes. 0 is the unknown sentinel ⇒ every face. */
1076
+ export function faceExposed(faceMask, d) {
1077
+ return faceMask === 0 || ((faceMask >>> d) & 1) === 1;
1060
1078
  }
1061
- /** How many faces this instance actually has to draw. mask 0 is the "unknown" sentinel ⇒ all six. */
1062
- export function exposedFaceCount(mask) {
1063
- if (mask === 0)
1079
+ /** How many faces this instance actually has to draw. 0 is the "unknown" sentinel ⇒ all six. */
1080
+ export function exposedFaceCount(faceMask) {
1081
+ if (faceMask === 0)
1064
1082
  return 6;
1065
1083
  let n = 0;
1066
1084
  for (let d = 0; d < 6; d++)
1067
- if (faceExposed(mask, d))
1085
+ if ((faceMask >>> d) & 1)
1068
1086
  n++;
1069
1087
  return n;
1070
1088
  }
1089
+ /** The six axial bits of a 27-bit occupancy mask, inverted into a face mask. For producers that
1090
+ * already hold an `occ` — a solid neighbour hides its face. */
1091
+ export function faceMaskFromOcc(occ) {
1092
+ if (occ === 0)
1093
+ return 0; // unknown stays unknown
1094
+ let m = 0;
1095
+ for (let d = 0; d < 6; d++) {
1096
+ const o = FACE_DIRS[d];
1097
+ const idx = o[0] + 1 + (o[1] + 1) * 3 + (o[2] + 1) * 9;
1098
+ if (((occ >>> idx) & 1) === 0)
1099
+ m |= 1 << d;
1100
+ }
1101
+ return m;
1102
+ }
1071
1103
  /**
1072
1104
  * Vertex counts for the three face-cull buckets, and the classifier that picks one.
1073
1105
  *
@@ -1080,8 +1112,8 @@ export function exposedFaceCount(mask) {
1080
1112
  * making as cheap as it can be — a 6x cut for the common case.
1081
1113
  */
1082
1114
  export const FACE_CULL_BUCKET_VERTS = [6, 18, 36];
1083
- export function faceCullClass(mask) {
1084
- const n = exposedFaceCount(mask);
1115
+ export function faceCullClass(faceMask) {
1116
+ const n = exposedFaceCount(faceMask);
1085
1117
  return n <= 1 ? 6 : n <= 3 ? 18 : 36;
1086
1118
  }
1087
1119
  // THE RIM TURN IS GONE, AND ITS ABSENCE IS THE POINT. rimTurn() clipped a face's corner and grew a
@@ -1587,29 +1619,27 @@ fn occAt(mask: u32, o: vec3<f32>) -> f32 {
1587
1619
  // ── FACE CULLING ────────────────────────────────────────────────────────────────────────────────
1588
1620
  // Mirrors faceExposed / exposedFaceCount / faceCullRemap in TS — ONE source, spliced into both the
1589
1621
  // draw and cull shaders, so there is no third copy. See the FACE CULLING block above buildCube.
1590
- const FACE_N = array<vec3<f32>, 6>(
1591
- vec3(1.0, 0.0, 0.0), vec3(-1.0, 0.0, 0.0),
1592
- vec3(0.0, 1.0, 0.0), vec3(0.0, -1.0, 0.0),
1593
- vec3(0.0, 0.0, 1.0), vec3(0.0, 0.0, -1.0),
1594
- );
1595
- fn faceExposedW(mask: u32, d: u32) -> bool {
1596
- return occAt(mask, FACE_N[d]) < 0.5; // a SET bit is a SOLID neighbour
1622
+ // ⚠️ THESE TAKE THE FACE MASK, NOT occ. Bit d means "draw face d". The mesher already computed the
1623
+ // six axial answers, so the shader no longer re-derives them from the 27-bit occupancy mask: that
1624
+ // cost a six-iteration loop calling occAt — which recomputes a 3D neighbour index per iteration —
1625
+ // for every vertex of every instance. occ is still needed in FULL for AO (cornerAOAt reads the
1626
+ // diagonals) and the bevel; it is simply not what culling asks.
1627
+ fn faceExposedW(fm: u32, d: u32) -> bool {
1628
+ return fm == 0u || ((fm >> d) & 1u) == 1u; // 0 = unknown, draw everything
1597
1629
  }
1598
- fn exposedFaceCountW(mask: u32) -> u32 {
1599
- if (mask == 0u) { return 6u; } // unknown ⇒ draw everything, same sentinel as the bevel
1600
- var n = 0u;
1601
- for (var d = 0u; d < 6u; d = d + 1u) { if (faceExposedW(mask, d)) { n = n + 1u; } }
1602
- return n;
1630
+ fn exposedFaceCountW(fm: u32) -> u32 {
1631
+ if (fm == 0u) { return 6u; } // unknown ⇒ draw everything, same sentinel as the bevel
1632
+ return countOneBits(fm & 63u);
1603
1633
  }
1604
1634
  // Rewrite a sharp-cube vertex index onto the (vi / 6)-th EXPOSED face, keeping its corner. Returns
1605
1635
  // 0xffffffff when this slot has no face left — the draw's bucket is sized for the WORST instance in
1606
1636
  // it, so most instances have slots to spare and the vertex shader collapses those to a point.
1607
- fn faceCullRemap(mask: u32, vi: u32) -> u32 {
1637
+ fn faceCullRemap(fm: u32, vi: u32) -> u32 {
1608
1638
  let slot = vi / 6u;
1609
1639
  let corner = vi % 6u;
1610
1640
  var seen = 0u;
1611
1641
  for (var d = 0u; d < 6u; d = d + 1u) {
1612
- if (faceExposedW(mask, d)) {
1642
+ if (faceExposedW(fm, d)) {
1613
1643
  if (seen == slot) { return d * 6u + corner; }
1614
1644
  seen = seen + 1u;
1615
1645
  }
@@ -2200,9 +2230,11 @@ fn cull(@builtin(global_invocation_id) gid: vec3<u32>) {
2200
2230
  // faceCull.test.mts are what hold that, one per mode.
2201
2231
  var b = 0u;
2202
2232
  if (cc.lod.y > 0.5) {
2203
- let mask = select(u32(inst.aux2.x) | (u32(inst.aux2.y) << 14u), 0u, inst.aux.x < -1.5);
2233
+ let raw2 = select(u32(inst.aux2.y), 0u, inst.aux.x < -1.5);
2234
+ let mask = select(u32(inst.aux2.x) | ((raw2 & 8191u) << 14u), 0u, inst.aux.x < -1.5);
2235
+ let fmask = raw2 >> 13u; // the six face bits riding above occ's high 13
2204
2236
  if (cc.lod.y > 1.5) {
2205
- let n = exposedFaceCountW(mask);
2237
+ let n = exposedFaceCountW(fmask);
2206
2238
  if (n > 3u) { b = 2u; } else if (n > 1u) { b = 1u; }
2207
2239
  } else {
2208
2240
  let cls = bevelClass(mask, cc.lod.z, cc.lod.w);
@@ -2310,7 +2342,10 @@ fn faceVec(code: f32) -> vec3<f32> {
2310
2342
  // TS template literal, so one ends the shader mid-string and the error surfaces as a TS parse failure
2311
2343
  // dozens of lines later.
2312
2344
  let isPatch = inst.aux.x < -1.5;
2313
- let mask = select(u32(inst.aux2.x) | (u32(inst.aux2.y) << 14u), 0u, isPatch);
2345
+ let raw2 = select(u32(inst.aux2.y), 0u, isPatch);
2346
+ let mask = select(u32(inst.aux2.x) | ((raw2 & 8191u) << 14u), 0u, isPatch);
2347
+ // The six face bits the mesher already computed, riding above occ's high 13. 0 = unknown ⇒ all six.
2348
+ let fmask = raw2 >> 13u;
2314
2349
  // THE BEVELED CUBE. cam.eye.w is the bevel amount; at 0 — every caller that has not opted in —
2315
2350
  // bevelPos returns the sharp corner and this is buildCube()'s geometry exactly. The draw is only
2316
2351
  // 36 vertices long in that case anyway (see the indirect args), so the tail is never even reached.
@@ -2326,7 +2361,7 @@ fn faceVec(code: f32) -> vec3<f32> {
2326
2361
  var vv = vi;
2327
2362
  var faceless = false; // this slot has no exposed face left → collapse it below
2328
2363
  if (cam.csm.w > 0.5 && cam.eye.w <= 0.0 && cam.csm.z <= 0.0 && mask != 0u) {
2329
- let r = faceCullRemap(mask, vi);
2364
+ let r = faceCullRemap(fmask, vi);
2330
2365
  if (r == 0xffffffffu) { faceless = true; } else { vv = r; }
2331
2366
  }
2332
2367
  let sharp = BEV_CORNER[vv];
@@ -3445,7 +3480,19 @@ export class GpuShadowRenderer {
3445
3480
  const pt = c.tile === -3 ? c.patch : undefined;
3446
3481
  const occ = c.occ ?? 0;
3447
3482
  scratch[o + 16] = pt ? pt.at : fm ? fm[0] : occ & 0x3fff;
3448
- scratch[o + 17] = pt ? pt.res : fm ? fm[1] : (occ / 0x4000) | 0;
3483
+ // ⚠️ THE FACE MASK RIDES ABOVE occ's HIGH 13 BITS, IN THE SAME FLOAT, AT NO WIRE COST. occ is
3484
+ // 27 bits split 14/13, so slot 17 carries values under 2^13 and an f32 holds integers exactly
3485
+ // to 2^24 — six more bits fit with room to spare (8192·64 = 524 288). The face mask could not
3486
+ // simply REPLACE occ: vertex AO reads the DIAGONAL neighbours (cornerAOAt samples n+a1+a2) and
3487
+ // the bevel shapes edges from the same neighbourhood, so all 27 bits are still required.
3488
+ //
3489
+ // Never in patchwork or face-material mode — there slots 16/17 are SIX MATERIAL IDS and the
3490
+ // shader already forces the mask to the 0 "unknown" sentinel.
3491
+ scratch[o + 17] = pt
3492
+ ? pt.res
3493
+ : fm
3494
+ ? fm[1]
3495
+ : ((occ / 0x4000) | 0) + (c.faceMask ?? 0) * 0x2000;
3449
3496
  scratch[o + 18] = c.jitter ?? 0;
3450
3497
  scratch[o + 19] = c.texFit ?? 0; // texFit: one atlas tile per face at any draw scale (see CubeInstance.texFit)
3451
3498
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ugly-game",
3
- "version": "0.15.28",
3
+ "version": "0.15.29",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {