ugly-game 0.14.4 → 0.14.6

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.
@@ -22,6 +22,20 @@ export interface SkinCreature {
22
22
  */
23
23
  uvs?: Float32Array;
24
24
  tex?: GPUTexture;
25
+ /**
26
+ * A SECOND albedo, and the fraction of it to show. `mix` of 0 is `tex` alone.
27
+ *
28
+ * ⚠️ Added for a character builder whose skin tone is a continuum between exactly two shipped
29
+ * maps, where snapping between them mid-drag is the most visible artefact on the page. Blending
30
+ * on the GPU costs one binding and one `mix`; compositing on the CPU would mean re-uploading a
31
+ * 2048² texture every time a slider moves.
32
+ *
33
+ * ⚠️ Both omitted ⇒ `tex2` binds the same 1×1 white texture as `tex` does and `mix` is 0, so
34
+ * `mix(a, white, 0)` is `a` and every existing caller renders byte-identically. That is the same
35
+ * contract `tex` itself shipped under; keep it.
36
+ */
37
+ tex2?: GPUTexture;
38
+ mix?: number;
25
39
  indices: Uint32Array;
26
40
  palette: Float32Array;
27
41
  model: Float32Array;
@@ -40,6 +54,10 @@ export declare class GpuSkinRenderer {
40
54
  private whiteTex;
41
55
  private albedoSamp;
42
56
  private matBGs;
57
+ /** A stable id per GPUTexture, so a PAIR can key a Map without stringifying the objects. */
58
+ private texIds;
59
+ private nextTexId;
60
+ private texId;
43
61
  constructor(device: GPUDevice, format: GPUTextureFormat);
44
62
  setCreatures(list: SkinCreature[]): void;
45
63
  /** Deform + draw every creature with the shared scene lighting. load=true composites over the world;
@@ -24,7 +24,7 @@ fn rot(m: mat4x4f, v: vec3f) -> vec3f { return mat3x3f(m[0].xyz, m[1].xyz, m[2].
24
24
  const DRAW = /* wgsl */ `
25
25
  // csm + cascades are APPENDED so every field above keeps its byte offset — a caller that knows
26
26
  // nothing about cascades writes exactly the bytes it always did. lightVP stays as the legacy slot.
27
- struct U { mvp: mat4x4<f32>, lightVP: mat4x4<f32>, sun: vec4<f32>, sunCol: vec4<f32>, amb: vec4<f32>, cam: vec4<f32>, fog: vec4<f32>, model: mat4x4<f32>, csm: vec4<f32>, cascades: array<mat4x4<f32>, ${MAX_CASCADES}> };
27
+ struct U { mvp: mat4x4<f32>, lightVP: mat4x4<f32>, sun: vec4<f32>, sunCol: vec4<f32>, amb: vec4<f32>, cam: vec4<f32>, fog: vec4<f32>, model: mat4x4<f32>, csm: vec4<f32>, cascades: array<mat4x4<f32>, ${MAX_CASCADES}>, blend: vec4<f32> };
28
28
  @group(0) @binding(0) var<uniform> u: U;
29
29
  @group(1) @binding(0) var shadowMap: texture_depth_2d_array; // the SAME cascaded sun shadow map the terrain cubes sample
30
30
  @group(1) @binding(1) var shadowSamp: sampler_comparison;
@@ -32,6 +32,7 @@ struct U { mvp: mat4x4<f32>, lightVP: mat4x4<f32>, sun: vec4<f32>, sunCol: vec4<
32
32
  // untextured path is byte-identical to before this existed.
33
33
  @group(2) @binding(0) var albedo: texture_2d<f32>;
34
34
  @group(2) @binding(1) var albedoSamp: sampler;
35
+ @group(2) @binding(2) var albedo2: texture_2d<f32>;
35
36
  struct VOut { @builtin(position) pos: vec4<f32>, @location(0) world: vec3<f32>, @location(1) col: vec3<f32>, @location(2) n: vec3<f32>, @location(3) uv: vec2<f32> };
36
37
  // o.world must be the WORLD position (model·p), not the local deformed p — else the fog distance is computed
37
38
  // against the world-space camera and blows up on a planet surface (huge coords), washing creatures into the fog.
@@ -69,7 +70,13 @@ fn shadowFactor(world: vec3<f32>, ndl: f32) -> f32 {
69
70
  @fragment fn fs(i: VOut) -> @location(0) vec4<f32> {
70
71
  let d = clamp(dot(normalize(i.n), -normalize(u.sun.xyz)), 0.0, 1.0);
71
72
  let sh = mix(1.0, shadowFactor(i.world, d), u.sun.w); // u.sun.w = shadow-receive enable (0 ⇒ off, byte-identical for demo)
72
- let base = i.col * textureSample(albedo, albedoSamp, i.uv).rgb;
73
+ // ⚠️ TWO albedos and a lerp. NO BACKTICKS IN THIS BLOCK — it is a JS template literal, and one
74
+ // stray backtick in a WGSL comment terminates the shader string and fails the build 100 lines
75
+ // away. With no second texture, albedo2 is the same 1x1 white as albedo and blend.x is 0, so this
76
+ // is exactly i.col * albedo — byte-identical to before 0.14.6.
77
+ let alb0 = textureSample(albedo, albedoSamp, i.uv).rgb;
78
+ let alb1 = textureSample(albedo2, albedoSamp, i.uv).rgb;
79
+ let base = i.col * mix(alb0, alb1, u.blend.x);
73
80
  let lit = base * (u.amb.xyz + d * u.sunCol.xyz * sh);
74
81
  let dist = length(i.world - u.cam.xyz);
75
82
  let f = clamp(1.0 - exp(-dist * u.fog.w), 0.0, 1.0);
@@ -93,6 +100,17 @@ export class GpuSkinRenderer {
93
100
  whiteTex;
94
101
  albedoSamp;
95
102
  matBGs = new Map();
103
+ /** A stable id per GPUTexture, so a PAIR can key a Map without stringifying the objects. */
104
+ texIds = new WeakMap();
105
+ nextTexId = 0;
106
+ texId(t) {
107
+ let id = this.texIds.get(t);
108
+ if (id === undefined) {
109
+ id = this.nextTexId++;
110
+ this.texIds.set(t, id);
111
+ }
112
+ return id;
113
+ }
96
114
  constructor(device, format) {
97
115
  this.device = device;
98
116
  const sm = device.createShaderModule({ code: SKIN });
@@ -192,16 +210,21 @@ export class GpuSkinRenderer {
192
210
  // coordinate, so the attribute still has to EXIST but its contents cannot matter.
193
211
  const uvB = mk(c.uvs && c.uvs.length >= n * 2 ? c.uvs : new Float32Array(n * 2), U.VERTEX | U.COPY_DST);
194
212
  const tex = c.tex ?? this.whiteTex;
195
- let matBG = this.matBGs.get(tex);
213
+ const tex2 = c.tex2 ?? this.whiteTex;
214
+ // ⚠️ Keyed on the PAIR. Keyed on `tex` alone, two creatures sharing a base map but blending
215
+ // toward different second maps would silently share the first one's bind group.
216
+ const matKey = `${this.texId(tex)}:${this.texId(tex2)}`;
217
+ let matBG = this.matBGs.get(matKey);
196
218
  if (!matBG) {
197
219
  matBG = dev.createBindGroup({
198
220
  layout: this.drawPipe.getBindGroupLayout(2),
199
221
  entries: [
200
222
  { binding: 0, resource: tex.createView() },
201
223
  { binding: 1, resource: this.albedoSamp },
224
+ { binding: 2, resource: tex2.createView() },
202
225
  ],
203
226
  });
204
- this.matBGs.set(tex, matBG);
227
+ this.matBGs.set(matKey, matBG);
205
228
  }
206
229
  const palB = dev.createBuffer({
207
230
  size: c.palette.length * 4,
@@ -228,7 +251,7 @@ export class GpuSkinRenderer {
228
251
  ],
229
252
  });
230
253
  const uBuf = dev.createBuffer({
231
- size: 96 + 16 * 7 + 64 + 16 + 64 * MAX_CASCADES, // + csm vec4 + the cascade matrices
254
+ size: 96 + 16 * 7 + 64 + 16 + 64 * MAX_CASCADES + 16, // + csm vec4 + cascades + blend vec4
232
255
  usage: U.UNIFORM | U.COPY_DST,
233
256
  });
234
257
  const uBG = dev.createBindGroup({
@@ -249,6 +272,7 @@ export class GpuSkinRenderer {
249
272
  palB,
250
273
  n,
251
274
  model: c.model,
275
+ mix: c.mix ?? 0,
252
276
  };
253
277
  });
254
278
  }
@@ -268,7 +292,7 @@ export class GpuSkinRenderer {
268
292
  }
269
293
  cp.end();
270
294
  for (const p of this.packed) {
271
- const u = new Float32Array(68 + 4 + 16 * MAX_CASCADES);
295
+ const u = new Float32Array(68 + 4 + 16 * MAX_CASCADES + 4);
272
296
  u.set(mul16(camVP, p.model), 0);
273
297
  u.set(mats[0] ?? IDENTITY4, 16); // legacy cascade-0 slot, kept so the offsets above never move
274
298
  u.set([sunDir[0], sunDir[1], sunDir[2], receive ? 1 : 0], 32); // sun.w = shadow-receive enable
@@ -282,6 +306,9 @@ export class GpuSkinRenderer {
282
306
  u[68] = receive ? Math.min(MAX_CASCADES, mats.length) : 0;
283
307
  for (let i = 0; i < Math.min(MAX_CASCADES, mats.length); i++)
284
308
  u.set(mats[i], 72 + i * 16);
309
+ // blend.x — the second albedo's share. 0 for every caller that does not set `mix`, and
310
+ // `mix(a, white, 0)` is `a`, so the default path is byte-identical to the pre-0.14.6 output.
311
+ u[72 + 16 * MAX_CASCADES] = p.mix;
285
312
  this.device.queue.writeBuffer(p.uBuf, 0, u);
286
313
  }
287
314
  // group 1: the shadow map + comparison sampler (dummy when not receiving). Rebuild only when the view changes.
@@ -51,6 +51,26 @@ export function requiredFeatureFor(target) {
51
51
  : 'texture-compression-etc2',
52
52
  ];
53
53
  }
54
+ /**
55
+ * Every block-compressed target here — ASTC-4x4, BC7, ETC2 — uses a 4x4 block, which is also why
56
+ * `bytesPerRow` below is a `/4` everywhere.
57
+ */
58
+ const BLOCK = 4;
59
+ /**
60
+ * The copy extent to hand `writeTexture` for one mip level.
61
+ *
62
+ * WebGPU requires a compressed copy to be a whole number of BLOCKS, and the tail of any mip chain is
63
+ * not: a 128² texture ends 4, 2, 1. Passing the logical 2 and 1 makes Chrome REJECT the write with
64
+ * "copySize.width (2) is not a multiple of compressed texture format block width (4)" — a console
65
+ * warning, not an exception, so the two smallest levels of every texture silently never uploaded and
66
+ * anything far enough away to sample them read undefined memory. Measured 1072 of these warnings on
67
+ * a single all-mine-sky world load, across every species albedo and all 120 atlas layers.
68
+ *
69
+ * Rounding UP is correct rather than a fudge: a mip's PHYSICAL subresource is already padded to a
70
+ * full block, and the transcoder hands back exactly that padded block, so 4x4 is the real size of
71
+ * the data and of the destination.
72
+ */
73
+ const copyExtent = (n, compressed) => compressed ? Math.ceil(n / BLOCK) * BLOCK : n;
54
74
  /** Transcode a cooked ETC1S KTX2 to a GPU-compressed texture (full mip chain) on `device`.
55
75
  * `basisMod` is an initialized Basis module (from loadBasisModule() or the harness's global). */
56
76
  export function transcodeKtx2(device, basisMod, ktx2Bytes) {
@@ -96,7 +116,10 @@ export function transcodeKtx2(device, basisMod, ktx2Bytes) {
96
116
  // writeTexture wants a plain ArrayBuffer view (COI/SAB-safe): copy off the transcoder heap.
97
117
  const src = new Uint8Array(m.bytes.byteLength);
98
118
  src.set(m.bytes);
99
- device.queue.writeTexture({ texture, mipLevel: mip }, src, { offset: 0, bytesPerRow, rowsPerImage }, { width: m.w, height: m.h });
119
+ device.queue.writeTexture({ texture, mipLevel: mip }, src, { offset: 0, bytesPerRow, rowsPerImage }, {
120
+ width: copyExtent(m.w, target.compressed),
121
+ height: copyExtent(m.h, target.compressed),
122
+ });
100
123
  }
101
124
  return {
102
125
  texture,
@@ -156,8 +179,8 @@ export function transcodeKtx2Array(device, basisMod, ktx2Bytes) {
156
179
  const src = new Uint8Array(dst.byteLength);
157
180
  src.set(dst);
158
181
  device.queue.writeTexture({ texture, mipLevel: mip, origin: { x: 0, y: 0, z: layer } }, src, { offset: 0, bytesPerRow, rowsPerImage }, {
159
- width: info.origWidth,
160
- height: info.origHeight,
182
+ width: copyExtent(info.origWidth, target.compressed),
183
+ height: copyExtent(info.origHeight, target.compressed),
161
184
  depthOrArrayLayers: 1,
162
185
  });
163
186
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ugly-game",
3
- "version": "0.14.4",
3
+ "version": "0.14.6",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {