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

查看:30
本文介绍了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 是您的网格几何体.)

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天全站免登陆