ugly-game 0.15.17 → 0.15.18

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.
@@ -14,7 +14,8 @@ export interface CubeInstance {
14
14
  *
15
15
  * `>= 0` this material, on every face of the instance.
16
16
  * `-1` (the default) flat hue — no atlas lookup at all.
17
- * `-2` PATCHWORK: `faceMats` carries six material ids, one per face. See `packFaceMats`.
17
+ * `-2` ONE MATERIAL PER FACE: `faceMats` carries six ids. See `packFaceMats`.
18
+ * `-3` PATCHWORK: `patch` points into `setFacePatches`' table, `res` texels per face edge.
18
19
  */
19
20
  tile?: number;
20
21
  /**
@@ -30,6 +31,22 @@ export interface CubeInstance {
30
31
  * 2^24, so hand-packing it wrong is silent.
31
32
  */
32
33
  faceMats?: [number, number];
34
+ /**
35
+ * PATCHWORK — only read when `tile === -3`. `at` is this instance's offset into the table uploaded by
36
+ * `setFacePatches`; `res` is its texels per face edge.
37
+ *
38
+ * ⚠️ IT SHARES THE SAME TWO SLOTS AS `faceMats`, and for the same reason: a patchwork instance is a merged
39
+ * LOD box whose neighbours are not blocks, so its 27-bit occupancy mask was already the 0 sentinel. The
40
+ * `tile` value is what decides which of the three readings applies, and no instance has two of them.
41
+ *
42
+ * The table holds MATERIAL IDS, not pixels — every material already has an atlas layer, so a patch says
43
+ * which layer each texel wants and the existing atlas draws it. Changing an instance's LOD rewrites a few
44
+ * bytes here; there is nothing to re-bake.
45
+ */
46
+ patch?: {
47
+ at: number;
48
+ res: number;
49
+ };
33
50
  up?: number;
34
51
  /**
35
52
  * CONTACT DECAL, packed. Both default to 0, which is exactly what fill() already wrote into these
@@ -483,6 +500,7 @@ export declare class GpuShadowRenderer {
483
500
  private atlasView;
484
501
  private atlasSampler;
485
502
  private tileLayerBuf;
503
+ private facePatchBuf;
486
504
  private useAtlas;
487
505
  /**
488
506
  * How much of the per-voxel edge AO survives on ATLAS-textured blocks, 0..1.
@@ -552,6 +570,18 @@ export declare class GpuShadowRenderer {
552
570
  constructor(device: GPUDevice, format: GPUTextureFormat, opts?: {
553
571
  bevelSeg?: number;
554
572
  });
573
+ /**
574
+ * Upload the patchwork table: one material id per face texel, for instances drawn with `tile === -3`.
575
+ *
576
+ * ⚠️ IDS, NOT PIXELS, AND THAT IS THE WHOLE DESIGN. Every material already has its own layer in the block
577
+ * atlas and the shader already turns an id into a layer, so a patchwork face needs nothing new drawn — it
578
+ * needs to know WHICH material each texel is. There is no bake, no cook step, and nothing to re-bake when
579
+ * a caller changes an instance's LOD: it rewrites a few bytes of index. A 4x4 face is 16 entries.
580
+ *
581
+ * Layout per instance: `6 faces x res x res`, row-major, from the offset the instance carries in
582
+ * `patchAt`. Face order is `faceIndex`'s: 0 +X, 1 -X, 2 +Y, 3 -Y, 4 +Z, 5 -Z.
583
+ */
584
+ setFacePatches(ids: Uint32Array<ArrayBuffer>): void;
555
585
  /** Bytes per bucket in a visible-index list: the instance capacity, rounded up to the 256-byte
556
586
  * alignment a storage-buffer binding offset requires. */
557
587
  private visStride;
package/dist/gpuShadow.js CHANGED
@@ -2010,6 +2010,10 @@ export const mainWgsl = (seg, B, V, S) => commonWgsl(seg, B, V, S) +
2010
2010
  @group(0) @binding(5) var atlasTex: texture_2d_array<f32>;
2011
2011
  @group(0) @binding(6) var atlasSamp: sampler;
2012
2012
  @group(0) @binding(7) var<storage, read> tileLayers: array<u32>; // array layer per (material*3 + face-variant)
2013
+ // PATCHWORK: material id per face texel, for instances with tile == -3. Laid out per instance as
2014
+ // 6 faces x res x res, row-major, starting at the offset the instance carries. NOT pixels — every material
2015
+ // already has its own atlas layer, so a patch says WHICH layer each texel wants and the atlas draws it.
2016
+ @group(0) @binding(10) var<storage, read> facePatch: array<u32>;
2013
2017
  // GLOBAL ILLUMINATION (giProbes.ts). org.xyz = grid origin, org.w = cell size; dim.xyz = nx,ny,nz,
2014
2018
  // dim.w = enable. A caller that never calls setGI leaves dim.w at 0, giAt returns zero, and this is
2015
2019
  // byte-identical to the flat-ambient version it replaced.
@@ -2030,7 +2034,7 @@ ${GI_WGSL}
2030
2034
  // vao is deliberately NOT flat: per-corner AO is computed once per vertex and the rasterizer's
2031
2035
  // interpolation across the quad IS the gradient — that smooth ramp into a crease is the whole
2032
2036
  // effect, and it costs nothing.
2033
- 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, @location(11) @interpolate(flat) fmat: vec2<f32> };
2037
+ 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, @location(11) @interpolate(flat) fmat: vec2<f32>, @location(12) f01: vec3<f32> };
2034
2038
 
