| 64 | } |
| 65 | |
| 66 | function frameArea(sizeToFitOnScreen, boxSize, boxCenter, camera) { |
| 67 | const halfSizeToFitOnScreen = sizeToFitOnScreen * 0.5; |
| 68 | const halfFovY = THREE.MathUtils.degToRad(camera.fov * 0.5); |
| 69 | const distance = halfSizeToFitOnScreen / Math.tan(halfFovY); |
| 70 | // compute a unit vector that points in the direction the camera is now |
| 71 | // in the xz plane from the center of the box |
| 72 | const direction = new THREE.Vector3() |
| 73 | .subVectors(camera.position, boxCenter) |
| 74 | .multiply(new THREE.Vector3(1, 0, 1)) |
| 75 | .normalize(); |
| 76 | |
| 77 | // move the camera to a position distance units way from the center |
| 78 | // in whatever direction the camera was from the center already |
| 79 | camera.position.copy(direction.multiplyScalar(distance).add(boxCenter)); |
| 80 | |
| 81 | // pick some near and far values for the frustum that |
| 82 | // will contain the box. |
| 83 | camera.near = boxSize / 100; |
| 84 | camera.far = boxSize * 100; |
| 85 | |
| 86 | camera.updateProjectionMatrix(); |
| 87 | |
| 88 | // point the camera to look at the center of the box |
| 89 | camera.lookAt(boxCenter.x, boxCenter.y, boxCenter.z); |
| 90 | } |
| 91 | |
| 92 | // load gltf file |
| 93 | { |