ThreeJS collada file not centred

China☆狼群 提交于 2019-12-02 16:29:11

问题


I'm new to ThreeJS and I'm trying to load collada files into a viewer. I've started by copying the code for the Elf colladaLoader demo.

https://threejs.org/examples/#webgl_loader_collada

I've got that working fine inside an 800px square modal window and it looks good. Trouble is when I load any other .dae file it's not centered.

The demo is obviously created with all the right settings to put the object and the camera in the right place, but I don't know what files are going to be loaded.

Is there any way of getting the camera to look directly at the object so it's centered and position it far enough back so the object fills the container?


回答1:


camera.lookAt( object.position ) will do most of what you want. Positioning the camera at an appropriate distance is a bit more subjective. I typically do something like this:

object.updateMatrixWorld();
const box = new THREE.Box3().setFromObject(object);
const size = box.getSize().length();
const center = box.getCenter();

object.position.x += (object.position.x - center.x);
object.position.y += (object.position.y - center.y);
object.position.z += (object.position.z - center.z);
camera.position.copy(center);
camera.position.x += size / 2.0;
camera.position.y += size / 5.0;
camera.position.z += size / 2.0;
camera.near = size / 100;
camera.far = size * 100;
camera.updateProjectionMatrix();
camera.lookAt(center);

See THREE.Object3D docs, three.js r89.



来源:https://stackoverflow.com/questions/48726645/threejs-collada-file-not-centred

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!