2017-04-19 23 views
0

我需要獲取網格中的面的世界位置。這是我的代碼爲:在縮放父網格之後獲取頂點的世界位置

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); 

這一切都工作得很好,除非父網格縮放然後。有誰知道如何解決這個問題?

+1

看看'THREE.Object3D.localToWorld'它可以改變一個地方載體向世界途徑。用法:'var worldVertex = yourMesh.localToWorld(yourMesh.geometry.vertices [i]);'。 [這個答案](http://stackoverflow.com/questions/43265361/rotating-icosahedron-with-circles-located-at-every-vertex-in-three-js/43325479#43325479)有一些進一步的解釋。 – TheJim01

+0

謝謝,我會看看。 – arpo

+0

是的,這很有用,謝謝。 – arpo

回答

0

感謝@ 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?