complete initial models list

This commit is contained in:
Yeicor
2024-02-24 20:03:14 +01:00
parent a72cc8dd09
commit f6c0d5f936
7 changed files with 202 additions and 56 deletions

View File

@@ -1,37 +1,46 @@
import type {ModelViewerElement} from '@google/model-viewer';
import type {ModelScene} from "@google/model-viewer/lib/three-components/ModelScene";
import {Ref} from 'vue';
import {Ref, ShallowRef} from 'vue';
import {Document} from '@gltf-transform/core';
import {mergeFinalize, mergePartial, toBuffer} from "./gltf";
import {mergeFinalize, mergePartial, removeModel, toBuffer} from "./gltf";
/** This class helps manage SceneManagerData. All methods are static to support reactivity... */
export class SceneMgr {
/** Loads a GLB model from a URL and adds it to the viewer or replaces it if the names match */
static async loadModel(sceneUrl: Ref<string>, document: Document, name: string, url: string): Promise<Document> {
static async loadModel(sceneUrl: Ref<string>, document: ShallowRef<Document>, name: string, url: string) {
let loadStart = performance.now();
// Start merging into the current document, replacing or adding as needed
document = await mergePartial(url, name, document);
document.value = await mergePartial(url, name, document.value);
// Display the final fully loaded model
document = await this.showCurrentDoc(sceneUrl, document);
await this.showCurrentDoc(sceneUrl, document);
console.log("Model", name, "loaded in", performance.now() - loadStart, "ms");
return document;
}
/** Serializes the current document into a GLB and updates the viewerSrc */
private static async showCurrentDoc(sceneUrl: Ref<string>, document: Document): Promise<Document> {
// Make sure the document is fully loaded and ready to be shown
document = await mergeFinalize(document);
/** Removes a model from the viewer */
static async removeModel(sceneUrl: Ref<string>, document: ShallowRef<Document>, name: string) {
let loadStart = performance.now();
// Serialize the document into a GLB and update the viewerSrc
let buffer = await toBuffer(document);
let blob = new Blob([buffer], {type: 'model/gltf-binary'});
//console.log("Showing current doc", document, "as", Array.from(buffer));
sceneUrl.value = URL.createObjectURL(blob);
// Remove the model from the document
document.value = await removeModel(name, document.value)
// Return the updated document
// Display the final fully loaded model
await this.showCurrentDoc(sceneUrl, document);
console.log("Model", name, "removed in", performance.now() - loadStart, "ms");
return document;
}
/** Serializes the current document into a GLB and updates the viewerSrc */
private static async showCurrentDoc(sceneUrl: Ref<string>, document: ShallowRef<Document>) {
// Make sure the document is fully loaded and ready to be shown
document.value = await mergeFinalize(document.value);
// Serialize the document into a GLB and update the viewerSrc
let buffer = await toBuffer(document.value);
let blob = new Blob([buffer], {type: 'model/gltf-binary'});
console.debug("Showing current doc", document, "as", Array.from(buffer));
sceneUrl.value = URL.createObjectURL(blob);
}
}