mirror of
https://github.com/yeicor-3d/yet-another-cad-viewer.git
synced 2026-01-28 00:54:15 +01:00
complete initial models list
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import {Document, Scene, Transform, WebIO} from "@gltf-transform/core";
|
||||
import {Document, Scene, Transform, WebIO, Buffer} from "@gltf-transform/core";
|
||||
import {unpartition} from "@gltf-transform/functions";
|
||||
|
||||
let io = new WebIO();
|
||||
export let extrasNameKey = "__yacv_name";
|
||||
|
||||
/**
|
||||
* Loads a GLB model from a URL and adds it to the document or replaces it if the names match.
|
||||
@@ -31,32 +32,27 @@ export async function toBuffer(doc: Document): Promise<Uint8Array> {
|
||||
return io.writeBinary(doc);
|
||||
}
|
||||
|
||||
export async function removeModel(name: string, document: Document): Promise<Document> {
|
||||
return await document.transform(dropByName(name));
|
||||
}
|
||||
|
||||
/** Given a parsed GLTF document and a name, it forces the names of all elements to be identified by the name (or derivatives) */
|
||||
function setNames(name: string): Transform {
|
||||
return (doc: Document, _: any) => {
|
||||
// Do this automatically for all elements changing any name
|
||||
for (let elem of doc.getGraph().listEdges().map(e => e.getChild())) {
|
||||
// If setName is available, use it (preserving original names)
|
||||
elem.setName(name + "/" + elem.getName());
|
||||
}
|
||||
|
||||
// Special cases, specify the kind and number ID of primitives
|
||||
let i = 0;
|
||||
for (let mesh of doc.getRoot().listMeshes()) {
|
||||
for (let prim of mesh.listPrimitives()) {
|
||||
let kind = (prim.getMode() === WebGL2RenderingContext.POINTS ? "vertex" :
|
||||
(prim.getMode() === WebGL2RenderingContext.LINES ? "edge" : "face"));
|
||||
prim.setName(name + "/" + kind + "/" + (i++));
|
||||
}
|
||||
if (!elem.getExtras()) elem.setExtras({});
|
||||
elem.getExtras()[extrasNameKey] = name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Ensures that all elements with the given name are removed from the document */
|
||||
function dropByName(name: string): Transform {
|
||||
export function dropByName(name: string): Transform {
|
||||
return (doc: Document, _: any) => {
|
||||
for (let elem of doc.getGraph().listEdges().map(e => e.getChild())) {
|
||||
if (elem.getName().startsWith(name + "/") && !(elem instanceof Scene)) {
|
||||
if (elem.getExtras() == null || elem instanceof Scene || elem instanceof Buffer) continue;
|
||||
if ((elem.getExtras()[extrasNameKey]?.toString() ?? "") == name) {
|
||||
elem.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user