Javascript Three.js 如何获取网格的位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14211627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Three.js How to get position of a mesh?
提问by Tezcan
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)但不是。那么位置向量是在渲染过程之后计算出来的吗?
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 创建的,它被正确添加到场景中,质心大约是 (-8, 3, 0) 但是 objMesh 的位置向量是 (0, 0, 0) 我们是否必须先自动计算一些东西还是我应该计算它从网格的几何顶点手动?
http://81.214.75.32:8181/adminis the url
http://81.214.75.32:8181/admin是网址
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”菜单项打开菜单项并选择“Proje A?” 该对话框中出现一个对话框 select MUTFAK_1 场景将出现在该场景中,每个网格位置为 (0, 0, 0) 是可能的:)
采纳答案by Tezcan
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)
是的。在与 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);
Now we have centroid of geometry...
现在我们有了几何的质心...
Update
according to https://github.com/mrdoob/three.js/wiki/Migration, the .addSelfhad been renamed to .addafter r55
根据https://github.com/mrdoob/three.js/wiki/Migration更新,.addSelf已更名为.addr55 之后
回答by mrdoob
object.positionis always local to the object. If you want to get the position in world space you need to get it from object.matrixWorld.
object.position始终位于对象的本地。如果你想获得世界空间中的位置,你需要从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().
回答by mrdoob
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
回答by Gero3
alert(objMesh.matrixWorld.getPosition().x + ',' + objMesh.matrixWorld.getPosition().y + ',' + objMesh.matrixWorld.getPosition().z);
回答by jimver04
According to this postthe center of gravity C for a mesh can be found by
根据这篇文章,可以通过以下方式找到网格的重心 C
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
这是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;
}

