我正在尝试缩放以使对象在平移或旋转到其他点时适合中心点。
我已经尝试过通过包围框和场景的中心点捕获对象的中心点并将其传递给相机来进行尝试,但是仍然没有清晰的输出。当我加载较大的模型时,该对象正适合相机,但不适合中心点。
我已附上a fiddle
document.querySelector('button').addEventListener('click', () => {
const boundingBox = new THREE.Box3()
boundingBox.setFromObject(cube)
const center = new THREE.Vector3()
boundingBox.getCenter(center)
camera.position.y = center.y
camera.position.x = center.x
camera.updateProjectionMatrix()
const size = new THREE.Vector3()
boundingBox.getSize(size)
const fov = camera.fov * (Math.PI / 180)
const maxDim = Math.max(size.x, size.y, size.z)
let cameraZ = Math.abs((maxDim / 4) * Math.tan(fov * 2))
camera.position.z = cameraZ
camera.updateProjectionMatrix()
camera.lookAt(center)
})