@manycore/aholo-splat-transform 1.2.8 → 1.2.9
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/CHANGELOG.md +120 -113
- package/README.md +39 -39
- package/THIRD_PARTY_LICENSES.txt +1373 -1373
- package/bin/cli.js +125 -118
- package/dist/SplatData.d.ts +67 -67
- package/dist/SplatData.js +167 -150
- package/dist/constant.d.ts +3 -3
- package/dist/constant.js +13 -13
- package/dist/file/IFile.d.ts +5 -5
- package/dist/file/IFile.js +1 -1
- package/dist/file/esz.d.ts +11 -11
- package/dist/file/esz.js +337 -322
- package/dist/file/index.d.ts +8 -8
- package/dist/file/index.js +7 -7
- package/dist/file/ksplat.d.ts +12 -12
- package/dist/file/ksplat.js +293 -231
- package/dist/file/lcc.d.ts +11 -11
- package/dist/file/lcc.js +161 -158
- package/dist/file/ply.d.ts +13 -13
- package/dist/file/ply.js +439 -390
- package/dist/file/sog.d.ts +80 -80
- package/dist/file/sog.js +525 -494
- package/dist/file/splat.d.ts +6 -6
- package/dist/file/splat.js +119 -99
- package/dist/file/spz.d.ts +11 -11
- package/dist/file/spz.js +597 -583
- package/dist/file/voxel.d.ts +43 -37
- package/dist/file/voxel.js +411 -280
- package/dist/index.d.ts +33 -33
- package/dist/index.js +54 -54
- package/dist/native/index.d.ts +54 -54
- package/dist/native/index.js +122 -129
- package/dist/native/utils.d.ts +1 -0
- package/dist/native/utils.js +54 -0
- package/dist/tasks/AutoChunkLodTask.d.ts +13 -13
- package/dist/tasks/AutoChunkLodTask.js +117 -117
- package/dist/tasks/AutoLodTask.d.ts +10 -10
- package/dist/tasks/AutoLodTask.js +20 -20
- package/dist/tasks/BaseTask.d.ts +15 -15
- package/dist/tasks/BaseTask.js +5 -5
- package/dist/tasks/FlexLodTask.d.ts +12 -12
- package/dist/tasks/FlexLodTask.js +54 -44
- package/dist/tasks/ModifyTask.d.ts +9 -9
- package/dist/tasks/ModifyTask.js +166 -156
- package/dist/tasks/ReadTask.d.ts +9 -9
- package/dist/tasks/ReadTask.js +29 -29
- package/dist/tasks/SkeletonLodTask.d.ts +10 -10
- package/dist/tasks/SkeletonLodTask.js +176 -156
- package/dist/tasks/VoxelTask.d.ts +35 -30
- package/dist/tasks/VoxelTask.js +40 -37
- package/dist/tasks/WriteTask.d.ts +12 -12
- package/dist/tasks/WriteTask.js +70 -70
- package/dist/utils/BufferReader.d.ts +12 -12
- package/dist/utils/BufferReader.js +45 -45
- package/dist/utils/Logger.d.ts +11 -11
- package/dist/utils/Logger.js +40 -40
- package/dist/utils/StreamChunkDecoder.d.ts +16 -16
- package/dist/utils/StreamChunkDecoder.js +31 -31
- package/dist/utils/index.d.ts +27 -27
- package/dist/utils/index.js +101 -101
- package/dist/utils/k-means.d.ts +4 -4
- package/dist/utils/k-means.js +340 -341
- package/dist/utils/math.d.ts +46 -46
- package/dist/utils/math.js +350 -346
- package/dist/utils/quantize-1d.d.ts +4 -4
- package/dist/utils/quantize-1d.js +164 -164
- package/dist/utils/sh-rotate.d.ts +2 -2
- package/dist/utils/sh-rotate.js +236 -175
- package/dist/utils/splat.d.ts +21 -21
- package/dist/utils/splat.js +397 -387
- package/dist/utils/voxel/binary.d.ts +8 -0
- package/dist/utils/voxel/binary.js +176 -0
- package/dist/utils/voxel/common.d.ts +178 -162
- package/dist/utils/voxel/common.js +1752 -1682
- package/dist/utils/voxel/coplanar-merge.d.ts +63 -63
- package/dist/utils/voxel/coplanar-merge.js +818 -819
- package/dist/utils/voxel/filter-cluster.d.ts +20 -0
- package/dist/utils/voxel/filter-cluster.js +628 -0
- package/dist/utils/voxel/gpu-dilation.d.ts +2 -2
- package/dist/utils/voxel/gpu-dilation.js +677 -656
- package/dist/utils/voxel/marching-cubes.d.ts +42 -42
- package/dist/utils/voxel/marching-cubes.js +1645 -1657
- package/dist/utils/voxel/mesh.d.ts +3 -3
- package/dist/utils/voxel/mesh.js +130 -130
- package/dist/utils/voxel/nav.d.ts +29 -29
- package/dist/utils/voxel/nav.js +1068 -1043
- package/dist/utils/voxel/postprocess.d.ts +23 -23
- package/dist/utils/voxel/postprocess.js +408 -375
- package/dist/utils/voxel/voxel-faces.d.ts +18 -18
- package/dist/utils/voxel/voxel-faces.js +662 -663
- package/dist/utils/voxel/voxelize.d.ts +34 -33
- package/dist/utils/voxel/voxelize.js +1208 -1193
- package/dist/utils/webgpu.d.ts +8 -8
- package/dist/utils/webgpu.js +122 -122
- package/package.json +37 -39
- package/dist/native/cpp/bin/linux/binding.node +0 -0
- package/dist/native/cpp/bin/windows/binding.node +0 -0
package/dist/file/esz.js
CHANGED
|
@@ -1,322 +1,337 @@
|
|
|
1
|
-
import { unzipSync, zipSync } from 'fflate';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
[
|
|
10
|
-
[1, 2,
|
|
11
|
-
]
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
this.
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
offset
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
this.
|
|
47
|
-
this.
|
|
48
|
-
this.
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
.then(
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
const
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
}
|
|
88
|
-
const
|
|
89
|
-
const [
|
|
90
|
-
const
|
|
91
|
-
const
|
|
92
|
-
const
|
|
93
|
-
const
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
single.
|
|
118
|
-
single.
|
|
119
|
-
single.
|
|
120
|
-
single.
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
if (
|
|
190
|
-
|
|
191
|
-
}
|
|
192
|
-
if (
|
|
193
|
-
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
logger.
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
const
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
TEMP_ROT
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
[
|
|
269
|
-
[
|
|
270
|
-
[
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
const
|
|
300
|
-
const
|
|
301
|
-
const
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
1
|
+
import { unzipSync, zipSync } from 'fflate';
|
|
2
|
+
import { ColIdx } from '../SplatData.js';
|
|
3
|
+
import { clamp, extractFromRootDir, isUrl, logger, mortonSort } from '../utils/index.js';
|
|
4
|
+
import { decodeWebP, encodeWebP, WebPLosslessProfile } from '../native/index.js';
|
|
5
|
+
import { SH_C0, SH_MAPS } from '../constant.js';
|
|
6
|
+
const TEMP_ROT = new Array(4);
|
|
7
|
+
const PERM_TABLE = [
|
|
8
|
+
// original quat idx ---> actual storage idx
|
|
9
|
+
[0, 1, 2, 3],
|
|
10
|
+
[3, 1, 2, 0],
|
|
11
|
+
[1, 3, 2, 0],
|
|
12
|
+
[1, 2, 3, 0],
|
|
13
|
+
];
|
|
14
|
+
const COLOR_SCALE = SH_C0 / 0.15;
|
|
15
|
+
const SH_SCALE1 = 1 << 3;
|
|
16
|
+
const SH_SCALE2 = 1 << 4;
|
|
17
|
+
function logTransform(value) {
|
|
18
|
+
return Math.sign(value) * Math.log(Math.abs(value) + 1);
|
|
19
|
+
}
|
|
20
|
+
export class EszFile {
|
|
21
|
+
constructor() {
|
|
22
|
+
this.counts = 0;
|
|
23
|
+
this.shDegree = 0;
|
|
24
|
+
/**
|
|
25
|
+
* @internal
|
|
26
|
+
*/
|
|
27
|
+
this.refs = {};
|
|
28
|
+
}
|
|
29
|
+
async load(stream, contentLength) {
|
|
30
|
+
const buffer = new Uint8Array(contentLength);
|
|
31
|
+
const reader = stream.getReader();
|
|
32
|
+
let offset = 0;
|
|
33
|
+
while (true) {
|
|
34
|
+
const { done, value } = await reader.read();
|
|
35
|
+
if (done) {
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
buffer.set(value, offset);
|
|
39
|
+
offset += value.length;
|
|
40
|
+
}
|
|
41
|
+
this.refs = extractFromRootDir(unzipSync(buffer));
|
|
42
|
+
const metaBuffer = this.refs['meta.json'];
|
|
43
|
+
if (!metaBuffer) {
|
|
44
|
+
throw new Error('SOG meta.json not found in the zip archive.');
|
|
45
|
+
}
|
|
46
|
+
const meta = (this.meta = JSON.parse(new TextDecoder().decode(metaBuffer)));
|
|
47
|
+
this.version = meta.version;
|
|
48
|
+
this.counts = meta.counts;
|
|
49
|
+
this.shDegree = meta.shDegree;
|
|
50
|
+
}
|
|
51
|
+
async loadTexture(path) {
|
|
52
|
+
let buffer = this.refs[path];
|
|
53
|
+
if (!buffer) {
|
|
54
|
+
if (isUrl(path)) {
|
|
55
|
+
buffer = await fetch(path)
|
|
56
|
+
.then(res => res.arrayBuffer())
|
|
57
|
+
.then(buf => new Uint8Array(buf));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (!buffer) {
|
|
61
|
+
throw new Error(`Cannot load texture: ${path}`);
|
|
62
|
+
}
|
|
63
|
+
const { data, width, height } = decodeWebP(buffer);
|
|
64
|
+
return {
|
|
65
|
+
data: new Uint8Array(data),
|
|
66
|
+
width,
|
|
67
|
+
height,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
async read(stream, contentLength, data) {
|
|
71
|
+
await this.load(stream, contentLength);
|
|
72
|
+
const offset = await data.initBlock(this.counts, this.shDegree);
|
|
73
|
+
const { resources } = this.meta;
|
|
74
|
+
this.cached = await Promise.all([resources.means_l, resources.means_u, resources.scales, resources.quats, resources.sh0, resources.shN]
|
|
75
|
+
.filter(path => !!path)
|
|
76
|
+
.map(path => this.loadTexture(path)));
|
|
77
|
+
const setFn = data.set.bind(data);
|
|
78
|
+
const setShFn = data.setShN.bind(data);
|
|
79
|
+
const SCALE_LUT = new Float32Array(256);
|
|
80
|
+
for (let i = 0; i < 256; i++) {
|
|
81
|
+
SCALE_LUT[i] = Math.exp(i / 16 - 10);
|
|
82
|
+
}
|
|
83
|
+
const COLOR_LUT = new Float32Array(256);
|
|
84
|
+
for (let i = 0; i < 256; i++) {
|
|
85
|
+
COLOR_LUT[i] = (i / 255 - 0.5) * COLOR_SCALE + 0.5;
|
|
86
|
+
}
|
|
87
|
+
const { meta: { box }, counts, shDegree, cached, } = this;
|
|
88
|
+
const [means_l, means_u, scales, quats, color, shN] = cached.map(v => v.data);
|
|
89
|
+
const { min: [centerMinX, centerMinY, centerMinZ], max: [centerMaxX, centerMaxY, centerMaxZ], } = box;
|
|
90
|
+
const rangeX = (centerMaxX - centerMinX) / 65535;
|
|
91
|
+
const rangeY = (centerMaxY - centerMinY) / 65535;
|
|
92
|
+
const rangeZ = (centerMaxZ - centerMinZ) / 65535;
|
|
93
|
+
const single = {
|
|
94
|
+
x: 0,
|
|
95
|
+
y: 0,
|
|
96
|
+
z: 0,
|
|
97
|
+
sx: 0,
|
|
98
|
+
sy: 0,
|
|
99
|
+
sz: 0,
|
|
100
|
+
qx: 0,
|
|
101
|
+
qy: 0,
|
|
102
|
+
qz: 0,
|
|
103
|
+
qw: 0,
|
|
104
|
+
r: 0,
|
|
105
|
+
g: 0,
|
|
106
|
+
b: 0,
|
|
107
|
+
a: 0,
|
|
108
|
+
shN: [],
|
|
109
|
+
};
|
|
110
|
+
for (let i = 0; i < counts; i++) {
|
|
111
|
+
const i4 = i * 4;
|
|
112
|
+
const x = centerMinX + rangeX * (means_l[i4 + 0] + (means_u[i4 + 0] << 8));
|
|
113
|
+
const y = centerMinY + rangeY * (means_l[i4 + 1] + (means_u[i4 + 1] << 8));
|
|
114
|
+
const z = centerMinZ + rangeZ * (means_l[i4 + 2] + (means_u[i4 + 2] << 8));
|
|
115
|
+
single.x = Math.sign(x) * (Math.exp(Math.abs(x)) - 1);
|
|
116
|
+
single.y = Math.sign(y) * (Math.exp(Math.abs(y)) - 1);
|
|
117
|
+
single.z = Math.sign(z) * (Math.exp(Math.abs(z)) - 1);
|
|
118
|
+
single.sx = SCALE_LUT[scales[i4 + 0]];
|
|
119
|
+
single.sy = SCALE_LUT[scales[i4 + 1]];
|
|
120
|
+
single.sz = SCALE_LUT[scales[i4 + 2]];
|
|
121
|
+
TEMP_ROT[0] = (quats[i4 + 0] / 255 - 0.5) * Math.SQRT2;
|
|
122
|
+
TEMP_ROT[1] = (quats[i4 + 1] / 255 - 0.5) * Math.SQRT2;
|
|
123
|
+
TEMP_ROT[2] = (quats[i4 + 2] / 255 - 0.5) * Math.SQRT2;
|
|
124
|
+
TEMP_ROT[3] = Math.sqrt(Math.max(0, 1.0 - TEMP_ROT[0] * TEMP_ROT[0] - TEMP_ROT[1] * TEMP_ROT[1] - TEMP_ROT[2] * TEMP_ROT[2]));
|
|
125
|
+
const PERM = PERM_TABLE[quats[i4 + 3] - 252];
|
|
126
|
+
single.qx = TEMP_ROT[PERM[0]];
|
|
127
|
+
single.qy = TEMP_ROT[PERM[1]];
|
|
128
|
+
single.qz = TEMP_ROT[PERM[2]];
|
|
129
|
+
single.qw = TEMP_ROT[PERM[3]];
|
|
130
|
+
single.r = COLOR_LUT[color[i4 + 0]];
|
|
131
|
+
single.g = COLOR_LUT[color[i4 + 1]];
|
|
132
|
+
single.b = COLOR_LUT[color[i4 + 2]];
|
|
133
|
+
single.a = color[i4 + 3] / 255;
|
|
134
|
+
setFn(offset + i, single);
|
|
135
|
+
}
|
|
136
|
+
if (shN) {
|
|
137
|
+
const shCounts = SH_MAPS[shDegree];
|
|
138
|
+
const shCoeffs = shCounts / 3;
|
|
139
|
+
const sh = new Array(shCounts);
|
|
140
|
+
for (let i = 0; i < counts; i++) {
|
|
141
|
+
const o = i * shCounts;
|
|
142
|
+
for (let j = 0; j < shCoeffs; j++) {
|
|
143
|
+
sh[o + j * 3 + 0] = (shN[(i * shCoeffs + j) * 4 + 0] - 128) / 128;
|
|
144
|
+
sh[o + j * 3 + 1] = (shN[(i * shCoeffs + j) * 4 + 1] - 128) / 128;
|
|
145
|
+
sh[o + j * 3 + 2] = (shN[(i * shCoeffs + j) * 4 + 2] - 128) / 128;
|
|
146
|
+
}
|
|
147
|
+
setShFn(offset + i, sh);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
data.finishBlock();
|
|
151
|
+
}
|
|
152
|
+
async write(stream, data, indices = mortonSort(data)) {
|
|
153
|
+
const { counts, shDegree, shCounts, table } = data;
|
|
154
|
+
const width = Math.ceil(Math.sqrt(counts) / 4) * 4;
|
|
155
|
+
const height = Math.ceil(counts / width / 4) * 4;
|
|
156
|
+
const webPProfile = new WebPLosslessProfile();
|
|
157
|
+
const output = {};
|
|
158
|
+
const meta = {
|
|
159
|
+
version: 1,
|
|
160
|
+
counts,
|
|
161
|
+
shDegree,
|
|
162
|
+
box: {
|
|
163
|
+
min: [Infinity, Infinity, Infinity],
|
|
164
|
+
max: [-Infinity, -Infinity, -Infinity],
|
|
165
|
+
},
|
|
166
|
+
resources: {
|
|
167
|
+
means_l: 'means_l.webp',
|
|
168
|
+
means_u: 'means_u.webp',
|
|
169
|
+
scales: 'scales.webp',
|
|
170
|
+
quats: 'quats.webp',
|
|
171
|
+
sh0: 'sh0.webp',
|
|
172
|
+
},
|
|
173
|
+
};
|
|
174
|
+
const xCol = table[ColIdx.x];
|
|
175
|
+
const yCol = table[ColIdx.y];
|
|
176
|
+
const zCol = table[ColIdx.z];
|
|
177
|
+
// calculate minmax & transform
|
|
178
|
+
let minX = Infinity;
|
|
179
|
+
let minY = Infinity;
|
|
180
|
+
let minZ = Infinity;
|
|
181
|
+
let maxX = -Infinity;
|
|
182
|
+
let maxY = -Infinity;
|
|
183
|
+
let maxZ = -Infinity;
|
|
184
|
+
for (let i = 0; i < counts; i++) {
|
|
185
|
+
const idx = indices[i];
|
|
186
|
+
const x = xCol[idx];
|
|
187
|
+
const y = yCol[idx];
|
|
188
|
+
const z = zCol[idx];
|
|
189
|
+
if (x < minX) {
|
|
190
|
+
minX = x;
|
|
191
|
+
}
|
|
192
|
+
if (x > maxX) {
|
|
193
|
+
maxX = x;
|
|
194
|
+
}
|
|
195
|
+
if (y < minY) {
|
|
196
|
+
minY = y;
|
|
197
|
+
}
|
|
198
|
+
if (y > maxY) {
|
|
199
|
+
maxY = y;
|
|
200
|
+
}
|
|
201
|
+
if (z < minZ) {
|
|
202
|
+
minZ = z;
|
|
203
|
+
}
|
|
204
|
+
if (z > maxZ) {
|
|
205
|
+
maxZ = z;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
meta.box.min = [minX, minY, minZ];
|
|
209
|
+
meta.box.max = [maxX, maxY, maxZ];
|
|
210
|
+
{
|
|
211
|
+
logger.time('ESZ encoding means');
|
|
212
|
+
minX = logTransform(minX);
|
|
213
|
+
minY = logTransform(minY);
|
|
214
|
+
minZ = logTransform(minZ);
|
|
215
|
+
maxX = logTransform(maxX);
|
|
216
|
+
maxY = logTransform(maxY);
|
|
217
|
+
maxZ = logTransform(maxZ);
|
|
218
|
+
const scaleX = 65535 / Math.max(maxX - minX, 1e-9);
|
|
219
|
+
const scaleY = 65535 / Math.max(maxY - minY, 1e-9);
|
|
220
|
+
const scaleZ = 65535 / Math.max(maxZ - minZ, 1e-9);
|
|
221
|
+
const meansL = new Uint8Array(width * height * 4).fill(0xff);
|
|
222
|
+
const meansU = new Uint8Array(width * height * 4).fill(0xff);
|
|
223
|
+
for (let i = 0; i < indices.length; i++) {
|
|
224
|
+
const idx = indices[i];
|
|
225
|
+
const x = (logTransform(xCol[idx]) - minX) * scaleX;
|
|
226
|
+
const y = (logTransform(yCol[idx]) - minY) * scaleY;
|
|
227
|
+
const z = (logTransform(zCol[idx]) - minZ) * scaleZ;
|
|
228
|
+
meansL[i * 4 + 0] = x & 0xff;
|
|
229
|
+
meansL[i * 4 + 1] = y & 0xff;
|
|
230
|
+
meansL[i * 4 + 2] = z & 0xff;
|
|
231
|
+
meansU[i * 4 + 0] = (x >> 8) & 0xff;
|
|
232
|
+
meansU[i * 4 + 1] = (y >> 8) & 0xff;
|
|
233
|
+
meansU[i * 4 + 2] = (z >> 8) & 0xff;
|
|
234
|
+
}
|
|
235
|
+
output['means_l.webp'] = encodeWebP(meansL, width, height, webPProfile);
|
|
236
|
+
output['means_u.webp'] = encodeWebP(meansU, width, height, webPProfile);
|
|
237
|
+
logger.timeEnd('ESZ encoding means');
|
|
238
|
+
}
|
|
239
|
+
{
|
|
240
|
+
logger.time('ESZ encoding scales');
|
|
241
|
+
const sxCol = table[ColIdx.sx];
|
|
242
|
+
const syCol = table[ColIdx.sy];
|
|
243
|
+
const szCol = table[ColIdx.sz];
|
|
244
|
+
const scales = new Uint8Array(width * height * 4).fill(0xff);
|
|
245
|
+
for (let i = 0; i < counts; i++) {
|
|
246
|
+
const idx = indices[i];
|
|
247
|
+
scales[i * 4 + 0] = clamp(Math.round((Math.log(sxCol[idx]) + 10) * 16), 0, 255);
|
|
248
|
+
scales[i * 4 + 1] = clamp(Math.round((Math.log(syCol[idx]) + 10) * 16), 0, 255);
|
|
249
|
+
scales[i * 4 + 2] = clamp(Math.round((Math.log(szCol[idx]) + 10) * 16), 0, 255);
|
|
250
|
+
}
|
|
251
|
+
output['scales.webp'] = encodeWebP(scales, width, height, webPProfile);
|
|
252
|
+
logger.timeEnd('ESZ encoding scales');
|
|
253
|
+
}
|
|
254
|
+
{
|
|
255
|
+
logger.time('ESZ encoding quats');
|
|
256
|
+
const qxCol = table[ColIdx.qx];
|
|
257
|
+
const qyCol = table[ColIdx.qy];
|
|
258
|
+
const qzCol = table[ColIdx.qz];
|
|
259
|
+
const qwCol = table[ColIdx.qw];
|
|
260
|
+
const quats = new Uint8Array(width * height * 4);
|
|
261
|
+
for (let i = 0; i < counts; i++) {
|
|
262
|
+
const idx = indices[i];
|
|
263
|
+
TEMP_ROT[0] = qwCol[idx];
|
|
264
|
+
TEMP_ROT[1] = qxCol[idx];
|
|
265
|
+
TEMP_ROT[2] = qyCol[idx];
|
|
266
|
+
TEMP_ROT[3] = qzCol[idx];
|
|
267
|
+
const l = Math.sqrt(TEMP_ROT[0] * TEMP_ROT[0] +
|
|
268
|
+
TEMP_ROT[1] * TEMP_ROT[1] +
|
|
269
|
+
TEMP_ROT[2] * TEMP_ROT[2] +
|
|
270
|
+
TEMP_ROT[3] * TEMP_ROT[3]);
|
|
271
|
+
TEMP_ROT.forEach((v, j) => {
|
|
272
|
+
TEMP_ROT[j] = v / l;
|
|
273
|
+
});
|
|
274
|
+
const maxComp = TEMP_ROT.reduce((v, _, i) => (Math.abs(TEMP_ROT[i]) > Math.abs(TEMP_ROT[v]) ? i : v), 0);
|
|
275
|
+
if (TEMP_ROT[maxComp] < 0) {
|
|
276
|
+
TEMP_ROT.forEach((_, j) => {
|
|
277
|
+
TEMP_ROT[j] *= -1;
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
TEMP_ROT.forEach((_, j) => (TEMP_ROT[j] *= Math.SQRT2));
|
|
281
|
+
const PERM = [
|
|
282
|
+
[1, 2, 3],
|
|
283
|
+
[0, 2, 3],
|
|
284
|
+
[0, 1, 3],
|
|
285
|
+
[0, 1, 2],
|
|
286
|
+
][maxComp];
|
|
287
|
+
quats[i * 4] = (TEMP_ROT[PERM[0]] * 0.5 + 0.5) * 255;
|
|
288
|
+
quats[i * 4 + 1] = (TEMP_ROT[PERM[1]] * 0.5 + 0.5) * 255;
|
|
289
|
+
quats[i * 4 + 2] = (TEMP_ROT[PERM[2]] * 0.5 + 0.5) * 255;
|
|
290
|
+
quats[i * 4 + 3] = 252 + maxComp;
|
|
291
|
+
}
|
|
292
|
+
output['quats.webp'] = encodeWebP(quats, width, height, webPProfile);
|
|
293
|
+
logger.timeEnd('ESZ encoding quats');
|
|
294
|
+
}
|
|
295
|
+
{
|
|
296
|
+
logger.time('ESZ encoding sh0');
|
|
297
|
+
const rCol = table[ColIdx.r];
|
|
298
|
+
const gCol = table[ColIdx.g];
|
|
299
|
+
const bCol = table[ColIdx.b];
|
|
300
|
+
const aCol = table[ColIdx.a];
|
|
301
|
+
const sh0 = new Uint8Array(width * height * 4).fill(0xff);
|
|
302
|
+
for (let i = 0; i < counts; i++) {
|
|
303
|
+
const idx = indices[i];
|
|
304
|
+
sh0[i * 4 + 0] = clamp(Math.round(((rCol[idx] - 0.5) / COLOR_SCALE + 0.5) * 255), 0, 255);
|
|
305
|
+
sh0[i * 4 + 1] = clamp(Math.round(((gCol[idx] - 0.5) / COLOR_SCALE + 0.5) * 255), 0, 255);
|
|
306
|
+
sh0[i * 4 + 2] = clamp(Math.round(((bCol[idx] - 0.5) / COLOR_SCALE + 0.5) * 255), 0, 255);
|
|
307
|
+
sh0[i * 4 + 3] = clamp(Math.round(aCol[idx] * 255), 0, 255);
|
|
308
|
+
}
|
|
309
|
+
output['sh0.webp'] = encodeWebP(sh0, width, height, webPProfile);
|
|
310
|
+
logger.timeEnd('ESZ encoding sh0');
|
|
311
|
+
}
|
|
312
|
+
if (shDegree > 0) {
|
|
313
|
+
logger.time('ESZ encoding shN');
|
|
314
|
+
const shCoeffs = shCounts / 3;
|
|
315
|
+
const pixels = counts * shCoeffs;
|
|
316
|
+
const shNWidth = Math.ceil(Math.sqrt(pixels) / 4) * 4;
|
|
317
|
+
const shNHeight = Math.ceil(pixels / shNWidth / 4) * 4;
|
|
318
|
+
const shN = new Uint8Array(shNWidth * shNHeight * 4).fill(0xff);
|
|
319
|
+
for (let i = 0; i < counts; i++) {
|
|
320
|
+
const idx = indices[i];
|
|
321
|
+
const o = i * shCoeffs;
|
|
322
|
+
for (let j = 0; j < shCoeffs; j++) {
|
|
323
|
+
const scale = j < 3 ? SH_SCALE1 : SH_SCALE2;
|
|
324
|
+
shN[(o + j) * 4 + 0] = clamp(Math.floor((Math.round(table[ColIdx.shOffset + (j * 3 + 0)][idx] * 128) + 128 + scale / 2) / scale) * scale, 0, 255);
|
|
325
|
+
shN[(o + j) * 4 + 1] = clamp(Math.floor((Math.round(table[ColIdx.shOffset + (j * 3 + 1)][idx] * 128) + 128 + scale / 2) / scale) * scale, 0, 255);
|
|
326
|
+
shN[(o + j) * 4 + 2] = clamp(Math.floor((Math.round(table[ColIdx.shOffset + (j * 3 + 2)][idx] * 128) + 128 + scale / 2) / scale) * scale, 0, 255);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
output['shN.webp'] = encodeWebP(shN, shNWidth, shNHeight, webPProfile);
|
|
330
|
+
meta.resources.shN = 'shN.webp';
|
|
331
|
+
logger.timeEnd('ESZ encoding shN');
|
|
332
|
+
}
|
|
333
|
+
output['meta.json'] = Buffer.from(JSON.stringify(meta), 'utf-8');
|
|
334
|
+
const result = zipSync(output);
|
|
335
|
+
await stream.getWriter().write(result);
|
|
336
|
+
}
|
|
337
|
+
}
|