简体   繁体   English

Three.js r68 - 无法使用OBJMTLLoader获取几何体的质心

[英]Three.js r68 - Can't get centroids of geometries using OBJMTLLoader

I've been using an old revision of Three.js for quite some time, so I decided to upgrade to the latest (r68). 我已经使用旧版本的Three.js很长一段时间了,所以我决定升级到最新版本(r68)。 I knew I would bump into some issues, but I wasn't expecting the removal of geometry.computeCentroids() and .centroid property. 我知道我会碰到一些问题,但我不希望删除geometry.computeCentroids()和.centroid属性。

I'm loading models using the OBJMTLLoader library. 我正在使用OBJMTLLoader库加载模型。 The problem with this is that since it no longer has the .computeCentroids() method, I have no way of getting them. 这个问题是因为它不再有.computeCentroids()方法,所以我无法获取它们。 I've tried to calculate them using other methods, but to no avail: 我试图用其他方法计算它们,但无济于事:

I've also tried to use the .localToWorld(point) methods, but they're not working either. 我也试过使用.localToWorld(point)方法,但它们也没有用。 It always returns (0,0,0). 它总是返回(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();

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. 它总是在console.log()中显示相同的向量,无论我点击网格的顺序。

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…}

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. 我没有尝试过以前的three.js版本,因为我真的想升级到最新版本并保持一致。

Thank you in advance. 先感谢您。

EDIT: Forgot to mention an important detail. 编辑:忘了提一个重要的细节。 I want the centroid in WORLD coordinates. 我想要WORLD坐标中的质心。

After using mrdoob 's method, I was able to get a centroid. 使用mrdoob的方法后,我得到了一个质心。 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. 我总是为每个网格获得相同的值,因为即使我在每个网格上使用.calculateBoundingBoxes(),这些边界也会引用主父对象。 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 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM