Three.js How to get position of a mesh?

后端 未结 5 487
被撕碎了的回忆
被撕碎了的回忆 2020-12-10 06:44

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?

me.         


        
相关标签:
5条回答
  • 2020-12-10 06:57

    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 )

    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

    0 讨论(0)
  • 2020-12-10 06:58
    alert(objMesh.matrixWorld.getPosition().x + ',' + objMesh.matrixWorld.getPosition().y + ',' + objMesh.matrixWorld.getPosition().z);
    
    0 讨论(0)
  • 2020-12-10 07:01

    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

    0 讨论(0)
  • 2020-12-10 07:11

    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

    and here is the code in 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;
    }
    
    0 讨论(0)
  • 2020-12-10 07:16

    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.

    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


    Update:

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

    0 讨论(0)
提交回复
热议问题