【问题标题】:Getting vertices world position after its parent mesh's been scaled缩放其父网格后获取顶点世界位置
【发布时间】:2017-04-22 22:18:27
【问题描述】:

我需要获取网格中人脸的世界位置。这是我的代码:

var that = this,
    addPoint,
    geometry = mesh.geometry,
    worldPos = mesh.getWorldPosition();

that.allMeshes[mesh.uuid] = {
    mesh: mesh,
    points: {}
};

addPoint = function (currPoint, face) {
    var point = that.octree.add([worldPos.x + currPoint.x, worldPos.y + currPoint.y, worldPos.z + currPoint.z], {mesh: mesh, geometry: geometry, face: face});
    that.allMeshes[mesh.uuid].points[point.id] = point;
};

geometry.faces.forEach(function(face) {

    //Get the faces points cords
    addPoint(geometry.vertices[face.a], face);
    addPoint(geometry.vertices[face.b], face);
    addPoint(geometry.vertices[face.c], face);

}, this);

这一切都很好,除非父网格被缩放。有谁知道如何解决这个问题?

【问题讨论】:

  • THREE.Object3D.localToWorld,它可以将局部向量转换为世界向量。用法:var worldVertex = yourMesh.localToWorld(yourMesh.geometry.vertices[i]);This answer 有一些进一步的解释。
  • 谢谢,我去看看。
  • 是的,这很好,谢谢。

标签: javascript three.js


【解决方案1】:

感谢@TheJim01,我找到了这个解决方案。

var that = this,
    addPoint,
    geometry = mesh.geometry;

that.allMeshes[mesh.uuid] = {
    mesh: mesh,
    points: {}
};

addPoint = function (currPoint, face) {

    //Get the world position like this.
    var vector = currPoint.clone();
    mesh.localToWorld(vector);

    var point = that.octree.add([vector.x, vector.y, vector.z], {mesh: mesh, geometry: geometry, face: face});
    that.allMeshes[mesh.uuid].points[point.id] = point;
};

//Run this on the mesh
mesh.updateMatrixWorld();


geometry.faces.forEach(function(face) {

    //Get the faces points cords
    addPoint(geometry.vertices[face.a], face);
    addPoint(geometry.vertices[face.b], face);
    addPoint(geometry.vertices[face.c], face);

}, this);

这是一个类似的问题: How to get the absolute position of a vertex in three.js?

【讨论】: