adjust model size

This commit is contained in:
DefectingCat
2023-05-16 14:23:41 +08:00
parent 20e358fb57
commit a349be156d
2 changed files with 61 additions and 20 deletions

View File

@ -1,3 +1,5 @@
import * as THREE from 'three';
export const sortByDate = (
{ date: a }: { date: string },
{ date: b }: { date: string }
@ -118,3 +120,34 @@ export const debounce: Debounce = (fn, ms) => {
}, ms);
};
};
export const frameArea = (
sizeToFitOnScreen: number,
boxSize: number,
boxCenter: THREE.Vector3,
camera: THREE.PerspectiveCamera
) => {
const halfSizeToFitOnScreen = sizeToFitOnScreen * 0.5;
const halfFovY = THREE.MathUtils.degToRad(camera.fov * 0.5);
const distance = halfSizeToFitOnScreen / Math.tan(halfFovY);
// compute a unit vector that points in the direction the camera is now
// in the xz plane from the center of the box
const direction = new THREE.Vector3()
.subVectors(camera.position, boxCenter)
.multiply(new THREE.Vector3(1, 0, 1))
.normalize();
// move the camera to a position distance units way from the center
// in whatever direction the camera was from the center already
camera.position.copy(direction.multiplyScalar(distance).add(boxCenter));
// pick some near and far values for the frustum that
// will contain the box.
camera.near = boxSize / 100;
camera.far = boxSize * 100;
camera.updateProjectionMatrix();
// point the camera to look at the center of the box
camera.lookAt(boxCenter.x, boxCenter.y, boxCenter.z);
};