@threenative/core 0.3.12 → 0.3.13
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/README.md +1 -1
- package/capabilities.json +746 -32
- package/dist/{assets-YUbXa3-Y.d.ts → assets-CfMKnOsK.d.ts} +78 -1
- package/dist/{canvas-layer-CWuFRT7G.d.ts → canvas-layer-G7ttVy7y.d.ts} +1 -1
- package/dist/{chunk-7UUT3TY5.js → chunk-4SRAQFTU.js} +1 -1
- package/dist/{chunk-GMHATRPW.js → chunk-DIPIJICH.js} +1590 -375
- package/dist/{chunk-OKXUCASC.js → chunk-EEABDLUC.js} +3 -4
- package/dist/{chunk-5O73CDMR.js → chunk-EFWZWLPY.js} +62 -16
- package/dist/{chunk-N5LRP4BR.js → chunk-GYUZJQ7U.js} +63 -3
- package/dist/{chunk-VAZM36TV.js → chunk-H4J6IAJE.js} +17 -10
- package/dist/{chunk-2Z44XSI2.js → chunk-SV4QVUTQ.js} +3 -7
- package/dist/{chunk-DBELTTTM.js → chunk-U4YHUKKL.js} +1 -1
- package/dist/chunk-VP43GIWW.js +18 -0
- package/dist/{chunk-6KLNISRJ.js → chunk-XDTZ4QMO.js} +11 -4
- package/dist/default-tone-mapping-GgKPTiBP.d.ts +4 -0
- package/dist/{game-D7pAzZUY.d.ts → game-k8K3hZai.d.ts} +72 -8
- package/dist/hot.d.ts +5 -4
- package/dist/hot.js +5 -8
- package/dist/index.d.ts +289 -1034
- package/dist/index.js +3096 -1305
- package/dist/{model-lod-B6BMqS1r.d.ts → model-lod-DPTzeCcD.d.ts} +4 -2
- package/dist/playtest.d.ts +5 -4
- package/dist/playtest.js +6 -5
- package/dist/quality-BYqKGAwo.d.ts +868 -0
- package/dist/react.d.ts +3 -2
- package/dist/render-environment.d.ts +170 -0
- package/dist/render-environment.js +352 -0
- package/dist/{renderer-DvFVoAgj.d.ts → renderer-BtOSseYf.d.ts} +29 -31
- package/dist/spells/index.js +1 -1
- package/dist/ui-layer.js +3 -2
- package/dist/worker.js +12 -5
- package/dist/world.d.ts +269 -15
- package/dist/world.js +1372 -628
- package/mcp/animation-server.mjs +1 -1
- package/mcp/engine-server.mjs +105 -7
- package/mcp/servers.mjs +4 -4
- package/package.json +10 -6
- package/src/animation-batch.ts +193 -0
- package/src/animation-layers.ts +7 -3
- package/src/animation.ts +209 -29
- package/src/asset-cache.ts +3 -3
- package/src/asset-memo.ts +202 -0
- package/src/assets.ts +436 -131
- package/src/auto-batch.ts +190 -44
- package/src/compute-driven.ts +1 -1
- package/src/config.ts +8 -0
- package/src/debug.ts +103 -22
- package/src/dev-global.ts +54 -0
- package/src/engine-hooks.ts +37 -0
- package/src/frame-budget.ts +19 -3
- package/src/game-boot-phase.ts +269 -0
- package/src/game-dev-tools.ts +7 -14
- package/src/game-frame-loop.ts +45 -82
- package/src/game-startup.ts +145 -70
- package/src/game-ui.ts +84 -0
- package/src/game.ts +168 -552
- package/src/gamepad-mappings.ts +269 -0
- package/src/generated/console-markers.ts +13 -0
- package/src/geometry-capture.ts +28 -4
- package/src/gpu-readings.ts +98 -0
- package/src/gpu-scene-bvh.ts +149 -36
- package/src/hot.ts +4 -7
- package/src/index.ts +115 -8
- package/src/input.ts +237 -25
- package/src/instanced-batch-lod.ts +3 -8
- package/src/interpolation.ts +171 -40
- package/src/look/ao-admission.ts +4 -3
- package/src/look/auto-exposure.ts +5 -16
- package/src/look/default-tone-mapping.ts +41 -5
- package/src/look/local-exposure.ts +2 -7
- package/src/look/quad-pass.ts +21 -0
- package/src/look/quality.ts +4 -0
- package/src/look/unreal-bloom.ts +4 -13
- package/src/look/world-environment.ts +58 -115
- package/src/merge-parts.ts +20 -24
- package/src/model-lod.ts +97 -14
- package/src/particle-significance.ts +12 -0
- package/src/particles.ts +19 -10
- package/src/platform.ts +23 -4
- package/src/profiling/FrameStages.ts +5 -6
- package/src/profiling/Spans.ts +3 -17
- package/src/profiling/debug-telemetry.ts +2 -6
- package/src/profiling/render-list-validate.ts +6 -15
- package/src/projection-marker.ts +2 -1
- package/src/quiet-markers.ts +3 -10
- package/src/render/cpu-bvh.ts +100 -0
- package/src/render/depth-pyramid.ts +6 -14
- package/src/render/environment/environmentContribution.ts +202 -0
- package/src/render/environment/environmentRadiance.ts +44 -0
- package/src/render/environment/environmentSampleCache.ts +56 -0
- package/src/render/environment/environmentSampling.ts +201 -0
- package/src/render/environment/environmentSnapshot.ts +71 -0
- package/src/render/environment/gate.ts +57 -0
- package/src/render/mesh-pool.ts +3 -19
- package/src/render/probe-trace.ts +872 -0
- package/src/render/probe-volume.ts +780 -34
- package/src/render/radiance-cache.ts +17 -0
- package/src/render/virtual-shadow-partial.ts +13 -0
- package/src/render/virtual-shadow.ts +18 -16
- package/src/render/world-impostor-surface.ts +109 -6
- package/src/render/world-impostor.ts +5 -2
- package/src/render-camera-cull.ts +98 -3
- package/src/render-environment.ts +41 -0
- package/src/renderer-config.ts +36 -0
- package/src/renderer-facts.ts +37 -0
- package/src/renderer.ts +52 -28
- package/src/resolution-scaler.ts +3 -2
- package/src/scene-entry-inspectors.ts +61 -0
- package/src/snow-field.ts +1 -0
- package/src/startup-offscreen-bytes.ts +78 -0
- package/src/static-transform.ts +25 -19
- package/src/streamed-textures.ts +112 -0
- package/src/streaming.ts +68 -4
- package/src/texture-streaming.ts +331 -0
- package/src/version.ts +1 -1
- package/src/worker.ts +12 -2
- package/src/world-bundle-safety.ts +55 -10
- package/src/world-cell-batches.ts +162 -200
- package/src/world-cell-chunks.ts +3 -7
- package/src/world-cell-impostors.ts +20 -8
- package/src/world-cell-options.ts +2 -14
- package/src/world-cell-placements.ts +194 -90
- package/src/world-cell-prewarm.ts +39 -4
- package/src/world-cell-records.ts +401 -0
- package/src/world-cell-render-bundles.ts +2 -2
- package/src/world-cell-render-gpu-dress.ts +13 -17
- package/src/world-cell-render-main-cull.ts +73 -17
- package/src/world-cell-render-util.ts +17 -4
- package/src/world-cell-render.ts +13 -5
- package/src/world-cell-residency.ts +15 -6
- package/src/world-cell-stale.ts +238 -0
- package/src/world-cells.ts +22 -5
- package/src/world-gpu-flags.ts +18 -50
- package/src/world-gpu-reference.ts +22 -8
- package/src/world-gpu-scene.ts +44 -24
- package/src/world-gpu-shadow.ts +21 -8
- package/src/world-gpu-types.ts +41 -13
- package/src/world-load-limiter.ts +5 -0
- package/src/world-model-sections.ts +80 -0
- package/src/world-river-ribbon.ts +282 -0
- package/src/world-root-frame.ts +39 -0
- package/src/world-shore-field.ts +91 -0
- package/src/world-static-pose.ts +163 -0
- package/src/world-surface-sampler.ts +48 -0
- package/src/world-terrain-splat.ts +10 -18
- package/src/world-tile-budget.ts +4 -11
- package/src/world-tiles.ts +79 -12
- package/src/world-validate.ts +6 -14
- package/src/world-warm.ts +51 -0
- package/src/world.ts +13 -0
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* KTX 2.0 mip residency math, pure and container-free: how many mips a screen size can justify
|
|
3
|
+
* (Unreal's `StreamingTexture.cpp` rule) and which byte ranges in the file still have to arrive.
|
|
4
|
+
*
|
|
5
|
+
* The container parser is the KTX 2.0 header (Khronos spec, "KTX 2.0 File Format"):
|
|
6
|
+
*
|
|
7
|
+
* 0 identifier 12 bytes «KTX 20» \r\n\x1A\n
|
|
8
|
+
* 12 vkFormat u32 typeSize u32 pixelWidth u32
|
|
9
|
+
* 20 pixelHeight u32 pixelDepth u32 layerCount u32
|
|
10
|
+
* 32 faceCount u32 levelCount u32 supercompressionScheme u32
|
|
11
|
+
* 48 dfdByteOffset u32, dfdByteLength u32, kvdByteOffset u32, kvdByteLength u32,
|
|
12
|
+
* sgdByteOffset u64, sgdByteLength u64
|
|
13
|
+
* 80 level index: levelCount entries of { byteOffset u64, byteLength u64, uncompressedByteLength u64 }
|
|
14
|
+
*
|
|
15
|
+
* Level 0 is the largest mip; the last entry is the 1x1. A file that says `levelCount` 0 has one.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const KTX2_IDENTIFIER: readonly number[] = [
|
|
19
|
+
0xab, 0x4b, 0x54, 0x58, 0x20, 0x32, 0x30, 0xbb, 0x0d, 0x0a, 0x1a, 0x0a,
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
const KTX2_INDEX_BYTES = 80;
|
|
23
|
+
const KTX2_LEVEL_ENTRY_BYTES = 24;
|
|
24
|
+
const MAX_SAFE_U64 = BigInt(Number.MAX_SAFE_INTEGER);
|
|
25
|
+
|
|
26
|
+
/** One level index entry: where a mip lives in the file and how big it is once decompressed. */
|
|
27
|
+
export interface IKtx2Level {
|
|
28
|
+
readonly byteOffset: number;
|
|
29
|
+
readonly byteLength: number;
|
|
30
|
+
readonly uncompressedByteLength: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The header fields streaming needs, plus the level index. `levelCount` is effective: 0 reads as 1. */
|
|
34
|
+
export interface IKtx2LevelIndex {
|
|
35
|
+
readonly vkFormat: number;
|
|
36
|
+
readonly typeSize: number;
|
|
37
|
+
readonly pixelWidth: number;
|
|
38
|
+
readonly pixelHeight: number;
|
|
39
|
+
readonly pixelDepth: number;
|
|
40
|
+
readonly layerCount: number;
|
|
41
|
+
readonly faceCount: number;
|
|
42
|
+
readonly levelCount: number;
|
|
43
|
+
readonly supercompressionScheme: number;
|
|
44
|
+
/** BasisLZ (scheme 1) needs this global block to transcode any level, so a range stream fetches it with the header. */
|
|
45
|
+
readonly sgdByteOffset: number;
|
|
46
|
+
readonly sgdByteLength: number;
|
|
47
|
+
readonly levels: readonly IKtx2Level[];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function headerFailure(reason: string): Error {
|
|
51
|
+
return new Error(`TN_KTX2_HEADER: ${reason}`);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The level index entry count a file declares: 0 means one mip, never zero. */
|
|
55
|
+
function levelEntryCount(levelCount: number): number {
|
|
56
|
+
return Number.isFinite(levelCount) ? Math.max(1, Math.trunc(levelCount)) : 1;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Byte length of the header plus level index for a file declaring `levelCount` mips. */
|
|
60
|
+
export function ktx2HeaderBytes(levelCount: number): number {
|
|
61
|
+
return KTX2_INDEX_BYTES + KTX2_LEVEL_ENTRY_BYTES * levelEntryCount(levelCount);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function readU64(view: DataView, offset: number, field: string): number {
|
|
65
|
+
const value = view.getBigUint64(offset, true);
|
|
66
|
+
if (value > MAX_SAFE_U64) {
|
|
67
|
+
throw headerFailure(`${field} is ${value.toString()}, above Number.MAX_SAFE_INTEGER`);
|
|
68
|
+
}
|
|
69
|
+
return Number(value);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Parses a KTX2 header and level index. Throws `TN_KTX2_HEADER: …` on anything malformed. */
|
|
73
|
+
export function parseKtx2LevelIndex(bytes: Uint8Array): IKtx2LevelIndex {
|
|
74
|
+
if (bytes.byteLength < KTX2_IDENTIFIER.length) {
|
|
75
|
+
throw headerFailure(
|
|
76
|
+
`buffer is ${String(bytes.byteLength)} bytes, too short for the identifier: ${String(KTX2_IDENTIFIER.length)}`,
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
for (let i = 0; i < KTX2_IDENTIFIER.length; i += 1) {
|
|
80
|
+
if (bytes[i] !== KTX2_IDENTIFIER[i]) {
|
|
81
|
+
throw headerFailure(`byte ${String(i)} is not the KTX 2.0 identifier`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (bytes.byteLength < KTX2_INDEX_BYTES) {
|
|
85
|
+
throw headerFailure(
|
|
86
|
+
`buffer is ${String(bytes.byteLength)} bytes, too short for the 80-byte header and index`,
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
91
|
+
const levelCount = levelEntryCount(view.getUint32(40, true));
|
|
92
|
+
const required = ktx2HeaderBytes(levelCount);
|
|
93
|
+
if (bytes.byteLength < required) {
|
|
94
|
+
throw headerFailure(
|
|
95
|
+
`buffer is ${String(bytes.byteLength)} bytes, ${String(levelCount)} level(s) need ${String(required)}`,
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const levels: IKtx2Level[] = [];
|
|
100
|
+
for (let level = 0; level < levelCount; level += 1) {
|
|
101
|
+
const at = KTX2_INDEX_BYTES + level * KTX2_LEVEL_ENTRY_BYTES;
|
|
102
|
+
levels.push({
|
|
103
|
+
byteOffset: readU64(view, at, `level ${String(level)} byteOffset`),
|
|
104
|
+
byteLength: readU64(view, at + 8, `level ${String(level)} byteLength`),
|
|
105
|
+
uncompressedByteLength: readU64(
|
|
106
|
+
view,
|
|
107
|
+
at + 16,
|
|
108
|
+
`level ${String(level)} uncompressedByteLength`,
|
|
109
|
+
),
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return {
|
|
114
|
+
vkFormat: view.getUint32(12, true),
|
|
115
|
+
typeSize: view.getUint32(16, true),
|
|
116
|
+
pixelWidth: view.getUint32(20, true),
|
|
117
|
+
pixelHeight: view.getUint32(24, true),
|
|
118
|
+
pixelDepth: view.getUint32(28, true),
|
|
119
|
+
layerCount: view.getUint32(32, true),
|
|
120
|
+
faceCount: view.getUint32(36, true),
|
|
121
|
+
levelCount,
|
|
122
|
+
supercompressionScheme: view.getUint32(44, true),
|
|
123
|
+
sgdByteOffset: readU64(view, 64, "sgdByteOffset"),
|
|
124
|
+
sgdByteLength: readU64(view, 72, "sgdByteLength"),
|
|
125
|
+
levels,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** The first read of a streamed file: the header, level index and metadata, and usually the tail too. */
|
|
130
|
+
const KTX2_PROBE_BYTES = 64 * 1024;
|
|
131
|
+
/** Mips at or below this side stay resident from the first read (Unreal keeps a mip tail, `TextureDerivedData.cpp:2658`). */
|
|
132
|
+
const KTX2_TAIL_MAX_PX = 64;
|
|
133
|
+
/** Level data alignment in a rebuilt file: a multiple of every block size (8, 16) and lcm(texel, 4) (4, 8, 12, 16). */
|
|
134
|
+
const KTX2_LEVEL_ALIGN = 48;
|
|
135
|
+
const BASISLZ = 1;
|
|
136
|
+
const BASISLZ_SGD_HEADER_BYTES = 20;
|
|
137
|
+
const BASISLZ_IMAGE_DESC_BYTES = 20;
|
|
138
|
+
|
|
139
|
+
/** A KTX2 file read by `Range`, coarse mips first. */
|
|
140
|
+
export interface IKtx2Stream {
|
|
141
|
+
readonly index: IKtx2LevelIndex;
|
|
142
|
+
/** Mip levels on hand, counted from the smallest; `index.levelCount` once the file is whole. */
|
|
143
|
+
readonly residentLevels: number;
|
|
144
|
+
/** False when the host ignored `Range` and sent the whole file, which then stays whole. */
|
|
145
|
+
readonly ranged: boolean;
|
|
146
|
+
/** Bytes read from the host so far. */
|
|
147
|
+
readonly bytesRead: number;
|
|
148
|
+
/** A complete KTX2 file of the resident levels, which any KTX2 decoder takes as it is. */
|
|
149
|
+
file(): Uint8Array;
|
|
150
|
+
/** Reads levels until `levels` are resident. The new file, or undefined when nothing was missing. */
|
|
151
|
+
want(levels: number): Promise<Uint8Array | undefined>;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** The fetch a stream reads through; `globalThis.fetch` by default. */
|
|
155
|
+
export type RangeFetch = (url: string, init: RequestInit) => Promise<Response>;
|
|
156
|
+
|
|
157
|
+
function alignUp(value: number, alignment: number): number {
|
|
158
|
+
return Math.ceil(value / alignment) * alignment;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** The BasisLZ global data for the levels from `first` on: one image descriptor per kept image. */
|
|
162
|
+
function trimBasisLzGlobalData(sgd: Uint8Array, index: IKtx2LevelIndex, first: number): Uint8Array {
|
|
163
|
+
const images = (level: number) =>
|
|
164
|
+
Math.max(1, index.layerCount) * index.faceCount * Math.max(1, index.pixelDepth >> level);
|
|
165
|
+
let dropped = 0;
|
|
166
|
+
for (let level = 0; level < first; level += 1) dropped += images(level);
|
|
167
|
+
let total = dropped;
|
|
168
|
+
for (let level = first; level < index.levelCount; level += 1) total += images(level);
|
|
169
|
+
const descs = BASISLZ_SGD_HEADER_BYTES;
|
|
170
|
+
const rest = descs + total * BASISLZ_IMAGE_DESC_BYTES;
|
|
171
|
+
if (sgd.byteLength < rest)
|
|
172
|
+
throw headerFailure(
|
|
173
|
+
`BasisLZ global data is ${String(sgd.byteLength)} bytes, ${String(rest)} needed`,
|
|
174
|
+
);
|
|
175
|
+
const out = new Uint8Array(sgd.byteLength - dropped * BASISLZ_IMAGE_DESC_BYTES);
|
|
176
|
+
out.set(sgd.subarray(0, descs));
|
|
177
|
+
out.set(sgd.subarray(descs + dropped * BASISLZ_IMAGE_DESC_BYTES), descs);
|
|
178
|
+
return out;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* A KTX2 file of the levels from `first` (the finest kept) to the 1x1, from the bytes of a file read
|
|
183
|
+
* up to them: the header names the smaller base size, the level index the moved data, and the
|
|
184
|
+
* descriptor, key/value and BasisLZ global data come along.
|
|
185
|
+
*/
|
|
186
|
+
function trimmedKtx2(src: Uint8Array, index: IKtx2LevelIndex, first: number): Uint8Array {
|
|
187
|
+
const view = new DataView(src.buffer, src.byteOffset, src.byteLength);
|
|
188
|
+
// A section past the bytes on hand is a broken file or a short read, never a shorter section.
|
|
189
|
+
const slice = (offset: number, length: number) => {
|
|
190
|
+
if (offset + length > src.byteLength)
|
|
191
|
+
throw new Error(
|
|
192
|
+
`TN_KTX2_HEADER: a section at ${String(offset)}+${String(length)} lies past the ${String(src.byteLength)} bytes read`,
|
|
193
|
+
);
|
|
194
|
+
return src.subarray(offset, offset + length);
|
|
195
|
+
};
|
|
196
|
+
const dfd = slice(view.getUint32(48, true), view.getUint32(52, true));
|
|
197
|
+
const kvd = slice(view.getUint32(56, true), view.getUint32(60, true));
|
|
198
|
+
let sgd = slice(index.sgdByteOffset, index.sgdByteLength);
|
|
199
|
+
if (index.supercompressionScheme === BASISLZ && sgd.byteLength > 0)
|
|
200
|
+
sgd = trimBasisLzGlobalData(sgd, index, first);
|
|
201
|
+
const kept = index.levels.slice(first);
|
|
202
|
+
const dfdAt = ktx2HeaderBytes(kept.length);
|
|
203
|
+
const kvdAt = dfdAt + dfd.byteLength;
|
|
204
|
+
const sgdAt = alignUp(kvdAt + kvd.byteLength, 8);
|
|
205
|
+
// Smallest level first, as the KTX2 spec stores them.
|
|
206
|
+
const placed: number[] = [];
|
|
207
|
+
let end = sgdAt + sgd.byteLength;
|
|
208
|
+
for (let i = kept.length - 1; i >= 0; i -= 1) {
|
|
209
|
+
const at = alignUp(end, KTX2_LEVEL_ALIGN);
|
|
210
|
+
placed[i] = at;
|
|
211
|
+
end = at + (kept[i]?.byteLength ?? 0);
|
|
212
|
+
}
|
|
213
|
+
const out = new Uint8Array(end);
|
|
214
|
+
const write = new DataView(out.buffer);
|
|
215
|
+
out.set(src.subarray(0, 48));
|
|
216
|
+
write.setUint32(20, Math.max(1, index.pixelWidth >> first), true);
|
|
217
|
+
if (index.pixelHeight > 0) write.setUint32(24, Math.max(1, index.pixelHeight >> first), true);
|
|
218
|
+
if (index.pixelDepth > 0) write.setUint32(28, Math.max(1, index.pixelDepth >> first), true);
|
|
219
|
+
write.setUint32(40, kept.length, true);
|
|
220
|
+
write.setUint32(48, dfd.byteLength > 0 ? dfdAt : 0, true);
|
|
221
|
+
write.setUint32(52, dfd.byteLength, true);
|
|
222
|
+
write.setUint32(56, kvd.byteLength > 0 ? kvdAt : 0, true);
|
|
223
|
+
write.setUint32(60, kvd.byteLength, true);
|
|
224
|
+
write.setBigUint64(64, BigInt(sgd.byteLength > 0 ? sgdAt : 0), true);
|
|
225
|
+
write.setBigUint64(72, BigInt(sgd.byteLength), true);
|
|
226
|
+
out.set(dfd, dfdAt);
|
|
227
|
+
out.set(kvd, kvdAt);
|
|
228
|
+
out.set(sgd, sgdAt);
|
|
229
|
+
kept.forEach((level, i) => {
|
|
230
|
+
const at = KTX2_INDEX_BYTES + i * KTX2_LEVEL_ENTRY_BYTES;
|
|
231
|
+
write.setBigUint64(at, BigInt(placed[i] ?? 0), true);
|
|
232
|
+
write.setBigUint64(at + 8, BigInt(level.byteLength), true);
|
|
233
|
+
write.setBigUint64(at + 16, BigInt(level.uncompressedByteLength), true);
|
|
234
|
+
out.set(slice(level.byteOffset, level.byteLength), placed[i]);
|
|
235
|
+
});
|
|
236
|
+
return out;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Opens a KTX2 file for mip streaming: one `Range` read takes the header, metadata and the mip tail
|
|
241
|
+
* (levels of 64 px or less, at least the 1x1), and `want` reads finer levels later, coarse first. The
|
|
242
|
+
* KTX2 spec stores levels smallest first, so the bytes on hand are always one prefix of the file. A
|
|
243
|
+
* host that answers 200 sent the whole file, which is kept whole: nothing is fetched twice.
|
|
244
|
+
*/
|
|
245
|
+
export async function openKtx2Stream(
|
|
246
|
+
url: string,
|
|
247
|
+
fetchRange: RangeFetch = (target, init) => fetch(target, init),
|
|
248
|
+
): Promise<IKtx2Stream> {
|
|
249
|
+
let have = new Uint8Array(0);
|
|
250
|
+
let ranged = true;
|
|
251
|
+
let bytesRead = 0;
|
|
252
|
+
const readTo = async (end: number): Promise<void> => {
|
|
253
|
+
if (!ranged || end <= have.byteLength) return;
|
|
254
|
+
const start = have.byteLength;
|
|
255
|
+
const response = await fetchRange(url, {
|
|
256
|
+
headers: { range: `bytes=${String(start)}-${String(end - 1)}` },
|
|
257
|
+
});
|
|
258
|
+
if (!response.ok)
|
|
259
|
+
throw new Error(`TN_KTX2_FETCH: '${url}' answered ${String(response.status)}`);
|
|
260
|
+
const body = new Uint8Array(await response.arrayBuffer());
|
|
261
|
+
bytesRead += body.byteLength;
|
|
262
|
+
if (response.status !== 206) {
|
|
263
|
+
have = body;
|
|
264
|
+
ranged = false;
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
const from = /^bytes (\d+)-/u.exec(response.headers.get("content-range") ?? "")?.[1];
|
|
268
|
+
if (from !== undefined && Number(from) !== start)
|
|
269
|
+
throw new Error(
|
|
270
|
+
`TN_KTX2_FETCH: '${url}' answered bytes from ${from}, ${String(start)} asked`,
|
|
271
|
+
);
|
|
272
|
+
const next = new Uint8Array(start + body.byteLength);
|
|
273
|
+
next.set(have);
|
|
274
|
+
next.set(body, start);
|
|
275
|
+
have = next;
|
|
276
|
+
};
|
|
277
|
+
await readTo(KTX2_PROBE_BYTES);
|
|
278
|
+
const index = parseKtx2LevelIndex(have);
|
|
279
|
+
const { levels } = index;
|
|
280
|
+
const last = levels.length - 1;
|
|
281
|
+
const levelEnd = (level: number) =>
|
|
282
|
+
(levels[level]?.byteOffset ?? 0) + (levels[level]?.byteLength ?? 0);
|
|
283
|
+
// A writer that broke the smallest-first order has no coarse prefix: read the file whole.
|
|
284
|
+
const ordered = levels.every(
|
|
285
|
+
(level, i) => i === last || level.byteOffset > (levels[i + 1]?.byteOffset ?? 0),
|
|
286
|
+
);
|
|
287
|
+
if (!ordered) await readTo(Math.max(...levels.map((_, i) => levelEnd(i))));
|
|
288
|
+
let tail = 1;
|
|
289
|
+
while (
|
|
290
|
+
tail < levels.length &&
|
|
291
|
+
Math.max(index.pixelWidth, index.pixelHeight) >> (levels.length - 1 - tail) <= KTX2_TAIL_MAX_PX
|
|
292
|
+
)
|
|
293
|
+
tail += 1;
|
|
294
|
+
await readTo(levelEnd(levels.length - tail));
|
|
295
|
+
const resident = (): number => {
|
|
296
|
+
if (!ranged) return levels.length;
|
|
297
|
+
let count = 0;
|
|
298
|
+
while (count < levels.length && levelEnd(last - count) <= have.byteLength) count += 1;
|
|
299
|
+
return count;
|
|
300
|
+
};
|
|
301
|
+
const file = (): Uint8Array => {
|
|
302
|
+
const count = resident();
|
|
303
|
+
// A host that answered a short 206 left not even the 1x1 level: no file to hand on.
|
|
304
|
+
if (count === 0)
|
|
305
|
+
throw new Error(
|
|
306
|
+
`TN_KTX2_FETCH: '${url}' sent ${String(have.byteLength)} bytes, short of its smallest mip level`,
|
|
307
|
+
);
|
|
308
|
+
return count === levels.length && !ranged
|
|
309
|
+
? have
|
|
310
|
+
: trimmedKtx2(have, index, levels.length - count);
|
|
311
|
+
};
|
|
312
|
+
return {
|
|
313
|
+
index,
|
|
314
|
+
get residentLevels() {
|
|
315
|
+
return resident();
|
|
316
|
+
},
|
|
317
|
+
get ranged() {
|
|
318
|
+
return ranged;
|
|
319
|
+
},
|
|
320
|
+
get bytesRead() {
|
|
321
|
+
return bytesRead;
|
|
322
|
+
},
|
|
323
|
+
file,
|
|
324
|
+
want: async (count) => {
|
|
325
|
+
const target = Math.min(levels.length, Math.max(1, Math.trunc(count)));
|
|
326
|
+
if (target <= resident()) return undefined;
|
|
327
|
+
await readTo(levelEnd(levels.length - target));
|
|
328
|
+
return file();
|
|
329
|
+
},
|
|
330
|
+
};
|
|
331
|
+
}
|
package/src/version.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// Generated by scripts/generate-version.mjs - do not edit. Source: package.json.
|
|
2
|
-
export const CORE_VERSION = "0.3.
|
|
2
|
+
export const CORE_VERSION = "0.3.13";
|
package/src/worker.ts
CHANGED
|
@@ -711,6 +711,7 @@ export function startWorkerGame(
|
|
|
711
711
|
}
|
|
712
712
|
};
|
|
713
713
|
|
|
714
|
+
let started = false;
|
|
714
715
|
let resolveReady: () => void = () => undefined;
|
|
715
716
|
let rejectReady: (error: Error) => void = () => undefined;
|
|
716
717
|
const ready = new Promise<void>((resolve, reject) => {
|
|
@@ -789,6 +790,7 @@ export function startWorkerGame(
|
|
|
789
790
|
installMirror(message.name, message.epoch);
|
|
790
791
|
break;
|
|
791
792
|
case "tn-worker-started":
|
|
793
|
+
started = true;
|
|
792
794
|
resolveReady();
|
|
793
795
|
break;
|
|
794
796
|
case "tn-worker-failed":
|
|
@@ -833,8 +835,16 @@ export function startWorkerGame(
|
|
|
833
835
|
|
|
834
836
|
listen(worker, "message", onWorkerMessage);
|
|
835
837
|
const onWorkerError = (event: Event): void => {
|
|
836
|
-
|
|
837
|
-
|
|
838
|
+
// A worker's ErrorEvent carries where it threw; without it the page sees only the message, and a
|
|
839
|
+
// stack-less "worker died" is unactionable (the 0.3.13 release proof lost a red this way).
|
|
840
|
+
const error = event as ErrorEvent;
|
|
841
|
+
const origin = [error.filename, error.lineno, error.colno].filter(Boolean).join(":");
|
|
842
|
+
const message = `${error.message || `worker ${event.type}`}${origin ? ` (${origin})` : ""}`;
|
|
843
|
+
// Once started, nothing awaits `ready`: without this line a frame that threw removed the canvas
|
|
844
|
+
// and printed nothing (PRD-857).
|
|
845
|
+
if (started && !stopped)
|
|
846
|
+
console.error(`TN_WORKER_FAILED: the running worker game stopped: ${message}`);
|
|
847
|
+
stop(new Error(message));
|
|
838
848
|
};
|
|
839
849
|
listen(worker, "error", onWorkerError);
|
|
840
850
|
listen(worker, "messageerror", onWorkerError);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type Material, type Mesh, Object3D } from "three";
|
|
2
2
|
import { isEngineRenderHook } from "./engine-render-hook.js";
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -69,9 +69,16 @@ const BUNDLE_PROOF_ROWS_PER_FRAME = 256;
|
|
|
69
69
|
*/
|
|
70
70
|
const BUNDLE_PROOF_MAX_PERIOD = 600;
|
|
71
71
|
|
|
72
|
+
/** The owned engine's witness of each proved material's surface flags (PRD-879). */
|
|
73
|
+
interface IMaterialWitnessHost {
|
|
74
|
+
__tnMaterialWatch?(materials: readonly Material[]): number;
|
|
75
|
+
__tnMaterialCheck?(): ArrayLike<number>;
|
|
76
|
+
}
|
|
77
|
+
|
|
72
78
|
/** Own-link and installed-Node child snapshots avoid inherited TSL method scans. Deep unversioned
|
|
73
79
|
* edits still invalidate: version/root identity alone cannot certify a mutable node graph. The
|
|
74
|
-
* shallow witness (version, transparency, visibility, transmission)
|
|
80
|
+
* shallow witness (version, transparency, visibility, transmission) holds on every check: read per
|
|
81
|
+
* check, or for a `deep: false` check the flags are the owned engine's one answer a frame. The deep
|
|
75
82
|
* rows are proved once per `staleFrames` period: the rows the last frame tested over a per-frame
|
|
76
83
|
* budget, so a small world proves every frame and a forest spreads its proofs evenly, each material
|
|
77
84
|
* in its own slot of the period. Three itself rebuilds a draw only when `material.version` moves
|
|
@@ -79,6 +86,16 @@ const BUNDLE_PROOF_MAX_PERIOD = 600;
|
|
|
79
86
|
* window. A graph past the witness caps uses the uncached safety walk. */
|
|
80
87
|
export class BundleSurfaceSafety {
|
|
81
88
|
#checks = new WeakMap<Material, IBundleMaterialCheck>();
|
|
89
|
+
/**
|
|
90
|
+
* The owned engine holds every cached verdict's `transparent`, `visible` and `transmission` and
|
|
91
|
+
* answers which moved in one call a frame, so a settled `deep: false` test reads none of them.
|
|
92
|
+
* Upstream three has no such host method and reads them per test, as before.
|
|
93
|
+
*
|
|
94
|
+
* ponytail: the list keeps every proved material until `clear`, which a world bounds by its assets.
|
|
95
|
+
*/
|
|
96
|
+
readonly #witness = new Object3D() as Object3D & IMaterialWitnessHost;
|
|
97
|
+
#witnessed: Material[] = [];
|
|
98
|
+
readonly #moved = new Set<Material>();
|
|
82
99
|
#revision = 0;
|
|
83
100
|
#frame = 0;
|
|
84
101
|
#frameRows = 0;
|
|
@@ -93,6 +110,9 @@ export class BundleSurfaceSafety {
|
|
|
93
110
|
|
|
94
111
|
clear(): void {
|
|
95
112
|
this.#checks = new WeakMap();
|
|
113
|
+
this.#witnessed = [];
|
|
114
|
+
this.#moved.clear();
|
|
115
|
+
this.#witness.__tnMaterialWatch?.(this.#witnessed);
|
|
96
116
|
}
|
|
97
117
|
|
|
98
118
|
/** Opens a frame: the period every deep proof is spread over follows the last frame's rows. */
|
|
@@ -103,6 +123,12 @@ export class BundleSurfaceSafety {
|
|
|
103
123
|
Math.max(1, Math.ceil(this.#frameRows / BUNDLE_PROOF_ROWS_PER_FRAME)),
|
|
104
124
|
);
|
|
105
125
|
this.#frameRows = 0;
|
|
126
|
+
// A moved material stays moved until its next test re-proves it, because that re-proof
|
|
127
|
+
// re-stamps the whole list and the engine would not answer for it again.
|
|
128
|
+
const moved = this.#witness.__tnMaterialCheck?.();
|
|
129
|
+
if (moved !== undefined)
|
|
130
|
+
for (let at = 0; at < moved.length; at++)
|
|
131
|
+
this.#moved.add(this.#witnessed[moved[at] as number] as Material);
|
|
106
132
|
}
|
|
107
133
|
|
|
108
134
|
/**
|
|
@@ -114,13 +140,7 @@ export class BundleSurfaceSafety {
|
|
|
114
140
|
test(material: Material, deep = true): IBundleMaterialCheck {
|
|
115
141
|
this.stats.checks++;
|
|
116
142
|
const previous = this.#checks.get(material);
|
|
117
|
-
if (
|
|
118
|
-
previous !== undefined &&
|
|
119
|
-
previous.version === material.version &&
|
|
120
|
-
previous.transparent === material.transparent &&
|
|
121
|
-
previous.visible === material.visible &&
|
|
122
|
-
previous.transmission === Reflect.get(material, "transmission")
|
|
123
|
-
) {
|
|
143
|
+
if (previous !== undefined && this.#shallowHolds(material, previous, deep)) {
|
|
124
144
|
if (!deep) return previous;
|
|
125
145
|
if (previous.testedFrame !== this.#frame) {
|
|
126
146
|
previous.testedFrame = this.#frame;
|
|
@@ -157,9 +177,34 @@ export class BundleSurfaceSafety {
|
|
|
157
177
|
if (capture.overflow) {
|
|
158
178
|
this.stats.uncached++;
|
|
159
179
|
this.#checks.delete(material);
|
|
160
|
-
} else
|
|
180
|
+
} else {
|
|
181
|
+
this.#checks.set(material, checked);
|
|
182
|
+
this.#rewatch(material);
|
|
183
|
+
}
|
|
161
184
|
return checked;
|
|
162
185
|
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Whether the material still holds the cached verdict's shallow witness. `version` is JS-side;
|
|
189
|
+
* the flags are the engine's answer when it witnesses them and a `deep: false` test asks.
|
|
190
|
+
*/
|
|
191
|
+
#shallowHolds(material: Material, previous: IBundleMaterialCheck, deep: boolean): boolean {
|
|
192
|
+
if (previous.version !== material.version) return false;
|
|
193
|
+
if (!deep && this.#witness.__tnMaterialCheck !== undefined) return !this.#moved.has(material);
|
|
194
|
+
return (
|
|
195
|
+
previous.transparent === material.transparent &&
|
|
196
|
+
previous.visible === material.visible &&
|
|
197
|
+
previous.transmission === Reflect.get(material, "transmission")
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** Re-stamp the engine's witness after a verdict was (re)taken, so it answers from this state. */
|
|
202
|
+
#rewatch(material: Material): void {
|
|
203
|
+
if (this.#witness.__tnMaterialWatch === undefined) return;
|
|
204
|
+
if (!this.#witnessed.includes(material)) this.#witnessed.push(material);
|
|
205
|
+
this.#moved.delete(material);
|
|
206
|
+
this.#witness.__tnMaterialWatch(this.#witnessed);
|
|
207
|
+
}
|
|
163
208
|
}
|
|
164
209
|
|
|
165
210
|
function graphSnapshotCurrent(row: IBundleGraphSnapshot): boolean {
|