From cde48d1750c7f7579fa40d506f963ea6ed8f9130 Mon Sep 17 00:00:00 2001 From: minggo Date: Wed, 17 Apr 2024 14:00:59 +0800 Subject: [PATCH] add Vec2.toVec3() and Vec3.toVec2() (#16906) * add Vec2.toVec3() and Vec3.toVec2() --- cocos/core/math/vec2.ts | 10 ++++++++++ cocos/core/math/vec3.ts | 12 ++++++++++++ tests/core/math/vec2.test.ts | 15 +++++++++++++++ tests/core/math/vec3.test.ts | 13 +++++++++++++ 4 files changed, 50 insertions(+) diff --git a/cocos/core/math/vec2.ts b/cocos/core/math/vec2.ts index a05053c5d2e..0ca486914a8 100644 --- a/cocos/core/math/vec2.ts +++ b/cocos/core/math/vec2.ts @@ -834,6 +834,16 @@ export class Vec2 extends ValueType { this.y = matrix.m01 * x + matrix.m05 * y + matrix.m13; return this; } + + /** + * @en Converts the current Vec2 object to a Vec3 object by adding a z-component of 0. + * @zh 将当前的Vec2对象转换为一个z分量为0的Vec3对象。 + * @returns Vec3 @en A new Vec3 object created from the current Vec2 object with z-component set to 0. + * @zh 从当前的Vec2对象创建一个新的Vec3对象,其中z分量设置为0。 + */ + public toVec3 (): Vec3 { + return new Vec3(this.x, this.y, 0); + } } CCClass.fastDefine('cc.Vec2', Vec2, { x: 0, y: 0 }); diff --git a/cocos/core/math/vec3.ts b/cocos/core/math/vec3.ts index 8a2cb04b91e..0a9f3b32c79 100644 --- a/cocos/core/math/vec3.ts +++ b/cocos/core/math/vec3.ts @@ -23,6 +23,7 @@ THE SOFTWARE. */ +import type { Vec2 } from './vec2'; import { CCClass } from '../data/class'; import { ValueType } from '../value-types/value-type'; import { Mat4 } from './mat4'; @@ -1237,6 +1238,17 @@ export class Vec3 extends ValueType { this.z = (matrix.m02 * x + matrix.m06 * y + matrix.m10 * z + matrix.m14) * rhw; return this; } + + /** + * @en Converts the current Vec3 object to a Vec2 object by adding a z-component of 0. + * @zh 将当前的Vec3对象转换为一个z分量为0的Vec3对象。 + * @returns Vec2 @en A new Vec2 object created from the current Vec3 object with z-component ignored. + * @zh 从当前的Vec3对象创建一个新的Vec2对象,丢弃z分量。 + */ + public toVec2 (): Vec2 { + // eslint-disable-next-line + return new legacyCC.Vec2(this.x, this.y); + } } CCClass.fastDefine('cc.Vec3', Vec3, { x: 0, y: 0, z: 0 }); diff --git a/tests/core/math/vec2.test.ts b/tests/core/math/vec2.test.ts index 68cab7a03c2..bfaeb687d43 100644 --- a/tests/core/math/vec2.test.ts +++ b/tests/core/math/vec2.test.ts @@ -103,6 +103,21 @@ describe('Test Vec2', () => { t(v1, v2(v1Angle + 280.0001), 280.0001 - 360); } }); + + test(`toVec3`, () => { + const vec2 = new Vec2(1, 2); + const vec3 = vec2.toVec3(); + expect(vec3.x).toBe(1); + expect(vec3.y).toBe(2); + expect(vec3.z).toBe(0); + + // vec2 will not affect vec3. + vec2.x = 2; + vec2.y = 3; + expect(vec3.x).toBe(1); + expect(vec3.y).toBe(2); + expect(vec3.z).toBe(0); + }); }); function polar(angle: number, length: number) { diff --git a/tests/core/math/vec3.test.ts b/tests/core/math/vec3.test.ts index fe0674eb07d..63b0d2881fd 100644 --- a/tests/core/math/vec3.test.ts +++ b/tests/core/math/vec3.test.ts @@ -330,4 +330,17 @@ describe('Test Vec3', () => { return result; } }); + + test(`toVec2`, () => { + const vec3 = new Vec3(1, 2, 3); + const vec2 = vec3.toVec2(); + expect(vec2.x).toBe(1); + expect(vec2.y).toBe(2); + + // vec3 will not affect vec2. + vec3.x = 2; + vec3.y = 3; + expect(vec2.x).toBe(1); + expect(vec2.y).toBe(2); + }); }); \ No newline at end of file