0

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!

flay_x
  • 3
  • 2

1 Answers1

1

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>
prisoner849
  • 16,894
  • 4
  • 34
  • 68
  • Thank you. But I've thried somethig like this. And I always have that "bath.geometry is undefined" error. May be I use somewhere wrong? Here are examples, what I did [link](http://rsidorkf.beget.tech/bath/bath.html) [link](http://rsidorkf.beget.tech/bath/bath1.html) – flay_x Jul 07 '18 at 15:30
  • @flay_x If you do `console.log(bath)`, you can see, that it's not `THREE.Mesh()`, but, I bet, it will be `THREE.Group()` or `THREE.Object3D()`. Thus you have to work with the children of the `bath` object. – prisoner849 Jul 07 '18 at 16:03