2035
2039
  // Which of the six faces an object-space normal points along: 0 +X, 1 -X, 2 +Y, 3 -Y, 4 +Z, 5 -Z.
2036
2040
  // Used by the PATCHWORK path to pick that face's own material — see tile == -2 in CubeInstance.
@@ -2214,6 +2218,9 @@ fn faceVec(code: f32) -> vec3<f32> {
2214
2218
  // edge in the world. From the beveled position the mapping is continuous and covers the tile
2215
2219
  // exactly once: the face spans [b, 1-b] and the two bands take [0, b] and [1-b, 1].
2216
2220
  o.blk = (local + vec3(0.5, 0.5, 0.5)) * tscl;
2221
+ // The SAME quantity before the scale: 0..1 across the whole instance, which is what a patchwork texel
2222
+ // index is measured in. Deriving it in the fragment would need inst.scl, which is not a varying.
2223
+ o.f01 = local + vec3(0.5, 0.5, 0.5);
2217
2224
  o.objN = home; // objN = pre-rotation face normal, FLAT — see VOut
2218
2225
  o.tile = inst.aux.x; o.upc = inst.aux.y; o.decal = inst.aux.zw;
2219
2226
  // ⚠️ AO READS THE SHARP CORNER. It is a property of that corner's neighbourhood, and its sign()
@@ -2331,7 +2338,20 @@ fn fixedFill(N: vec3<f32>, up: vec3<f32>) -> f32 {
2331
2338
  // cells and can only be one shape, but its six faces look at six different pieces of ground, and
2332
2339
  // painting all of them with one material is what makes coarse terrain read as plastic.
2333
2340
  var matId = i.tile;
2334
- if (i.tile < -1.5) {
2341
+ if (i.tile < -2.5) {
2342
+ // PATCHWORK. aux2.x is this instance's offset into facePatch, aux2.y its texels per edge. The face is
2343
+ // divided into res x res texels and each names its own material; the tile UV below is UNCHANGED, so the
2344
+ // texture keeps the density it always had and only the LAYER varies across the face.
2345
+ let f = faceIndex(i.objN);
2346
+ let res = max(1u, u32(i.fmat.y));
2347
+ var a = i.f01.y; var b = i.f01.z; // the two axes this face spans
2348
+ if (f == 2u || f == 3u) { a = i.f01.x; b = i.f01.z; }
2349
+ if (f == 4u || f == 5u) { a = i.f01.x; b = i.f01.y; }
2350
+ let tu = min(res - 1u, u32(clamp(a, 0.0, 0.999) * f32(res)));
2351
+ let tv = min(res - 1u, u32(clamp(b, 0.0, 0.999) * f32(res)));
2352
+ let at = u32(i.fmat.x) + f * res * res + tv * res + tu;
2353
+ matId = f32(facePatch[at]);
2354
+ } else if (i.tile < -1.5) {
2335
2355
  let f = faceIndex(i.objN);
2336
2356
  let word = u32(select(i.fmat.x, i.fmat.y, f >= 3u));
2337
2357
  matId = f32((word >> ((f % 3u) * 8u)) & 255u);
@@ -2590,6 +2610,7 @@ export class GpuShadowRenderer {
2590
2610
  atlasView;
2591
2611
  atlasSampler;
2592
2612
  tileLayerBuf;
2613
+ facePatchBuf;
2593
2614
  useAtlas = 0;
2594
2615
  /**
2595
2616
  * How much of the per-voxel edge AO survives on ATLAS-textured blocks, 0..1.
@@ -2739,6 +2760,11 @@ export class GpuShadowRenderer {
2739
2760
  visibility: GPUShaderStage.FRAGMENT,
2740
2761
  buffer: { type: 'uniform' },
2741
2762
  }, // GI grid descriptor
2763
+ {
2764
+ binding: 10,
2765
+ visibility: GPUShaderStage.FRAGMENT,
2766
+ buffer: { type: 'read-only-storage' },
2767
+ }, // patchwork: material id per face texel — see facePatch in the WGSL
2742
2768
  {
2743
2769
  binding: 9,
2744
2770
  visibility: GPUShaderStage.FRAGMENT,
@@ -2928,6 +2954,32 @@ export class GpuShadowRenderer {
2928
2954
  size: 16,
2929
2955
  usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
2930
2956
  });
2957
+ // A one-entry placeholder so the bind group is always complete. A caller that never sets patches never
2958
+ // emits a `tile === -3` instance either, so nothing reads it.
2959
+ this.facePatchBuf = device.createBuffer({
2960
+ size: 16,
2961
+ usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
2962
+ });
2963
+ }
2964
+ /**
2965
+ * Upload the patchwork table: one material id per face texel, for instances drawn with `tile === -3`.
2966
+ *
2967
+ * ⚠️ IDS, NOT PIXELS, AND THAT IS THE WHOLE DESIGN. Every material already has its own layer in the block
2968
+ * atlas and the shader already turns an id into a layer, so a patchwork face needs nothing new drawn — it
2969
+ * needs to know WHICH material each texel is. There is no bake, no cook step, and nothing to re-bake when
2970
+ * a caller changes an instance's LOD: it rewrites a few bytes of index. A 4x4 face is 16 entries.
2971
+ *
2972
+ * Layout per instance: `6 faces x res x res`, row-major, from the offset the instance carries in
2973
+ * `patchAt`. Face order is `faceIndex`'s: 0 +X, 1 -X, 2 +Y, 3 -Y, 4 +Z, 5 -Z.
2974
+ */
2975
+ setFacePatches(ids) {
2976
+ const bytes = Math.max(16, ids.byteLength);
2977
+ this.facePatchBuf = this.device.createBuffer({
2978
+ size: bytes,
2979
+ usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
2980
+ });
2981
+ this.device.queue.writeBuffer(this.facePatchBuf, 0, ids);
2982
+ this.makeBindGroups();
2931
2983
  }
2932
2984
  /** Bytes per bucket in a visible-index list: the instance capacity, rounded up to the 256-byte
2933
2985
  * alignment a storage-buffer binding offset requires. */
@@ -3080,9 +3132,10 @@ export class GpuShadowRenderer {
3080
3132
  // ⚠️ PATCHWORK WRITES THE SAME TWO SLOTS. `tile === -2` is the gate the shader reads, so the two
3081
3133
  // encodings can never be live at once — see `CubeInstance.faceMats`.
3082
3134
  const fm = c.tile === -2 ? c.faceMats : undefined;
3135
+ const pt = c.tile === -3 ? c.patch : undefined;
3083
3136
  const occ = c.occ ?? 0;
3084
- scratch[o + 16] = fm ? fm[0] : occ & 0x3fff;
3085
- scratch[o + 17] = fm ? fm[1] : (occ / 0x4000) | 0;
3137
+ scratch[o + 16] = pt ? pt.at : fm ? fm[0] : occ & 0x3fff;
3138
+ scratch[o + 17] = pt ? pt.res : fm ? fm[1] : (occ / 0x4000) | 0;
3086
3139
  scratch[o + 18] = c.jitter ?? 0;
3087
3140
  scratch[o + 19] = c.texFit ?? 0; // texFit: one atlas tile per face at any draw scale (see CubeInstance.texFit)
3088
3141
  }
@@ -3121,6 +3174,7 @@ export class GpuShadowRenderer {
3121
3174
  { binding: 7, resource: { buffer: this.tileLayerBuf } },
3122
3175
  { binding: 8, resource: { buffer: this.giGridBuf } },
3123
3176
  { binding: 9, resource: { buffer: this.giProbeBuf } },
3177
+ { binding: 10, resource: { buffer: this.facePatchBuf } },
3124
3178
  ],
3125
3179
  }));
3126
3180
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ugly-game",
3
- "version": "0.15.17",
3
+ "version": "0.15.18",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {