Three.js子网格位置始终返回(0,0,0) [英] Three.js Child Mesh position always return (0,0,0)

查看:180
本文介绍了Three.js子网格位置始终返回(0,0,0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过OBJLoader加载了一些对象,加载的对象包含一个父对象和多个子对象;然后我应用Raycaster并找到被点击的孩子.

I loaded some objects via OBJLoader , loaded object contain one parent and multiple childs; then I apply Raycaster and find clicked child.

然后我想更新子对象的位置,但是所有子对象的初始位置都为零.

Then I want to update position of child object, but initial position comes zero for all childs.

var intersect = intersects[0].object;
intersect.position.y = intersect.position.y + 5; // 0 + 5 

但是在我的场景中,一切看起来都很好.另外,如果我删除单击的对象,则实际上是从场景中删除了该对象.我想我错过了他们的位置不能为(0,0,0)的观点.我怎样才能达到他们的相对位置?

But in my scene all looks fine. Also, If i remove clicked object, actually it is removed from scene. I think I missed some point their positions cant be (0,0,0). How can I reach their relative position ?

推荐答案

尝试一下,然后阅读position().我遇到了同样的问题,在这里得到了答案. (geom是您的Meshs几何.)

Try this, then read position(). I had the same issue, got an answer here. (geom is your Meshes geometry.)

objMesh.centroid = new THREE.Vector3();
for (var i = 0, l = geom.vertices.length; i < l; i++) {
    objMesh.centroid.add(geom.vertices[i].clone());
}
objMesh.centroid.divideScalar(geom.vertices.length);
var offset = objMesh.centroid.clone();

objMesh.geometry.applyMatrix(new THREE.Matrix4().makeTranslation(-offset.x, -offset.y, -offset.z));

objMesh.position.copy(objMesh.centroid);

这篇关于Three.js子网格位置始终返回(0,0,0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