mirror of
https://github.com/yeicor-3d/yet-another-cad-viewer.git
synced 2025-12-20 14:37:03 +01:00
better networked model loading
This commit is contained in:
@@ -3,6 +3,7 @@ import type {ModelScene} from "@google/model-viewer/lib/three-components/ModelSc
|
||||
import {ref, Ref} from 'vue';
|
||||
import {Document} from '@gltf-transform/core';
|
||||
import {ModelViewerInfo} from "./viewer/ModelViewerWrapper.vue";
|
||||
import {splitGlbs} from "../models/glb/glbs";
|
||||
|
||||
type SceneManagerData = {
|
||||
/** When updated, forces the viewer to load a new model replacing the current one */
|
||||
@@ -32,6 +33,22 @@ export class SceneMgr {
|
||||
});
|
||||
}
|
||||
|
||||
/** Loads a GLB/GLBS model from a URL and adds it to the viewer or replaces it if the names match */
|
||||
static async loadModel(data: SceneManagerData, name: string, url: string) {
|
||||
let response = await fetch(url);
|
||||
if (!response.ok) throw new Error("Failed to fetch model: " + response.statusText);
|
||||
let glbsSplitter = splitGlbs(response.body!);
|
||||
let {value: numChunks} = await glbsSplitter.next();
|
||||
console.log("Loading model with", numChunks, "chunks");
|
||||
while (true) {
|
||||
let {value: chunk, done} = await glbsSplitter.next();
|
||||
if (done) break;
|
||||
console.log("Got chunk", chunk);
|
||||
// Override the current model with the new one
|
||||
data.viewerSrc = URL.createObjectURL(new Blob([chunk], {type: 'model/gltf-binary'}));
|
||||
}
|
||||
}
|
||||
|
||||
/** Should be called any model finishes loading successfully (after a viewerSrc update) */
|
||||
static onload(data: SceneManagerData, info: typeof ModelViewerInfo) {
|
||||
console.log("ModelViewer loaded", info);
|
||||
|
||||
Reference in New Issue
Block a user