ugly-game 0.15.23 → 0.15.25
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/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/dist/gpuShadow.d.ts +16 -0
- package/dist/gpuShadow.js +36 -3
- package/package.json +16 -15
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/dist/gpuShadow.d.ts
CHANGED
|
@@ -571,6 +571,8 @@ export declare class GpuShadowRenderer {
|
|
|
571
571
|
private lightCull;
|
|
572
572
|
private lightCullBG;
|
|
573
573
|
private statsBuf;
|
|
574
|
+
/** Current shadow-map edge length; see setShadowMapSize. */
|
|
575
|
+
private shadowSize;
|
|
574
576
|
/** Last frame's visible counts (main-pass camera cull, shadow-pass light cull) + total — for IR/telemetry. */
|
|
575
577
|
lastCull: {
|
|
576
578
|
total: number;
|
|
@@ -786,6 +788,20 @@ export declare class GpuShadowRenderer {
|
|
|
786
788
|
get cascadeCount(): number;
|
|
787
789
|
get shadowMapSampler(): GPUSampler;
|
|
788
790
|
get shadowMapSize(): number;
|
|
791
|
+
/**
|
|
792
|
+
* Resize the cascaded shadow map. Powers of two 512..4096; anything else is clamped to the nearest.
|
|
793
|
+
*
|
|
794
|
+
* ⚠️ THE FILL, NOT THE INSTANCES, IS WHAT THIS BUYS. Measured in all-mine-sky at N=256: dropping
|
|
795
|
+
* from three cascades to one made the light see 2.9x MORE instances and ran 14.2 ms FASTER, while
|
|
796
|
+
* cutting light-visible instances 56x at three cascades bought only 11.4 ms. The pass is dominated
|
|
797
|
+
* by clearing, rasterizing and storing its depth targets, so edge length is the lever with the
|
|
798
|
+
* squared term in it — and unlike dropping cascades it keeps the cascade STRUCTURE, which is what
|
|
799
|
+
* carries near-field shadow quality.
|
|
800
|
+
*
|
|
801
|
+
* ⚠️ REALLOCATES THE TEXTURE AND EVERY BIND GROUP THAT SAMPLES IT. Cheap once, ruinous per frame —
|
|
802
|
+
* this is a settings-change call, not something to drive from a slider's every tick.
|
|
803
|
+
*/
|
|
804
|
+
setShadowMapSize(px: number): void;
|
|
789
805
|
/** camVP/lightVP: column-major Float32Array(16). sunDir: travel direction. Pass `occlusion` (the depth target +
|
|
790
806
|
* its size) to enable Hi-Z occlusion culling on the camera pass — the renderer rebuilds a coarse Hi-Z from that
|
|
791
807
|
* depth each frame and the NEXT frame's cull uses it (hole-free: the first frame + any resize skip occlusion). */
|
package/dist/gpuShadow.js
CHANGED
|
@@ -14,6 +14,12 @@
|
|
|
14
14
|
import { frustumPlanes } from './mat4';
|
|
15
15
|
import { GI_WGSL, PROBE_STRIDE } from './giProbes';
|
|
16
16
|
const SHADOW_SIZE = 2048;
|
|
17
|
+
/**
|
|
18
|
+
* Shadow-map edge lengths a caller may ask for. Powers of two only, and bounded at both ends: below
|
|
19
|
+
* 512 a cascade cannot resolve a block at any useful range, and above 4096 the allocation is larger
|
|
20
|
+
* than most of the frame's other targets combined.
|
|
21
|
+
*/
|
|
22
|
+
const SHADOW_SIZES = [512, 1024, 1536, 2048, 3072, 4096];
|
|
17
23
|
// CASCADED SHADOW MAPS. One ortho map has to choose between resolution and reach: AMS ran a single
|
|
18
24
|
// 90-block box against a 280-block draw distance, so everything past it was simply unshadowed and
|
|
19
25
|
// the far terrain read flat. Cascades split the range into nested boxes — tight and crisp near the
|
|
@@ -2781,6 +2787,8 @@ export class GpuShadowRenderer {
|
|
|
2781
2787
|
lightCull = [];
|
|
2782
2788
|
lightCullBG = [];
|
|
2783
2789
|
statsBuf; // readback: [camVisible, lightVisible] for the IR/tests
|
|
2790
|
+
/** Current shadow-map edge length; see setShadowMapSize. */
|
|
2791
|
+
shadowSize = SHADOW_SIZE;
|
|
2784
2792
|
/** Last frame's visible counts (main-pass camera cull, shadow-pass light cull) + total — for IR/telemetry. */
|
|
2785
2793
|
lastCull = { total: 0, camVisible: 0, lightVisible: 0 };
|
|
2786
2794
|
// Hi-Z OCCLUSION: a coarse max-depth buffer rebuilt from each frame's depth, sampled by next frame's camera cull.
|
|
@@ -3223,7 +3231,7 @@ export class GpuShadowRenderer {
|
|
|
3223
3231
|
b.destroy();
|
|
3224
3232
|
this.cascades = n;
|
|
3225
3233
|
this.shadowTex = this.device.createTexture({
|
|
3226
|
-
size: [
|
|
3234
|
+
size: [this.shadowSize, this.shadowSize, n],
|
|
3227
3235
|
format: 'depth32float',
|
|
3228
3236
|
dimension: '2d',
|
|
3229
3237
|
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING,
|
|
@@ -3684,7 +3692,32 @@ export class GpuShadowRenderer {
|
|
|
3684
3692
|
return this.shadowSampler;
|
|
3685
3693
|
}
|
|
3686
3694
|
get shadowMapSize() {
|
|
3687
|
-
return
|
|
3695
|
+
return this.shadowSize;
|
|
3696
|
+
}
|
|
3697
|
+
/**
|
|
3698
|
+
* Resize the cascaded shadow map. Powers of two 512..4096; anything else is clamped to the nearest.
|
|
3699
|
+
*
|
|
3700
|
+
* ⚠️ THE FILL, NOT THE INSTANCES, IS WHAT THIS BUYS. Measured in all-mine-sky at N=256: dropping
|
|
3701
|
+
* from three cascades to one made the light see 2.9x MORE instances and ran 14.2 ms FASTER, while
|
|
3702
|
+
* cutting light-visible instances 56x at three cascades bought only 11.4 ms. The pass is dominated
|
|
3703
|
+
* by clearing, rasterizing and storing its depth targets, so edge length is the lever with the
|
|
3704
|
+
* squared term in it — and unlike dropping cascades it keeps the cascade STRUCTURE, which is what
|
|
3705
|
+
* carries near-field shadow quality.
|
|
3706
|
+
*
|
|
3707
|
+
* ⚠️ REALLOCATES THE TEXTURE AND EVERY BIND GROUP THAT SAMPLES IT. Cheap once, ruinous per frame —
|
|
3708
|
+
* this is a settings-change call, not something to drive from a slider's every tick.
|
|
3709
|
+
*/
|
|
3710
|
+
setShadowMapSize(px) {
|
|
3711
|
+
const want = SHADOW_SIZES.reduce((best, s) => Math.abs(s - px) < Math.abs(best - px) ? s : best);
|
|
3712
|
+
if (want === this.shadowSize)
|
|
3713
|
+
return;
|
|
3714
|
+
this.shadowSize = want;
|
|
3715
|
+
// Force the reallocation: allocCascades is the only thing that sizes the texture, and
|
|
3716
|
+
// ensureCascades short-circuits when the COUNT is unchanged, which it is here.
|
|
3717
|
+
const n = Math.max(1, this.cascades);
|
|
3718
|
+
this.allocCascades(n);
|
|
3719
|
+
if (this.sceneReady)
|
|
3720
|
+
this.makeBindGroups();
|
|
3688
3721
|
}
|
|
3689
3722
|
/** camVP/lightVP: column-major Float32Array(16). sunDir: travel direction. Pass `occlusion` (the depth target +
|
|
3690
3723
|
* its size) to enable Hi-Z occlusion culling on the camera pass — the renderer rebuilds a coarse Hi-Z from that
|
|
@@ -3715,7 +3748,7 @@ export class GpuShadowRenderer {
|
|
|
3715
3748
|
this.cam[S + 4] = sunCol[0];
|
|
3716
3749
|
this.cam[S + 5] = sunCol[1];
|
|
3717
3750
|
this.cam[S + 6] = sunCol[2];
|
|
3718
|
-
this.cam[S + 7] =
|
|
3751
|
+
this.cam[S + 7] = this.shadowSize; // sunCol.w — the PCF texel step reads this, so it MUST follow a resize
|
|
3719
3752
|
this.cam[S + 8] = tint[0];
|
|
3720
3753
|
this.cam[S + 9] = tint[1];
|
|
3721
3754
|
this.cam[S + 10] = tint[2];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ugly-game",
|
|
3
|
-
"version": "0.15.
|
|
3
|
+
"version": "0.15.25",
|
|
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
|
+
}
|