三。如何获得网格的位置?

时间:2021-12-21 03:20:21

With the below code, position of a mesh is returned as (0, 0, 0) but it is not. So is the positioın vector calculated after render process?

在下面的代码中,网格的位置被返回为(0,0,0),但它不是。所以是positioın向量计算后呈现过程?

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

objMesh is created from objfile, it is added to the scene correctly and centroid is approx (-8, 3, 0) but position vector of objMesh is (0, 0, 0) do we have to auto calculate something first or should i calculate it manually from geometry vertices of the mesh ?

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

http://81.214.75.32:8181/admin is the url

url http://81.214.75.32:8181管理

the site is in Turkish so i will translate the UI items

这个网站是土耳其语,所以我会翻译UI项目。

in the site there is "Dosya" menu item oppen the menu item and select "Proje Aç" a dialog appears in that dialog select MUTFAK_1 scene will appear in that scene, every meshes position is (0, 0, 0) is that possible :)

在网站上有“Dosya”菜单项oppen菜单项,并选择“projeac”对话框出现在对话框中,选择MUTFAK_1场景,每个网格位置都是(0,0,0),这是可能的:)

5 个解决方案

#1


21  

object.position is always local to the object. If you want to get the position in world space you need to get it from object.matrixWorld.

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

Try with this:

试试这个:

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

r58

r58


Update:

The function getPositionFromMatrix() has been renamed to setFromMatrixPosition().

函数getPositionFromMatrix()已被重命名为setFromMatrixPosition()。

#2


11  

For finding where in world space is the geometry centroid, try this:

为了找到世界空间的几何中心,试试这个:

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

r58

#3


4  

Yeah. after some talk with mrdoob, i realized that .position of objects are local to theirselves. My situation was to find the center point of my mesh considering the vertices. Below is the code to get the centroid which came from an answer #447 ( https://github.com/mrdoob/three.js/issues/447 )

是的。在和多布尔谈过之后,我意识到,物体的位置对他们来说是局部的。我的情况是找出我的网格的中心点,考虑到顶点。下面是获取来自答案#447的centroid的代码(https://github.com/mrdoob/three.js/addrees/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);

Now we have centroid of geometry...

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

Update according to https://github.com/mrdoob/three.js/wiki/Migration, the .addSelf had been renamed to .add after r55

根据https://github.com/mrdoob/three.js/wiki/Migration,将.addSelf重新命名为.add,在r55之后。

#4


2  

alert(objMesh.matrixWorld.getPosition().x + ',' + objMesh.matrixWorld.getPosition().y + ',' + objMesh.matrixWorld.getPosition().z);

#5


0  

According to this post the center of gravity C for a mesh can be found by

根据这篇文章,可以找到一个网格的重心。

C = [sum of all (A*R)] / [sum of all A]
A = (area of a face * 2)
R = face centroid = average of vertices making the face

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

and here is the code in three.js

这是三个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;
}

#1


21  

object.position is always local to the object. If you want to get the position in world space you need to get it from object.matrixWorld.

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

Try with this:

试试这个:

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

r58

r58


Update:

The function getPositionFromMatrix() has been renamed to setFromMatrixPosition().

函数getPositionFromMatrix()已被重命名为setFromMatrixPosition()。

#2


11  

For finding where in world space is the geometry centroid, try this:

为了找到世界空间的几何中心,试试这个:

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

r58

#3


4  

Yeah. after some talk with mrdoob, i realized that .position of objects are local to theirselves. My situation was to find the center point of my mesh considering the vertices. Below is the code to get the centroid which came from an answer #447 ( https://github.com/mrdoob/three.js/issues/447 )

是的。在和多布尔谈过之后,我意识到,物体的位置对他们来说是局部的。我的情况是找出我的网格的中心点,考虑到顶点。下面是获取来自答案#447的centroid的代码(https://github.com/mrdoob/three.js/addrees/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);

Now we have centroid of geometry...

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

Update according to https://github.com/mrdoob/three.js/wiki/Migration, the .addSelf had been renamed to .add after r55

根据https://github.com/mrdoob/three.js/wiki/Migration,将.addSelf重新命名为.add,在r55之后。

#4


2  

alert(objMesh.matrixWorld.getPosition().x + ',' + objMesh.matrixWorld.getPosition().y + ',' + objMesh.matrixWorld.getPosition().z);

#5


0  

According to this post the center of gravity C for a mesh can be found by

根据这篇文章,可以找到一个网格的重心。

C = [sum of all (A*R)] / [sum of all A]
A = (area of a face * 2)
R = face centroid = average of vertices making the face

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

and here is the code in three.js

这是三个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;
}