本文介绍了Three.js r68 - 无法使用 OBJMTLLoader 获取几何的质心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Three.js 的旧版本已经有一段时间了,所以我决定升级到最新版本 (r68).我知道我会遇到一些问题,但我没想到会删除 geometry.computeCentroids() 和 .centroid 属性.

I've been using an old revision of Three.js for quite some time, so I decided to upgrade to the latest (r68). I knew I would bump into some issues, but I wasn't expecting the removal of geometry.computeCentroids() and .centroid property.

我正在使用 OBJMTLLoader 库加载模型.问题在于,由于它不再具有 .computeCentroids() 方法,因此我无法获取它们.我尝试使用其他方法计算它们,但无济于事:

I'm loading models using the OBJMTLLoader library. The problem with this is that since it no longer has the .computeCentroids() method, I have no way of getting them. I've tried to calculate them using other methods, but to no avail:

我也尝试使用 .localToWorld(point) 方法,但它们也不起作用.它总是返回 (0,0,0).

I've also tried to use the .localToWorld(point) methods, but they're not working either. It always returns (0,0,0).

现在,当我点击一个网格时,我正在计算(或试图计算)它的质心:

For now, when I click a mesh, I'm calculating (or trying to calculate) its centroid with:

 if (mesh.centroid === undefined) {
    mesh.centroid = new THREE.Vector3();
    mesh.centroid = mesh.geometry.center();
    console.log(mesh.centroid); }

每当我向场景中添加新对象(具有子网格)时,我也会使用它:

I'm also using this whenever I add a new object to the scene (which has child meshes):

//desperate try to find different values for centroids
object.updateMatrix();
scene.updateMatrix();
object.updateMatrixWorld();
scene.updateMatrixWorld();

更奇怪的是,我试图计算的质心不一致.无论我单击网格的顺序如何,它始终在 console.log() 中显示相同的向量.

What's even more strange is that the centroids I'm trying to calculate aren't consistent.It always shows the same vectors in the console.log(), no matter which order I click the meshes.

THREE.Vector3 {x: 158.89799999999997, y: -4.115949999999998, z: 67.75310000000002, constructor: function, set: function…}
THREE.Vector3 {x: 0.000005004882780212938, y: 0.16375010757446518, z: 0.0000024658203301441972, constructor: function, set: function…}
THREE.Vector3 {x: -1.7053025658242404e-13, y: -0.0818749484539012, z: 1.1368683772161603e-13, constructor: function, set: function…}

我想知道是否有人遇到过类似的问题.我还没有尝试过 Three.js 以前的版本,因为我真的很想升级到最新版本并保持一致.

I was wondering if someone had a similar issue with this. I haven't tried previous revisions of three.js, since I really want to upgrade to the latest version and keep it consistent from there.

提前致谢.

忘记提及一个重要细节.我想要世界坐标中的质心.

Forgot to mention an important detail. I want the centroid in WORLD coordinates.

使用 mrdoob 的方法后,我能够得到一个质心.我总是为每个网格获得相同的值,因为即使我在每个网格上使用 .calculateBoundingBoxes() ,这些边界也引用了主要的父对象.每个几何体都分配了对象的所有顶点,我假设边界框是根据这些顶点计算的,这就是为什么我为每个几何体获得相同的值.

After using mrdoob 's method, I was able to get a centroid. I always get the same value for every mesh because, even though I'm using .calculateBoundingBoxes() on each mesh, these bounds refer to the main parent object.Each geometry has all of the object's vertices assigned, and I'm assuming that the bounding boxes are calculated according to these vertices, and that's why I'm getting the same values for every geometry.

推荐答案

如果你想计算整个几何的质心,这是你需要的代码:

If you want to compute the centroid of the whole geometry this is the code you need:

geometry.computeBoundingBox();

var centroid = new THREE.Vector3();
centroid.addVectors( geometry.boundingBox.min, geometry.boundingBox.max );
centroid.multiplyScalar( - 0.5 );

centroid.applyMatrix4( mesh.matrixWorld );

这篇关于Three.js r68 - 无法使用 OBJMTLLoader 获取几何的质心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 12:04