/////////////////////////////////////////////////////////////////////////////// // Copyright (C) 2002-2026, Open Design Alliance (the "Alliance"). // All rights reserved. // // This software and its documentation and related materials are owned by // the Alliance. The software may only be incorporated into application // programs owned by members of the Alliance, subject to a signed // Membership Agreement and Supplemental Software License Agreement with the // Alliance. The structure and organization of this software are the valuable // trade secrets of the Alliance and its suppliers. The software is also // protected by copyright law and international treaty provisions. Application // programs incorporating this software must include the following statement // with their copyright notices: // // This application incorporates Open Design Alliance software pursuant to a // license agreement with Open Design Alliance. // Open Design Alliance Copyright (C) 2002-2026 by Open Design Alliance. // All rights reserved. // // By use of this software, its documentation or related materials, you // acknowledge and accept the above terms. /////////////////////////////////////////////////////////////////////////////// import { Color, FileLoader, Group, LoaderUtils } from "three"; import { IOptions, Loader, Options } from "@inweb/viewer-core"; import { Viewer } from "../Viewer"; import { DynamicModelImpl } from "./DynamicGltfLoader/DynamicModelImpl"; import { DynamicGltfLoader } from "./DynamicGltfLoader/DynamicGltfLoader.js"; import { GltfStructure } from "./DynamicGltfLoader/GltfStructure.js"; import { GLTFLoadingManager, GLTFLoadParams } from "./GLTFLoadingManager"; import { GLTFBinaryParser } from "./GLTFBinaryParser"; import { JSONStreamParser } from "./JSONStreamParser"; import { RangesLoader } from "./RangesLoader"; export class GLTFFileDynamicLoader extends Loader { public viewer: Viewer; private gltfLoader: DynamicGltfLoader; private manager: GLTFLoadingManager; private glb: ArrayBuffer; constructor(viewer: Viewer) { super(); this.viewer = viewer; } override dispose() { this.viewer.removeEventListener("optionschange", this.syncHighlightSettings); if (this.gltfLoader) this.gltfLoader.clear(); if (this.manager) this.manager.dispose(); } override isSupport(file: any, format = ""): boolean { return ( (typeof file === "string" || file instanceof globalThis.File || file instanceof ArrayBuffer) && /(gltf|glb)$/i.test(format) ); } override async load(file: any, format?: string, params: GLTFLoadParams = {}): Promise { this.manager = new GLTFLoadingManager(file, params); const scene = new Group(); this.gltfLoader = new DynamicGltfLoader(this.viewer.camera, scene, this.viewer.renderer); this.gltfLoader.setMemoryLimit(this.viewer.options.memoryLimit); this.gltfLoader.setVisibleEdges(this.viewer.options.edgeModel); this.gltfLoader.setMaxConcurrentChunks(params.maxConcurrentChunks); this.gltfLoader.setDracoLoader(params.dracoLoader); this.gltfLoader.setHighlightSettings(this.getHighlightSettings()); const modelImpl = new DynamicModelImpl(scene); modelImpl.id = params.modelId || this.extractFileName(file); modelImpl.gltfLoader = this.gltfLoader; modelImpl.initialize(); this.viewer.addEventListener("optionschange", this.syncHighlightSettings); this.gltfLoader.addEventListener("databasechunk", () => { this.viewer.scene.add(scene); this.viewer.models.push(modelImpl); this.viewer.syncOptions(); this.viewer.syncOverlay(); this.viewer.emitEvent({ type: "databasechunk", data: scene, file }); this.viewer.update(true); }); this.gltfLoader.addEventListener("geometryerror", (data) => { this.viewer.emitEvent({ type: "geometryerror", data, file }); }); this.gltfLoader.addEventListener("update", () => { this.viewer.update(); }); const loadController = { loadJson: async () => { const loader = new FileLoader(this.manager); loader.setPath(this.manager.path); loader.setRequestHeader((params.requestHeader as any) || {}); loader.setWithCredentials(params.withCredentials || loader.withCredentials); loader.setResponseType("arraybuffer"); const progress = (event: ProgressEvent) => { const { lengthComputable, loaded, total } = event; const progress = lengthComputable ? loaded / total : 1; this.viewer.emitEvent({ type: "geometryprogress", data: progress, file }); }; const data = await loader.loadAsync(this.manager.fileURL, progress); const binaryParser = new GLTFBinaryParser(data as ArrayBuffer); const jsonParser = new JSONStreamParser(binaryParser.content); if (!jsonParser.json) { throw new Error("GLTFFileDynamicLoader: JSON content not found or invalid."); } this.glb = binaryParser.body; return jsonParser.json; }, loadBinaryData: (ranges, uri = "") => { const loader = new RangesLoader(); loader.setRequestHeader((params.requestHeader as any) || {}); loader.setWithCredentials(params.withCredentials || false); loader.setAbortSignal(this.gltfLoader.abortController.signal); if (this.glb) return loader.extractRanges(this.glb, ranges); const path = this.manager.path || this.manager.resourcePath; const url = LoaderUtils.resolveURL(uri, path); return loader.load(this.manager.resolveURL(url), ranges); }, resolveURL: (uri) => { const path = this.manager.path || this.manager.resourcePath; const url = LoaderUtils.resolveURL(uri, path); return Promise.resolve(this.manager.resolveURL(url)); }, }; const structure = new GltfStructure(modelImpl.id, loadController); await this.gltfLoader.loadStructure(structure); await this.gltfLoader.loadNodes(); return this; } override cancel() { if (this.gltfLoader) this.gltfLoader.abortLoading(); } getHighlightSettings(options?: IOptions): any { if (!options) options = this.viewer.options; if (!options.enableCustomHighlight) options = Options.defaults(); let { facesColor, facesTransparancy, edgesColor } = options; facesColor = new Color().setRGB(facesColor.r / 255, facesColor.g / 255, facesColor.b / 255); facesTransparancy = (255 - facesTransparancy) / 255; edgesColor = new Color().setRGB(edgesColor.r / 255, edgesColor.g / 255, edgesColor.b / 255); return { facesColor, facesTransparancy, facesOverlap: options.facesOverlap, edgesColor, edgesVisibility: options.edgesVisibility, edgesOverlap: options.edgesOverlap, }; } syncHighlightSettings = ({ data }) => { this.gltfLoader.setHighlightSettings(this.getHighlightSettings(data)); }; }