从 Sketchfab.com 下载 3D 模型并导入到 Three.js 场景后,它们大部分时间都不居中,并且它们的尺寸非常大。
有没有办法使用脚本或软件(在 Linux 上运行)自动居中和缩放 gltf 模型,或者以其他方式在 Three.js 中即时进行,并让相机使用 OrbitControls 在对象周围移动。
最佳答案
是的。这段代码获取一个节点并找到它的大小和中心点,然后重新缩放它,使其最大范围为 1,并以 0,0,0 为中心,并在 y 轴上高于地面。
var mroot = yourScene;
var bbox = new THREE.Box3().setFromObject(mroot);
var cent = bbox.getCenter(new THREE.Vector3());
var size = bbox.getSize(new THREE.Vector3());
//Rescale the object to normalized space
var maxAxis = Math.max(size.x, size.y, size.z);
mroot.scale.multiplyScalar(1.0 / maxAxis);
bbox.setFromObject(mroot);
bbox.getCenter(cent);
bbox.getSize(size);
//Reposition to 0,halfY,0
mroot.position.copy(cent).multiplyScalar(-1);
mroot.position.y-= (size.y * 0.5);
关于three.js - 在 Three.js 中自动居中和调整 GLTF 模型的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52271397/