ugly-game 0.14.5 → 0.14.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.
@@ -22,6 +22,37 @@ 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;
39
+ /**
40
+ * Opacity, 1 (the default) being fully opaque.
41
+ *
42
+ * ⚠️ Below 1 the creature moves to a SECOND pipeline — source-alpha blended, and with DEPTH
43
+ * WRITES OFF — drawn after every opaque one. Depth writes have to go: a translucent surface that
44
+ * writes depth occludes whatever is behind it, which is the one thing an opacity control exists
45
+ * to prevent. The cost is that a closed mesh blends against its own far side, so a body at 0.4
46
+ * reads as an x-ray rather than as tinted glass. For the character builder that wants to see a
47
+ * skeleton through the skin, that is the desired picture, not a defect to sort around.
48
+ *
49
+ * ⚠️ There is NO depth sort between translucent creatures. Two overlapping ones composite in
50
+ * submission order. Adding a sort would be a per-frame CPU cost on a path that today draws one.
51
+ *
52
+ * `alpha` of 1, or omitted, keeps the creature on the original pipeline with the original depth
53
+ * state — byte-identical to before this existed.
54
+ */
55
+ alpha?: number;
25
56
  indices: Uint32Array;
26
57
  palette: Float32Array;
27
58
  model: Float32Array;
