/////////////////////////////////////////////////////////////////////////////// // 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, Box3Helper, BoxGeometry, Color, DoubleSide, Group, Mesh, MeshBasicMaterial, Object3D, Vector2, } from "three"; import { CSS2DObject, CSS2DRenderer } from "three/examples/jsm/renderers/CSS2DRenderer.js"; import { IComponent, components, Viewer } from "@inweb/viewer-three"; const emptyRaycast = () => {}; const OBJECT_COLOR = 0; const RANDOM_COLOR = 1; export const ColorModes = Object.freeze({ OBJECT_COLOR, RANDOM_COLOR, }); function getRandomColor(): Color { const h = Math.random(); const s = 0.5 + Math.random() * 0.5; const l = 0.375 + Math.random() * 0.25; return new Color().setHSL(h, s, l); } export interface BoundsHelperOptions { displayObjectBounds?: boolean; displayParentBounds?: boolean; displayHiddenBounds?: boolean; displayObjectHandles?: boolean; colorMode?: number; wireframe?: boolean; enabled?: boolean; } export class BoundsHelperComponent implements IComponent { private viewer: Viewer; private boxGroup: Group; private labelRenderer?: CSS2DRenderer; private _displayObjectBounds: boolean; private _displayParentBounds: boolean; private _displayHiddenBounds: boolean; private _displayObjectHandles: boolean; private _colorMode: number; private _wireframe: boolean; private _enabled: boolean; static get ColorModes() { return ColorModes; } constructor(viewer: Viewer, options: BoundsHelperOptions = {}) { this._displayObjectBounds = options.displayObjectBounds ?? true; this._displayParentBounds = options.displayParentBounds ?? true; this._displayHiddenBounds = options.displayHiddenBounds ?? false; this._displayObjectHandles = options.displayObjectHandles ?? true; this._colorMode = options.colorMode ?? OBJECT_COLOR; this._wireframe = options.wireframe ?? false; this._enabled = options.enabled ?? true; this.boxGroup = new Group(); this.boxGroup.name = "BoundsHelper"; this.viewer = viewer; this.viewer.on("initialize", this.onInitialize); this.viewer.on("resize", this.onResize); this.viewer.on("render", this.onRender); this.viewer.on("databasechunk", this.syncHelper); this.viewer.on("geometrychunk", this.syncHelper); this.viewer.on("geometryend", this.syncHelper); this.viewer.on("clear", this.syncHelper); this.viewer.on("explode", this.updatePositions); this.viewer.on("isolate", this.updateVisibility); this.viewer.on("hide", this.updateVisibility); this.viewer.on("hideall", this.updateVisibility); this.viewer.on("show", this.updateVisibility); this.viewer.on("showall", this.updateVisibility); } dispose() { this.clearHelpers(); this.disposeOverlay(); this.viewer.off("initialize", this.onInitialize); this.viewer.off("resize", this.onResize); this.viewer.off("render", this.onRender); this.viewer.off("databasechunk", this.syncHelper); this.viewer.off("geometrychunk", this.syncHelper); this.viewer.off("geometryend", this.syncHelper); this.viewer.off("clear", this.syncHelper); this.viewer.off("explode", this.updatePositions); this.viewer.off("isolate", this.updateVisibility); this.viewer.off("hide", this.updateVisibility); this.viewer.off("hideall", this.updateVisibility); this.viewer.off("show", this.updateVisibility); this.viewer.off("showall", this.updateVisibility); } get displayObjectBounds(): boolean { return this._displayObjectBounds; } set displayObjectBounds(value: boolean) { if (this._displayObjectBounds !== value) { this._displayObjectBounds = value; this.updateVisibility(); } } get displayParentBounds(): boolean { return this._displayParentBounds; } set displayParentBounds(value: boolean) { if (this._displayParentBounds !== value) { this._displayParentBounds = value; this.updateVisibility(); } } get displayHiddenBounds(): boolean { return this._displayHiddenBounds; } set displayHiddenBounds(value: boolean) { if (this._displayHiddenBounds !== value) { this._displayHiddenBounds = value; this.updateVisibility(); } } get displayObjectHandles(): boolean { return this._displayObjectHandles; } set displayObjectHandles(value: boolean) { if (this._displayObjectHandles !== value) { this._displayObjectHandles = value; this.updateVisibility(); } } get colorMode(): number { return this._colorMode; } set colorMode(value: number) { if (this._colorMode !== value) { this._colorMode = value; this.updateColors(); } } get wireframe(): boolean { return this._wireframe; } set wireframe(value: boolean) { if (this._wireframe !== value) { this._wireframe = value; this.updateVisibility(); } } get enabled(): boolean { return this._enabled; } set enabled(value: boolean) { if (this._enabled !== value) { this._enabled = value; if (this._enabled) { this.initOverlay(); this.syncHelper(); } else { this.clearHelpers(); this.disposeOverlay(); this.viewer.update(); } } } onInitialize = () => { this.initOverlay(); }; onResize = (event: any) => { this.labelRenderer?.setSize(event.width, event.height); }; onRender = () => { this.labelRenderer?.render(this.viewer.scene, this.viewer.camera); }; syncHelper = () => { this.clearHelpers(); if (!this._enabled) { this.viewer.update(); return; } for (const model of this.viewer.models) { const root = model.scene; const objects = model.getObjects(); const parentChildren = new Map>(); for (const object of objects) { if (!(object as any).geometry || object.userData?.isEdge) continue; const depth = this.getObjectDepth(object, root); const helper = this.createObjectHelper(object, false, depth); this.boxGroup.add(helper); let parent = object.parent; while (parent && parent !== root) { let childrenSet = parentChildren.get(parent); if (!childrenSet) { childrenSet = new Set(); parentChildren.set(parent, childrenSet); } childrenSet.add(object); parent = parent.parent; } } for (const [parent, childrenSet] of parentChildren.entries()) { const depth = this.getObjectDepth(parent, root); const helper = this.createObjectHelper(parent, true, depth, Array.from(childrenSet)); this.boxGroup.add(helper); } } this.viewer.helpers.add(this.boxGroup); this.updatePositions(); this.updateVisibility(); }; updatePositions = () => { if (!this._enabled) return; this.viewer.scene.updateMatrixWorld(true); const objectBoxes = new Map(); for (const helper of this.boxGroup.children) { const { object, isParent, box, boxHelper, fillMesh, label } = helper.userData; if (isParent) continue; this.getObjectBox(object, box); objectBoxes.set(object, box); if (!box.isEmpty()) { boxHelper.updateMatrixWorld(true); box.getCenter(fillMesh.position); box.getSize(fillMesh.scale); label.position.copy(box.max); } } for (const helper of this.boxGroup.children) { const { isParent, children, box, boxHelper, fillMesh, label } = helper.userData; if (!isParent) continue; box.makeEmpty(); for (const child of children) { const childBox = objectBoxes.get(child); if (childBox && !childBox.isEmpty()) box.union(childBox); } if (!box.isEmpty()) { boxHelper.updateMatrixWorld(true); box.getCenter(fillMesh.position); box.getSize(fillMesh.scale); label.position.copy(box.min); } } this.viewer.update(); }; updateVisibility = () => { if (!this._enabled) return; const visibleSet = new Set(this.viewer.models.flatMap((model) => model.getVisibleObjects())); for (const helper of this.boxGroup.children) { const { object, isParent, children, box, fillMesh, label } = helper.userData; if (box.isEmpty()) { helper.visible = false; continue; } if (!isParent) { const isVisible = visibleSet.has(object); helper.visible = (isVisible && this._displayObjectBounds) || (!isVisible && this._displayHiddenBounds); } else { const hasVisibleChild = children.some((child: Object3D) => visibleSet.has(child)); helper.visible = this._displayParentBounds && (hasVisibleChild || this._displayHiddenBounds); } fillMesh.visible = !this._wireframe; label.visible = this._displayObjectHandles; } this.viewer.update(); }; private updateColors(): void { if (!this._enabled) return; for (const helper of this.boxGroup.children) { const { object, boxHelper, fillMesh, label } = helper.userData; const color = this.getObjectColor(object); boxHelper.material.color.copy(color); fillMesh.material.color.copy(color); label.element.style.color = color.getStyle(); } this.viewer.update(); } private initOverlay(): void { if (!this._enabled) return; if (this.labelRenderer) return; this.labelRenderer = new CSS2DRenderer(); this.labelRenderer.domElement.id = "boundshelper-overlay"; this.labelRenderer.domElement.style.position = "absolute"; this.labelRenderer.domElement.style.top = "0px"; this.labelRenderer.domElement.style.left = "0px"; this.labelRenderer.domElement.style.pointerEvents = "none"; const size = this.viewer.renderer.getSize(new Vector2()); this.labelRenderer.setSize(size.x, size.y); this.viewer.canvas.parentElement.appendChild(this.labelRenderer.domElement); } private disposeOverlay(): void { if (!this.labelRenderer) return; this.labelRenderer.domElement.remove(); this.labelRenderer = undefined; } private clearHelpers(): void { this.boxGroup.traverse((child: any) => { if (child.isCSS2DObject) child.element?.remove(); if (child.geometry) child.geometry.dispose(); if (child.material) child.material.dispose(); }); this.boxGroup.clear(); this.boxGroup.removeFromParent(); } private getObjectBox(object: any, target = new Box3()): Box3 { target.makeEmpty(); if (!object.geometry) return target; object.updateWorldMatrix(false, false); if (object.boundingBox !== undefined) { if (object.boundingBox === null) object.computeBoundingBox(); target.copy(object.boundingBox); } else { if (object.geometry.boundingBox === null) object.geometry.computeBoundingBox(); target.copy(object.geometry.boundingBox); } target.applyMatrix4(object.matrixWorld); return target; } private getObjectDepth(object: Object3D, root: Object3D): number { let depth = object.userData.depth; if (!depth) { depth = 0; while (object && object !== root) { depth++; object = object.parent; } } return depth; } private getObjectColor(object: any): Color { if (this._colorMode === OBJECT_COLOR) { const material = Array.isArray(object.material) ? object.material[0] : object.material; if (material?.color) { return material.color; } } if (!object.userData.boundsColor) { object.userData.boundsColor = getRandomColor(); } return object.userData.boundsColor; } private createHandleLabel(color: Color, text: string, depth = 1): CSS2DObject { const fontSize = Math.max(24 - depth * 2, 8); const div = document.createElement("div"); div.textContent = text; div.style.color = color.getStyle(); div.style.fontFamily = "Arial, sans-serif"; div.style.fontSize = `${fontSize}px`; div.style.fontWeight = "bold"; div.style.opacity = "0.7"; div.style.pointerEvents = "none"; div.style.whiteSpace = "nowrap"; const label = new CSS2DObject(div); label.name = text; label.visible = this._displayObjectHandles; label.raycast = emptyRaycast; return label; } private createObjectHelper(object: Object3D, isParent: boolean, depth: number, children: Object3D[] = []): Group { const color = this.getObjectColor(object); const box = new Box3(); const objectHelperGroup = new Group(); objectHelperGroup.name = "ObjectHelper"; const boxHelper = new Box3Helper(box, color); boxHelper.raycast = emptyRaycast; objectHelperGroup.add(boxHelper); const fillMesh = new Mesh( new BoxGeometry(), new MeshBasicMaterial({ color, transparent: true, depthWrite: false, opacity: 0.05, side: DoubleSide, }) ); fillMesh.raycast = emptyRaycast; fillMesh.visible = !this._wireframe; objectHelperGroup.add(fillMesh); const labelText = `${depth} - ${object.userData?.handle || undefined}`; const label = this.createHandleLabel(color, labelText, depth); objectHelperGroup.add(label); objectHelperGroup.userData = { object, isParent, children, box, boxHelper, fillMesh, label, }; return objectHelperGroup; } } components.registerComponent("BoundsHelperComponent", (viewer) => new BoundsHelperComponent(viewer));