UNPKG

@inweb/viewer-three

Version:

JavaScript library for rendering CAD and BIM files in a browser using Three.js

64 lines (54 loc) 2.62 kB
/////////////////////////////////////////////////////////////////////////////// // Copyright (C) 2002-2025, 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-2025 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 { Quaternion, Sphere, Vector3 } from "three"; import type { Viewer } from "../Viewer"; import { zoomTo } from "./ZoomTo"; export const defaultViewPositions = { front: new Vector3(0, 0, -1), back: new Vector3(0, 0, 1), left: new Vector3(1, 0, 0), right: new Vector3(-1, 0, 0), bottom: new Vector3(0, -1, 0), top: new Vector3(0, 1, 0), se: new Vector3(-1, 1, -1).normalize(), sw: new Vector3(1, 1, -1).normalize(), ne: new Vector3(-1, 1, 1).normalize(), nw: new Vector3(1, 1, 1).normalize(), }; export function setDefaultViewPosition(viewer: Viewer, position: string): void { const extentsCenter = viewer.extents.getCenter(new Vector3()); const extentsSize = viewer.extents.getBoundingSphere(new Sphere()).radius * 2; const upY = new Vector3(0, 1, 0); const offsetY = defaultViewPositions[position] || defaultViewPositions["sw"]; const up = new Vector3().copy(viewer.camera.up); const quaternion = new Quaternion().setFromUnitVectors(upY, up); const offset = new Vector3().copy(offsetY).applyQuaternion(quaternion); const camera = viewer.camera; camera.position.copy(offset).multiplyScalar(extentsSize).add(extentsCenter); camera.lookAt(extentsCenter); camera.updateMatrixWorld(); viewer.target.copy(extentsCenter); viewer.update(); viewer.emit({ type: "viewposition", data: position }); zoomTo(viewer, viewer.extents); }