ugly-game 0.14.5 → 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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ugly-game",
3
- "version": "0.14.5",
3
+ "version": "0.14.6",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -15,6 +15,14 @@
15
15
  "files": [
16
16
  "dist"
17
17
  ],
18
+ "scripts": {
19
+ "preinstall": "npx only-allow pnpm",
20
+ "build": "tsc -p tsconfig.json && mkdir -p dist/assets/basis && cp src/assets/basis/basis_transcoder.js src/assets/basis/basis_transcoder.bin dist/assets/basis/",
21
+ "lint": "ugly-app lint",
22
+ "lint:fix": "ugly-app lint --fix",
23
+ "pretty": "prettier --write .",
24
+ "prepublishOnly": "pnpm run pretty && ugly-app lint && tsc -p tsconfig.json && mkdir -p dist/assets/basis && cp src/assets/basis/basis_transcoder.js src/assets/basis/basis_transcoder.bin dist/assets/basis/"
25
+ },
18
26
  "dependencies": {
19
27
  "meshoptimizer": "^1.2.0"
20
28
  },
@@ -27,12 +35,5 @@
27
35
  "@webgpu/types": "^0.1.40",
28
36
  "prettier": "^3.9.5",
29
37
  "ugly-app": "^0.1.841"
30
- },
31
- "scripts": {
32
- "preinstall": "npx only-allow pnpm",
33
- "build": "tsc -p tsconfig.json && mkdir -p dist/assets/basis && cp src/assets/basis/basis_transcoder.js src/assets/basis/basis_transcoder.bin dist/assets/basis/",
34
- "lint": "ugly-app lint",
35
- "lint:fix": "ugly-app lint --fix",
36
- "pretty": "prettier --write ."
37
38
  }
38
- }
39
+ }