diff --git a/src/misc/scene.ts b/src/misc/scene.ts index f1405fe..8a4acbc 100644 --- a/src/misc/scene.ts +++ b/src/misc/scene.ts @@ -4,7 +4,7 @@ import {Ref, ref} from 'vue'; import {Document} from '@gltf-transform/core'; import {ModelViewerInfo} from "./viewer/ModelViewerWrapper.vue"; import {splitGlbs} from "../models/glb/glbs"; -import {merge, toBuffer} from "../models/glb/merge"; +import {mergeFinalize, mergePartial, toBuffer} from "../models/glb/merge"; import {settings} from "./settings"; export type SceneMgrRefData = { @@ -28,7 +28,7 @@ export class SceneMgr { } /** Creates a new SceneManagerData object */ - static newData(): [Ref, SceneMgrData] { + static newData(): [Ref, SceneMgrData] { let refData: any = ref({ viewerSrc: null, viewer: null, @@ -54,17 +54,11 @@ export class SceneMgr { console.log("Loading", name, "which has", numChunks, "GLB chunks"); // Start merging each chunk into the current document, replacing or adding as needed - let lastShow = performance.now(); while (true) { let {value: glbData, done} = await glbsSplitter.next(); if (done) break; - data.document = await merge(glbData, name, data.document); - - // Show the partial model while loading every once in a while - if (performance.now() - lastShow > settings.displayLoadingEveryMs) { - await this.showCurrentDoc(refData, data); - lastShow = performance.now(); - } + data.document = await mergePartial(glbData, name, data.document); + // TODO: Report load progress } // Display the final fully loaded model @@ -73,6 +67,7 @@ export class SceneMgr { /** Serializes the current document into a GLB and updates the viewerSrc */ private static async showCurrentDoc(refData: Ref, data: SceneMgrData) { + data.document = await mergeFinalize(data.document); let buffer = await toBuffer(data.document); let blob = new Blob([buffer], {type: 'model/gltf-binary'}); console.log("Showing current doc", data.document, "as", Array.from(buffer)); diff --git a/src/models/glb/merge.ts b/src/models/glb/merge.ts index df560ac..7a86d99 100644 --- a/src/models/glb/merge.ts +++ b/src/models/glb/merge.ts @@ -7,8 +7,10 @@ let io = new WebIO(); * Given the bytes of a GLB file and a parsed GLTF document, it parses and merges the GLB into the document. * * It can replace previous models in the document if the provided name matches the name of a previous model. + * + * Remember to call mergeFinalize after all models have been merged (slower required operations). */ -export async function merge(glb: Uint8Array, name: string, document: Document): Promise { +export async function mergePartial(glb: Uint8Array, name: string, document: Document): Promise { let newDoc = await io.readBinary(glb); @@ -18,12 +20,14 @@ export async function merge(glb: Uint8Array, name: string, document: Document): let merged = document.merge(newDoc); // noinspection TypeScriptValidateJSTypes - return await merged.transform(mergeScenes(), unpartition()); // Single scene & buffer required! + return await merged.transform(mergeScenes()); // Single scene & buffer required! +} +export async function mergeFinalize(document: Document): Promise { + return await document.transform(unpartition()); } export async function toBuffer(doc: Document): Promise { - return io.writeBinary(doc); }