Vector3 swizzling

I often find I need to just take subelements of a Vector3, especially just x & y to convert to a vector to. Would there be any interest in adding swizzle like properties on Vector3 so you could so something like myVector.xy to get a Vector2 from a Vector3?

Hi!

let v3 = new THREE.Vector3(1, 2, 3);
let v2 = new THREE.Vector2().copy(v3)
console.log(v2);

This will give you the result Object { x: 1, y: 2 } in the console.

3 Likes

That’s useful I didn’t know about that behavior. Thanks for the pointer. I guess the swizzle approach would be a little bit more concise and flexible but this would cover my most common use case.

I agree it would be nice if this worked :

const v2 = v3.xz();

Or alternately the methods could take a target vector to avoid creating a new object :

v3.xz( v2 );
3 Likes

This independent lib was implemented, but performance always a concern~~

Other related:

2 Likes