Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Ability To Export Scene to Merged Data Buffer #88

Merged
merged 2 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 27 additions & 26 deletions src/core/Scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,49 +59,50 @@ class Scene extends EventDispatcher {
this.reset();
}

saveToFile(name: string | null = null, format: string | null = null) {
if (!document) return;

if (!format) {
format = "splat";
} else if (format !== "splat" && format !== "ply") {
throw new Error("Invalid format. Must be 'splat' or 'ply'");
}

if (!name) {
const now = new Date();
name = `scene-${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}.${format}`;
}

getMergedSceneDataBuffer(format: "splat" | "ply" = "splat"): ArrayBuffer {
const buffers: Uint8Array[] = [];
let vertexCount = 0;

for (const object of this.objects) {
object.applyRotation();
object.applyScale();
object.applyPosition();
if (object instanceof Splat) {
const buffer = object.data.serialize();
const splatClone = object.clone() as Splat;

splatClone.applyRotation();
splatClone.applyScale();
splatClone.applyPosition();
const buffer = splatClone.data.serialize();

buffers.push(buffer);
vertexCount += object.data.vertexCount;
vertexCount += splatClone.data.vertexCount;
}
}

const data = new Uint8Array(vertexCount * SplatData.RowLength);
const mergedSplatData = new Uint8Array(vertexCount * SplatData.RowLength);
let offset = 0;
for (const buffer of buffers) {
data.set(buffer, offset);
mergedSplatData.set(buffer, offset);
offset += buffer.length;
}

let blob;
if (format === "ply") {
const plyData = Converter.SplatToPLY(data.buffer, vertexCount);
blob = new Blob([plyData], { type: "application/octet-stream" });
} else {
blob = new Blob([data.buffer], { type: "application/octet-stream" });
return Converter.SplatToPLY(mergedSplatData.buffer, vertexCount);
}

return mergedSplatData.buffer;
}

saveToFile(name: string | null = null, format: "splat" | "ply" = "splat") {
if (!document) return;

if (!name) {
const now = new Date();
name = `scene-${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}.${format}`;
}

const mergedData = this.getMergedSceneDataBuffer(format);

const blob = new Blob([mergedData], { type: "application/octet-stream" });

const link = document.createElement("a");
link.download = name;
link.href = URL.createObjectURL(blob);
Expand Down
28 changes: 16 additions & 12 deletions src/splats/Splat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,24 @@ class Splat extends Object3D {
this.recalculateBounds();
}

saveToFile(name: string | null = null, format: string | null = null) {
saveToFile(name: string | null = null, format: "splat" | "ply" = "splat") {
if (!document) return;

if (!format) {
format = "splat";
} else if (format !== "splat" && format !== "ply") {
throw new Error("Invalid format. Must be 'splat' or 'ply'");
}

if (!name) {
const now = new Date();
name = `splat-${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}.${format}`;
}

this.applyRotation();
this.applyScale();
this.applyPosition();
const splatClone = this.clone();

splatClone.applyRotation();
splatClone.applyScale();
splatClone.applyPosition();

const data = this.data.serialize();
const data = splatClone.data.serialize();
let blob;
if (format === "ply") {
const plyData = Converter.SplatToPLY(data.buffer, this.data.vertexCount);
const plyData = Converter.SplatToPLY(data.buffer, splatClone.data.vertexCount);
blob = new Blob([plyData], { type: "application/octet-stream" });
} else {
blob = new Blob([data.buffer], { type: "application/octet-stream" });
Expand Down Expand Up @@ -127,6 +123,14 @@ class Splat extends Object3D {

return new Box3(center.subtract(size.divide(2)), center.add(size.divide(2)));
}

clone() {
const splat = new Splat(this.data.clone());
splat.position = this.position.clone();
splat.rotation = this.rotation.clone();
splat.scale = this.scale.clone();
return splat;
}
}

export { Splat };
10 changes: 10 additions & 0 deletions src/splats/SplatData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,16 @@ class SplatData {
get selection() {
return this._selection;
}

clone() {
return new SplatData(
this.vertexCount,
new Float32Array(this.positions),
new Float32Array(this.rotations),
new Float32Array(this.scales),
new Uint8Array(this.colors),
);
}
}

export { SplatData };
4 changes: 2 additions & 2 deletions src/wasm/data.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/wasm/sort.js

Large diffs are not rendered by default.

Loading