Skip to content

Commit

Permalink
Add toJSON in base math class (#1380) (#1409)
Browse files Browse the repository at this point in the history
* feat: add toJSON in base math class
  • Loading branch information
gz65555 authored Mar 15, 2023
1 parent 92fb306 commit 4e94f65
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/math/src/Color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,19 @@ export class Color implements IClone<Color>, ICopy<ColorLike, Color> {

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 {
Expand Down
13 changes: 13 additions & 0 deletions packages/math/src/Quaternion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,19 @@ export class Quaternion implements IClone<Quaternion>, ICopy<QuaternionLike, Qua
out[outOffset + 3] = this._w;
}

/**
* Serialize this quaternion to a JSON representation.
* @returns A JSON Object representation of this quaternion
*/
toJSON(): QuaternionLike {
return {
x: this._x,
y: this._y,
z: this._z,
w: this._w
};
}

private _toYawPitchRoll(out: Vector3): void {
// https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles
const { _x: x, _y: y, _z: z, _w: w } = this;
Expand Down
11 changes: 11 additions & 0 deletions packages/math/src/Vector2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,17 @@ export class Vector2 implements IClone<Vector2>, ICopy<Vector2Like, Vector2> {
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 {
Expand Down
12 changes: 12 additions & 0 deletions packages/math/src/Vector3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,18 @@ export class Vector3 implements IClone<Vector3>, ICopy<Vector3Like, Vector3> {
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 {
Expand Down
13 changes: 13 additions & 0 deletions packages/math/src/Vector4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,19 @@ export class Vector4 implements IClone<Vector4>, ICopy<Vector4Like, Vector4> {
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 {
Expand Down
6 changes: 6 additions & 0 deletions tests/src/math/Color.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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});
})
});
5 changes: 5 additions & 0 deletions tests/src/math/Vector2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
});
});
5 changes: 5 additions & 0 deletions tests/src/math/Vector3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}');
});
});
5 changes: 5 additions & 0 deletions tests/src/math/Vector4.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}');
});
});

0 comments on commit 4e94f65

Please sign in to comment.