ugly-game 0.15.55 → 0.15.56
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,68 @@
|
|
|
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) {
|
|
5
|
+
await MeshoptEncoder.ready;
|
|
6
|
+
const h = new DataView(raw.buffer, raw.byteOffset, raw.byteLength);
|
|
7
|
+
const vertices = h.getUint32(4, true), indices = h.getUint32(8, true);
|
|
8
|
+
const streams = [];
|
|
9
|
+
let offset = 32;
|
|
10
|
+
for (const [count, stride] of [
|
|
11
|
+
[vertices, 12],
|
|
12
|
+
[vertices, 12],
|
|
13
|
+
[vertices, 12],
|
|
14
|
+
[vertices, 8],
|
|
15
|
+
[indices, 4],
|
|
16
|
+
]) {
|
|
17
|
+
const bytes = raw.subarray(offset, offset + count * stride);
|
|
18
|
+
streams.push(count
|
|
19
|
+
? MeshoptEncoder.encodeVertexBuffer(bytes, count, stride)
|
|
20
|
+
: new Uint8Array());
|
|
21
|
+
offset += bytes.length;
|
|
22
|
+
}
|
|
23
|
+
const result = new Uint8Array(64 + streams.reduce((n, s) => n + s.length, 0));
|
|
24
|
+
result.set(raw.subarray(0, 32));
|
|
25
|
+
const header = new DataView(result.buffer);
|
|
26
|
+
header.setUint32(0, 0x32595047, true);
|
|
27
|
+
offset = 64;
|
|
28
|
+
streams.forEach((stream, i) => {
|
|
29
|
+
header.setUint32(32 + i * 4, stream.length, true);
|
|
30
|
+
result.set(stream, offset);
|
|
31
|
+
offset += stream.length;
|
|
32
|
+
});
|
|
33
|
+
return result;
|
|
34
|
+
}
|
|
35
|
+
export async function decodeGpuGeometry(packed) {
|
|
36
|
+
if (packed.length < 64)
|
|
37
|
+
throw new Error('Incomplete GPU geometry');
|
|
38
|
+
const h = new DataView(packed.buffer, packed.byteOffset, packed.byteLength);
|
|
39
|
+
const vertices = h.getUint32(4, true), indices = h.getUint32(8, true);
|
|
40
|
+
if (h.getUint32(0, true) !== 0x32595047 ||
|
|
41
|
+
vertices > 2_000_000 ||
|
|
42
|
+
indices > 6_000_000)
|
|
43
|
+
throw new Error('Unsupported GPU geometry');
|
|
44
|
+
const lengths = Array.from({ length: 5 }, (_, i) => h.getUint32(32 + i * 4, true));
|
|
45
|
+
if (64 + lengths.reduce((n, v) => n + v, 0) !== packed.length)
|
|
46
|
+
throw new Error('Invalid GPU stream sizes');
|
|
47
|
+
await MeshoptDecoder.ready;
|
|
48
|
+
const result = new Uint8Array(32 + vertices * 44 + indices * 4);
|
|
49
|
+
result.set(packed.subarray(0, 32));
|
|
50
|
+
new DataView(result.buffer).setUint32(0, 0x31595047, true);
|
|
51
|
+
let source = 64, dest = 32, stream = 0;
|
|
52
|
+
for (const [count, stride] of [
|
|
53
|
+
[vertices, 12],
|
|
54
|
+
[vertices, 12],
|
|
55
|
+
[vertices, 12],
|
|
56
|
+
[vertices, 8],
|
|
57
|
+
[indices, 4],
|
|
58
|
+
]) {
|
|
59
|
+
const length = lengths[stream++];
|
|
60
|
+
if (count)
|
|
61
|
+
MeshoptDecoder.decodeVertexBuffer(result.subarray(dest, dest + count * stride), count, stride, packed.subarray(source, source + length));
|
|
62
|
+
else if (length)
|
|
63
|
+
throw new Error('Nonempty payload for empty GPU stream');
|
|
64
|
+
source += length;
|
|
65
|
+
dest += count * stride;
|
|
66
|
+
}
|
|
67
|
+
return result;
|
|
68
|
+
}
|
|
@@ -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;
|
package/dist/assets/gpuProps.js
CHANGED
|
@@ -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
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
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
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
//
|
|
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
|
-
|
|
929
|
-
|
|
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);
|