@@ -31,6 +62,17 @@ export declare class GpuSkinRenderer {
31
62
  private device;
32
63
  private skinPipe;
33
64
  private drawPipe;
65
+ /**
66
+ * The same pipeline with source-alpha blending and depth writes off, for `alpha < 1`.
67
+ *
68
+ * ⚠️ Both are built from ONE EXPLICIT pipeline layout rather than `layout: 'auto'`. Two `auto`
69
+ * pipelines get two distinct bind-group layouts even from identical shader code, so a bind group
70
+ * made for one is rejected by the other — and every bind group here is built once in
71
+ * `setCreatures` and reused by whichever pipeline draws it.
72
+ */
73
+ private blendPipe;
74
+ /** The explicit group 0/1/2 layouts, shared by both render pipelines. */
75
+ private drawBGL;
34
76
  private packed;
35
77
  private dummyShadowView;
36
78
  private dummyShadowSamp;
@@ -40,6 +82,10 @@ export declare class GpuSkinRenderer {
40
82
  private whiteTex;
41
83
  private albedoSamp;
42
84
  private matBGs;
85
+ /** A stable id per GPUTexture, so a PAIR can key a Map without stringifying the objects. */
86
+ private texIds;
87
+ private nextTexId;
88
+ private texId;
43
89
  constructor(device: GPUDevice, format: GPUTextureFormat);
44
90
  setCreatures(list: SkinCreature[]): void;
45
91
  /** 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,16 +70,35 @@ 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);
76
- return vec4(mix(lit, u.fog.xyz, f), 1.0);
83
+ // blend.y is the creature's opacity, 1 for everyone who does not set it — the same constant this
84
+ // returned before, so the opaque pipeline is unchanged.
85
+ return vec4(mix(lit, u.fog.xyz, f), u.blend.y);
77
86
  }`;
78
87
  export class GpuSkinRenderer {
79
88
  device;
80
89
  skinPipe;
81
90
  drawPipe;
91
+ /**
92
+ * The same pipeline with source-alpha blending and depth writes off, for `alpha < 1`.
93
+ *
94
+ * ⚠️ Both are built from ONE EXPLICIT pipeline layout rather than `layout: 'auto'`. Two `auto`
95
+ * pipelines get two distinct bind-group layouts even from identical shader code, so a bind group
96
+ * made for one is rejected by the other — and every bind group here is built once in
97
+ * `setCreatures` and reused by whichever pipeline draws it.
98
+ */
99
+ blendPipe;
100
+ /** The explicit group 0/1/2 layouts, shared by both render pipelines. */
101
+ drawBGL;
82
102
  packed = [];
83
103
  // shadow-receive (group 1): dummy 1×1 depth map when no sun shadow is supplied (demo path), else the terrain's
84
104
  // shared shadow map. The bind group is cached and rebuilt only when the supplied view/sampler identity changes.
@@ -93,6 +113,17 @@ export class GpuSkinRenderer {
93
113
  whiteTex;
94
114
  albedoSamp;
95
115
  matBGs = new Map();
116
+ /** A stable id per GPUTexture, so a PAIR can key a Map without stringifying the objects. */
117
+ texIds = new WeakMap();
118
+ nextTexId = 0;
119
+ texId(t) {
120
+ let id = this.texIds.get(t);
121
+ if (id === undefined) {
122
+ id = this.nextTexId++;
123
+ this.texIds.set(t, id);
124
+ }
125
+ return id;
126
+ }
96
127
  constructor(device, format) {
97
128
  this.device = device;
98
129
  const sm = device.createShaderModule({ code: SKIN });
@@ -101,8 +132,57 @@ export class GpuSkinRenderer {
101
132
  compute: { module: sm, entryPoint: 'main' },
102
133
  });
103
134
  const dm = device.createShaderModule({ code: DRAW });
104
- this.drawPipe = device.createRenderPipeline({
105
- layout: 'auto',
135
+ // The three groups the DRAW shader declares, written out so BOTH render pipelines share them.
136
+ // Under `layout: 'auto'` each pipeline would mint its own, and the bind groups `setCreatures`
137
+ // builds could only ever be used by one of them.
138
+ const bgl0 = device.createBindGroupLayout({
139
+ entries: [
140
+ {
141
+ binding: 0,
142
+ visibility: GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT,
143
+ buffer: { type: 'uniform' },
144
+ },
145
+ ],
146
+ });
147
+ const bgl1 = device.createBindGroupLayout({
148
+ entries: [
149
+ {
150
+ binding: 0,
151
+ visibility: GPUShaderStage.FRAGMENT,
152
+ texture: { sampleType: 'depth', viewDimension: '2d-array' },
153
+ },
154
+ {
155
+ binding: 1,
156
+ visibility: GPUShaderStage.FRAGMENT,
157
+ sampler: { type: 'comparison' },
158
+ },
159
+ ],
160
+ });
161
+ const bgl2 = device.createBindGroupLayout({
162
+ entries: [
163
+ {
164
+ binding: 0,
165
+ visibility: GPUShaderStage.FRAGMENT,
166
+ texture: { sampleType: 'float' },
167
+ },
168
+ {
169
+ binding: 1,
170
+ visibility: GPUShaderStage.FRAGMENT,
171
+ sampler: { type: 'filtering' },
172
+ },
173
+ {
174
+ binding: 2,
175
+ visibility: GPUShaderStage.FRAGMENT,
176
+ texture: { sampleType: 'float' },
177
+ },
178
+ ],
179
+ });
180
+ this.drawBGL = [bgl0, bgl1, bgl2];
181
+ const layout = device.createPipelineLayout({
182
+ bindGroupLayouts: this.drawBGL,
183
+ });
184
+ const common = {
185
+ layout,
106
186
  vertex: {
107
187
  module: dm,
108
188
  entryPoint: 'vs',
@@ -125,14 +205,46 @@ export class GpuSkinRenderer {
125
205
  },
126
206
  ],
127
207
  },
128
- fragment: { module: dm, entryPoint: 'fs', targets: [{ format }] },
129
208
  primitive: { topology: 'triangle-list', cullMode: 'none' },
209
+ };
210
+ this.drawPipe = device.createRenderPipeline({
211
+ ...common,
212
+ fragment: { module: dm, entryPoint: 'fs', targets: [{ format }] },
130
213
  depthStencil: {
131
214
  format: 'depth24plus',
132
215
  depthWriteEnabled: true,
133
216
  depthCompare: 'less',
134
217
  },
135
218
  });
219
+ this.blendPipe = device.createRenderPipeline({
220
+ ...common,
221
+ fragment: {
222
+ module: dm,
223
+ entryPoint: 'fs',
224
+ targets: [
225
+ {
226
+ format,
227
+ blend: {
228
+ color: {
229
+ srcFactor: 'src-alpha',
230
+ dstFactor: 'one-minus-src-alpha',
231
+ },
232
+ // The destination alpha is left alone — an opaque canvas has nothing to accumulate,
233
+ // and `one` on a target that is already 1 is a no-op either way.
234
+ alpha: { srcFactor: 'zero', dstFactor: 'one' },
235
+ },
236
+ },
237
+ ],
238
+ },
239
+ depthStencil: {
240
+ format: 'depth24plus',
241
+ // ⚠️ OFF. A translucent surface that writes depth hides what is behind it, which is exactly
242
+ // what the caller asked to see through. It still TESTS against depth, so an opaque creature
243
+ // in front still occludes it.
244
+ depthWriteEnabled: false,
245
+ depthCompare: 'less',
246
+ },
247
+ });
136
248
  const dummy = device.createTexture({
137
249
  size: [1, 1],
138
250
  format: 'depth32float',
@@ -192,16 +304,21 @@ export class GpuSkinRenderer {
192
304
  // coordinate, so the attribute still has to EXIST but its contents cannot matter.
193
305
  const uvB = mk(c.uvs && c.uvs.length >= n * 2 ? c.uvs : new Float32Array(n * 2), U.VERTEX | U.COPY_DST);
194
306
  const tex = c.tex ?? this.whiteTex;
195
- let matBG = this.matBGs.get(tex);
307
+ const tex2 = c.tex2 ?? this.whiteTex;
308
+ // ⚠️ Keyed on the PAIR. Keyed on `tex` alone, two creatures sharing a base map but blending
309
+ // toward different second maps would silently share the first one's bind group.
310
+ const matKey = `${this.texId(tex)}:${this.texId(tex2)}`;
311
+ let matBG = this.matBGs.get(matKey);
196
312
  if (!matBG) {
197
313
  matBG = dev.createBindGroup({
198
- layout: this.drawPipe.getBindGroupLayout(2),
314
+ layout: this.drawBGL[2],
199
315
  entries: [
200
316
  { binding: 0, resource: tex.createView() },
201
317
  { binding: 1, resource: this.albedoSamp },
318
+ { binding: 2, resource: tex2.createView() },
202
319
  ],
203
320
  });
204
- this.matBGs.set(tex, matBG);
321
+ this.matBGs.set(matKey, matBG);
205
322
  }
206
323
  const palB = dev.createBuffer({
207
324
  size: c.palette.length * 4,
@@ -228,11 +345,11 @@ export class GpuSkinRenderer {
228
345
  ],
229
346
  });
230
347
  const uBuf = dev.createBuffer({
231
- size: 96 + 16 * 7 + 64 + 16 + 64 * MAX_CASCADES, // + csm vec4 + the cascade matrices
348
+ size: 96 + 16 * 7 + 64 + 16 + 64 * MAX_CASCADES + 16, // + csm vec4 + cascades + blend vec4
232
349
  usage: U.UNIFORM | U.COPY_DST,
233
350
  });
234
351
  const uBG = dev.createBindGroup({
235
- layout: this.drawPipe.getBindGroupLayout(0),
352
+ layout: this.drawBGL[0],
236
353
  entries: [{ binding: 0, resource: { buffer: uBuf } }],
237
354
  });
238
355
  return {
@@ -249,6 +366,8 @@ export class GpuSkinRenderer {
249
366
  palB,
250
367
  n,
251
368
  model: c.model,
369
+ mix: c.mix ?? 0,
370
+ alpha: c.alpha ?? 1,
252
371
  };
253
372
  });
254
373
  }
@@ -268,7 +387,7 @@ export class GpuSkinRenderer {
268
387
  }
269
388
  cp.end();
270
389
  for (const p of this.packed) {
271
- const u = new Float32Array(68 + 4 + 16 * MAX_CASCADES);
390
+ const u = new Float32Array(68 + 4 + 16 * MAX_CASCADES + 4);
272
391
  u.set(mul16(camVP, p.model), 0);
273
392
  u.set(mats[0] ?? IDENTITY4, 16); // legacy cascade-0 slot, kept so the offsets above never move
274
393
  u.set([sunDir[0], sunDir[1], sunDir[2], receive ? 1 : 0], 32); // sun.w = shadow-receive enable
@@ -282,13 +401,19 @@ export class GpuSkinRenderer {
282
401
  u[68] = receive ? Math.min(MAX_CASCADES, mats.length) : 0;
283
402
  for (let i = 0; i < Math.min(MAX_CASCADES, mats.length); i++)
284
403
  u.set(mats[i], 72 + i * 16);
404
+ // blend.x — the second albedo's share. 0 for every caller that does not set `mix`, and
405
+ // `mix(a, white, 0)` is `a`, so the default path is byte-identical to the pre-0.14.6 output.
406
+ u[72 + 16 * MAX_CASCADES] = p.mix;
407
+ // blend.y — opacity. 1 for every caller that does not set `alpha`, which is the constant the
408
+ // fragment shader used to return outright.
409
+ u[73 + 16 * MAX_CASCADES] = p.alpha;
285
410
  this.device.queue.writeBuffer(p.uBuf, 0, u);
286
411
  }
287
412
  // group 1: the shadow map + comparison sampler (dummy when not receiving). Rebuild only when the view changes.
288
413
  const sv = shadowView ?? this.dummyShadowView, ss = shadowSamp ?? this.dummyShadowSamp;
289
414
  if (!this.shadowBG || sv !== this.lastSV || ss !== this.lastSS) {
290
415
  this.shadowBG = this.device.createBindGroup({
291
- layout: this.drawPipe.getBindGroupLayout(1),
416
+ layout: this.drawBGL[1],
292
417
  entries: [
293
418
  { binding: 0, resource: sv },
294
419
  { binding: 1, resource: ss },
@@ -318,9 +443,8 @@ export class GpuSkinRenderer {
318
443
  depthStoreOp: 'store',
319
444
  },
320
445
  });
321
- rp.setPipeline(this.drawPipe);
322
446
  rp.setBindGroup(1, this.shadowBG);
323
- for (const p of this.packed) {
447
+ const drawOne = (p) => {
324
448
  rp.setBindGroup(0, p.uBG);
325
449
  rp.setVertexBuffer(0, p.defB);
326
450
  rp.setVertexBuffer(1, p.colB);
@@ -329,6 +453,21 @@ export class GpuSkinRenderer {
329
453
  rp.setBindGroup(2, p.matBG);
330
454
  rp.setIndexBuffer(p.idxB, 'uint32');
331
455
  rp.drawIndexed(p.idxCount);
456
+ };
457
+ // ⚠️ OPAQUE FIRST, in two passes over the list rather than one. The translucent creatures do not
458
+ // write depth, so anything opaque drawn after them would still appear — but they must blend
459
+ // against a colour buffer that already HAS the opaque scene in it, or they composite over the
460
+ // background and the thing behind them is simply missing. With no `alpha` set anywhere the
461
+ // second loop is empty and this is the original single loop.
462
+ rp.setPipeline(this.drawPipe);
463
+ for (const p of this.packed)
464
+ if (p.alpha >= 1)
465
+ drawOne(p);
466
+ const translucent = this.packed.filter((p) => p.alpha < 1);
467
+ if (translucent.length) {
468
+ rp.setPipeline(this.blendPipe);
469
+ for (const p of translucent)
470
+ drawOne(p);
332
471
  }
333
472
  rp.end();
334
473
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ugly-game",
3
- "version": "0.14.5",
3
+ "version": "0.14.7",
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
+ }