@threenative/core 0.3.12 → 0.3.15
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 +1047 -44
- 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-2GPHICV6.js} +1 -1
- package/dist/{chunk-GMHATRPW.js → chunk-GEMQJBY4.js} +1650 -383
- package/dist/{chunk-VAZM36TV.js → chunk-H4J6IAJE.js} +17 -10
- package/dist/{chunk-5O73CDMR.js → chunk-QAMSM3Z5.js} +415 -27
- package/dist/{chunk-2Z44XSI2.js → chunk-QAQHYOWY.js} +3 -7
- package/dist/{chunk-DBELTTTM.js → chunk-U4YHUKKL.js} +1 -1
- package/dist/{chunk-OKXUCASC.js → chunk-VBBYWGAT.js} +3 -4
- package/dist/chunk-VP43GIWW.js +18 -0
- package/dist/{chunk-N5LRP4BR.js → chunk-VQRPEOKY.js} +66 -3
- 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 +871 -1018
- package/dist/index.js +4786 -1631
- 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-BADStsTn.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 +280 -16
- package/dist/world.js +1529 -642
- package/mcp/animation-server.mjs +1 -1
- package/mcp/engine-server.mjs +135 -11
- package/mcp/servers.mjs +4 -4
- package/package.json +10 -6
- package/scripts/ensure-mcp.mjs +6 -1
- 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/clustered-batch.ts +146 -3
- package/src/clustered-mesh.ts +548 -20
- package/src/compute-driven.ts +1 -1
- package/src/config.ts +8 -0
- package/src/debug.ts +119 -22
- package/src/dev-global.ts +54 -0
- package/src/displaced-surface.ts +428 -0
- package/src/engine-hooks.ts +37 -0
- package/src/frame-budget.ts +35 -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 +14 -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/grass-field.ts +628 -0
- package/src/hot.ts +4 -7
- package/src/index.ts +227 -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/material-key.ts +21 -1
- 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/screen-probe-gather.ts +303 -0
- package/src/render/short-range-ao.ts +274 -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 +207 -9
- package/src/render/world-impostor.ts +5 -2
- package/src/render-camera-cull.ts +105 -4
- 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 +78 -32
- package/src/resolution-scaler.ts +3 -2
- package/src/scatter-field.ts +496 -0
- package/src/scene-entry-inspectors.ts +61 -0
- package/src/snow-field.ts +1 -0
- package/src/startup-offscreen-bytes.ts +84 -0
- package/src/state.ts +16 -3
- package/src/static-transform.ts +100 -25
- package/src/streamed-textures.ts +112 -0
- package/src/streaming.ts +68 -4
- package/src/texture-streaming.ts +331 -0
- package/src/tile-ring.ts +294 -0
- package/src/version.ts +1 -1
- package/src/warmup.ts +15 -2
- package/src/worker.ts +12 -2
- package/src/world-bundle-safety.ts +55 -10
- package/src/world-cell-batches.ts +166 -200
- package/src/world-cell-chunks.ts +3 -7
- package/src/world-cell-data.ts +7 -0
- package/src/world-cell-impostors.ts +20 -8
- package/src/world-cell-models.ts +36 -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 +17 -19
- package/src/world-cell-render-main-cull.ts +73 -17
- package/src/world-cell-render-util.ts +18 -5
- 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 +23 -6
- 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 +273 -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-water-bodies.ts +113 -3
- 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/tile-ring.ts
ADDED
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
/** A tile is built inside this many draw distances, so it exists before it is seen. */
|
|
2
|
+
export const CREATE_BAND = 1.3;
|
|
3
|
+
/** A tile is discarded only past this many draw distances, so a camera on the edge never thrashes. */
|
|
4
|
+
export const DISCARD_BAND = 1.4;
|
|
5
|
+
|
|
6
|
+
export interface ITileBounds {
|
|
7
|
+
readonly minX: number;
|
|
8
|
+
readonly minZ: number;
|
|
9
|
+
readonly maxX: number;
|
|
10
|
+
readonly maxZ: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* What a `TileRing` needs from the field it streams: the live settings every frame reads, one tile
|
|
15
|
+
* builder, and the three things to do to a live tile. The field keeps the ground query, the look and
|
|
16
|
+
* the level-of-detail rule; the ring keeps the ring.
|
|
17
|
+
*/
|
|
18
|
+
export interface ITileRingField<TTile> {
|
|
19
|
+
readonly chunkSize: number;
|
|
20
|
+
readonly drawDistance: number;
|
|
21
|
+
readonly buildBudgetMs: number;
|
|
22
|
+
/** Whether a column is inside the field's bounds; the ring asks before queueing it. */
|
|
23
|
+
inBounds(ix: number, iz: number): boolean;
|
|
24
|
+
/** Builds one tile. Resolve an empty tile by returning `null`; it is remembered, not rebuilt. */
|
|
25
|
+
build(ix: number, iz: number): TTile | null;
|
|
26
|
+
/** The tile is live and inside the draw distance: show it and pick its level of detail. */
|
|
27
|
+
show(tile: TTile, distance: number): void;
|
|
28
|
+
/** The tile is live but outside the draw distance, and not yet discarded. */
|
|
29
|
+
hide(tile: TTile): void;
|
|
30
|
+
dispose(tile: TTile): void;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface ITileRingStats {
|
|
34
|
+
/** Live tiles, built or remembered empty. */
|
|
35
|
+
tiles: number;
|
|
36
|
+
/** Tiles the last `update` showed. */
|
|
37
|
+
visible: number;
|
|
38
|
+
/** Tiles queued but not built at the end of the last `update`. */
|
|
39
|
+
pending: number;
|
|
40
|
+
/** Tiles built since the ring was made. */
|
|
41
|
+
built: number;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
interface ITile<TTile> {
|
|
45
|
+
readonly ix: number;
|
|
46
|
+
readonly iz: number;
|
|
47
|
+
/** `null` when the builder found nothing to place: remembered so it is not rebuilt every frame. */
|
|
48
|
+
readonly tile: TTile | null;
|
|
49
|
+
seen: number;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Square tiles streamed round a moving follow point, nearest first, under a per-frame build budget.
|
|
54
|
+
*
|
|
55
|
+
* A tile is built inside 1.3 × the draw distance and discarded past 1.4 ×, Unreal Landscape grass's
|
|
56
|
+
* guard band (UE 5.8.3: Engine/Source/Runtime/Landscape/Private/LandscapeGrass.cpp:88, :94), so a
|
|
57
|
+
* follow point on the edge never thrashes. One tile always builds, so the ring advances even under a
|
|
58
|
+
* budget smaller than one build; the rest wait nearest first. The field supplies what a tile is, how
|
|
59
|
+
* it looks at a distance and how it is freed, so grass blades and scattered props stream through the
|
|
60
|
+
* same loop.
|
|
61
|
+
*
|
|
62
|
+
* @constraint call `update(x, z)` once per rendered frame
|
|
63
|
+
* @constraint rotating or scaling the owning group is not supported; translating it is
|
|
64
|
+
*/
|
|
65
|
+
export class TileRing<TTile> {
|
|
66
|
+
readonly stats: ITileRingStats = { built: 0, pending: 0, tiles: 0, visible: 0 };
|
|
67
|
+
readonly #field: ITileRingField<TTile>;
|
|
68
|
+
readonly #tiles = new Map<number, ITile<TTile>>();
|
|
69
|
+
readonly #queue: { d: number; ix: number; iz: number; k: number }[] = [];
|
|
70
|
+
#frame = 0;
|
|
71
|
+
#prewarm = false;
|
|
72
|
+
|
|
73
|
+
constructor(field: ITileRingField<TTile>) {
|
|
74
|
+
this.#field = field;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Every live tile, for a field that has to re-scatter or re-key its own build. */
|
|
78
|
+
tiles(): IterableIterator<ITile<TTile>> {
|
|
79
|
+
return this.#tiles.values();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Builds every tile in range on the next `update` regardless of budget: on load or after a teleport. */
|
|
83
|
+
prewarm(): void {
|
|
84
|
+
this.#prewarm = true;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Drops every live tile: the field's density or layout changed. */
|
|
88
|
+
clear(): void {
|
|
89
|
+
for (const entry of this.#tiles.values())
|
|
90
|
+
if (entry.tile !== null) this.#field.dispose(entry.tile);
|
|
91
|
+
this.#tiles.clear();
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Frees the tiles touching a rectangle, so only what changed re-scatters. */
|
|
95
|
+
disposeInside(rect: ITileBounds): void {
|
|
96
|
+
const S = this.#field.chunkSize;
|
|
97
|
+
for (const [k, entry] of this.#tiles) {
|
|
98
|
+
if (
|
|
99
|
+
entry.ix * S > rect.maxX ||
|
|
100
|
+
(entry.ix + 1) * S < rect.minX ||
|
|
101
|
+
entry.iz * S > rect.maxZ ||
|
|
102
|
+
(entry.iz + 1) * S < rect.minZ
|
|
103
|
+
)
|
|
104
|
+
continue;
|
|
105
|
+
if (entry.tile !== null) this.#field.dispose(entry.tile);
|
|
106
|
+
this.#tiles.delete(k);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Streams, shows and frees the tiles round `(cx, cz)`. Call once per rendered frame. */
|
|
111
|
+
update(cx: number, cz: number): void {
|
|
112
|
+
const field = this.#field;
|
|
113
|
+
const R = field.drawDistance;
|
|
114
|
+
const frame = ++this.#frame;
|
|
115
|
+
this.stats.visible = 0;
|
|
116
|
+
this.#visit(cx, cz, R, frame);
|
|
117
|
+
this.#buildQueued(frame);
|
|
118
|
+
this.#prewarm = false;
|
|
119
|
+
this.#evict(cx, cz, R, frame);
|
|
120
|
+
this.stats.tiles = this.#tiles.size;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Marks the tiles inside the create band and queues the missing ones. */
|
|
124
|
+
#visit(cx: number, cz: number, R: number, frame: number): void {
|
|
125
|
+
const S = this.#field.chunkSize;
|
|
126
|
+
const reach = R * CREATE_BAND;
|
|
127
|
+
const queue = this.#queue;
|
|
128
|
+
queue.length = 0;
|
|
129
|
+
for (let iz = Math.floor((cz - reach) / S); iz <= Math.floor((cz + reach) / S); iz++) {
|
|
130
|
+
for (let ix = Math.floor((cx - reach) / S); ix <= Math.floor((cx + reach) / S); ix++) {
|
|
131
|
+
const d = tileDistance(ix, iz, cx, cz, S);
|
|
132
|
+
if (d >= reach || !this.#field.inBounds(ix, iz)) continue;
|
|
133
|
+
const k = tileKey(ix, iz);
|
|
134
|
+
const entry = this.#tiles.get(k);
|
|
135
|
+
if (!entry) {
|
|
136
|
+
queue.push({ d, ix, iz, k });
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
entry.seen = frame;
|
|
140
|
+
if (entry.tile !== null) this.#field.show(entry.tile, d);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Builds queued tiles nearest first; one always builds, the rest only inside the budget. */
|
|
146
|
+
#buildQueued(frame: number): void {
|
|
147
|
+
const queue = this.#queue;
|
|
148
|
+
queue.sort((a, c) => a.d - c.d);
|
|
149
|
+
const budget = this.#prewarm ? Number.POSITIVE_INFINITY : this.#field.buildBudgetMs;
|
|
150
|
+
const t0 = performance.now();
|
|
151
|
+
let built = 0;
|
|
152
|
+
for (const q of queue) {
|
|
153
|
+
if (built > 0 && performance.now() - t0 > budget) break;
|
|
154
|
+
const tile = this.#field.build(q.ix, q.iz);
|
|
155
|
+
this.#tiles.set(q.k, { ix: q.ix, iz: q.iz, seen: frame, tile });
|
|
156
|
+
if (tile !== null) this.#field.show(tile, q.d);
|
|
157
|
+
built++;
|
|
158
|
+
}
|
|
159
|
+
this.stats.built += built;
|
|
160
|
+
this.stats.pending = queue.length - built;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Hides tiles that left the view radius and frees those past the discard band. */
|
|
164
|
+
#evict(cx: number, cz: number, R: number, frame: number): void {
|
|
165
|
+
const S = this.#field.chunkSize;
|
|
166
|
+
for (const [k, entry] of this.#tiles) {
|
|
167
|
+
if (entry.seen === frame) continue;
|
|
168
|
+
if (tileDistance(entry.ix, entry.iz, cx, cz, S) > R * DISCARD_BAND) {
|
|
169
|
+
if (entry.tile !== null) this.#field.dispose(entry.tile);
|
|
170
|
+
this.#tiles.delete(k);
|
|
171
|
+
} else if (entry.tile !== null) this.#field.hide(entry.tile);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** The distance from a point to a tile's nearest corner, in the ground plane. */
|
|
177
|
+
export function tileDistance(ix: number, iz: number, cx: number, cz: number, S: number): number {
|
|
178
|
+
const dx = Math.max(ix * S - cx, 0, cx - (ix + 1) * S);
|
|
179
|
+
const dz = Math.max(iz * S - cz, 0, cz - (iz + 1) * S);
|
|
180
|
+
return Math.sqrt(dx * dx + dz * dz);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** A tile column's key: unique for ix and iz in ±32767. */
|
|
184
|
+
export const tileKey = (ix: number, iz: number): number => (ix + 0x8000) * 0x10000 + (iz + 0x8000);
|
|
185
|
+
|
|
186
|
+
/** A seeded 32-bit integer hash of a column and a salt. */
|
|
187
|
+
export function hashInt(x: number, y: number, s: number): number {
|
|
188
|
+
let h =
|
|
189
|
+
Math.imul(x | 0, 0x27d4eb2d) ^ Math.imul(y | 0, 0x165667b1) ^ Math.imul(s | 0, 0x9e3779b1);
|
|
190
|
+
h = Math.imul(h ^ (h >>> 15), 0x85ebca6b);
|
|
191
|
+
h = Math.imul(h ^ (h >>> 13), 0xc2b2ae35);
|
|
192
|
+
return (h ^ (h >>> 16)) >>> 0;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** The seeded generator both fields scatter their instances from. */
|
|
196
|
+
export function mulberry32(seed: number): () => number {
|
|
197
|
+
let a = seed >>> 0;
|
|
198
|
+
return () => {
|
|
199
|
+
a = (a + 0x6d2b79f5) | 0;
|
|
200
|
+
let t = Math.imul(a ^ (a >>> 15), 1 | a);
|
|
201
|
+
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
|
|
202
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* 0..total-1 in a seeded random order. Visiting a jittered grid in this order means any prefix
|
|
208
|
+
* covers the tile evenly, so distance thinning is a plain instance count.
|
|
209
|
+
*/
|
|
210
|
+
export function shuffled(total: number, rng: () => number): Uint32Array {
|
|
211
|
+
const order = new Uint32Array(total);
|
|
212
|
+
for (let i = 0; i < total; i++) order[i] = i;
|
|
213
|
+
for (let i = total - 1; i > 0; i--) {
|
|
214
|
+
const j = (rng() * (i + 1)) | 0;
|
|
215
|
+
const tmp = order[i] as number;
|
|
216
|
+
order[i] = order[j] as number;
|
|
217
|
+
order[j] = tmp;
|
|
218
|
+
}
|
|
219
|
+
return order;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** Height, d/dx and d/dz of the last `groundGrid` sample. */
|
|
223
|
+
export const sample = new Float64Array(3);
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Ground height on a grid one ring wider than a tile, for gradients; `sample` reads it bilinearly.
|
|
227
|
+
* Heights come from the game's own `getHeight`, so a tilt on a prop follows the ground the game
|
|
228
|
+
* draws, not a second copy of it.
|
|
229
|
+
*/
|
|
230
|
+
export function groundGrid(
|
|
231
|
+
getHeight: (x: number, z: number) => number,
|
|
232
|
+
ox: number,
|
|
233
|
+
oz: number,
|
|
234
|
+
S: number,
|
|
235
|
+
spacing: number,
|
|
236
|
+
): { sample(lx: number, lz: number): void } {
|
|
237
|
+
const G = Math.max(1, Math.ceil(S / Math.max(spacing, 0.05)));
|
|
238
|
+
const gs = S / G;
|
|
239
|
+
const N = G + 3;
|
|
240
|
+
const hg = new Float32Array(N * N);
|
|
241
|
+
for (let j = 0; j < N; j++)
|
|
242
|
+
for (let i = 0; i < N; i++)
|
|
243
|
+
hg[j * N + i] = getHeight(ox + (i - 1) * gs, oz + (j - 1) * gs) || 0;
|
|
244
|
+
const gx = new Float32Array(N * N);
|
|
245
|
+
const gz = new Float32Array(N * N);
|
|
246
|
+
const inv2 = 1 / (2 * gs);
|
|
247
|
+
for (let j = 1; j < N - 1; j++)
|
|
248
|
+
for (let i = 1; i < N - 1; i++) {
|
|
249
|
+
const o = j * N + i;
|
|
250
|
+
gx[o] = ((hg[o + 1] as number) - (hg[o - 1] as number)) * inv2;
|
|
251
|
+
gz[o] = ((hg[o + N] as number) - (hg[o - N] as number)) * inv2;
|
|
252
|
+
}
|
|
253
|
+
return {
|
|
254
|
+
sample(lx, lz) {
|
|
255
|
+
const fx = lx / gs + 1;
|
|
256
|
+
const fz = lz / gs + 1;
|
|
257
|
+
const i0 = Math.min(fx | 0, G);
|
|
258
|
+
const j0 = Math.min(fz | 0, G);
|
|
259
|
+
const u = fx - i0;
|
|
260
|
+
const v = fz - j0;
|
|
261
|
+
const o = j0 * N + i0;
|
|
262
|
+
// Bilinear weights once, no closure per call: this runs for every candidate.
|
|
263
|
+
const w00 = (1 - u) * (1 - v);
|
|
264
|
+
const w10 = u * (1 - v);
|
|
265
|
+
const w01 = (1 - u) * v;
|
|
266
|
+
const w11 = u * v;
|
|
267
|
+
const o1 = o + 1;
|
|
268
|
+
const oN = o + N;
|
|
269
|
+
const oN1 = oN + 1;
|
|
270
|
+
sample[0] =
|
|
271
|
+
(hg[o] as number) * w00 +
|
|
272
|
+
(hg[o1] as number) * w10 +
|
|
273
|
+
(hg[oN] as number) * w01 +
|
|
274
|
+
(hg[oN1] as number) * w11;
|
|
275
|
+
sample[1] =
|
|
276
|
+
(gx[o] as number) * w00 +
|
|
277
|
+
(gx[o1] as number) * w10 +
|
|
278
|
+
(gx[oN] as number) * w01 +
|
|
279
|
+
(gx[oN1] as number) * w11;
|
|
280
|
+
sample[2] =
|
|
281
|
+
(gz[o] as number) * w00 +
|
|
282
|
+
(gz[o1] as number) * w10 +
|
|
283
|
+
(gz[oN] as number) * w01 +
|
|
284
|
+
(gz[oN1] as number) * w11;
|
|
285
|
+
},
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
export const clamp01 = (v: number): number => (v < 0 ? 0 : v > 1 ? 1 : v);
|
|
290
|
+
|
|
291
|
+
export function smoothstep(e0: number, e1: number, x: number): number {
|
|
292
|
+
const t = clamp01((x - e0) / Math.max(e1 - e0, 1e-6));
|
|
293
|
+
return t * t * (3 - 2 * t);
|
|
294
|
+
}
|
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.15";
|
package/src/warmup.ts
CHANGED
|
@@ -286,6 +286,11 @@ interface INativeEnvironmentPreparer {
|
|
|
286
286
|
): Promise<boolean>;
|
|
287
287
|
}
|
|
288
288
|
|
|
289
|
+
/** `drawPastFrameGate`'s refusal: the renderer never took the covered draw in its whole wait. */
|
|
290
|
+
function isFrameGateTimeout(error: unknown): boolean {
|
|
291
|
+
return error instanceof Error && error.message.startsWith("TN_WARMUP_FRAME_GATE");
|
|
292
|
+
}
|
|
293
|
+
|
|
289
294
|
function nativeEnvironmentPreparer(raw: unknown): INativeEnvironmentPreparer | undefined {
|
|
290
295
|
return typeof (raw as Partial<INativeEnvironmentPreparer> | null)?.prepareEnvironmentAsync ===
|
|
291
296
|
"function"
|
|
@@ -1229,12 +1234,19 @@ export async function warmUpScene(
|
|
|
1229
1234
|
// Skipped because the budget ran out first: lazy work, like any compile that never
|
|
1230
1235
|
// started. Rejecting here failed a held startup whose compile merely ran long.
|
|
1231
1236
|
if (passError === undefined && now() >= startedAt + budgetMs) return;
|
|
1237
|
+
// The frame gate declined the covered draw for its whole wait: the GPU was busy (a
|
|
1238
|
+
// software adapter holds each frame for seconds), which no pipeline caused. The pipelines
|
|
1239
|
+
// build lazily at the first frame that needs them; a rejection here failed the held launch.
|
|
1240
|
+
if (isFrameGateTimeout(passError)) return;
|
|
1232
1241
|
throw passError ?? new Error("covered world warm render did not settle");
|
|
1233
1242
|
}
|
|
1234
1243
|
});
|
|
1235
1244
|
};
|
|
1236
1245
|
|
|
1237
1246
|
let gpuDrainSettled = true;
|
|
1247
|
+
// The last compile the deadline outran (still pending, not rejected). The expiry timer can fire a
|
|
1248
|
+
// fraction of a millisecond before `now()` reaches the deadline, so the clock alone misses it.
|
|
1249
|
+
let compileOutran = false;
|
|
1238
1250
|
const compileWithinDeadline = async (
|
|
1239
1251
|
deadline: number,
|
|
1240
1252
|
work: Promise<void>,
|
|
@@ -1250,7 +1262,8 @@ export async function warmUpScene(
|
|
|
1250
1262
|
};
|
|
1251
1263
|
void work.then(track, track);
|
|
1252
1264
|
const finished = await withinDeadline(deadline, work, yieldFrame, now);
|
|
1253
|
-
|
|
1265
|
+
compileOutran = !finished && !settled;
|
|
1266
|
+
if (compileOutran) pendingCompiles.push(work);
|
|
1254
1267
|
return finished;
|
|
1255
1268
|
};
|
|
1256
1269
|
const finish = (report: IWarmUpReport): IWarmUpReport => {
|
|
@@ -1369,7 +1382,7 @@ export async function warmUpScene(
|
|
|
1369
1382
|
elapsedMs: now() - startedAt,
|
|
1370
1383
|
unsupported: false,
|
|
1371
1384
|
abandoned: finished ? 0 : 1,
|
|
1372
|
-
timedOut: !finished && (passError === undefined || now() >= deadline),
|
|
1385
|
+
timedOut: !finished && (passError === undefined || compileOutran || now() >= deadline),
|
|
1373
1386
|
}),
|
|
1374
1387
|
);
|
|
1375
1388
|
}
|