ugly-game 0.15.55 → 0.15.57

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.
@@ -0,0 +1,2 @@
1
+ export declare function encodeGpuGeometry(raw: Uint8Array, precisionBits?: number): Promise<Uint8Array>;
2
+ export declare function decodeGpuGeometry(packed: Uint8Array): Promise<Uint8Array>;
@@ -0,0 +1,92 @@
1
+ /** Lossless transport of final GPU streams. No quantization, normal conversion or
2
+ * vertex/index remapping is performed by the decoder. */
3
+ import { MeshoptEncoder, MeshoptDecoder } from 'meshoptimizer';
4
+ export async function encodeGpuGeometry(raw, precisionBits = 0) {
5
+ if (precisionBits &&
6
+ (!Number.isInteger(precisionBits) ||
7
+ precisionBits < 12 ||
8
+ precisionBits > 24))
9
+ throw new Error('Unsupported cook precision');
10
+ await MeshoptEncoder.ready;
11
+ const h = new DataView(raw.buffer, raw.byteOffset, raw.byteLength);
12
+ const vertices = h.getUint32(4, true), indices = h.getUint32(8, true);
13
+ const streams = [];
14
+ let offset = 32, streamIndex = 0;
15
+ for (const [count, stride] of [
16
+ [vertices, 12],
17
+ [vertices, 12],
18
+ [vertices, 12],
19
+ [vertices, 8],
20
+ [indices, 4],
21
+ ]) {
22
+ const bytes = raw.subarray(offset, offset + count * stride);
23
+ const indexStream = streamIndex++ === 4;
24
+ const filtered = precisionBits && !indexStream && count
25
+ ? MeshoptEncoder.encodeFilterExp(new Float32Array(bytes.buffer, bytes.byteOffset, bytes.byteLength / 4), count, stride, precisionBits, 'Separate')
26
+ : bytes;
27
+ streams.push(count
28
+ ? indexStream
29
+ ? MeshoptEncoder.encodeIndexSequence(bytes, count, stride)
30
+ : MeshoptEncoder.encodeVertexBuffer(filtered, count, stride)
31
+ : new Uint8Array());
32
+ offset += bytes.length;
33
+ }
34
+ const result = new Uint8Array(64 + streams.reduce((n, s) => n + s.length, 0));
35
+ result.set(raw.subarray(0, 32));
36
+ const header = new DataView(result.buffer);
37
+ header.setUint32(0, 0x32595047, true);
38
+ header.setUint32(20, precisionBits ? 1 : 0, true);
39
+ header.setUint32(24, 1, true);
40
+ offset = 64;
41
+ streams.forEach((stream, i) => {
42
+ header.setUint32(32 + i * 4, stream.length, true);
43
+ result.set(stream, offset);
44
+ offset += stream.length;
45
+ });
46
+ return result;
47
+ }
48
+ export async function decodeGpuGeometry(packed) {
49
+ if (packed.length < 64)
50
+ throw new Error('Incomplete GPU geometry');
51
+ const h = new DataView(packed.buffer, packed.byteOffset, packed.byteLength);
52
+ const vertices = h.getUint32(4, true), indices = h.getUint32(8, true);
53
+ if (h.getUint32(0, true) !== 0x32595047 ||
54
+ vertices > 2_000_000 ||
55
+ indices > 6_000_000)
56
+ throw new Error('Unsupported GPU geometry');
57
+ const exp = h.getUint32(20, true), indexSequence = h.getUint32(24, true);
58
+ if (exp > 1 || indexSequence > 1)
59
+ throw new Error('Unsupported GPU stream codec');
60
+ const lengths = Array.from({ length: 5 }, (_, i) => h.getUint32(32 + i * 4, true));
61
+ if (64 + lengths.reduce((n, v) => n + v, 0) !== packed.length)
62
+ throw new Error('Invalid GPU stream sizes');
63
+ await MeshoptDecoder.ready;
64
+ const result = new Uint8Array(32 + vertices * 44 + indices * 4);
65
+ result.set(packed.subarray(0, 32));
66
+ const resultHeader = new DataView(result.buffer);
67
+ resultHeader.setUint32(0, 0x31595047, true);
68
+ resultHeader.setUint32(20, 0, true);
69
+ resultHeader.setUint32(24, 0, true);
70
+ let source = 64, dest = 32, stream = 0;
71
+ for (const [count, stride] of [
72
+ [vertices, 12],
73
+ [vertices, 12],
74
+ [vertices, 12],
75
+ [vertices, 8],
76
+ [indices, 4],
77
+ ]) {
78
+ const length = lengths[stream++];
79
+ if (count) {
80
+ const target = result.subarray(dest, dest + count * stride), sourceBytes = packed.subarray(source, source + length);
81
+ if (stream === 5 && indexSequence)
82
+ MeshoptDecoder.decodeIndexSequence(target, count, stride, sourceBytes);
83
+ else
84
+ MeshoptDecoder.decodeVertexBuffer(target, count, stride, sourceBytes, stream < 5 && exp ? 'EXPONENTIAL' : undefined);
85
+ }
86
+ else if (length)
87
+ throw new Error('Nonempty payload for empty GPU stream');
88
+ source += length;
89
+ dest += count * stride;
90
+ }
91
+ return result;
92
+ }
@@ -23,6 +23,11 @@ export interface PropWorld {
23
23
  valueLift: number;
24
24
  }
