ugly-game 0.15.24 → 0.15.26
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.
- package/dist/assets/gpuProps.d.ts +7 -0
- package/dist/assets/gpuProps.js +78 -20
- package/dist/assets/lod.d.ts +19 -1
- package/dist/assets/lod.js +10 -3
- package/dist/assets/lodSelect.d.ts +56 -0
- package/dist/assets/lodSelect.js +70 -0
- package/dist/assets/ugm.d.ts +68 -12
- package/dist/assets/ugm.js +127 -13
- package/package.json +16 -15
|
@@ -183,6 +183,13 @@ export declare class GpuPropsRenderer {
|
|
|
183
183
|
private cullPipe;
|
|
184
184
|
private cullU;
|
|
185
185
|
private cullArgs;
|
|
186
|
+
/**
|
|
187
|
+
* The shared geometry arena: every model's vertices and indices, concatenated.
|
|
188
|
+
*
|
|
189
|
+
* One binding for the whole pass instead of five per model — see setModels for why that matters to
|
|
190
|
+
* the LOD system rather than merely to the encoder.
|
|
191
|
+
*/
|
|
192
|
+
private geo;
|
|
186
193
|
private cullVis;
|
|
187
194
|
private cullBGs;
|
|
188
195
|
/** Survivor lists are per (model, cascade) but packed into one buffer per model at this stride. */
|
package/dist/assets/gpuProps.js
CHANGED
|
@@ -394,6 +394,13 @@ export class GpuPropsRenderer {
|
|
|
394
394
|
cullPipe = null;
|
|
395
395
|
cullU = []; // planes + info, one per (model, cascade)
|
|
396
396
|
cullArgs = []; // drawIndexedIndirect args
|
|
397
|
+
/**
|
|
398
|
+
* The shared geometry arena: every model's vertices and indices, concatenated.
|
|
399
|
+
*
|
|
400
|
+
* One binding for the whole pass instead of five per model — see setModels for why that matters to
|
|
401
|
+
* the LOD system rather than merely to the encoder.
|
|
402
|
+
*/
|
|
403
|
+
geo = null;
|
|
397
404
|
cullVis = []; // survivor list — per MODEL, see cullPass
|
|
398
405
|
cullBGs = [];
|
|
399
406
|
/** Survivor lists are per (model, cascade) but packed into one buffer per model at this stride. */
|
|
@@ -667,12 +674,8 @@ export class GpuPropsRenderer {
|
|
|
667
674
|
setModels(meshes) {
|
|
668
675
|
const dev = this.device, U = GPUBufferUsage;
|
|
669
676
|
for (const p of this.models) {
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
p.colB.destroy();
|
|
673
|
-
p.uvB.destroy();
|
|
674
|
-
p.idxB.destroy();
|
|
675
|
-
// Only what this renderer allocated. See Packed.ownsTex — freeing a caller's GPUTexture makes
|
|
677
|
+
// Geometry is no longer per model — the shared arena is freed once, below, after the new one
|
|
678
|
+
// has been sized. Only what this renderer allocated. See Packed.ownsTex — freeing a caller's GPUTexture makes
|
|
676
679
|
// a repeated setModels bind dead resources.
|
|
677
680
|
if (p.ownsTex)
|
|
678
681
|
p.tex?.destroy();
|
|
@@ -706,8 +709,43 @@ export class GpuPropsRenderer {
|
|
|
706
709
|
dev.queue.writeTexture({ texture: t }, new Uint8Array([255, 255, 255, 255]), {}, [1, 1]);
|
|
707
710
|
return t;
|
|
708
711
|
})();
|
|
712
|
+
// ── ONE GEOMETRY ARENA FOR EVERY MODEL ────────────────────────────────────────────────────────
|
|
713
|
+
//
|
|
714
|
+
// Every model's vertices land in four shared buffers and every model's indices in one, so a draw
|
|
715
|
+
// pass binds geometry ONCE and addresses a model with `firstIndex` / `baseVertex` instead of
|
|
716
|
+
// rebinding. Per-model that is 8 encoder calls (4 setVertexBuffer + setIndexBuffer + 2
|
|
717
|
+
// setBindGroup + the draw) down to 3.
|
|
718
|
+
//
|
|
719
|
+
// ⚠️ THE POINT IS NOT THE ENCODER CALLS, IT IS WHAT THEY WERE COSTING THE LOD SYSTEM. A rung is a
|
|
720
|
+
// separate model here, so every extra rung in flight cost one full rebind per species — measured
|
|
721
|
+
// in all-mine-sky at ~28 draws per rung, which is why the cheapest configuration was the one that
|
|
722
|
+
// collapsed every plant onto a SINGLE rung and the LOD selector was penalised for using its
|
|
723
|
+
// ladder. Geometry rebinding was the thing making granularity expensive; it is now free, and a
|
|
724
|
+
// selector can spend rungs on quality without buying draw-call cost with them.
|
|
725
|
+
//
|
|
726
|
+
// Indices stay MODEL-LOCAL and are offset by `baseVertex` at draw time rather than being rewritten
|
|
727
|
+
// — rewriting them would work equally well for the camera pass and would silently break the
|
|
728
|
+
// occluder pass, whose index buffer is fed to `drawIndexedIndirect` with args the GPU writes.
|
|
729
|
+
const totalVerts = meshes.reduce((n, m) => n + m.positions.length / 3, 0);
|
|
730
|
+
const totalIdx = meshes.reduce((n, m) => n + m.indices.length, 0);
|
|
731
|
+
const allPos = new Float32Array(Math.max(3, totalVerts * 3));
|
|
732
|
+
const allNrm = new Float32Array(allPos.length);
|
|
733
|
+
const allCol = new Float32Array(allPos.length);
|
|
734
|
+
const allUv = new Float32Array(Math.max(2, totalVerts * 2));
|
|
735
|
+
const allIdx = new Uint32Array(Math.max(1, totalIdx));
|
|
736
|
+
let vCursor = 0, iCursor = 0;
|
|
709
737
|
this.models = meshes.map((m) => {
|
|
710
738
|
const verts = m.positions.length / 3;
|
|
739
|
+
const baseVertex = vCursor, firstIndex = iCursor;
|
|
740
|
+
allPos.set(m.positions, baseVertex * 3);
|
|
741
|
+
allNrm.set(m.normals, baseVertex * 3);
|
|
742
|
+
allCol.set(m.colors, baseVertex * 3);
|
|
743
|
+
// zero-filled when absent: the shader still reads location 3, and white x anything is anything
|
|
744
|
+
if (m.uvs)
|
|
745
|
+
allUv.set(m.uvs, baseVertex * 2);
|
|
746
|
+
allIdx.set(m.indices, firstIndex);
|
|
747
|
+
vCursor += verts;
|
|
748
|
+
iCursor += m.indices.length;
|
|
711
749
|
// A GPUTexture is already uploaded and already mipped (the KTX2 path) — bind it directly.
|
|
712
750
|
// Only an ImageBitmap needs uploadAlbedo's hand-rolled half-scale mip chain.
|
|
713
751
|
const ownsTex = !!m.albedo && !(m.albedo instanceof GPUTexture);
|
|
@@ -728,12 +766,8 @@ export class GpuPropsRenderer {
|
|
|
728
766
|
cutout,
|
|
729
767
|
matBuf,
|
|
730
768
|
ownsTex,
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
colB: mk(m.colors, U.VERTEX | U.COPY_DST),
|
|
734
|
-
// zero-filled when absent: the shader still reads location 3, and white x anything is anything
|
|
735
|
-
uvB: mk(m.uvs ?? new Float32Array(verts * 2), U.VERTEX | U.COPY_DST),
|
|
736
|
-
idxB: mk(m.indices, U.INDEX | U.COPY_DST),
|
|
769
|
+
firstIndex,
|
|
770
|
+
baseVertex,
|
|
737
771
|
idxCount: m.indices.length,
|
|
738
772
|
extent: measureExtent(m.positions),
|
|
739
773
|
tex,
|
|
@@ -747,6 +781,19 @@ export class GpuPropsRenderer {
|
|
|
747
781
|
}),
|
|
748
782
|
};
|
|
749
783
|
});
|
|
784
|
+
// Built AFTER the map, because the map is what fills the arenas.
|
|
785
|
+
this.geo?.destroy();
|
|
786
|
+
this.geo = {
|
|
787
|
+
pos: mk(allPos, U.VERTEX | U.COPY_DST),
|
|
788
|
+
nrm: mk(allNrm, U.VERTEX | U.COPY_DST),
|
|
789
|
+
col: mk(allCol, U.VERTEX | U.COPY_DST),
|
|
790
|
+
uv: mk(allUv, U.VERTEX | U.COPY_DST),
|
|
791
|
+
idx: mk(allIdx, U.INDEX | U.COPY_DST),
|
|
792
|
+
destroy() {
|
|
793
|
+
for (const b of [this.pos, this.nrm, this.col, this.uv, this.idx])
|
|
794
|
+
b.destroy();
|
|
795
|
+
},
|
|
796
|
+
};
|
|
750
797
|
this.caps = meshes.map(() => 0);
|
|
751
798
|
this.instBufs = meshes.map(() => null);
|
|
752
799
|
this.bgs = meshes.map(() => null);
|
|
@@ -916,6 +963,11 @@ export class GpuPropsRenderer {
|
|
|
916
963
|
this.cullStats.submitted += this.counts[m] * nc;
|
|
917
964
|
reset[0] = this.models[m].idxCount;
|
|
918
965
|
reset[1] = this.cullOn ? 0 : this.counts[m]; // cull off ⇒ draw them all, no dispatch
|
|
966
|
+
// ⚠️ THE ARENA OFFSETS RIDE IN THE INDIRECT ARGS. The occluder pass binds one shared index
|
|
967
|
+
// buffer, so without these every model would draw model 0's triangles — every shadow in the
|
|
968
|
+
// world cast by the same silhouette, which is a picture that looks plausible at a glance.
|
|
969
|
+
reset[2] = this.models[m].firstIndex;
|
|
970
|
+
reset[3] = this.models[m].baseVertex;
|
|
919
971
|
for (let c = 0; c < nc; c++)
|
|
920
972
|
this.device.queue.writeBuffer(this.cullArgs[m][c], 0, reset);
|
|
921
973
|
}
|
|
@@ -989,6 +1041,10 @@ export class GpuPropsRenderer {
|
|
|
989
1041
|
], 16);
|
|
990
1042
|
this.device.queue.writeBuffer(this.lightBufs[c], 0, ob);
|
|
991
1043
|
pass.setPipeline(this.occPipe);
|
|
1044
|
+
if (!this.geo)
|
|
1045
|
+
return;
|
|
1046
|
+
pass.setVertexBuffer(0, this.geo.pos);
|
|
1047
|
+
pass.setIndexBuffer(this.geo.idx, 'uint32');
|
|
992
1048
|
for (let m = 0; m < this.models.length; m++) {
|
|
993
1049
|
const mdl = this.models[m];
|
|
994
1050
|
// A model whose cull resources have not been allocated yet (its bucket was empty when the
|
|
@@ -1014,8 +1070,6 @@ export class GpuPropsRenderer {
|
|
|
1014
1070
|
if (!args)
|
|
1015
1071
|
continue;
|
|
1016
1072
|
pass.setBindGroup(0, bg);
|
|
1017
|
-
pass.setVertexBuffer(0, mdl.posB);
|
|
1018
|
-
pass.setIndexBuffer(mdl.idxB, 'uint32');
|
|
1019
1073
|
// ⚠️ INDIRECT, so the count comes from the GPU. Reading it back to skip empty models would cost
|
|
1020
1074
|
// a stall per model per cascade to save a draw whose instanceCount is already 0 — and the draw
|
|
1021
1075
|
// count is the number this whole exercise is not allowed to raise.
|
|
@@ -1072,18 +1126,22 @@ export class GpuPropsRenderer {
|
|
|
1072
1126
|
},
|
|
1073
1127
|
});
|
|
1074
1128
|
rp.setBindGroup(1, this.shadowBG);
|
|
1129
|
+
// The arena, bound once for every model in both loops below.
|
|
1130
|
+
if (this.geo) {
|
|
1131
|
+
rp.setVertexBuffer(0, this.geo.pos);
|
|
1132
|
+
rp.setVertexBuffer(1, this.geo.nrm);
|
|
1133
|
+
rp.setVertexBuffer(2, this.geo.col);
|
|
1134
|
+
rp.setVertexBuffer(3, this.geo.uv);
|
|
1135
|
+
rp.setIndexBuffer(this.geo.idx, 'uint32');
|
|
1136
|
+
}
|
|
1075
1137
|
const drawModel = (m) => {
|
|
1076
1138
|
const bg = this.bgs[m], mdl = this.models[m];
|
|
1077
1139
|
if (!bg || this.counts[m] === 0)
|
|
1078
1140
|
return;
|
|
1079
1141
|
rp.setBindGroup(0, bg);
|
|
1080
|
-
rp.setVertexBuffer(0, mdl.posB);
|
|
1081
|
-
rp.setVertexBuffer(1, mdl.nrmB);
|
|
1082
|
-
rp.setVertexBuffer(2, mdl.colB);
|
|
1083
|
-
rp.setVertexBuffer(3, mdl.uvB);
|
|
1084
1142
|
rp.setBindGroup(2, mdl.matBG);
|
|
1085
|
-
|
|
1086
|
-
rp.drawIndexed(mdl.idxCount, this.counts[m]);
|
|
1143
|
+
// Geometry is bound ONCE for the whole pass; a model is a range inside it.
|
|
1144
|
+
rp.drawIndexed(mdl.idxCount, this.counts[m], mdl.firstIndex, mdl.baseVertex);
|
|
1087
1145
|
};
|
|
1088
1146
|
// OPAQUE FIRST, then the blended models — the whole point of the second pass. A translucent
|
|
1089
1147
|
// crystal drawn before an opaque rock composites against whatever the sky left in the target and
|
package/dist/assets/lod.d.ts
CHANGED
|
@@ -1,2 +1,20 @@
|
|
|
1
1
|
import type { RawMesh } from './geometry';
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* One rung, plus HOW WRONG IT IS — the number a render-size LOD selector runs on.
|
|
4
|
+
*
|
|
5
|
+
* `error` is the rung's geometric deviation from the source in the mesh's own units. meshopt returns
|
|
6
|
+
* it relative to the mesh extent, so it is multiplied by `getScale` here and never leaves this module
|
|
7
|
+
* in relative form: a caller comparing a relative error against a world-space budget would be right
|
|
8
|
+
* only for a mesh that happens to be one unit across.
|
|
9
|
+
*
|
|
10
|
+
* It was being DISCARDED. `simplify` has always returned `[indices, error]` and every caller
|
|
11
|
+
* destructured the first element alone, which left every consumer with no way to ask the only
|
|
12
|
+
* question that matters at draw time — "is this rung good enough from here?" — and forced them to
|
|
13
|
+
* guess from triangle counts or hardcoded distance bands instead.
|
|
14
|
+
*/
|
|
15
|
+
export interface LodRung extends RawMesh {
|
|
16
|
+
/** Absolute geometric deviation from the source, in the mesh's own units. Exactly 0 for the
|
|
17
|
+
* source rung, and non-decreasing as the ladder coarsens. */
|
|
18
|
+
error: number;
|
|
19
|
+
}
|
|
20
|
+
export declare function buildLodLadder(m: RawMesh, ratios: number[]): Promise<LodRung[]>;
|
package/dist/assets/lod.js
CHANGED
|
@@ -4,15 +4,22 @@
|
|
|
4
4
|
import { MeshoptSimplifier } from 'meshoptimizer';
|
|
5
5
|
export async function buildLodLadder(m, ratios) {
|
|
6
6
|
await MeshoptSimplifier.ready;
|
|
7
|
+
// Extent of the SOURCE mesh, taken once: every rung reuses the source's position array, so a
|
|
8
|
+
// per-rung scale would be the same number computed n times.
|
|
9
|
+
const scale = MeshoptSimplifier.getScale(m.positions, 3);
|
|
7
10
|
const out = [];
|
|
8
11
|
for (const r of ratios) {
|
|
9
12
|
if (r >= 1) {
|
|
10
|
-
out.push({ positions: m.positions, indices: m.indices });
|
|
13
|
+
out.push({ positions: m.positions, indices: m.indices, error: 0 });
|
|
11
14
|
continue;
|
|
12
15
|
}
|
|
13
16
|
const targetCount = Math.max(3, Math.floor((m.indices.length * r) / 3) * 3);
|
|
14
|
-
const [simplified] = MeshoptSimplifier.simplify(m.indices, m.positions, 3, targetCount, 0.05, ['LockBorder']);
|
|
15
|
-
out.push({
|
|
17
|
+
const [simplified, relError] = MeshoptSimplifier.simplify(m.indices, m.positions, 3, targetCount, 0.05, ['LockBorder']);
|
|
18
|
+
out.push({
|
|
19
|
+
positions: m.positions,
|
|
20
|
+
indices: Uint32Array.from(simplified),
|
|
21
|
+
error: relError * scale,
|
|
22
|
+
});
|
|
16
23
|
}
|
|
17
24
|
return out;
|
|
18
25
|
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { LodLevel } from './ugm';
|
|
2
|
+
/**
|
|
3
|
+
* Pixels per unit of (size / distance) — the one camera number this file needs.
|
|
4
|
+
*
|
|
5
|
+
* Both halves of the perspective are in here: `viewportHeightPx` carries the resolution and
|
|
6
|
+
* `fovY` the zoom, so a selector fed this value tracks a window resize and a scope-in without
|
|
7
|
+
* knowing that either happened.
|
|
8
|
+
*/
|
|
9
|
+
export declare function focalPixels(fovY: number, viewportHeightPx: number): number;
|
|
10
|
+
/**
|
|
11
|
+
* How many pixels a rung's geometric error covers from `distance` away.
|
|
12
|
+
*
|
|
13
|
+
* `worldScale` is the instance's own scale — metres per model unit — because the table is cooked in
|
|
14
|
+
* model units. Two copies of one species at 1x and 3x are genuinely different LOD problems and this
|
|
15
|
+
* is where they separate.
|
|
16
|
+
*/
|
|
17
|
+
export declare function projectedErrorPx(error: number, worldScale: number, distance: number, focalPx: number): number;
|
|
18
|
+
/**
|
|
19
|
+
* RENDER SIZE: how tall the whole model stands on screen, in pixels, from `distance` away.
|
|
20
|
+
*
|
|
21
|
+
* Not what selection runs on — error is — but it is the number a human can judge, so it is what
|
|
22
|
+
* telemetry and lab readouts should show. "This broadleaf is 14 px tall and drawing 6,000 triangles"
|
|
23
|
+
* is a legible bug report; "its projected error is 0.4 px" is not.
|
|
24
|
+
*
|
|
25
|
+
* `radius` is the container's `bounds[0]`, so this is the bounding-sphere diameter and reads a
|
|
26
|
+
* little generous for a tall thin plant. That is the right direction to be wrong in for a readout.
|
|
27
|
+
*/
|
|
28
|
+
export declare function projectedSizePx(radius: number, worldScale: number, distance: number, focalPx: number): number;
|
|
29
|
+
export interface LodPick {
|
|
30
|
+
/** Instance scale, in metres per model unit. */
|
|
31
|
+
worldScale: number;
|
|
32
|
+
/** Metres from the camera to the instance. */
|
|
33
|
+
distance: number;
|
|
34
|
+
/** From `focalPixels`. */
|
|
35
|
+
focalPx: number;
|
|
36
|
+
/** The quality knob: geometric deviation, in pixels, you are willing not to notice. ~1 px is
|
|
37
|
+
* conservative; raising it is the cheapest quality-for-speed trade in the renderer. */
|
|
38
|
+
tolerancePx: number;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* The COARSEST rung whose error stays inside the pixel budget — or the finest, if none does.
|
|
42
|
+
*
|
|
43
|
+
* `levels` must be ascending by `detailLevel`, which is what `readLodChunk` returns. Nothing is
|
|
44
|
+
* sorted or allocated here on purpose: this runs once per decor instance per rebuild, thousands of
|
|
45
|
+
* times, and a sort per call would cost more than the draw it is saving.
|
|
46
|
+
*
|
|
47
|
+
* ⚠️ CORRECT ON A BROKEN LADDER, and two shipped containers need it to be. `flora.fungal.lumen` was
|
|
48
|
+
* cooked with three identical coarse rungs; scanning coarse-to-fine for the first rung that FITS
|
|
49
|
+
* picks the cheapest of those three rather than assuming error falls monotonically and bisecting
|
|
50
|
+
* into the wrong one. A defective cook should cost triangles, not correctness.
|
|
51
|
+
*
|
|
52
|
+
* ⚠️ AND DEGENERATE INPUT RESOLVES TO THE FINEST RUNG. A zero viewport during layout, a
|
|
53
|
+
* not-yet-known scale, a camera exactly on top of a plant: every one of those is a moment where
|
|
54
|
+
* guessing coarse would show a visible pop for no measurable saving.
|
|
55
|
+
*/
|
|
56
|
+
export declare function pickDetailLevel(levels: readonly LodLevel[], p: LodPick): number;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pixels per unit of (size / distance) — the one camera number this file needs.
|
|
3
|
+
*
|
|
4
|
+
* Both halves of the perspective are in here: `viewportHeightPx` carries the resolution and
|
|
5
|
+
* `fovY` the zoom, so a selector fed this value tracks a window resize and a scope-in without
|
|
6
|
+
* knowing that either happened.
|
|
7
|
+
*/
|
|
8
|
+
export function focalPixels(fovY, viewportHeightPx) {
|
|
9
|
+
const t = Math.tan(fovY / 2);
|
|
10
|
+
return t > 0 ? viewportHeightPx / 2 / t : 0;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* How many pixels a rung's geometric error covers from `distance` away.
|
|
14
|
+
*
|
|
15
|
+
* `worldScale` is the instance's own scale — metres per model unit — because the table is cooked in
|
|
16
|
+
* model units. Two copies of one species at 1x and 3x are genuinely different LOD problems and this
|
|
17
|
+
* is where they separate.
|
|
18
|
+
*/
|
|
19
|
+
export function projectedErrorPx(error, worldScale, distance, focalPx) {
|
|
20
|
+
return distance > 0 ? (error * worldScale * focalPx) / distance : Infinity;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* RENDER SIZE: how tall the whole model stands on screen, in pixels, from `distance` away.
|
|
24
|
+
*
|
|
25
|
+
* Not what selection runs on — error is — but it is the number a human can judge, so it is what
|
|
26
|
+
* telemetry and lab readouts should show. "This broadleaf is 14 px tall and drawing 6,000 triangles"
|
|
27
|
+
* is a legible bug report; "its projected error is 0.4 px" is not.
|
|
28
|
+
*
|
|
29
|
+
* `radius` is the container's `bounds[0]`, so this is the bounding-sphere diameter and reads a
|
|
30
|
+
* little generous for a tall thin plant. That is the right direction to be wrong in for a readout.
|
|
31
|
+
*/
|
|
32
|
+
export function projectedSizePx(radius, worldScale, distance, focalPx) {
|
|
33
|
+
return distance > 0
|
|
34
|
+
? (2 * radius * worldScale * focalPx) / distance
|
|
35
|
+
: Infinity;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The COARSEST rung whose error stays inside the pixel budget — or the finest, if none does.
|
|
39
|
+
*
|
|
40
|
+
* `levels` must be ascending by `detailLevel`, which is what `readLodChunk` returns. Nothing is
|
|
41
|
+
* sorted or allocated here on purpose: this runs once per decor instance per rebuild, thousands of
|
|
42
|
+
* times, and a sort per call would cost more than the draw it is saving.
|
|
43
|
+
*
|
|
44
|
+
* ⚠️ CORRECT ON A BROKEN LADDER, and two shipped containers need it to be. `flora.fungal.lumen` was
|
|
45
|
+
* cooked with three identical coarse rungs; scanning coarse-to-fine for the first rung that FITS
|
|
46
|
+
* picks the cheapest of those three rather than assuming error falls monotonically and bisecting
|
|
47
|
+
* into the wrong one. A defective cook should cost triangles, not correctness.
|
|
48
|
+
*
|
|
49
|
+
* ⚠️ AND DEGENERATE INPUT RESOLVES TO THE FINEST RUNG. A zero viewport during layout, a
|
|
50
|
+
* not-yet-known scale, a camera exactly on top of a plant: every one of those is a moment where
|
|
51
|
+
* guessing coarse would show a visible pop for no measurable saving.
|
|
52
|
+
*/
|
|
53
|
+
export function pickDetailLevel(levels, p) {
|
|
54
|
+
const n = levels.length;
|
|
55
|
+
if (n === 0)
|
|
56
|
+
return 0;
|
|
57
|
+
const finest = levels[n - 1].detailLevel;
|
|
58
|
+
if (!(p.tolerancePx > 0) ||
|
|
59
|
+
!(p.distance > 0) ||
|
|
60
|
+
!(p.focalPx > 0) ||
|
|
61
|
+
!(p.worldScale > 0))
|
|
62
|
+
return finest;
|
|
63
|
+
for (let i = 0; i < n; i++) {
|
|
64
|
+
const l = levels[i];
|
|
65
|
+
if (projectedErrorPx(l.error, p.worldScale, p.distance, p.focalPx) <=
|
|
66
|
+
p.tolerancePx)
|
|
67
|
+
return l.detailLevel;
|
|
68
|
+
}
|
|
69
|
+
return finest;
|
|
70
|
+
}
|
package/dist/assets/ugm.d.ts
CHANGED
|
@@ -1,15 +1,33 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
/**
|
|
2
|
+
* A const object rather than a TS `enum`, and the reason is that the package's own test runner could
|
|
3
|
+
* not read this file otherwise.
|
|
4
|
+
*
|
|
5
|
+
* `node --experimental-strip-types` — which runs every test in `game/src` — refuses an `enum`: it is
|
|
6
|
+
* the one TypeScript construct that emits runtime code, so strip-only mode cannot erase it. That is
|
|
7
|
+
* why there has never been a container test in this package despite `.ugm` being the format
|
|
8
|
+
* everything else is cooked into. The shape below is erasable, so the format is now testable where it
|
|
9
|
+
* lives.
|
|
10
|
+
*
|
|
11
|
+
* Source-compatible for readers: `ChunkKind.VTX` still resolves, and `ChunkKind` still names the
|
|
12
|
+
* type. The one behaviour that changes is that a bare `number` no longer assigns to it without a
|
|
13
|
+
* cast, which is a numeric-enum looseness rather than something worth keeping.
|
|
14
|
+
*/
|
|
15
|
+
export declare const ChunkKind: {
|
|
16
|
+
readonly VTX: 1;
|
|
17
|
+
readonly IDX: 2;
|
|
18
|
+
readonly CLUSTER: 3;
|
|
19
|
+
readonly MIP: 4;
|
|
20
|
+
readonly MAT: 5;
|
|
21
|
+
readonly SKEL: 6;
|
|
22
|
+
readonly ANIM: 7;
|
|
23
|
+
readonly MORPH: 8;
|
|
24
|
+
readonly UV: 9;
|
|
25
|
+
readonly KTX2: 10;
|
|
26
|
+
/** The LOD SELECTION TABLE — see packLodChunk. One per container, `base: true`, so a coarse
|
|
27
|
+
* prefix can already answer "which rung should I draw from here?". */
|
|
28
|
+
readonly LOD: 11;
|
|
29
|
+
};
|
|
30
|
+
export type ChunkKind = (typeof ChunkKind)[keyof typeof ChunkKind];
|
|
13
31
|
export interface ChunkIn {
|
|
14
32
|
kind: ChunkKind;
|
|
15
33
|
detailLevel: number;
|
|
@@ -57,3 +75,41 @@ export interface UgmHeader {
|
|
|
57
75
|
export declare function readHeader(buf: Uint8Array): UgmHeader;
|
|
58
76
|
export declare function readToc(buf: Uint8Array, h: UgmHeader): TocEntry[];
|
|
59
77
|
export declare function readChunk(buf: Uint8Array, e: TocEntry): Uint8Array;
|
|
78
|
+
export declare const LOD_MAGIC = 1145849685;
|
|
79
|
+
export declare const LOD_CHUNK_VERSION = 1;
|
|
80
|
+
export interface LodLevel {
|
|
81
|
+
/** The `detailLevel` this row describes — matches the VTX/IDX chunks of the same rung. */
|
|
82
|
+
detailLevel: number;
|
|
83
|
+
/**
|
|
84
|
+
* Geometric deviation of this rung from the source, in MODEL units.
|
|
85
|
+
*
|
|
86
|
+
* Model units, not metres, and the distinction is the whole contract: the cooks normalise their
|
|
87
|
+
* meshes (the life kit to unit height) and the world scales each instance as it places it, so an
|
|
88
|
+
* instance's world error is this number times ITS OWN scale. Baking metres in here would make the
|
|
89
|
+
* table wrong for every instance not drawn at 1x.
|
|
90
|
+
*
|
|
91
|
+
* 0 for the source rung, and non-decreasing as the ladder coarsens.
|
|
92
|
+
*/
|
|
93
|
+
error: number;
|
|
94
|
+
}
|
|
95
|
+
/** Serialise a LOD selection table. Sorted by detailLevel so a reader can bisect it. */
|
|
96
|
+
export declare function packLodChunk(levels: LodLevel[]): Uint8Array;
|
|
97
|
+
/**
|
|
98
|
+
* Parse a LOD selection table, or return null when these bytes are not one.
|
|
99
|
+
*
|
|
100
|
+
* NULL RATHER THAN THROW, for the two cases that are normal rather than exceptional: a container
|
|
101
|
+
* cooked before the table existed has no LOD chunk at all, and a container being STREAMED advertises
|
|
102
|
+
* its whole TOC from byte 64 while the payload is still arriving — so a caller can hold a TOC entry
|
|
103
|
+
* whose bytes have not landed. Both mean "select some other way for now", not "this file is broken".
|
|
104
|
+
* A future version is treated the same way, so a new cook cannot crash an old client.
|
|
105
|
+
*/
|
|
106
|
+
export declare function readLodChunk(bytes: Uint8Array): LodLevel[] | null;
|
|
107
|
+
/**
|
|
108
|
+
* One model's LOD table, or null if it carries none / has not streamed that far yet.
|
|
109
|
+
*
|
|
110
|
+
* ⚠️ KEYED BY `targetId`, because a container is not always one model. `nature.ugm` packs ten, each
|
|
111
|
+
* with its own ladder under its own targetId, so a per-CONTAINER lookup would hand all ten the first
|
|
112
|
+
* model's errors — a bush selecting rungs by a tree's simplification error. Single-model containers
|
|
113
|
+
* cook everything at targetId 0 and get the default.
|
|
114
|
+
*/
|
|
115
|
+
export declare function lodTableOf(buf: Uint8Array, toc: TocEntry[], targetId?: number): LodLevel[] | null;
|
package/dist/assets/ugm.js
CHANGED
|
@@ -2,19 +2,35 @@
|
|
|
2
2
|
// Layout (little-endian): [64B header][TOC: chunkCount × 24B][chunk bytes …].
|
|
3
3
|
// Chunks are sorted base-first then ascending detailLevel, so a leading byte range
|
|
4
4
|
// (0 .. basePayloadLen) is a complete coarse-renderable asset.
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
5
|
+
/**
|
|
6
|
+
* A const object rather than a TS `enum`, and the reason is that the package's own test runner could
|
|
7
|
+
* not read this file otherwise.
|
|
8
|
+
*
|
|
9
|
+
* `node --experimental-strip-types` — which runs every test in `game/src` — refuses an `enum`: it is
|
|
10
|
+
* the one TypeScript construct that emits runtime code, so strip-only mode cannot erase it. That is
|
|
11
|
+
* why there has never been a container test in this package despite `.ugm` being the format
|
|
12
|
+
* everything else is cooked into. The shape below is erasable, so the format is now testable where it
|
|
13
|
+
* lives.
|
|
14
|
+
*
|
|
15
|
+
* Source-compatible for readers: `ChunkKind.VTX` still resolves, and `ChunkKind` still names the
|
|
16
|
+
* type. The one behaviour that changes is that a bare `number` no longer assigns to it without a
|
|
17
|
+
* cast, which is a numeric-enum looseness rather than something worth keeping.
|
|
18
|
+
*/
|
|
19
|
+
export const ChunkKind = {
|
|
20
|
+
VTX: 1,
|
|
21
|
+
IDX: 2,
|
|
22
|
+
CLUSTER: 3,
|
|
23
|
+
MIP: 4,
|
|
24
|
+
MAT: 5,
|
|
25
|
+
SKEL: 6,
|
|
26
|
+
ANIM: 7,
|
|
27
|
+
MORPH: 8,
|
|
28
|
+
UV: 9,
|
|
29
|
+
KTX2: 10,
|
|
30
|
+
/** The LOD SELECTION TABLE — see packLodChunk. One per container, `base: true`, so a coarse
|
|
31
|
+
* prefix can already answer "which rung should I draw from here?". */
|
|
32
|
+
LOD: 11,
|
|
33
|
+
};
|
|
18
34
|
export const MAGIC = 0x324d4755; // "UGM2" little-endian
|
|
19
35
|
export const HEADER_BYTES = 64;
|
|
20
36
|
export const TOC_ENTRY_BYTES = 24; // kind1 detail1 targetId2 offset4 length4 hash4 depMask4 pad4
|
|
@@ -108,6 +124,9 @@ export function readToc(buf, h) {
|
|
|
108
124
|
for (let i = 0; i < h.chunkCount; i++) {
|
|
109
125
|
const t = h.tocOffset + i * TOC_ENTRY_BYTES;
|
|
110
126
|
out.push({
|
|
127
|
+
// Cast: the byte on disk is whatever was written, including a kind this build has never heard
|
|
128
|
+
// of. Validating here would mean rejecting forward-compatible containers, and every consumer
|
|
129
|
+
// already filters by the kinds it wants — an unknown one is simply never matched.
|
|
111
130
|
kind: dv.getUint8(t),
|
|
112
131
|
detailLevel: dv.getUint8(t + 1),
|
|
113
132
|
targetId: dv.getUint16(t + 2, true),
|
|
@@ -122,3 +141,98 @@ export function readToc(buf, h) {
|
|
|
122
141
|
export function readChunk(buf, e) {
|
|
123
142
|
return buf.subarray(e.offset, e.offset + e.length);
|
|
124
143
|
}
|
|
144
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
145
|
+
// THE LOD SELECTION TABLE (ChunkKind.LOD)
|
|
146
|
+
//
|
|
147
|
+
// A container has always described WHAT its rungs are — one VTX/IDX pair per detailLevel — and never
|
|
148
|
+
// how far away each one stops being good enough. Every consumer therefore invented its own answer:
|
|
149
|
+
// a distance band, a triangle-count guess, or the terrain chunk's stride. Those disagree with each
|
|
150
|
+
// other, none of them knows the field of view or the resolution, and none of them survives a mesh
|
|
151
|
+
// being re-cooked coarser.
|
|
152
|
+
//
|
|
153
|
+
// What the cook knows and the renderer does not is the GEOMETRIC ERROR: how far a rung's surface
|
|
154
|
+
// deviates from the source, in model units. That single number turns LOD selection into arithmetic —
|
|
155
|
+
// project the error through the camera and compare it against a pixel budget — and it is the one
|
|
156
|
+
// quantity that cannot be recovered at runtime, because the source mesh is not shipped.
|
|
157
|
+
//
|
|
158
|
+
// ⚠️ A CHUNK RATHER THAN THE 20 FREE HEADER BYTES. The header would hold four f32 errors today and
|
|
159
|
+
// nothing beyond five rungs, and `detailLevel` is a u8: a format whose extension budget runs out at
|
|
160
|
+
// the sixth rung is not one worth spending a version number on. A chunk also versions itself, is
|
|
161
|
+
// content-hashed and deduplicated like every other chunk, and — the part that made the decision —
|
|
162
|
+
// is INVISIBLE to readers that do not know about it, because every consumer filters the TOC by kind.
|
|
163
|
+
// Old readers see one extra chunk they ignore; new readers see an old file as "no table" and fall
|
|
164
|
+
// back. Neither direction needs a format-version bump, so this ships without re-cooking the world.
|
|
165
|
+
//
|
|
166
|
+
// Layout (little-endian):
|
|
167
|
+
// [u32 magic][u16 version][u8 levelCount][u8 flags] then levelCount x [u8 detailLevel][3B pad][f32 error]
|
|
168
|
+
//
|
|
169
|
+
// Pairs, not a bare f32 array indexed by position: `detailLevel` is free to be sparse — a container
|
|
170
|
+
// that ships rungs 0 and 3 and nothing between is legal — and a positional array would silently
|
|
171
|
+
// misattribute every error in that case.
|
|
172
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
173
|
+
export const LOD_MAGIC = 0x444c4755; // "UGLD" little-endian
|
|
174
|
+
export const LOD_CHUNK_VERSION = 1;
|
|
175
|
+
const LOD_HEAD_BYTES = 8;
|
|
176
|
+
const LOD_LEVEL_BYTES = 8;
|
|
177
|
+
/** Serialise a LOD selection table. Sorted by detailLevel so a reader can bisect it. */
|
|
178
|
+
export function packLodChunk(levels) {
|
|
179
|
+
const sorted = [...levels].sort((a, b) => a.detailLevel - b.detailLevel);
|
|
180
|
+
if (sorted.length > 255)
|
|
181
|
+
throw new Error('.ugm LOD: levelCount exceeds the u8 field');
|
|
182
|
+
const out = new Uint8Array(LOD_HEAD_BYTES + sorted.length * LOD_LEVEL_BYTES);
|
|
183
|
+
const dv = new DataView(out.buffer);
|
|
184
|
+
dv.setUint32(0, LOD_MAGIC, true);
|
|
185
|
+
dv.setUint16(4, LOD_CHUNK_VERSION, true);
|
|
186
|
+
dv.setUint8(6, sorted.length);
|
|
187
|
+
dv.setUint8(7, 0); // flags reserved
|
|
188
|
+
for (let i = 0; i < sorted.length; i++) {
|
|
189
|
+
const o = LOD_HEAD_BYTES + i * LOD_LEVEL_BYTES;
|
|
190
|
+
dv.setUint8(o, sorted[i].detailLevel);
|
|
191
|
+
dv.setFloat32(o + 4, sorted[i].error, true);
|
|
192
|
+
}
|
|
193
|
+
return out;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Parse a LOD selection table, or return null when these bytes are not one.
|
|
197
|
+
*
|
|
198
|
+
* NULL RATHER THAN THROW, for the two cases that are normal rather than exceptional: a container
|
|
199
|
+
* cooked before the table existed has no LOD chunk at all, and a container being STREAMED advertises
|
|
200
|
+
* its whole TOC from byte 64 while the payload is still arriving — so a caller can hold a TOC entry
|
|
201
|
+
* whose bytes have not landed. Both mean "select some other way for now", not "this file is broken".
|
|
202
|
+
* A future version is treated the same way, so a new cook cannot crash an old client.
|
|
203
|
+
*/
|
|
204
|
+
export function readLodChunk(bytes) {
|
|
205
|
+
if (bytes.byteLength < LOD_HEAD_BYTES)
|
|
206
|
+
return null;
|
|
207
|
+
const dv = dvOf(bytes);
|
|
208
|
+
if (dv.getUint32(0, true) !== LOD_MAGIC)
|
|
209
|
+
return null;
|
|
210
|
+
if (dv.getUint16(4, true) !== LOD_CHUNK_VERSION)
|
|
211
|
+
return null;
|
|
212
|
+
const count = dv.getUint8(6);
|
|
213
|
+
if (bytes.byteLength < LOD_HEAD_BYTES + count * LOD_LEVEL_BYTES)
|
|
214
|
+
return null;
|
|
215
|
+
const out = [];
|
|
216
|
+
for (let i = 0; i < count; i++) {
|
|
217
|
+
const o = LOD_HEAD_BYTES + i * LOD_LEVEL_BYTES;
|
|
218
|
+
out.push({
|
|
219
|
+
detailLevel: dv.getUint8(o),
|
|
220
|
+
error: dv.getFloat32(o + 4, true),
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
return out;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* One model's LOD table, or null if it carries none / has not streamed that far yet.
|
|
227
|
+
*
|
|
228
|
+
* ⚠️ KEYED BY `targetId`, because a container is not always one model. `nature.ugm` packs ten, each
|
|
229
|
+
* with its own ladder under its own targetId, so a per-CONTAINER lookup would hand all ten the first
|
|
230
|
+
* model's errors — a bush selecting rungs by a tree's simplification error. Single-model containers
|
|
231
|
+
* cook everything at targetId 0 and get the default.
|
|
232
|
+
*/
|
|
233
|
+
export function lodTableOf(buf, toc, targetId = 0) {
|
|
234
|
+
const e = toc.find((t) => t.kind === ChunkKind.LOD && t.targetId === targetId);
|
|
235
|
+
if (!e || e.offset + e.length > buf.length)
|
|
236
|
+
return null;
|
|
237
|
+
return readLodChunk(readChunk(buf, e));
|
|
238
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ugly-game",
|
|
3
|
-
"version": "0.15.
|
|
3
|
+
"version": "0.15.26",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -15,6 +15,20 @@
|
|
|
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
|
+
"lint:wgsl": "node --experimental-strip-types tools/wgslGuard.mts && node --experimental-strip-types tools/wgslLint.mts",
|
|
24
|
+
"test": "node --experimental-strip-types --import ./tools/tsResolve.mjs --test src/*.test.mts",
|
|
25
|
+
"test:flight": "node --experimental-strip-types --import ./tools/tsResolve.mjs --test src/flight*.test.mts",
|
|
26
|
+
"test:flight-browser": "playwright test -c playwright.flight.config.ts",
|
|
27
|
+
"test:flight-perf": "playwright test -c playwright.flight.config.ts --grep 'CPU and retained-memory'",
|
|
28
|
+
"test:types": "tsc -p tsconfig.test.json",
|
|
29
|
+
"pretty": "prettier --write .",
|
|
30
|
+
"prepublishOnly": "pnpm run pretty && ugly-app lint && tsc -p tsconfig.json && pnpm run lint:wgsl && mkdir -p dist/assets/basis && cp src/assets/basis/basis_transcoder.js src/assets/basis/basis_transcoder.bin dist/assets/basis/"
|
|
31
|
+
},
|
|
18
32
|
"dependencies": {
|
|
19
33
|
"meshoptimizer": "^1.2.0"
|
|
20
34
|
},
|
|
@@ -30,18 +44,5 @@
|
|
|
30
44
|
"typescript": "^5.6.0",
|
|
31
45
|
"ugly-app": "^0.1.841",
|
|
32
46
|
"wesl": "0.7.28"
|
|
33
|
-
},
|
|
34
|
-
"scripts": {
|
|
35
|
-
"preinstall": "npx only-allow pnpm",
|
|
36
|
-
"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/",
|
|
37
|
-
"lint": "ugly-app lint",
|
|
38
|
-
"lint:fix": "ugly-app lint --fix",
|
|
39
|
-
"lint:wgsl": "node --experimental-strip-types tools/wgslGuard.mts && node --experimental-strip-types tools/wgslLint.mts",
|
|
40
|
-
"test": "node --experimental-strip-types --import ./tools/tsResolve.mjs --test src/*.test.mts",
|
|
41
|
-
"test:flight": "node --experimental-strip-types --import ./tools/tsResolve.mjs --test src/flight*.test.mts",
|
|
42
|
-
"test:flight-browser": "playwright test -c playwright.flight.config.ts",
|
|
43
|
-
"test:flight-perf": "playwright test -c playwright.flight.config.ts --grep 'CPU and retained-memory'",
|
|
44
|
-
"test:types": "tsc -p tsconfig.test.json",
|
|
45
|
-
"pretty": "prettier --write ."
|
|
46
47
|
}
|
|
47
|
-
}
|
|
48
|
+
}
|