# Update positions of geometry

**URL:** https://discourse.threejs.org/t/update-positions-of-geometry/51706
**Category:** Questions
**Tags:** geometry
**Created:** [May 17, 2023, 2:47pm UTC](https://discourse.threejs.org/t/update-positions-of-geometry/51706 "2023-05-17T14:47:15Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![bugsbun1](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/bugsbun1/32/6523_2.png) [@bugsbun1](https://discourse.threejs.org/u/bugsbun1)
#### Post date: [May 17, 2023, 2:47pm UTC](https://discourse.threejs.org/t/update-positions-of-geometry/51706/1 "2023-05-17T14:47:15Z")

</div>

I am trying to update the positions of the vertices, after I constructed the geometry but I am unable to do so . I have read the “how to update things” page on threejs docs but still no success.

Here is the simple fiddle with a single geometry : [Three.js base - JSFiddle - Code Playground](https://jsfiddle.net/joe09v3u/8/)

Can somebody point me where I am going wrong…

---

<div class="post-metadata">

### Author: ![PavelBoytchev](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/pavelboytchev/32/38639_2.png) [@PavelBoytchev](https://discourse.threejs.org/u/PavelBoytchev)
#### Post date: [May 17, 2023, 6:06pm UTC](https://discourse.threejs.org/t/update-positions-of-geometry/51706/2 "2023-05-17T18:06:14Z")

</div>

Try this. It is not exactly what your code intends to do (the function below just randomly moves vertices), but it shows one possible way to update vertices’ coordinates:

```javascript
function updateGeometry() {
    if (mesh != null) {
        let g = mesh.geometry;
        let p = g.getAttribute('position');

        for (let i = 0; i<p.count; i++) { 
            let x = p.getX(i) + 0.01*(Math.random()-0.5);
            let y = p.getY(i) + 0.01*(Math.random()-0.5);
            let z = p.getZ(i) + 0.01*(Math.random()-0.5);
            p.setXYZ(i, x, y, z);
        }
        
        p.needsUpdate = true;
    }
}

```

![image](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/6/f/6f37db6afb515c7279d72a1d833cfe3355968595.png)

---

<div class="post-metadata">

### Author: ![bugsbun1](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/bugsbun1/32/6523_2.png) [@bugsbun1](https://discourse.threejs.org/u/bugsbun1)
#### Post date: [May 18, 2023, 5:06pm UTC](https://discourse.threejs.org/t/update-positions-of-geometry/51706/3 "2023-05-18T17:06:12Z")

</div>

thanks @PavelBoytchev !

I guess I needed was

`p.needsUpdate = true;`

I thought that was already going to get updated when I called `geometry.needsUpdate = true;`

Now my function is :

```javascript
function updateGeometry() {
    if (mesh != null) {
                
        let gPA = mesh.geometry.attributes.position;
        let geomPosAttr = gPA.array;
        let l = geomPosAttr.length;

        for (let i = l - 4; i > -1; i -= 1) 
            geomPosAttr[i] = positionArray[i] + positionArray[i] * Math.random()*100;
    
        gPA.needsUpdate = true;
    }
}

```
