ugly-game 0.14.1 → 0.14.2

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.
@@ -4,6 +4,24 @@ export interface SkinCreature {
4
4
  joints: Uint16Array;
5
5
  weights: Float32Array;
6
6
  colors: Float32Array;
7
+ /**
8
+ * Optional N·2 UVs and an albedo texture, multiplied over `colors`.
9
+ *
10
+ * Both omitted ⇒ a 1×1 white texture is bound and the result is `colors` unchanged, so every
11
+ * existing caller renders byte-identically.
12
+ *
13
+ * ⚠️ THE ALBEDO MULTIPLIES `colors`, it does not replace them. A caller supplying a texture should
14
+ * supply WHITE vertex colours, or the texture is tinted by whatever is in them — all-mine-sky's
15
+ * cooked fauna carry a flat 0.55 grey, which would darken every map by 45% and look like a
16
+ * lighting bug rather than a mistake in the data.
17
+ *
18
+ * `tex` is a GPUTexture the CALLER owns and uploads ONCE, not image data. That is deliberate:
19
+ * `setCreatures` tears down and rebuilds its buffers on every call, and AMS calls it every frame,
20
+ * so anything decoded or allocated in here would be decoded or allocated sixty times a second.
21
+ * The renderer caches the material bind group per texture identity for the same reason.
22
+ */
23
+ uvs?: Float32Array;
24
+ tex?: GPUTexture;
7
25
  indices: Uint32Array;
8
26
  palette: Float32Array;
9
27
  model: Float32Array;
@@ -19,6 +37,9 @@ export declare class GpuSkinRenderer {
19
37
  private shadowBG;
20
38
  private lastSV;
21
39
  private lastSS;
40
+ private whiteTex;
41
+ private albedoSamp;
42
+ private matBGs;
22
43
  constructor(device: GPUDevice, format: GPUTextureFormat);
23
44
  setCreatures(list: SkinCreature[]): void;
24
45
  /** Deform + draw every creature with the shared scene lighting. load=true composites over the world;
@@ -28,10 +28,14 @@ struct U { mvp: mat4x4<f32>, lightVP: mat4x4<f32>, sun: vec4<f32>, sunCol: vec4<
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;
31
- struct VOut { @builtin(position) pos: vec4<f32>, @location(0) world: vec3<f32>, @location(1) col: vec3<f32>, @location(2) n: vec3<f32> };
31
+ // ALBEDO. A 1×1 white texture is bound when a caller supplies none, so white × col = col and the
32
+ // untextured path is byte-identical to before this existed.
33
+ @group(2) @binding(0) var albedo: texture_2d<f32>;
34
+ @group(2) @binding(1) var albedoSamp: sampler;
35
+ 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> };
32
36
  // o.world must be the WORLD position (model·p), not the local deformed p — else the fog distance is computed
33
37
  // against the world-space camera and blows up on a planet surface (huge coords), washing creatures into the fog.
34
- @vertex fn vs(@location(0) p: vec3<f32>, @location(1) c: vec3<f32>, @location(2) nrm: vec3<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; return o; }
38
+ @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; }
35
39
  // PCF 3×3 sun shadow — mirrors gpuShadow.shadowFactor so creatures sit in the SAME shadow as the terrain. 1 = lit, 0 = shadowed.
36
40
  fn pcfLayer(world: vec3<f32>, ndl: f32, layer: i32, inside: ptr<function, bool>) -> f32 {
37
41
  let lp = u.cascades[layer] * vec4(world, 1.0);
@@ -65,7 +69,8 @@ fn shadowFactor(world: vec3<f32>, ndl: f32) -> f32 {
65
69
  @fragment fn fs(i: VOut) -> @location(0) vec4<f32> {
66
70
  let d = clamp(dot(normalize(i.n), -normalize(u.sun.xyz)), 0.0, 1.0);
67
71
  let sh = mix(1.0, shadowFactor(i.world, d), u.sun.w); // u.sun.w = shadow-receive enable (0 ⇒ off, byte-identical for demo)
68
- let lit = i.col * (u.amb.xyz + d * u.sunCol.xyz * sh);
72
+ let base = i.col * textureSample(albedo, albedoSamp, i.uv).rgb;
73
+ let lit = base * (u.amb.xyz + d * u.sunCol.xyz * sh);
69
74
  let dist = length(i.world - u.cam.xyz);
70
75
  let f = clamp(1.0 - exp(-dist * u.fog.w), 0.0, 1.0);
71
76
  return vec4(mix(lit, u.fog.xyz, f), 1.0);
@@ -82,6 +87,12 @@ export class GpuSkinRenderer {
82
87
  shadowBG = null;
83
88
  lastSV = null;
84
89
  lastSS = null;
90
+ // ALBEDO (group 2). The white 1×1 keeps the untextured path byte-identical; the cache is keyed by
91
+ // GPUTexture identity because `setCreatures` runs every frame and rebuilding a bind group per
92
+ // creature per frame is pure waste for something that changes when the KIT changes.
93
+ whiteTex;
94
+ albedoSamp;
95
+ matBGs = new Map();
85
96
  constructor(device, format) {
86
97
  this.device = device;
87
98
  const sm = device.createShaderModule({ code: SKIN });
@@ -108,6 +119,10 @@ export class GpuSkinRenderer {
108
119
  arrayStride: 16,
109
120
  attributes: [{ shaderLocation: 2, offset: 0, format: 'float32x3' }],
110
121
  },
122
+ {
123
+ arrayStride: 8,
124
+ attributes: [{ shaderLocation: 3, offset: 0, format: 'float32x2' }],
125
+ },
111
126
  ],
112
127
  },
113
128
  fragment: { module: dm, entryPoint: 'fs', targets: [{ format }] },
@@ -125,6 +140,19 @@ export class GpuSkinRenderer {
125
140
  });
126
141
  this.dummyShadowView = dummy.createView({ dimension: '2d-array' }); // the binding is an array now
127
142
  this.dummyShadowSamp = device.createSampler({ compare: 'less' });
143
+ this.whiteTex = device.createTexture({
144
+ size: [1, 1],
145
+ format: 'rgba8unorm',
146
+ usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST,
147
+ });
148
+ device.queue.writeTexture({ texture: this.whiteTex }, new Uint8Array([255, 255, 255, 255]), { bytesPerRow: 4 }, [1, 1]);
149
+ this.albedoSamp = device.createSampler({
150
+ magFilter: 'linear',
151
+ minFilter: 'linear',
152
+ mipmapFilter: 'linear',
153
+ addressModeU: 'repeat',
154
+ addressModeV: 'repeat',
155
+ });
128
156
  }
129
157
  setCreatures(list) {
130
158
  const dev = this.device, U = GPUBufferUsage;
@@ -132,6 +160,7 @@ export class GpuSkinRenderer {
132
160
  p.defB.destroy();
133
161
  p.defNB.destroy();
134
162
  p.colB.destroy();
163
+ p.uvB.destroy();
135
164
  p.idxB.destroy();
136
165
  p.uBuf.destroy();
137
166
  p.palB.destroy();
@@ -159,6 +188,21 @@ export class GpuSkinRenderer {
159
188
  };
160
189
  const restB = mk(rest, U.STORAGE | U.COPY_DST), restNB = mk(restN, U.STORAGE | U.COPY_DST), jointsB = mk(joints, U.STORAGE | U.COPY_DST), weightsB = mk(weights, U.STORAGE | U.COPY_DST);
161
190
  const colB = mk(c.colors, U.VERTEX | U.COPY_DST), idxB = mk(c.indices, U.INDEX | U.COPY_DST);
191
+ // A zero-filled UV buffer when the caller has none: the white fallback samples to 1 at any
192
+ // coordinate, so the attribute still has to EXIST but its contents cannot matter.
193
+ const uvB = mk(c.uvs && c.uvs.length >= n * 2 ? c.uvs : new Float32Array(n * 2), U.VERTEX | U.COPY_DST);
194
+ const tex = c.tex ?? this.whiteTex;
195
+ let matBG = this.matBGs.get(tex);
196
+ if (!matBG) {
197
+ matBG = dev.createBindGroup({
198
+ layout: this.drawPipe.getBindGroupLayout(2),
199
+ entries: [
200
+ { binding: 0, resource: tex.createView() },
201
+ { binding: 1, resource: this.albedoSamp },
202
+ ],
203
+ });
204
+ this.matBGs.set(tex, matBG);
205
+ }
162
206
  const palB = dev.createBuffer({
163
207
  size: c.palette.length * 4,
164
208
  usage: U.STORAGE | U.COPY_DST,
@@ -195,6 +239,8 @@ export class GpuSkinRenderer {
195
239
  defB,
196
240
  defNB,
197
241
  colB,
242
+ uvB,
243
+ matBG,
198
244
  idxB,
199
245
  idxCount: c.indices.length,
200
246
  skinBG,
@@ -279,6 +325,8 @@ export class GpuSkinRenderer {
279
325
  rp.setVertexBuffer(0, p.defB);
280
326
  rp.setVertexBuffer(1, p.colB);
281
327
  rp.setVertexBuffer(2, p.defNB);
328
+ rp.setVertexBuffer(3, p.uvB);
329
+ rp.setBindGroup(2, p.matBG);
282
330
  rp.setIndexBuffer(p.idxB, 'uint32');
283
331
  rp.drawIndexed(p.idxCount);
284
332
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ugly-game",
3
- "version": "0.14.1",
3
+ "version": "0.14.2",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -15,14 +15,6 @@
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
- },
26
18
  "dependencies": {
27
19
  "meshoptimizer": "^1.2.0"
28
20
  },
@@ -35,5 +27,12 @@
35
27
  "@webgpu/types": "^0.1.40",
36
28
  "prettier": "^3.9.5",
37
29
  "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 ."
38
37
  }
39
- }
38
+ }