ugly-game 0.14.6 → 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.
@@ -36,6 +36,23 @@ export interface SkinCreature {
36
36
  */
37
37
  tex2?: GPUTexture;
38
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;
39
56
  indices: Uint32Array;
40
57
  palette: Float32Array;
41
58
  model: Float32Array;
@@ -45,6 +62,17 @@ export declare class GpuSkinRenderer {
45
62
  private device;
46
63
  private skinPipe;
47
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;
48
76
  private packed;
49
77
  private dummyShadowView;
50
78
  private dummyShadowSamp;
@@ -80,12 +80,25 @@ fn shadowFactor(world: vec3<f32>, ndl: f32) -> f32 {
80
80
  let lit = base * (u.amb.xyz + d * u.sunCol.xyz * sh);
81
81
  let dist = length(i.world - u.cam.xyz);
82
82
  let f = clamp(1.0 - exp(-dist * u.fog.w), 0.0, 1.0);
83
- 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);
84
86
  }`;
85
87
  export class GpuSkinRenderer {
86
88
  device;
87
89
  skinPipe;
88
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;
89
102
  packed = [];
90
103
  // shadow-receive (group 1): dummy 1×1 depth map when no sun shadow is supplied (demo path), else the terrain's
91
104
  // shared shadow map. The bind group is cached and rebuilt only when the supplied view/sampler identity changes.
@@ -119,8 +132,57 @@ export class GpuSkinRenderer {
119
132
  compute: { module: sm, entryPoint: 'main' },
120
133
  });
121
134
  const dm = device.createShaderModule({ code: DRAW });
122
- this.drawPipe = device.createRenderPipeline({
123
- 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,
124
186
  vertex: {
125
187
  module: dm,
126
188
  entryPoint: 'vs',
@@ -143,14 +205,46 @@ export class GpuSkinRenderer {
143
205
  },
144
206
  ],
145
207
  },
146
- fragment: { module: dm, entryPoint: 'fs', targets: [{ format }] },
147
208
  primitive: { topology: 'triangle-list', cullMode: 'none' },
209
+ };
210
+ this.drawPipe = device.createRenderPipeline({
211
+ ...common,
212
+ fragment: { module: dm, entryPoint: 'fs', targets: [{ format }] },
148
213
  depthStencil: {
149
214
  format: 'depth24plus',
150
215
  depthWriteEnabled: true,
151
216
  depthCompare: 'less',
152
217
  },
153
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
+ });
154
248
  const dummy = device.createTexture({
155
249
  size: [1, 1],
156
250
  format: 'depth32float',
@@ -217,7 +311,7 @@ export class GpuSkinRenderer {
217
311
  let matBG = this.matBGs.get(matKey);
218
312
  if (!matBG) {
219
313
  matBG = dev.createBindGroup({
220
- layout: this.drawPipe.getBindGroupLayout(2),
314
+ layout: this.drawBGL[2],
221
315
  entries: [
222
316
  { binding: 0, resource: tex.createView() },
223
317
  { binding: 1, resource: this.albedoSamp },
@@ -255,7 +349,7 @@ export class GpuSkinRenderer {
255
349
  usage: U.UNIFORM | U.COPY_DST,
256
350
  });
257
351
  const uBG = dev.createBindGroup({
258
- layout: this.drawPipe.getBindGroupLayout(0),
352
+ layout: this.drawBGL[0],
259
353
  entries: [{ binding: 0, resource: { buffer: uBuf } }],
260
354
  });
261
355
  return {
@@ -273,6 +367,7 @@ export class GpuSkinRenderer {
273
367
  n,
274
368
  model: c.model,
275
369
  mix: c.mix ?? 0,
370
+ alpha: c.alpha ?? 1,
276
371
  };
277
372
  });
278
373
  }
@@ -309,13 +404,16 @@ export class GpuSkinRenderer {
309
404
  // blend.x — the second albedo's share. 0 for every caller that does not set `mix`, and
310
405
  // `mix(a, white, 0)` is `a`, so the default path is byte-identical to the pre-0.14.6 output.
311
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;
312
410
  this.device.queue.writeBuffer(p.uBuf, 0, u);
313
411
  }
314
412
  // group 1: the shadow map + comparison sampler (dummy when not receiving). Rebuild only when the view changes.
315
413
  const sv = shadowView ?? this.dummyShadowView, ss = shadowSamp ?? this.dummyShadowSamp;
316
414
  if (!this.shadowBG || sv !== this.lastSV || ss !== this.lastSS) {
317
415
  this.shadowBG = this.device.createBindGroup({
318
- layout: this.drawPipe.getBindGroupLayout(1),
416
+ layout: this.drawBGL[1],
319
417
  entries: [
320
418
  { binding: 0, resource: sv },
321
419
  { binding: 1, resource: ss },
@@ -345,9 +443,8 @@ export class GpuSkinRenderer {
345
443
  depthStoreOp: 'store',
346
444
  },
347
445
  });
348
- rp.setPipeline(this.drawPipe);
349
446
  rp.setBindGroup(1, this.shadowBG);
350
- for (const p of this.packed) {
447
+ const drawOne = (p) => {
351
448
  rp.setBindGroup(0, p.uBG);
352
449
  rp.setVertexBuffer(0, p.defB);
353
450
  rp.setVertexBuffer(1, p.colB);
@@ -356,6 +453,21 @@ export class GpuSkinRenderer {
356
453
  rp.setBindGroup(2, p.matBG);
357
454
  rp.setIndexBuffer(p.idxB, 'uint32');
358
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);
359
471
  }
360
472
  rp.end();
361
473
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ugly-game",
3
- "version": "0.14.6",
3
+ "version": "0.14.7",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {