@null3d/splats 0.0.0-stage → 0.1.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/THIRD-PARTY-NOTICES.txt +63 -0
- 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 +65 -0
- package/lib/select-task.js +200 -0
- package/lib/select.d.ts +41 -0
- package/lib/select.js +145 -0
- package/lib/splats-mesh.wgsl-material-glsl-0-01097b38ab28a3d5.js +24 -0
- package/lib/splats-mesh.wgsl-material-glsl-1-3f27ff0f14b7cdd2.js +24 -0
- package/lib/splats-mesh.wgsl-material-glsl-2-d471bfaf3a580318.js +24 -0
- package/lib/splats-mesh.wgsl-material-glsl-3-e048b0d077060953.js +24 -0
- package/lib/splats-mesh.wgsl-material-wgsl-0-c529a49353714424.js +24 -0
- package/lib/splats-mesh.wgsl-material-wgsl-2-ec594872c7d3fbf6.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-32dc5311875620e4.js +24 -0
- package/lib/splats.wgsl-material-glsl-1-da80dc94627cf770.js +24 -0
- package/lib/splats.wgsl-material-glsl-2-fb7895ef8b78d665.js +24 -0
- package/lib/splats.wgsl-material-glsl-3-b8800106bf9b5770.js +24 -0
- package/lib/splats.wgsl-material-glsl-64-9f7d0841d0ac147d.js +24 -0
- package/lib/splats.wgsl-material-glsl-65-fbc638499588c846.js +24 -0
- package/lib/splats.wgsl-material-glsl-66-3c49fd84ca72bada.js +24 -0
- package/lib/splats.wgsl-material-glsl-67-5aabadfe22c2f13c.js +24 -0
- package/lib/splats.wgsl-material-wgsl-0-eef591a67552ad7e.js +24 -0
- package/lib/splats.wgsl-material-wgsl-2-15569422a33203f4.js +24 -0
- package/lib/splats.wgsl-material-wgsl-64-769cd6ac619835c2.js +24 -0
- package/lib/splats.wgsl-material-wgsl-66-f1515cd808e3a573.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 +498 -0
- package/lib/version.d.ts +1 -0
- package/lib/version.js +3 -0
- package/package.json +56 -5
package/lib/coverage.js
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
// How much of a chunk's surface its first splats cover. A chunk keeps its splats in importance
|
|
2
|
+
// order, and a coarse level draws a prefix of them. A prefix that still covers the surface needs no
|
|
3
|
+
// help, as in a dense cloud whose splats overlap; a prefix that leaves holes, as on a scan's ground
|
|
4
|
+
// of small flat splats, grows its splats until they cover the holes again.
|
|
5
|
+
//
|
|
6
|
+
// The measure sees the chunk from three directions, along each axis, through a grid of cells about
|
|
7
|
+
// the size of a typical splat. It walks the splats in order, and each one hides the cells under its
|
|
8
|
+
// projected ellipse by its own Gaussian. A cell counts as covered once the splats over it hide half
|
|
9
|
+
// of what lies behind, so the measure follows what a view of the chunk shows. The share of the whole chunk's covered cells that each prefix covers is recorded at
|
|
10
|
+
// prefix lengths that halve every two steps, from the whole chunk down to one splat.
|
|
11
|
+
/**
|
|
12
|
+
* The most cells that a splat reaches along each axis of a view on each side of its center, which
|
|
13
|
+
* bounds its work: a splat larger than that is counted as covering that much.
|
|
14
|
+
*/
|
|
15
|
+
const MOST_REACH = 16;
|
|
16
|
+
/** How far a splat reaches, in standard deviations. */
|
|
17
|
+
const REACH_DEVIATIONS = 2;
|
|
18
|
+
/** The square root of two pi, the width of a unit Gaussian's area along one axis. */
|
|
19
|
+
const GAUSSIAN_WIDTH = Math.sqrt(2 * Math.PI);
|
|
20
|
+
/** The share of what lies behind a cell that its splats must hide before it counts as covered. */
|
|
21
|
+
const COVERED = 0.5;
|
|
22
|
+
/** The cells of one axis of the grid at most, so a cell's index fits in a safe whole number. */
|
|
23
|
+
const MOST_CELLS = 65_536;
|
|
24
|
+
/** The opacity under which a splat's share of a cell is left out. */
|
|
25
|
+
const FAINTEST = 1 / 255;
|
|
26
|
+
/** The table of the cells that a measure has touched, which the measures share and grow. */
|
|
27
|
+
let keys = new Float64Array(0);
|
|
28
|
+
let clear = new Float32Array(0);
|
|
29
|
+
/**
|
|
30
|
+
* The share of the chunk's covered cells that its first splats cover, at the prefix lengths that
|
|
31
|
+
* `coveragePrefix` names: index 0 is the whole chunk, whose share is 1, and each next index keeps
|
|
32
|
+
* a prefix the square root of two times shorter. The arrays hold the chunk's splats in order.
|
|
33
|
+
*
|
|
34
|
+
* The chunk is seen from three directions, along each axis. In each view, a splat hides the cells
|
|
35
|
+
* under its projected ellipse by its own Gaussian, so a flat splat seen face on covers its disc and
|
|
36
|
+
* one seen edge on a line. A splat narrower than a cell along an axis takes the share of the cell
|
|
37
|
+
* that it covers along that axis, so a crowd of tiny splats covers a cell as their areas add up.
|
|
38
|
+
*/
|
|
39
|
+
export function coverageShares(positions, rotations, scales, colors, count, center, radius) {
|
|
40
|
+
const steps = count > 1 ? Math.floor(2 * Math.log2(count)) + 1 : 1;
|
|
41
|
+
const shares = new Float32Array(steps).fill(1);
|
|
42
|
+
if (count < 2)
|
|
43
|
+
return shares;
|
|
44
|
+
// A cell about the size of the chunk's middle splat, and no finer than the grid allows.
|
|
45
|
+
const sizes = new Float32Array(count);
|
|
46
|
+
let largest = 0;
|
|
47
|
+
for (let i = 0; i < count; i++) {
|
|
48
|
+
const a = scales[i * 3];
|
|
49
|
+
const b = scales[i * 3 + 1];
|
|
50
|
+
const c = scales[i * 3 + 2];
|
|
51
|
+
sizes[i] = Math.sqrt(Math.max(a * b, b * c, a * c));
|
|
52
|
+
largest = Math.max(largest, a, b, c);
|
|
53
|
+
}
|
|
54
|
+
sizes.sort();
|
|
55
|
+
const span = 2 * (radius + REACH_DEVIATIONS * largest) + 1e-6;
|
|
56
|
+
const cell = Math.max(sizes[count >> 1], span / (MOST_CELLS - 1), 1e-9);
|
|
57
|
+
const origin = [
|
|
58
|
+
center[0] - span / 2,
|
|
59
|
+
center[1] - span / 2,
|
|
60
|
+
center[2] - span / 2,
|
|
61
|
+
];
|
|
62
|
+
const capacity = tableFor(count);
|
|
63
|
+
const mask = capacity - 1;
|
|
64
|
+
keys.fill(0, 0, capacity);
|
|
65
|
+
clear.fill(1, 0, capacity);
|
|
66
|
+
let used = 0;
|
|
67
|
+
let covered = 0;
|
|
68
|
+
// The prefix lengths at which the walk records how many cells were covered, shortest last.
|
|
69
|
+
const marks = new Int32Array(steps);
|
|
70
|
+
for (let k = 0; k < steps; k++)
|
|
71
|
+
marks[k] = coveragePrefix(count, k);
|
|
72
|
+
const coveredAt = new Float64Array(steps);
|
|
73
|
+
let next = steps - 1;
|
|
74
|
+
const sigma = [0, 0, 0, 0, 0, 0];
|
|
75
|
+
for (let i = 0; i < count; i++) {
|
|
76
|
+
const alpha = Math.min(Math.max(colors[i * 4 + 3], 0), 1);
|
|
77
|
+
if (alpha >= FAINTEST) {
|
|
78
|
+
covarianceOf(scales, rotations, i, sigma);
|
|
79
|
+
for (let view = 0; view < 3; view++) {
|
|
80
|
+
// The view looks along one axis, and its cells lie across the other two.
|
|
81
|
+
const a = VIEW_AXES[view * 2];
|
|
82
|
+
const b = VIEW_AXES[view * 2 + 1];
|
|
83
|
+
const saa = sigma[SIGMA_AT[a * 3 + a]] + 1e-12;
|
|
84
|
+
const sbb = sigma[SIGMA_AT[b * 3 + b]] + 1e-12;
|
|
85
|
+
const sab = sigma[SIGMA_AT[a * 3 + b]];
|
|
86
|
+
const det = Math.max(saa * sbb - sab * sab, 1e-24);
|
|
87
|
+
const wa = Math.sqrt(saa);
|
|
88
|
+
const wb = Math.sqrt(sbb);
|
|
89
|
+
const ra = Math.min(MOST_REACH, Math.floor((REACH_DEVIATIONS * wa) / cell));
|
|
90
|
+
const rb = Math.min(MOST_REACH, Math.floor((REACH_DEVIATIONS * wb) / cell));
|
|
91
|
+
const fa = ra === 0 ? Math.min(1, (GAUSSIAN_WIDTH * wa) / cell) : 1;
|
|
92
|
+
const fb = rb === 0 ? Math.min(1, (GAUSSIAN_WIDTH * wb) / cell) : 1;
|
|
93
|
+
const pa = positions[i * 3 + a] - origin[a];
|
|
94
|
+
const pb = positions[i * 3 + b] - origin[b];
|
|
95
|
+
const ca = Math.floor(pa / cell);
|
|
96
|
+
const cb = Math.floor(pb / cell);
|
|
97
|
+
for (let v = cb - rb; v <= cb + rb; v++)
|
|
98
|
+
for (let u = ca - ra; u <= ca + ra; u++) {
|
|
99
|
+
if (u < 0 || v < 0 || u >= MOST_CELLS || v >= MOST_CELLS)
|
|
100
|
+
continue;
|
|
101
|
+
// The cell's center from the splat's, in the ellipse's own measure; an axis that
|
|
102
|
+
// the splat is narrower than a cell along takes its share instead.
|
|
103
|
+
const da = ra === 0 ? 0 : (u + 0.5) * cell - pa;
|
|
104
|
+
const db = rb === 0 ? 0 : (v + 0.5) * cell - pb;
|
|
105
|
+
const power = (0.5 * (sbb * da * da - 2 * sab * da * db + saa * db * db)) / det;
|
|
106
|
+
const hides = alpha * fa * fb * Math.exp(-power);
|
|
107
|
+
if (hides < FAINTEST)
|
|
108
|
+
continue;
|
|
109
|
+
const key = u + v * MOST_CELLS + view * MOST_CELLS * MOST_CELLS + 1;
|
|
110
|
+
let slot = hash(u, v, view) & mask;
|
|
111
|
+
while (keys[slot] !== 0 && keys[slot] !== key)
|
|
112
|
+
slot = (slot + 1) & mask;
|
|
113
|
+
if (keys[slot] === 0) {
|
|
114
|
+
// A full table counts each new cell as covered, which only lowers the growth.
|
|
115
|
+
if (used * 4 >= capacity * 3) {
|
|
116
|
+
covered++;
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
keys[slot] = key;
|
|
120
|
+
used++;
|
|
121
|
+
}
|
|
122
|
+
const before = clear[slot];
|
|
123
|
+
const after = before * (1 - hides);
|
|
124
|
+
clear[slot] = after;
|
|
125
|
+
if (before > COVERED && after <= COVERED)
|
|
126
|
+
covered++;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
while (next >= 0 && i + 1 === marks[next]) {
|
|
131
|
+
coveredAt[next] = covered;
|
|
132
|
+
next--;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
const whole = coveredAt[0];
|
|
136
|
+
if (whole > 0)
|
|
137
|
+
for (let k = 0; k < steps; k++)
|
|
138
|
+
shares[k] = coveredAt[k] / whole;
|
|
139
|
+
return shares;
|
|
140
|
+
}
|
|
141
|
+
/** The two axes that the cells of each view lie across: the views look along z, x and y. */
|
|
142
|
+
const VIEW_AXES = [0, 1, 1, 2, 0, 2];
|
|
143
|
+
/** Where each entry of a symmetric 3 x 3 matrix lies in its six stored values. */
|
|
144
|
+
const SIGMA_AT = [0, 1, 2, 1, 3, 4, 2, 4, 5];
|
|
145
|
+
/** The length of the prefix that index `k` of a chunk's coverage shares measures. */
|
|
146
|
+
export function coveragePrefix(count, k) {
|
|
147
|
+
return Math.max(1, Math.ceil(count / 2 ** (k / 2)));
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* The share of the chunk's covered cells that its first `kept` splats cover, read between the
|
|
151
|
+
* recorded prefix lengths along the halving of the prefix.
|
|
152
|
+
*/
|
|
153
|
+
export function coverageAt(shares, count, kept) {
|
|
154
|
+
if (kept >= count || shares.length < 2)
|
|
155
|
+
return 1;
|
|
156
|
+
const step = Math.min(2 * Math.log2(count / Math.max(kept, 1)), shares.length - 1);
|
|
157
|
+
const low = Math.floor(step);
|
|
158
|
+
const high = Math.min(low + 1, shares.length - 1);
|
|
159
|
+
const t = step - low;
|
|
160
|
+
return shares[low] * (1 - t) + shares[high] * t;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* The covariance of splat `i`, its rotation times its squared spreads times the rotation turned
|
|
164
|
+
* back, as the six values xx, xy, xz, yy, yz and zz in `out`.
|
|
165
|
+
*/
|
|
166
|
+
function covarianceOf(scales, rotations, i, out) {
|
|
167
|
+
const qx = rotations[i * 4];
|
|
168
|
+
const qy = rotations[i * 4 + 1];
|
|
169
|
+
const qz = rotations[i * 4 + 2];
|
|
170
|
+
const qw = rotations[i * 4 + 3];
|
|
171
|
+
const length = Math.hypot(qx, qy, qz, qw) || 1;
|
|
172
|
+
const x = qx / length;
|
|
173
|
+
const y = qy / length;
|
|
174
|
+
const z = qz / length;
|
|
175
|
+
const w = qw / length;
|
|
176
|
+
const sx = scales[i * 3];
|
|
177
|
+
const sy = scales[i * 3 + 1];
|
|
178
|
+
const sz = scales[i * 3 + 2];
|
|
179
|
+
// The rotation's columns, each scaled by its spread.
|
|
180
|
+
const m00 = (1 - 2 * (y * y + z * z)) * sx;
|
|
181
|
+
const m10 = 2 * (x * y + z * w) * sx;
|
|
182
|
+
const m20 = 2 * (x * z - y * w) * sx;
|
|
183
|
+
const m01 = 2 * (x * y - z * w) * sy;
|
|
184
|
+
const m11 = (1 - 2 * (x * x + z * z)) * sy;
|
|
185
|
+
const m21 = 2 * (y * z + x * w) * sy;
|
|
186
|
+
const m02 = 2 * (x * z + y * w) * sz;
|
|
187
|
+
const m12 = 2 * (y * z - x * w) * sz;
|
|
188
|
+
const m22 = (1 - 2 * (x * x + y * y)) * sz;
|
|
189
|
+
out[0] = m00 * m00 + m01 * m01 + m02 * m02;
|
|
190
|
+
out[1] = m00 * m10 + m01 * m11 + m02 * m12;
|
|
191
|
+
out[2] = m00 * m20 + m01 * m21 + m02 * m22;
|
|
192
|
+
out[3] = m10 * m10 + m11 * m11 + m12 * m12;
|
|
193
|
+
out[4] = m10 * m20 + m11 * m21 + m12 * m22;
|
|
194
|
+
out[5] = m20 * m20 + m21 * m21 + m22 * m22;
|
|
195
|
+
}
|
|
196
|
+
/** A cell's place in the table, mixed from its three indices. */
|
|
197
|
+
function hash(x, y, z) {
|
|
198
|
+
return (Math.imul(x, 73_856_093) ^ Math.imul(y, 19_349_663) ^ Math.imul(z, 83_492_791)) >>> 0;
|
|
199
|
+
}
|
|
200
|
+
/** Makes the shared table hold room for a chunk of `count` splats, and gives its size. */
|
|
201
|
+
function tableFor(count) {
|
|
202
|
+
const want = Math.min(2 ** 22, Math.max(1024, 2 ** Math.ceil(Math.log2(count * 16))));
|
|
203
|
+
if (keys.length < want) {
|
|
204
|
+
keys = new Float64Array(want);
|
|
205
|
+
clear = new Float32Array(want);
|
|
206
|
+
}
|
|
207
|
+
return want;
|
|
208
|
+
}
|
package/lib/feature.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type FeatureDescription } from '@null3d/engine';
|
|
2
|
+
/** The splats that draw at full quality on each preset, from Low to Ultra. */
|
|
3
|
+
export declare const PRESET_SPLATS: readonly [100000, 250000, 500000, 800000];
|
|
4
|
+
/** The WebGL2 presets, which sort and upload the same splats with fewer threads in reach. */
|
|
5
|
+
export declare const WEBGL2_SPLATS: readonly [50000, 120000, 250000, 400000];
|
|
6
|
+
/**
|
|
7
|
+
* The feature that the add-on registers: its `drawn` setting takes the budget's share of the
|
|
8
|
+
* preset's count, and `memory` reports the GPU bytes that the add-on's rows and quad hold.
|
|
9
|
+
*/
|
|
10
|
+
export declare function splatsFeature(memory: () => number): FeatureDescription;
|
package/lib/feature.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// The feature that the splats add-on registers with the engine: the splats that draw at full
|
|
2
|
+
// quality on each preset, the time budget whose share the frame-budget governor lowers, and the
|
|
3
|
+
// memory of the batch's rows and its quad. It lives apart from the drawing code so that a test can
|
|
4
|
+
// check the preset rows and the governor's steps without a GPU.
|
|
5
|
+
import { defineFeature } from '@null3d/engine';
|
|
6
|
+
/** The splats that draw at full quality on each preset, from Low to Ultra. */
|
|
7
|
+
export const PRESET_SPLATS = [100_000, 250_000, 500_000, 800_000];
|
|
8
|
+
/** The WebGL2 presets, which sort and upload the same splats with fewer threads in reach. */
|
|
9
|
+
export const WEBGL2_SPLATS = [50_000, 120_000, 250_000, 400_000];
|
|
10
|
+
/** The time in ms that a frame of splats may take at full quality, from Low to Ultra. */
|
|
11
|
+
const PRESET_BUDGET = [2, 3, 4, 6];
|
|
12
|
+
/** The lowest share of that time that the governor leaves the add-on. */
|
|
13
|
+
const BUDGET_MIN = 0.2;
|
|
14
|
+
/** The most splats that the draw takes, which bounds the setting's range. */
|
|
15
|
+
const MAX_SPLATS = 8_000_000;
|
|
16
|
+
/**
|
|
17
|
+
* The feature that the add-on registers: its `drawn` setting takes the budget's share of the
|
|
18
|
+
* preset's count, and `memory` reports the GPU bytes that the add-on's rows and quad hold.
|
|
19
|
+
*/
|
|
20
|
+
export function splatsFeature(memory) {
|
|
21
|
+
return defineFeature({
|
|
22
|
+
name: 'splats',
|
|
23
|
+
// The camera draws the splats at one sample per pixel and blends them over the scene,
|
|
24
|
+
// so a multisampled frame does not blend each of their overlapping cards per sample.
|
|
25
|
+
frame: 'resolve',
|
|
26
|
+
kind: 'draw',
|
|
27
|
+
views: ['main'],
|
|
28
|
+
settings: {
|
|
29
|
+
drawn: {
|
|
30
|
+
presets: PRESET_SPLATS,
|
|
31
|
+
webgl2: WEBGL2_SPLATS,
|
|
32
|
+
values: { min: 0, max: MAX_SPLATS, whole: true },
|
|
33
|
+
governor: 'scale',
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
budget: { presets: PRESET_BUDGET, min: BUDGET_MIN },
|
|
37
|
+
memory,
|
|
38
|
+
});
|
|
39
|
+
}
|
package/lib/format.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One cloud of Gaussian splats, ready to draw.
|
|
3
|
+
*
|
|
4
|
+
* @category api/splats
|
|
5
|
+
*/
|
|
6
|
+
export interface SplatData {
|
|
7
|
+
/** The number of splats. Every array holds this many entries. */
|
|
8
|
+
readonly count: number;
|
|
9
|
+
/** The centers, 3 floats each, in meters. */
|
|
10
|
+
readonly positions: Float32Array;
|
|
11
|
+
/** The three principal standard deviations, 3 floats each, in meters, 0 or more. */
|
|
12
|
+
readonly scales: Float32Array;
|
|
13
|
+
/** The rotations as unit quaternions (x, y, z, w), 4 floats each. */
|
|
14
|
+
readonly rotations: Float32Array;
|
|
15
|
+
/** Linear RGBA colors, 4 floats each, with the opacity in alpha. */
|
|
16
|
+
readonly colors: Float32Array;
|
|
17
|
+
}
|
|
18
|
+
/** The first code that the package's errors use, outside the engine's own range. */
|
|
19
|
+
export declare const SPLATS_CODES = 2100;
|
|
20
|
+
/** An error that the package raises, with its own code and a message that says how to fix it. */
|
|
21
|
+
export declare function splatsError(code: number, detail: string): Error;
|
|
22
|
+
/**
|
|
23
|
+
* The linear RGB of a first-band spherical-harmonic coefficient, from its three channels. `encoded`
|
|
24
|
+
* says that the reconstructed value is an sRGB display value, which this decodes; leave it out for
|
|
25
|
+
* a value that is already linear.
|
|
26
|
+
*
|
|
27
|
+
* @category api/splats
|
|
28
|
+
*/
|
|
29
|
+
export declare function colorOfSh(dc: readonly [number, number, number], encoded?: boolean, out?: Float32Array<ArrayBuffer>): Float32Array;
|
|
30
|
+
/**
|
|
31
|
+
* Checks that a cloud's arrays hold what its count says, and throws when they do not.
|
|
32
|
+
*
|
|
33
|
+
* @category api/splats
|
|
34
|
+
*/
|
|
35
|
+
export declare function checkSplats(data: SplatData, what: string): SplatData;
|
package/lib/format.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// One cloud of Gaussian splats, in the form that every reader of the package gives the draw: a
|
|
2
|
+
// center, three principal spreads and a unit rotation for each splat, and its linear color and
|
|
3
|
+
// opacity. The readers of SPZ and of KHR_gaussian_splatting both fill this, and the draw takes it
|
|
4
|
+
// without caring where it came from.
|
|
5
|
+
//
|
|
6
|
+
// A reader reconstructs a splat's color from the first band of its spherical harmonics, as the
|
|
7
|
+
// glTF extension defines: the coefficient times the band's constant, plus the half that moves the
|
|
8
|
+
// range onto 0 to 1. Higher bands make the color change with the view; this package ignores them,
|
|
9
|
+
// which the extension allows.
|
|
10
|
+
/** The constant that turns the first spherical-harmonic band into a color. */
|
|
11
|
+
const SH_C0 = 0.2820947917738781;
|
|
12
|
+
/** The first code that the package's errors use, outside the engine's own range. */
|
|
13
|
+
export const SPLATS_CODES = 2100;
|
|
14
|
+
/** An error that the package raises, with its own code and a message that says how to fix it. */
|
|
15
|
+
export function splatsError(code, detail) {
|
|
16
|
+
return new Error(`splats E${code}: ${detail}`);
|
|
17
|
+
}
|
|
18
|
+
/** Turns a chunk of sRGB display values into linear values, as the engine's lighting expects. */
|
|
19
|
+
function srgbToLinear(value) {
|
|
20
|
+
return value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The linear RGB of a first-band spherical-harmonic coefficient, from its three channels. `encoded`
|
|
24
|
+
* says that the reconstructed value is an sRGB display value, which this decodes; leave it out for
|
|
25
|
+
* a value that is already linear.
|
|
26
|
+
*
|
|
27
|
+
* @category api/splats
|
|
28
|
+
*/
|
|
29
|
+
export function colorOfSh(dc, encoded = true, out = new Float32Array(3)) {
|
|
30
|
+
for (let i = 0; i < 3; i++) {
|
|
31
|
+
const value = Math.min(Math.max(dc[i] * SH_C0 + 0.5, 0), 1);
|
|
32
|
+
out[i] = encoded ? srgbToLinear(value) : value;
|
|
33
|
+
}
|
|
34
|
+
return out;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Checks that a cloud's arrays hold what its count says, and throws when they do not.
|
|
38
|
+
*
|
|
39
|
+
* @category api/splats
|
|
40
|
+
*/
|
|
41
|
+
export function checkSplats(data, what) {
|
|
42
|
+
const { count, positions, scales, rotations, colors } = data;
|
|
43
|
+
const whole = Number.isInteger(count) && count >= 0;
|
|
44
|
+
if (!whole)
|
|
45
|
+
throw splatsError(2101, `${what} holds ${count} splats, and a cloud takes a whole number of splats, 0 or more.`);
|
|
46
|
+
const fits = positions.length === count * 3 &&
|
|
47
|
+
scales.length === count * 3 &&
|
|
48
|
+
rotations.length === count * 4 &&
|
|
49
|
+
colors.length === count * 4;
|
|
50
|
+
if (!fits)
|
|
51
|
+
throw splatsError(2101, `${what} holds ${count} splats, and its arrays hold ${positions.length / 3}, ${scales.length / 3}, ${rotations.length / 4} and ${colors.length / 4} of them. Give every array one entry for each splat.`);
|
|
52
|
+
return data;
|
|
53
|
+
}
|
package/lib/gltf.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type GltfExtensionHandler } from '@null3d/engine';
|
|
2
|
+
/**
|
|
3
|
+
* The glTF extension that a splat file carries, which this handler reads.
|
|
4
|
+
*
|
|
5
|
+
* @category api/splats
|
|
6
|
+
*/
|
|
7
|
+
export declare const SPLAT_EXTENSION = "KHR_gaussian_splatting";
|
|
8
|
+
/** The reader of `KHR_gaussian_splatting`, which the add-on's module registers as it loads. */
|
|
9
|
+
export declare const splatGltfHandler: GltfExtensionHandler;
|
package/lib/gltf.js
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
// The reader of the KHR_gaussian_splatting glTF extension. A primitive that carries the extension
|
|
2
|
+
// holds one point for each splat, with its center, rotation, spreads, opacity and first-band
|
|
3
|
+
// color. This handler turns that into the mesh and material that draw the splats: four vertices
|
|
4
|
+
// for each splat, with the splat's values in the mesh's attributes, and a full shader that billboards
|
|
5
|
+
// each quad and fades it as a Gaussian.
|
|
6
|
+
//
|
|
7
|
+
// The splats are cut into a few parts in space, so the transparent pass sorts the parts by depth and
|
|
8
|
+
// a model near the camera draws its nearer splats over its farther ones. The splats inside a part
|
|
9
|
+
// draw in the file's order; the add-on's own files draw through an instance batch, whose rows the
|
|
10
|
+
// engine sorts one by one.
|
|
11
|
+
import { registerGltfExtension, } from '@null3d/engine';
|
|
12
|
+
import { colorOfSh, splatsError } from './format.js';
|
|
13
|
+
import meshShader from './splats-mesh.wgsl.js';
|
|
14
|
+
/**
|
|
15
|
+
* The glTF extension that a splat file carries, which this handler reads.
|
|
16
|
+
*
|
|
17
|
+
* @category api/splats
|
|
18
|
+
*/
|
|
19
|
+
export const SPLAT_EXTENSION = 'KHR_gaussian_splatting';
|
|
20
|
+
/** The prefix of the extension's vertex attributes, such as its rotation. */
|
|
21
|
+
const PREFIX = `${SPLAT_EXTENSION}:`;
|
|
22
|
+
/** The splats that one part holds at most, which bounds a part's draw and its sort. */
|
|
23
|
+
const PART_SPLATS = 25_000;
|
|
24
|
+
/** The largest number of parts that a primitive is cut into. */
|
|
25
|
+
const MAX_PARTS = 64;
|
|
26
|
+
/** The colors of the extension, and whether a reconstructed value is an sRGB display value. */
|
|
27
|
+
const COLOR_SPACES = {
|
|
28
|
+
srgb_rec709_display: true,
|
|
29
|
+
lin_rec709_display: false,
|
|
30
|
+
};
|
|
31
|
+
/** The reader of `KHR_gaussian_splatting`, which the add-on's module registers as it loads. */
|
|
32
|
+
export const splatGltfHandler = {
|
|
33
|
+
extensions: [SPLAT_EXTENSION],
|
|
34
|
+
load: (data, context) => loadPrimitive(data, context),
|
|
35
|
+
};
|
|
36
|
+
// An add-on reads its extension wherever its module loads, so a page that imports the add-on loads
|
|
37
|
+
// a splat model with `assets.loadGltf` and no other call. A development reload registers it again,
|
|
38
|
+
// which replaces the earlier handler.
|
|
39
|
+
registerGltfExtension(splatGltfHandler);
|
|
40
|
+
/** The parts that draw one primitive of splats. */
|
|
41
|
+
function loadPrimitive(data, context) {
|
|
42
|
+
const { primitive, extension } = data;
|
|
43
|
+
const json = (data.json ?? {});
|
|
44
|
+
if (json.kernel !== undefined && json.kernel !== 'ellipse')
|
|
45
|
+
throw splatsError(2106, `the glTF file gives the Gaussian kernel ${JSON.stringify(json.kernel)}, and this add-on draws the 'ellipse' kernel. Convert the file to the ellipse kernel.`);
|
|
46
|
+
const encoded = COLOR_SPACES[json.colorSpace];
|
|
47
|
+
if (encoded === undefined)
|
|
48
|
+
throw splatsError(2107, `the glTF file gives the color space ${JSON.stringify(json.colorSpace)}, and this add-on reads 'srgb_rec709_display' and 'lin_rec709_display'.`);
|
|
49
|
+
const attributes = primitive.attributes;
|
|
50
|
+
const positions = attributes.POSITION;
|
|
51
|
+
const rotations = attributes[`${PREFIX}ROTATION`];
|
|
52
|
+
const scales = attributes[`${PREFIX}SCALE`];
|
|
53
|
+
const opacities = attributes[`${PREFIX}OPACITY`];
|
|
54
|
+
if (!positions || !rotations || !scales || !opacities)
|
|
55
|
+
throw splatsError(2108, `the glTF splat primitive (${extension}) lacks one of POSITION, ${PREFIX}ROTATION, ${PREFIX}SCALE and ${PREFIX}OPACITY, which a splat needs.`);
|
|
56
|
+
const count = primitive.count;
|
|
57
|
+
if (positions.length < count * 3 || rotations.length < count * 4 || scales.length < count * 3)
|
|
58
|
+
throw splatsError(2108, `the glTF splat primitive holds ${count} splats, and its attributes do not hold that many.`);
|
|
59
|
+
const splats = new Array(count);
|
|
60
|
+
const band = attributes[`${PREFIX}SH_DEGREE_0_COEF_0`];
|
|
61
|
+
// The file's fallback color, which glTF stores linear, for a splat without the extension's band.
|
|
62
|
+
const fallback = attributes.COLOR_0;
|
|
63
|
+
const scratch = new Float32Array(3);
|
|
64
|
+
for (let i = 0; i < count; i++) {
|
|
65
|
+
const color = band !== undefined
|
|
66
|
+
? colorOfSh([band[i * 3], band[i * 3 + 1], band[i * 3 + 2]], encoded, scratch)
|
|
67
|
+
: fallback !== undefined
|
|
68
|
+
? [
|
|
69
|
+
fallback[i * 3],
|
|
70
|
+
fallback[i * 3 + 1],
|
|
71
|
+
fallback[i * 3 + 2],
|
|
72
|
+
]
|
|
73
|
+
: [0, 0, 0];
|
|
74
|
+
splats[i] = {
|
|
75
|
+
position: [
|
|
76
|
+
positions[i * 3],
|
|
77
|
+
positions[i * 3 + 1],
|
|
78
|
+
positions[i * 3 + 2],
|
|
79
|
+
],
|
|
80
|
+
rotation: [
|
|
81
|
+
rotations[i * 4],
|
|
82
|
+
rotations[i * 4 + 1],
|
|
83
|
+
rotations[i * 4 + 2],
|
|
84
|
+
rotations[i * 4 + 3],
|
|
85
|
+
],
|
|
86
|
+
scale: [scales[i * 3], scales[i * 3 + 1], scales[i * 3 + 2]],
|
|
87
|
+
opacity: opacities[i],
|
|
88
|
+
color: [color[0], color[1], color[2], opacities[i]],
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
const material = context.materials.shader({
|
|
92
|
+
wgsl: meshShader,
|
|
93
|
+
alphaMode: 'blend',
|
|
94
|
+
depthWrite: false,
|
|
95
|
+
// The card turns to the camera, and its own winding decides no face: draw both.
|
|
96
|
+
doubleSided: true,
|
|
97
|
+
});
|
|
98
|
+
const order = spatialOrder(splats);
|
|
99
|
+
const parts = [];
|
|
100
|
+
const perPart = Math.max(PART_SPLATS, Math.ceil(count / MAX_PARTS));
|
|
101
|
+
for (let first = 0; first < count; first += perPart) {
|
|
102
|
+
const chosen = order.slice(first, Math.min(first + perPart, count));
|
|
103
|
+
// Each quad is a splat that the shader shapes from its record, so the parts take no levels.
|
|
104
|
+
const arrays = { ...arraysOf(splats, chosen), levels: false };
|
|
105
|
+
parts.push({ mesh: context.geometry.fromArrays(arrays), material });
|
|
106
|
+
}
|
|
107
|
+
return parts;
|
|
108
|
+
}
|
|
109
|
+
/** The splats' indices in a space-filling order, so each part holds nearby splats. */
|
|
110
|
+
function spatialOrder(splats) {
|
|
111
|
+
const count = splats.length;
|
|
112
|
+
const min = [Infinity, Infinity, Infinity];
|
|
113
|
+
const max = [-Infinity, -Infinity, -Infinity];
|
|
114
|
+
for (const { position } of splats)
|
|
115
|
+
for (let k = 0; k < 3; k++) {
|
|
116
|
+
min[k] = Math.min(min[k], position[k]);
|
|
117
|
+
max[k] = Math.max(max[k], position[k]);
|
|
118
|
+
}
|
|
119
|
+
const span = [0, 1, 2].map((k) => {
|
|
120
|
+
const size = max[k] - min[k];
|
|
121
|
+
return size > 0 ? 1023 / size : 0;
|
|
122
|
+
});
|
|
123
|
+
const codes = new Float64Array(count);
|
|
124
|
+
const order = new Uint32Array(count);
|
|
125
|
+
for (let i = 0; i < count; i++) {
|
|
126
|
+
const { position } = splats[i];
|
|
127
|
+
const x = Math.round((position[0] - min[0]) * span[0]);
|
|
128
|
+
const y = Math.round((position[1] - min[1]) * span[1]);
|
|
129
|
+
const z = Math.round((position[2] - min[2]) * span[2]);
|
|
130
|
+
codes[i] = interleave(x, y, z);
|
|
131
|
+
order[i] = i;
|
|
132
|
+
}
|
|
133
|
+
return order.sort((a, b) => codes[a] - codes[b]);
|
|
134
|
+
}
|
|
135
|
+
/** The Morton code of three 10-bit coordinates. */
|
|
136
|
+
function interleave(x, y, z) {
|
|
137
|
+
let code = 0;
|
|
138
|
+
for (let bit = 0; bit < 10; bit++) {
|
|
139
|
+
code += ((x >> bit) & 1) * 2 ** (3 * bit);
|
|
140
|
+
code += ((y >> bit) & 1) * 2 ** (3 * bit + 1);
|
|
141
|
+
code += ((z >> bit) & 1) * 2 ** (3 * bit + 2);
|
|
142
|
+
}
|
|
143
|
+
return code;
|
|
144
|
+
}
|
|
145
|
+
/** The vertex arrays of the chosen splats: four vertices and two triangles for each splat. */
|
|
146
|
+
function arraysOf(splats, chosen) {
|
|
147
|
+
const count = chosen.length;
|
|
148
|
+
const positions = new Float32Array(count * 12);
|
|
149
|
+
const normals = new Float32Array(count * 12);
|
|
150
|
+
const uvs = new Float32Array(count * 8);
|
|
151
|
+
const tangents = new Float32Array(count * 16);
|
|
152
|
+
const colors = new Float32Array(count * 16);
|
|
153
|
+
const indices = count * 4 > 65_535 ? new Uint32Array(count * 6) : new Uint16Array(count * 6);
|
|
154
|
+
const corners = [
|
|
155
|
+
[0, 0],
|
|
156
|
+
[1, 0],
|
|
157
|
+
[1, 1],
|
|
158
|
+
[0, 1],
|
|
159
|
+
];
|
|
160
|
+
chosen.forEach((source, k) => {
|
|
161
|
+
const splat = splats[source];
|
|
162
|
+
for (let corner = 0; corner < 4; corner++) {
|
|
163
|
+
const at = k * 4 + corner;
|
|
164
|
+
positions.set(splat.position, at * 3);
|
|
165
|
+
normals.set(splat.scale, at * 3);
|
|
166
|
+
tangents.set(splat.rotation, at * 4);
|
|
167
|
+
colors.set(splat.color, at * 4);
|
|
168
|
+
uvs.set(corners[corner], at * 2);
|
|
169
|
+
}
|
|
170
|
+
const base = k * 4;
|
|
171
|
+
indices.set([base, base + 1, base + 2, base, base + 2, base + 3], k * 6);
|
|
172
|
+
});
|
|
173
|
+
return { positions, normals, uvs, tangents, colors, indices };
|
|
174
|
+
}
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export type { ChunkOptions, SplatChunk, SplatChunks, Yielder } from './chunks.js';
|
|
2
|
+
export { chunkSplats } from './chunks.js';
|
|
3
|
+
export type { SplatData } from './format.js';
|
|
4
|
+
export { checkSplats, colorOfSh } from './format.js';
|
|
5
|
+
export { SPLAT_EXTENSION } from './gltf.js';
|
|
6
|
+
export { decodeSpz } from './spz.js';
|
|
7
|
+
export { encodeSpz } from './spz-write.js';
|
|
8
|
+
export type { SplatStream, StreamChunk } from './stream.js';
|
|
9
|
+
export { parseStreamIndex } from './stream.js';
|
|
10
|
+
export type { Splats, SplatsDraw, SplatsOptions } from './system.js';
|
|
11
|
+
export { createSplats } from './system.js';
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { checkAddonVersion } from '@null3d/engine';
|
|
2
|
+
import { VERSION } from './version.js';
|
|
3
|
+
checkAddonVersion('@null3d/splats', VERSION);
|
|
4
|
+
export { chunkSplats } from './chunks.js';
|
|
5
|
+
export { checkSplats, colorOfSh } from './format.js';
|
|
6
|
+
export { SPLAT_EXTENSION } from './gltf.js';
|
|
7
|
+
export { decodeSpz } from './spz.js';
|
|
8
|
+
export { encodeSpz } from './spz-write.js';
|
|
9
|
+
export { parseStreamIndex } from './stream.js';
|
|
10
|
+
export { createSplats } from './system.js';
|
package/lib/pack.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { SplatChunk } from './chunks.js';
|
|
2
|
+
/** The batch rows that a pack writes, and the calls that tell the engine what changed. */
|
|
3
|
+
export interface SplatRows {
|
|
4
|
+
positions: Float32Array;
|
|
5
|
+
rotations: Float32Array;
|
|
6
|
+
scales: Float32Array;
|
|
7
|
+
colors: Float32Array;
|
|
8
|
+
markDirty(start: number, count: number): void;
|
|
9
|
+
setActiveCount(count: number): void;
|
|
10
|
+
}
|
|
11
|
+
/** A chunk as the pack sees it: its whole count and the data that has arrived, if any. */
|
|
12
|
+
export interface SplatSlot {
|
|
13
|
+
readonly count: number;
|
|
14
|
+
readonly data: SplatChunk | undefined;
|
|
15
|
+
}
|
|
16
|
+
/** The prefix view of one chunk, which changes only when its chosen count does. */
|
|
17
|
+
export interface PrefixView {
|
|
18
|
+
count: number;
|
|
19
|
+
positions: Float32Array;
|
|
20
|
+
rotations: Float32Array;
|
|
21
|
+
scales: Float32Array;
|
|
22
|
+
colors: Float32Array;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Writes each slot's chosen prefix into the rows in order, and sets the number that draws. Returns
|
|
26
|
+
* that number. A slot without data, or with a chosen count of zero or less, writes nothing.
|
|
27
|
+
*/
|
|
28
|
+
export declare function packRows(rows: SplatRows, slots: readonly SplatSlot[], counts: ArrayLike<number>, prefixes: (PrefixView | undefined)[]): number;
|
|
29
|
+
/**
|
|
30
|
+
* The factor that a chunk's first `count` splats grow by, so that together they cover what the
|
|
31
|
+
* whole chunk covers: 1 for the whole chunk or a prefix that covers its surface already, and at
|
|
32
|
+
* most the cap for a prefix that leaves most of it bare.
|
|
33
|
+
*/
|
|
34
|
+
export declare function growthOf(chunk: SplatChunk, count: number): number;
|
package/lib/pack.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { coverageAt } from './coverage.js';
|
|
2
|
+
/** The most that a prefix's splats grow, so a chunk far away never draws as a few large blobs. */
|
|
3
|
+
const MOST_GROWTH = 3;
|
|
4
|
+
/**
|
|
5
|
+
* Writes each slot's chosen prefix into the rows in order, and sets the number that draws. Returns
|
|
6
|
+
* that number. A slot without data, or with a chosen count of zero or less, writes nothing.
|
|
7
|
+
*/
|
|
8
|
+
export function packRows(rows, slots, counts, prefixes) {
|
|
9
|
+
let at = 0;
|
|
10
|
+
for (let i = 0; i < slots.length; i++) {
|
|
11
|
+
const slot = slots[i];
|
|
12
|
+
const chunk = slot.data;
|
|
13
|
+
if (!chunk) {
|
|
14
|
+
prefixes[i] = undefined;
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
const chosen = i < counts.length ? counts[i] : 0;
|
|
18
|
+
// A chunk that arrived after the worker chose takes its turn in the next selection.
|
|
19
|
+
const count = Math.min(chosen, chunk.count);
|
|
20
|
+
if (!(count > 0)) {
|
|
21
|
+
prefixes[i] = undefined;
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
let prefix = prefixes[i];
|
|
25
|
+
if (!prefix || prefix.count !== count) {
|
|
26
|
+
const growth = growthOf(chunk, count);
|
|
27
|
+
const scales = chunk.scales.subarray(0, count * 3);
|
|
28
|
+
prefix = {
|
|
29
|
+
count,
|
|
30
|
+
positions: chunk.positions.subarray(0, count * 3),
|
|
31
|
+
rotations: chunk.rotations.subarray(0, count * 4),
|
|
32
|
+
scales: growth === 1 ? scales : scales.map((value) => value * growth),
|
|
33
|
+
colors: chunk.colors.subarray(0, count * 4),
|
|
34
|
+
};
|
|
35
|
+
prefixes[i] = prefix;
|
|
36
|
+
}
|
|
37
|
+
rows.positions.set(prefix.positions, at * 3);
|
|
38
|
+
rows.rotations.set(prefix.rotations, at * 4);
|
|
39
|
+
rows.scales.set(prefix.scales, at * 3);
|
|
40
|
+
rows.colors.set(prefix.colors, at * 4);
|
|
41
|
+
at += count;
|
|
42
|
+
}
|
|
43
|
+
if (at > 0)
|
|
44
|
+
rows.markDirty(0, at);
|
|
45
|
+
rows.setActiveCount(at);
|
|
46
|
+
return at;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* The factor that a chunk's first `count` splats grow by, so that together they cover what the
|
|
50
|
+
* whole chunk covers: 1 for the whole chunk or a prefix that covers its surface already, and at
|
|
51
|
+
* most the cap for a prefix that leaves most of it bare.
|
|
52
|
+
*/
|
|
53
|
+
export function growthOf(chunk, count) {
|
|
54
|
+
if (count >= chunk.count)
|
|
55
|
+
return 1;
|
|
56
|
+
const share = coverageAt(chunk.coverage, chunk.count, count);
|
|
57
|
+
if (!(share > 0))
|
|
58
|
+
return MOST_GROWTH;
|
|
59
|
+
return Math.min(MOST_GROWTH, Math.max(1, Math.sqrt(1 / share)));
|
|
60
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The level that a chunk draws at, from the distance to its nearest point and the cloud's size. The
|
|
3
|
+
* distance is measured in the cloud's own radius, not the chunk's: a finer cut makes smaller chunks,
|
|
4
|
+
* and measuring against them would let a smaller chunk pass for a farther one, so the same view
|
|
5
|
+
* would draw fewer splats.
|
|
6
|
+
*/
|
|
7
|
+
export declare function levelOf(distance: number, radius: number, cloudRadius: number): number;
|
|
8
|
+
/**
|
|
9
|
+
* Chooses each chunk's count and writes them after the chunks. Answers the total that draws.
|
|
10
|
+
* `view` is the whole buffer: the header, the chunks and the counts.
|
|
11
|
+
*/
|
|
12
|
+
export declare function runSelection(view: Float32Array): number;
|