-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
VecN/Color array conversion support #7312
Conversation
I wander if it should be on |
This would be super helpful for the React lib |
Copy implies that the target you are copying from is of the same type. |
I understand the wish for Edit: same goes for |
It was mainly from typechecking standpoint as we can convert from type |
So |
That can easily be fixed by just added undefined defaults |
Can we add an optional index to the to/from methods? I have similar methods, but I use it with large arrays and need to point to the values location. |
I can do it for |
Agreed, and for toArray I would also provide optional |
…clude optional target array
* Adds better support for converting vectors/color back and from arrays * Added null check in set method * Moved array set to separate method * Removed extra space * Removed change to vec2 ctor * Added tests * Shortened methods * Added fallback values as the original properties * Updated to and from array to include optional offsets + toArray to include optional target array * Added tests for new arguments * Switched names of array to arr for consistency * Updated order of alpha argument * Updated order of arguments in test
@kpal81xd Is there something like |
I used {number[]|ArrayBufferView} in few places. |
* array. Default is 0. | ||
* @returns {number[]} The vector as an array. | ||
* @example | ||
* const v = new pc.Vec3(20, 10, 5, 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The +1 version of Vec3 😅 Great work anyway, good job
If array is provided into |
* array. Default is 0. | ||
* @returns {number[]} The vector as an array. | ||
* @example | ||
* const v = new pc.Vec3(20, 10); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And this copypaste had to become Vec2
I would not expect so, that's not worth the cost overhead. It's up to the user to make sure array is in the right size if they don't want resize. |
Aren't we just reinventing an opinionated/non-configurable What if we just did: pc.Vec3.prototype[Symbol.iterator] = function *() {
yield this.x;
yield this.y;
yield this.z;
}
a = new pc.Vec3(1, 2, 3);
b = new pc.Vec3(11, 22, 33);
c = new pc.Vec3(111, 222, 333);
[...a, ...b, ...c];
arr = [10, 20, 30, 40, 50, 60, 70, 80]
console.log(...arr);
// At pos 5, delete three add three from a at same place:
arr.splice(5, 3, ...a);
console.log(...arr);
// At pos 2, delete nothing, splice c into arr
arr.splice(2, 0, ...c);
console.log(...arr); Outputs: We also still can't do: Array.from(a); // Should output: [1, 2, 3] (we pretty much successfully added another PC specific idiosyncrasy again) |
Motivation
When using Vectors/Colors it is very common to want to serialize and deserialize data to either be used for UI (in conjunction with observers) or to synchronise over a network
Overview
fromArray(offset: number = 0)
methods on VecN and Color classes to allow setting from arraystoArray(out: any[], offset: number = 0)
methods to VecN and Color classes to for explicit conversion to array (avoids potential class fields being added from usingObject.values
Testing