ugly-game 0.15.13 → 0.15.15

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.
@@ -302,6 +302,11 @@ export declare function bevelVertex(corner: readonly [number, number, number], h
302
302
  * Defaults to the convex radius, which is the "one uniformly rounded solid" the design wants. */
303
303
  concave?: number): [number, number, number];
304
304
  export declare const COMMON_WGSL: string;
305
+ export declare const CULL_WGSL = "\n// \u26A0\uFE0F THE SAME STRIDE AS THE DRAW PASSES' Inst, and it has to be kept that way BY HAND \u2014 this is a\n// second declaration of one buffer's layout, not a shared one. The cull pass only reads pos/scl/rot,\n// so a stale copy still compiles and still runs; it just indexes every instance past the first at\n// the wrong offset, and the symptom is cubes winking out of existence at the edge of view rather\n// than anything that looks like a struct mismatch.\nstruct Inst { pos: vec4<f32>, scl: vec4<f32>, rot: vec4<f32>, aux: vec4<f32>, aux2: vec4<f32> };\nstruct Args { vc: u32, ic: atomic<u32>, fv: u32, fi: u32 };\nstruct CullCam {\n planes: array<vec4<f32>, 6>, cnt: vec4<f32>, // cnt.x = instance count\n camVP: mat4x4<f32>, occ: vec4<f32>, // occ.x = occlusion on \u00B7 occ.yz = hiz w,h \u00B7 occ.w = downscale\n};\n@group(0) @binding(0) var<storage, read> insts: array<Inst>;\n@group(0) @binding(1) var<storage, read_write> args: Args;\n@group(0) @binding(2) var<storage, read_write> visible: array<u32>;\n@group(0) @binding(3) var<uniform> cc: CullCam;\n@group(0) @binding(4) var hiz: texture_2d<f32>;\nfn qr(q: vec4<f32>, v: vec3<f32>) -> vec3<f32> { let t = 2.0 * cross(q.xyz, v); return v + q.w * t + cross(q.xyz, t); }\n@compute @workgroup_size(64)\nfn cull(@builtin(global_invocation_id) gid: vec3<u32>) {\n let i = gid.x;\n if (i >= u32(cc.cnt.x)) { return; }\n let inst = insts[i];\n let c = inst.pos.xyz;\n let h = 0.5 * inst.scl.xyz;\n let bx = qr(inst.rot, vec3<f32>(h.x, 0.0, 0.0)); // rotated box semi-axes\n let by = qr(inst.rot, vec3<f32>(0.0, h.y, 0.0));\n let bz = qr(inst.rot, vec3<f32>(0.0, 0.0, h.z));\n for (var p = 0u; p < 6u; p++) {\n let pl = cc.planes[p];\n let e = abs(dot(pl.xyz, bx)) + abs(dot(pl.xyz, by)) + abs(dot(pl.xyz, bz)); // box half-width along the normal\n if (dot(pl.xyz, c) + pl.w < -e) { return; }\n }\n // \u2500\u2500 Hi-Z occlusion (camera pass only) \u2500\u2500\n if (cc.occ.x > 0.5) {\n var mn = vec2<f32>(1e9, 1e9);\n var mx = vec2<f32>(-1e9, -1e9);\n var nearZ = 1e9;\n var ok = true;\n for (var ci = 0u; ci < 8u; ci++) {\n let s = vec3<f32>(select(-1.0, 1.0, (ci & 1u) != 0u), select(-1.0, 1.0, (ci & 2u) != 0u), select(-1.0, 1.0, (ci & 4u) != 0u));\n let corner = c + bx * s.x + by * s.y + bz * s.z;\n let clip = cc.camVP * vec4<f32>(corner, 1.0);\n if (clip.w <= 0.0) { ok = false; break; } // a corner at/behind the eye \u2192 don't occlusion-cull (keep)\n let ndc = clip.xyz / clip.w;\n let uv = ndc.xy * vec2<f32>(0.5, -0.5) + vec2<f32>(0.5, 0.5);\n mn = min(mn, uv); mx = max(mx, uv);\n nearZ = min(nearZ, ndc.z);\n }\n if (ok && nearZ >= 0.0) {\n let dim = vec2<f32>(cc.occ.y, cc.occ.z);\n let t0 = vec2<i32>(floor(clamp(mn, vec2<f32>(0.0), vec2<f32>(1.0)) * dim));\n let t1 = vec2<i32>(floor(clamp(mx, vec2<f32>(0.0), vec2<f32>(1.0)) * dim));\n // only query a bounded footprint; a huge on-screen box just isn't occlusion-tested (kept)\n if (t1.x - t0.x <= 8 && t1.y - t0.y <= 8) {\n // DILATE the query by 2 Hi-Z texels. hiz is LAST frame's depth; under camera motion a cube reprojects by\n // a few source pixels between frames, so a cube sitting at an occluder's silhouette gets tested against\n // stale NEAR depth from where the occluder used to be and is wrongly culled \u2014 it blinks out for a frame\n // (worst on thin/sparse geometry, but a one-frame hole in dense terrain too). Taking MAX over a dilated\n // footprint is strictly MORE conservative (it can only raise farOccluder \u21D2 only ever KEEP more cubes), so it\n // never hides real geometry; it just leaves a couple of deeply-hidden cubes unculled. 2\u00D78px of slack.\n let lim = vec2<i32>(i32(cc.occ.y) - 1, i32(cc.occ.z) - 1);\n let q0 = max(t0 - vec2<i32>(2, 2), vec2<i32>(0, 0));\n let q1 = min(t1 + vec2<i32>(2, 2), lim);\n var farOccluder = 0.0; // MAX over the footprint of the coarse max-depth\n for (var y = q0.y; y <= q1.y; y++) {\n for (var x = q0.x; x <= q1.x; x++) {\n farOccluder = max(farOccluder, textureLoad(hiz, vec2<i32>(x, y), 0).r);\n }\n }\n if (nearZ > farOccluder) { return; } // behind the farthest occluder everywhere \u2192 occluded\n }\n }\n }\n let slot = atomicAdd(&args.ic, 1u);\n visible[slot] = i;\n}\n";
306
+ export declare const HIZ_WGSL = "\n@group(0) @binding(0) var srcDepth: texture_depth_2d;\n@group(0) @binding(1) var dst: texture_storage_2d<r32float, write>;\nstruct P { dim: vec4<u32> }; // dim.xy = dst size, dim.zw = src size\n@group(0) @binding(2) var<uniform> p: P;\n@compute @workgroup_size(8, 8)\nfn build(@builtin(global_invocation_id) gid: vec3<u32>) {\n if (gid.x >= p.dim.x || gid.y >= p.dim.y) { return; }\n let down = 8u;\n var mx = 0.0;\n for (var dy = 0u; dy < down; dy++) {\n for (var dx = 0u; dx < down; dx++) {\n let sx = gid.x * down + dx; let sy = gid.y * down + dy;\n if (sx < p.dim.z && sy < p.dim.w) { mx = max(mx, textureLoad(srcDepth, vec2<i32>(i32(sx), i32(sy)), 0)); }\n }\n }\n textureStore(dst, vec2<i32>(i32(gid.x), i32(gid.y)), vec4<f32>(mx, 0.0, 0.0, 1.0));\n}\n";
307
+ export declare const SHADOW_WGSL: string;
308
+ export declare const MAIN_WGSL: string;
309
+ export declare const ALPHA_WGSL: string;
305
310
  export declare class GpuShadowRenderer {
306
311
  private device;
307
312
  private instBuf;
package/dist/gpuShadow.js CHANGED
@@ -1083,6 +1083,10 @@ concave = bevel) {
1083
1083
  return out;
1084
1084
  }
