From be8fefa2178fbb967297eb75a12eaea38d0fd59d Mon Sep 17 00:00:00 2001 From: James Chen Date: Tue, 24 Dec 2024 09:28:40 +0800 Subject: [PATCH] [v3.8.6] Fix toString for math types (#18060) --- cocos/core/math/mat3.ts | 4 ++-- cocos/core/math/mat4.ts | 9 +++++---- cocos/core/math/quat.ts | 8 ++++++++ cocos/core/math/rect.ts | 2 +- cocos/core/math/size.ts | 2 +- cocos/core/math/vec2.ts | 2 +- cocos/core/math/vec3.ts | 2 +- cocos/core/math/vec4.ts | 2 +- tests/core/math/mat3.test.ts | 16 ++++++++++++++++ tests/core/math/mat4.test.ts | 16 ++++++++++++++++ tests/core/math/rect.test.ts | 2 +- tests/core/math/size.test.ts | 2 +- tests/core/math/vec2.test.ts | 5 +++++ tests/core/math/vec3.test.ts | 5 +++++ tests/core/math/vec4.test.ts | 5 +++++ 15 files changed, 69 insertions(+), 13 deletions(-) diff --git a/cocos/core/math/mat3.ts b/cocos/core/math/mat3.ts index 6c5101129a..ad588c2ca9 100644 --- a/cocos/core/math/mat3.ts +++ b/cocos/core/math/mat3.ts @@ -833,8 +833,8 @@ export class Mat3 extends ValueType { const t = this; return `[\n${ t.m00}, ${t.m01}, ${t.m02},\n${ - t.m03},\n${t.m04}, ${t.m05},\n${ - t.m06}, ${t.m07},\n${t.m08}\n` + t.m03}, ${t.m04}, ${t.m05},\n${ + t.m06}, ${t.m07}, ${t.m08}\n` + `]`; } diff --git a/cocos/core/math/mat4.ts b/cocos/core/math/mat4.ts index 266e899004..9e2da778df 100644 --- a/cocos/core/math/mat4.ts +++ b/cocos/core/math/mat4.ts @@ -1825,11 +1825,12 @@ export class Mat4 extends ValueType { * @return 当前矩阵的字符串表示。 */ public toString (): string { + const t = this; return `[\n${ - this.m00}, ${this.m01}, ${this.m02}, ${this.m03},\n${ - this.m04}, ${this.m05}, ${this.m06}, ${this.m07},\n${ - this.m08}, ${this.m09}, ${this.m10}, ${this.m11},\n${ - this.m12}, ${this.m13}, ${this.m14}, ${this.m15}\n` + t.m00}, ${t.m01}, ${t.m02}, ${t.m03},\n${ + t.m04}, ${t.m05}, ${t.m06}, ${t.m07},\n${ + t.m08}, ${t.m09}, ${t.m10}, ${t.m11},\n${ + t.m12}, ${t.m13}, ${t.m14}, ${t.m15}\n` + ']'; } diff --git a/cocos/core/math/quat.ts b/cocos/core/math/quat.ts index 7687ba113f..01ed7930ac 100644 --- a/cocos/core/math/quat.ts +++ b/cocos/core/math/quat.ts @@ -917,6 +917,14 @@ export class Quat extends ValueType { const { x, y, z, w } = self; return x * x + y * y + z * z + w * w; } + + /** + * @en Return the information of the quaternion in string. + * @zh 返回当前四元数的字符串表示。 + */ + public toString (): string { + return `(${this.x}, ${this.y}, ${this.z}, ${this.w})`; + } } const qt_1 = new Quat(); diff --git a/cocos/core/math/rect.ts b/cocos/core/math/rect.ts index 735135a769..a9342cd4f5 100644 --- a/cocos/core/math/rect.ts +++ b/cocos/core/math/rect.ts @@ -387,7 +387,7 @@ export class Rect extends ValueType { */ public toString (): string { const self = this; - return `(${self.x.toFixed(2)}, ${self.y.toFixed(2)}, ${self.width.toFixed(2)}, ${self.height.toFixed(2)})`; + return `(${self.x}, ${self.y}, ${self.width}, ${self.height})`; } /** diff --git a/cocos/core/math/size.ts b/cocos/core/math/size.ts index 3b66006865..eedd4eabed 100644 --- a/cocos/core/math/size.ts +++ b/cocos/core/math/size.ts @@ -165,7 +165,7 @@ export class Size extends ValueType { * @returns The information of the current size in string */ public toString (): string { - return `(${this.width.toFixed(2)}, ${this.height.toFixed(2)})`; + return `(${this.width}, ${this.height})`; } } diff --git a/cocos/core/math/vec2.ts b/cocos/core/math/vec2.ts index b36576e48c..ef6a30400d 100644 --- a/cocos/core/math/vec2.ts +++ b/cocos/core/math/vec2.ts @@ -561,7 +561,7 @@ export class Vec2 extends ValueType { * @returns The string with vector information */ public toString (): string { - return `(${this.x.toFixed(2)}, ${this.y.toFixed(2)})`; + return `(${this.x}, ${this.y})`; } /** diff --git a/cocos/core/math/vec3.ts b/cocos/core/math/vec3.ts index 5767065eec..a3f8d7084f 100644 --- a/cocos/core/math/vec3.ts +++ b/cocos/core/math/vec3.ts @@ -1007,7 +1007,7 @@ export class Vec3 extends ValueType { * @returns The string with vector information */ public toString (): string { - return `(${this.x.toFixed(2)}, ${this.y.toFixed(2)}, ${this.z.toFixed(2)})`; + return `(${this.x}, ${this.y}, ${this.z})`; } /** diff --git a/cocos/core/math/vec4.ts b/cocos/core/math/vec4.ts index bb1fffdde3..eac0c17a95 100644 --- a/cocos/core/math/vec4.ts +++ b/cocos/core/math/vec4.ts @@ -715,7 +715,7 @@ export class Vec4 extends ValueType { * @returns The string with vector information */ public toString (): string { - return `(${this.x.toFixed(2)}, ${this.y.toFixed(2)}, ${this.z.toFixed(2)}, ${this.w.toFixed(2)})`; + return `(${this.x}, ${this.y}, ${this.z}, ${this.w})`; } /** diff --git a/tests/core/math/mat3.test.ts b/tests/core/math/mat3.test.ts index a2bf16c0e5..e5e302519d 100644 --- a/tests/core/math/mat3.test.ts +++ b/tests/core/math/mat3.test.ts @@ -138,4 +138,20 @@ describe('Test Mat3', () => { log('toEuler: ', v0); expect(Vec3.equals(v0, expect0)).toBe(true); }); + + test('toString', () => { + const m = new Mat3( +12.1234, 0, 44.123433, +55.12234, 6.1230835, 12384.126, +7348.12237, 1283.8, 0.000009 +); + const expect0 = `[ +12.1234, 0, 44.123433, +55.12234, 6.1230835, 12384.126, +7348.12237, 1283.8, 0.000009 +]`; + const s = m.toString(); + log('toString: ', s); + expect(s).toBe(expect0); + }); }); \ No newline at end of file diff --git a/tests/core/math/mat4.test.ts b/tests/core/math/mat4.test.ts index 9740ca02dc..dff276fd0d 100644 --- a/tests/core/math/mat4.test.ts +++ b/tests/core/math/mat4.test.ts @@ -631,4 +631,20 @@ describe('Test Mat4', () => { const r = Mat4.multiplyScalarAndAdd(new Mat4(), b, a, 2); expect(Mat4.equals(r, res)).toBe(true); }); + + test('toString', () => { + const m = new Mat4( +12.1234, 0.002, 44.123433, 55.12234, +6.1230835, 12384.126, 7348.12237, 283.23848943, +1283.8, 0.000009, 0.012384294, 0.18492384, +1284734.123, 0.12841203, 0, 0.00001283 +); + const expect0 = `[ +12.1234, 0.002, 44.123433, 55.12234, +6.1230835, 12384.126, 7348.12237, 283.23848943, +1283.8, 0.000009, 0.012384294, 0.18492384, +1284734.123, 0.12841203, 0, 0.00001283 +]`; + expect(m.toString()).toBe(expect0); + }); }); \ No newline at end of file diff --git a/tests/core/math/rect.test.ts b/tests/core/math/rect.test.ts index 95c8260ee8..db6c785601 100644 --- a/tests/core/math/rect.test.ts +++ b/tests/core/math/rect.test.ts @@ -78,7 +78,7 @@ describe('Test Rect', () => { }); test('toString', () => { const rect1 = new Rect(0, 0, 100, 100); - const res = '(0.00, 0.00, 100.00, 100.00)'; + const res = '(0, 0, 100, 100)'; expect(rect1.toString()).toBe(res); }); diff --git a/tests/core/math/size.test.ts b/tests/core/math/size.test.ts index 341eaa31f9..934b7f9d33 100644 --- a/tests/core/math/size.test.ts +++ b/tests/core/math/size.test.ts @@ -34,7 +34,7 @@ describe('Test Size', () => { test('toString', () => { const size = new Size(100, 100); - expect(size.toString()).toBe('(100.00, 100.00)'); + expect(size.toString()).toBe('(100, 100)'); }); }); \ No newline at end of file diff --git a/tests/core/math/vec2.test.ts b/tests/core/math/vec2.test.ts index bfaeb687d4..1ac80219ef 100644 --- a/tests/core/math/vec2.test.ts +++ b/tests/core/math/vec2.test.ts @@ -118,6 +118,11 @@ describe('Test Vec2', () => { expect(vec3.y).toBe(2); expect(vec3.z).toBe(0); }); + + test('toString', () => { + const v = new Vec2(100, 100); + expect(v.toString()).toBe('(100, 100)'); + }); }); function polar(angle: number, length: number) { diff --git a/tests/core/math/vec3.test.ts b/tests/core/math/vec3.test.ts index 63b0d2881f..17c0859221 100644 --- a/tests/core/math/vec3.test.ts +++ b/tests/core/math/vec3.test.ts @@ -343,4 +343,9 @@ describe('Test Vec3', () => { expect(vec2.x).toBe(1); expect(vec2.y).toBe(2); }); + + test('toString', () => { + const v = new Vec3(100, 100, 1283.3892389); + expect(v.toString()).toBe('(100, 100, 1283.3892389)'); + }); }); \ No newline at end of file diff --git a/tests/core/math/vec4.test.ts b/tests/core/math/vec4.test.ts index 3880180693..b587a81c84 100644 --- a/tests/core/math/vec4.test.ts +++ b/tests/core/math/vec4.test.ts @@ -474,4 +474,9 @@ describe('Test Vec4', () => { expect(Vec4.equals(v4_0, v4_1)).toBeTruthy(); expect(Vec4.equals(v4_0, v4_2)).toBeFalsy(); }); + + test('toString', () => { + const v = new Vec4(100, 100, 1283.3892389, 0); + expect(v.toString()).toBe('(100, 100, 1283.3892389, 0)'); + }); }); \ No newline at end of file