import Constant from "../Constant"; import { dataService } from "./DataService"; const orthoBase = 10; const maxSize = 800; //模型最大占据像素 export default class CameraService { constructor() { this.aspect = Constant.cadImg_Width / Constant.cadImg_Height; this.top = null; this.bottom = null; this.left = null; this.right = null; } //modelSize:{x,y,z} getCurrentScale(modelSize) { let angle = dataService.getAngle(); modelSize = modelSize.clone().applyEuler(new THREE.Euler(0, angle, 0)); var n = Math.max( Math.abs(modelSize.x), Math.abs(modelSize.z) * this.aspect ); //视口宽高比 >= 1 情况下,模型所占最大视口尺寸 let screenSize = Math.min( Constant.cadImg_Width / Constant.ratio, Constant.cadImg_Height / Constant.ratio ); let ratio = Math.max((screenSize * 1.2) / maxSize, 1.2); let currentScale = (n / 2 / orthoBase) * ratio; //根据模型所占最大视口尺寸调整缩放 return currentScale; } setCamera(currentScale) { let cameraLeft = -10 * currentScale; let cameraRight = 10 * currentScale; let cameraTop = (10 * currentScale) / this.aspect; let cameraBottom = (-10 * currentScale) / this.aspect; this.top = cameraTop; this.bottom = cameraBottom; this.left = cameraLeft; this.right = cameraRight; } } const cameraService = new CameraService(); export { cameraService };