From 52ee7c5a1c0894a81d1878eac9eddcb9be803ef5 Mon Sep 17 00:00:00 2001 From: Jesper van den Ende Date: Mon, 12 Aug 2024 13:16:40 +0200 Subject: [PATCH] Allow passing quaternions to vec4 --- src/math/Vec4.js | 6 ++++-- test/unit/src/math/Vec4.test.js | 23 ++++++++++++++++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/math/Vec4.js b/src/math/Vec4.js index 4b99574cb..099a39479 100644 --- a/src/math/Vec4.js +++ b/src/math/Vec4.js @@ -1,15 +1,17 @@ import { Vec2 } from "./Vec2.js"; import { Vec3 } from "./Vec3.js"; import { Mat4 } from "./Mat4.js"; +import { Quat } from "./Quat.js"; /** * @typedef {() => Vec4} vec4SetEmptySignature * @typedef {(vec: Vec2) => Vec4} vec4SetVec2Signature * @typedef {(vec: Vec3) => Vec4} vec4SetVec3Signature * @typedef {(vec: Vec4) => Vec4} vec4SetVec4Signature + * @typedef {(vec: Quat) => Vec4} vec4SetQuatSignature * @typedef {(x?: number, y?: number, z?: number, w?: number) => Vec4} vec4SetNumNumSignature * @typedef {(xyzw: number[]) => Vec4} vec4SetArraySignature - * @typedef {import("./MathTypes.js").MergeParameters} Vec4Parameters + * @typedef {import("./MathTypes.js").MergeParameters} Vec4Parameters */ /** @@ -73,7 +75,7 @@ export class Vec4 { if (args.length == 1) { const arg = args[0]; - if (arg instanceof Vec4) { + if (arg instanceof Vec4 || arg instanceof Quat) { this._x = arg.x; this._y = arg.y; this._z = arg.z; diff --git a/test/unit/src/math/Vec4.test.js b/test/unit/src/math/Vec4.test.js index 86c01a331..f50e64298 100644 --- a/test/unit/src/math/Vec4.test.js +++ b/test/unit/src/math/Vec4.test.js @@ -1,5 +1,5 @@ import { assertEquals, assertNotStrictEquals } from "std/testing/asserts.ts"; -import { Mat4, Vec2, Vec3, Vec4 } from "../../../../src/mod.js"; +import { Mat4, Quat, Vec2, Vec3, Vec4 } from "../../../../src/mod.js"; import { assertAlmostEquals, assertVecAlmostEquals } from "../../../../src/util/asserts.js"; Deno.test({ @@ -41,6 +41,16 @@ Deno.test({ }, }); +Deno.test({ + name: "Create with Quat", + fn() { + const quat = new Quat([1, 2, 3, 4]); + const vec = new Vec4(quat); + + assertEquals(vec.toArray(), [1, 2, 3, 4]); + }, +}); + Deno.test({ name: "Create with one number", fn() { @@ -155,6 +165,17 @@ Deno.test({ }, }); +Deno.test({ + name: "Set with Quat", + fn() { + const quat = new Quat([1, 2, 3, 4]); + const vec = new Vec4(); + vec.set(quat); + + assertEquals(vec.toArray(), [1, 2, 3, 4]); + }, +}); + Deno.test({ name: "Set with two numbers", fn() {