Spaces:
Running
Running
File size: 15,144 Bytes
352fb85 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 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 190 191 192 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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 |
import { Scene } from "../../../core/Scene";
import { Splat } from "../../../splats/Splat";
import DataWorker from "web-worker:./DataWorker.ts";
import loadWasm from "../../../wasm/data";
import { Matrix4 } from "../../../math/Matrix4";
class RenderData {
public dataChanged = false;
public transformsChanged = false;
public colorTransformsChanged = false;
private _splatIndices: Map<Splat, number>;
private _offsets: Map<Splat, number>;
private _data: Uint32Array;
private _width: number;
private _height: number;
private _transforms: Float32Array;
private _transformsWidth: number;
private _transformsHeight: number;
private _transformIndices: Uint32Array;
private _transformIndicesWidth: number;
private _transformIndicesHeight: number;
private _colorTransforms: Float32Array;
private _colorTransformsWidth: number;
private _colorTransformsHeight: number;
private _colorTransformIndices: Uint32Array;
private _colorTransformIndicesWidth: number;
private _colorTransformIndicesHeight: number;
private _positions: Float32Array;
private _rotations: Float32Array;
private _scales: Float32Array;
private _vertexCount: number;
private _updating: Set<Splat> = new Set<Splat>();
private _dirty: Set<Splat> = new Set<Splat>();
private _worker: Worker;
getSplat: (index: number) => Splat | null;
getLocalIndex: (splat: Splat, index: number) => number;
markDirty: (splat: Splat) => void;
rebuild: () => void;
dispose: () => void;
constructor(scene: Scene) {
let vertexCount = 0;
let splatIndex = 0;
this._splatIndices = new Map<Splat, number>();
this._offsets = new Map<Splat, number>();
const lookup = new Map<number, Splat>();
for (const object of scene.objects) {
if (object instanceof Splat) {
this._splatIndices.set(object, splatIndex);
this._offsets.set(object, vertexCount);
lookup.set(vertexCount, object);
vertexCount += object.data.vertexCount;
splatIndex++;
}
}
this._vertexCount = vertexCount;
this._width = 2048;
this._height = Math.ceil((2 * this.vertexCount) / this.width);
this._data = new Uint32Array(this.width * this.height * 4);
this._transformsWidth = 5;
this._transformsHeight = lookup.size;
this._transforms = new Float32Array(this._transformsWidth * this._transformsHeight * 4);
this._transformIndicesWidth = 1024;
this._transformIndicesHeight = Math.ceil(this.vertexCount / this._transformIndicesWidth);
this._transformIndices = new Uint32Array(this._transformIndicesWidth * this._transformIndicesHeight);
this._colorTransformsWidth = 4;
this._colorTransformsHeight = 64;
this._colorTransforms = new Float32Array(this._colorTransformsWidth * this._colorTransformsHeight * 4);
this._colorTransforms.fill(0);
this._colorTransforms[0] = 1;
this._colorTransforms[5] = 1;
this._colorTransforms[10] = 1;
this._colorTransforms[15] = 1;
this._colorTransformIndicesWidth = 1024;
this._colorTransformIndicesHeight = Math.ceil(this.vertexCount / this._colorTransformIndicesWidth);
this._colorTransformIndices = new Uint32Array(
this._colorTransformIndicesWidth * this._colorTransformIndicesHeight,
);
this.colorTransformIndices.fill(0);
this._positions = new Float32Array(this.vertexCount * 3);
this._rotations = new Float32Array(this.vertexCount * 4);
this._scales = new Float32Array(this.vertexCount * 3);
this._worker = new DataWorker();
const updateTransform = (splat: Splat) => {
const splatIndex = this._splatIndices.get(splat) as number;
this._transforms.set(splat.transform.buffer, splatIndex * 20);
this._transforms[splatIndex * 20 + 16] = splat.selected ? 1 : 0;
splat.positionChanged = false;
splat.rotationChanged = false;
splat.scaleChanged = false;
splat.selectedChanged = false;
this.transformsChanged = true;
};
const updateColorTransforms = () => {
let colorTransformsChanged = false;
for (const splat of this._splatIndices.keys()) {
if (splat.colorTransformChanged) {
colorTransformsChanged = true;
break;
}
}
if (!colorTransformsChanged) {
return;
}
const colorTransformsMap: Matrix4[] = [new Matrix4()];
this._colorTransformIndices.fill(0);
let i = 1;
for (const splat of this._splatIndices.keys()) {
const offset = this._offsets.get(splat) as number;
for (const colorTransform of splat.colorTransforms) {
if (!colorTransformsMap.includes(colorTransform)) {
colorTransformsMap.push(colorTransform);
i++;
}
}
for (const index of splat.colorTransformsMap.keys()) {
const colorTransformIndex = splat.colorTransformsMap.get(index) as number;
this._colorTransformIndices[index + offset] = colorTransformIndex + i - 1;
}
splat.colorTransformChanged = false;
}
for (let index = 0; index < colorTransformsMap.length; index++) {
const colorTransform = colorTransformsMap[index];
this._colorTransforms.set(colorTransform.buffer, index * 16);
}
this.colorTransformsChanged = true;
};
this._worker.onmessage = (e) => {
if (e.data.response) {
const response = e.data.response;
const splat = lookup.get(response.offset) as Splat;
updateTransform(splat);
updateColorTransforms();
const splatIndex = this._splatIndices.get(splat) as number;
for (let i = 0; i < splat.data.vertexCount; i++) {
this._transformIndices[response.offset + i] = splatIndex;
}
this._data.set(response.data, response.offset * 8);
splat.data.reattach(
response.positions,
response.rotations,
response.scales,
response.colors,
response.selection,
);
this._positions.set(response.worldPositions, response.offset * 3);
this._rotations.set(response.worldRotations, response.offset * 4);
this._scales.set(response.worldScales, response.offset * 3);
this._updating.delete(splat);
splat.selectedChanged = false;
this.dataChanged = true;
}
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let wasmModule: any;
async function initWasm() {
wasmModule = await loadWasm();
}
initWasm();
async function waitForWasm() {
while (!wasmModule) {
await new Promise((resolve) => setTimeout(resolve, 0));
}
}
const buildImmediate = (splat: Splat) => {
if (!wasmModule) {
waitForWasm().then(() => {
buildImmediate(splat);
});
return;
}
updateTransform(splat);
const positionsPtr = wasmModule._malloc(3 * splat.data.vertexCount * 4);
const rotationsPtr = wasmModule._malloc(4 * splat.data.vertexCount * 4);
const scalesPtr = wasmModule._malloc(3 * splat.data.vertexCount * 4);
const colorsPtr = wasmModule._malloc(4 * splat.data.vertexCount);
const selectionPtr = wasmModule._malloc(splat.data.vertexCount);
const dataPtr = wasmModule._malloc(8 * splat.data.vertexCount * 4);
const worldPositionsPtr = wasmModule._malloc(3 * splat.data.vertexCount * 4);
const worldRotationsPtr = wasmModule._malloc(4 * splat.data.vertexCount * 4);
const worldScalesPtr = wasmModule._malloc(3 * splat.data.vertexCount * 4);
wasmModule.HEAPF32.set(splat.data.positions, positionsPtr / 4);
wasmModule.HEAPF32.set(splat.data.rotations, rotationsPtr / 4);
wasmModule.HEAPF32.set(splat.data.scales, scalesPtr / 4);
wasmModule.HEAPU8.set(splat.data.colors, colorsPtr);
wasmModule.HEAPU8.set(splat.data.selection, selectionPtr);
wasmModule._pack(
splat.selected,
splat.data.vertexCount,
positionsPtr,
rotationsPtr,
scalesPtr,
colorsPtr,
selectionPtr,
dataPtr,
worldPositionsPtr,
worldRotationsPtr,
worldScalesPtr,
);
const outData = new Uint32Array(wasmModule.HEAPU32.buffer, dataPtr, splat.data.vertexCount * 8);
const worldPositions = new Float32Array(
wasmModule.HEAPF32.buffer,
worldPositionsPtr,
splat.data.vertexCount * 3,
);
const worldRotations = new Float32Array(
wasmModule.HEAPF32.buffer,
worldRotationsPtr,
splat.data.vertexCount * 4,
);
const worldScales = new Float32Array(wasmModule.HEAPF32.buffer, worldScalesPtr, splat.data.vertexCount * 3);
const splatIndex = this._splatIndices.get(splat) as number;
const offset = this._offsets.get(splat) as number;
for (let i = 0; i < splat.data.vertexCount; i++) {
this._transformIndices[offset + i] = splatIndex;
}
this._data.set(outData, offset * 8);
this._positions.set(worldPositions, offset * 3);
this._rotations.set(worldRotations, offset * 4);
this._scales.set(worldScales, offset * 3);
wasmModule._free(positionsPtr);
wasmModule._free(rotationsPtr);
wasmModule._free(scalesPtr);
wasmModule._free(colorsPtr);
wasmModule._free(selectionPtr);
wasmModule._free(dataPtr);
wasmModule._free(worldPositionsPtr);
wasmModule._free(worldRotationsPtr);
wasmModule._free(worldScalesPtr);
this.dataChanged = true;
this.colorTransformsChanged = true;
};
const build = (splat: Splat) => {
if (splat.positionChanged || splat.rotationChanged || splat.scaleChanged || splat.selectedChanged) {
updateTransform(splat);
}
if (splat.colorTransformChanged) {
updateColorTransforms();
}
if (!splat.data.changed || splat.data.detached) return;
const serializedSplat = {
position: new Float32Array(splat.position.flat()),
rotation: new Float32Array(splat.rotation.flat()),
scale: new Float32Array(splat.scale.flat()),
selected: splat.selected,
vertexCount: splat.data.vertexCount,
positions: splat.data.positions,
rotations: splat.data.rotations,
scales: splat.data.scales,
colors: splat.data.colors,
selection: splat.data.selection,
offset: this._offsets.get(splat) as number,
};
this._worker.postMessage(
{
splat: serializedSplat,
},
[
serializedSplat.position.buffer,
serializedSplat.rotation.buffer,
serializedSplat.scale.buffer,
serializedSplat.positions.buffer,
serializedSplat.rotations.buffer,
serializedSplat.scales.buffer,
serializedSplat.colors.buffer,
serializedSplat.selection.buffer,
],
);
this._updating.add(splat);
splat.data.detached = true;
};
this.getSplat = (index: number) => {
let splat = null;
for (const [key, value] of this._offsets) {
if (index >= value) {
splat = key;
} else {
break;
}
}
return splat;
};
this.getLocalIndex = (splat: Splat, index: number) => {
const offset = this._offsets.get(splat) as number;
return index - offset;
};
this.markDirty = (splat: Splat) => {
this._dirty.add(splat);
};
this.rebuild = () => {
for (const splat of this._dirty) {
build(splat);
}
this._dirty.clear();
};
this.dispose = () => {
this._worker.terminate();
};
for (const splat of this._splatIndices.keys()) {
buildImmediate(splat);
}
updateColorTransforms();
}
get offsets() {
return this._offsets;
}
get data() {
return this._data;
}
get width() {
return this._width;
}
get height() {
return this._height;
}
get transforms() {
return this._transforms;
}
get transformsWidth() {
return this._transformsWidth;
}
get transformsHeight() {
return this._transformsHeight;
}
get transformIndices() {
return this._transformIndices;
}
get transformIndicesWidth() {
return this._transformIndicesWidth;
}
get transformIndicesHeight() {
return this._transformIndicesHeight;
}
get colorTransforms() {
return this._colorTransforms;
}
get colorTransformsWidth() {
return this._colorTransformsWidth;
}
get colorTransformsHeight() {
return this._colorTransformsHeight;
}
get colorTransformIndices() {
return this._colorTransformIndices;
}
get colorTransformIndicesWidth() {
return this._colorTransformIndicesWidth;
}
get colorTransformIndicesHeight() {
return this._colorTransformIndicesHeight;
}
get positions() {
return this._positions;
}
get rotations() {
return this._rotations;
}
get scales() {
return this._scales;
}
get vertexCount() {
return this._vertexCount;
}
get needsRebuild() {
return this._dirty.size > 0;
}
get updating() {
return this._updating.size > 0;
}
}
export { RenderData };
|