Update positions of geometry

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

Can somebody point me where I am going wrong…

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:

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

1 Like

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 :

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;
    }
}
1 Like