diff --git a/packages/math/src/Color.ts b/packages/math/src/Color.ts index 018ff3ad8..cb67fa1c2 100644 --- a/packages/math/src/Color.ts +++ b/packages/math/src/Color.ts @@ -258,6 +258,19 @@ export class Color implements IClone, ICopy { return this; } + /** + * Copy to color like object. + * @param target - Color like object. + * @returns This Color like object + */ + copyTo(target: ColorLike): ColorLike { + target.r = this._r; + target.g = this._g; + target.b = this._b; + target.a = this._a; + return target; + } + /** * Copy from array like object. * @param source - Array like object diff --git a/packages/math/src/ICopy.ts b/packages/math/src/ICopy.ts index ac0f210c5..0bf4d1212 100644 --- a/packages/math/src/ICopy.ts +++ b/packages/math/src/ICopy.ts @@ -7,4 +7,10 @@ export interface ICopy { * @returns This object */ copyFrom(source: S): T; + + /** + * Copy to target object. + * @returns This target object + */ + copyTo?(target: S): S; } diff --git a/packages/math/src/Quaternion.ts b/packages/math/src/Quaternion.ts index 0b396ae89..112dcc12d 100644 --- a/packages/math/src/Quaternion.ts +++ b/packages/math/src/Quaternion.ts @@ -730,6 +730,19 @@ export class Quaternion implements IClone, ICopy, ICopy { return this; } + /** + * Copy to vector2 like object. + * @param target - Vector2 like object + * @returns This Vector2 like object + */ + copyTo(target: Vector2Like): Vector2Like { + target.x = this._x; + target.y = this._y; + return target; + } + /** * Copy the value of this vector from an array. * @param array - The array diff --git a/packages/math/src/Vector3.ts b/packages/math/src/Vector3.ts index a5eec72e9..db6f2cb9f 100644 --- a/packages/math/src/Vector3.ts +++ b/packages/math/src/Vector3.ts @@ -561,6 +561,18 @@ export class Vector3 implements IClone, ICopy { return this; } + /** + * Copy to vector3 like object. + * @param target - Vector3 like object + * @returns This Vector3 like object + */ + copyTo(target: Vector3Like): Vector3Like { + target.x = this._x; + target.y = this._y; + target.z = this._z; + return target; + } + /** * Copy the value of this vector from an array. * @param array - The array diff --git a/packages/math/src/Vector4.ts b/packages/math/src/Vector4.ts index 7328b3b09..7884f8330 100644 --- a/packages/math/src/Vector4.ts +++ b/packages/math/src/Vector4.ts @@ -477,6 +477,19 @@ export class Vector4 implements IClone, ICopy { return this; } + /** + * Copy to vector4 like object. + * @param target - Vector4 like object + * @returns This Vector4 like object + */ + copyTo(target: Vector4Like): Vector4Like { + target.x = this._x; + target.y = this._y; + target.z = this._z; + target.w = this._w; + return target; + } + /** * Copy the value of this vector by an array. * @param array - The array diff --git a/tests/src/math/Color.test.ts b/tests/src/math/Color.test.ts index 8fb93561b..9c961b091 100644 --- a/tests/src/math/Color.test.ts +++ b/tests/src/math/Color.test.ts @@ -58,6 +58,14 @@ describe("Color test", () => { expect(Color.equals(a, out)).to.eq(true); }); + it("copyTo", () => { + const a = new Color(1, 0, 0, 1); + const out = new Color(); + + a.copyTo(out); + expect(Color.equals(a, out)).to.eq(true); + }); + it("copyFromArray", () => { const a = new Color(); const b = new Color(0, 0, 1, 1); diff --git a/tests/src/math/Quaternion.test.ts b/tests/src/math/Quaternion.test.ts index 49e81e8ff..a2b2bdec3 100644 --- a/tests/src/math/Quaternion.test.ts +++ b/tests/src/math/Quaternion.test.ts @@ -261,6 +261,13 @@ describe("Quaternion test", () => { expect(toString(a)).to.eq(toString(out)); }); + it("copyTo", () => { + const a = new Quaternion(3, 4, 5, 0); + const out = new Quaternion(); + a.copyTo(out); + expect(toString(a)).to.eq(toString(out)); + }); + it("conjugate", () => { const a = new Quaternion(1, 1, 1, 1); expect(toString(a.conjugate())).to.eq("quat(-1, -1, -1, 1)"); diff --git a/tests/src/math/Vector2.test.ts b/tests/src/math/Vector2.test.ts index a51779a8d..fdcf0dda3 100644 --- a/tests/src/math/Vector2.test.ts +++ b/tests/src/math/Vector2.test.ts @@ -148,6 +148,13 @@ describe("Vector2 test", () => { expect(toString(a)).to.eq(toString(out)); }); + it("copyTo", () => { + const a = new Vector2(3, 4); + const out = new Vector2(); + a.copyTo(out); + expect(toString(a)).to.eq(toString(out)); + }); + it("add", () => { const a = new Vector2(3, 4); const ret = new Vector2(1, 2); diff --git a/tests/src/math/Vector3.test.ts b/tests/src/math/Vector3.test.ts index 60640f8f9..9a9c0d6b5 100644 --- a/tests/src/math/Vector3.test.ts +++ b/tests/src/math/Vector3.test.ts @@ -176,6 +176,13 @@ describe("Vector3 test", () => { expect(toString(a)).to.eq(toString(out)); }); + it("copyTo", () => { + const a = new Vector3(3, 4, 5); + const out = new Vector3(); + a.copyTo(out); + expect(toString(a)).to.eq(toString(out)); + }); + it("add", () => { const a = new Vector3(3, 4, 5); const ret = new Vector3(1, 2, 4); diff --git a/tests/src/math/Vector4.test.ts b/tests/src/math/Vector4.test.ts index 816314192..0f41d3473 100644 --- a/tests/src/math/Vector4.test.ts +++ b/tests/src/math/Vector4.test.ts @@ -158,6 +158,13 @@ describe("Vector4 test", () => { expect(toString(a)).to.eq(toString(out)); }); + it("copyTo", () => { + const a = new Vector4(3, 4, 5, 0); + const out = new Vector4(); + a.copyTo(out); + expect(toString(a)).to.eq(toString(out)); + }); + it("add", () => { const a = new Vector4(3, 4, 5, 1); const ret = new Vector4(1, 2, 4, 1);