three.jsverticesbuffer-geometryobjloader

THREE.js - vertex's coordinates of BufferGeometry (ObjLoader)


I'm new to Three.js, and have an issue with getting coordinates of 2 verticies. I need their x,y,z coordinates to calculate and draw sizes.

I used ObjLoader and I know exactly ID's (indexes) of verticies - 11889 and 11877.

I tried to use some properties of BufferGeometry, but, obviously, wrong. So here is the simple code I'm staying with.

                var coord1 = [11889,0,0,0], coord2 = [11877,0,0,0];

            var mtlLoader = new THREE.MTLLoader();      
            mtlLoader.load(
                'bath1.mtl',
                function( materials ) 
                {
                    materials.preload();
                    oLoader.setMaterials( materials );
                    oLoader.load('bath1.obj', function ( bath ) 
                    {
                        bath.name="bath1";
                        bath.position.y -=1; bath.position.x -=0.25; bath.position.z -=1;
                        scene.add( bath ); 
                    },
                    function ( xhr ) {},
                    function ( error ) {console.log( 'An error happened' );}
                    );

                }
            );

I read a few similar questions, but I could not adapt the answers for my problem. What should I do to get coordinates? Thanks!


Solution

  • As an option, you can use .fromArray() method of THREE.Vector3(), this way:

    var geo = new THREE.PlaneBufferGeometry(5, 5);
    var bath = new THREE.Mesh(geo, new THREE.MeshBasicMaterial());
    
    var vert1 = new THREE.Vector3().fromArray(bath.geometry.attributes.position.array, 1 * 3);
    var vert2 = new THREE.Vector3().fromArray(bath.geometry.attributes.position.array, 2 * 3);
    
    console.log(vert1, vert2);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/94/three.min.js"></script>

    Another approach is to use .getXXX() methods of THREE.BufferAttribute():

    var geom = new THREE.PlaneBufferGeometry(5, 5);
    var bath = new THREE.Mesh(geom, new THREE.MeshBasicMaterial());
    
    var position = bath.geometry.attributes.position;
    var x = position.getX(1);
    var y = position.getY(1);
    var z = position.getZ(1);
    
    console.log(x, y, z);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/94/three.min.js"></script>