CameraService.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import Constant from "../Constant";
  2. import { dataService } from "./DataService";
  3. const orthoBase = 10;
  4. const maxSize = 800; //模型最大占据像素
  5. export default class CameraService {
  6. constructor() {
  7. this.aspect = Constant.cadImg_Width / Constant.cadImg_Height;
  8. this.top = null;
  9. this.bottom = null;
  10. this.left = null;
  11. this.right = null;
  12. }
  13. //modelSize:{x,y,z}
  14. getCurrentScale(modelSize) {
  15. let angle = dataService.getAngle();
  16. modelSize = modelSize.clone().applyEuler(new THREE.Euler(0, angle, 0));
  17. var n = Math.max(
  18. Math.abs(modelSize.x),
  19. Math.abs(modelSize.z) * this.aspect
  20. ); //视口宽高比 >= 1 情况下,模型所占最大视口尺寸
  21. let screenSize = Math.min(
  22. Constant.cadImg_Width / Constant.ratio,
  23. Constant.cadImg_Height / Constant.ratio
  24. );
  25. let ratio = Math.max((screenSize * 1.2) / maxSize, 1.2);
  26. let currentScale = (n / 2 / orthoBase) * ratio; //根据模型所占最大视口尺寸调整缩放
  27. return currentScale;
  28. }
  29. setCamera(currentScale) {
  30. let cameraLeft = -10 * currentScale;
  31. let cameraRight = 10 * currentScale;
  32. let cameraTop = (10 * currentScale) / this.aspect;
  33. let cameraBottom = (-10 * currentScale) / this.aspect;
  34. this.top = cameraTop;
  35. this.bottom = cameraBottom;
  36. this.left = cameraLeft;
  37. this.right = cameraRight;
  38. }
  39. }
  40. const cameraService = new CameraService();
  41. export { cameraService };