1085
1085
  // Shared vertex helpers + cube data. cam.sunCol.w carries the shadow-map texel size.
1086
+ // ⚠️ EVERY SHADER SOURCE IS EXPORTED SO IT CAN BE COMPILED BY A TEST. bevelParity checks that
1087
+ // bevelPos agrees with the oracle, but it builds a COMPUTE shader — it never compiles the vertex
1088
+ // stage, so a WGSL error in vs() passed every check and shipped as a BLACK SCREEN: the pipeline
1089
+ // failed to build and nothing drew at all. bevelShaders.spec.mts compiles all five.
1086
1090
  // ⚠️ EXPORTED SO THE SHADER CAN BE TESTED. bevelPos() here and bevelVertex() in TS are mirrored BY
1087
1091
  // HAND, and for the whole life of this file nothing checked that they agree — every closure and
1088
1092
  // intersection number came from the TS side, so the shader could drift arbitrarily and the suite
@@ -1490,7 +1494,7 @@ fn hsv(hh: f32) -> vec3<f32> {
1490
1494
  // the larger region only ever raises the occluder distance, so it strictly keeps more cubes and never hides real
1491
1495
  // geometry. occ.x toggles it (camera pass only; the shadow pass never occludes — an off-screen occluder still
1492
1496
  // casts a shadow into view). occ.yz = hiz dims, occ.w = pixels/texel.
1493
- const CULL_WGSL = /* wgsl */ `
1497
+ export const CULL_WGSL = /* wgsl */ `
1494
1498
  // ⚠️ THE SAME STRIDE AS THE DRAW PASSES' Inst, and it has to be kept that way BY HAND — this is a
1495
1499
  // second declaration of one buffer's layout, not a shared one. The cull pass only reads pos/scl/rot,
1496
1500
  // so a stale copy still compiles and still runs; it just indexes every instance past the first at
@@ -1571,7 +1575,7 @@ fn cull(@builtin(global_invocation_id) gid: vec3<u32>) {
1571
1575
  // Hi-Z BUILD: max-reduce the previous frame's depth (a sampleable depth texture) into a coarse R32float buffer —
1572
1576
  // each output texel = the FARTHEST (max) NDC depth over its `down`×`down` source block, so "cube behind this" is a
1573
1577
  // safe occlusion test. depth24plus reads as a depth texture; textureLoad returns the stored [0,1] depth.
1574
- const HIZ_WGSL = /* wgsl */ `
1578
+ export const HIZ_WGSL = /* wgsl */ `
1575
1579
  @group(0) @binding(0) var srcDepth: texture_depth_2d;
1576
1580
  @group(0) @binding(1) var dst: texture_storage_2d<r32float, write>;
1577
1581
  struct P { dim: vec4<u32> }; // dim.xy = dst size, dim.zw = src size
@@ -1591,7 +1595,7 @@ fn build(@builtin(global_invocation_id) gid: vec3<u32>) {
1591
1595
  }
1592
1596
  `;
1593
1597
  const HIZ_DOWN = 8; // source pixels per Hi-Z texel (coarse ⇒ conservative, cheap)
1594
- const SHADOW_WGSL = COMMON_WGSL +
1598
+ export const SHADOW_WGSL = COMMON_WGSL +
1595
1599
  /* wgsl */ `
1596
1600
  @group(0) @binding(2) var<storage, read> vis: array<u32>;
1597
1601
  // Always index [0]: each cascade's shadow pass binds its OWN Cam buffer whose lightVP[0] holds that
@@ -1600,7 +1604,7 @@ const SHADOW_WGSL = COMMON_WGSL +
1600
1604
  return cam.lightVP[0] * vec4(worldOf(insts[vis[ii]], vi), 1.0);
1601
1605
  }
1602
1606
  `;
1603
- const MAIN_WGSL = COMMON_WGSL +
1607
+ export const MAIN_WGSL = COMMON_WGSL +
1604
1608
  /* wgsl */ `
1605
1609
  @group(0) @binding(2) var shadowMap: texture_depth_2d_array;
1606
1610
  @group(0) @binding(3) var shadowSamp: sampler_comparison;
@@ -1662,8 +1666,9 @@ fn faceVec(code: f32) -> vec3<f32> {
1662
1666
  // wedges once already.
1663
1667
  if (BEV_ROLE[vi] == ${ROLE_BASE}.0) {
1664
1668
  let gb = baseOwner(mask, sharp);
1665
- if (length(gb) > 0.5) { home = -gb; }
1669
+ if (length(gb) > 0.5) { home = -gb; } // the TEXTURE is the flat surface's; the NORMAL is below
1666
1670
  }
1671
+
1667
1672
  let local = bevelPos(mask, sharp, home, cam.eye.w, BEV_ROLE[vi], BEV_AXIS[vi], cam.csm.z);
1668
1673
  let world = inst.pos.xyz + qrot(inst.rot, local * inst.scl.xyz);
1669
1674
  var o: VOut;
@@ -1680,6 +1685,49 @@ fn faceVec(code: f32) -> vec3<f32> {
1680
1685
  // curve through the groove. objN stays home, so the texture still belongs to the face it grew from.
1681
1686
  var shade = home;
1682
1687
  if (BEV_ROLE[vi] == ${ROLE_FMID}.0) { shade = normalize(home - BEV_AXIS[vi]); }
1688
+ // ⚠️ A BAND'S NORMAL SWEEPS ACROSS THE STRIP. That sweep is the only reason a flat 45° chamfer
1689
+ // reads as a cylinder — the geometry is a plane and the shading is a curve. It used to happen for
1690
+ // free, because the quad's two long edges carried different home faces; subdividing the band
1691
+ // gave every vertex the OWNING face so the texture would stay put, and with it every rounded edge
1692
+ // in the world went flat. Reported as "the shading is way off", and it was.
1693
+ //
1694
+ // Rotate from the owning face to the partner across the arc. At step 0 and BEVEL_SEG this is
1695
+ // exactly the two normals the old quad carried, so at one segment nothing changes.
1696
+ // ⚠️ THE BASE PATCH'S NORMAL WRAPS WITH ITS GEOMETRY, which turns through 90°: from the flat
1697
+ // surface, over the fillet's crest, onto the block's two walls. One flat normal across all of that
1698
+ // is the same mistake the band made.
1699
+ //
1700
+ // ⚠️ AND IT LIVES HERE, NOT UP WITH THE home RESOLUTION. Written there it assigned shade above
1701
+ // the line that declares it — WGSL use-before-declaration, the shader failed to compile, the
1702
+ // pipeline never built and the screen went black. bevelParity.spec.ts could not see it: it
1703
+ // compiles COMMON_WGSL into a COMPUTE shader and never builds the vertex stage at all.
1704
+ if (BEV_ROLE[vi] == ${ROLE_BASE}.0) {
1705
+ let gb2 = baseOwner(mask, sharp);
1706
+ if (length(gb2) > 0.5) {
1707
+ let hb = -gb2;
1708
+ let ipb = -sign(sharp) * (vec3(1.0, 1.0, 1.0) - abs(gb2));
1709
+ var b1 = vec3(0.0, 0.0, 0.0);
1710
+ var b2 = vec3(0.0, 0.0, 0.0);
1711
+ if (abs(gb2.x) > 0.5) { b1 = vec3(0.0, ipb.y, 0.0); b2 = vec3(0.0, 0.0, ipb.z); }
1712
+ else if (abs(gb2.y) > 0.5) { b1 = vec3(ipb.x, 0.0, 0.0); b2 = vec3(0.0, 0.0, ipb.z); }
1713
+ else { b1 = vec3(ipb.x, 0.0, 0.0); b2 = vec3(0.0, ipb.y, 0.0); }
1714
+ var fa2 = b1;
1715
+ var fb2 = b2;
1716
+ if (dot(cross(b1, b2), hb) < 0.0) { fa2 = b2; fb2 = b1; }
1717
+ let ib = i32(BEV_AXIS[vi].x + 0.5);
1718
+ if (ib == 1) { shade = normalize(hb + fb2); }
1719
+ else if (ib == 2) { shade = fb2; }
1720
+ else if (ib == 3) { shade = fa2; }
1721
+ else if (ib == 4) { shade = normalize(hb + fa2); }
1722
+ else { shade = hb; }
1723
+ }
1724
+ }
1725
+ if (BEV_ROLE[vi] == ${ROLE_BAND}.0) {
1726
+ let pb = sign(BEV_AXIS[vi]);
1727
+ let stb = round(max(max(abs(BEV_AXIS[vi].x), abs(BEV_AXIS[vi].y)), abs(BEV_AXIS[vi].z))) - 1.0;
1728
+ let thb = (stb / ${BEVEL_SEG}.0) * 1.5707963267948966;
1729
+ shade = normalize(home * cos(thb) + pb * sin(thb));
1730
+ }
1683
1731
  // ⚠️ A SUBDIVIDED FACET SHADES AS THE SPHERE IT IS. Its vertices carry a face normal for the
1684
1732
  // TEXTURE, and at one segment those three normals sweeping across one triangle is what made a flat
1685
1733
  // chamfer corner read round. Once the patch is a real octant, the honest normal is the sphere's —
@@ -2032,7 +2080,7 @@ fn fixedFill(N: vec3<f32>, up: vec3<f32>) -> f32 {
2032
2080
  // Translucent OVERLAY pass — full-size, alpha-blended cubes drawn over the opaque scene with depth test but
2033
2081
  // no depth write (so they don't occlude each other or the world behind). scl.w carries the ALPHA (0..1).
2034
2082
  // Used for the place ghost: a full-size, low-opacity preview of the block that will materialize.
2035
- const ALPHA_WGSL = COMMON_WGSL +
2083
+ export const ALPHA_WGSL = COMMON_WGSL +
2036
2084
  /* wgsl */ `
2037
2085
  struct AOut { @builtin(position) clip: vec4<f32>, @location(0) hue: f32, @location(1) a: f32 };
2038
2086
  @vertex fn vs(@builtin(vertex_index) vi: u32, @builtin(instance_index) ii: u32) -> AOut {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ugly-game",
3
- "version": "0.15.13",
3
+ "version": "0.15.15",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {