ugly-game 0.15.56 → 0.15.58
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.
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare function encodeGpuGeometry(raw: Uint8Array): Promise<Uint8Array>;
|
|
1
|
+
export declare function encodeGpuGeometry(raw: Uint8Array, precisionBits?: number): Promise<Uint8Array>;
|
|
2
2
|
export declare function decodeGpuGeometry(packed: Uint8Array): Promise<Uint8Array>;
|
|
@@ -1,12 +1,18 @@
|
|
|
1
|
-
/**
|
|
2
|
-
*
|
|
1
|
+
/** Cooked GPU streams with lossless byte transport by default. Optional precision
|
|
2
|
+
* reduction happens at cook time; the decoder decompresses into final float buffers
|
|
3
|
+
* without topology, color, normal or layout rebuilding. */
|
|
3
4
|
import { MeshoptEncoder, MeshoptDecoder } from 'meshoptimizer';
|
|
4
|
-
export async function encodeGpuGeometry(raw) {
|
|
5
|
+
export async function encodeGpuGeometry(raw, precisionBits = 0) {
|
|
6
|
+
if (precisionBits &&
|
|
7
|
+
(!Number.isInteger(precisionBits) ||
|
|
8
|
+
precisionBits < 12 ||
|
|
9
|
+
precisionBits > 24))
|
|
10
|
+
throw new Error('Unsupported cook precision');
|
|
5
11
|
await MeshoptEncoder.ready;
|
|
6
12
|
const h = new DataView(raw.buffer, raw.byteOffset, raw.byteLength);
|
|
7
13
|
const vertices = h.getUint32(4, true), indices = h.getUint32(8, true);
|
|
8
14
|
const streams = [];
|
|
9
|
-
let offset = 32;
|
|
15
|
+
let offset = 32, streamIndex = 0;
|
|
10
16
|
for (const [count, stride] of [
|
|
11
17
|
[vertices, 12],
|
|
12
18
|
[vertices, 12],
|
|
@@ -15,8 +21,14 @@ export async function encodeGpuGeometry(raw) {
|
|
|
15
21
|
[indices, 4],
|
|
16
22
|
]) {
|
|
17
23
|
const bytes = raw.subarray(offset, offset + count * stride);
|
|
24
|
+
const indexStream = streamIndex++ === 4;
|
|
25
|
+
const filtered = precisionBits && !indexStream && count
|
|
26
|
+
? MeshoptEncoder.encodeFilterExp(new Float32Array(bytes.buffer, bytes.byteOffset, bytes.byteLength / 4), count, stride, precisionBits, 'SharedComponent')
|
|
27
|
+
: bytes;
|
|
18
28
|
streams.push(count
|
|
19
|
-
?
|
|
29
|
+
? indexStream
|
|
30
|
+
? MeshoptEncoder.encodeIndexSequence(bytes, count, stride)
|
|
31
|
+
: MeshoptEncoder.encodeVertexBuffer(filtered, count, stride)
|
|
20
32
|
: new Uint8Array());
|
|
21
33
|
offset += bytes.length;
|
|
22
34
|
}
|
|
@@ -24,6 +36,8 @@ export async function encodeGpuGeometry(raw) {
|
|
|
24
36
|
result.set(raw.subarray(0, 32));
|
|
25
37
|
const header = new DataView(result.buffer);
|
|
26
38
|
header.setUint32(0, 0x32595047, true);
|
|
39
|
+
header.setUint32(20, precisionBits ? 1 : 0, true);
|
|
40
|
+
header.setUint32(24, 1, true);
|
|
27
41
|
offset = 64;
|
|
28
42
|
streams.forEach((stream, i) => {
|
|
29
43
|
header.setUint32(32 + i * 4, stream.length, true);
|
|
@@ -41,13 +55,19 @@ export async function decodeGpuGeometry(packed) {
|
|
|
41
55
|
vertices > 2_000_000 ||
|
|
42
56
|
indices > 6_000_000)
|
|
43
57
|
throw new Error('Unsupported GPU geometry');
|
|
58
|
+
const exp = h.getUint32(20, true), indexSequence = h.getUint32(24, true);
|
|
59
|
+
if (exp > 1 || indexSequence > 1)
|
|
60
|
+
throw new Error('Unsupported GPU stream codec');
|
|
44
61
|
const lengths = Array.from({ length: 5 }, (_, i) => h.getUint32(32 + i * 4, true));
|
|
45
62
|
if (64 + lengths.reduce((n, v) => n + v, 0) !== packed.length)
|
|
46
63
|
throw new Error('Invalid GPU stream sizes');
|
|
47
64
|
await MeshoptDecoder.ready;
|
|
48
65
|
const result = new Uint8Array(32 + vertices * 44 + indices * 4);
|
|
49
66
|
result.set(packed.subarray(0, 32));
|
|
50
|
-
new DataView(result.buffer)
|
|
67
|
+
const resultHeader = new DataView(result.buffer);
|
|
68
|
+
resultHeader.setUint32(0, 0x31595047, true);
|
|
69
|
+
resultHeader.setUint32(20, 0, true);
|
|
70
|
+
resultHeader.setUint32(24, 0, true);
|
|
51
71
|
let source = 64, dest = 32, stream = 0;
|
|
52
72
|
for (const [count, stride] of [
|
|
53
73
|
[vertices, 12],
|
|
@@ -57,8 +77,13 @@ export async function decodeGpuGeometry(packed) {
|
|
|
57
77
|
[indices, 4],
|
|
58
78
|
]) {
|
|
59
79
|
const length = lengths[stream++];
|
|
60
|
-
if (count)
|
|
61
|
-
|
|
80
|
+
if (count) {
|
|
81
|
+
const target = result.subarray(dest, dest + count * stride), sourceBytes = packed.subarray(source, source + length);
|
|
82
|
+
if (stream === 5 && indexSequence)
|
|
83
|
+
MeshoptDecoder.decodeIndexSequence(target, count, stride, sourceBytes);
|
|
84
|
+
else
|
|
85
|
+
MeshoptDecoder.decodeVertexBuffer(target, count, stride, sourceBytes, stream < 5 && exp ? 'EXPONENTIAL' : undefined);
|
|
86
|
+
}
|
|
62
87
|
else if (length)
|
|
63
88
|
throw new Error('Nonempty payload for empty GPU stream');
|
|
64
89
|
source += length;
|