From cf655cba7883349516d6d4057bac3f069ab97df6 Mon Sep 17 00:00:00 2001 From: Donovan Hutchence Date: Wed, 23 Oct 2024 10:17:08 +0100 Subject: [PATCH] stream fixes --- src/file-handler.ts | 29 +++++++++++++++++++---------- src/splat-serialize.ts | 9 +++++---- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/file-handler.ts b/src/file-handler.ts index 2b42e85..1fd6b37 100644 --- a/src/file-handler.ts +++ b/src/file-handler.ts @@ -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', { diff --git a/src/splat-serialize.ts b/src/splat-serialize.ts index d272724..2b80928 100644 --- a/src/splat-serialize.ts +++ b/src/splat-serialize.ts @@ -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}`; @@ -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`, @@ -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) => { @@ -717,7 +718,7 @@ const serializeSplat = async (splats: Splat[], write: WriteFunc) => { } } - await write(result); + await write(result, true); }; export {