对于正交相机OrthographicCamera:
- 计算几何体的包围球的直径;
- 分别计算画布宽度、高度对于包围球直径的倍数;
- 确定相机的缩放倍数。
//使用包围球的方式,设置模型自适应大小。
geometry.computeBoundingSphere();
const maxDiameter = geometry.boundingSphere.radius * 2;
// 画布宽度是 包围球直径的 widthZoom倍。即包围球缩放widthZoom倍,宽度正好填满画布。
const widthZoom = window.innerWidth / maxDiameter;
// 画布高度是 包围球直径的 heightZoom倍。即包围球缩放heightZoom倍,高度正好填满画布。
const heightZoom = window.innerHeight / maxDiameter;
// 取两者缩放比例较小者,设置为正交相机的缩放倍数。
if (widthZoom >= heightZoom) {
camera.zoom = heightZoom;
} else {
camera.zoom = widthZoom;
}
camera.updateProjectionMatrix();