Elegant way to copy a Vector3 into a Float32Array?

Hi all,

I want to let a THREE.line start at the current camera position. Is there a more elegant (1-liner) way to do that other than the following (which does work)? :thinking:

upMesh.geometry.attributes.position.array[0] = camera.position.x;
upMesh.geometry.attributes.position.array[1] = camera.position.y;
upMesh.geometry.attributes.position.array[2] = camera.position.z;

The left side is a Float32Array:

while the camera.position is a Vector3:

Thanks for any help.

Maybe something like this:

camera.position.toArray( upMesh.geometry.attributes.position.array, 0);
upMesh.geometry.attributes.position.needsUpdate = true;


upMesh.geometry.attributes.position.setXYZ(0, camera.position.x, camera.position.y, camera.position.z);
upMesh.geometry.attributes.position.needsUpdate = true;


1 Like

Thanks, @prisoner849!

Considering that the needsUpdate was part of my solution anyways, after I also set the endpoint of my upMesh line in a similar way, your proposals reduce my 2 * 3 + 1 = 7 lines of code to a mere 3, and I believe that’s as concise as it gets. :+1:

1 Like