Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an expand method to Vector2, Vector3, Quaternion, etc. #30261

Closed
philipswan opened this issue Jan 3, 2025 · 3 comments
Closed

Add an expand method to Vector2, Vector3, Quaternion, etc. #30261

philipswan opened this issue Jan 3, 2025 · 3 comments
Milestone

Comments

@philipswan
Copy link

Description

Currently repetitive typing is needed when calling some functions that take, for example, x, y, and z as parameters. This can lead to lengthy lines of code when these values come from a Vector3 with a long name. Implementing an expand method should save coding time and lead to cleaner more readable code.

Solution

// Add an expand method to, for example, the THREE.Vector3 prototype
THREE.Vector3.prototype.expand = function () {
  return [this.x, this.y, this.z];
};

// Function that we'd like to pass coordinated to.
function xyzFunction(x, y, z) {
  console.log(x, y, z);
}

// Use the expand method with the spread operator
const myVector = new THREE.Vector3(1, 2, 3);
xyzFunction(...myVector.expand()); // Outputs: 1 2 3

Alternatives

Overloading all of the functions that take x, y, z as inputs with versions that take Vector3 (don't like this as much)

Additional context

Seeing this used in examples will also expose newbies to the use of the spread operator.

@Mugen87
Copy link
Collaborator

Mugen87 commented Jan 4, 2025

xyzFunction(...myVector.expand()); // Outputs: 1 2 3

I would not recommend doing this since every time expand() is called the method returns a new array object. If this code is used in an animation loop multiple times, you increase the memory churn and thus end up with more GC overhead. Besides, object creation tends to be an expensive operation in JavaScript so you want to avoid it whenever possible. At least in performance critical code sections. So overall it's indeed better to write functions to accept Vector3 objects or alternatively pass in numerical values:

xyzFunction(myVector);

// or

xyzFunction(myVector.x, myVector.y, myVector.z);

@Mugen87
Copy link
Collaborator

Mugen87 commented Jan 4, 2025

If you insist in working with arrays, you can use the existing myVector.toArray(), btw. We have added this for the serialization/deserialization logic but you can use it of course for other use cases as well. Just keep in mind the previous comment if you head for best performance.

@philipswan
Copy link
Author

philipswan commented Jan 4, 2025

Cool - its basically already been implemented. With toArray() you can even pass in an empty scratch array to avoid making lots of new calls when the code needs to be performant.

@Mugen87 Mugen87 closed this as completed Jan 4, 2025
@Mugen87 Mugen87 added this to the r173 milestone Jan 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants