/////////////////////////////////////////////////////////////////////////////// // 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 { Box3, Matrix4, Object3D, Vector3 } from "three"; import { IInfo, Info } from "@inweb/viewer-core"; import { ModelImpl } from "../../models/ModelImpl"; import { IHighlightStyle } from "../../models/Highlighter.types"; import { DynamicGltfLoader } from "./DynamicGltfLoader.js"; // Dynamic model implementation. export class DynamicModelImpl extends ModelImpl { public gltfLoader: DynamicGltfLoader; override initialize() { // do not call super !!! } override getInfo(): IInfo { const stats = this.gltfLoader.getStats(); const info = new Info(); info.scene.objects = stats.scene.beforeOptimization.objects; info.scene.triangles = stats.scene.beforeOptimization.triangles; info.scene.lines = stats.scene.beforeOptimization.lines; info.scene.edges = stats.scene.beforeOptimization.edges; info.optimizedScene.objects = stats.scene.afterOptimization.objects; info.optimizedScene.triangles = stats.scene.afterOptimization.triangles; info.optimizedScene.lines = stats.scene.afterOptimization.lines; info.optimizedScene.edges = stats.scene.afterOptimization.edges; info.memory.geometries = stats.memory.geometries.count; info.memory.geometryBytes = stats.memory.geometries.bytes; info.memory.optimizedGeometryBytes = stats.memory.geometries.optimizedBytes; info.memory.textures = stats.memory.textures.count; info.memory.materials = stats.memory.materials.count; info.memory.totalEstimatedGpuBytes = stats.memory.totalEstimatedGpuBytes; return info; } override getExtents(target: Box3): Box3 { return target.union(this.gltfLoader.getTotalGeometryExtent()); } override getObjects(): Object3D[] { const objects: Object3D[] = []; this.gltfLoader.originalObjects.forEach((object: Object3D) => { objects.push(object); }); return objects; } override getVisibleObjects(): Object3D[] { return this.gltfLoader.getOriginalObjectForSelect(); } override getObjectsByHandles(handles: string[]): Object3D[] { return handles.flatMap((handle) => this.gltfLoader.getObjectsByHandle(handle)); } override getHandles(): string[] { return Array.from(this.gltfLoader.handleToObjects.keys()); } override getHandlesByObjects(objects: Object3D[]): string[] { return this.gltfLoader.getHandlesByObjects(objects); } // IModel override hide(handles: string[]): this { this.gltfLoader.hideObjects(handles); return this; } override isolate(handles: string[]): this { this.gltfLoader.isolateObjects(new Set(handles)); return this; } override show(handles: string[]): this { this.gltfLoader.showObjects(handles); return this; } override showAll(): this { this.gltfLoader.showAllHiddenObjects(); return this; } override highlight(handles: string[], style?: string): this { this.gltfLoader.highlightObjects(handles, style); return this; } override unhighlight(handles: string[]): this { this.gltfLoader.unhighlightObjects(handles); return this; } override setHighlightStyle(name: string, style: IHighlightStyle): this { this.gltfLoader.setHighlightStyle(name, style); return this; } override explode(scale = 0, coeff = 4): this { const centersCache = new Map(); const calcObjectCenter = (object: Object3D, target: Vector3): Vector3 => { const extents = new Box3().setFromObject(object); const handle = object.userData.handle; if (!handle) return extents.getCenter(target); const fullHandle = handle; // <- DynamicGltfLoader uses composite handles by default const center = centersCache.get(fullHandle); if (center) return target.copy(center); const objects = this.getObjectsByHandles([fullHandle]); objects.forEach((x: Object3D) => extents.expandByObject(x)); extents.getCenter(target); centersCache.set(fullHandle, target.clone()); return target; }; const calcObjectOffset = (object: Object3D, target: Vector3): Vector3 => { const parent = object.parent; if (!parent || parent.userData.originalCenter === undefined) return target; return target.subVectors(object.userData.originalCenter, parent.userData.originalCenter); }; const calcObjectDepth = (object: Object3D): number => { if (object.userData.depth !== undefined) return object.userData.depth; const parent = object.parent; const depth = parent && object !== explodeRoot ? calcObjectDepth(parent) + 1 : 0; object.userData.depth = depth; object.userData.originalPosition = object.position.clone(); object.userData.originalCenter = calcObjectCenter(object, new Vector3()); object.userData.originalOffset = calcObjectOffset(object, new Vector3()); return depth; }; const explodeScale = scale / 100; const explodeRoot = this.scene.children[0]; const explodeObjects = this.getObjects(); if (explodeRoot.userData.explodeDepth === undefined) { let maxDepth = 0; explodeObjects.forEach((object: Object3D) => { const depth = calcObjectDepth(object); if (depth > maxDepth) maxDepth = depth; }); explodeRoot.userData.explodeDepth = maxDepth; } const maxDepth = explodeRoot.userData.explodeDepth; const scaledExplodeDepth = explodeScale * maxDepth + 1; const explodeDepth = 0 | scaledExplodeDepth; const currentSegmentFraction = scaledExplodeDepth - explodeDepth; const offsetCache = new Map(); const calcExplodeOffset = (object: Object3D, target: Vector3): Vector3 => { if (offsetCache.has(object)) return target.copy(offsetCache.get(object)); const parent = object.parent; if (parent && object !== explodeRoot) calcExplodeOffset(parent, target); const depth = object.userData.depth; if (depth > 0 && depth <= explodeDepth) { let objectScale = explodeScale * coeff; if (depth === explodeDepth) objectScale *= currentSegmentFraction; target.addScaledVector(object.userData.originalOffset, objectScale); } offsetCache.set(object, target.clone()); return target; }; const transformMap = new Map(); explodeObjects.forEach((object: Object3D) => { const offset = calcExplodeOffset(object, new Vector3()); transformMap.set(object, new Matrix4().makeTranslation(offset)); }); this.gltfLoader.applyObjectTransforms(transformMap); this.scene.updateMatrixWorld(); return this; } }