ugly-game 0.15.0 → 0.15.2

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.
@@ -445,12 +445,16 @@ export class GpuPropsRenderer {
445
445
  },
446
446
  ],
447
447
  },
448
- // Depth TEST stays on — a crystal behind a hill is still behind the hill. Depth WRITE goes:
449
- // otherwise the translucent surface occludes whatever is drawn after it, which is the whole
450
- // thing this exists to prevent.
448
+ // Depth TEST stays on — a crystal behind a hill is still behind the hill. Depth WRITE stays
449
+ // TOO: consumers may draw later passes (e.g. all-mine-sky's water) that depth-test against
450
+ // this buffer, and a translucent prop that writes no depth lets those passes composite OVER
451
+ // it even when the prop sits in front of them. With depth written, a nearer translucent prop
452
+ // occludes later passes correctly. The cost: overlapping translucent props no longer blend
453
+ // in submission order — the nearer one clips the farther. For the glassy species (ice
454
+ // crystals, tektite shards) that is the right trade.
451
455
  depthStencil: {
452
456
  format: 'depth24plus',
453
- depthWriteEnabled: false,
457
+ depthWriteEnabled: true,
454
458
  depthCompare: 'less',
455
459
  },
456
460
  });
@@ -24,6 +24,35 @@ export interface CubeInstance {
24
24
  */
25
25
  decalPos?: number;
26
26
  decalKind?: number;
27
+ /**
28
+ * NEIGHBOUR OCCUPANCY, 27 bits — which of the 26 cells around this one are solid.
29
+ *
30
+ * Bit `(dx+1) + (dy+1)*3 + (dz+1)*9` for the neighbour at offset (dx,dy,dz) ∈ {−1,0,1}³. Bit 13
31
+ * is this cell itself and is ignored. Feeding this lets the vertex shader compute real per-corner
32
+ * ambient occlusion — the dark that collects where two surfaces meet — which is the cue that
33
+ * stops a field of identical blocks from reading as one flat sheet.
34
+ *
35
+ * ⚠️ 0 MEANS UNKNOWN, NOT "NOTHING AROUND ME". A caller that never sets this gets AO of exactly
36
+ * 1.0 and is byte-identical to a build before corner AO existed. That matters because 0 is also
37
+ * the honest encoding of "floating in mid-air", and a renderer that treated the two alike would
38
+ * darken every corner of every cube in every game that has not opted in. The sentinel costs the
39
+ * ability to distinguish a genuinely isolated block, which needs no AO anyway.
40
+ *
41
+ * ⚠️ AND IT IS 27 BITS, WHICH AN f32 CANNOT HOLD. Floats are exact only to 2^24, so `fill()`
42
+ * splits this across two slots. Keep it a plain JS number here — a double holds it exactly — and
43
+ * let the packing be the renderer's problem.
44
+ */
45
+ occ?: number;
46
+ /**
47
+ * PER-BLOCK ALBEDO JITTER, signed, roughly ±0.1. Multiplies the base colour, so a large field of
48
+ * one material stops reading as a single painted sheet.
49
+ *
50
+ * ⚠️ COMPUTED BY THE CALLER, NOT HASHED FROM WORLD POSITION IN THE SHADER, and that is not a
51
+ * stylistic choice. Under a floating origin the world coordinate handed to the shader is REBASED,
52
+ * so a position hash would re-roll every block the instant the origin moved — the whole landscape
53
+ * visibly re-patterning as you walk. The caller knows the stable coordinate; the shader does not.
54
+ */
55
+ jitter?: number;
27
56
  }
