8

使用下面的代码,网格的位置返回为(0, 0, 0)但不是。那么在渲染过程之后计算位置向量吗?

me.scene.add(objMesh); //me is a project class
objMesh.updateMatrixWorld(true);
alert(objMesh.position.x + ',' + objMesh.position.y + ',' + objMesh.position.z);

objMesh 是从 objfile 创建的,它被正确添加到场景中并且质心大约是 (-8, 3, 0) 但 objMesh 的位置向量是 (0, 0, 0) 我们必须先自动计算一些东西还是应该计算它手动从网格的几何顶点?

http://81.214.75.32:8181/admin是网址

该网站是土耳其语,所以我将翻译 UI 项目

在站点中有“Dosya”菜单项打开菜单项并选择“Proje Aç”在该对话框中出现一个对话框选择MUTFAK_1场景将出现在该场景中,每个网格位置都是(0,0,0)是可能的: )

4

5 回答 5

28

object.position始终是对象的本地。如果你想获得世界空间中的位置,你需要从object.matrixWorld.

试试这个:

scene.add(objMesh);
scene.updateMatrixWorld(true);
var position = new THREE.Vector3();
position.getPositionFromMatrix( objMesh.matrixWorld );
alert(position.x + ',' + position.y + ',' + position.z);

r58


更新:

该功能getPositionFromMatrix()已重命名为setFromMatrixPosition().

于 2013-01-08T13:44:44.177 回答
13

要查找世界空间中几何质心的位置,请尝试以下操作:

objMesh.geometry.computeBoundingBox();

var boundingBox = objMesh.geometry.boundingBox;

var position = new THREE.Vector3();
position.subVectors( boundingBox.max, boundingBox.min );
position.multiplyScalar( 0.5 );
position.add( boundingBox.min );

position.applyMatrix4( objMesh.matrixWorld );

alert(position.x + ',' + position.y + ',' + position.z);

r58

于 2013-01-08T22:46:47.480 回答
4

是的。在与 mrdoob 交谈后,我意识到对象的 .position 对它们自己来说是本地的。我的情况是在考虑顶点的情况下找到我的网格的中心点。下面是获取来自答案 #447 ( https://github.com/mrdoob/three.js/issues/447 )的质心的代码

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

现在我们有了几何的质心......

根据https://github.com/mrdoob/three.js/wiki/Migration更新,.addSelf已重命名为.addr55 之后

于 2013-01-09T09:27:05.130 回答
2
alert(objMesh.matrixWorld.getPosition().x + ',' + objMesh.matrixWorld.getPosition().y + ',' + objMesh.matrixWorld.getPosition().z);
于 2013-01-08T10:01:02.210 回答
0

根据这篇文章,网格的重心 C 可以通过以下方式找到

C = [所有 (A*R) 的总和] / [所有 A 的总和]
A = (面的面积 * 2)
R = 面质心 = 构成面的顶点的平均值

这是three.js中的代码

function calculateCenterOfMass( mesh ){

    var centroid = new THREE.Vector3();

    // centroid = centroidNominator / centroidDenominator;
    var centroidNominator = new THREE.Vector3(); 
    var centroidDenominator = 0;

    for(var i = 0; i < mesh.geometry.faces.length; i++){

        var Pi = mesh.geometry.faces[i].a;
        var Qi = mesh.geometry.faces[i].b;
        var Ri = mesh.geometry.faces[i].c;

        var a = new THREE.Vector3(mesh.geometry.vertices[Pi].x, mesh.geometry.vertices[Pi].y, mesh.geometry.vertices[Pi].z);
        var b = new THREE.Vector3(mesh.geometry.vertices[Qi].x, mesh.geometry.vertices[Qi].y, mesh.geometry.vertices[Qi].z);
        var c = new THREE.Vector3(mesh.geometry.vertices[Ri].x, mesh.geometry.vertices[Ri].y, mesh.geometry.vertices[Ri].z);

        var ab = b.clone().sub(a);
        var ac = c.clone().sub(a);

        var cross = new THREE.Vector3();
        cross.crossVectors( ab, ac );

        var faceArea = cross.lengthSq() / 2;
        var faceCentroid = new THREE.Vector3( (a.x + b.x + c.x)/3, (a.y + b.y + c.y)/3, (a.z + b.z + c.z)/3 );

        if (!isNaN(faceArea)){
            centroidNominator.add(faceCentroid.multiplyScalar(faceArea));
            centroidDenominator += faceArea;
        }
    }


    centroid = centroidNominator.divideScalar(centroidDenominator);

    return centroid;
}
于 2016-02-12T11:47:58.220 回答