Simplest way to add a vector to bufferAttribute?

	var vector = new THREE.Vector2( shiftX,shiftY );
    let uvAttr = geometry.attributes.uv;
	let idx = geometry.index;
			case idx.getX(0) :
				uvAttr.setXY( uvAttr.getX( idx.getX(0) )+vector.x , uvAttr.getY( idx.getX(0) )+vector.y );

As you can see, what I currently have seems like a mess… Is there any simpler way to add a vector to a bufferAttribute?

this line:

uvAttr.setXY( uvAttr.getX( idx.getX(0) )+vector.x , uvAttr.getY( idx.getX(0) )+vector.y );

Are you computing UVs on a shape buffer geometry?

no, just a regular BufferGeometry.

Would be cool to provide an example, that shows the geometry and how you compute UV for it.
If it’s not an ultimately top secret project. :slight_smile:
jsfiddle, codepen, github repo etc.

I’m basically just looking for a simpler way to perform operations on bufferAttributes (doesn’t have to be uvs), where the indexed values get added/multiplied/divided by Vectors…

It seems a bit long and arduous to have to do the getX/setX , and vector.x, etc… Would be nice to just be able to do something like:

bufferAttr.multiply( vector, index );

So I thought I’d ask in-case I’m overlooking something…


You can copy the buffer at an offset into a work (temporary) vector(s) and do your operations on the vector objects. At the end of the computation you write back the vector object into the buffer.

1 Like

I’m curious about this as well.
I have a shape, and would like to change every x in all the attributes position vector3s to be negative instead of just doing that to the mesh they’re in. (So the points can be used matching-ly in other things)

Should be a function out there that does what is asked in this thread

not sure, but i solved my issue with this … maybe it can be used in what you’re’ doing, prominent.

                    const points = [];

                    let positions = objInput.geometry.attributes.position.array;
                    let ptCout = positions.length / 3;
                    for (let i = 0; i < ptCout; i++)
                        let px = positions[i * 3];
                        let pxN = px * -1;
                        let py = positions[i * 3 + 1];
                        let pyN = py;

                        points.push( new THREE.Vector2(


                        ) );
                    console.log('positions: ',positions);
                // }
                console.log('points: ',points);

this is to put them in vector2’s but obviously can be used for vector3.

(objInput is a mesh. if you’re doing it straight to the buffergeometry, remove ‘objInput.geometry’, replace with your geometry var name.)

again, obviously, this is just what I needed to do… (making all x’s be negative) but anything could be done to each point using this method.