|
1 | | -import { PerspectiveCamera, Vector3, MOUSE } from 'three'; |
| 1 | +import { PerspectiveCamera, Vector3, MOUSE, Box3, MathUtils } from 'three'; |
2 | 2 | import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'; |
3 | 3 | import { IfcComponent, Context } from '../../base-types'; |
4 | 4 |
|
@@ -33,6 +33,30 @@ export class IfcCamera extends IfcComponent { |
33 | 33 | this.controls.enabled = active; |
34 | 34 | } |
35 | 35 |
|
| 36 | + fitModelToFrame() { |
| 37 | + const scene = this.context.getScene(); |
| 38 | + const box = new Box3().setFromObject(scene.children[scene.children.length - 1]); |
| 39 | + const boxSize = box.getSize(new Vector3()).length(); |
| 40 | + const boxCenter = box.getCenter(new Vector3()); |
| 41 | + |
| 42 | + const halfSizeToFitOnScreen = boxSize * 0.5; |
| 43 | + const halfFovY = MathUtils.degToRad(this.camera.fov * 0.5); |
| 44 | + const distance = halfSizeToFitOnScreen / Math.tan(halfFovY); |
| 45 | + |
| 46 | + const direction = new Vector3() |
| 47 | + .subVectors(this.camera.position, boxCenter) |
| 48 | + .multiply(new Vector3(1, 0, 1)) |
| 49 | + .normalize(); |
| 50 | + |
| 51 | + this.camera.position.copy(direction.multiplyScalar(distance).add(boxCenter)); |
| 52 | + this.camera.updateProjectionMatrix(); |
| 53 | + this.camera.lookAt(boxCenter.x, boxCenter.y, boxCenter.z); |
| 54 | + |
| 55 | + // set target to newest loaded model |
| 56 | + this.controls.target.copy(boxCenter); |
| 57 | + this.controls.update(); |
| 58 | + } |
| 59 | + |
36 | 60 | private setupCamera() { |
37 | 61 | // camera.up = new Vector3(0, 0, 1); |
38 | 62 | this.camera.position.z = 8; |
|
0 commit comments