From 74ce4d4c523432197be178786a8e56be6157741e Mon Sep 17 00:00:00 2001 From: gz65555 Date: Tue, 21 Feb 2023 11:03:09 +0800 Subject: [PATCH 1/4] 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..55139f312a 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; } + + /** + * json serialization. + * return {r: number, g: number, b: number, a: number} + */ + 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..ad29ad3c8b 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; } + + /** + * JSON serialization. + * @returns A string 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..2174e90adf 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; } + + /** + * JSON serialization. + * @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..66b1178136 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; } + + /** + * JSON serialization. + * @returns A string 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..14a61b8b2e 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}'); + }); }); From 7e690c352d8295ba0a5c5ccbbbdedac608e9745b Mon Sep 17 00:00:00 2001 From: gz65555 Date: Tue, 21 Feb 2023 11:18:51 +0800 Subject: [PATCH 2/4] test: fix test result --- tests/src/math/Vector3.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/src/math/Vector3.test.ts b/tests/src/math/Vector3.test.ts index 14a61b8b2e..0dfaeb5cfd 100644 --- a/tests/src/math/Vector3.test.ts +++ b/tests/src/math/Vector3.test.ts @@ -260,6 +260,6 @@ describe("Vector3 test", () => { it("toJSON", () => { const a = new Vector3(2, 3, 4); - expect(JSON.stringify(a)).to.eq("{x:2,y:3,z:4}"); + expect(JSON.stringify(a)).to.eq('{"x":2,"y":3,"z":4}'); }); }); From 090d4ab6d8ebf1a194f60f269883d39312f8d511 Mon Sep 17 00:00:00 2001 From: gz65555 Date: Tue, 21 Feb 2023 15:16:06 +0800 Subject: [PATCH 3/4] docs: refactor comments --- packages/math/src/Color.ts | 4 ++-- packages/math/src/Quaternion.ts | 2 +- packages/math/src/Vector2.ts | 2 +- packages/math/src/Vector4.ts | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/math/src/Color.ts b/packages/math/src/Color.ts index 55139f312a..bf78f418f2 100644 --- a/packages/math/src/Color.ts +++ b/packages/math/src/Color.ts @@ -305,8 +305,8 @@ export class Color implements IClone, ICopy { } /** - * json serialization. - * return {r: number, g: number, b: number, a: number} + * JSON serialization. + * @return A JSON representation of this color */ toJSON(): ColorLike { return { diff --git a/packages/math/src/Quaternion.ts b/packages/math/src/Quaternion.ts index ad29ad3c8b..60102c5b7f 100644 --- a/packages/math/src/Quaternion.ts +++ b/packages/math/src/Quaternion.ts @@ -759,7 +759,7 @@ export class Quaternion implements IClone, ICopy, ICopy { /** * JSON serialization. - * @returns A string representation of this vector + * @returns A JSON representation of this vector */ toJSON(): Vector2Like { return { diff --git a/packages/math/src/Vector4.ts b/packages/math/src/Vector4.ts index 66b1178136..6ac13f33d5 100644 --- a/packages/math/src/Vector4.ts +++ b/packages/math/src/Vector4.ts @@ -506,7 +506,7 @@ export class Vector4 implements IClone, ICopy { /** * JSON serialization. - * @returns A string representation of this vector + * @returns A JSON representation of this vector */ toJSON(): Vector4Like { return { From 88cf4d0e36bdbdd7d7f51a50b159a665be5d9faf Mon Sep 17 00:00:00 2001 From: gz65555 Date: Tue, 21 Feb 2023 15:30:43 +0800 Subject: [PATCH 4/4] docs: fix comments --- packages/math/src/Color.ts | 2 +- packages/math/src/Quaternion.ts | 2 +- packages/math/src/Vector2.ts | 2 +- packages/math/src/Vector3.ts | 2 +- packages/math/src/Vector4.ts | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/math/src/Color.ts b/packages/math/src/Color.ts index bf78f418f2..c9d9385db0 100644 --- a/packages/math/src/Color.ts +++ b/packages/math/src/Color.ts @@ -305,7 +305,7 @@ export class Color implements IClone, ICopy { } /** - * JSON serialization. + * Serialize this color to a JSON representation. * @return A JSON representation of this color */ toJSON(): ColorLike { diff --git a/packages/math/src/Quaternion.ts b/packages/math/src/Quaternion.ts index 60102c5b7f..25467985b2 100644 --- a/packages/math/src/Quaternion.ts +++ b/packages/math/src/Quaternion.ts @@ -758,7 +758,7 @@ export class Quaternion implements IClone, ICopy, ICopy { } /** - * JSON serialization. + * Serialize this vector to a JSON representation. * @returns A JSON representation of this vector */ toJSON(): Vector2Like { diff --git a/packages/math/src/Vector3.ts b/packages/math/src/Vector3.ts index 2174e90adf..a5eec72e9b 100644 --- a/packages/math/src/Vector3.ts +++ b/packages/math/src/Vector3.ts @@ -587,7 +587,7 @@ export class Vector3 implements IClone, ICopy { } /** - * JSON serialization. + * Serialize this vector to a JSON representation. * @returns A JSON representation of this vector */ toJSON(): Vector3Like { diff --git a/packages/math/src/Vector4.ts b/packages/math/src/Vector4.ts index 6ac13f33d5..7328b3b09f 100644 --- a/packages/math/src/Vector4.ts +++ b/packages/math/src/Vector4.ts @@ -505,7 +505,7 @@ export class Vector4 implements IClone, ICopy { } /** - * JSON serialization. + * Serialize this vector to a JSON representation. * @returns A JSON representation of this vector */ toJSON(): Vector4Like {