28
57
  /** Atlas layout: which ARRAY LAYER a material's top/side/bottom faces sample.
29
58
  *
package/dist/gpuShadow.js CHANGED
@@ -23,8 +23,18 @@ const SHADOW_SIZE = 2048;
23
23
  // one sample, and the selection loop terminates immediately. That keeps the five demo pages and
24
24
  // AMS's existing props/doors/skin path unmoved until each opts in.
25
25
  const MAX_CASCADES = 4;
26
+ // How dark a fully-enclosed corner goes. 0.55 is a real crease without inking the world black; the
27
+ // term is a MULTIPLY on the lit result, so this is the floor the deepest corner reaches.
28
+ const AO_MIN = 0.55;
26
29
  const CULL_WG = 64; // compute-cull workgroup size
27
- const INST_F = 16; // floats per instance: pos.xyz+hue, scl.xyz+emissive, rot quat, aux (tile,up,_,_)
30
+ // Floats per instance: pos.xyz+hue, scl.xyz+emissive, rot quat, aux (tile, up, decalPos, decalKind),
31
+ // aux2 (occLo, occHi, jitter, _).
32
+ //
33
+ // ⚠️ 20, NOT 17, AND THE PADDING IS NOT WASTE. `Inst` is read as a `array<Inst>` of vec4s, so WGSL
34
+ // rounds the struct's stride up to a multiple of 16 bytes: one more float would cost a whole vec4
35
+ // anyway. Spending the other two on a spare slot is free, and the last time this buffer grew it was
36
+ // for the contact decal, which had to widen it for exactly the same reason.
37
+ const INST_F = 20;
28
38
  const OVERLAY_CAP = 512; // max translucent overlay cubes per frame (the place ghost + headroom)
29
39
  // Build the 36-vertex cube (6 faces × 2 tris) in JS and inject as WGSL const arrays —
30
40
  // bulletproof + readable vs. bit-twiddling from vertex_index. cullMode is 'none', so winding
@@ -72,7 +82,7 @@ struct Cam {
72
82
  csm: vec4<f32>, // x = active cascade count · y = crossfade band in uv units
73
83
  eye: vec4<f32>, // xyz = camera position in world space (material relief + specular); w unused
74
84
  };
75
- struct Inst { pos: vec4<f32>, scl: vec4<f32>, rot: vec4<f32>, aux: 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; see setAtlasAO/CubeInstance.decal*)
85
+ 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 spare
76
86
  @group(0) @binding(0) var<uniform> cam: Cam;
77
87
  @group(0) @binding(1) var<storage, read> insts: array<Inst>;
78
88
 
@@ -81,6 +91,51 @@ const CUBE_NRM = array<vec3<f32>, 36>(${CUBE.nrm});
81
91
 
82
92
  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); }
83
93
  fn worldOf(inst: Inst, vi: u32) -> vec3<f32> { return inst.pos.xyz + qrot(inst.rot, CUBE_POS[vi] * inst.scl.xyz); }
94
+
95
+ // ── PER-CORNER AMBIENT OCCLUSION ────────────────────────────────────────────────────────────────
96
+ //
97
+ // The dark that collects where two surfaces meet: the inside of a step, the base of a wall, the
98
+ // corner of a pit. A voxel world has that geometry everywhere and no cue for it, so a hillside of
99
+ // identical blocks reads as one painted sheet. blockShade() below darkens every block's rim by the
100
+ // same amount whatever is beside it, which draws the GRID but says nothing about SHAPE — an inside
101
+ // corner and open ground come out identical.
102
+ //
103
+ // This is the classic voxel solution and it is strictly better than the screen-space pass it
104
+ // replaces: it reads actual neighbour occupancy instead of estimating a normal from neighbouring
105
+ // depth, so it cannot invent shading from a coarse mesh's faceting, it costs nothing per frame
106
+ // (three bit tests per vertex, then the rasterizer interpolates for free), and it can only ever
107
+ // touch geometry whose caller supplied a mask.
108
+ //
109
+ // occAt reads one neighbour out of the 27-bit mask; the index is the same
110
+ // (dx+1) + (dy+1)*3 + (dz+1)*9 the caller packed with.
111
+ fn occAt(mask: u32, o: vec3<f32>) -> f32 {
112
+ let idx = u32((o.x + 1.0) + (o.y + 1.0) * 3.0 + (o.z + 1.0) * 9.0);
113
+ return f32((mask >> idx) & 1u);
114
+ }
115
+ // Vertex AO for cube vertex vi, in [AO_MIN, 1]. Every cube vertex sits at a CORNER, so CUBE_POS is
116
+ // (±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 {
118
+ 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));
121
+ var a1 = vec3(0.0, 0.0, 0.0);
122
+ var a2 = vec3(0.0, 0.0, 0.0);
123
+ if (abs(n.x) > 0.5) { a1 = vec3(0.0, ip.y, 0.0); a2 = vec3(0.0, 0.0, ip.z); }
124
+ else if (abs(n.y) > 0.5) { a1 = vec3(ip.x, 0.0, 0.0); a2 = vec3(0.0, 0.0, ip.z); }
125
+ else { a1 = vec3(ip.x, 0.0, 0.0); a2 = vec3(0.0, ip.y, 0.0); }
126
+ // The three cells that can shade this corner, all one step OUT along the face normal: the two
127
+ // edge neighbours and the one diagonally between them.
128
+ let s1 = occAt(mask, n + a1);
129
+ let s2 = occAt(mask, n + a2);
130
+ let sc = occAt(mask, n + a1 + a2);
131
+ // ⚠️ TWO TOUCHING SIDES IS THE DARKEST CASE, DARKER THAN THE SUM SUGGESTS. When both edges are
132
+ // solid the corner is enclosed whatever the diagonal does — and the diagonal is not even visible
133
+ // from there. Falling through to 3 − (s1+s2+sc) would light an enclosed corner more brightly when
134
+ // the cell diagonally behind it happens to be air, which is backwards.
135
+ var lvl = 3.0 - (s1 + s2 + sc);
136
+ if (s1 > 0.5 && s2 > 0.5) { lvl = 0.0; }
137
+ return mix(${AO_MIN}, 1.0, lvl / 3.0);
138
+ }
84
139
  fn hsv(hh: f32) -> vec3<f32> {
85
140
  let h = fract(hh) * 6.0;
86
141
  let x = 1.0 - abs((h % 2.0) - 1.0);
@@ -110,7 +165,12 @@ fn hsv(hh: f32) -> vec3<f32> {
110
165
  // geometry. occ.x toggles it (camera pass only; the shadow pass never occludes — an off-screen occluder still
111
166
  // casts a shadow into view). occ.yz = hiz dims, occ.w = pixels/texel.
112
167
  const CULL_WGSL = /* wgsl */ `
113
- struct Inst { pos: vec4<f32>, scl: vec4<f32>, rot: vec4<f32>, aux: vec4<f32> };
168
+ // ⚠️ THE SAME STRIDE AS THE DRAW PASSES' Inst, and it has to be kept that way BY HAND — this is a
169
+ // second declaration of one buffer's layout, not a shared one. The cull pass only reads pos/scl/rot,
170
+ // so a stale copy still compiles and still runs; it just indexes every instance past the first at
171
+ // the wrong offset, and the symptom is cubes winking out of existence at the edge of view rather
172
+ // than anything that looks like a struct mismatch.
173
+ struct Inst { pos: vec4<f32>, scl: vec4<f32>, rot: vec4<f32>, aux: vec4<f32>, aux2: vec4<f32> };
114
174
  struct Args { vc: u32, ic: atomic<u32>, fv: u32, fi: u32 };
115
175
  struct CullCam {
116
176
  planes: array<vec4<f32>, 6>, cnt: vec4<f32>, // cnt.x = instance count
@@ -230,7 +290,10 @@ struct GiGrid { org: vec4<f32>, dim: vec4<f32> };
230
290
  @group(0) @binding(9) var<storage, read> probes: array<vec4<f32>>;
231
291
  ${GI_WGSL}
232
292
 
233
- 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> };
293
+ // vao is deliberately NOT flat: per-corner AO is computed once per vertex and the rasterizer's
294
+ // interpolation across the quad IS the gradient — that smooth ramp into a crease is the whole
295
+ // 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 };
234
297
 
235
298
  fn faceVec(code: f32) -> vec3<f32> {
236
299
  let c = i32(code);
@@ -254,6 +317,10 @@ fn faceVec(code: f32) -> vec3<f32> {
254
317
  o.blk = (CUBE_POS[vi] + vec3(0.5, 0.5, 0.5)) * inst.scl.xyz;
255
318
  o.objN = CUBE_NRM[vi];
256
319
  o.tile = inst.aux.x; o.upc = inst.aux.y; o.decal = inst.aux.zw;
320
+ // The 27-bit occupancy mask, reassembled from the 14/13 split fill() had to make because an f32
321
+ // is exact only to 2^24.
322
+ o.vao = cornerAO(u32(inst.aux2.x) | (u32(inst.aux2.y) << 14u), vi);
323
+ o.jit = inst.aux2.z;
257
324
  return o;
258
325
  }
259
326
 
@@ -471,6 +538,13 @@ fn fixedFill(N: vec3<f32>, up: vec3<f32>) -> f32 {
471
538
  // grass; pulling blue down hardest is what makes the eye call it earth rather than shadow. Still a
472
539
  // MULTIPLY, so the atlas grain and the planet tint survive and no per-biome palette is needed.
473
540
  base = base * mix(vec3(1.0), vec3(0.72, 0.52, 0.34), soil);
541
+ // PER-BLOCK VALUE JITTER. A hillside is thousands of instances of one material, and one material
542
+ // is one colour, so it reads as a painted sheet no matter how well it is lit. A few percent of
543
+ // variation block to block breaks that up at no cost — the eye stops seeing a surface and starts
544
+ // seeing blocks. MULTIPLICATIVE and signed, so it darkens and lightens about the material's own
545
+ // colour rather than pushing the whole field one way, and so it survives on textured and flat-hue
546
+ // blocks alike. 0 when the caller supplies none, which is exactly the old behaviour.
547
+ base = base * (1.0 + i.jit);
474
548
  // per-block edge AO makes each voxel visible on FLAT-hue cubes, but reads as a hard GRID over textures — so the
475
549
  // atlas path dropped it ENTIRELY, which left textured terrain with no per-voxel AO at all: only SSAO, which
476
550
  // resolves a pixel or two of crease and nothing at the metre scale. The result is a surface that reads as a slab,
@@ -481,7 +555,13 @@ fn fixedFill(N: vec3<f32>, up: vec3<f32>) -> f32 {
481
555
  // Around 0.35, EDGE_DARK 0.6 yields a floor of 0.86: enough that a cube reads as a cube, far short of the inked
482
556
  // grid the full term draws. (No backticks in here: this is inside a JS template literal.)
483
557
  let bs = blockShade(fx, fz);
484
- let ao = select(bs, mix(1.0, bs, cam.atlas.y), cam.atlas.x > 0.5 && i.tile >= 0.0) * contact;
558
+ // ⚠️ CORNER AO MULTIPLIES IN ON TOP OF blockShade RATHER THAN REPLACING IT, because the two say
559
+ // different things and a voxel world wants both. blockShade is an OUTLINE — it marks where one
560
+ // block ends and the next begins, identically everywhere, which is what keeps a flat field
561
+ // legible as blocks. i.vao is SHAPE — it darkens only where neighbours actually enclose this
562
+ // corner. Dropping the outline for the shape term would make flat ground read smooth again;
563
+ // dropping the shape term is what left the world reading as a slab in the first place.
564
+ let ao = select(bs, mix(1.0, bs, cam.atlas.y), cam.atlas.x > 0.5 && i.tile >= 0.0) * contact * i.vao;
485
565
  // LIGHTING = fixed directional FILL (always on, reads 3D shape in the dark) + the sun KEY (primary, shadowed).
486
566
  // cam.sun.w is the fill/ambient brightness knob; the fill's directional term (0.5..1.35) keeps every face's
487
567
  // orientation legible day or night, while the sun adds bright, shadow-casting key light on top during the day.
@@ -1034,6 +1114,14 @@ export class GpuShadowRenderer {
1034
1114
  scratch[o + 13] = c.up ?? 1;
1035
1115
  scratch[o + 14] = c.decalPos ?? 0;
1036
1116
  scratch[o + 15] = c.decalKind ?? 0; // aux: atlas tile id, up-face code, contact decal (0 = none)
1117
+ // aux2. The 27-bit occupancy mask SPLIT 14/13 because an f32 is only exact to 2^24 — packing
1118
+ // it whole would silently round the high bits and shade corners off neighbours that are not
1119
+ // there. Both halves are 0 when `occ` is absent, which is the "unknown ⇒ no AO" sentinel.
1120
+ const occ = c.occ ?? 0;
1121
+ scratch[o + 16] = occ & 0x3fff;
1122
+ scratch[o + 17] = (occ / 0x4000) | 0;
1123
+ scratch[o + 18] = c.jitter ?? 0;
1124
+ scratch[o + 19] = 0; // spare
1037
1125
  }
1038
1126
  }
1039
1127
  makeBindGroups() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ugly-game",
3
- "version": "0.15.0",
3
+ "version": "0.15.2",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {