ugly-game 0.14.8 → 0.14.10

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.
@@ -53,6 +53,32 @@ export interface SkinCreature {
53
53
  * state — byte-identical to before this existed.
54
54
  */
55
55
  alpha?: number;
56
+ /**
57
+ * Discard any fragment whose ALBEDO ALPHA is below this. 0, the default, is off.
58
+ *
59
+ * ⚠️ For cards-and-a-mask geometry — hair, foliage, chain-link — where the mesh is quads and the
60
+ * texture is what carves the shape out of them. Around 40% of a MakeHuman hair map is fully
61
+ * transparent; drawn without this it is a set of opaque rectangles.
62
+ *
63
+ * ⚠️ Stays on the OPAQUE pipeline, with depth writes ON. That is the difference between a cutout
64
+ * and `alpha` — a cutout surface still occludes, it just has holes. Routing it through the blend
65
+ * pipeline instead would stop it writing depth, and hair would fail to hide the scalp behind it.
66
+ */
67
+ cutout?: number;
68
+ /**
69
+ * A THIRD texture, MULTIPLIED over the albedo — not lerped toward it the way `tex2` is.
70
+ *
71
+ * ⚠️ Sampled with `uvs2`, deliberately. An albedo's UVs are an atlas packing; a tint's are a
72
+ * projection. Sampling both with the same coordinates paints the tint onto the packing layout,
73
+ * which for hair is a patchwork of rotated islands rather than a head.
74
+ *
75
+ * ⚠️ Omitted ⇒ the same 1×1 white texture `tex` falls back to, so `albedo × white` is `albedo`
76
+ * and every existing caller renders byte-identically. Same contract `tex` and `tex2` shipped
77
+ * under; keep it.
78
+ */
79
+ tint?: GPUTexture;
80
+ /** `n * 2` for `tint`. Omitted ⇒ `uvs`, which with the white fallback cannot matter. */
81
+ uvs2?: Float32Array;
56
82
  indices: Uint32Array;
57
83
  palette: Float32Array;
58
84
  model: Float32Array;
@@ -33,10 +33,13 @@ struct U { mvp: mat4x4<f32>, lightVP: mat4x4<f32>, sun: vec4<f32>, sunCol: vec4<
33
33
  @group(2) @binding(0) var albedo: texture_2d<f32>;
34
34
  @group(2) @binding(1) var albedoSamp: sampler;
35
35
  @group(2) @binding(2) var albedo2: texture_2d<f32>;
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
+ // TINT — a third map, MULTIPLIED over the albedo and sampled with its OWN uv set. A 1x1 white is
37
+ // bound when a caller supplies none, so albedo x white = albedo and the untinted path is unchanged.
38
+ @group(2) @binding(3) var tintTex: texture_2d<f32>;
39
+ 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>, @location(4) uv2: vec2<f32> };
37
40
  // o.world must be the WORLD position (model·p), not the local deformed p — else the fog distance is computed
38
41
  // against the world-space camera and blows up on a planet surface (huge coords), washing creatures into the fog.
39
- @vertex fn vs(@location(0) p: vec3<f32>, @location(1) c: vec3<f32>, @location(2) nrm: vec3<f32>, @location(3) uv: vec2<f32>) -> VOut { var o: VOut; o.pos=u.mvp*vec4(p,1.0); o.world=(u.model*vec4(p,1.0)).xyz; o.col=c; o.n=nrm; o.uv=uv; return o; }
42
+ @vertex fn vs(@location(0) p: vec3<f32>, @location(1) c: vec3<f32>, @location(2) nrm: vec3<f32>, @location(3) uv: vec2<f32>, @location(4) uv2: vec2<f32>) -> VOut { var o: VOut; o.pos=u.mvp*vec4(p,1.0); o.world=(u.model*vec4(p,1.0)).xyz; o.col=c; o.n=nrm; o.uv=uv; o.uv2=uv2; return o; }
40
43
  // PCF 3×3 sun shadow — mirrors gpuShadow.shadowFactor so creatures sit in the SAME shadow as the terrain. 1 = lit, 0 = shadowed.
41
44
  fn pcfLayer(world: vec3<f32>, ndl: f32, layer: i32, inside: ptr<function, bool>) -> f32 {
42
45
  let lp = u.cascades[layer] * vec4(world, 1.0);
@@ -74,9 +77,13 @@ fn shadowFactor(world: vec3<f32>, ndl: f32) -> f32 {
74
77
  // stray backtick in a WGSL comment terminates the shader string and fails the build 100 lines
75
78
  // away. With no second texture, albedo2 is the same 1x1 white as albedo and blend.x is 0, so this
76
79
  // is exactly i.col * albedo — byte-identical to before 0.14.6.
77
- let alb0 = textureSample(albedo, albedoSamp, i.uv).rgb;
80
+ let alb = textureSample(albedo, albedoSamp, i.uv);
78
81
  let alb1 = textureSample(albedo2, albedoSamp, i.uv).rgb;
79
- let base = i.col * mix(alb0, alb1, u.blend.x);
82
+ // blend.z is the cutout threshold, 0 for everyone who does not set one. A cards-and-mask surface
83
+ // is mostly holes; without this it draws as rectangles.
84
+ if (u.blend.z > 0.0 && alb.w < u.blend.z) { discard; }
85
+ let tint = textureSample(tintTex, albedoSamp, i.uv2).rgb;
86
+ let base = i.col * mix(alb.rgb, alb1, u.blend.x) * tint;
80
87
  let lit = base * (u.amb.xyz + d * u.sunCol.xyz * sh);
81
88
  let dist = length(i.world - u.cam.xyz);
82
89
  let f = clamp(1.0 - exp(-dist * u.fog.w), 0.0, 1.0);
@@ -175,6 +182,11 @@ export class GpuSkinRenderer {
175
182
  visibility: GPUShaderStage.FRAGMENT,
176
183
  texture: { sampleType: 'float' },
177
184
  },
185
+ {
186
+ binding: 3,
187
+ visibility: GPUShaderStage.FRAGMENT,
188
+ texture: { sampleType: 'float' },
189
+ },
178
190
  ],
179
191
  });
