How can we get world position of a vertex?

Can we get world position of a vertex rather than that of an object?

1 Like

Extract the vertex from the position attribute and then use Object3D.localToWorld().

const geometry = object.geometry;
const positionAttribute = geometry.getAttribute( 'position' );

const vertex = new THREE.Vector3();
vertex.fromBufferAttribute( positionAttribute, index );

object.localToWorld( vertex );
6 Likes

@Mugen87 It says geometry.getAttribute is not a function.
`
var geometry2= new THREE.BoxGeometry(5,5,5);

   var material= new THREE.MeshBasicMaterial({color:"brown"});
          var head= new THREE.Mesh(geometry2,material);
          var geometry1=new THREE.BoxGeometry(8,4,5);
          var material1= new THREE.MeshBasicMaterial({color:"green"});
          var body= new THREE.Mesh(geometry1,material1);
          const geometry = head.geometry;
          const positionAttribute = geometry.getAttribute( 'position' );
          const vertex = new THREE.Vector3();
          vertex.fromBufferAttribute( positionAttribute, index );
          head.localToWorld( vertex );
          console.log(vertex);
          body.position.set(-6.5,-0.5,0);
          scene.add(body);
          scene.add(head);`

Hi!
Use THREE.BoxBufferGeometry() instead of THREE.BoxGeometry().

1 Like

Index is not defined :thinking:

var geometry2= new THREE.BoxBufferGeometry(5,5,5);

          var material= new THREE.MeshBasicMaterial({color:"brown"});

          var head= new THREE.Mesh(geometry2,material);

          var geometry1=new THREE.BoxBufferGeometry(8,4,5);

          var material1= new THREE.MeshBasicMaterial({color:"green"});

          var body= new THREE.Mesh(geometry1,material1);

          const geometry = head.geometry;

          const positionAttribute = geometry.getAttribute( 'position' );

          const vertex = new THREE.Vector3();

          vertex.fromBufferAttribute( positionAttribute, index );

          head.localToWorld( vertex );

          console.log(vertex);

          body.position.set(-6.5,-0.5,0);

          scene.add(body);

          scene.add(head);

You have to set the value of index before using it here vertex.fromBufferAttribute( positionAttribute, index );

Thankyou, but could you share about it in detail?

@Athira_Narayan Since you are asking a lot of beginner questions, I recommend you study in more detail how three.js works. Most of your question will be automatically clear if you read one of the books listed at the homepage: https://threejs.org/

1 Like

Feel no hesitation to read the docs attentively: three.js docs :slight_smile:

index - index in the attribute.
You need to know by yourself about the vertex of the chosen index from the specified attribute you want to obtain.

2 Likes

Yeah. I get you. When we start going deeper, we get more doubts even in basics. Thankyou O:)

Pardon me if am asking too many questions. How can I get the position of the custom made geometry?
The thing is, I need to restrict the movement of the pyramid along z axis. It should move only along the top edge of red cube. If it violates that, a warning should be shown.
https://jsfiddle.net/daqo07y5/3/

I’m not sure that I got what you want to achieve.
Do you want to move the pyramid manually (using your mouse and a raycaster) and set limits for its movement?

I cant believe you guys made her switch to BufferGeometry instead of just telling her she could do

var vertexCopy = mesh.geometry.vertices[i].clone();
mesh.localToWorld(vertexCopy);

This level of cruelty should not be legal :smiley:

6 Likes

Agree with @makc3d , if Geometry is still in the library it’s a valid class to use. Much easier to extract the vertex actually.

To clarify, you have to clone it first, because otherwise you would change the vertex on the geometry, and it may render not the way you want it to.

2 Likes

I want to move the pyramids using keyboard controls and set limits for the pyramid so that movement is possible only along the top edge of the red cube.

I think you have an extremely specific use case, please follow @Mugen87’s advice, go through the more basic examples, and it will be clearer how to solve yours.

3 Likes

first,you need to

mesh.updateMatrixWorld();
1 Like

Does this account for rotation of the object? I’ve noticed that If I use a render loop to continually rotate the object, this value does not update.

Edit: I had to applyQuaternion to the vector of the shapes Quaternion to get the correct position XYZ for the vertices.