@nativewrappers/common 0.0.55 → 0.0.57
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/utils/Vector.d.ts +6 -0
- package/utils/Vector.js +8 -1
package/package.json
CHANGED
package/utils/Vector.d.ts
CHANGED
|
@@ -47,6 +47,11 @@ type VectorLike = Vec | Vector;
|
|
|
47
47
|
* Utility type to get the vector type of an object based on its component.
|
|
48
48
|
*/
|
|
49
49
|
type InferVector<T> = T extends Vec4 | VectorN<4> ? Vector4 : T extends Vec3 | VectorN<3> ? Vector3 : T extends Vec2 | VectorN<2> ? Vector2 : any;
|
|
50
|
+
type VectorKey = "x" | "y" | "z" | "w";
|
|
51
|
+
type VectorSwizzle = Vec2Swizzle | Vec3Swizzle | Vec4Swizzle;
|
|
52
|
+
type Vec2Swizzle = `${VectorKey}${VectorKey}`;
|
|
53
|
+
type Vec3Swizzle = `${VectorKey}${VectorKey}${VectorKey}`;
|
|
54
|
+
type Vec4Swizzle = `${VectorKey}${VectorKey}${VectorKey}${VectorKey}`;
|
|
50
55
|
/**
|
|
51
56
|
* A base vector class inherited by all vector classes.
|
|
52
57
|
*/
|
|
@@ -302,6 +307,7 @@ declare abstract class Vector {
|
|
|
302
307
|
* @returns The magnitude of the vector.
|
|
303
308
|
*/
|
|
304
309
|
get Length(): number;
|
|
310
|
+
swizzle<T extends VectorSwizzle>(components: T): T extends Vec2Swizzle ? Vector2 : T extends Vec3Swizzle ? Vector3 : Vector4;
|
|
305
311
|
}
|
|
306
312
|
/**
|
|
307
313
|
* Represents a 2-dimensional vector.
|
package/utils/Vector.js
CHANGED
|
@@ -436,6 +436,13 @@ class Vector {
|
|
|
436
436
|
sum += value * value;
|
|
437
437
|
return Math.sqrt(sum);
|
|
438
438
|
}
|
|
439
|
+
swizzle(components) {
|
|
440
|
+
if (!/^[xyzw]+$/.test(components))
|
|
441
|
+
throw new Error(`Invalid key in swizzle components (${components}).`);
|
|
442
|
+
const arr = components.split("").map((char) => this[char] ?? 0);
|
|
443
|
+
//@ts-ignore
|
|
444
|
+
return Vector.create(...arr);
|
|
445
|
+
}
|
|
439
446
|
}
|
|
440
447
|
/**
|
|
441
448
|
* Represents a 2-dimensional vector.
|
|
@@ -550,7 +557,7 @@ export class Vector4 extends Vector {
|
|
|
550
557
|
*/
|
|
551
558
|
constructor(x, y = x, z = y, w = z) {
|
|
552
559
|
super(x, y, z, w);
|
|
553
|
-
this.z =
|
|
560
|
+
this.z = z;
|
|
554
561
|
this.w = w;
|
|
555
562
|
}
|
|
556
563
|
/**
|