@babylonjs/loaders 9.12.0 → 9.13.0
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/FBX/fbxFileLoader.d.ts +194 -0
- package/FBX/fbxFileLoader.js +2440 -0
- package/FBX/fbxFileLoader.js.map +1 -0
- package/FBX/fbxFileLoader.metadata.d.ts +11 -0
- package/FBX/fbxFileLoader.metadata.js +11 -0
- package/FBX/fbxFileLoader.metadata.js.map +1 -0
- package/FBX/index.d.ts +3 -0
- package/FBX/index.js +3 -0
- package/FBX/index.js.map +1 -0
- package/FBX/interpreter/animation.d.ts +122 -0
- package/FBX/interpreter/animation.js +648 -0
- package/FBX/interpreter/animation.js.map +1 -0
- package/FBX/interpreter/blendShapes.d.ts +44 -0
- package/FBX/interpreter/blendShapes.js +192 -0
- package/FBX/interpreter/blendShapes.js.map +1 -0
- package/FBX/interpreter/connections.d.ts +95 -0
- package/FBX/interpreter/connections.js +233 -0
- package/FBX/interpreter/connections.js.map +1 -0
- package/FBX/interpreter/fbxInterpreter.d.ts +149 -0
- package/FBX/interpreter/fbxInterpreter.js +496 -0
- package/FBX/interpreter/fbxInterpreter.js.map +1 -0
- package/FBX/interpreter/geometry.d.ts +55 -0
- package/FBX/interpreter/geometry.js +573 -0
- package/FBX/interpreter/geometry.js.map +1 -0
- package/FBX/interpreter/materials.d.ts +50 -0
- package/FBX/interpreter/materials.js +144 -0
- package/FBX/interpreter/materials.js.map +1 -0
- package/FBX/interpreter/propertyTemplates.d.ts +22 -0
- package/FBX/interpreter/propertyTemplates.js +125 -0
- package/FBX/interpreter/propertyTemplates.js.map +1 -0
- package/FBX/interpreter/rig.d.ts +20 -0
- package/FBX/interpreter/rig.js +259 -0
- package/FBX/interpreter/rig.js.map +1 -0
- package/FBX/interpreter/sceneDiagnostics.d.ts +14 -0
- package/FBX/interpreter/sceneDiagnostics.js +55 -0
- package/FBX/interpreter/sceneDiagnostics.js.map +1 -0
- package/FBX/interpreter/skeleton.d.ts +93 -0
- package/FBX/interpreter/skeleton.js +515 -0
- package/FBX/interpreter/skeleton.js.map +1 -0
- package/FBX/interpreter/transform.d.ts +21 -0
- package/FBX/interpreter/transform.js +92 -0
- package/FBX/interpreter/transform.js.map +1 -0
- package/FBX/parsers/fbxAsciiParser.d.ts +5 -0
- package/FBX/parsers/fbxAsciiParser.js +330 -0
- package/FBX/parsers/fbxAsciiParser.js.map +1 -0
- package/FBX/parsers/fbxBinaryParser.d.ts +6 -0
- package/FBX/parsers/fbxBinaryParser.js +255 -0
- package/FBX/parsers/fbxBinaryParser.js.map +1 -0
- package/FBX/parsers/zlibInflate.d.ts +7 -0
- package/FBX/parsers/zlibInflate.js +350 -0
- package/FBX/parsers/zlibInflate.js.map +1 -0
- package/FBX/types/fbxTypes.d.ts +54 -0
- package/FBX/types/fbxTypes.js +66 -0
- package/FBX/types/fbxTypes.js.map +1 -0
- package/SPLAT/gaussianSplattingStream.d.ts +404 -0
- package/SPLAT/gaussianSplattingStream.js +1159 -0
- package/SPLAT/gaussianSplattingStream.js.map +1 -0
- package/SPLAT/gaussianSplattingWorkBuffer.d.ts +71 -0
- package/SPLAT/gaussianSplattingWorkBuffer.js +249 -0
- package/SPLAT/gaussianSplattingWorkBuffer.js.map +1 -0
- package/SPLAT/gaussianSplattingWorkBufferShaders.d.ts +25 -0
- package/SPLAT/gaussianSplattingWorkBufferShaders.js +255 -0
- package/SPLAT/gaussianSplattingWorkBufferShaders.js.map +1 -0
- package/SPLAT/index.d.ts +1 -0
- package/SPLAT/index.js +1 -0
- package/SPLAT/index.js.map +1 -1
- package/SPLAT/sog.d.ts +6 -1
- package/SPLAT/sog.js +111 -25
- package/SPLAT/sog.js.map +1 -1
- package/SPLAT/splatFileLoader.d.ts +8 -0
- package/SPLAT/splatFileLoader.js +49 -0
- package/SPLAT/splatFileLoader.js.map +1 -1
- package/dynamic.js +9 -0
- package/dynamic.js.map +1 -1
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/index.js.map +1 -1
- package/package.json +3 -3
|
@@ -0,0 +1,1159 @@
|
|
|
1
|
+
import { GaussianSplattingMesh } from "@babylonjs/core/Meshes/GaussianSplatting/gaussianSplattingMesh.js";
|
|
2
|
+
import { Logger } from "@babylonjs/core/Misc/logger.js";
|
|
3
|
+
import { Tools } from "@babylonjs/core/Misc/tools.js";
|
|
4
|
+
import { Vector3, Matrix } from "@babylonjs/core/Maths/math.vector.js";
|
|
5
|
+
import { Color4 } from "@babylonjs/core/Maths/math.color.js";
|
|
6
|
+
import { Frustum } from "@babylonjs/core/Maths/math.frustum.js";
|
|
7
|
+
import { Plane } from "@babylonjs/core/Maths/math.plane.js";
|
|
8
|
+
import { Camera } from "@babylonjs/core/Cameras/camera.js";
|
|
9
|
+
import { BoundingInfo } from "@babylonjs/core/Culling/boundingInfo.js";
|
|
10
|
+
import { CreateLineSystem } from "@babylonjs/core/Meshes/Builders/linesBuilder.js";
|
|
11
|
+
import { VertexBuffer } from "@babylonjs/core/Buffers/buffer.js";
|
|
12
|
+
import { ParseSogMetaAsTextures } from "./sog.js";
|
|
13
|
+
import { GaussianSplattingWorkBuffer } from "./gaussianSplattingWorkBuffer.js";
|
|
14
|
+
// tan(22.5deg): reference half-FOV for a 45-degree vertical FOV, used for FOV compensation (matches PlayCanvas).
|
|
15
|
+
const RefTanHalfFov = Math.tan((22.5 * Math.PI) / 180);
|
|
16
|
+
// Scratch objects reused by the per-frame optimal-LOD evaluation (avoids per-call allocations).
|
|
17
|
+
const TmpInvWorld = new Matrix();
|
|
18
|
+
const TmpLocalCamera = new Vector3();
|
|
19
|
+
const TmpLocalForward = new Vector3();
|
|
20
|
+
const TmpWorldForward = new Vector3();
|
|
21
|
+
// Camera-local forward axis (+Z) used to derive the world-space view direction.
|
|
22
|
+
const LocalForwardAxis = new Vector3(0, 0, 1);
|
|
23
|
+
// The 12 edges of a box, as index pairs into its 8 corners. 12 edges x 2 endpoints = 24 vertices per box.
|
|
24
|
+
const BoxEdges = [
|
|
25
|
+
[0, 1],
|
|
26
|
+
[1, 2],
|
|
27
|
+
[2, 3],
|
|
28
|
+
[3, 0],
|
|
29
|
+
[4, 5],
|
|
30
|
+
[5, 6],
|
|
31
|
+
[6, 7],
|
|
32
|
+
[7, 4],
|
|
33
|
+
[0, 4],
|
|
34
|
+
[1, 5],
|
|
35
|
+
[2, 6],
|
|
36
|
+
[3, 7],
|
|
37
|
+
];
|
|
38
|
+
// Vertices generated per leaf box (BoxEdges.length * 2).
|
|
39
|
+
const VerticesPerBox = BoxEdges.length * 2;
|
|
40
|
+
/**
|
|
41
|
+
* Wireframe colors per LOD level (cycled by `node.activeLod`).
|
|
42
|
+
*/
|
|
43
|
+
const GsLodDebugColors = [
|
|
44
|
+
new Color4(1.0, 0.2, 0.2, 1.0), // LOD 0 - red
|
|
45
|
+
new Color4(1.0, 0.6, 0.1, 1.0), // LOD 1 - orange
|
|
46
|
+
new Color4(1.0, 1.0, 0.2, 1.0), // LOD 2 - yellow
|
|
47
|
+
new Color4(0.3, 1.0, 0.3, 1.0), // LOD 3 - green
|
|
48
|
+
new Color4(0.2, 1.0, 1.0, 1.0), // LOD 4 - cyan
|
|
49
|
+
new Color4(0.4, 0.5, 1.0, 1.0), // LOD 5 - blue
|
|
50
|
+
new Color4(0.9, 0.4, 1.0, 1.0), // LOD 6 - magenta
|
|
51
|
+
new Color4(1.0, 1.0, 1.0, 1.0), // LOD 7 - white
|
|
52
|
+
];
|
|
53
|
+
/**
|
|
54
|
+
* Streams a PlayCanvas-style SOG LOD scene (`lod-meta.json`) into a single Gaussian Splatting mesh.
|
|
55
|
+
*
|
|
56
|
+
* Each selected SOG file (plus the environment) is loaded directly as GPU textures and decoded on the
|
|
57
|
+
* GPU into one unified, PlayCanvas-style square work buffer (no CPU splat decode or `updateData`). Only
|
|
58
|
+
* the splats of each node's currently-selected LOD are rendered/sorted via the mesh's interval filter.
|
|
59
|
+
*
|
|
60
|
+
* The coarsest (least-detail) LOD of every node is streamed first as a permanent base layer so the whole
|
|
61
|
+
* scene is visible quickly with no holes. A distance-based "optimal" LOD is then computed per node (see
|
|
62
|
+
* {@link evaluateOptimalLods}); finer LOD source files are streamed on demand and a node only switches to
|
|
63
|
+
* a finer LOD once that file is decoded, so transitions never flash or leave gaps.
|
|
64
|
+
*
|
|
65
|
+
* @experimental
|
|
66
|
+
*/
|
|
67
|
+
export class GaussianSplattingStream extends GaussianSplattingMesh {
|
|
68
|
+
/**
|
|
69
|
+
* Returns true when the parsed JSON looks like a PlayCanvas-style `lod-meta.json` payload.
|
|
70
|
+
* @param data parsed JSON
|
|
71
|
+
* @returns whether the data is SOG LOD metadata
|
|
72
|
+
*/
|
|
73
|
+
static IsLODMetadata(data) {
|
|
74
|
+
if (typeof data !== "object" || data === null) {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
const meta = data;
|
|
78
|
+
return typeof meta.lodLevels === "number" && Array.isArray(meta.filenames) && typeof meta.tree === "object" && meta.tree !== null;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Creates a new SOG LOD streaming mesh and immediately starts streaming (non-blocking).
|
|
82
|
+
* @param name mesh name
|
|
83
|
+
* @param metadata parsed `lod-meta.json`
|
|
84
|
+
* @param rootUrl base URL the metadata's relative paths resolve against
|
|
85
|
+
* @param scene hosting scene
|
|
86
|
+
* @param options streaming options
|
|
87
|
+
*/
|
|
88
|
+
constructor(name, metadata, rootUrl, scene, options = {}) {
|
|
89
|
+
super(name, null, scene, false);
|
|
90
|
+
// Flat list of leaf nodes that carry renderable LOD entries (used by the LOD heuristic and debug).
|
|
91
|
+
this._leafNodes = [];
|
|
92
|
+
// LOD heuristic parameters (PlayCanvas-aligned defaults).
|
|
93
|
+
this._lodBaseDistance = 5;
|
|
94
|
+
this._lodMultiplier = 3;
|
|
95
|
+
this._lodBehindPenalty = 1;
|
|
96
|
+
this._lodRangeMin = 0;
|
|
97
|
+
this._maxDecodesPerFrame = 1;
|
|
98
|
+
this._lodCooldownFrames = 10;
|
|
99
|
+
// Minimum frames between LOD re-evaluations, and minimum camera movement (world units) to re-evaluate.
|
|
100
|
+
this._lodUpdateInterval = 4;
|
|
101
|
+
this._lodUpdateDistance = 0.5;
|
|
102
|
+
this._maxDetailLod = 0;
|
|
103
|
+
// Frustum LOD bias: when enabled, nodes outside the camera frustum are rendered at their coarsest LOD.
|
|
104
|
+
this._frustumCulling = true;
|
|
105
|
+
// Reused world-space frustum planes and view-projection scratch matrix (avoids per-frame allocation).
|
|
106
|
+
this._frustumPlanes = [
|
|
107
|
+
new Plane(0, 0, 0, 0),
|
|
108
|
+
new Plane(0, 0, 0, 0),
|
|
109
|
+
new Plane(0, 0, 0, 0),
|
|
110
|
+
new Plane(0, 0, 0, 0),
|
|
111
|
+
new Plane(0, 0, 0, 0),
|
|
112
|
+
new Plane(0, 0, 0, 0),
|
|
113
|
+
];
|
|
114
|
+
this._cullViewProj = new Matrix();
|
|
115
|
+
// GPU work buffer holding all decoded splats; created once the total capacity is known.
|
|
116
|
+
this._workBuffer = null;
|
|
117
|
+
// True once GPU position readback has been validated against a CPU decode (see _probeReadbackAsync). While
|
|
118
|
+
// false, positions are decoded on the CPU from the means images; once validated, every SOG image uses the
|
|
119
|
+
// fast direct upload and positions are read back from the work buffer (non-blocking).
|
|
120
|
+
this._useGpuPositionReadback = false;
|
|
121
|
+
// Whether the engine reports GPU readback support (candidate to validate on the first decode).
|
|
122
|
+
this._readbackCandidate = false;
|
|
123
|
+
// Set once the one-time readback validation has run (success or failure).
|
|
124
|
+
this._readbackProbed = false;
|
|
125
|
+
// Global splat offset where each source file begins in the work buffer (fixed for all files up front).
|
|
126
|
+
this._fileBaseSplat = new Map();
|
|
127
|
+
// Splat count of each source file (learned from its metadata before allocation).
|
|
128
|
+
this._fileCounts = new Map();
|
|
129
|
+
// Cached SOG metadata per file so on-demand decodes don't refetch the meta.json.
|
|
130
|
+
this._fileMeta = new Map();
|
|
131
|
+
// Files whose splats have been GPU-decoded into the work buffer.
|
|
132
|
+
this._decodedFiles = new Set();
|
|
133
|
+
// Files whose decode is currently in flight (dedupes concurrent requests).
|
|
134
|
+
this._loadingFiles = new Set();
|
|
135
|
+
// FIFO of file ids waiting to be decoded (drained under a per-frame budget).
|
|
136
|
+
this._decodeQueue = [];
|
|
137
|
+
// Global range covered by the environment file (always rendered), or null until it loads.
|
|
138
|
+
this._environmentRange = null;
|
|
139
|
+
// Unzipped environment bundle contents, retained between count-gathering and decode.
|
|
140
|
+
this._environmentFiles = null;
|
|
141
|
+
// Per-frame LOD streaming loop; installed once the base layer is ready.
|
|
142
|
+
this._lodObserver = null;
|
|
143
|
+
this._baseLayerReady = false;
|
|
144
|
+
// Throttling state for the per-frame LOD loop.
|
|
145
|
+
this._framesSinceLodUpdate = 0;
|
|
146
|
+
this._lastLodCamPos = new Vector3(Infinity, Infinity, Infinity);
|
|
147
|
+
// Forces the next LOD update to run regardless of the throttle (e.g. after a budget change).
|
|
148
|
+
this._forceLodUpdate = false;
|
|
149
|
+
// Running local-space bounds of all decoded splat centers (for frustum culling / picking).
|
|
150
|
+
this._boundsMin = new Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
|
|
151
|
+
this._boundsMax = new Vector3(-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE);
|
|
152
|
+
// Debug LOD-node wireframe display.
|
|
153
|
+
this._debugDisplay = false;
|
|
154
|
+
this._debugLodSource = "optimal";
|
|
155
|
+
this._debugMesh = null;
|
|
156
|
+
this._debugObserver = null;
|
|
157
|
+
// Per-vertex RGBA color buffer mirror, updated in place when LOD colors change (avoids mesh rebuild flicker).
|
|
158
|
+
this._debugColorData = null;
|
|
159
|
+
// Signature of the per-leaf displayed LOD levels, used to skip rebuilding unchanged debug geometry.
|
|
160
|
+
this._debugSignature = 0;
|
|
161
|
+
this._disposed = false;
|
|
162
|
+
this._metadata = metadata;
|
|
163
|
+
this._rootUrl = rootUrl;
|
|
164
|
+
this._streamOptions = options;
|
|
165
|
+
// LOD heuristic parameters: take the provided values, otherwise keep the PlayCanvas-aligned defaults.
|
|
166
|
+
const maxLod = Math.max(0, metadata.lodLevels - 1);
|
|
167
|
+
this._lodRangeMax = maxLod;
|
|
168
|
+
if (options.lodBaseDistance !== undefined) {
|
|
169
|
+
this._lodBaseDistance = Math.max(0.1, options.lodBaseDistance);
|
|
170
|
+
}
|
|
171
|
+
if (options.lodMultiplier !== undefined) {
|
|
172
|
+
this._lodMultiplier = Math.max(1.2, options.lodMultiplier);
|
|
173
|
+
}
|
|
174
|
+
if (options.lodBehindPenalty !== undefined) {
|
|
175
|
+
this._lodBehindPenalty = Math.max(1, options.lodBehindPenalty);
|
|
176
|
+
}
|
|
177
|
+
if (options.lodRangeMin !== undefined) {
|
|
178
|
+
this._lodRangeMin = Math.max(0, Math.min(options.lodRangeMin, maxLod));
|
|
179
|
+
}
|
|
180
|
+
if (options.lodRangeMax !== undefined) {
|
|
181
|
+
this._lodRangeMax = Math.max(this._lodRangeMin, Math.min(options.lodRangeMax, maxLod));
|
|
182
|
+
}
|
|
183
|
+
if (options.maxDecodesPerFrame !== undefined) {
|
|
184
|
+
this._maxDecodesPerFrame = Math.max(1, options.maxDecodesPerFrame);
|
|
185
|
+
}
|
|
186
|
+
if (options.lodCooldownFrames !== undefined) {
|
|
187
|
+
this._lodCooldownFrames = Math.max(0, options.lodCooldownFrames);
|
|
188
|
+
}
|
|
189
|
+
if (options.lodUpdateInterval !== undefined) {
|
|
190
|
+
this._lodUpdateInterval = Math.max(1, options.lodUpdateInterval);
|
|
191
|
+
}
|
|
192
|
+
if (options.lodUpdateDistance !== undefined) {
|
|
193
|
+
this._lodUpdateDistance = Math.max(0, options.lodUpdateDistance);
|
|
194
|
+
}
|
|
195
|
+
if (options.maxDetailLod !== undefined) {
|
|
196
|
+
this._maxDetailLod = Math.max(0, Math.floor(options.maxDetailLod));
|
|
197
|
+
}
|
|
198
|
+
if (options.frustumCulling !== undefined) {
|
|
199
|
+
this._frustumCulling = options.frustumCulling;
|
|
200
|
+
}
|
|
201
|
+
if (options.debugLodSource) {
|
|
202
|
+
this._debugLodSource = options.debugLodSource;
|
|
203
|
+
}
|
|
204
|
+
// PlayCanvas SOG data is authored with a flipped Y; match the standard SOG loader.
|
|
205
|
+
this.scaling.y *= -1;
|
|
206
|
+
// PlayCanvas SOG LOD scenes are authored Z-up; rotate into Babylon's Y-up convention.
|
|
207
|
+
this.rotation.x = -Math.PI / 2;
|
|
208
|
+
this._collectLodEntries(metadata.tree);
|
|
209
|
+
if (options.debugDisplay) {
|
|
210
|
+
this.debugDisplay = true;
|
|
211
|
+
}
|
|
212
|
+
// Kick off streaming without blocking the caller or the render loop.
|
|
213
|
+
// eslint-disable-next-line @typescript-eslint/no-floating-promises, github/no-then
|
|
214
|
+
this._streamAllAsync().catch((e) => {
|
|
215
|
+
Logger.Error("GaussianSplattingStream: streaming failed: " + (e?.message ?? e));
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
getClassName() {
|
|
219
|
+
return "GaussianSplattingStream";
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Finest (most detailed) LOD level any node is allowed to render. `0` allows full detail (level 0);
|
|
223
|
+
* `1` caps detail at the next-coarser level, and so on. Nodes already coarser than this cap (by
|
|
224
|
+
* distance) are unaffected. Changes take effect in real time.
|
|
225
|
+
*/
|
|
226
|
+
get maxDetailLod() {
|
|
227
|
+
return this._maxDetailLod;
|
|
228
|
+
}
|
|
229
|
+
set maxDetailLod(value) {
|
|
230
|
+
const level = Math.max(0, Math.floor(value));
|
|
231
|
+
if (this._maxDetailLod === level) {
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
this._maxDetailLod = level;
|
|
235
|
+
// Re-evaluate LODs on the next frame regardless of the movement throttle so the change is immediate.
|
|
236
|
+
this._forceLodUpdate = true;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Coarsest LOD level index in the scene (number of LOD levels minus one). Useful as the upper bound
|
|
240
|
+
* for {@link maxDetailLod}.
|
|
241
|
+
*/
|
|
242
|
+
get maxLodLevel() {
|
|
243
|
+
return Math.max(0, this._metadata.lodLevels - 1);
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* When true (default), nodes whose bounding box is outside the camera frustum are biased to the coarsest
|
|
247
|
+
* LOD instead of being hidden. They stay in the sort/render set (their off-screen splats are clipped), so
|
|
248
|
+
* turning the camera toward them shows low detail immediately with no invisible frames, then refines.
|
|
249
|
+
* Changes take effect in real time.
|
|
250
|
+
*/
|
|
251
|
+
get frustumCulling() {
|
|
252
|
+
return this._frustumCulling;
|
|
253
|
+
}
|
|
254
|
+
set frustumCulling(value) {
|
|
255
|
+
if (this._frustumCulling === value) {
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
this._frustumCulling = value;
|
|
259
|
+
// Re-evaluate LODs next frame so the off-screen bias is applied/removed immediately.
|
|
260
|
+
this._forceLodUpdate = true;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* When true, renders a wireframe box per LOD node, colored by the LOD level selected by {@link debugLodSource}.
|
|
264
|
+
*/
|
|
265
|
+
get debugDisplay() {
|
|
266
|
+
return this._debugDisplay;
|
|
267
|
+
}
|
|
268
|
+
set debugDisplay(value) {
|
|
269
|
+
if (this._debugDisplay === value) {
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
this._debugDisplay = value;
|
|
273
|
+
if (value) {
|
|
274
|
+
this._refreshDebugDisplay();
|
|
275
|
+
}
|
|
276
|
+
else {
|
|
277
|
+
this._clearDebugDisplay();
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Selects which LOD value drives the debug wireframe colors: the distance-based `"optimal"` LOD
|
|
282
|
+
* (default, recomputed as the camera moves) or the `"current"` streamed/rendered LOD.
|
|
283
|
+
*/
|
|
284
|
+
get debugLodSource() {
|
|
285
|
+
return this._debugLodSource;
|
|
286
|
+
}
|
|
287
|
+
set debugLodSource(value) {
|
|
288
|
+
if (this._debugLodSource === value) {
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
this._debugLodSource = value;
|
|
292
|
+
if (this._debugDisplay) {
|
|
293
|
+
this._refreshDebugDisplay();
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
dispose(doNotRecurse) {
|
|
297
|
+
this._disposed = true;
|
|
298
|
+
if (this._lodObserver) {
|
|
299
|
+
this._scene.onBeforeRenderObservable.remove(this._lodObserver);
|
|
300
|
+
this._lodObserver = null;
|
|
301
|
+
}
|
|
302
|
+
this._clearDebugDisplay();
|
|
303
|
+
this._workBuffer?.dispose();
|
|
304
|
+
this._workBuffer = null;
|
|
305
|
+
super.dispose(doNotRecurse);
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Re-evaluates the optimal LOD for every node based on the camera position. The result is stored in
|
|
309
|
+
* each node's `optimalLod`. Rendering is unaffected; this currently drives only diagnostics and the
|
|
310
|
+
* debug wireframe display.
|
|
311
|
+
* @param camera camera to evaluate against (defaults to the scene's active camera)
|
|
312
|
+
*/
|
|
313
|
+
evaluateOptimalLods(camera = this._scene.activeCamera) {
|
|
314
|
+
if (!camera || this._leafNodes.length === 0) {
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
const maxLod = Math.max(0, this._metadata.lodLevels - 1);
|
|
318
|
+
const base = this._lodBaseDistance;
|
|
319
|
+
const mult = this._lodMultiplier;
|
|
320
|
+
const behindPenalty = this._lodBehindPenalty;
|
|
321
|
+
const rangeMin = this._lodRangeMin;
|
|
322
|
+
const rangeMax = this._lodRangeMax;
|
|
323
|
+
// FOV compensation: use min(tanHalfV, tanHalfH) so transitions stay perceptually uniform (matches PlayCanvas).
|
|
324
|
+
const aspect = this._scene.getEngine().getAspectRatio(camera) || 1;
|
|
325
|
+
let tanHalfV = Math.tan(camera.fov * 0.5);
|
|
326
|
+
if (camera.fovMode === Camera.FOVMODE_HORIZONTAL_FIXED) {
|
|
327
|
+
tanHalfV /= aspect;
|
|
328
|
+
}
|
|
329
|
+
const tanHalfH = tanHalfV * aspect;
|
|
330
|
+
const fovScale = Math.min(tanHalfV, tanHalfH) / RefTanHalfFov;
|
|
331
|
+
// Transform the camera into the mesh's local space (where the node bounds live).
|
|
332
|
+
this.computeWorldMatrix(false).invertToRef(TmpInvWorld);
|
|
333
|
+
const localCamera = Vector3.TransformCoordinatesToRef(camera.globalPosition, TmpInvWorld, TmpLocalCamera);
|
|
334
|
+
const px = localCamera.x;
|
|
335
|
+
const py = localCamera.y;
|
|
336
|
+
const pz = localCamera.z;
|
|
337
|
+
let fwx = 0;
|
|
338
|
+
let fwy = 0;
|
|
339
|
+
let fwz = 0;
|
|
340
|
+
if (behindPenalty > 1) {
|
|
341
|
+
camera.getDirectionToRef(LocalForwardAxis, TmpWorldForward);
|
|
342
|
+
const localForward = Vector3.TransformNormalToRef(TmpWorldForward, TmpInvWorld, TmpLocalForward);
|
|
343
|
+
localForward.normalize();
|
|
344
|
+
fwx = localForward.x;
|
|
345
|
+
fwy = localForward.y;
|
|
346
|
+
fwz = localForward.z;
|
|
347
|
+
}
|
|
348
|
+
for (const node of this._leafNodes) {
|
|
349
|
+
const mn = node.bound.min;
|
|
350
|
+
const mx = node.bound.max;
|
|
351
|
+
// Distance from the camera to the closest point on this node's AABB (local space).
|
|
352
|
+
const qx = px < mn[0] ? mn[0] : px > mx[0] ? mx[0] : px;
|
|
353
|
+
const qy = py < mn[1] ? mn[1] : py > mx[1] ? mx[1] : py;
|
|
354
|
+
const qz = pz < mn[2] ? mn[2] : pz > mx[2] ? mx[2] : pz;
|
|
355
|
+
const dx = qx - px;
|
|
356
|
+
const dy = qy - py;
|
|
357
|
+
const dz = qz - pz;
|
|
358
|
+
const actualDistance = Math.sqrt(dx * dx + dy * dy + dz * dz);
|
|
359
|
+
// Push nodes behind the camera toward coarser LODs when a penalty is configured.
|
|
360
|
+
let penalizedDistance = actualDistance;
|
|
361
|
+
if (behindPenalty > 1 && actualDistance > 0.01) {
|
|
362
|
+
const dotOverDistance = (fwx * dx + fwy * dy + fwz * dz) / actualDistance;
|
|
363
|
+
if (dotOverDistance < 0) {
|
|
364
|
+
penalizedDistance = actualDistance * (1 + -dotOverDistance * (behindPenalty - 1));
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
// Geometric LOD bands: threshold[k] = base * mult^(k-1).
|
|
368
|
+
const fovAdjustedDistance = penalizedDistance * fovScale;
|
|
369
|
+
let optimalLod;
|
|
370
|
+
if (maxLod === 0 || fovAdjustedDistance < base) {
|
|
371
|
+
optimalLod = 0;
|
|
372
|
+
}
|
|
373
|
+
else {
|
|
374
|
+
optimalLod = maxLod;
|
|
375
|
+
while (optimalLod > 1 && fovAdjustedDistance < base * Math.pow(mult, optimalLod - 1)) {
|
|
376
|
+
optimalLod--;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
if (optimalLod < rangeMin) {
|
|
380
|
+
optimalLod = rangeMin;
|
|
381
|
+
}
|
|
382
|
+
else if (optimalLod > rangeMax) {
|
|
383
|
+
optimalLod = rangeMax;
|
|
384
|
+
}
|
|
385
|
+
// Frustum-based LOD bias: nodes outside the camera frustum are pushed to the coarsest allowed
|
|
386
|
+
// level instead of being hidden. They stay in the render/sort set (their splats are off-screen
|
|
387
|
+
// and clipped anyway), so when the camera turns to include them they are already present at low
|
|
388
|
+
// detail with no invisible frames, then refine to the distance-optimal level.
|
|
389
|
+
if (this._frustumCulling && node.inFrustum === false) {
|
|
390
|
+
optimalLod = rangeMax;
|
|
391
|
+
}
|
|
392
|
+
node.optimalLod = optimalLod;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* The LOD level used to color a node's debug box, per {@link debugLodSource}.
|
|
397
|
+
* @param node leaf node
|
|
398
|
+
* @returns the displayed LOD level
|
|
399
|
+
*/
|
|
400
|
+
_displayedLodLevel(node) {
|
|
401
|
+
if (this._debugLodSource === "optimal") {
|
|
402
|
+
return node.optimalLod ?? node.activeLod ?? 0;
|
|
403
|
+
}
|
|
404
|
+
return node.activeLod ?? 0;
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Rebuilds the debug wireframe (evaluating the optimal LOD first when needed) and wires up the per-frame
|
|
408
|
+
* recolor observer. The observer runs for both LOD sources: "optimal" colors track the camera, and
|
|
409
|
+
* "current" colors track LOD levels as they stream in/out.
|
|
410
|
+
*/
|
|
411
|
+
_refreshDebugDisplay() {
|
|
412
|
+
if (this._debugLodSource === "optimal") {
|
|
413
|
+
this.evaluateOptimalLods();
|
|
414
|
+
}
|
|
415
|
+
this._buildDebugMesh();
|
|
416
|
+
const needsObserver = this._debugDisplay;
|
|
417
|
+
if (needsObserver && !this._debugObserver) {
|
|
418
|
+
this._debugObserver = this._scene.onBeforeRenderObservable.add(() => this._onDebugFrame());
|
|
419
|
+
}
|
|
420
|
+
else if (!needsObserver && this._debugObserver) {
|
|
421
|
+
this._scene.onBeforeRenderObservable.remove(this._debugObserver);
|
|
422
|
+
this._debugObserver = null;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
/**
|
|
426
|
+
* Per-frame debug update: recolors the existing wireframe in place whenever the displayed LOD levels
|
|
427
|
+
* change. For the "optimal" source the optimal LOD is recomputed first (it tracks the camera); for the
|
|
428
|
+
* "current" source the levels are driven by the streaming loop, so no recomputation is needed here. The
|
|
429
|
+
* geometry is never rebuilt, which avoids the dispose/recreate flicker while the camera moves.
|
|
430
|
+
*/
|
|
431
|
+
_onDebugFrame() {
|
|
432
|
+
if (this._debugLodSource === "optimal") {
|
|
433
|
+
this.evaluateOptimalLods();
|
|
434
|
+
}
|
|
435
|
+
if (this._computeDebugSignature() !== this._debugSignature) {
|
|
436
|
+
this._updateDebugColors();
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* Builds the LOD-node wireframe boxes once (one box per leaf node), colored by the displayed LOD level.
|
|
441
|
+
* The color vertex buffer is created updatable so subsequent recolors can happen in place.
|
|
442
|
+
*/
|
|
443
|
+
_buildDebugMesh() {
|
|
444
|
+
if (this._debugMesh) {
|
|
445
|
+
this._debugMesh.dispose();
|
|
446
|
+
this._debugMesh = null;
|
|
447
|
+
}
|
|
448
|
+
this._debugColorData = null;
|
|
449
|
+
const lines = [];
|
|
450
|
+
const colors = [];
|
|
451
|
+
for (const node of this._leafNodes) {
|
|
452
|
+
const color = GsLodDebugColors[this._displayedLodLevel(node) % GsLodDebugColors.length];
|
|
453
|
+
const mn = node.bound.min;
|
|
454
|
+
const mx = node.bound.max;
|
|
455
|
+
const corners = [
|
|
456
|
+
new Vector3(mn[0], mn[1], mn[2]),
|
|
457
|
+
new Vector3(mx[0], mn[1], mn[2]),
|
|
458
|
+
new Vector3(mx[0], mx[1], mn[2]),
|
|
459
|
+
new Vector3(mn[0], mx[1], mn[2]),
|
|
460
|
+
new Vector3(mn[0], mn[1], mx[2]),
|
|
461
|
+
new Vector3(mx[0], mn[1], mx[2]),
|
|
462
|
+
new Vector3(mx[0], mx[1], mx[2]),
|
|
463
|
+
new Vector3(mn[0], mx[1], mx[2]),
|
|
464
|
+
];
|
|
465
|
+
for (const edge of BoxEdges) {
|
|
466
|
+
lines.push([corners[edge[0]], corners[edge[1]]]);
|
|
467
|
+
colors.push([color, color]);
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
this._debugSignature = this._computeDebugSignature();
|
|
471
|
+
if (lines.length === 0) {
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
const mesh = CreateLineSystem(this.name + "_lodDebug", { lines, colors, updatable: true, useVertexAlpha: false }, this._scene);
|
|
475
|
+
mesh.parent = this;
|
|
476
|
+
mesh.isPickable = false;
|
|
477
|
+
mesh.doNotSerialize = true;
|
|
478
|
+
mesh.reservedDataStore = { hidden: true };
|
|
479
|
+
this._debugMesh = mesh;
|
|
480
|
+
this._debugColorData = new Float32Array(this._leafNodes.length * VerticesPerBox * 4);
|
|
481
|
+
}
|
|
482
|
+
/**
|
|
483
|
+
* Recolors the existing wireframe in place from the current displayed LOD levels, without rebuilding geometry.
|
|
484
|
+
*/
|
|
485
|
+
_updateDebugColors() {
|
|
486
|
+
if (!this._debugMesh || !this._debugColorData) {
|
|
487
|
+
return;
|
|
488
|
+
}
|
|
489
|
+
const data = this._debugColorData;
|
|
490
|
+
let offset = 0;
|
|
491
|
+
for (const node of this._leafNodes) {
|
|
492
|
+
const color = GsLodDebugColors[this._displayedLodLevel(node) % GsLodDebugColors.length];
|
|
493
|
+
for (let v = 0; v < VerticesPerBox; v++) {
|
|
494
|
+
data[offset++] = color.r;
|
|
495
|
+
data[offset++] = color.g;
|
|
496
|
+
data[offset++] = color.b;
|
|
497
|
+
data[offset++] = color.a;
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
this._debugMesh.updateVerticesData(VertexBuffer.ColorKind, data);
|
|
501
|
+
this._debugSignature = this._computeDebugSignature();
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* Computes a cheap 32-bit rolling hash of every leaf's displayed LOD level, used to detect when the
|
|
505
|
+
* debug wireframe needs recoloring. Avoids per-frame string allocation in the render loop.
|
|
506
|
+
* @returns a numeric signature of the current displayed LOD levels
|
|
507
|
+
*/
|
|
508
|
+
_computeDebugSignature() {
|
|
509
|
+
let hash = 0;
|
|
510
|
+
for (const node of this._leafNodes) {
|
|
511
|
+
hash = (hash * 31 + this._displayedLodLevel(node)) | 0;
|
|
512
|
+
}
|
|
513
|
+
return hash;
|
|
514
|
+
}
|
|
515
|
+
/**
|
|
516
|
+
* Disposes the LOD-node wireframe boxes and stops live debug updates.
|
|
517
|
+
*/
|
|
518
|
+
_clearDebugDisplay() {
|
|
519
|
+
if (this._debugObserver) {
|
|
520
|
+
this._scene.onBeforeRenderObservable.remove(this._debugObserver);
|
|
521
|
+
this._debugObserver = null;
|
|
522
|
+
}
|
|
523
|
+
if (this._debugMesh) {
|
|
524
|
+
this._debugMesh.dispose();
|
|
525
|
+
this._debugMesh = null;
|
|
526
|
+
}
|
|
527
|
+
this._debugColorData = null;
|
|
528
|
+
this._debugSignature = 0;
|
|
529
|
+
}
|
|
530
|
+
/**
|
|
531
|
+
* Walks the LOD tree and records every leaf that carries renderable LOD entries, capturing the set of
|
|
532
|
+
* available levels and the coarsest (base) level for each.
|
|
533
|
+
* @param node current tree node
|
|
534
|
+
*/
|
|
535
|
+
_collectLodEntries(node) {
|
|
536
|
+
if (node.children) {
|
|
537
|
+
for (const child of node.children) {
|
|
538
|
+
this._collectLodEntries(child);
|
|
539
|
+
}
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
if (!node.lods) {
|
|
543
|
+
return;
|
|
544
|
+
}
|
|
545
|
+
// Collect all levels that hold splats (PlayCanvas convention: level 0 is the finest, higher = coarser).
|
|
546
|
+
const levels = [];
|
|
547
|
+
for (const key of Object.keys(node.lods)) {
|
|
548
|
+
const level = Number(key);
|
|
549
|
+
const entry = node.lods[key];
|
|
550
|
+
if (Number.isFinite(level) && entry && entry.count > 0) {
|
|
551
|
+
levels.push(level);
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
if (levels.length === 0) {
|
|
555
|
+
return;
|
|
556
|
+
}
|
|
557
|
+
levels.sort((a, b) => a - b);
|
|
558
|
+
node.availableLevels = levels;
|
|
559
|
+
node.baseLod = levels[levels.length - 1];
|
|
560
|
+
node.activeLod = undefined;
|
|
561
|
+
node.lodCooldown = 0;
|
|
562
|
+
node.inFrustum = true;
|
|
563
|
+
// Local-space bounds for the per-node frustum test; the mesh world matrix is applied per evaluation.
|
|
564
|
+
node.cullBounds = new BoundingInfo(Vector3.FromArray(node.bound.min), Vector3.FromArray(node.bound.max));
|
|
565
|
+
this._leafNodes.push(node);
|
|
566
|
+
}
|
|
567
|
+
/**
|
|
568
|
+
* Streams the scene: learns every source file's splat count, allocates one unified GPU work buffer
|
|
569
|
+
* sized for all LOD files, decodes the environment and the coarsest LOD of every node as a permanent
|
|
570
|
+
* base layer, then installs the per-frame loop that streams finer LODs on demand.
|
|
571
|
+
*/
|
|
572
|
+
async _streamAllAsync() {
|
|
573
|
+
// Phase 1: learn splat counts for the environment and every referenced LOD file (cheap meta only).
|
|
574
|
+
const fileIds = this._collectAllFileIds();
|
|
575
|
+
const envCount = await this._gatherCountsAsync(fileIds);
|
|
576
|
+
if (this._disposed) {
|
|
577
|
+
return;
|
|
578
|
+
}
|
|
579
|
+
// Phase 2: assign fixed work-buffer offsets (environment first, then every file) and allocate.
|
|
580
|
+
// Index 0 is reserved as a never-decoded padding splat: the sort worker and index buffer pad unused
|
|
581
|
+
// slots with index 0, and leaving that slot zeroed (center.w = 0 => zero covariance, alpha 0) makes
|
|
582
|
+
// the padding invisible instead of ghosting a copy of the first real splat.
|
|
583
|
+
let capacity = 1;
|
|
584
|
+
if (envCount > 0) {
|
|
585
|
+
this._environmentRange = { offset: capacity, count: envCount };
|
|
586
|
+
capacity += envCount;
|
|
587
|
+
}
|
|
588
|
+
for (const fileId of fileIds) {
|
|
589
|
+
const count = this._fileCounts.get(fileId);
|
|
590
|
+
if (count === undefined || count <= 0) {
|
|
591
|
+
continue;
|
|
592
|
+
}
|
|
593
|
+
this._fileBaseSplat.set(fileId, capacity);
|
|
594
|
+
capacity += count;
|
|
595
|
+
}
|
|
596
|
+
if (capacity <= 1) {
|
|
597
|
+
return;
|
|
598
|
+
}
|
|
599
|
+
this._workBuffer = new GaussianSplattingWorkBuffer(this._scene, capacity);
|
|
600
|
+
// GPU readback is only enabled after it is validated against a CPU decode on the first file (see
|
|
601
|
+
// _probeReadbackAsync); until then positions are decoded on the CPU so there is always a correct result.
|
|
602
|
+
this._readbackCandidate = this._workBuffer.supportsAsyncCentersReadback;
|
|
603
|
+
const splatPositions = new Float32Array(capacity * 4);
|
|
604
|
+
const textures = this._workBuffer.textures;
|
|
605
|
+
this._setExternalWorkBuffer(textures[0], textures[1], textures[2], textures[3], splatPositions, capacity);
|
|
606
|
+
// Nothing is active until at least one resource has been decoded.
|
|
607
|
+
this.setSplatIndexRanges([]);
|
|
608
|
+
this.setEnabled(true);
|
|
609
|
+
// Phase 3: decode the environment, then every node's coarsest LOD as the permanent base layer.
|
|
610
|
+
if (this._environmentRange && this._environmentFiles) {
|
|
611
|
+
await this._decodeEnvironmentAsync();
|
|
612
|
+
}
|
|
613
|
+
this._environmentFiles = null;
|
|
614
|
+
const baseFiles = new Set();
|
|
615
|
+
for (const node of this._leafNodes) {
|
|
616
|
+
const entry = node.lods[String(node.baseLod)];
|
|
617
|
+
if (entry && this._fileBaseSplat.has(entry.file)) {
|
|
618
|
+
baseFiles.add(entry.file);
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
for (const fileId of Array.from(baseFiles)) {
|
|
622
|
+
if (this._disposed) {
|
|
623
|
+
return;
|
|
624
|
+
}
|
|
625
|
+
// eslint-disable-next-line no-await-in-loop
|
|
626
|
+
await this._decodeFileAsync(fileId);
|
|
627
|
+
}
|
|
628
|
+
if (this._disposed) {
|
|
629
|
+
return;
|
|
630
|
+
}
|
|
631
|
+
// Phase 4: hand off to the per-frame LOD streaming loop.
|
|
632
|
+
this._baseLayerReady = true;
|
|
633
|
+
if (!this._lodObserver) {
|
|
634
|
+
this._lodObserver = this._scene.onBeforeRenderObservable.add(() => this._onLodFrame());
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
/**
|
|
638
|
+
* Collects the unique set of source file indices referenced by any LOD of any leaf, sorted ascending.
|
|
639
|
+
* @returns sorted unique file indices
|
|
640
|
+
*/
|
|
641
|
+
_collectAllFileIds() {
|
|
642
|
+
const ids = new Set();
|
|
643
|
+
for (const node of this._leafNodes) {
|
|
644
|
+
for (const level of node.availableLevels) {
|
|
645
|
+
const entry = node.lods[String(level)];
|
|
646
|
+
if (entry) {
|
|
647
|
+
ids.add(entry.file);
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
return Array.from(ids).sort((a, b) => a - b);
|
|
652
|
+
}
|
|
653
|
+
/**
|
|
654
|
+
* Fetches the environment bundle and every referenced file's metadata to learn splat counts, caching
|
|
655
|
+
* each file's parsed metadata for the later on-demand decode. Metadata fetches run in parallel.
|
|
656
|
+
* @param fileIds file indices to fetch metadata for
|
|
657
|
+
* @returns the environment splat count (0 when there is no environment)
|
|
658
|
+
*/
|
|
659
|
+
async _gatherCountsAsync(fileIds) {
|
|
660
|
+
let envCount = 0;
|
|
661
|
+
if (this._metadata.environment) {
|
|
662
|
+
try {
|
|
663
|
+
const url = this._rootUrl + this._metadata.environment;
|
|
664
|
+
const buffer = (await Tools.LoadFileAsync(url, true));
|
|
665
|
+
const files = await this._unzipAsync(new Uint8Array(buffer));
|
|
666
|
+
const metaBytes = files.get("meta.json");
|
|
667
|
+
if (metaBytes) {
|
|
668
|
+
const meta = JSON.parse(new TextDecoder().decode(metaBytes));
|
|
669
|
+
envCount = GaussianSplattingStream._GetSplatCount(meta);
|
|
670
|
+
this._environmentFiles = files;
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
catch (e) {
|
|
674
|
+
// The environment is non-essential — keep streaming the LOD tree even if it fails.
|
|
675
|
+
Logger.Warn("GaussianSplattingStream: failed to load environment: " + (e?.message ?? e));
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
await Promise.all(fileIds.map(async (fileId) => {
|
|
679
|
+
const relativePath = this._metadata.filenames[fileId];
|
|
680
|
+
if (!relativePath) {
|
|
681
|
+
Logger.Warn(`GaussianSplattingStream: missing filename for file index ${fileId}.`);
|
|
682
|
+
return;
|
|
683
|
+
}
|
|
684
|
+
try {
|
|
685
|
+
const metaUrl = this._rootUrl + relativePath;
|
|
686
|
+
const subRootUrl = metaUrl.substring(0, metaUrl.lastIndexOf("/") + 1);
|
|
687
|
+
const metaText = (await Tools.LoadFileAsync(metaUrl, false));
|
|
688
|
+
const sogData = JSON.parse(metaText);
|
|
689
|
+
this._fileCounts.set(fileId, GaussianSplattingStream._GetSplatCount(sogData));
|
|
690
|
+
this._fileMeta.set(fileId, { sogData, subRootUrl });
|
|
691
|
+
}
|
|
692
|
+
catch (e) {
|
|
693
|
+
Logger.Warn(`GaussianSplattingStream: failed to load metadata for ${relativePath}: ${e?.message ?? e}`);
|
|
694
|
+
}
|
|
695
|
+
}));
|
|
696
|
+
return envCount;
|
|
697
|
+
}
|
|
698
|
+
/**
|
|
699
|
+
* Queues a file for on-demand decode if it isn't already decoded, in flight, or already queued.
|
|
700
|
+
* @param fileId file index to decode
|
|
701
|
+
*/
|
|
702
|
+
_enqueueDecode(fileId) {
|
|
703
|
+
if (this._decodedFiles.has(fileId) || this._loadingFiles.has(fileId) || !this._fileMeta.has(fileId)) {
|
|
704
|
+
return;
|
|
705
|
+
}
|
|
706
|
+
if (this._decodeQueue.indexOf(fileId) === -1) {
|
|
707
|
+
this._decodeQueue.push(fileId);
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
/**
|
|
711
|
+
* Starts up to {@link _maxDecodesPerFrame} queued decodes for this frame. Decodes run asynchronously
|
|
712
|
+
* and promote any waiting nodes once they complete.
|
|
713
|
+
*/
|
|
714
|
+
_pumpDecodeQueue() {
|
|
715
|
+
let started = 0;
|
|
716
|
+
while (this._decodeQueue.length > 0 && started < this._maxDecodesPerFrame) {
|
|
717
|
+
const fileId = this._decodeQueue.shift();
|
|
718
|
+
if (this._decodedFiles.has(fileId) || this._loadingFiles.has(fileId)) {
|
|
719
|
+
continue;
|
|
720
|
+
}
|
|
721
|
+
started++;
|
|
722
|
+
// eslint-disable-next-line github/no-then
|
|
723
|
+
this._decodeFileAsync(fileId).catch((e) => {
|
|
724
|
+
Logger.Warn("GaussianSplattingStream: decode failed: " + (e?.message ?? e));
|
|
725
|
+
});
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
/**
|
|
729
|
+
* Writes a decoded splat range's positions into the shared buffer, expands the bounds, and incrementally
|
|
730
|
+
* patches the sort worker.
|
|
731
|
+
* @param positions stride-4 positions for the range
|
|
732
|
+
* @param base first splat index of the range in the work buffer
|
|
733
|
+
* @param count number of splats in the range
|
|
734
|
+
*/
|
|
735
|
+
_applyPositions(positions, base, count) {
|
|
736
|
+
this._splatPositions.set(positions, base * 4);
|
|
737
|
+
this._updateBounds(positions, count);
|
|
738
|
+
// Incrementally patch only this range in the sort worker (avoids the full position-buffer re-copy).
|
|
739
|
+
this._postWorkerPositionsRange(base, count);
|
|
740
|
+
}
|
|
741
|
+
/**
|
|
742
|
+
* One-time validation of GPU position readback: reads a sample of the just-decoded range back from the work
|
|
743
|
+
* buffer and compares it to the CPU-decoded positions. Enables {@link _useGpuPositionReadback} only on an
|
|
744
|
+
* exact (within float tolerance) match, so an unsupported or incorrect readback (e.g. a backend without the
|
|
745
|
+
* required texture usage, or an orientation mismatch) safely keeps the CPU decode path.
|
|
746
|
+
* @param base first splat index of the validated range
|
|
747
|
+
* @param count number of splats in the range
|
|
748
|
+
* @param cpuPositions the CPU-decoded stride-4 positions for the range (ground truth)
|
|
749
|
+
*/
|
|
750
|
+
async _probeReadbackAsync(base, count, cpuPositions) {
|
|
751
|
+
this._readbackProbed = true;
|
|
752
|
+
if (!this._workBuffer) {
|
|
753
|
+
return;
|
|
754
|
+
}
|
|
755
|
+
const sampleCount = Math.min(count, 1024);
|
|
756
|
+
let ok = false;
|
|
757
|
+
try {
|
|
758
|
+
const gpu = await this._workBuffer.readCentersRangeAsync(base, sampleCount);
|
|
759
|
+
if (this._disposed) {
|
|
760
|
+
return;
|
|
761
|
+
}
|
|
762
|
+
if (gpu && gpu.length >= sampleCount * 4) {
|
|
763
|
+
ok = true;
|
|
764
|
+
for (let i = 0; i < sampleCount && ok; i++) {
|
|
765
|
+
for (let j = 0; j < 3; j++) {
|
|
766
|
+
const a = gpu[i * 4 + j];
|
|
767
|
+
const b = cpuPositions[i * 4 + j];
|
|
768
|
+
if (Math.abs(a - b) > 1e-2 * (1 + Math.abs(b))) {
|
|
769
|
+
ok = false;
|
|
770
|
+
break;
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
catch {
|
|
777
|
+
ok = false;
|
|
778
|
+
}
|
|
779
|
+
this._useGpuPositionReadback = ok;
|
|
780
|
+
Logger.Log(ok
|
|
781
|
+
? "GaussianSplattingStream: GPU position readback validated; streamed LOD positions are read back from the GPU."
|
|
782
|
+
: "GaussianSplattingStream: GPU position readback unavailable; decoding LOD positions on the CPU.");
|
|
783
|
+
}
|
|
784
|
+
/**
|
|
785
|
+
* Resolves the decoded positions for a splat range and applies them. Once GPU readback has been validated,
|
|
786
|
+
* positions are read back from the work buffer (non-blocking) and `pack.positions` is empty; otherwise the
|
|
787
|
+
* CPU-decoded `pack.positions` are used, and — on the first such decode — the GPU readback is validated
|
|
788
|
+
* against them so subsequent decodes can use the fast path.
|
|
789
|
+
* @param pack the parsed SOG pack (its `positions` is populated only on the CPU path)
|
|
790
|
+
* @param base first splat index of the range in the work buffer
|
|
791
|
+
* @param count number of splats in the range
|
|
792
|
+
* @returns whether positions were applied
|
|
793
|
+
*/
|
|
794
|
+
async _applyDecodedPositionsAsync(pack, base, count) {
|
|
795
|
+
if (this._useGpuPositionReadback && this._workBuffer) {
|
|
796
|
+
const positions = await this._workBuffer.readCentersRangeAsync(base, count);
|
|
797
|
+
if (this._disposed) {
|
|
798
|
+
return false;
|
|
799
|
+
}
|
|
800
|
+
if (positions && this._splatPositions) {
|
|
801
|
+
this._applyPositions(positions, base, count);
|
|
802
|
+
return true;
|
|
803
|
+
}
|
|
804
|
+
// Validated readback unexpectedly returned nothing; fall through to the (likely empty) CPU positions.
|
|
805
|
+
}
|
|
806
|
+
const cpu = pack.positions.length >= count * 4 ? pack.positions.subarray(0, count * 4) : null;
|
|
807
|
+
if (!cpu || !this._splatPositions) {
|
|
808
|
+
return false;
|
|
809
|
+
}
|
|
810
|
+
this._applyPositions(cpu, base, count);
|
|
811
|
+
// First CPU decode while readback is a candidate: validate it so later decodes can use the fast path.
|
|
812
|
+
if (!this._readbackProbed && this._readbackCandidate) {
|
|
813
|
+
await this._probeReadbackAsync(base, count, cpu);
|
|
814
|
+
}
|
|
815
|
+
return true;
|
|
816
|
+
}
|
|
817
|
+
/**
|
|
818
|
+
* Decodes the always-on environment bundle into its work-buffer block and activates its range.
|
|
819
|
+
*/
|
|
820
|
+
async _decodeEnvironmentAsync() {
|
|
821
|
+
if (!this._environmentRange || !this._environmentFiles || !this._workBuffer) {
|
|
822
|
+
return;
|
|
823
|
+
}
|
|
824
|
+
const range = this._environmentRange;
|
|
825
|
+
try {
|
|
826
|
+
const parsed = await ParseSogMetaAsTextures(this._environmentFiles, "", this._scene, !this._useGpuPositionReadback);
|
|
827
|
+
const pack = parsed.sogTextures;
|
|
828
|
+
if (!pack) {
|
|
829
|
+
return;
|
|
830
|
+
}
|
|
831
|
+
try {
|
|
832
|
+
if (this._disposed || !this._workBuffer) {
|
|
833
|
+
return;
|
|
834
|
+
}
|
|
835
|
+
await this._workBuffer.decodeAsync(pack, range.offset);
|
|
836
|
+
if (this._disposed) {
|
|
837
|
+
return;
|
|
838
|
+
}
|
|
839
|
+
await this._applyDecodedPositionsAsync(pack, range.offset, range.count);
|
|
840
|
+
if (this._disposed) {
|
|
841
|
+
return;
|
|
842
|
+
}
|
|
843
|
+
this._refreshActiveRanges();
|
|
844
|
+
}
|
|
845
|
+
finally {
|
|
846
|
+
// Always release the GPU source textures (the decode pass is the only consumer).
|
|
847
|
+
GaussianSplattingStream._DisposePack(pack);
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
catch (e) {
|
|
851
|
+
Logger.Warn("GaussianSplattingStream: failed to decode environment: " + (e?.message ?? e));
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
/**
|
|
855
|
+
* Loads one LOD source file as GPU textures, decodes it into its fixed work-buffer block, records its
|
|
856
|
+
* CPU centers for sorting, frees the source textures, then promotes any nodes that were waiting for it.
|
|
857
|
+
* Concurrent or repeat requests for the same file are ignored.
|
|
858
|
+
* @param fileId file index to decode
|
|
859
|
+
*/
|
|
860
|
+
async _decodeFileAsync(fileId) {
|
|
861
|
+
if (this._decodedFiles.has(fileId) || this._loadingFiles.has(fileId)) {
|
|
862
|
+
return;
|
|
863
|
+
}
|
|
864
|
+
const meta = this._fileMeta.get(fileId);
|
|
865
|
+
const base = this._fileBaseSplat.get(fileId);
|
|
866
|
+
const count = this._fileCounts.get(fileId);
|
|
867
|
+
if (!meta || base === undefined || count === undefined) {
|
|
868
|
+
return;
|
|
869
|
+
}
|
|
870
|
+
this._loadingFiles.add(fileId);
|
|
871
|
+
try {
|
|
872
|
+
const parsed = await ParseSogMetaAsTextures(meta.sogData, meta.subRootUrl, this._scene, !this._useGpuPositionReadback);
|
|
873
|
+
const pack = parsed.sogTextures;
|
|
874
|
+
if (!pack) {
|
|
875
|
+
return;
|
|
876
|
+
}
|
|
877
|
+
try {
|
|
878
|
+
if (this._disposed || !this._workBuffer) {
|
|
879
|
+
return;
|
|
880
|
+
}
|
|
881
|
+
await this._workBuffer.decodeAsync(pack, base);
|
|
882
|
+
if (this._disposed) {
|
|
883
|
+
return;
|
|
884
|
+
}
|
|
885
|
+
await this._applyDecodedPositionsAsync(pack, base, count);
|
|
886
|
+
if (this._disposed) {
|
|
887
|
+
return;
|
|
888
|
+
}
|
|
889
|
+
this._decodedFiles.add(fileId);
|
|
890
|
+
// Promote any nodes that can now reach their desired LOD via this newly decoded file.
|
|
891
|
+
if (this._applyDesiredLods()) {
|
|
892
|
+
this._refreshActiveRanges();
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
finally {
|
|
896
|
+
// Always release the GPU source textures (the decode pass is the only consumer).
|
|
897
|
+
GaussianSplattingStream._DisposePack(pack);
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
finally {
|
|
901
|
+
this._loadingFiles.delete(fileId);
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
/**
|
|
905
|
+
* Snaps a desired LOD level to the nearest level the node provides, while never selecting a level finer
|
|
906
|
+
* than {@link maxDetailLod} (i.e. with an index below the cap). Ties prefer the finer allowed level. If
|
|
907
|
+
* the node has no level at or coarser than the cap, its coarsest available level is used.
|
|
908
|
+
* @param node leaf node
|
|
909
|
+
* @param desired desired LOD level
|
|
910
|
+
* @returns the chosen available level
|
|
911
|
+
*/
|
|
912
|
+
_cappedLevelForNode(node, desired) {
|
|
913
|
+
const levels = node.availableLevels;
|
|
914
|
+
const floor = this._maxDetailLod;
|
|
915
|
+
let best = -1;
|
|
916
|
+
let bestDiff = Number.POSITIVE_INFINITY;
|
|
917
|
+
for (const level of levels) {
|
|
918
|
+
if (level < floor) {
|
|
919
|
+
continue;
|
|
920
|
+
}
|
|
921
|
+
const diff = Math.abs(level - desired);
|
|
922
|
+
if (diff < bestDiff) {
|
|
923
|
+
best = level;
|
|
924
|
+
bestDiff = diff;
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
// No level is coarse enough to satisfy the cap: fall back to the coarsest the node has.
|
|
928
|
+
return best < 0 ? node.baseLod : best;
|
|
929
|
+
}
|
|
930
|
+
/**
|
|
931
|
+
* Computes each node's {@link ISOGLODNode.targetLevel}: the distance-based optimal level snapped to an
|
|
932
|
+
* available level, capped so no node renders finer (more detailed) than {@link maxDetailLod}.
|
|
933
|
+
*/
|
|
934
|
+
_computeTargetLevels() {
|
|
935
|
+
for (const node of this._leafNodes) {
|
|
936
|
+
const desired = node.optimalLod ?? node.baseLod;
|
|
937
|
+
node.targetLevel = this._cappedLevelForNode(node, desired);
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
/**
|
|
941
|
+
* Applies each node's {@link ISOGLODNode.targetLevel}: switches a node to its target level when that
|
|
942
|
+
* level's file is already decoded, otherwise queues the file and leaves the node on its current LOD (so
|
|
943
|
+
* nothing ever disappears). Nodes within their post-switch cooldown are left untouched to damp oscillation.
|
|
944
|
+
* @returns true when at least one node changed LOD (callers should refresh the active ranges)
|
|
945
|
+
*/
|
|
946
|
+
_applyDesiredLods() {
|
|
947
|
+
let dirty = false;
|
|
948
|
+
for (const node of this._leafNodes) {
|
|
949
|
+
if (node.lodCooldown && node.lodCooldown > 0) {
|
|
950
|
+
continue;
|
|
951
|
+
}
|
|
952
|
+
const desired = node.targetLevel ?? node.baseLod;
|
|
953
|
+
if (desired === node.activeLod) {
|
|
954
|
+
continue;
|
|
955
|
+
}
|
|
956
|
+
const entry = node.lods[String(desired)];
|
|
957
|
+
if (!entry) {
|
|
958
|
+
continue;
|
|
959
|
+
}
|
|
960
|
+
if (this._decodedFiles.has(entry.file)) {
|
|
961
|
+
node.activeLod = desired;
|
|
962
|
+
node.lodCooldown = this._lodCooldownFrames;
|
|
963
|
+
dirty = true;
|
|
964
|
+
}
|
|
965
|
+
else {
|
|
966
|
+
this._enqueueDecode(entry.file);
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
return dirty;
|
|
970
|
+
}
|
|
971
|
+
/**
|
|
972
|
+
* Per-frame LOD streaming loop. Ticks cooldowns and pumps the decode queue every frame, and runs the
|
|
973
|
+
* cheap per-node frustum test every frame so the off-screen LOD bias tracks camera rotation. The LOD
|
|
974
|
+
* re-evaluation is throttled to at most every {@link _lodUpdateInterval} frames once the camera has
|
|
975
|
+
* translated far enough, but also runs immediately whenever a node enters/leaves the frustum (so its
|
|
976
|
+
* detail upgrades/downgrades promptly) or a cap change forces it. Active ranges rebuild on any LOD change.
|
|
977
|
+
*/
|
|
978
|
+
_onLodFrame() {
|
|
979
|
+
if (this._disposed || !this._baseLayerReady) {
|
|
980
|
+
return;
|
|
981
|
+
}
|
|
982
|
+
for (const node of this._leafNodes) {
|
|
983
|
+
if (node.lodCooldown && node.lodCooldown > 0) {
|
|
984
|
+
node.lodCooldown--;
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
// In-flight/queued decodes still progress every frame.
|
|
988
|
+
this._pumpDecodeQueue();
|
|
989
|
+
// Per-node frustum test runs every frame (cheap) so the off-screen LOD bias tracks camera rotation,
|
|
990
|
+
// not just the translation that gates the throttled LOD re-evaluation below.
|
|
991
|
+
const frustumChanged = this._updateNodeFrustum();
|
|
992
|
+
let runLodEval = this._forceLodUpdate || frustumChanged;
|
|
993
|
+
if (!runLodEval && ++this._framesSinceLodUpdate >= this._lodUpdateInterval) {
|
|
994
|
+
const camera = this._scene.activeCamera;
|
|
995
|
+
const threshold = this._lodUpdateDistance;
|
|
996
|
+
if (!camera || Vector3.DistanceSquared(camera.globalPosition, this._lastLodCamPos) >= threshold * threshold) {
|
|
997
|
+
if (camera) {
|
|
998
|
+
this._lastLodCamPos.copyFrom(camera.globalPosition);
|
|
999
|
+
}
|
|
1000
|
+
runLodEval = true;
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
if (runLodEval) {
|
|
1004
|
+
this._forceLodUpdate = false;
|
|
1005
|
+
this._framesSinceLodUpdate = 0;
|
|
1006
|
+
this.evaluateOptimalLods(this._scene.activeCamera);
|
|
1007
|
+
this._computeTargetLevels();
|
|
1008
|
+
if (this._applyDesiredLods()) {
|
|
1009
|
+
this._refreshActiveRanges();
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
/**
|
|
1014
|
+
* Updates each leaf node's {@link ISOGLODNode.inFrustum} flag from a per-node frustum test against the
|
|
1015
|
+
* active camera. When {@link frustumCulling} is disabled (or there is no camera) every node is marked
|
|
1016
|
+
* in-frustum. Bounds are static (from the LOD tree), so flags are valid for all nodes regardless of
|
|
1017
|
+
* decode state. Returns true when any node's in-frustum state changed (so the LOD bias must be re-applied).
|
|
1018
|
+
* @returns whether any node's in-frustum state changed
|
|
1019
|
+
*/
|
|
1020
|
+
_updateNodeFrustum() {
|
|
1021
|
+
const camera = this._scene.activeCamera;
|
|
1022
|
+
let changed = false;
|
|
1023
|
+
if (!this._frustumCulling || !camera) {
|
|
1024
|
+
for (const node of this._leafNodes) {
|
|
1025
|
+
if (node.inFrustum === false) {
|
|
1026
|
+
node.inFrustum = true;
|
|
1027
|
+
changed = true;
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
return changed;
|
|
1031
|
+
}
|
|
1032
|
+
// World-space frustum planes from the current view-projection, tested against each node's world AABB.
|
|
1033
|
+
// force=false uses the renderId/sync fast-path (still recomputes when the transform actually changed),
|
|
1034
|
+
// avoiding a full world-matrix recompute every frame for the per-node frustum test.
|
|
1035
|
+
const world = this.computeWorldMatrix(false);
|
|
1036
|
+
camera.getViewMatrix().multiplyToRef(camera.getProjectionMatrix(), this._cullViewProj);
|
|
1037
|
+
Frustum.GetPlanesToRef(this._cullViewProj, this._frustumPlanes);
|
|
1038
|
+
for (const node of this._leafNodes) {
|
|
1039
|
+
node.cullBounds.update(world);
|
|
1040
|
+
const inFrustum = node.cullBounds.isInFrustum(this._frustumPlanes);
|
|
1041
|
+
if (inFrustum !== node.inFrustum) {
|
|
1042
|
+
node.inFrustum = inFrustum;
|
|
1043
|
+
changed = true;
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
return changed;
|
|
1047
|
+
}
|
|
1048
|
+
/**
|
|
1049
|
+
* Reads the splat count from SOG metadata.
|
|
1050
|
+
* @param data SOG metadata
|
|
1051
|
+
* @returns the splat count
|
|
1052
|
+
*/
|
|
1053
|
+
static _GetSplatCount(data) {
|
|
1054
|
+
return data.count ?? (Array.isArray(data.means.shape) ? data.means.shape[0] : 0);
|
|
1055
|
+
}
|
|
1056
|
+
/**
|
|
1057
|
+
* Disposes all GPU source textures of a SOG pack (they are only needed for the one decode pass).
|
|
1058
|
+
* @param pack the SOG texture pack
|
|
1059
|
+
*/
|
|
1060
|
+
static _DisposePack(pack) {
|
|
1061
|
+
pack.meansTextureL.dispose();
|
|
1062
|
+
pack.meansTextureU.dispose();
|
|
1063
|
+
pack.scalesTexture.dispose();
|
|
1064
|
+
pack.quatsTexture.dispose();
|
|
1065
|
+
pack.sh0Texture.dispose();
|
|
1066
|
+
pack.shCentroidsTexture?.dispose();
|
|
1067
|
+
pack.shLabelsTexture?.dispose();
|
|
1068
|
+
pack.codebookTexture?.dispose();
|
|
1069
|
+
}
|
|
1070
|
+
/**
|
|
1071
|
+
* Expands the running splat-center bounds with a newly decoded file's centers and updates the
|
|
1072
|
+
* mesh bounding info so the GS is correctly frustum-culled and pickable.
|
|
1073
|
+
* @param positions stride-4 splat centers for the new file
|
|
1074
|
+
* @param count number of splats
|
|
1075
|
+
*/
|
|
1076
|
+
_updateBounds(positions, count) {
|
|
1077
|
+
const min = this._boundsMin;
|
|
1078
|
+
const max = this._boundsMax;
|
|
1079
|
+
for (let i = 0; i < count; i++) {
|
|
1080
|
+
const x = positions[i * 4 + 0];
|
|
1081
|
+
const y = positions[i * 4 + 1];
|
|
1082
|
+
const z = positions[i * 4 + 2];
|
|
1083
|
+
min.minimizeInPlaceFromFloats(x, y, z);
|
|
1084
|
+
max.maximizeInPlaceFromFloats(x, y, z);
|
|
1085
|
+
}
|
|
1086
|
+
this.setBoundingInfo(new BoundingInfo(min, max));
|
|
1087
|
+
}
|
|
1088
|
+
/**
|
|
1089
|
+
* Rebuilds the active interval set from the environment plus each node's currently-selected LOD entry,
|
|
1090
|
+
* coalesces adjacent ranges, and pushes the result to the sort worker.
|
|
1091
|
+
*/
|
|
1092
|
+
_refreshActiveRanges() {
|
|
1093
|
+
const ranges = [];
|
|
1094
|
+
if (this._environmentRange) {
|
|
1095
|
+
ranges.push({ offset: this._environmentRange.offset, count: this._environmentRange.count });
|
|
1096
|
+
}
|
|
1097
|
+
for (const node of this._leafNodes) {
|
|
1098
|
+
if (node.activeLod === undefined) {
|
|
1099
|
+
continue;
|
|
1100
|
+
}
|
|
1101
|
+
const entry = node.lods[String(node.activeLod)];
|
|
1102
|
+
if (!entry) {
|
|
1103
|
+
continue;
|
|
1104
|
+
}
|
|
1105
|
+
const base = this._fileBaseSplat.get(entry.file);
|
|
1106
|
+
if (base === undefined) {
|
|
1107
|
+
continue;
|
|
1108
|
+
}
|
|
1109
|
+
ranges.push({ offset: base + entry.offset, count: entry.count });
|
|
1110
|
+
}
|
|
1111
|
+
this.setSplatIndexRanges(GaussianSplattingStream._CoalesceRanges(ranges));
|
|
1112
|
+
}
|
|
1113
|
+
/**
|
|
1114
|
+
* Sorts and merges adjacent/overlapping ranges to keep the interval list compact.
|
|
1115
|
+
* @param ranges raw ranges
|
|
1116
|
+
* @returns coalesced ranges
|
|
1117
|
+
*/
|
|
1118
|
+
static _CoalesceRanges(ranges) {
|
|
1119
|
+
if (ranges.length <= 1) {
|
|
1120
|
+
return ranges;
|
|
1121
|
+
}
|
|
1122
|
+
const sorted = ranges.slice().sort((a, b) => a.offset - b.offset);
|
|
1123
|
+
const merged = [{ offset: sorted[0].offset, count: sorted[0].count }];
|
|
1124
|
+
for (let i = 1; i < sorted.length; i++) {
|
|
1125
|
+
const last = merged[merged.length - 1];
|
|
1126
|
+
const range = sorted[i];
|
|
1127
|
+
const lastEnd = last.offset + last.count;
|
|
1128
|
+
if (range.offset <= lastEnd) {
|
|
1129
|
+
const end = Math.max(lastEnd, range.offset + range.count);
|
|
1130
|
+
last.count = end - last.offset;
|
|
1131
|
+
}
|
|
1132
|
+
else {
|
|
1133
|
+
merged.push({ offset: range.offset, count: range.count });
|
|
1134
|
+
}
|
|
1135
|
+
}
|
|
1136
|
+
return merged;
|
|
1137
|
+
}
|
|
1138
|
+
/**
|
|
1139
|
+
* Unzips a `.sog` bundle into a name -> bytes map, loading fflate on demand.
|
|
1140
|
+
* @param data zipped bytes
|
|
1141
|
+
* @returns map of entry name to bytes
|
|
1142
|
+
*/
|
|
1143
|
+
async _unzipAsync(data) {
|
|
1144
|
+
let fflateModule = this._streamOptions.fflate;
|
|
1145
|
+
if (!fflateModule) {
|
|
1146
|
+
if (typeof window.fflate === "undefined") {
|
|
1147
|
+
await Tools.LoadScriptAsync(this._streamOptions.deflateURL ?? "https://unpkg.com/fflate/umd/index.js");
|
|
1148
|
+
}
|
|
1149
|
+
fflateModule = window.fflate;
|
|
1150
|
+
}
|
|
1151
|
+
const unzipped = fflateModule.unzipSync(data);
|
|
1152
|
+
const files = new Map();
|
|
1153
|
+
for (const [name, content] of Object.entries(unzipped)) {
|
|
1154
|
+
files.set(name, content);
|
|
1155
|
+
}
|
|
1156
|
+
return files;
|
|
1157
|
+
}
|
|
1158
|
+
}
|
|
1159
|
+
//# sourceMappingURL=gaussianSplattingStream.js.map
|