0

I need to get the world position of faces in a mesh. This is my code for this:

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

This all works very well unless the parent mesh is scaled then. Does anyone know how to solve this?

arpo
  • 1,831
  • 2
  • 27
  • 48
  • 1
    Look at `THREE.Object3D.localToWorld` which can transform a local vector into a world vector. Usage: `var worldVertex = yourMesh.localToWorld(yourMesh.geometry.vertices[i]);`. [This answer](http://stackoverflow.com/questions/43265361/rotating-icosahedron-with-circles-located-at-every-vertex-in-three-js/43325479#43325479) has some further explanations. – TheJim01 Apr 20 '17 at 20:42
  • Thanks I'll have a look. – arpo Apr 21 '17 at 11:47
  • Yes this works great thank you. – arpo Apr 22 '17 at 14:12

1 Answers1

0

Thanks to @TheJim01 I found this solution.

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

Here's a similar question: How to get the absolute position of a vertex in three.js?

Community
  • 1
  • 1
arpo
  • 1,831
  • 2
  • 27
  • 48