# How can we get world position of a vertex?

**URL:** <https://discourse.threejs.org/t/how-can-we-get-world-position-of-a-vertex/13275>\
**Category:** Questions\
**Tags:** transform\
**Created:** [March 4, 2020, 8:36am UTC](https://discourse.threejs.org/t/how-can-we-get-world-position-of-a-vertex/13275 "2020-03-04T08:36:52Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Athira\_Narayan](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/athira_narayan/32/9201_2.png) [@Athira\_Narayan](https://discourse.threejs.org/u/Athira_Narayan)\
**Post date:** [March 4, 2020, 8:36am UTC](https://discourse.threejs.org/t/how-can-we-get-world-position-of-a-vertex/13275/1 "2020-03-04T08:36:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mugen87](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mugen87/32/5645_2.png) [@Mugen87](https://discourse.threejs.org/u/Mugen87)\
**Post date:** [March 4, 2020, 9:17am UTC](https://discourse.threejs.org/t/how-can-we-get-world-position-of-a-vertex/13275/2 "2020-03-04T09:17:37Z")

</div>

Extract the vertex from the position attribute and then use [Object3D.localToWorld()](https://threejs.org/docs/index.html#api/en/core/Object3D.localToWorld).

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

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

object.localToWorld( vertex );

```

---

<div class="post-metadata">

**Author:** ![Athira\_Narayan](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/athira_narayan/32/9201_2.png) [@Athira\_Narayan](https://discourse.threejs.org/u/Athira_Narayan)\
**Post date:** [March 4, 2020, 9:31am UTC](https://discourse.threejs.org/t/how-can-we-get-world-position-of-a-vertex/13275/3 "2020-03-04T09:31:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![prisoner849](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/prisoner849/32/535_2.png) [@prisoner849](https://discourse.threejs.org/u/prisoner849)\
**Post date:** [March 4, 2020, 9:34am UTC](https://discourse.threejs.org/t/how-can-we-get-world-position-of-a-vertex/13275/4 "2020-03-04T09:34:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Athira\_Narayan](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/athira_narayan/32/9201_2.png) [@Athira\_Narayan](https://discourse.threejs.org/u/Athira_Narayan)\
**Post date:** [March 4, 2020, 9:38am UTC](https://discourse.threejs.org/t/how-can-we-get-world-position-of-a-vertex/13275/5 "2020-03-04T09:38:19Z")

</div>

Index is not defined 🤔

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

---

<div class="post-metadata">

**Author:** ![prisoner849](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/prisoner849/32/535_2.png) [@prisoner849](https://discourse.threejs.org/u/prisoner849)\
**Post date:** [March 4, 2020, 9:47am UTC](https://discourse.threejs.org/t/how-can-we-get-world-position-of-a-vertex/13275/6 "2020-03-04T09:47:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Athira\_Narayan](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/athira_narayan/32/9201_2.png) [@Athira\_Narayan](https://discourse.threejs.org/u/Athira_Narayan)\
**Post date:** [March 4, 2020, 9:50am UTC](https://discourse.threejs.org/t/how-can-we-get-world-position-of-a-vertex/13275/7 "2020-03-04T09:50:24Z")

</div>

Thankyou, but could you share about it in detail?

---

<div class="post-metadata">

**Author:** ![Mugen87](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mugen87/32/5645_2.png) [@Mugen87](https://discourse.threejs.org/u/Mugen87)\
**Post date:** [March 4, 2020, 9:58am UTC](https://discourse.threejs.org/t/how-can-we-get-world-position-of-a-vertex/13275/8 "2020-03-04T09:58:23Z")

</div>

@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/](https://threejs.org/)

---

<div class="post-metadata">

**Author:** ![prisoner849](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/prisoner849/32/535_2.png) [@prisoner849](https://discourse.threejs.org/u/prisoner849)\
**Post date:** [March 4, 2020, 10:01am UTC](https://discourse.threejs.org/t/how-can-we-get-world-position-of-a-vertex/13275/9 "2020-03-04T10:01:24Z")

</div>

Feel no hesitation to read the docs attentively: [three.js docs](https://threejs.org/docs/#api/en/math/Vector3.fromBufferAttribute) 🙂

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.

---

<div class="post-metadata">

**Author:** ![Athira\_Narayan](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/athira_narayan/32/9201_2.png) [@Athira\_Narayan](https://discourse.threejs.org/u/Athira_Narayan)\
**Post date:** [March 4, 2020, 1:22pm UTC](https://discourse.threejs.org/t/how-can-we-get-world-position-of-a-vertex/13275/10 "2020-03-04T13:22:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Athira\_Narayan](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/athira_narayan/32/9201_2.png) [@Athira\_Narayan](https://discourse.threejs.org/u/Athira_Narayan)\
**Post date:** [March 5, 2020, 11:22am UTC](https://discourse.threejs.org/t/how-can-we-get-world-position-of-a-vertex/13275/11 "2020-03-05T11:22:25Z")

</div>

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/](https://jsfiddle.net/daqo07y5/3/)

---

<div class="post-metadata">

**Author:** ![prisoner849](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/prisoner849/32/535_2.png) [@prisoner849](https://discourse.threejs.org/u/prisoner849)\
**Post date:** [March 5, 2020, 12:34pm UTC](https://discourse.threejs.org/t/how-can-we-get-world-position-of-a-vertex/13275/12 "2020-03-05T12:34:06Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![makc3d](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/makc3d/32/10452_2.png) [@makc3d](https://discourse.threejs.org/u/makc3d)\
**Post date:** [March 5, 2020, 3:30pm UTC](https://discourse.threejs.org/t/how-can-we-get-world-position-of-a-vertex/13275/13 "2020-03-05T15:30:33Z")

</div>

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 😃

---

<div class="post-metadata">

**Author:** ![pailhead](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/pailhead/32/85_2.png) [@pailhead](https://discourse.threejs.org/u/pailhead)\
**Post date:** [March 6, 2020, 1:15am UTC](https://discourse.threejs.org/t/how-can-we-get-world-position-of-a-vertex/13275/14 "2020-03-06T01:15:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Athira\_Narayan](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/athira_narayan/32/9201_2.png) [@Athira\_Narayan](https://discourse.threejs.org/u/Athira_Narayan)\
**Post date:** [March 6, 2020, 4:33am UTC](https://discourse.threejs.org/t/how-can-we-get-world-position-of-a-vertex/13275/15 "2020-03-06T04:33:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pailhead](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/pailhead/32/85_2.png) [@pailhead](https://discourse.threejs.org/u/pailhead)\
**Post date:** [March 6, 2020, 7:57am UTC](https://discourse.threejs.org/t/how-can-we-get-world-position-of-a-vertex/13275/16 "2020-03-06T07:57:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sonygod](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/sonygod/32/23416_2.png) [@sonygod](https://discourse.threejs.org/u/sonygod)\
**Post date:** [February 23, 2022, 1:44am UTC](https://discourse.threejs.org/t/how-can-we-get-world-position-of-a-vertex/13275/17 "2022-02-23T01:44:08Z")

</div>

> [@makc3d](#):
>
> `mesh.localToWorld`

first，you need to

```
mesh.updateMatrixWorld();

```

---

<div class="post-metadata">

**Author:** ![Michael\_Nicol](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/michael_nicol/32/14659_2.png) [@Michael\_Nicol](https://discourse.threejs.org/u/Michael_Nicol)\
**Post date:** [June 2, 2023, 4:33pm UTC](https://discourse.threejs.org/t/how-can-we-get-world-position-of-a-vertex/13275/18 "2023-06-02T16:33:47Z")

</div>

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.
