Skip to content

Commit

Permalink
Stream fixes (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
slimbuck authored Oct 23, 2024
1 parent 61dc335 commit 79ecc72
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
29 changes: 19 additions & 10 deletions src/file-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,21 +362,30 @@ const initFileHandler = async (scene: Scene, events: Events, dropTarget: HTMLEle
await stream.truncate(cursor);
await stream.close();
} else if (options.filename) {
// (safari): concatenate data into single buffer
// safari and firefox: concatenate data into single buffer for old-school download
let data: Uint8Array = null;
let cursor = 0;
let data = new Uint8Array(1024 * 1024);

const writeFunc = (chunk: Uint8Array) => {
if (cursor + chunk.byteLength > data.byteLength) {
const newData = new Uint8Array(data.byteLength * 2);
newData.set(data);
data = newData;
const writeFunc = (chunk: Uint8Array, finalWrite?: boolean) => {
if (!data) {
data = finalWrite ? chunk : chunk.slice();
cursor = chunk.byteLength;
} else {
if (data.byteLength < cursor + chunk.byteLength) {
let newSize = data.byteLength * 2;
while (newSize < cursor + chunk.byteLength) {
newSize *= 2;
}
const newData = new Uint8Array(newSize);
newData.set(data);
data = newData;
}
data.set(chunk, cursor);
cursor += chunk.byteLength;
}
data.set(chunk, cursor);
cursor += chunk.byteLength;
};
await writeScene(options.type, writeFunc);
download(options.filename, new Uint8Array(data.buffer, 0, cursor));
download(options.filename, (cursor === data.byteLength) ? data : new Uint8Array(data.buffer, 0, cursor));
}
} catch (err) {
events.invoke('showPopup', {
Expand Down
9 changes: 5 additions & 4 deletions src/splat-serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { SHRotation } from './sh-utils';
import { version } from '../package.json';

// async function for writing data
type WriteFunc = (data: Uint8Array) => void;
type WriteFunc = (data: Uint8Array, finalWrite?: boolean) => void;

const generatedByString = `Generated by SuperSplat ${version}`;

Expand Down Expand Up @@ -146,7 +146,8 @@ const serializePly = async (splats: Splat[], write: WriteFunc) => {
const headerText = [
`ply`,
`format binary_little_endian 1.0`,
`comment ${generatedByString}`,
// FIXME: disable for now due to other tooling not supporting any header
// `comment ${generatedByString}`,
`element vertex ${totalSplats}`,
propNames.map(p => `property float ${p}`),
`end_header`,
Expand Down Expand Up @@ -645,7 +646,7 @@ const serializePlyCompressed = async (splats: Splat[], write: WriteFunc) => {
}
}

await write(result);
await write(result, true);
};

const serializeSplat = async (splats: Splat[], write: WriteFunc) => {
Expand Down Expand Up @@ -717,7 +718,7 @@ const serializeSplat = async (splats: Splat[], write: WriteFunc) => {
}
}

await write(result);
await write(result, true);
};

export {
Expand Down

0 comments on commit 79ecc72

Please sign in to comment.