@null3d/splats 0.0.0-stage → 0.0.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/LICENSE-APACHE +176 -0
- package/LICENSE-MIT +21 -0
- package/README.md +55 -2
- package/dist/splats.wasm +0 -0
- package/lib/chunks.d.ts +74 -0
- package/lib/chunks.js +170 -0
- package/lib/coverage.d.ts +18 -0
- package/lib/coverage.js +208 -0
- package/lib/feature.d.ts +10 -0
- package/lib/feature.js +39 -0
- package/lib/format.d.ts +35 -0
- package/lib/format.js +53 -0
- package/lib/gltf.d.ts +9 -0
- package/lib/gltf.js +174 -0
- package/lib/index.d.ts +11 -0
- package/lib/index.js +10 -0
- package/lib/pack.d.ts +34 -0
- package/lib/pack.js +60 -0
- package/lib/select-reference.d.ts +12 -0
- package/lib/select-reference.js +103 -0
- package/lib/select-task.d.ts +58 -0
- package/lib/select-task.js +198 -0
- package/lib/select.d.ts +34 -0
- package/lib/select.js +121 -0
- package/lib/splats-mesh.wgsl-material-glsl-0-7c70ea7f8bc68f80.js +24 -0
- package/lib/splats-mesh.wgsl-material-glsl-1-4c5238f07ce504f6.js +24 -0
- package/lib/splats-mesh.wgsl-material-glsl-2-a1c90d44f96d6edc.js +24 -0
- package/lib/splats-mesh.wgsl-material-glsl-3-71d6a8492ec15318.js +24 -0
- package/lib/splats-mesh.wgsl-material-wgsl-0-e3d76625f2ac6140.js +24 -0
- package/lib/splats-mesh.wgsl-material-wgsl-2-a79d411b03e48a8f.js +24 -0
- package/lib/splats-mesh.wgsl.d.ts +6 -0
- package/lib/splats-mesh.wgsl.js +36 -0
- package/lib/splats.wgsl-material-glsl-0-820a413bdc6c214f.js +24 -0
- package/lib/splats.wgsl-material-glsl-1-218c97fea0e0021a.js +24 -0
- package/lib/splats.wgsl-material-glsl-2-a01ede5b80d9c4ec.js +24 -0
- package/lib/splats.wgsl-material-glsl-3-a38e648847680581.js +24 -0
- package/lib/splats.wgsl-material-glsl-64-284224c0525054d5.js +24 -0
- package/lib/splats.wgsl-material-glsl-65-fed0d228f3996b85.js +24 -0
- package/lib/splats.wgsl-material-glsl-66-5e88b8d27331ba9b.js +24 -0
- package/lib/splats.wgsl-material-glsl-67-b8d5a4ba09da08f2.js +24 -0
- package/lib/splats.wgsl-material-wgsl-0-7d98def36b682735.js +24 -0
- package/lib/splats.wgsl-material-wgsl-2-2117fc0ae3c904bc.js +24 -0
- package/lib/splats.wgsl-material-wgsl-64-646c09348ffc2153.js +24 -0
- package/lib/splats.wgsl-material-wgsl-66-16bc1350a6ccb2f9.js +24 -0
- package/lib/splats.wgsl.d.ts +14 -0
- package/lib/splats.wgsl.js +42 -0
- package/lib/spz-write.d.ts +9 -0
- package/lib/spz-write.js +133 -0
- package/lib/spz.d.ts +9 -0
- package/lib/spz.js +240 -0
- package/lib/stream.d.ts +126 -0
- package/lib/stream.js +304 -0
- package/lib/system.d.ts +110 -0
- package/lib/system.js +476 -0
- package/lib/version.d.ts +1 -0
- package/lib/version.js +3 -0
- package/package.json +54 -5
package/lib/system.d.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { type FeatureHandle, type InstanceBatch, type Material, type SketchContext, type Vec3Like } from '@null3d/engine';
|
|
2
|
+
import { type SplatData } from './format.js';
|
|
3
|
+
import './gltf.js';
|
|
4
|
+
/**
|
|
5
|
+
* Options of `createSplats`. Give the cloud itself, or the address of a splat file or a stream
|
|
6
|
+
* index.
|
|
7
|
+
*
|
|
8
|
+
* @category api/splats
|
|
9
|
+
*/
|
|
10
|
+
export interface SplatsOptions {
|
|
11
|
+
/** The cloud to draw, decoded already, such as `decodeSpz` gives it. */
|
|
12
|
+
readonly data?: SplatData;
|
|
13
|
+
/** The address of an SPZ file or of a splat stream index to download and draw. */
|
|
14
|
+
readonly url?: string | URL;
|
|
15
|
+
/**
|
|
16
|
+
* The point that the splats' positions are relative to, which keeps them precise far from the
|
|
17
|
+
* world's origin. The default is (0, 0, 0).
|
|
18
|
+
*/
|
|
19
|
+
readonly origin?: Vec3Like;
|
|
20
|
+
/**
|
|
21
|
+
* How far in front of an opaque mesh a splat fades in, in meters. The default is 0.25, which
|
|
22
|
+
* softens the edge where a splat meets a wall or the ground. 0 keeps a hard edge.
|
|
23
|
+
*/
|
|
24
|
+
readonly softness?: number;
|
|
25
|
+
/** How many standard deviations of a splat its card covers. The default is 3.3. */
|
|
26
|
+
readonly extent?: number;
|
|
27
|
+
/**
|
|
28
|
+
* The most splats that the batch holds, which caps every preset's budget. The default is the
|
|
29
|
+
* preset's own count on this device, or the cloud's own count where that is smaller, so a
|
|
30
|
+
* cloud allocates what the device may draw.
|
|
31
|
+
*/
|
|
32
|
+
readonly capacity?: number;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The draw that a cloud fills: the instance batch whose rows are the splats that draw. Write the
|
|
36
|
+
* rows, mark them, and set the number that draw. The draw's material is the add-on's shader.
|
|
37
|
+
*
|
|
38
|
+
* @category api/splats
|
|
39
|
+
*/
|
|
40
|
+
export interface SplatsDraw {
|
|
41
|
+
/** The batch whose rows place the splats. Write its arrays, then mark the rows that draw. */
|
|
42
|
+
readonly batch: InstanceBatch;
|
|
43
|
+
/** The draw's material, which the cloud makes. */
|
|
44
|
+
readonly material: Material;
|
|
45
|
+
/** Draws the first `count` rows, and hides the rest. */
|
|
46
|
+
setCount(count: number): void;
|
|
47
|
+
/** Frees the rows, the material and the shape. */
|
|
48
|
+
destroy(): void;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* A cloud of Gaussian splats that a scene draws, with its levels and its budget.
|
|
52
|
+
*
|
|
53
|
+
* @category api/splats
|
|
54
|
+
*/
|
|
55
|
+
export interface Splats {
|
|
56
|
+
/** The add-on feature, whose settings, budget and figures the engine resolves. */
|
|
57
|
+
readonly feature: FeatureHandle;
|
|
58
|
+
/** The draw: its batch's rows are the splats that draw. */
|
|
59
|
+
readonly draw: SplatsDraw;
|
|
60
|
+
/** The number of splats that the cloud holds. */
|
|
61
|
+
readonly total: number;
|
|
62
|
+
/** The number of splats that draw now. */
|
|
63
|
+
readonly drawn: number;
|
|
64
|
+
/** The number of chunks that the cloud has. */
|
|
65
|
+
readonly chunks: number;
|
|
66
|
+
/** The number of chunks whose data has loaded. */
|
|
67
|
+
readonly loaded: number;
|
|
68
|
+
/** The number of chunks that stayed unreachable. */
|
|
69
|
+
readonly failed: number;
|
|
70
|
+
/** The bytes of the whole cloud: the file's size, or the chunk sizes that a stream index gives. */
|
|
71
|
+
readonly bytes: number;
|
|
72
|
+
/** The bytes of the cloud that have downloaded. */
|
|
73
|
+
readonly loadedBytes: number;
|
|
74
|
+
/**
|
|
75
|
+
* Resolves when the cloud can draw its first view. For a file, every chunk is ready. For a
|
|
76
|
+
* stream, the nearest chunks that the last `update` needs are loaded, about a megabyte of files
|
|
77
|
+
* or the nearest chunk alone, and the rest keep loading behind the frames: call `update` once
|
|
78
|
+
* before awaiting it.
|
|
79
|
+
*/
|
|
80
|
+
readonly ready: Promise<void>;
|
|
81
|
+
/**
|
|
82
|
+
* Resolves once the splats that draw are chosen and packed, which a test or a still picture
|
|
83
|
+
* waits for. For a stream, it also waits for the chunks that the selection wants, so the view
|
|
84
|
+
* draws every part that it asks for. Call it after `update`.
|
|
85
|
+
*/
|
|
86
|
+
settle(): Promise<void>;
|
|
87
|
+
/** Caps the splats that draw below the preset's budget, as a device or a test needs. */
|
|
88
|
+
setBudget(count: number): void;
|
|
89
|
+
/** Chooses the splats that draw from a camera's position. Call it once in each frame. */
|
|
90
|
+
update(eye: Vec3Like): void;
|
|
91
|
+
/** Stops the draw and frees its memory. The glTF reader stays registered. */
|
|
92
|
+
destroy(): void;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Draws a cloud of Gaussian splats in a sketch. The cloud comes from an SPZ file, from a splat
|
|
96
|
+
* stream index, or from the data of another reader. A glTF model that carries
|
|
97
|
+
* `KHR_gaussian_splatting` loads through `assets.loadGltf`, because the add-on's module registers
|
|
98
|
+
* the reader as it loads. A cloud draws with detail levels, a budget that follows the quality
|
|
99
|
+
* preset and the GPU path, and a soft mix with the scene's opaque meshes.
|
|
100
|
+
*
|
|
101
|
+
* A splat stream starts drawing before its whole cloud has downloaded: give the address of its
|
|
102
|
+
* index, and the add-on fetches the chunks that the view needs as it needs them. Each chunk of the
|
|
103
|
+
* cloud is an SPZ file, and the index names them.
|
|
104
|
+
*
|
|
105
|
+
* Call `splats.update(eye)` once in each frame, with the camera's position from `getPosition`, and
|
|
106
|
+
* free the cloud with `splats.destroy()`.
|
|
107
|
+
*
|
|
108
|
+
* @category api/splats
|
|
109
|
+
*/
|
|
110
|
+
export declare function createSplats(ctx: SketchContext, options: SplatsOptions): Promise<Splats>;
|
package/lib/system.js
ADDED
|
@@ -0,0 +1,476 @@
|
|
|
1
|
+
// The splats of a scene, drawn through the engine's feature draw: one instance batch whose rows are
|
|
2
|
+
// the splats that draw, a surface function that turns a row into a soft Gaussian card, and a
|
|
3
|
+
// selection that a job worker makes each time the camera or the budget changes.
|
|
4
|
+
//
|
|
5
|
+
// The cloud is cut into chunks. Each chunk keeps its splats in importance order, so a prefix of the
|
|
6
|
+
// list is a coarser level of it. A job worker chooses a prefix for each chunk by its distance and by
|
|
7
|
+
// the preset's budget, in the add-on's Rust module, and the core packs the chosen splats into the
|
|
8
|
+
// batch in its own memory. The engine then culls and sorts the rows back to front in the transparent
|
|
9
|
+
// pass, which is what lets the splats mix with each other and with the scene's opaque meshes.
|
|
10
|
+
//
|
|
11
|
+
// A file that a page downloads whole is cut as the bytes arrive. A splat stream names its chunks in
|
|
12
|
+
// a small index, so the cloud starts at once and the loader fetches only the chunks that the
|
|
13
|
+
// selection asks for, nearest first.
|
|
14
|
+
import { checkAddonVersion, declareAddonNamespace, FeatureHost, } from '@null3d/engine';
|
|
15
|
+
import { chunkSplats } from './chunks.js';
|
|
16
|
+
import { splatsFeature } from './feature.js';
|
|
17
|
+
import { checkSplats, splatsError } from './format.js';
|
|
18
|
+
// The module registers the glTF extension as the add-on loads.
|
|
19
|
+
import './gltf.js';
|
|
20
|
+
import { SplatSelector } from './select.js';
|
|
21
|
+
import splatShader from './splats.wgsl.js';
|
|
22
|
+
import { firstViewCount, parseStreamIndex, SplatStreamLoader, startsStreamIndex, } from './stream.js';
|
|
23
|
+
import { VERSION } from './version.js';
|
|
24
|
+
checkAddonVersion('@null3d/splats', VERSION);
|
|
25
|
+
/** The add-on's names, claimed once as the sketch's module loads. */
|
|
26
|
+
const NAMESPACE = declareAddonNamespace({
|
|
27
|
+
name: 'splats',
|
|
28
|
+
package: '@null3d/splats',
|
|
29
|
+
errorCodes: [2100, 2199],
|
|
30
|
+
});
|
|
31
|
+
/** The bytes of one batch row: its place, rotation, scales and color. */
|
|
32
|
+
const ROW_BYTES = (3 + 4 + 3 + 4) * 4;
|
|
33
|
+
/** One quad, which every row of the batch repeats. */
|
|
34
|
+
const QUAD = {
|
|
35
|
+
positions: new Float32Array([-1, -1, 0, 1, -1, 0, 1, 1, 0, -1, 1, 0]),
|
|
36
|
+
normals: new Float32Array([0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1]),
|
|
37
|
+
uvs: new Float32Array([0, 0, 1, 0, 1, 1, 0, 1]),
|
|
38
|
+
indices: new Uint16Array([0, 1, 2, 0, 2, 3]),
|
|
39
|
+
};
|
|
40
|
+
/** How far the camera moves before a selection is made again, in meters. */
|
|
41
|
+
const MOVE_EPSILON = 0.25;
|
|
42
|
+
/**
|
|
43
|
+
* Draws a cloud of Gaussian splats in a sketch. The cloud comes from an SPZ file, from a splat
|
|
44
|
+
* stream index, or from the data of another reader. A glTF model that carries
|
|
45
|
+
* `KHR_gaussian_splatting` loads through `assets.loadGltf`, because the add-on's module registers
|
|
46
|
+
* the reader as it loads. A cloud draws with detail levels, a budget that follows the quality
|
|
47
|
+
* preset and the GPU path, and a soft mix with the scene's opaque meshes.
|
|
48
|
+
*
|
|
49
|
+
* A splat stream starts drawing before its whole cloud has downloaded: give the address of its
|
|
50
|
+
* index, and the add-on fetches the chunks that the view needs as it needs them. Each chunk of the
|
|
51
|
+
* cloud is an SPZ file, and the index names them.
|
|
52
|
+
*
|
|
53
|
+
* Call `splats.update(eye)` once in each frame, with the camera's position from `getPosition`, and
|
|
54
|
+
* free the cloud with `splats.destroy()`.
|
|
55
|
+
*
|
|
56
|
+
* @category api/splats
|
|
57
|
+
*/
|
|
58
|
+
export async function createSplats(ctx, options) {
|
|
59
|
+
if (options.data === undefined && options.url === undefined)
|
|
60
|
+
throw splatsError(2110, 'give createSplats() the cloud as data, or the url of an SPZ file or a splat stream index.');
|
|
61
|
+
const source = options.data !== undefined
|
|
62
|
+
? { data: checkSplats(options.data, 'the splat cloud'), bytes: 0 }
|
|
63
|
+
: await readSource(options.url);
|
|
64
|
+
const data = 'data' in source ? source.data : undefined;
|
|
65
|
+
const stream = 'stream' in source ? source.stream : undefined;
|
|
66
|
+
const totalSplats = data ? data.count : stream.count;
|
|
67
|
+
// The slots that the selection reads: a stream names all of them at once, and a file adds them
|
|
68
|
+
// as its cut makes them.
|
|
69
|
+
const slots = [];
|
|
70
|
+
if (stream)
|
|
71
|
+
for (const chunk of stream.chunks)
|
|
72
|
+
slots.push({
|
|
73
|
+
center: chunk.center,
|
|
74
|
+
radius: chunk.radius,
|
|
75
|
+
count: chunk.count,
|
|
76
|
+
arrived: false,
|
|
77
|
+
});
|
|
78
|
+
// The size of each chunk's file, which decides the chunks that the first view waits for.
|
|
79
|
+
const chunkBytes = stream
|
|
80
|
+
? Float64Array.from(stream.chunks, (chunk) => chunk.bytes)
|
|
81
|
+
: new Float64Array(0);
|
|
82
|
+
// The rows that the batch holds, which the preset and the GPU path decide once the host has
|
|
83
|
+
// resolved them: a phone then pays for the splats that it may draw, not for the Ultra count.
|
|
84
|
+
let capacity = 1;
|
|
85
|
+
const description = splatsFeature(() => capacity * ROW_BYTES + QUAD.positions.byteLength * 4);
|
|
86
|
+
const host = new FeatureHost({
|
|
87
|
+
quality: ctx.quality,
|
|
88
|
+
tier: ctx.engine.capabilities.tier,
|
|
89
|
+
page: ctx.page,
|
|
90
|
+
namespace: NAMESPACE,
|
|
91
|
+
});
|
|
92
|
+
const feature = host.add(description);
|
|
93
|
+
capacity = Math.max(1, Math.min(Math.floor(options.capacity ?? feature.settings.drawn), Math.max(totalSplats, 1)));
|
|
94
|
+
// The rows that the batch holds decide the memory that the feature reports, and they are known
|
|
95
|
+
// only after the link resolved the preset's count.
|
|
96
|
+
feature.refresh();
|
|
97
|
+
const draw = await ctx.scene.addFeatureInstances(description, {
|
|
98
|
+
shader: splatShader,
|
|
99
|
+
vertices: QUAD,
|
|
100
|
+
instances: capacity,
|
|
101
|
+
rows: { colors: true },
|
|
102
|
+
origin: options.origin,
|
|
103
|
+
uniforms: {
|
|
104
|
+
extent: options.extent ?? 3.3,
|
|
105
|
+
blur: 0,
|
|
106
|
+
softness: options.softness ?? 0.25,
|
|
107
|
+
cutoff: 1 / 255,
|
|
108
|
+
},
|
|
109
|
+
alphaMode: 'blend',
|
|
110
|
+
depthWrite: false,
|
|
111
|
+
doubleSided: true,
|
|
112
|
+
sceneInputs: ['sceneDepth'],
|
|
113
|
+
});
|
|
114
|
+
// A splat's card reaches past its center, so its rows need a sphere that holds it.
|
|
115
|
+
draw.batch.setBoundingRadius(4);
|
|
116
|
+
if (!draw.batch.colors)
|
|
117
|
+
throw splatsError(2111, 'the splats batch has no row colors, which its draw needs.');
|
|
118
|
+
// The writer stores each chunk in the core's memory and packs the chosen prefixes into the
|
|
119
|
+
// batch's rows there, so a frame copies no rows through the sketch's heap.
|
|
120
|
+
const writer = await draw.batch.createChunkRows();
|
|
121
|
+
// A new batch draws every row, whose colors and places are still zero: draw none until the
|
|
122
|
+
// first selection fills them.
|
|
123
|
+
draw.setCount(0);
|
|
124
|
+
let version = 0;
|
|
125
|
+
let selector;
|
|
126
|
+
let selectorChunks = 0;
|
|
127
|
+
// The counts and the total of the selection that fills the draw, which keeps its own copy so a
|
|
128
|
+
// later selection in the shared buffer can differ from it without allocating.
|
|
129
|
+
let lastCounts = new Float32Array(stream ? slots.length : 0);
|
|
130
|
+
let lastTotal = -1;
|
|
131
|
+
/** The chunk version that the rows hold, so a chunk that arrives repacks them once. */
|
|
132
|
+
let packedVersion = -1;
|
|
133
|
+
// The camera's position at the last ask, in a typed array, so a frame's ask allocates nothing.
|
|
134
|
+
const eye = new Float64Array(3);
|
|
135
|
+
const lastEye = new Float64Array(3);
|
|
136
|
+
let lastBudget = -1;
|
|
137
|
+
let dirty = true;
|
|
138
|
+
let inFlight = false;
|
|
139
|
+
let stop = false;
|
|
140
|
+
let override;
|
|
141
|
+
let drawn = 0;
|
|
142
|
+
let loadedChunks = 0;
|
|
143
|
+
let reported;
|
|
144
|
+
/** Packs the chosen prefixes into the batch's rows, in the core's memory. */
|
|
145
|
+
const pack = (counts) => {
|
|
146
|
+
drawn = writer.pack(counts);
|
|
147
|
+
packedVersion = version;
|
|
148
|
+
};
|
|
149
|
+
/** The ask that waits for its answer, and the chunk version that it saw. */
|
|
150
|
+
let asking;
|
|
151
|
+
let askedVersion = 0;
|
|
152
|
+
/** The chunks that the stream should fetch, nearest first, in buffers that a selection reuses. */
|
|
153
|
+
const wanted = [];
|
|
154
|
+
const wantedDistance = [];
|
|
155
|
+
/** The nearest wanted chunks that the first view waits for, and whether it has drawn. */
|
|
156
|
+
const firstView = [];
|
|
157
|
+
let firstViewDone = false;
|
|
158
|
+
/**
|
|
159
|
+
* Asks the loader for the chunks that the selection wants and that have not arrived, nearest
|
|
160
|
+
* first. A chunk behind the camera still counts: the budget decides what the view needs, and
|
|
161
|
+
* the distance decides what it needs first.
|
|
162
|
+
*/
|
|
163
|
+
const askForChunks = (loader, counts) => {
|
|
164
|
+
wanted.length = 0;
|
|
165
|
+
wantedDistance.length = 0;
|
|
166
|
+
const eyeX = eye[0];
|
|
167
|
+
const eyeY = eye[1];
|
|
168
|
+
const eyeZ = eye[2];
|
|
169
|
+
for (let i = 0; i < slots.length; i++) {
|
|
170
|
+
const slot = slots[i];
|
|
171
|
+
if (slot.arrived || !(i < counts.length && counts[i] > 0))
|
|
172
|
+
continue;
|
|
173
|
+
const dx = slot.center[0] - eyeX;
|
|
174
|
+
const dy = slot.center[1] - eyeY;
|
|
175
|
+
const dz = slot.center[2] - eyeZ;
|
|
176
|
+
const distance = Math.max(Math.sqrt(dx * dx + dy * dy + dz * dz) - slot.radius, 0);
|
|
177
|
+
// Keep the list nearest first, so the closest chunk's splats arrive first.
|
|
178
|
+
let at = wanted.length;
|
|
179
|
+
while (at > 0 && wantedDistance[at - 1] > distance) {
|
|
180
|
+
wanted[at] = wanted[at - 1];
|
|
181
|
+
wantedDistance[at] = wantedDistance[at - 1];
|
|
182
|
+
at--;
|
|
183
|
+
}
|
|
184
|
+
wanted[at] = i;
|
|
185
|
+
wantedDistance[at] = distance;
|
|
186
|
+
}
|
|
187
|
+
// The first view draws from the nearest chunks, and the rest keep loading behind the frames
|
|
188
|
+
// that draw, so a large cloud never leaves the screen empty while its whole first view loads.
|
|
189
|
+
firstView.length = 0;
|
|
190
|
+
if (!firstViewDone && wanted.length > 0) {
|
|
191
|
+
const count = firstViewCount(wanted, chunkBytes);
|
|
192
|
+
for (let k = 0; k < count; k++)
|
|
193
|
+
firstView.push(wanted[k]);
|
|
194
|
+
}
|
|
195
|
+
loader.load(wanted);
|
|
196
|
+
};
|
|
197
|
+
let loader;
|
|
198
|
+
let settledOnce = false;
|
|
199
|
+
let readyResolved = false;
|
|
200
|
+
let resolveReady = () => undefined;
|
|
201
|
+
const streamReady = new Promise((resolve) => {
|
|
202
|
+
resolveReady = resolve;
|
|
203
|
+
});
|
|
204
|
+
/** True when the chunks that the first view waits for have arrived, or have stayed unreachable. */
|
|
205
|
+
const firstViewLoaded = () => {
|
|
206
|
+
if (loader === undefined)
|
|
207
|
+
return true;
|
|
208
|
+
if (firstView.length === 0)
|
|
209
|
+
return loader.idle;
|
|
210
|
+
for (const index of firstView)
|
|
211
|
+
if (!loader.settled(index))
|
|
212
|
+
return false;
|
|
213
|
+
return true;
|
|
214
|
+
};
|
|
215
|
+
const maybeReady = () => {
|
|
216
|
+
if (readyResolved)
|
|
217
|
+
return;
|
|
218
|
+
if (!settledOnce || !firstViewLoaded())
|
|
219
|
+
return;
|
|
220
|
+
readyResolved = true;
|
|
221
|
+
firstViewDone = true;
|
|
222
|
+
resolveReady();
|
|
223
|
+
};
|
|
224
|
+
/** Starts the loader's requests, and resolves the stream's readiness once they settle. */
|
|
225
|
+
const loadFor = (counts) => {
|
|
226
|
+
if (loader)
|
|
227
|
+
askForChunks(loader, counts);
|
|
228
|
+
maybeReady();
|
|
229
|
+
};
|
|
230
|
+
/** True when the camera has moved far enough that the last selection no longer fits it. */
|
|
231
|
+
const movedSinceLastSelection = () => Math.abs(eye[0] - lastEye[0]) +
|
|
232
|
+
Math.abs(eye[1] - lastEye[1]) +
|
|
233
|
+
Math.abs(eye[2] - lastEye[2]) >
|
|
234
|
+
MOVE_EPSILON;
|
|
235
|
+
/**
|
|
236
|
+
* Packs a chunk that arrived with the last selection's counts, unless the camera has moved: a
|
|
237
|
+
* chunk that the view no longer wants takes no row, and the next selection places the rest.
|
|
238
|
+
*/
|
|
239
|
+
const packArrived = () => {
|
|
240
|
+
if (lastTotal < 0 || lastCounts.length !== slots.length)
|
|
241
|
+
return;
|
|
242
|
+
if (inFlight || movedSinceLastSelection())
|
|
243
|
+
return;
|
|
244
|
+
pack(lastCounts);
|
|
245
|
+
};
|
|
246
|
+
/** The callers that wait for a settled selection, which the next one answers. */
|
|
247
|
+
const waiters = [];
|
|
248
|
+
const settleWaiters = () => {
|
|
249
|
+
if (inFlight || dirty || waiters.length === 0)
|
|
250
|
+
return;
|
|
251
|
+
if (loader) {
|
|
252
|
+
if (!loader.idle)
|
|
253
|
+
return;
|
|
254
|
+
// A chunk that arrived while the camera had moved waits for a selection that packs it.
|
|
255
|
+
if (packedVersion !== version) {
|
|
256
|
+
dirty = true;
|
|
257
|
+
choose();
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
for (const resolve of waiters.splice(0))
|
|
262
|
+
resolve();
|
|
263
|
+
};
|
|
264
|
+
const budgetNow = () => Math.min(capacity, Math.floor(override ?? feature.settings.drawn));
|
|
265
|
+
/** Takes the answer: fills the draw again when the counts or the total changed. */
|
|
266
|
+
const selected = (answer) => {
|
|
267
|
+
inFlight = false;
|
|
268
|
+
const asked = asking;
|
|
269
|
+
asking = undefined;
|
|
270
|
+
if (stop || !asked)
|
|
271
|
+
return;
|
|
272
|
+
// Without shared memory, the worker answered with a copy, which the sketch writes in.
|
|
273
|
+
if (answer.counts)
|
|
274
|
+
asked.counts.set(answer.counts);
|
|
275
|
+
const total = answer.total;
|
|
276
|
+
const changed = total !== lastTotal || version !== packedVersion || differs(asked.counts, lastCounts);
|
|
277
|
+
if (changed) {
|
|
278
|
+
lastTotal = total;
|
|
279
|
+
// A chunk that arrived while the worker chose changed the list's length.
|
|
280
|
+
if (lastCounts.length !== asked.counts.length)
|
|
281
|
+
lastCounts = new Float32Array(asked.counts.length);
|
|
282
|
+
lastCounts.set(asked.counts);
|
|
283
|
+
pack(asked.counts);
|
|
284
|
+
}
|
|
285
|
+
settledOnce = true;
|
|
286
|
+
// A new file chunk joins the selector, so the next selection includes it. A stream's
|
|
287
|
+
// chunks are all known from its index, and an arrival repacks the rows on its own.
|
|
288
|
+
dirty = stream ? false : version !== askedVersion;
|
|
289
|
+
if (stream)
|
|
290
|
+
loadFor(asked.counts);
|
|
291
|
+
settleWaiters();
|
|
292
|
+
// A caller that waits for a settled set keeps the selections coming.
|
|
293
|
+
if (waiters.length > 0)
|
|
294
|
+
choose();
|
|
295
|
+
};
|
|
296
|
+
/** Gives up on the selection, and reports the first failure. */
|
|
297
|
+
const failed = (error) => {
|
|
298
|
+
inFlight = false;
|
|
299
|
+
dirty = false;
|
|
300
|
+
asking = undefined;
|
|
301
|
+
if (reported === undefined) {
|
|
302
|
+
reported = error instanceof Error ? error : new Error(String(error));
|
|
303
|
+
console.error(`splats: the selection failed. ${reported.message}`);
|
|
304
|
+
}
|
|
305
|
+
settledOnce = true;
|
|
306
|
+
maybeReady();
|
|
307
|
+
settleWaiters();
|
|
308
|
+
};
|
|
309
|
+
const choose = () => {
|
|
310
|
+
if (stop || !selector || inFlight)
|
|
311
|
+
return;
|
|
312
|
+
const budget = budgetNow();
|
|
313
|
+
const moved = movedSinceLastSelection();
|
|
314
|
+
if (!dirty && !moved && budget === lastBudget)
|
|
315
|
+
return;
|
|
316
|
+
inFlight = true;
|
|
317
|
+
askedVersion = version;
|
|
318
|
+
asking = selector;
|
|
319
|
+
lastEye[0] = eye[0];
|
|
320
|
+
lastEye[1] = eye[1];
|
|
321
|
+
lastEye[2] = eye[2];
|
|
322
|
+
lastBudget = budget;
|
|
323
|
+
selector.select(eye, budget).then(selected, failed);
|
|
324
|
+
};
|
|
325
|
+
/** Makes the selector read every chunk that the cloud has, when the list's length changed. */
|
|
326
|
+
const syncSelector = () => {
|
|
327
|
+
if (slots.length === selectorChunks)
|
|
328
|
+
return;
|
|
329
|
+
selectorChunks = slots.length;
|
|
330
|
+
// The core's chunk table grows with the cloud, so a file's later chunks take their places.
|
|
331
|
+
writer.reserve(selectorChunks);
|
|
332
|
+
const previous = selector;
|
|
333
|
+
// The worker keeps the previous selector's arrays for this one while they hold the chunks.
|
|
334
|
+
selector = selectorChunks > 0 ? new SplatSelector(slots, previous?.id) : undefined;
|
|
335
|
+
if (!selector)
|
|
336
|
+
previous?.drop();
|
|
337
|
+
// A new selector has counts of its own, and the draw keeps the last set until the worker
|
|
338
|
+
// answers for this one.
|
|
339
|
+
lastCounts = new Float32Array(selectorChunks);
|
|
340
|
+
lastTotal = -1;
|
|
341
|
+
dirty = true;
|
|
342
|
+
};
|
|
343
|
+
let ready;
|
|
344
|
+
if (stream) {
|
|
345
|
+
loader = new SplatStreamLoader(stream, {
|
|
346
|
+
decode: decodeChunk,
|
|
347
|
+
onChunk: (index, chunk) => {
|
|
348
|
+
const slot = slots[index];
|
|
349
|
+
// The rows move into the core, so the slot keeps only the answer that they arrived.
|
|
350
|
+
slot.arrived = true;
|
|
351
|
+
writer.write(index, chunk);
|
|
352
|
+
loadedChunks++;
|
|
353
|
+
version++;
|
|
354
|
+
packArrived();
|
|
355
|
+
maybeReady();
|
|
356
|
+
},
|
|
357
|
+
onFailed: (index, error) => {
|
|
358
|
+
console.error(`splats: chunk ${index} of the splat stream did not load, so that part of the cloud stays empty. ${error.message}`);
|
|
359
|
+
maybeReady();
|
|
360
|
+
},
|
|
361
|
+
onIdle: () => {
|
|
362
|
+
maybeReady();
|
|
363
|
+
settleWaiters();
|
|
364
|
+
},
|
|
365
|
+
});
|
|
366
|
+
ready = streamReady;
|
|
367
|
+
syncSelector();
|
|
368
|
+
// A sketch usually names its view in the same turn as this call returns, so the first
|
|
369
|
+
// selection waits a turn: it then asks for that view's chunks instead of the origin's. A
|
|
370
|
+
// sketch that never calls update still gets a selection, and its ready resolves.
|
|
371
|
+
setTimeout(() => {
|
|
372
|
+
if (!stop && !settledOnce)
|
|
373
|
+
choose();
|
|
374
|
+
}, 0);
|
|
375
|
+
}
|
|
376
|
+
else {
|
|
377
|
+
const cut = chunkSplats(data, {
|
|
378
|
+
yieldTo: () => new Promise((resolve) => setTimeout(resolve, 0)),
|
|
379
|
+
onChunk: (chunk) => {
|
|
380
|
+
writer.write(slots.length, chunk);
|
|
381
|
+
slots.push({
|
|
382
|
+
center: chunk.center,
|
|
383
|
+
radius: chunk.radius,
|
|
384
|
+
count: chunk.count,
|
|
385
|
+
arrived: true,
|
|
386
|
+
});
|
|
387
|
+
loadedChunks++;
|
|
388
|
+
version++;
|
|
389
|
+
dirty = true;
|
|
390
|
+
},
|
|
391
|
+
}).then(() => undefined);
|
|
392
|
+
ready = cut;
|
|
393
|
+
}
|
|
394
|
+
return {
|
|
395
|
+
feature,
|
|
396
|
+
draw: draw,
|
|
397
|
+
total: totalSplats,
|
|
398
|
+
ready,
|
|
399
|
+
get drawn() {
|
|
400
|
+
return drawn;
|
|
401
|
+
},
|
|
402
|
+
get chunks() {
|
|
403
|
+
return slots.length;
|
|
404
|
+
},
|
|
405
|
+
get loaded() {
|
|
406
|
+
return loadedChunks;
|
|
407
|
+
},
|
|
408
|
+
get failed() {
|
|
409
|
+
return loader?.failed ?? 0;
|
|
410
|
+
},
|
|
411
|
+
bytes: source.bytes,
|
|
412
|
+
get loadedBytes() {
|
|
413
|
+
return loader ? loader.receivedBytes : source.bytes;
|
|
414
|
+
},
|
|
415
|
+
setBudget(count) {
|
|
416
|
+
override = Math.max(0, Math.floor(count));
|
|
417
|
+
dirty = true;
|
|
418
|
+
},
|
|
419
|
+
settle() {
|
|
420
|
+
return new Promise((resolve) => {
|
|
421
|
+
waiters.push(resolve);
|
|
422
|
+
choose();
|
|
423
|
+
settleWaiters();
|
|
424
|
+
});
|
|
425
|
+
},
|
|
426
|
+
update(next) {
|
|
427
|
+
eye[0] = next[0] ?? 0;
|
|
428
|
+
eye[1] = next[1] ?? 0;
|
|
429
|
+
eye[2] = next[2] ?? 0;
|
|
430
|
+
syncSelector();
|
|
431
|
+
choose();
|
|
432
|
+
},
|
|
433
|
+
destroy() {
|
|
434
|
+
stop = true;
|
|
435
|
+
loader?.abort();
|
|
436
|
+
selector?.drop();
|
|
437
|
+
writer.destroy();
|
|
438
|
+
host.destroy();
|
|
439
|
+
draw.destroy();
|
|
440
|
+
},
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
/** Reads one chunk file of a stream, which is an SPZ file. */
|
|
444
|
+
async function decodeChunk(bytes) {
|
|
445
|
+
const { decodeSpz } = await import('./spz.js');
|
|
446
|
+
return decodeSpz(bytes);
|
|
447
|
+
}
|
|
448
|
+
/** True when two lists of chunk counts differ, in length or in any count. */
|
|
449
|
+
function differs(a, b) {
|
|
450
|
+
if (a.length !== b.length)
|
|
451
|
+
return true;
|
|
452
|
+
for (let i = 0; i < a.length; i++)
|
|
453
|
+
if (a[i] !== b[i])
|
|
454
|
+
return true;
|
|
455
|
+
return false;
|
|
456
|
+
}
|
|
457
|
+
/**
|
|
458
|
+
* Downloads a cloud. A stream index is a small JSON file, so it arrives at once. Any other file is
|
|
459
|
+
* an SPZ file, which the reader decodes whole.
|
|
460
|
+
*/
|
|
461
|
+
async function readSource(url) {
|
|
462
|
+
const address = url instanceof URL
|
|
463
|
+
? url
|
|
464
|
+
: new URL(url, globalThis.location?.href ?? 'http://localhost/');
|
|
465
|
+
const response = await fetch(address);
|
|
466
|
+
if (!response.ok)
|
|
467
|
+
throw splatsError(2109, `the splat file ${address} answered ${response.status}. Give the address of an SPZ file or a splat stream index that the page may read.`);
|
|
468
|
+
const bytes = new Uint8Array(await response.arrayBuffer());
|
|
469
|
+
if (startsStreamIndex(bytes)) {
|
|
470
|
+
const text = new TextDecoder().decode(bytes);
|
|
471
|
+
const stream = parseStreamIndex(text, address, `the splat stream ${address}`);
|
|
472
|
+
return { stream, bytes: stream.bytes };
|
|
473
|
+
}
|
|
474
|
+
const { decodeSpz } = await import('./spz.js');
|
|
475
|
+
return { data: await decodeSpz(bytes), bytes: bytes.byteLength };
|
|
476
|
+
}
|
package/lib/version.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const VERSION = "0.0.0";
|
package/lib/version.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,55 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@null3d/splats",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Gaussian splats for null3D sketches: SPZ and KHR_gaussian_splatting files, detail levels, a per-device budget and depth mixing with meshes, built on the engine's public API.",
|
|
5
|
+
"license": "MIT OR Apache-2.0",
|
|
6
|
+
"author": "Ramesh Nair (https://hiddentao.com)",
|
|
7
|
+
"homepage": "https://github.com/null3d-engine/null3d#readme",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/null3d-engine/null3d.git",
|
|
11
|
+
"directory": "packages/splats"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/null3d-engine/null3d/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"null3d",
|
|
18
|
+
"3d",
|
|
19
|
+
"splats",
|
|
20
|
+
"gaussian-splatting",
|
|
21
|
+
"spz",
|
|
22
|
+
"khr-gaussian-splatting",
|
|
23
|
+
"webgpu",
|
|
24
|
+
"webgl2"
|
|
25
|
+
],
|
|
26
|
+
"type": "module",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"null3d-source": "./src/index.ts",
|
|
30
|
+
"types": "./lib/index.d.ts",
|
|
31
|
+
"default": "./lib/index.js"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"lib",
|
|
36
|
+
"dist",
|
|
37
|
+
"LICENSE-MIT",
|
|
38
|
+
"LICENSE-APACHE"
|
|
39
|
+
],
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public",
|
|
42
|
+
"provenance": true
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"prepack": "bun ../../tools/build-package.ts splats"
|
|
46
|
+
},
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"@null3d/engine": "0.0.0"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@null3d/engine": "0.0.0",
|
|
52
|
+
"@null3d/vite-plugin": "0.0.0",
|
|
53
|
+
"@webgpu/types": "0.1.74"
|
|
54
|
+
}
|
|
55
|
+
}
|