25
25
  export interface PropMesh {
26
+ /** Offline-cooked deformation bounds. Omit for procedural meshes. */
27
+ extent?: {
28
+ invH: number;
29
+ invHalfW: number;
30
+ };
26
31
  /** Opt-in metallic/roughness shading. Omitted preserves legacy matte lighting. */
27
32
  roughness?: number;
28
33
  metalness?: number;
@@ -864,11 +864,6 @@ export class GpuPropsRenderer {
864
864
  p.tex?.destroy();
865
865
  p.matBuf.destroy();
866
866
  }
867
- const mk = (d, u) => {
868
- const b = dev.createBuffer({ size: d.byteLength, usage: u });
869
- dev.queue.writeBuffer(b, 0, d);
870
- return b;
871
- };
872
867
  // One sampler for every prop. Repeat + trilinear: props are UV-mapped charts, not tiled
873
868
  // surfaces, but mipmapping is not optional — at tens of thousands of instances an unmipped
874
869
  // texture aliases into noise at distance, which looks worse than the vertex colours it replaced.
@@ -911,22 +906,50 @@ export class GpuPropsRenderer {
911
906
  // occluder pass, whose index buffer is fed to `drawIndexedIndirect` with args the GPU writes.
912
907
  const totalVerts = meshes.reduce((n, m) => n + m.positions.length / 3, 0);
913
908
  const totalIdx = meshes.reduce((n, m) => n + m.indices.length, 0);
914
- const allPos = new Float32Array(Math.max(3, totalVerts * 3));
915
- const allNrm = new Float32Array(allPos.length);
916
- const allCol = new Float32Array(allPos.length);
917
- const allUv = new Float32Array(Math.max(2, totalVerts * 2));
918
- const allIdx = new Uint32Array(Math.max(1, totalIdx));
909
+ // Cooked streams already have the GPU layout. Upload them directly to their
910
+ // arena offsets instead of allocating/copying five more CPU-sized arenas.
911
+ this.geo?.destroy();
912
+ this.geo = {
913
+ pos: dev.createBuffer({
914
+ size: Math.max(12, totalVerts * 12),
915
+ usage: U.VERTEX | U.COPY_DST,
916
+ }),
917
+ nrm: dev.createBuffer({
918
+ size: Math.max(12, totalVerts * 12),
919
+ usage: U.VERTEX | U.COPY_DST,
920
+ }),
921
+ col: dev.createBuffer({
922
+ size: Math.max(12, totalVerts * 12),
923
+ usage: U.VERTEX | U.COPY_DST,
924
+ }),
925
+ uv: dev.createBuffer({
926
+ size: Math.max(8, totalVerts * 8),
927
+ usage: U.VERTEX | U.COPY_DST,
928
+ }),
929
+ idx: dev.createBuffer({
930
+ size: Math.max(4, totalIdx * 4),
931
+ usage: U.INDEX | U.COPY_DST,
932
+ }),
933
+ destroy() {
934
+ for (const buffer of [this.pos, this.nrm, this.col, this.uv, this.idx])
935
+ buffer.destroy();
936
+ },
937
+ };
938
+ const upload = (buffer, offset, data) => {
939
+ if (data.byteLength)
940
+ dev.queue.writeBuffer(buffer, offset, data);
941
+ };
919
942
  let vCursor = 0, iCursor = 0;
920
943
  this.models = meshes.map((m) => {
921
944
  const verts = m.positions.length / 3;
922
945
  const baseVertex = vCursor, firstIndex = iCursor;
923
- allPos.set(m.positions, baseVertex * 3);
924
- allNrm.set(m.normals, baseVertex * 3);
925
- allCol.set(m.colors, baseVertex * 3);
926
- // zero-filled when absent: the shader still reads location 3, and white x anything is anything
946
+ upload(this.geo.pos, baseVertex * 12, m.positions);
947
+ upload(this.geo.nrm, baseVertex * 12, m.normals);
948
+ upload(this.geo.col, baseVertex * 12, m.colors);
949
+ // Fresh WebGPU allocations are zero initialized, including absent UVs.
927
950
  if (m.uvs)
928
- allUv.set(m.uvs, baseVertex * 2);
929
- allIdx.set(m.indices, firstIndex);
951
+ upload(this.geo.uv, baseVertex * 8, m.uvs);
952
+ upload(this.geo.idx, firstIndex * 4, m.indices);
930
953
  vCursor += verts;
931
954
  iCursor += m.indices.length;
932
955
  // A GPUTexture is already uploaded and already mipped (the KTX2 path) — bind it directly.
@@ -977,7 +1000,7 @@ export class GpuPropsRenderer {
977
1000
  firstIndex,
978
1001
  baseVertex,
979
1002
  idxCount: m.indices.length,
980
- extent: measureExtent(m.positions),
1003
+ extent: m.extent ?? measureExtent(m.positions),
981
1004
  tex,
982
1005
  matBG: dev.createBindGroup({
983
1006
  layout: this.drawBGL[2],
@@ -995,19 +1018,6 @@ export class GpuPropsRenderer {
995
1018
  }),
996
1019
  };
997
1020
  });
998
- // Built AFTER the map, because the map is what fills the arenas.
999
- this.geo?.destroy();
1000
- this.geo = {
1001
- pos: mk(allPos, U.VERTEX | U.COPY_DST),
1002
- nrm: mk(allNrm, U.VERTEX | U.COPY_DST),
1003
- col: mk(allCol, U.VERTEX | U.COPY_DST),
1004
- uv: mk(allUv, U.VERTEX | U.COPY_DST),
1005
- idx: mk(allIdx, U.INDEX | U.COPY_DST),
1006
- destroy() {
1007
- for (const b of [this.pos, this.nrm, this.col, this.uv, this.idx])
1008
- b.destroy();
1009
- },
1010
- };
1011
1021
  this.caps = meshes.map(() => 0);
1012
1022
  this.instBufs = meshes.map(() => null);
1013
1023
  this.bgs = meshes.map(() => null);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ugly-game",
3
- "version": "0.15.55",
3
+ "version": "0.15.57",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {