From 4e94f658fe5bea50884e11e45d26b7806273e282 Mon Sep 17 00:00:00 2001 From: Hu Song Date: Wed, 15 Mar 2023 16:07:58 +0800 Subject: [PATCH] Add `toJSON` in base math class (#1380) (#1409) * feat: add toJSON in base math class --- packages/math/src/Color.ts | 13 +++++++++++++ packages/math/src/Quaternion.ts | 13 +++++++++++++ packages/math/src/Vector2.ts | 11 +++++++++++ packages/math/src/Vector3.ts | 12 ++++++++++++ packages/math/src/Vector4.ts | 13 +++++++++++++ tests/src/math/Color.test.ts | 6 ++++++ tests/src/math/Vector2.test.ts | 5 +++++ tests/src/math/Vector3.test.ts | 5 +++++ tests/src/math/Vector4.test.ts | 5 +++++ 9 files changed, 83 insertions(+) diff --git a/packages/math/src/Color.ts b/packages/math/src/Color.ts index 00931f5f8f..c9d9385db0 100644 --- a/packages/math/src/Color.ts +++ b/packages/math/src/Color.ts @@ -303,6 +303,19 @@ export class Color implements IClone, ICopy { return (max + min) / 2; } + + /** + * Serialize this color to a JSON representation. + * @return A JSON representation of this color + */ + toJSON(): ColorLike { + return { + r: this._r, + g: this._g, + b: this._b, + a: this._a + }; + } } interface ColorLike { diff --git a/packages/math/src/Quaternion.ts b/packages/math/src/Quaternion.ts index f721f4e2f7..25467985b2 100644 --- a/packages/math/src/Quaternion.ts +++ b/packages/math/src/Quaternion.ts @@ -757,6 +757,19 @@ export class Quaternion implements IClone, ICopy, ICopy { out[outOffset] = this._x; out[outOffset + 1] = this._y; } + + /** + * Serialize this vector to a JSON representation. + * @returns A JSON representation of this vector + */ + toJSON(): Vector2Like { + return { + x: this._x, + y: this._y + }; + } } interface Vector2Like { diff --git a/packages/math/src/Vector3.ts b/packages/math/src/Vector3.ts index b41ff454bb..a5eec72e9b 100644 --- a/packages/math/src/Vector3.ts +++ b/packages/math/src/Vector3.ts @@ -585,6 +585,18 @@ export class Vector3 implements IClone, ICopy { out[outOffset + 1] = this._y; out[outOffset + 2] = this._z; } + + /** + * Serialize this vector to a JSON representation. + * @returns A JSON representation of this vector + */ + toJSON(): Vector3Like { + return { + x: this._x, + y: this._y, + z: this._z + }; + } } interface Vector3Like { diff --git a/packages/math/src/Vector4.ts b/packages/math/src/Vector4.ts index e207765d7f..7328b3b09f 100644 --- a/packages/math/src/Vector4.ts +++ b/packages/math/src/Vector4.ts @@ -503,6 +503,19 @@ export class Vector4 implements IClone, ICopy { out[outOffset + 2] = this._z; out[outOffset + 3] = this._w; } + + /** + * Serialize this vector to a JSON representation. + * @returns A JSON representation of this vector + */ + toJSON(): Vector4Like { + return { + x: this._x, + y: this._y, + z: this._z, + w: this._w + }; + } } interface Vector4Like { diff --git a/tests/src/math/Color.test.ts b/tests/src/math/Color.test.ts index 375a79181b..afd1a480dc 100644 --- a/tests/src/math/Color.test.ts +++ b/tests/src/math/Color.test.ts @@ -84,4 +84,10 @@ describe("Color test", () => { expect(Color.equals(colorLinear, colorNewLinear)).to.eq(true); } }); + + it('toJSON', ()=>{ + const color = new Color(1, 0.5, 0.5, 1); + const json = color.toJSON(); + expect(json).to.deep.eq({r: 1, g: 0.5, b: 0.5, a: 1}); + }) }); diff --git a/tests/src/math/Vector2.test.ts b/tests/src/math/Vector2.test.ts index 0353c71f39..617717df3f 100644 --- a/tests/src/math/Vector2.test.ts +++ b/tests/src/math/Vector2.test.ts @@ -203,4 +203,9 @@ describe("Vector2 test", () => { expect(toString(a.scale(2))).to.eq(toString(a)); expect(toString(a)).to.eq("vec2(6, 8)"); }); + + it("toJSON", () => { + const a = new Vector2(3, 4); + expect(a.toJSON()).to.deep.eq({ x: 3, y: 4 }); + }); }); diff --git a/tests/src/math/Vector3.test.ts b/tests/src/math/Vector3.test.ts index 6edbb9e887..0dfaeb5cfd 100644 --- a/tests/src/math/Vector3.test.ts +++ b/tests/src/math/Vector3.test.ts @@ -257,4 +257,9 @@ describe("Vector3 test", () => { a.transformByQuat(new Quaternion(2, 3, 4, 5)); expect(toString(a)).to.eq("vec3(108, 162, 216)"); }); + + it("toJSON", () => { + const a = new Vector3(2, 3, 4); + expect(JSON.stringify(a)).to.eq('{"x":2,"y":3,"z":4}'); + }); }); diff --git a/tests/src/math/Vector4.test.ts b/tests/src/math/Vector4.test.ts index fb395592a5..7a4c3d8bcb 100644 --- a/tests/src/math/Vector4.test.ts +++ b/tests/src/math/Vector4.test.ts @@ -209,4 +209,9 @@ describe("Vector4 test", () => { expect(toString(a.scale(2))).to.eq(toString(a)); expect(toString(a)).to.eq("vec4(6, 8, 0, 0)"); }); + + it("toJSON", () => { + const a = new Vector4(3, 4, 5, 0); + expect(JSON.stringify(a)).to.eq('{"x":3,"y":4,"z":5,"w":0}'); + }); });