180
192
  this.drawBGL = [bgl0, bgl1, bgl2];
@@ -203,6 +215,10 @@ export class GpuSkinRenderer {
203
215
  arrayStride: 8,
204
216
  attributes: [{ shaderLocation: 3, offset: 0, format: 'float32x2' }],
205
217
  },
218
+ {
219
+ arrayStride: 8,
220
+ attributes: [{ shaderLocation: 4, offset: 0, format: 'float32x2' }],
221
+ },
206
222
  ],
207
223
  },
208
224
  primitive: { topology: 'triangle-list', cullMode: 'none' },
@@ -218,6 +234,19 @@ export class GpuSkinRenderer {
218
234
  });
219
235
  this.blendPipe = device.createRenderPipeline({
220
236
  ...common,
237
+ // ⚠️ BACK FACES CULLED, and this is the difference between translucency and mud.
238
+ //
239
+ // With `cullMode: 'none'` and no depth write, every triangle of a closed mesh blends: the far
240
+ // side of the torso, then the near side, in index order. How many layers survive at a given
241
+ // pixel depends on what opaque geometry happens to sit between them, so an opaque marker
242
+ // INSIDE the body masks the far side there and not elsewhere — which stamps the marker's
243
+ // silhouette into the skin as a hard-edged rectangle of different accumulated opacity. The
244
+ // body came out blotchy rather than see-through, and the blotches were shaped like the very
245
+ // markers it was supposed to reveal.
246
+ //
247
+ // One front-facing layer per pixel is the fix. Overlapping SEPARATE surfaces (an arm across
248
+ // the torso) still blend twice, which is correct — you are looking through two things.
249
+ primitive: { ...common.primitive, cullMode: 'back' },
221
250
  fragment: {
222
251
  module: dm,
223
252
  entryPoint: 'fs',
@@ -273,6 +302,7 @@ export class GpuSkinRenderer {
273
302
  p.defNB.destroy();
274
303
  p.colB.destroy();
275
304
  p.uvB.destroy();
305
+ p.uv2B.destroy();
276
306
  p.idxB.destroy();
277
307
  p.uBuf.destroy();
278
308
  p.palB.destroy();
@@ -303,11 +333,20 @@ export class GpuSkinRenderer {
303
333
  // A zero-filled UV buffer when the caller has none: the white fallback samples to 1 at any
304
334
  // coordinate, so the attribute still has to EXIST but its contents cannot matter.
305
335
  const uvB = mk(c.uvs && c.uvs.length >= n * 2 ? c.uvs : new Float32Array(n * 2), U.VERTEX | U.COPY_DST);
336
+ // Falls back to `uvs`, then to zeros. The white tint samples to 1 at any coordinate, so the
337
+ // attribute must EXIST but its contents cannot matter.
338
+ const uv2Src = c.uvs2 && c.uvs2.length >= n * 2
339
+ ? c.uvs2
340
+ : c.uvs && c.uvs.length >= n * 2
341
+ ? c.uvs
342
+ : new Float32Array(n * 2);
343
+ const uv2B = mk(uv2Src, U.VERTEX | U.COPY_DST);
306
344
  const tex = c.tex ?? this.whiteTex;
307
345
  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)}`;
346
+ const tint = c.tint ?? this.whiteTex;
347
+ // ⚠️ Keyed on the TRIPLE. Keyed on any subset, two creatures sharing those textures but
348
+ // differing in the others would silently share the first one's bind group.
349
+ const matKey = `${this.texId(tex)}:${this.texId(tex2)}:${this.texId(tint)}`;
311
350
  let matBG = this.matBGs.get(matKey);
312
351
  if (!matBG) {
313
352
  matBG = dev.createBindGroup({
@@ -316,6 +355,7 @@ export class GpuSkinRenderer {
316
355
  { binding: 0, resource: tex.createView() },
317
356
  { binding: 1, resource: this.albedoSamp },
318
357
  { binding: 2, resource: tex2.createView() },
358
+ { binding: 3, resource: tint.createView() },
319
359
  ],
320
360
  });
321
361
  this.matBGs.set(matKey, matBG);
@@ -357,6 +397,7 @@ export class GpuSkinRenderer {
357
397
  defNB,
358
398
  colB,
359
399
  uvB,
400
+ uv2B,
360
401
  matBG,
361
402
  idxB,
362
403
  idxCount: c.indices.length,
@@ -368,6 +409,7 @@ export class GpuSkinRenderer {
368
409
  model: c.model,
369
410
  mix: c.mix ?? 0,
370
411
  alpha: c.alpha ?? 1,
412
+ cutout: c.cutout ?? 0,
371
413
  };
372
414
  });
373
415
  }
@@ -407,6 +449,9 @@ export class GpuSkinRenderer {
407
449
  // blend.y — opacity. 1 for every caller that does not set `alpha`, which is the constant the
408
450
  // fragment shader used to return outright.
409
451
  u[73 + 16 * MAX_CASCADES] = p.alpha;
452
+ // blend.z — the cutout threshold. 0 for every caller that does not set one, which is the
453
+ // constant the shader's guard compares against, so the untouched path is unchanged.
454
+ u[74 + 16 * MAX_CASCADES] = p.cutout;
410
455
  this.device.queue.writeBuffer(p.uBuf, 0, u);
411
456
  }
412
457
  // group 1: the shadow map + comparison sampler (dummy when not receiving). Rebuild only when the view changes.
@@ -450,6 +495,7 @@ export class GpuSkinRenderer {
450
495
  rp.setVertexBuffer(1, p.colB);
451
496
  rp.setVertexBuffer(2, p.defNB);
452
497
  rp.setVertexBuffer(3, p.uvB);
498
+ rp.setVertexBuffer(4, p.uv2B);
453
499
  rp.setBindGroup(2, p.matBG);
454
500
  rp.setIndexBuffer(p.idxB, 'uint32');
455
501
  rp.drawIndexed(p.idxCount);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ugly-game",
3
- "version": "0.14.8",
3
+ "version": "0.14.10",
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
+ }