ugly-game 0.15.5 → 0.15.7

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.
@@ -82,6 +82,66 @@ export declare const CUBE: {
82
82
  pos: string;
83
83
  nrm: string;
84
84
  };
85
+ /** Vertex counts for the two cube geometries the camera pass can draw. */
86
+ export declare const SHARP_VERTS = 36;
87
+ export declare const BEVEL_VERTS = 132;
88
+ export interface BevelCube {
89
+ /** Which of the 8 sharp corners each vertex sits on — always (±0.5, ±0.5, ±0.5). */
90
+ corner: [number, number, number][];
91
+ /** The face normal each vertex belongs to. Doubles as its shading normal AND the anchor
92
+ * every one of its bevel gates is measured from. */
93
+ home: [number, number, number][];
94
+ cornerWgsl: string;
95
+ homeWgsl: string;
96
+ }
97
+ export declare const BEVEL: BevelCube;
98
+ /** Is the neighbour at offset `o` solid, per the 27-bit mask? 1 = solid. Mirrors `occAt` in WGSL. */
99
+ export declare function occSolid(mask: number, o: readonly [number, number, number]): number;
100
+ /**
101
+ * Is the edge between faces `a` and `b` exposed — and therefore beveled?
102
+ *
103
+ * ⚠️ THE DIAGONAL TERM IS NOT OPTIONAL. Two blocks that touch only along a diagonal (a stair step)
104
+ * each see air on both of the faces meeting at that edge, so on the two-face test alone both would
105
+ * cut it back and open a diamond-shaped hole straight through the terrain. The third cell is the
106
+ * one that knows the other block is there.
107
+ */
108
+ export declare function edgeOpen(mask: number, a: readonly [number, number, number], b: readonly [number, number, number]): number;
109
+ /**
110
+ * THE NOTCH — the exact complement of `edgeOpen`: both faces SOLID, the diagonal between them AIR.
111
+ *
112
+ * Three blocks in an L around an empty cell. The two with a FACE on that cell chamfer it normally.
113
+ * The third touches it only along one corner edge, so `edgeOpen` refuses — both its face-neighbours
114
+ * are solid — and it keeps a hard 90° arris running down the inside of the notch while everything
115
+ * around it is rounded. It is the one edge in a rounded world that still looks stamped out of a grid.
116
+ *
117
+ * ⚠️ CUTTING IT CANNOT OPEN A HOLE, which is worth stating because the diagonal test everywhere else
118
+ * in this file exists to PREVENT holes. The wedge removed lies inside this block's own cell, and the
119
+ * two neighbours' buried faces close the gap behind it — every instance draws all six faces, so they
120
+ * are already there. Neither neighbour cuts at that edge: each has one OPEN face there, so
121
+ * `edgeOpen` (needs both open) and `notchOpen` (needs both solid) are false for both of them.
122
+ */
123
+ export declare function notchOpen(mask: number, a: readonly [number, number, number], b: readonly [number, number, number]): number;
124
+ /** How much of the main bevel a notch edge gets. Half — it softens an INSIDE corner rather than
125
+ * rounding an outside one, and at the full amount it reads as a gouge instead of a relief. */
126
+ export declare const NOTCH_SCALE = 0.5;
127
+ /**
128
+ * THE VERTEX RULE, in JS — the oracle `gpuShadow.bevel.test.mts` checks and the exact thing
129
+ * `bevelPos()` in COMMON_WGSL does. Kept in step with it by hand; the test is what notices.
130
+ *
131
+ * A vertex's pull along a free axis `k` is inward (`-sign(corner[k])`), gated by one of three
132
+ * conditions on that axis:
133
+ *
134
+ * CONVEX — `edgeOpen(home, e_k)`. The ordinary chamfer, at full size.
135
+ * NOTCH, FLANKING — `notchOpen(home, e_k)`. This vertex is on one of the two faces meeting at a
136
+ * notch edge; this cuts that edge itself.
137
+ * NOTCH, CAPPING — `notchOpen(e_k, e_j)`, `j` being the OTHER free axis. This vertex is on the
138
+ * face PERPENDICULAR to a notch edge — the ground above it, say. Both its free axes pull, which
139
+ * cuts that face's corner diagonally so the notch chamfer does not stop dead in a step.
140
+ *
141
+ * The convex term cannot collide with either notch term: it requires both cells AIR, they require
142
+ * one SOLID.
143
+ */
144
+ export declare function bevelVertex(corner: readonly [number, number, number], home: readonly [number, number, number], mask: number, bevel: number): [number, number, number];
85
145
  export declare class GpuShadowRenderer {
86
146
  private device;
87
147
  private instBuf;
@@ -136,6 +196,7 @@ export declare class GpuShadowRenderer {
136
196
  private matQuality;
137
197
  private matCutoff;
138
198
  private eye;
199
+ private bevel;
139
200
  private alphaPipe;
140
201
  private alphaBGL;
141
202
  private overlayBuf;
@@ -226,6 +287,21 @@ export declare class GpuShadowRenderer {
226
287
  * highlight will sit somewhere else entirely.
227
288
  */
228
289
  setEye(x: number, y: number, z: number): void;
290
+ /**
291
+ * ROUND THE EXPOSED EDGES of every cube that supplies a `CubeInstance.occ` mask, by `amount`
292
+ * block-fractions (0.06 is a subtle rim; 0.2 is pebble-like). 0 — the default — is the sharp cube.
293
+ *
294
+ * Only edges open to air on BOTH adjacent faces and on the cell diagonally between them are cut
295
+ * back, so blocks that touch still meet flush and a stair step cannot open a hole. An instance
296
+ * without a mask (`occ` absent ⇒ 0, the "unknown" sentinel) is never beveled, which is why the far
297
+ * field, sky-body shells, ships, creatures and props stay sharp with no work on their part.
298
+ *
299
+ * ⚠️ NOT FREE WHILE IT IS ON. A non-zero amount draws 132 vertices per instance instead of 36 in
300
+ * the CAMERA pass — for every instance, beveled or not, because they all ride one indirect draw.
301
+ * The shadow pass keeps the sharp cube either way. At 0 the vertex count drops back to 36, so
302
+ * leaving this alone costs exactly what it cost before the feature existed.
303
+ */
304
+ setBevel(amount: number): void;
229
305
  setAtlas(tex: GPUTexture, layout: AtlasLayout): void;
230
306
  /** Static scene: sizes the buffer to exactly these instances. */
231
307
  setScene(instances: CubeInstance[]): void;
package/dist/gpuShadow.js CHANGED
@@ -69,6 +69,191 @@ function buildCube() {
69
69
  }
70
70
  /** The 36-vertex cube as WGSL array-literal bodies (pos + per-face normal). Shared by shadow modules. */
71
71
  export const CUBE = buildCube();
72
+ /** Vertex counts for the two cube geometries the camera pass can draw. */
73
+ export const SHARP_VERTS = 36;
74
+ export const BEVEL_VERTS = 132; // 6 face quads (36) + 12 bevel bands (72) + 8 corner tris (24)
75
+ // THE CHAMFERED CUBE — a cube with all twelve edges cut back, where each cut COLLAPSES to a sharp
76
+ // corner unless that edge is actually exposed to air or water.
77
+ //
78
+ // Every vertex is one of the 8 corners displaced inward along up to three axes, and each
79
+ // displacement is gated by exactly one edge. That is the whole design, and three properties fall
80
+ // out of it rather than having to be engineered:
81
+ //
82
+ // WATERTIGHT — a face quad, its bevel bands and its corner tri re-derive a shared vertex from the
83
+ // same (corner, home) pair under the same gate, so they cannot disagree and no crack can open.
84
+ //
85
+ // FLUSH — a buried edge has a solid face-neighbour, its gate is false, and it collapses. Blocks
86
+ // that touch still meet exactly as they did before this existed.
87
+ //
88
+ // FREE WHEN UNKNOWN — occ == 0 (every non-terrain caller: the far field, sky bodies, the ship,
89
+ // creatures, props, the place ghost) makes every gate false, so all 132 vertices land on the 8
90
+ // sharp corners and the geometry is identical to buildCube()'s.
91
+ //
92
+ // ⚠️ FACE QUADS ARE EMITTED FIRST, AND THAT ORDERING IS LOAD-BEARING. Vertices 0..35 are byte-for-
93
+ // byte buildCube()'s cube, which is what lets the camera pass draw 36 instead of 132 whenever the
94
+ // bevel is off — so turning the feature off costs exactly what it cost before the feature existed,
95
+ // rather than merely looking the same while paying for 96 degenerate vertices per instance.
96
+ function buildBevelCube() {
97
+ // The same six faces, in the same order, as buildCube().
98
+ const faces = [
99
+ { n: [1, 0, 0], u: [0, 0, 1], v: [0, 1, 0] },
100
+ { n: [-1, 0, 0], u: [0, 0, 1], v: [0, 1, 0] },
101
+ { n: [0, 1, 0], u: [1, 0, 0], v: [0, 0, 1] },
102
+ { n: [0, -1, 0], u: [1, 0, 0], v: [0, 0, 1] },
103
+ { n: [0, 0, 1], u: [1, 0, 0], v: [0, 1, 0] },
104
+ { n: [0, 0, -1], u: [1, 0, 0], v: [0, 1, 0] },
105
+ ];
106
+ const quad = [
107
+ [-0.5, -0.5],
108
+ [0.5, -0.5],
109
+ [0.5, 0.5],
110
+ [-0.5, -0.5],
111
+ [0.5, 0.5],
112
+ [-0.5, 0.5],
113
+ ];
114
+ const corner = [];
115
+ const home = [];
116
+ const push = (c, h) => {
117
+ corner.push(c);
118
+ home.push(h);
119
+ };
120
+ const axisOf = (n) => (n[0] !== 0 ? 0 : n[1] !== 0 ? 1 : 2);
121
+ // ── 1. FACE QUADS (vertices 0..35) — identical to buildCube(), see the ordering warning above.
122
+ for (const f of faces)
123
+ for (const [cu, cv] of quad)
124
+ push([
125
+ f.n[0] * 0.5 + f.u[0] * cu + f.v[0] * cv,
126
+ f.n[1] * 0.5 + f.u[1] * cu + f.v[1] * cv,
127
+ f.n[2] * 0.5 + f.u[2] * cu + f.v[2] * cv,
128
+ ], [...f.n]);
129
+ // ── 2. BEVEL BANDS (36..107) — one quad per edge, 12 edges.
130
+ // Each band spans two faces, so its two long sides carry DIFFERENT home normals; interpolating
131
+ // between them across the band is what makes a 45° cut read as a curve instead of a facet.
132
+ //
133
+ // ⚠️ THE OWNING FACE'S VERTEX GOES FIRST IN EVERY TRIANGLE. `objN` is flat-interpolated and
134
+ // WebGPU's provoking vertex is the FIRST, so vertex order is what decides which of the two faces'
135
+ // atlas tile and in-face axes the whole band samples. Emitting them in the other order silently
136
+ // hands the band to the neighbouring face.
137
+ for (let ai = 0; ai < faces.length; ai++)
138
+ for (let bi = ai + 1; bi < faces.length; bi++) {
139
+ const a = faces[ai].n, b = faces[bi].n;
140
+ const axA = axisOf(a), axB = axisOf(b);
141
+ if (axA === axB)
142
+ continue; // opposite faces share no edge
143
+ const axE = 3 - axA - axB; // the third axis: the one the edge runs along
144
+ const at = (t, h) => {
145
+ const c = [0, 0, 0];
146
+ c[axA] = a[axA] * 0.5;
147
+ c[axB] = b[axB] * 0.5;
148
+ c[axE] = t;
149
+ push(c, h);
150
+ };
151
+ // two triangles, A-side vertex first in each
152
+ at(-0.5, [...a]);
153
+ at(-0.5, [...b]);
154
+ at(0.5, [...b]);
155
+ at(-0.5, [...a]);
156
+ at(0.5, [...b]);
157
+ at(0.5, [...a]);
158
+ }
159
+ // ── 3. CORNER TRIS (108..131) — one triangle per corner, closing the three-way junction.
160
+ // With all three edges beveled this is the classic chamfer corner; with only some of them beveled
161
+ // it is exactly the leftover gap between a band's end and an unbeveled face corner. The same rule
162
+ // produces both, which is why the mixed cases need no special handling.
163
+ for (const sx of [-0.5, 0.5])
164
+ for (const sy of [-0.5, 0.5])
165
+ for (const sz of [-0.5, 0.5]) {
166
+ const c = [sx, sy, sz];
167
+ push([...c], [Math.sign(sx), 0, 0]);
168
+ push([...c], [0, Math.sign(sy), 0]);
169
+ push([...c], [0, 0, Math.sign(sz)]);
170
+ }
171
+ const wgsl = (a) => a.map((p) => `vec3<f32>(${p[0]},${p[1]},${p[2]})`).join(', ');
172
+ return { corner, home, cornerWgsl: wgsl(corner), homeWgsl: wgsl(home) };
173
+ }
174
+ export const BEVEL = buildBevelCube();
175
+ /** Is the neighbour at offset `o` solid, per the 27-bit mask? 1 = solid. Mirrors `occAt` in WGSL. */
176
+ export function occSolid(mask, o) {
177
+ const idx = o[0] + 1 + (o[1] + 1) * 3 + (o[2] + 1) * 9;
178
+ return (mask >>> idx) & 1;
179
+ }
180
+ /**
181
+ * Is the edge between faces `a` and `b` exposed — and therefore beveled?
182
+ *
183
+ * ⚠️ THE DIAGONAL TERM IS NOT OPTIONAL. Two blocks that touch only along a diagonal (a stair step)
184
+ * each see air on both of the faces meeting at that edge, so on the two-face test alone both would
185
+ * cut it back and open a diamond-shaped hole straight through the terrain. The third cell is the
186
+ * one that knows the other block is there.
187
+ */
188
+ export function edgeOpen(mask, a, b) {
189
+ if (mask === 0)
190
+ return 0; // unknown ⇒ no bevel, same sentinel as CubeInstance.occ
191
+ const d = [a[0] + b[0], a[1] + b[1], a[2] + b[2]];
192
+ return ((1 - occSolid(mask, a)) * (1 - occSolid(mask, b)) * (1 - occSolid(mask, d)));
193
+ }
194
+ /**
195
+ * THE NOTCH — the exact complement of `edgeOpen`: both faces SOLID, the diagonal between them AIR.
196
+ *
197
+ * Three blocks in an L around an empty cell. The two with a FACE on that cell chamfer it normally.
198
+ * The third touches it only along one corner edge, so `edgeOpen` refuses — both its face-neighbours
199
+ * are solid — and it keeps a hard 90° arris running down the inside of the notch while everything
200
+ * around it is rounded. It is the one edge in a rounded world that still looks stamped out of a grid.
201
+ *
202
+ * ⚠️ CUTTING IT CANNOT OPEN A HOLE, which is worth stating because the diagonal test everywhere else
203
+ * in this file exists to PREVENT holes. The wedge removed lies inside this block's own cell, and the
204
+ * two neighbours' buried faces close the gap behind it — every instance draws all six faces, so they
205
+ * are already there. Neither neighbour cuts at that edge: each has one OPEN face there, so
206
+ * `edgeOpen` (needs both open) and `notchOpen` (needs both solid) are false for both of them.
207
+ */
208
+ export function notchOpen(mask, a, b) {
209
+ if (mask === 0)
210
+ return 0; // unknown ⇒ no bevel, see CubeInstance.occ
211
+ const d = [a[0] + b[0], a[1] + b[1], a[2] + b[2]];
212
+ return occSolid(mask, a) * occSolid(mask, b) * (1 - occSolid(mask, d));
213
+ }
214
+ /** How much of the main bevel a notch edge gets. Half — it softens an INSIDE corner rather than
215
+ * rounding an outside one, and at the full amount it reads as a gouge instead of a relief. */
216
+ export const NOTCH_SCALE = 0.5;
217
+ /**
218
+ * THE VERTEX RULE, in JS — the oracle `gpuShadow.bevel.test.mts` checks and the exact thing
219
+ * `bevelPos()` in COMMON_WGSL does. Kept in step with it by hand; the test is what notices.
220
+ *
221
+ * A vertex's pull along a free axis `k` is inward (`-sign(corner[k])`), gated by one of three
222
+ * conditions on that axis:
223
+ *
224
+ * CONVEX — `edgeOpen(home, e_k)`. The ordinary chamfer, at full size.
225
+ * NOTCH, FLANKING — `notchOpen(home, e_k)`. This vertex is on one of the two faces meeting at a
226
+ * notch edge; this cuts that edge itself.
227
+ * NOTCH, CAPPING — `notchOpen(e_k, e_j)`, `j` being the OTHER free axis. This vertex is on the
228
+ * face PERPENDICULAR to a notch edge — the ground above it, say. Both its free axes pull, which
229
+ * cuts that face's corner diagonally so the notch chamfer does not stop dead in a step.
230
+ *
231
+ * The convex term cannot collide with either notch term: it requires both cells AIR, they require
232
+ * one SOLID.
233
+ */
234
+ export function bevelVertex(corner, home, mask, bevel) {
235
+ const out = [corner[0], corner[1], corner[2]];
236
+ if (bevel <= 0 || mask === 0)
237
+ return out;
238
+ const free = [0, 1, 2].filter((i) => home[i] === 0); // the two in-face axes
239
+ const axis = (i) => {
240
+ const v = [0, 0, 0];
241
+ v[i] = Math.sign(corner[i]);
242
+ return v;
243
+ };
244
+ for (const k of free) {
245
+ const j = free.find((f) => f !== k) ?? k;
246
+ const bk = axis(k);
247
+ // ⚠️ max, NOT +. See the note above: both notch terms hold at once on a face flanking two
248
+ // notches at right angles, and summing there pulls the corner twice as far as any neighbour
249
+ // agrees to — which is a crack.
250
+ const notch = Math.max(notchOpen(mask, home, bk), notchOpen(mask, bk, axis(j)));
251
+ out[k] -=
252
+ Math.sign(corner[k]) *
253
+ (bevel * edgeOpen(mask, home, bk) + bevel * NOTCH_SCALE * notch);
254
+ }
255
+ return out;
256
+ }
72
257
  // Shared vertex helpers + cube data. cam.sunCol.w carries the shadow-map texel size.
73
258
  const COMMON_WGSL = /* wgsl */ `
74
259
  struct Cam {
@@ -80,7 +265,7 @@ struct Cam {
80
265
  atlas: vec4<f32>, // x = useAtlas (1/0) · y = per-voxel AO surviving on textured blocks · z = MATERIAL quality (0 flat · 1 normal-mapped · 2 + parallax) · w = parallax cutoff distance in world units
81
266
  up: vec4<f32>, // xyz = local gravity up (for hemisphere face-shading readability); w = linear-HDR flag
82
267
  csm: vec4<f32>, // x = active cascade count · y = crossfade band in uv units
83
- eye: vec4<f32>, // xyz = camera position in world space (material relief + specular); w unused
268
+ eye: vec4<f32>, // xyz = camera position in world space (material relief + specular); w = edge bevel amount (0 = sharp cube)
84
269
  };
85
270
  struct Inst { pos: vec4<f32>, scl: vec4<f32>, rot: vec4<f32>, aux: vec4<f32>, aux2: vec4<f32> }; // pos.w = hue (<0 ⇒ ground) · scl.w = emissive/alpha · rot = quaternion · aux.x = atlas tile (<0 ⇒ hue) · aux.y = up-face code · aux.z/aux.w = CONTACT DECAL (0,0 = none) · aux2.xy = 27-bit NEIGHBOUR OCCUPANCY split 14/13 (0,0 = unknown ⇒ no corner AO) · aux2.z = per-block albedo jitter · aux2.w = texFit (≠0 ⇒ one atlas tile per face, texture coords NOT scaled by inst.scl)
86
271
  @group(0) @binding(0) var<uniform> cam: Cam;
@@ -88,6 +273,8 @@ struct Inst { pos: vec4<f32>, scl: vec4<f32>, rot: vec4<f32>, aux: vec4<f32>, au
88
273
 
89
274
  const CUBE_POS = array<vec3<f32>, 36>(${CUBE.pos});
90
275
  const CUBE_NRM = array<vec3<f32>, 36>(${CUBE.nrm});
276
+ const BEV_CORNER = array<vec3<f32>, ${BEVEL_VERTS}>(${BEVEL.cornerWgsl});
277
+ const BEV_HOME = array<vec3<f32>, ${BEVEL_VERTS}>(${BEVEL.homeWgsl});
91
278
 
92
279
  fn qrot(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); }
93
280
  fn worldOf(inst: Inst, vi: u32) -> vec3<f32> { return inst.pos.xyz + qrot(inst.rot, CUBE_POS[vi] * inst.scl.xyz); }
@@ -112,12 +299,64 @@ fn occAt(mask: u32, o: vec3<f32>) -> f32 {
112
299
  let idx = u32((o.x + 1.0) + (o.y + 1.0) * 3.0 + (o.z + 1.0) * 9.0);
113
300
  return f32((mask >> idx) & 1u);
114
301
  }
302
+ // ── ROUNDED EDGES ───────────────────────────────────────────────────────────────────────────────
303
+ // Is the edge between faces a and b exposed to air or water — and so beveled? The two faces AND
304
+ // the cell diagonally between them must all be clear.
305
+ //
306
+ // ⚠️ THE DIAGONAL TERM IS WHAT STOPS A STAIR STEP FROM OPENING A HOLE. Two blocks touching only
307
+ // along a diagonal each see air on both faces at that edge; cutting both back on the two-face test
308
+ // alone leaves a diamond you can see the sky through. Mirrors edgeOpen() in TS — see bevelVertex.
309
+ fn edgeOpen(mask: u32, a: vec3<f32>, b: vec3<f32>) -> f32 {
310
+ if (mask == 0u) { return 0.0; } // unknown ⇒ no bevel, see CubeInstance.occ
311
+ return (1.0 - occAt(mask, a)) * (1.0 - occAt(mask, b)) * (1.0 - occAt(mask, a + b));
312
+ }
313
+ // The vertex rule. A vertex's pull along a free axis is INWARD, gated by the edge between its home
314
+ // face and the face on that side. Both primitives that share a vertex resolve the same face/axis
315
+ // pair, so they cannot land in different places — that is where watertightness comes from, and it
316
+ // is why this takes (corner, home) rather than a vertex index.
317
+ // THE NOTCH — the exact complement of edgeOpen: both faces SOLID, the diagonal between them AIR.
318
+ // Three blocks in an L around an empty cell: the two with a FACE on it chamfer normally, the third
319
+ // touches it only along one corner edge and keeps a hard arris down the inside of the notch. This is
320
+ // what softens that. Cutting it cannot open a hole — the wedge is inside this block's own cell and
321
+ // the neighbours' buried faces (drawn, like every face of every instance) close it. Mirrors
322
+ // notchOpen() in TS.
323
+ fn notchOpen(mask: u32, a: vec3<f32>, b: vec3<f32>) -> f32 {
324
+ if (mask == 0u) { return 0.0; }
325
+ return occAt(mask, a) * occAt(mask, b) * (1.0 - occAt(mask, a + b));
326
+ }
327
+ // The vertex rule. A vertex's pull along a free axis is INWARD, gated by the edge between its home
328
+ // face and the face on that side (CONVEX, full size), or by a notch it flanks, or by a notch it caps
329
+ // from the perpendicular face — see bevelVertex in TS for the full statement.
330
+ //
331
+ // ⚠️ max, NOT +, ON THE TWO NOTCH TERMS. Both hold at once on a face flanking two notches at right
332
+ // angles, and summing there pulls that corner twice as far as any neighbour agrees to — a crack.
333
+ fn bevelPos(mask: u32, c: vec3<f32>, a: vec3<f32>, bevel: f32) -> vec3<f32> {
334
+ if (bevel <= 0.0 || mask == 0u) { return c; }
335
+ let free = vec3(1.0, 1.0, 1.0) - abs(a); // the two in-face axes; the face axis is never pulled
336
+ let s = sign(c);
337
+ let ax = vec3(s.x, 0.0, 0.0);
338
+ let ay = vec3(0.0, s.y, 0.0);
339
+ let az = vec3(0.0, 0.0, s.z);
340
+ // The OTHER free axis for each axis: one of the three is masked off by "free", so the remaining
341
+ // two are each other's partner — the partner direction is the sum of the free ones minus this one.
342
+ let fx = free.x * ax; let fy = free.y * ay; let fz = free.z * az;
343
+ let sum = fx + fy + fz;
344
+ var d = vec3(0.0, 0.0, 0.0);
345
+ d.x = free.x * s.x * bevel * (edgeOpen(mask, a, ax)
346
+ + ${NOTCH_SCALE} * max(notchOpen(mask, a, ax), notchOpen(mask, ax, sum - fx)));
347
+ d.y = free.y * s.y * bevel * (edgeOpen(mask, a, ay)
348
+ + ${NOTCH_SCALE} * max(notchOpen(mask, a, ay), notchOpen(mask, ay, sum - fy)));
349
+ d.z = free.z * s.z * bevel * (edgeOpen(mask, a, az)
350
+ + ${NOTCH_SCALE} * max(notchOpen(mask, a, az), notchOpen(mask, az, sum - fz)));
351
+ return c - d;
352
+ }
115
353
  // Vertex AO for cube vertex vi, in [AO_MIN, 1]. Every cube vertex sits at a CORNER, so CUBE_POS is
116
354
  // (±0.5,±0.5,±0.5) and its sign gives the two in-plane directions once the face axis is masked off.
117
- fn cornerAO(mask: u32, vi: u32) -> f32 {
355
+ // Takes the corner and normal directly, because the BEVELED cube's vertex index no longer selects a
356
+ // corner on its own — three of its vertices can share one.
357
+ fn cornerAOAt(mask: u32, c: vec3<f32>, n: vec3<f32>) -> f32 {
118
358
  if (mask == 0u) { return 1.0; } // unknown ⇒ no AO, see CubeInstance.occ
119
- let n = CUBE_NRM[vi];
120
- let ip = sign(CUBE_POS[vi]) * (vec3(1.0, 1.0, 1.0) - abs(n));
359
+ let ip = sign(c) * (vec3(1.0, 1.0, 1.0) - abs(n));
121
360
  var a1 = vec3(0.0, 0.0, 0.0);
122
361
  var a2 = vec3(0.0, 0.0, 0.0);
123
362
  if (abs(n.x) > 0.5) { a1 = vec3(0.0, ip.y, 0.0); a2 = vec3(0.0, 0.0, ip.z); }
@@ -136,6 +375,10 @@ fn cornerAO(mask: u32, vi: u32) -> f32 {
136
375
  if (s1 > 0.5 && s2 > 0.5) { lvl = 0.0; }
137
376
  return mix(${AO_MIN}, 1.0, lvl / 3.0);
138
377
  }
378
+ // The 36-vertex cube's form, unchanged for every caller that still indexes CUBE_POS/CUBE_NRM.
379
+ fn cornerAO(mask: u32, vi: u32) -> f32 {
380
+ return cornerAOAt(mask, CUBE_POS[vi], CUBE_NRM[vi]);
381
+ }
139
382
  fn hsv(hh: f32) -> vec3<f32> {
140
383
  let h = fract(hh) * 6.0;
141
384
  let x = 1.0 - abs((h % 2.0) - 1.0);
@@ -290,10 +533,19 @@ struct GiGrid { org: vec4<f32>, dim: vec4<f32> };
290
533
  @group(0) @binding(9) var<storage, read> probes: array<vec4<f32>>;
291
534
  ${GI_WGSL}
292
535
 
536
+ // ⚠️ objN IS FLAT AND HAS TO BE. fs() picks the in-face axis pair AND the atlas top/side/bottom
537
+ // variant from the DOMINANT AXIS of objN. A bevel band spans two faces, so a smooth objN would
538
+ // sweep from one face normal to the other and flip that dominant axis halfway across — a hard seam
539
+ // straight down the middle of every rounded edge, with the tile variant changing at the same line.
540
+ // Flat gives each band one face's parameterization (the provoking vertex is the FIRST, which is why
541
+ // buildBevelCube emits the owning face's vertex first). This is a no-op for the sharp cube: all
542
+ // three vertices of a plain face triangle already carry the same objN. nrm stays smooth — that is
543
+ // the varying that does the rounding.
544
+ //
293
545
  // vao is deliberately NOT flat: per-corner AO is computed once per vertex and the rasterizer's
294
546
  // interpolation across the quad IS the gradient — that smooth ramp into a crease is the whole
295
547
  // effect, and it costs nothing.
296
- struct VOut { @builtin(position) clip: vec4<f32>, @location(0) world: vec3<f32>, @location(1) nrm: vec3<f32>, @location(2) hue: f32, @location(3) emis: f32, @location(4) blk: vec3<f32>, @location(5) objN: vec3<f32>, @location(6) tile: f32, @location(7) upc: f32, @location(8) @interpolate(flat) decal: vec2<f32>, @location(9) vao: f32, @location(10) @interpolate(flat) jit: f32 };
548
+ struct VOut { @builtin(position) clip: vec4<f32>, @location(0) world: vec3<f32>, @location(1) nrm: vec3<f32>, @location(2) hue: f32, @location(3) emis: f32, @location(4) blk: vec3<f32>, @location(5) @interpolate(flat) objN: vec3<f32>, @location(6) tile: f32, @location(7) upc: f32, @location(8) @interpolate(flat) decal: vec2<f32>, @location(9) vao: f32, @location(10) @interpolate(flat) jit: f32 };
297
549
 
298
550
  fn faceVec(code: f32) -> vec3<f32> {
299
551
  let c = i32(code);
@@ -304,11 +556,23 @@ fn faceVec(code: f32) -> vec3<f32> {
304
556
 
305
557
  @vertex fn vs(@builtin(vertex_index) vi: u32, @builtin(instance_index) ii: u32) -> VOut {
306
558
  let inst = insts[vis[ii]];
307
- let world = worldOf(inst, vi);
559
+ // The 27-bit occupancy mask, reassembled from the 14/13 split fill() had to make because an f32
560
+ // is exact only to 2^24. Drives BOTH the corner AO and the edge bevel.
561
+ let mask = u32(inst.aux2.x) | (u32(inst.aux2.y) << 14u);
562
+ // THE BEVELED CUBE. cam.eye.w is the bevel amount; at 0 — every caller that has not opted in —
563
+ // bevelPos returns the sharp corner and this is buildCube()'s geometry exactly. The draw is only
564
+ // 36 vertices long in that case anyway (see the indirect args), so the tail is never even reached.
565
+ let sharp = BEV_CORNER[vi];
566
+ let home = BEV_HOME[vi];
567
+ let local = bevelPos(mask, sharp, home, cam.eye.w);
568
+ let world = inst.pos.xyz + qrot(inst.rot, local * inst.scl.xyz);
308
569
  var o: VOut;
309
570
  o.clip = cam.camVP * vec4(world, 1.0);
310
571
  o.world = world;
311
- o.nrm = normalize(qrot(inst.rot, CUBE_NRM[vi] / inst.scl.xyz)); // normal under non-uniform scale + rotation
572
+ // SMOOTH ACROSS THE BAND, BY DESIGN. A bevel band's two long sides carry different home normals,
573
+ // and letting the rasterizer sweep between them is the entire reason a 45° cut reads as a curve
574
+ // rather than a bright faceted line. (Normal under non-uniform scale + rotation.)
575
+ o.nrm = normalize(qrot(inst.rot, home / inst.scl.xyz));
312
576
  o.hue = inst.pos.w;
313
577
  o.emis = inst.scl.w;
314
578
  // OBJECT-space block coord (per-sub-block units, anchored to the model, NOT world) so per-block AO moves +
@@ -316,12 +580,17 @@ fn faceVec(code: f32) -> vec3<f32> {
316
580
  // by inst.scl so a merged LOD cube (scl>1) still shows its sub-blocks — UNLESS aux2.w (texFit) says the
317
581
  // instance is ONE block drawn at a registered scale, where the multiply would just repeat its tile scale×.
318
582
  let tscl = select(inst.scl.xyz, vec3<f32>(1.0, 1.0, 1.0), vec3<bool>(inst.aux2.w != 0.0));
319
- o.blk = (CUBE_POS[vi] + vec3(0.5, 0.5, 0.5)) * tscl;
320
- o.objN = CUBE_NRM[vi]; // objN = pre-rotation face normal
583
+ // ⚠️ FROM THE BEVELED POSITION, NOT THE SHARP CORNER. Deriving blk from the sharp corner makes it
584
+ // CONSTANT across a band's width, and a face's far corner gives blk = 1.0, so fract(1.0) = 0.0
585
+ // would make every band sample the tile's OPPOSITE edge — a visibly wrong sliver on every rounded
586
+ // edge in the world. From the beveled position the mapping is continuous and covers the tile
587
+ // exactly once: the face spans [b, 1-b] and the two bands take [0, b] and [1-b, 1].
588
+ o.blk = (local + vec3(0.5, 0.5, 0.5)) * tscl;
589
+ o.objN = home; // objN = pre-rotation face normal, FLAT — see VOut
321
590
  o.tile = inst.aux.x; o.upc = inst.aux.y; o.decal = inst.aux.zw;
322
- // The 27-bit occupancy mask, reassembled from the 14/13 split fill() had to make because an f32
323
- // is exact only to 2^24.
324
- o.vao = cornerAO(u32(inst.aux2.x) | (u32(inst.aux2.y) << 14u), vi);
591
+ // ⚠️ AO READS THE SHARP CORNER. It is a property of that corner's neighbourhood, and its sign()
592
+ // derivation needs components that are exactly ±0.5 — a beveled position would round to nothing.
593
+ o.vao = cornerAOAt(mask, sharp, home);
325
594
  o.jit = inst.aux2.z;
326
595
  return o;
327
596
  }
@@ -682,6 +951,7 @@ export class GpuShadowRenderer {
682
951
  matQuality = 0; // see setMaterialQuality — 0 keeps the pre-material look exactly
683
952
  matCutoff = 28; // world units; parallax fades out before here
684
953
  eye = new Float32Array(3);
954
+ bevel = 0; // see setBevel — 0 keeps the sharp cube exactly, and draws only 36 vertices
685
955
  alphaPipe;
686
956
  alphaBGL; // kept so the overlay buffer can grow and rebind on demand
687
957
  overlayBuf;
@@ -1208,6 +1478,23 @@ export class GpuShadowRenderer {
1208
1478
  this.eye[1] = y;
1209
1479
  this.eye[2] = z;
1210
1480
  }
1481
+ /**
1482
+ * ROUND THE EXPOSED EDGES of every cube that supplies a `CubeInstance.occ` mask, by `amount`
1483
+ * block-fractions (0.06 is a subtle rim; 0.2 is pebble-like). 0 — the default — is the sharp cube.
1484
+ *
1485
+ * Only edges open to air on BOTH adjacent faces and on the cell diagonally between them are cut
1486
+ * back, so blocks that touch still meet flush and a stair step cannot open a hole. An instance
1487
+ * without a mask (`occ` absent ⇒ 0, the "unknown" sentinel) is never beveled, which is why the far
1488
+ * field, sky-body shells, ships, creatures and props stay sharp with no work on their part.
1489
+ *
1490
+ * ⚠️ NOT FREE WHILE IT IS ON. A non-zero amount draws 132 vertices per instance instead of 36 in
1491
+ * the CAMERA pass — for every instance, beveled or not, because they all ride one indirect draw.
1492
+ * The shadow pass keeps the sharp cube either way. At 0 the vertex count drops back to 36, so
1493
+ * leaving this alone costs exactly what it cost before the feature existed.
1494
+ */
1495
+ setBevel(amount) {
1496
+ this.bevel = Math.max(0, amount);
1497
+ }
1211
1498
  setAtlas(tex, layout) {
1212
1499
  this.atlasView = tex.createView({ dimension: '2d-array' });
1213
1500
  const ids = Object.keys(layout.tiles).map(Number);
@@ -1480,6 +1767,7 @@ export class GpuShadowRenderer {
1480
1767
  this.cam[S + 24] = this.eye[0];
1481
1768
  this.cam[S + 25] = this.eye[1];
1482
1769
  this.cam[S + 26] = this.eye[2];
1770
+ this.cam[S + 27] = this.bevel; // eye.w — the edge bevel amount, see setBevel
1483
1771
  this.device.queue.writeBuffer(this.camBuf, 0, this.cam);
1484
1772
  // Each cascade's shadow pass reads its OWN copy with that cascade's matrix parked at lightVP[0].
1485
1773
  for (let i = 0; i < nc; i++) {
@@ -1493,10 +1781,15 @@ export class GpuShadowRenderer {
1493
1781
  // GPU-DRIVEN CULL: reset each frustum's indirect args ([vertexCount=36, instanceCount=0, 0, 0]), then compute-cull
1494
1782
  // — main pass vs the camera frustum (+ Hi-Z occlusion), shadow pass vs the light frustum. Each dispatch atomically
1495
1783
  // compacts its survivors into a visible list + counts them in instanceCount, which the matching drawIndirect consumes.
1496
- const resetArgs = new Uint32Array([36, 0, 0, 0]);
1497
- this.device.queue.writeBuffer(this.camArgs, 0, resetArgs);
1784
+ // ⚠️ TWO RESETS, NOT ONE, AND THE VERTEX COUNTS DIFFER ON PURPOSE. The camera pass draws the
1785
+ // beveled cube's 132 vertices only while a bevel is actually set; at 0 it draws the first 36,
1786
+ // which buildBevelCube guarantees ARE the sharp cube. The shadow pass always draws 36 — a 6% cut
1787
+ // is invisible in a shadow map and there is no reason to quadruple the cascades' vertex work.
1788
+ const camVerts = this.bevel > 0 ? BEVEL_VERTS : SHARP_VERTS;
1789
+ this.device.queue.writeBuffer(this.camArgs, 0, new Uint32Array([camVerts, 0, 0, 0]));
1790
+ const lightReset = new Uint32Array([SHARP_VERTS, 0, 0, 0]);
1498
1791
  for (const a of this.lightArgs)
1499
- this.device.queue.writeBuffer(a, 0, resetArgs);
1792
+ this.device.queue.writeBuffer(a, 0, lightReset);
1500
1793
  this.writeCull(this.camCull, camVP, occludeNow);
1501
1794
  for (let i = 0; i < nc; i++)
1502
1795
  this.writeCull(this.lightCull[i], mats[i], false);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ugly-game",
3
- "version": "0.15.5",
3
+ "version": "0.15.7",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {