From 6be79f8118ea977bc9b3dbc25581595e8f550a56 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Thu, 6 Jan 2022 17:43:50 +0800 Subject: [PATCH 01/21] refactor: delete deprecate code --- packages/core/src/Entity.ts | 56 ++---------------------- packages/core/src/trail/TrailRenderer.ts | 4 +- 2 files changed, 6 insertions(+), 54 deletions(-) diff --git a/packages/core/src/Entity.ts b/packages/core/src/Entity.ts index e43315d68d..f71bd31272 100644 --- a/packages/core/src/Entity.ts +++ b/packages/core/src/Entity.ts @@ -1,15 +1,15 @@ -import { Matrix, Quaternion, Vector3 } from "@oasis-engine/math"; +import { Matrix } from "@oasis-engine/math"; import { EngineObject } from "./base"; import { ComponentCloner } from "./clone/ComponentCloner"; import { Component } from "./Component"; -import { Script } from "./Script"; import { ComponentsDependencies } from "./ComponentsDependencies"; +import { DisorderedArray } from "./DisorderedArray"; import { Engine } from "./Engine"; import { Layer } from "./Layer"; import { Scene } from "./Scene"; +import { Script } from "./Script"; import { Transform } from "./Transform"; import { UpdateFlag } from "./UpdateFlag"; -import { DisorderedArray } from "./DisorderedArray"; /** * Entity, be used as components container. @@ -341,7 +341,7 @@ export class Entity extends EngineObject { */ destroy(): void { if (this._destroyed) return; - + super.destroy(); const abilityArray = this._components; for (let i = abilityArray.length - 1; i >= 0; i--) { @@ -486,54 +486,6 @@ export class Entity extends EngineObject { private _invModelMatrix: Matrix = new Matrix(); private _inverseWorldMatFlag: UpdateFlag; - /** - * @deprecated - * Use transform.position instead. - */ - get position(): Vector3 { - return this.transform.position; - } - - set position(val: Vector3) { - this.transform.position = val; - } - - /** - * @deprecated - * Use transform.worldPosition instead. - */ - get worldPosition(): Vector3 { - return this.transform.worldPosition; - } - - set worldPosition(val: Vector3) { - this.transform.worldPosition = val; - } - - /** - * @deprecated - * Use transform.rotationQuaternion instead. - */ - get rotation(): Quaternion { - return this.transform.rotationQuaternion; - } - - set rotation(val: Quaternion) { - this.transform.rotationQuaternion = val; - } - - /** - * @deprecated - * Use transform.scale instead. - */ - get scale(): Vector3 { - return this.transform.scale; - } - - set scale(val: Vector3) { - this.transform.scale = val; - } - /** * @deprecated */ diff --git a/packages/core/src/trail/TrailRenderer.ts b/packages/core/src/trail/TrailRenderer.ts index f88a21a480..2f68868748 100644 --- a/packages/core/src/trail/TrailRenderer.ts +++ b/packages/core/src/trail/TrailRenderer.ts @@ -86,7 +86,7 @@ export class TrailRenderer extends MeshRenderer { appendNewPoint = false; } else if (this._curPointNum > 0) { const lastPoint = this._points[this._points.length - 1]; - if (Vector3.distance(this.entity.worldPosition, lastPoint) < this._minSeg) { + if (Vector3.distance(this.entity.transform.worldPosition, lastPoint) < this._minSeg) { appendNewPoint = false; } else { // debugger @@ -95,7 +95,7 @@ export class TrailRenderer extends MeshRenderer { if (appendNewPoint) { this._pointStates[this._curPointNum] = this._lifetime; - this.entity.worldPosition.cloneTo(this._points[this._curPointNum]); + this.entity.transform.worldPosition.cloneTo(this._points[this._curPointNum]); this._curPointNum++; } From 9f389475a112f4e5207b6582c95a20822bf1ab3d Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Mon, 17 Jan 2022 18:50:39 +0800 Subject: [PATCH 02/21] refactor: opt add new transform design --- packages/controls/src/FreeControl.ts | 2 +- packages/controls/src/OrbitControl.ts | 2 +- packages/core/src/Transform.ts | 21 +++++++-- packages/math/src/Vector3.ts | 63 +++++++++++++++++++++------ 4 files changed, 69 insertions(+), 19 deletions(-) diff --git a/packages/controls/src/FreeControl.ts b/packages/controls/src/FreeControl.ts index 52d42353e3..53cab157cd 100644 --- a/packages/controls/src/FreeControl.ts +++ b/packages/controls/src/FreeControl.ts @@ -281,7 +281,7 @@ export class FreeControl extends Script { */ updateSpherical(): void { this._v3Cache.setValue(0, 0, -1); - Vector3.transformByQuat(this._v3Cache, this.camera.rotation, this._v3Cache); + Vector3.transformByQuat(this._v3Cache, this.camera.transform.rotationQuaternion, this._v3Cache); this._spherical.setFromVec3(this._v3Cache); this._theta = this._spherical.theta; this._phi = this._spherical.phi; diff --git a/packages/controls/src/OrbitControl.ts b/packages/controls/src/OrbitControl.ts index e68e0fe2aa..b3ede278c4 100644 --- a/packages/controls/src/OrbitControl.ts +++ b/packages/controls/src/OrbitControl.ts @@ -400,7 +400,7 @@ export class OrbitControl extends Script { */ pan(deltaX: number, deltaY: number) { // perspective only - const position: Vector3 = this.camera.position; + const position: Vector3 = this.camera.transform.position; position.cloneTo(this._vPan); this._vPan.subtract(this.target); let targetDistance = this._vPan.length(); diff --git a/packages/core/src/Transform.ts b/packages/core/src/Transform.ts index 757a4fa0dc..1ad3dde8d9 100644 --- a/packages/core/src/Transform.ts +++ b/packages/core/src/Transform.ts @@ -1,6 +1,7 @@ import { MathUtil, Matrix, Matrix3x3, Quaternion, Vector3 } from "@oasis-engine/math"; import { deepClone, ignoreClone } from "./clone/CloneManager"; import { Component } from "./Component"; +import { Entity } from "./Entity"; import { UpdateFlag } from "./UpdateFlag"; import { UpdateFlagManager } from "./UpdateFlagManager"; @@ -59,8 +60,6 @@ export class Transform extends Component { if (this._position !== value) { value.cloneTo(this._position); } - this._setDirtyFlagTrue(TransformFlag.LocalMatrix); - this._updateWorldPositionFlag(); } /** @@ -216,8 +215,6 @@ export class Transform extends Component { if (this._scale !== value) { value.cloneTo(this._scale); } - this._setDirtyFlagTrue(TransformFlag.LocalMatrix); - this._updateWorldScaleFlag(); } /** @@ -293,6 +290,22 @@ export class Transform extends Component { this._setDirtyFlagFalse(TransformFlag.WorldMatrix); } + constructor(entity: Entity) { + super(entity); + + //@ts-ignore + this._position._onValueChanged = () => { + this._setDirtyFlagTrue(TransformFlag.LocalMatrix); + this._updateWorldPositionFlag(); + }; + + //@ts-ignore + this._scale._onValueChanged = () => { + this._setDirtyFlagTrue(TransformFlag.LocalMatrix); + this._updateWorldScaleFlag(); + }; + } + /** * Set local position by X, Y, Z value. * @param x - X coordinate diff --git a/packages/math/src/Vector3.ts b/packages/math/src/Vector3.ts index b238f9be8b..6fcc99e6bb 100644 --- a/packages/math/src/Vector3.ts +++ b/packages/math/src/Vector3.ts @@ -301,12 +301,48 @@ export class Vector3 implements IClone { out.z = iz * qw - iw * qz - ix * qy + iy * qx; } - /** The x component of the vector.*/ - x: number; - /** The y component of the vector.*/ - y: number; - /** The z component of the vector.*/ - z: number; + private _x: number; + private _y: number; + private _z: number; + + /** @internal */ + _onValueChanged: () => void; + + /** + * The x component of the vector. + */ + public get x() { + return this._x; + } + + public set x(value: number) { + this._x = value; + this._onValueChanged && this._onValueChanged(); + } + + /** + * The y component of the vector. + */ + public get y() { + return this._y; + } + + public set y(value: number) { + this._y = value; + this._onValueChanged && this._onValueChanged(); + } + + /** + * The z component of the vector. + */ + public get z() { + return this._z; + } + + public set z(value: number) { + this._z = value; + this._onValueChanged && this._onValueChanged(); + } /** * Constructor of Vector3. @@ -315,9 +351,9 @@ export class Vector3 implements IClone { * @param z - The z component of the vector, default 0 */ constructor(x: number = 0, y: number = 0, z: number = 0) { - this.x = x; - this.y = y; - this.z = z; + this._x = x; + this._y = y; + this._z = z; } /** @@ -461,7 +497,7 @@ export class Vector3 implements IClone { * @returns A clone of this vector */ clone(): Vector3 { - return new Vector3(this.x, this.y, this.z); + return new Vector3(this._x, this._y, this._z); } /** @@ -470,9 +506,10 @@ export class Vector3 implements IClone { * @returns The specified vector */ cloneTo(out: Vector3): Vector3 { - out.x = this.x; - out.y = this.y; - out.z = this.z; + out._x = this.x; + out._y = this.y; + out._z = this.z; + this._onValueChanged && this._onValueChanged(); return out; } From bf357d077609e6d38e8aaf6f4f8350d24c81d349 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Mon, 17 Jan 2022 19:32:13 +0800 Subject: [PATCH 03/21] refactor: opt code --- packages/math/src/Vector3.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/math/src/Vector3.ts b/packages/math/src/Vector3.ts index 6fcc99e6bb..a8d93dca73 100644 --- a/packages/math/src/Vector3.ts +++ b/packages/math/src/Vector3.ts @@ -506,10 +506,10 @@ export class Vector3 implements IClone { * @returns The specified vector */ cloneTo(out: Vector3): Vector3 { - out._x = this.x; - out._y = this.y; - out._z = this.z; - this._onValueChanged && this._onValueChanged(); + out._x = this._x; + out._y = this._y; + out._z = this._z; + out._onValueChanged && out._onValueChanged(); return out; } From baa05c2252ab32ae6bc7f3d672c68c0050baf7ca Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Tue, 18 Jan 2022 12:03:48 +0800 Subject: [PATCH 04/21] refactor: opt code --- packages/core/src/Transform.ts | 1 - packages/math/src/Vector3.ts | 10 ++++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/core/src/Transform.ts b/packages/core/src/Transform.ts index 1ad3dde8d9..34f0c37b69 100644 --- a/packages/core/src/Transform.ts +++ b/packages/core/src/Transform.ts @@ -14,7 +14,6 @@ export class Transform extends Component { private static _tempMat30: Matrix3x3 = new Matrix3x3(); private static _tempMat31: Matrix3x3 = new Matrix3x3(); private static _tempMat32: Matrix3x3 = new Matrix3x3(); - private static _tempMat40: Matrix = new Matrix(); private static _tempMat41: Matrix = new Matrix(); private static _tempMat42: Matrix = new Matrix(); private static _tempMat43: Matrix = new Matrix(); diff --git a/packages/math/src/Vector3.ts b/packages/math/src/Vector3.ts index a8d93dca73..958c797aa4 100644 --- a/packages/math/src/Vector3.ts +++ b/packages/math/src/Vector3.ts @@ -20,9 +20,10 @@ export class Vector3 implements IClone { * @param out - The sum of two vectors */ static add(left: Vector3, right: Vector3, out: Vector3): void { - out.x = left.x + right.x; - out.y = left.y + right.y; - out.z = left.z + right.z; + out._x = left._x + right._x; + out._y = left._y + right._y; + out._z = left._z + right._z; + out._onValueChanged && out._onValueChanged(); } /** @@ -306,7 +307,8 @@ export class Vector3 implements IClone { private _z: number; /** @internal */ - _onValueChanged: () => void; + _onValueChanged: () => void = null; + _dirty: boolean = false; /** * The x component of the vector. From 12dac012ba81d8da2f5efd4713e029cf0decc04c Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Tue, 18 Jan 2022 15:01:45 +0800 Subject: [PATCH 05/21] refactor: oopt code --- packages/controls/src/Spherical.ts | 9 +-- .../src/RenderPipeline/BasicRenderPipeline.ts | 2 +- packages/math/src/BoundingFrustum.ts | 26 +++------ packages/math/src/CollisionUtil.ts | 4 +- packages/math/src/Matrix.ts | 6 +- packages/math/src/Plane.ts | 7 +-- packages/math/src/Vector3.ts | 56 ++++++++++--------- 7 files changed, 46 insertions(+), 64 deletions(-) diff --git a/packages/controls/src/Spherical.ts b/packages/controls/src/Spherical.ts index e1efbd3065..1d01aed262 100644 --- a/packages/controls/src/Spherical.ts +++ b/packages/controls/src/Spherical.ts @@ -43,10 +43,11 @@ export class Spherical { setToVec3(v3: Vector3) { const sinPhiRadius = Math.sin(this.phi) * this.radius; - - v3.x = sinPhiRadius * Math.sin(this.theta); - v3.y = Math.cos(this.phi) * this.radius; - v3.z = sinPhiRadius * Math.cos(this.theta); + v3.setValue( + sinPhiRadius * Math.sin(this.theta), + Math.cos(this.phi) * this.radius, + sinPhiRadius * Math.cos(this.theta) + ); return this; } diff --git a/packages/core/src/RenderPipeline/BasicRenderPipeline.ts b/packages/core/src/RenderPipeline/BasicRenderPipeline.ts index 327ec60758..6d651d3b8c 100644 --- a/packages/core/src/RenderPipeline/BasicRenderPipeline.ts +++ b/packages/core/src/RenderPipeline/BasicRenderPipeline.ts @@ -178,7 +178,7 @@ export class BasicRenderPipeline { if (pass.renderOverride) { pass.render(camera, this._opaqueQueue, this._alphaTestQueue, this._transparentQueue); } else { - this._opaqueQueue.render(camera, pass.replaceMaterial, pass.mask); + // this._opaqueQueue.render(camera, pass.replaceMaterial, pass.mask); this._alphaTestQueue.render(camera, pass.replaceMaterial, pass.mask); if (background.mode === BackgroundMode.Sky) { this._drawSky(engine, camera, background.sky); diff --git a/packages/math/src/BoundingFrustum.ts b/packages/math/src/BoundingFrustum.ts index c1daa57c7b..a9e3392697 100644 --- a/packages/math/src/BoundingFrustum.ts +++ b/packages/math/src/BoundingFrustum.ts @@ -1,8 +1,8 @@ -import { IClone } from "./IClone"; import { BoundingBox } from "./BoundingBox"; import { BoundingSphere } from "./BoundingSphere"; import { CollisionUtil } from "./CollisionUtil"; import { ContainmentType } from "./enums/ContainmentType"; +import { IClone } from "./IClone"; import { Matrix } from "./Matrix"; import { Plane } from "./Plane"; @@ -118,50 +118,38 @@ export class BoundingFrustum implements IClone { // near const nearNormal = this.near.normal; - nearNormal.x = -m14 - m13; - nearNormal.y = -m24 - m23; - nearNormal.z = -m34 - m33; + nearNormal.setValue(-m14 - m13, -m24 - m23, -m34 - m33); this.near.distance = -m44 - m43; this.near.normalize(); // far const farNormal = this.far.normal; - farNormal.x = m13 - m14; - farNormal.y = m23 - m24; - farNormal.z = m33 - m34; + farNormal.setValue(m13 - m14, m23 - m24, m33 - m34); this.far.distance = m43 - m44; this.far.normalize(); // left const leftNormal = this.left.normal; - leftNormal.x = -m14 - m11; - leftNormal.y = -m24 - m21; - leftNormal.z = -m34 - m31; + leftNormal.setValue(-m14 - m11, -m24 - m21, -m34 - m31); this.left.distance = -m44 - m41; this.left.normalize(); // right const rightNormal = this.right.normal; - rightNormal.x = m11 - m14; - rightNormal.y = m21 - m24; - rightNormal.z = m31 - m34; + rightNormal.setValue(m11 - m14, m21 - m24, m31 - m34); this.right.distance = m41 - m44; this.right.normalize(); // top const topNormal = this.top.normal; - topNormal.x = m12 - m14; - topNormal.y = m22 - m24; - topNormal.z = m32 - m34; + topNormal.setValue(m12 - m14, m22 - m24, m32 - m34); this.top.distance = m42 - m44; this.top.normalize(); // bottom const bottomNormal = this.bottom.normal; - bottomNormal.x = -m14 - m12; - bottomNormal.y = -m24 - m22; - bottomNormal.z = -m34 - m32; + bottomNormal.setValue(-m14 - m12, -m24 - m22, -m34 - m32); this.bottom.distance = -m44 - m42; this.bottom.normalize(); } diff --git a/packages/math/src/CollisionUtil.ts b/packages/math/src/CollisionUtil.ts index e02eb00df9..31bd7662f5 100644 --- a/packages/math/src/CollisionUtil.ts +++ b/packages/math/src/CollisionUtil.ts @@ -323,9 +323,7 @@ export class CollisionUtil { const plane = frustum.getPlane(i); const normal = plane.normal; - back.x = normal.x >= 0 ? min.x : max.x; - back.y = normal.y >= 0 ? min.y : max.y; - back.z = normal.z >= 0 ? min.z : max.z; + back.setValue(normal.x >= 0 ? min.x : max.x, normal.y >= 0 ? min.y : max.y, normal.z >= 0 ? min.z : max.z); if (Vector3.dot(plane.normal, back) > -plane.distance) { return false; } diff --git a/packages/math/src/Matrix.ts b/packages/math/src/Matrix.ts index 0ae87e3559..cc5b93ba2b 100644 --- a/packages/math/src/Matrix.ts +++ b/packages/math/src/Matrix.ts @@ -1177,11 +1177,7 @@ export class Matrix implements IClone { */ getTranslation(out: Vector3): Vector3 { const e = this.elements; - - out.x = e[12]; - out.y = e[13]; - out.z = e[14]; - + out.setValue(e[12], e[13], e[14]); return out; } diff --git a/packages/math/src/Plane.ts b/packages/math/src/Plane.ts index eaff0163b3..4657a61911 100644 --- a/packages/math/src/Plane.ts +++ b/packages/math/src/Plane.ts @@ -12,12 +12,9 @@ export class Plane implements IClone { */ static normalize(p: Plane, out: Plane): void { const { normal } = p; - const factor = 1.0 / normal.length(); - const outNormal = out.normal; - outNormal.x = normal.x * factor; - outNormal.y = normal.y * factor; - outNormal.z = normal.z * factor; + const factor = 1.0 / normal.length(); + Vector3.scale(normal, factor, out.normal); out.distance = p.distance * factor; } diff --git a/packages/math/src/Vector3.ts b/packages/math/src/Vector3.ts index 958c797aa4..64689e9237 100644 --- a/packages/math/src/Vector3.ts +++ b/packages/math/src/Vector3.ts @@ -33,9 +33,10 @@ export class Vector3 implements IClone { * @param out - The difference between two vectors */ static subtract(left: Vector3, right: Vector3, out: Vector3): void { - out.x = left.x - right.x; - out.y = left.y - right.y; - out.z = left.z - right.z; + out._x = left._x - right._x; + out._y = left._y - right._y; + out._z = left._z - right._z; + out._onValueChanged && out._onValueChanged(); } /** @@ -86,9 +87,7 @@ export class Vector3 implements IClone { const by = right.y; const bz = right.z; - out.x = ay * bz - az * by; - out.y = az * bx - ax * bz; - out.z = ax * by - ay * bx; + out.setValue(ay * bz - az * by, az * bx - ax * bz, ax * by - ay * bx); } /** @@ -135,10 +134,11 @@ export class Vector3 implements IClone { * @param out - The result of linear blending between two vectors */ static lerp(start: Vector3, end: Vector3, t: number, out: Vector3): void { - const { x, y, z } = start; - out.x = x + (end.x - x) * t; - out.y = y + (end.y - y) * t; - out.z = z + (end.z - z) * t; + const { _x, _y, _z } = start; + out._x = _x + (end._x - _x) * t; + out._y = _y + (end._y - _y) * t; + out._z = _z + (end._z - _z) * t; + out._onValueChanged && out._onValueChanged(); } /** @@ -182,14 +182,12 @@ export class Vector3 implements IClone { * @param out - The normalized vector */ static normalize(a: Vector3, out: Vector3): void { - const { x, y, z } = a; - let len: number = Math.sqrt(x * x + y * y + z * z); + const { _x, _y, _z } = a; + let len: number = Math.sqrt(_x * _x + _y * _y + _z * _z); if (len > 0) { // TODO len = 1 / len; - out.x = x * len; - out.y = y * len; - out.z = z * len; + out.setValue(_x * len, _y * len, _z * len); } } @@ -200,9 +198,10 @@ export class Vector3 implements IClone { * @param out - The scaled vector */ static scale(a: Vector3, s: number, out: Vector3): void { - out.x = a.x * s; - out.y = a.y * s; - out.z = a.z * s; + out._x = a._x * s; + out._y = a._y * s; + out._z = a._z * s; + out._onValueChanged && out._onValueChanged(); } /** @@ -366,9 +365,10 @@ export class Vector3 implements IClone { * @returns This vector */ setValue(x: number, y: number, z: number): Vector3 { - this.x = x; - this.y = y; - this.z = z; + this._x = x; + this._y = y; + this._z = z; + this._onValueChanged && this._onValueChanged(); return this; } @@ -391,9 +391,10 @@ export class Vector3 implements IClone { * @returns This vector */ add(right: Vector3): Vector3 { - this.x += right.x; - this.y += right.y; - this.z += right.z; + this._x += right._x; + this._y += right._y; + this._z += right._z; + this._onValueChanged && this._onValueChanged(); return this; } @@ -403,9 +404,10 @@ export class Vector3 implements IClone { * @returns This vector */ subtract(right: Vector3): Vector3 { - this.x -= right.x; - this.y -= right.y; - this.z -= right.z; + this._x -= right._x; + this._y -= right._y; + this._z -= right._z; + this._onValueChanged && this._onValueChanged(); return this; } From a81e9d6280bf7e9b375b28e542e369316ef4c69f Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Mon, 24 Jan 2022 15:36:29 +0800 Subject: [PATCH 06/21] refactor: opt code --- .../src/RenderPipeline/BasicRenderPipeline.ts | 2 +- packages/core/src/Transform.ts | 88 ++++++++++++------- packages/core/src/animation/Animator.ts | 3 - packages/math/src/Quaternion.ts | 71 ++++++++++++--- packages/math/src/Vector3.ts | 69 ++++++++------- 5 files changed, 153 insertions(+), 80 deletions(-) diff --git a/packages/core/src/RenderPipeline/BasicRenderPipeline.ts b/packages/core/src/RenderPipeline/BasicRenderPipeline.ts index 6d651d3b8c..327ec60758 100644 --- a/packages/core/src/RenderPipeline/BasicRenderPipeline.ts +++ b/packages/core/src/RenderPipeline/BasicRenderPipeline.ts @@ -178,7 +178,7 @@ export class BasicRenderPipeline { if (pass.renderOverride) { pass.render(camera, this._opaqueQueue, this._alphaTestQueue, this._transparentQueue); } else { - // this._opaqueQueue.render(camera, pass.replaceMaterial, pass.mask); + this._opaqueQueue.render(camera, pass.replaceMaterial, pass.mask); this._alphaTestQueue.render(camera, pass.replaceMaterial, pass.mask); if (background.mode === BackgroundMode.Sky) { this._drawSky(engine, camera, background.sky); diff --git a/packages/core/src/Transform.ts b/packages/core/src/Transform.ts index 34f0c37b69..b7d3d46572 100644 --- a/packages/core/src/Transform.ts +++ b/packages/core/src/Transform.ts @@ -81,15 +81,6 @@ export class Transform extends Component { if (this._worldPosition !== value) { value.cloneTo(this._worldPosition); } - const parent = this._getParentTransform(); - if (parent) { - Matrix.invert(parent.worldMatrix, Transform._tempMat41); - Vector3.transformCoordinate(value, Transform._tempMat41, this._position); - } else { - value.cloneTo(this._position); - } - this.position = this._position; - this._setDirtyFlagFalse(TransformFlag.WorldPosition); } /** @@ -101,7 +92,6 @@ export class Transform extends Component { if (this._isContainDirtyFlag(TransformFlag.LocalEuler)) { this._rotationQuaternion.toEuler(this._rotation); this._rotation.scale(MathUtil.radToDegreeFactor); // radians to degrees - this._setDirtyFlagFalse(TransformFlag.LocalEuler); } return this._rotation; @@ -111,9 +101,6 @@ export class Transform extends Component { if (this._rotation !== value) { value.cloneTo(this._rotation); } - this._setDirtyFlagTrue(TransformFlag.LocalMatrix | TransformFlag.LocalQuat); - this._setDirtyFlagFalse(TransformFlag.LocalEuler); - this._updateWorldRotationFlag(); } /** @@ -134,14 +121,6 @@ export class Transform extends Component { if (this._worldRotation !== value) { value.cloneTo(this._worldRotation); } - Quaternion.rotationEuler( - MathUtil.degreeToRadian(value.x), - MathUtil.degreeToRadian(value.y), - MathUtil.degreeToRadian(value.z), - this._worldRotationQuaternion - ); - this.worldRotationQuaternion = this._worldRotationQuaternion; - this._setDirtyFlagFalse(TransformFlag.WorldEuler); } /** @@ -165,9 +144,6 @@ export class Transform extends Component { if (this._rotationQuaternion !== value) { value.cloneTo(this._rotationQuaternion); } - this._setDirtyFlagTrue(TransformFlag.LocalMatrix | TransformFlag.LocalEuler); - this._setDirtyFlagFalse(TransformFlag.LocalQuat); - this._updateWorldRotationFlag(); } /** @@ -191,15 +167,6 @@ export class Transform extends Component { if (this._worldRotationQuaternion !== value) { value.cloneTo(this._worldRotationQuaternion); } - const parent = this._getParentTransform(); - if (parent) { - Quaternion.invert(parent.worldRotationQuaternion, Transform._tempQuat0); - Quaternion.multiply(value, Transform._tempQuat0, this._rotationQuaternion); - } else { - value.cloneTo(this._rotationQuaternion); - } - this.rotationQuaternion = this._rotationQuaternion; - this._setDirtyFlagFalse(TransformFlag.WorldQuat); } /** @@ -298,6 +265,61 @@ export class Transform extends Component { this._updateWorldPositionFlag(); }; + //@ts-ignore + this._worldPosition._onValueChanged = () => { + const worldPosition = this._worldPosition; + const parent = this._getParentTransform(); + if (parent) { + Matrix.invert(parent.worldMatrix, Transform._tempMat41); + Vector3.transformCoordinate(worldPosition, Transform._tempMat41, this._position); + } else { + worldPosition.cloneTo(this._position); + } + this.position = this._position; + this._setDirtyFlagFalse(TransformFlag.WorldPosition); + }; + + //@ts-ignore + this._rotation._onValueChanged = () => { + this._setDirtyFlagTrue(TransformFlag.LocalMatrix | TransformFlag.LocalQuat); + this._setDirtyFlagFalse(TransformFlag.LocalEuler); + this._updateWorldRotationFlag(); + }; + + //@ts-ignore + this._worldRotation._onValueChanged = () => { + const worldRotation = this._worldRotation; + Quaternion.rotationEuler( + MathUtil.degreeToRadian(worldRotation.x), + MathUtil.degreeToRadian(worldRotation.y), + MathUtil.degreeToRadian(worldRotation.z), + this._worldRotationQuaternion + ); + this.worldRotationQuaternion = this._worldRotationQuaternion; + this._setDirtyFlagFalse(TransformFlag.WorldEuler); + }; + + //@ts-ignore + this._rotationQuaternion._onValueChanged = () => { + this._setDirtyFlagTrue(TransformFlag.LocalMatrix | TransformFlag.LocalEuler); + this._setDirtyFlagFalse(TransformFlag.LocalQuat); + this._updateWorldRotationFlag(); + }; + + //@ts-ignore + this._worldRotationQuaternion._onValueChanged = () => { + const worldRotationQuaternion = this._worldRotationQuaternion; + const parent = this._getParentTransform(); + if (parent) { + Quaternion.invert(parent.worldRotationQuaternion, Transform._tempQuat0); + Quaternion.multiply(worldRotationQuaternion, Transform._tempQuat0, this._rotationQuaternion); + } else { + worldRotationQuaternion.cloneTo(this._rotationQuaternion); + } + this.rotationQuaternion = this._rotationQuaternion; + this._setDirtyFlagFalse(TransformFlag.WorldQuat); + }; + //@ts-ignore this._scale._onValueChanged = () => { this._setDirtyFlagTrue(TransformFlag.LocalMatrix); diff --git a/packages/core/src/animation/Animator.ts b/packages/core/src/animation/Animator.ts index d85700ba56..32d80f84f5 100644 --- a/packages/core/src/animation/Animator.ts +++ b/packages/core/src/animation/Animator.ts @@ -685,7 +685,6 @@ export class Animator extends Component { } else { const position = transform.position; Vector3.lerp(position, value, weight, position); - transform.position = position; } break; case AnimationProperty.Rotation: @@ -694,7 +693,6 @@ export class Animator extends Component { } else { const rotationQuaternion = transform.rotationQuaternion; Quaternion.slerp(rotationQuaternion, value, weight, rotationQuaternion); - transform.rotationQuaternion = rotationQuaternion; } break; case AnimationProperty.Scale: @@ -703,7 +701,6 @@ export class Animator extends Component { } else { const scale = transform.scale; Vector3.lerp(scale, value, weight, scale); - transform.scale = scale; } break; } diff --git a/packages/math/src/Quaternion.ts b/packages/math/src/Quaternion.ts index 08d24ab718..7cf384d661 100644 --- a/packages/math/src/Quaternion.ts +++ b/packages/math/src/Quaternion.ts @@ -415,14 +415,61 @@ export class Quaternion implements IClone { out.w = a.w * s; } - /** The x component of the quaternion. */ - x: number; - /** The y component of the quaternion. */ - y: number; - /** The z component of the quaternion. */ - z: number; - /** The w component of the quaternion. */ - w: number; + private _x: number; + private _y: number; + private _z: number; + private _w: number; + + /** @internal */ + _onValueChanged: () => void = null; + + /** + * The x component of the quaternion. + */ + public get x() { + return this._x; + } + + public set x(value: number) { + this._x = value; + this._onValueChanged && this._onValueChanged(); + } + + /** + * The y component of the quaternion. + */ + public get y() { + return this._y; + } + + public set y(value: number) { + this._y = value; + this._onValueChanged && this._onValueChanged(); + } + + /** + * The z component of the quaternion. + */ + public get z() { + return this._z; + } + + public set z(value: number) { + this._z = value; + this._onValueChanged && this._onValueChanged(); + } + + /** + * The w component of the quaternion. + */ + public get w() { + return this._w; + } + + public set w(value: number) { + this._w = value; + this._onValueChanged && this._onValueChanged(); + } /** * Constructor of Quaternion. @@ -432,10 +479,10 @@ export class Quaternion implements IClone { * @param w - The w component of the quaternion, default 1 */ constructor(x: number = 0, y: number = 0, z: number = 0, w: number = 1) { - this.x = x; - this.y = y; - this.z = z; - this.w = w; + this._x = x; + this._y = y; + this._z = z; + this._w = w; } /** diff --git a/packages/math/src/Vector3.ts b/packages/math/src/Vector3.ts index 64689e9237..6d45f17253 100644 --- a/packages/math/src/Vector3.ts +++ b/packages/math/src/Vector3.ts @@ -46,9 +46,10 @@ export class Vector3 implements IClone { * @param out - The product of two vectors */ static multiply(left: Vector3, right: Vector3, out: Vector3): void { - out.x = left.x * right.x; - out.y = left.y * right.y; - out.z = left.z * right.z; + out._x = left._x * right._x; + out._y = left._y * right._y; + out._z = left._z * right._z; + out._onValueChanged && out._onValueChanged(); } /** @@ -58,9 +59,10 @@ export class Vector3 implements IClone { * @param out - The divisor of two vectors */ static divide(left: Vector3, right: Vector3, out: Vector3): void { - out.x = left.x / right.x; - out.y = left.y / right.y; - out.z = left.z / right.z; + out._x = left._x / right._x; + out._y = left._y / right._y; + out._z = left._z / right._z; + out._onValueChanged && out._onValueChanged(); } /** @@ -70,7 +72,7 @@ export class Vector3 implements IClone { * @returns The dot product of two vectors */ static dot(left: Vector3, right: Vector3): number { - return left.x * right.x + left.y * right.y + left.z * right.z; + return left._x * right._x + left._y * right._y + left._z * right._z; } /** @@ -80,12 +82,12 @@ export class Vector3 implements IClone { * @param out - The cross product of two vectors */ static cross(left: Vector3, right: Vector3, out: Vector3): void { - const ax = left.x; - const ay = left.y; - const az = left.z; - const bx = right.x; - const by = right.y; - const bz = right.z; + const ax = left._x; + const ay = left._y; + const az = left._z; + const bx = right._x; + const by = right._y; + const bz = right._z; out.setValue(ay * bz - az * by, az * bx - ax * bz, ax * by - ay * bx); } @@ -110,9 +112,9 @@ export class Vector3 implements IClone { * @returns The squared distance of two vectors */ static distanceSquared(a: Vector3, b: Vector3): number { - const x = b.x - a.x; - const y = b.y - a.y; - const z = b.z - a.z; + const x = b._x - a._x; + const y = b._y - a._y; + const z = b._z - a._z; return x * x + y * y + z * z; } @@ -123,7 +125,9 @@ export class Vector3 implements IClone { * @returns True if the specified vectors are equals, false otherwise */ static equals(left: Vector3, right: Vector3): boolean { - return MathUtil.equals(left.x, right.x) && MathUtil.equals(left.y, right.y) && MathUtil.equals(left.z, right.z); + return ( + MathUtil.equals(left._x, right._x) && MathUtil.equals(left._y, right._y) && MathUtil.equals(left._z, right._z) + ); } /** @@ -148,9 +152,10 @@ export class Vector3 implements IClone { * @param out - The vector containing the largest components of the specified vectors */ static max(left: Vector3, right: Vector3, out: Vector3): void { - out.x = Math.max(left.x, right.x); - out.y = Math.max(left.y, right.y); - out.z = Math.max(left.z, right.z); + out._x = Math.max(left._x, right._x); + out._y = Math.max(left._y, right._y); + out._z = Math.max(left._z, right._z); + out._onValueChanged && out._onValueChanged(); } /** @@ -160,9 +165,10 @@ export class Vector3 implements IClone { * @param out - The vector containing the smallest components of the specified vectors */ static min(left: Vector3, right: Vector3, out: Vector3): void { - out.x = Math.min(left.x, right.x); - out.y = Math.min(left.y, right.y); - out.z = Math.min(left.z, right.z); + out._x = Math.min(left._x, right._x); + out._y = Math.min(left._y, right._y); + out._z = Math.min(left._z, right._z); + out._onValueChanged && out._onValueChanged(); } /** @@ -171,9 +177,10 @@ export class Vector3 implements IClone { * @param out - The vector facing in the opposite direction */ static negate(a: Vector3, out: Vector3): void { - out.x = -a.x; - out.y = -a.y; - out.z = -a.z; + out._x = -a._x; + out._y = -a._y; + out._z = -a._z; + out._onValueChanged && out._onValueChanged(); } /** @@ -217,11 +224,12 @@ export class Vector3 implements IClone { * @param out - The transformed normal */ static transformNormal(v: Vector3, m: Matrix, out: Vector3): void { - const { x, y, z } = v; + const { _x, _y, _z } = v; const e = m.elements; - out.x = x * e[0] + y * e[4] + z * e[8]; - out.y = x * e[1] + y * e[5] + z * e[9]; - out.z = x * e[2] + y * e[6] + z * e[10]; + out._x = _x * e[0] + _y * e[4] + _z * e[8]; + out._y = _x * e[1] + _y * e[5] + _z * e[9]; + out._z = _x * e[2] + _y * e[6] + _z * e[10]; + out._onValueChanged && out._onValueChanged(); } /** @@ -307,7 +315,6 @@ export class Vector3 implements IClone { /** @internal */ _onValueChanged: () => void = null; - _dirty: boolean = false; /** * The x component of the vector. From 9c52c60e3e087677932e0ef5c25555ee65591579 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Mon, 24 Jan 2022 15:57:49 +0800 Subject: [PATCH 07/21] refactor: opt code --- packages/math/src/Quaternion.ts | 53 +++++++++++++++++---------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/packages/math/src/Quaternion.ts b/packages/math/src/Quaternion.ts index 7cf384d661..f5f5d87d31 100644 --- a/packages/math/src/Quaternion.ts +++ b/packages/math/src/Quaternion.ts @@ -19,10 +19,11 @@ export class Quaternion implements IClone { * @param out - The sum of two quaternions */ static add(left: Quaternion, right: Quaternion, out: Quaternion): void { - out.x = left.x + right.x; - out.y = left.y + right.y; - out.z = left.z + right.z; - out.w = left.w + right.w; + out._x = left._x + right._x; + out._y = left._y + right._y; + out._z = left._z + right._z; + out._w = left._w + right._w; + out._onValueChanged && out._onValueChanged(); } /** @@ -32,19 +33,20 @@ export class Quaternion implements IClone { * @param out - The product of two quaternions */ static multiply(left: Quaternion, right: Quaternion, out: Quaternion): void { - const ax = left.x, - ay = left.y, - az = left.z, - aw = left.w; - const bx = right.x, - by = right.y, - bz = right.z, - bw = right.w; + const ax = left._x, + ay = left._y, + az = left._z, + aw = left._w; + const bx = right._x, + by = right._y, + bz = right._z, + bw = right._w; - out.x = ax * bw + aw * bx + ay * bz - az * by; - out.y = ay * bw + aw * by + az * bx - ax * bz; - out.z = az * bw + aw * bz + ax * by - ay * bx; - out.w = aw * bw - ax * bx - ay * by - az * bz; + out._x = ax * bw + aw * bx + ay * bz - az * by; + out._y = ay * bw + aw * by + az * bx - ax * bz; + out._z = az * bw + aw * bz + ax * by - ay * bx; + out._w = aw * bw - ax * bx - ay * by - az * bz; + out._onValueChanged && out._onValueChanged(); } /** @@ -53,10 +55,11 @@ export class Quaternion implements IClone { * @param out - The conjugate version of the specified quaternion */ static conjugate(a: Quaternion, out: Quaternion): void { - out.x = -a.x; - out.y = -a.y; - out.z = -a.z; - out.w = a.w; + out._x = -a._x; + out._y = -a._y; + out._z = -a._z; + out._w = a._w; + out._onValueChanged && out._onValueChanged(); } /** @@ -66,7 +69,7 @@ export class Quaternion implements IClone { * @returns The dot product of two quaternions */ static dot(left: Quaternion, right: Quaternion): number { - return left.x * right.x + left.y * right.y + left.z * right.z + left.w * right.w; + return left._x * right._x + left._y * right._y + left._z * right._z + left._w * right._w; } /** @@ -77,10 +80,10 @@ export class Quaternion implements IClone { */ static equals(left: Quaternion, right: Quaternion): boolean { return ( - MathUtil.equals(left.x, right.x) && - MathUtil.equals(left.y, right.y) && - MathUtil.equals(left.z, right.z) && - MathUtil.equals(left.w, right.w) + MathUtil.equals(left._x, right._x) && + MathUtil.equals(left._y, right._y) && + MathUtil.equals(left._z, right._z) && + MathUtil.equals(left._w, right._w) ); } From 8d8df2a55e437796b982b5d61b76eb83c4070213 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Mon, 24 Jan 2022 16:09:40 +0800 Subject: [PATCH 08/21] refactor: opt code --- packages/math/src/Vector3.ts | 97 +++++++++++++++++++----------------- 1 file changed, 52 insertions(+), 45 deletions(-) diff --git a/packages/math/src/Vector3.ts b/packages/math/src/Vector3.ts index 6d45f17253..1a76ba8882 100644 --- a/packages/math/src/Vector3.ts +++ b/packages/math/src/Vector3.ts @@ -99,9 +99,9 @@ export class Vector3 implements IClone { * @returns The distance of two vectors */ static distance(a: Vector3, b: Vector3): number { - const x = b.x - a.x; - const y = b.y - a.y; - const z = b.z - a.z; + const x = b._x - a._x; + const y = b._y - a._y; + const z = b._z - a._z; return Math.sqrt(x * x + y * y + z * z); } @@ -190,7 +190,7 @@ export class Vector3 implements IClone { */ static normalize(a: Vector3, out: Vector3): void { const { _x, _y, _z } = a; - let len: number = Math.sqrt(_x * _x + _y * _y + _z * _z); + let len = Math.sqrt(_x * _x + _y * _y + _z * _z); if (len > 0) { // TODO len = 1 / len; @@ -239,12 +239,13 @@ export class Vector3 implements IClone { * @param out - The transformed vector3 */ static transformToVec3(v: Vector3, m: Matrix, out: Vector3): void { - const { x, y, z } = v; + const { _x, _y, _z } = v; const e = m.elements; - out.x = x * e[0] + y * e[4] + z * e[8] + e[12]; - out.y = x * e[1] + y * e[5] + z * e[9] + e[13]; - out.z = x * e[2] + y * e[6] + z * e[10] + e[14]; + out._x = _x * e[0] + _y * e[4] + _z * e[8] + e[12]; + out._y = _x * e[1] + _y * e[5] + _z * e[9] + e[13]; + out._z = _x * e[2] + _y * e[6] + _z * e[10] + e[14]; + out._onValueChanged && out._onValueChanged(); } /** @@ -254,13 +255,13 @@ export class Vector3 implements IClone { * @param out - The transformed vector4 */ static transformToVec4(v: Vector3, m: Matrix, out: Vector4): void { - const { x, y, z } = v; + const { _x, _y, _z } = v; const e = m.elements; - out.x = x * e[0] + y * e[4] + z * e[8] + e[12]; - out.y = x * e[1] + y * e[5] + z * e[9] + e[13]; - out.z = x * e[2] + y * e[6] + z * e[10] + e[14]; - out.w = x * e[3] + y * e[7] + z * e[11] + e[15]; + out.x = _x * e[0] + _y * e[4] + _z * e[8] + e[12]; + out.y = _x * e[1] + _y * e[5] + _z * e[9] + e[13]; + out.z = _x * e[2] + _y * e[6] + _z * e[10] + e[14]; + out.w = _x * e[3] + _y * e[7] + _z * e[11] + e[15]; } /** @@ -277,14 +278,15 @@ export class Vector3 implements IClone { * @param out - The transformed coordinates */ static transformCoordinate(v: Vector3, m: Matrix, out: Vector3): void { - const { x, y, z } = v; + const { _x, _y, _z } = v; const e = m.elements; - let w = x * e[3] + y * e[7] + z * e[11] + e[15]; + let w = _x * e[3] + _y * e[7] + _z * e[11] + e[15]; w = 1.0 / w; - out.x = (x * e[0] + y * e[4] + z * e[8] + e[12]) * w; - out.y = (x * e[1] + y * e[5] + z * e[9] + e[13]) * w; - out.z = (x * e[2] + y * e[6] + z * e[10] + e[14]) * w; + out._x = (_x * e[0] + _y * e[4] + _z * e[8] + e[12]) * w; + out._y = (_x * e[1] + _y * e[5] + _z * e[9] + e[13]) * w; + out._z = (_x * e[2] + _y * e[6] + _z * e[10] + e[14]) * w; + out._onValueChanged && out._onValueChanged(); } /** @@ -294,19 +296,20 @@ export class Vector3 implements IClone { * @param out - The transformed vector */ static transformByQuat(v: Vector3, quaternion: Quaternion, out: Vector3): void { - const { x, y, z } = v; + const { _x, _y, _z } = v; const { x: qx, y: qy, z: qz, w: qw } = quaternion; // calculate quat * vec - const ix = qw * x + qy * z - qz * y; - const iy = qw * y + qz * x - qx * z; - const iz = qw * z + qx * y - qy * x; - const iw = -qx * x - qy * y - qz * z; + const ix = qw * _x + qy * _z - qz * _y; + const iy = qw * _y + qz * _x - qx * _z; + const iz = qw * _z + qx * _y - qy * _x; + const iw = -qx * _x - qy * _y - qz * _z; // calculate result * inverse quat - out.x = ix * qw - iw * qx - iy * qz + iz * qy; - out.y = iy * qw - iw * qy - iz * qx + ix * qz; - out.z = iz * qw - iw * qz - ix * qy + iy * qx; + out._x = ix * qw - iw * qx - iy * qz + iz * qy; + out._y = iy * qw - iw * qy - iz * qx + ix * qz; + out._z = iz * qw - iw * qz - ix * qy + iy * qx; + out._onValueChanged && out._onValueChanged(); } private _x: number; @@ -424,9 +427,10 @@ export class Vector3 implements IClone { * @returns This vector */ multiply(right: Vector3): Vector3 { - this.x *= right.x; - this.y *= right.y; - this.z *= right.z; + this._x *= right._x; + this._y *= right._y; + this._z *= right._z; + this._onValueChanged && this._onValueChanged(); return this; } @@ -436,9 +440,10 @@ export class Vector3 implements IClone { * @returns This vector */ divide(right: Vector3): Vector3 { - this.x /= right.x; - this.y /= right.y; - this.z /= right.z; + this._x /= right._x; + this._y /= right._y; + this._z /= right._z; + this._onValueChanged && this._onValueChanged(); return this; } @@ -447,8 +452,8 @@ export class Vector3 implements IClone { * @returns The length of this vector */ length(): number { - const { x, y, z } = this; - return Math.sqrt(x * x + y * y + z * z); + const { _x, _y, _z } = this; + return Math.sqrt(_x * _x + _y * _y + _z * _z); } /** @@ -456,8 +461,8 @@ export class Vector3 implements IClone { * @returns The squared length of this vector */ lengthSquared(): number { - const { x, y, z } = this; - return x * x + y * y + z * z; + const { _x, _y, _z } = this; + return _x * _x + _y * _y + _z * _z; } /** @@ -465,9 +470,10 @@ export class Vector3 implements IClone { * @returns This vector */ negate(): Vector3 { - this.x = -this.x; - this.y = -this.y; - this.z = -this.z; + this._x = -this._x; + this._y = -this._y; + this._z = -this._z; + this._onValueChanged && this._onValueChanged(); return this; } @@ -486,9 +492,10 @@ export class Vector3 implements IClone { * @returns This vector */ scale(s: number): Vector3 { - this.x *= s; - this.y *= s; - this.z *= s; + this._x *= s; + this._y *= s; + this._z *= s; + this._onValueChanged && this._onValueChanged(); return this; } @@ -498,9 +505,9 @@ export class Vector3 implements IClone { * @param outOffset - The start offset of the array */ toArray(out: number[] | Float32Array | Float64Array, outOffset: number = 0) { - out[outOffset] = this.x; - out[outOffset + 1] = this.y; - out[outOffset + 2] = this.z; + out[outOffset] = this._x; + out[outOffset + 1] = this._y; + out[outOffset + 2] = this._z; } /** From cc8e65a80f96b6be78986d91ccfb0438156462cd Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Mon, 24 Jan 2022 17:30:29 +0800 Subject: [PATCH 09/21] refactor: opt code --- packages/math/src/Quaternion.ts | 121 +++++++++++++++++--------------- packages/math/src/Vector3.ts | 9 ++- 2 files changed, 69 insertions(+), 61 deletions(-) diff --git a/packages/math/src/Quaternion.ts b/packages/math/src/Quaternion.ts index f5f5d87d31..6fe644464c 100644 --- a/packages/math/src/Quaternion.ts +++ b/packages/math/src/Quaternion.ts @@ -497,11 +497,11 @@ export class Quaternion implements IClone { * @returns This quaternion */ setValue(x: number, y: number, z: number, w: number): Quaternion { - this.x = x; - this.y = y; - this.z = z; - this.w = w; - + this._x = x; + this._y = y; + this._z = z; + this._w = w; + this._onValueChanged && this._onValueChanged(); return this; } @@ -512,10 +512,11 @@ export class Quaternion implements IClone { * @returns This quaternion */ setValueByArray(array: ArrayLike, offset: number = 0): Quaternion { - this.x = array[offset]; - this.y = array[offset + 1]; - this.z = array[offset + 2]; - this.w = array[offset + 3]; + this._x = array[offset]; + this._y = array[offset + 1]; + this._z = array[offset + 2]; + this._w = array[offset + 3]; + this._onValueChanged && this._onValueChanged(); return this; } @@ -524,10 +525,10 @@ export class Quaternion implements IClone { * @returns This quaternion */ conjugate(): Quaternion { - this.x *= -1; - this.y *= -1; - this.z *= -1; - + this._x *= -1; + this._y *= -1; + this._z *= -1; + this._onValueChanged && this._onValueChanged(); return this; } @@ -537,22 +538,22 @@ export class Quaternion implements IClone { * @returns The rotation angle (unit: radians) */ getAxisAngle(out: Vector3): number { - const { x, y, z } = this; - const length = x * x + y * y + z * z; + const { _x, _y, _z } = this; + const length = _x * _x + _y * _y + _z * _z; if (length < MathUtil.zeroTolerance) { - out.x = 1; - out.y = 0; - out.z = 0; + out._x = 1; + out._y = 0; + out._z = 0; return 0; } else { const inv = 1.0 / length; - out.x = this.x * inv; - out.y = this.y * inv; - out.z = this.z * inv; + out._x = this._x * inv; + out._y = this._y * inv; + out._z = this._z * inv; - return Math.acos(this.w) * 2.0; + return Math.acos(this._w) * 2.0; } } @@ -561,10 +562,11 @@ export class Quaternion implements IClone { * @returns This quaternion after identity */ identity(): Quaternion { - this.x = 0; - this.y = 0; - this.z = 0; - this.w = 1; + this._x = 0; + this._y = 0; + this._z = 0; + this._w = 1; + this._onValueChanged && this._onValueChanged(); return this; } @@ -573,8 +575,8 @@ export class Quaternion implements IClone { * @returns The length of this quaternion */ length(): number { - const { x, y, z, w } = this; - return Math.sqrt(x * x + y * y + z * z + w * w); + const { _x, _y, _z, _w } = this; + return Math.sqrt(_x * _x + _y * _y + _z * _z + _w * _w); } /** @@ -582,8 +584,8 @@ export class Quaternion implements IClone { * @returns The squared length of this quaternion */ lengthSquared(): number { - const { x, y, z, w } = this; - return x * x + y * y + z * z + w * w; + const { _x, _y, _z, _w } = this; + return _x * _x + _y * _y + _z * _z + _w * _w; } /** @@ -602,9 +604,10 @@ export class Quaternion implements IClone { */ toEuler(out: Vector3): Vector3 { this.toYawPitchRoll(out); - const t = out.x; - out.x = out.y; - out.y = t; + // @todo: + const t = out._x; + out._x = out._y; + out._y = t; return out; } @@ -614,26 +617,27 @@ export class Quaternion implements IClone { * @returns Euler x->yaw y->pitch z->roll */ toYawPitchRoll(out: Vector3): Vector3 { - const { x, y, z, w } = this; - const xx = x * x; - const yy = y * y; - const zz = z * z; - const xy = x * y; - const zw = z * w; - const zx = z * x; - const yw = y * w; - const yz = y * z; - const xw = x * w; - - out.y = Math.asin(2.0 * (xw - yz)); + const { _x, _y, _z, _w } = this; + const xx = _x * _x; + const yy = _y * _y; + const zz = _z * _z; + const xy = _x * _y; + const zw = _z * _w; + const zx = _z * _x; + const yw = _y * _w; + const yz = _y * _z; + const xw = _x * _w; + + out._y = Math.asin(2.0 * (xw - yz)); if (Math.cos(out.y) > MathUtil.zeroTolerance) { - out.z = Math.atan2(2.0 * (xy + zw), 1.0 - 2.0 * (zz + xx)); - out.x = Math.atan2(2.0 * (zx + yw), 1.0 - 2.0 * (yy + xx)); + out._z = Math.atan2(2.0 * (xy + zw), 1.0 - 2.0 * (zz + xx)); + out._x = Math.atan2(2.0 * (zx + yw), 1.0 - 2.0 * (yy + xx)); } else { - out.z = Math.atan2(-2.0 * (xy - zw), 1.0 - 2.0 * (yy + zz)); - out.x = 0.0; + out._z = Math.atan2(-2.0 * (xy - zw), 1.0 - 2.0 * (yy + zz)); + out._x = 0.0; } + out._onValueChanged && out._onValueChanged(); return out; } @@ -643,10 +647,10 @@ export class Quaternion implements IClone { * @param outOffset - The start offset of the array */ toArray(out: number[] | Float32Array | Float64Array, outOffset: number = 0) { - out[outOffset] = this.x; - out[outOffset + 1] = this.y; - out[outOffset + 2] = this.z; - out[outOffset + 3] = this.w; + out[outOffset] = this._x; + out[outOffset + 1] = this._y; + out[outOffset + 2] = this._z; + out[outOffset + 3] = this._w; } /** @@ -654,7 +658,7 @@ export class Quaternion implements IClone { * @returns A clone of this quaternion */ clone(): Quaternion { - return new Quaternion(this.x, this.y, this.z, this.w); + return new Quaternion(this._x, this._y, this._z, this._w); } /** @@ -663,10 +667,11 @@ export class Quaternion implements IClone { * @returns The specified quaternion */ cloneTo(out: Quaternion): Quaternion { - out.x = this.x; - out.y = this.y; - out.z = this.z; - out.w = this.w; + out._x = this._x; + out._y = this._y; + out._z = this._z; + out._w = this._w; + out._onValueChanged && out._onValueChanged(); return out; } diff --git a/packages/math/src/Vector3.ts b/packages/math/src/Vector3.ts index 1a76ba8882..3b41f83766 100644 --- a/packages/math/src/Vector3.ts +++ b/packages/math/src/Vector3.ts @@ -312,9 +312,12 @@ export class Vector3 implements IClone { out._onValueChanged && out._onValueChanged(); } - private _x: number; - private _y: number; - private _z: number; + /** @internal */ + _x: number; + /** @internal */ + _y: number; + /** @internal */ + _z: number; /** @internal */ _onValueChanged: () => void = null; From f24c8d9e02cfc4bae11c828608b0fd6fd571394f Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Mon, 24 Jan 2022 17:50:18 +0800 Subject: [PATCH 10/21] refactor: clear code --- packages/core/src/animation/Animator.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/core/src/animation/Animator.ts b/packages/core/src/animation/Animator.ts index 32d80f84f5..d482ff9923 100644 --- a/packages/core/src/animation/Animator.ts +++ b/packages/core/src/animation/Animator.ts @@ -653,9 +653,7 @@ export class Animator extends Component { value = Animator._tempQuaternion; break; case AnimationProperty.Scale: { - const scale = transform.scale; Vector3.lerp(srcValue as Vector3, destValue as Vector3, crossWeight, Animator._tempVector3); - transform.scale = scale; value = Animator._tempVector3; break; } From 49621d8aa48b17b1eea97c0273f8f0d2057e0085 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Wed, 26 Jan 2022 14:27:38 +0800 Subject: [PATCH 11/21] refactor: opt Vector2 Math --- packages/math/src/Vector2.ts | 165 ++++++++++++++++++++++------------- packages/math/src/Vector3.ts | 5 +- packages/math/src/Vector4.ts | 66 ++++++++++++-- 3 files changed, 164 insertions(+), 72 deletions(-) diff --git a/packages/math/src/Vector2.ts b/packages/math/src/Vector2.ts index a4ee1ec855..2927ba19f2 100644 --- a/packages/math/src/Vector2.ts +++ b/packages/math/src/Vector2.ts @@ -17,8 +17,9 @@ export class Vector2 implements IClone { * @param out - The sum of two vectors */ static add(left: Vector2, right: Vector2, out: Vector2): void { - out.x = left.x + right.x; - out.y = left.y + right.y; + out._x = left._x + right._x; + out._y = left._y + right._y; + out._onValueChanged && out._onValueChanged(); } /** @@ -28,8 +29,9 @@ export class Vector2 implements IClone { * @param out - The difference between two vectors */ static subtract(left: Vector2, right: Vector2, out: Vector2): void { - out.x = left.x - right.x; - out.y = left.y - right.y; + out._x = left._x - right._x; + out._y = left._y - right._y; + out._onValueChanged && out._onValueChanged(); } /** @@ -39,8 +41,9 @@ export class Vector2 implements IClone { * @param out - The product of two vectors */ static multiply(left: Vector2, right: Vector2, out: Vector2): void { - out.x = left.x * right.x; - out.y = left.y * right.y; + out._x = left._x * right._x; + out._y = left._y * right._y; + out._onValueChanged && out._onValueChanged(); } /** @@ -50,8 +53,9 @@ export class Vector2 implements IClone { * @param out - The divisor of two vectors */ static divide(left: Vector2, right: Vector2, out: Vector2): void { - out.x = left.x / right.x; - out.y = left.y / right.y; + out._x = left._x / right._x; + out._y = left._y / right._y; + out._onValueChanged && out._onValueChanged(); } /** @@ -61,7 +65,7 @@ export class Vector2 implements IClone { * @returns The dot product of two vectors */ static dot(left: Vector2, right: Vector2): number { - return left.x * right.x + left.y * right.y; + return left._x * right._x + left._y * right._y; } /** @@ -71,8 +75,8 @@ export class Vector2 implements IClone { * @returns The distance of two vectors */ static distance(left: Vector2, right: Vector2): number { - const x = right.x - left.x; - const y = right.y - left.y; + const x = right._x - left._x; + const y = right._y - left._y; return Math.sqrt(x * x + y * y); } @@ -83,8 +87,8 @@ export class Vector2 implements IClone { * @returns The squared distance of two vectors */ static distanceSquared(left: Vector2, right: Vector2): number { - const x = right.x - left.x; - const y = right.y - left.y; + const x = right._x - left._x; + const y = right._y - left._y; return x * x + y * y; } @@ -95,7 +99,7 @@ export class Vector2 implements IClone { * @returns True if the specified vectors are equals, false otherwise */ static equals(left: Vector2, right: Vector2): boolean { - return MathUtil.equals(left.x, right.x) && MathUtil.equals(left.y, right.y); + return MathUtil.equals(left._x, right._x) && MathUtil.equals(left._y, right._y); } /** @@ -106,9 +110,10 @@ export class Vector2 implements IClone { * @param out - The result of linear blending between two vectors */ static lerp(left: Vector2, right: Vector2, t: number, out: Vector2): void { - const { x, y } = left; - out.x = x + (right.x - x) * t; - out.y = y + (right.y - y) * t; + const { _x, _y } = left; + out._x = _x + (right.x - _x) * t; + out._y = _y + (right.y - _y) * t; + out._onValueChanged && out._onValueChanged(); } /** @@ -118,8 +123,9 @@ export class Vector2 implements IClone { * @param out - The vector containing the largest components of the specified vectors */ static max(left: Vector2, right: Vector2, out: Vector2): void { - out.x = Math.max(left.x, right.x); - out.y = Math.max(left.y, right.y); + out._x = Math.max(left._x, right._x); + out._y = Math.max(left._y, right._y); + out._onValueChanged && out._onValueChanged(); } /** @@ -129,8 +135,9 @@ export class Vector2 implements IClone { * @param out - The vector containing the smallest components of the specified vectors */ static min(left: Vector2, right: Vector2, out: Vector2): void { - out.x = Math.min(left.x, right.x); - out.y = Math.min(left.y, right.y); + out._x = Math.min(left._x, right._x); + out._y = Math.min(left._y, right._y); + out._onValueChanged && out._onValueChanged(); } /** @@ -139,8 +146,9 @@ export class Vector2 implements IClone { * @param out - The vector facing in the opposite direction */ static negate(left: Vector2, out: Vector2): void { - out.x = -left.x; - out.y = -left.y; + out._x = -left._x; + out._y = -left._y; + out._onValueChanged && out._onValueChanged(); } /** @@ -149,12 +157,13 @@ export class Vector2 implements IClone { * @param out - The normalized vector */ static normalize(left: Vector2, out: Vector2): void { - const { x, y } = left; - let len: number = Math.sqrt(x * x + y * y); + const { _x, _y } = left; + let len = Math.sqrt(_x * _x + _y * _y); if (len > MathUtil.zeroTolerance) { len = 1 / len; - out.x = x * len; - out.y = y * len; + out._x = _x * len; + out._y = _y * len; + out._onValueChanged && out._onValueChanged(); } } @@ -165,14 +174,41 @@ export class Vector2 implements IClone { * @param out - The scaled vector */ static scale(left: Vector2, s: number, out: Vector2): void { - out.x = left.x * s; - out.y = left.y * s; + out._x = left._x * s; + out._y = left._y * s; + out._onValueChanged && out._onValueChanged(); } - /** The x component of the vector. */ - x: number; - /** The y component of the vector. */ - y: number; + /** @internal */ + _x: number; + /** @internal */ + _y: number; + /** @internal */ + _onValueChanged: () => void = null; + + /** + * The x component of the vector. + */ + public get x() { + return this._x; + } + + public set x(value: number) { + this._x = value; + this._onValueChanged && this._onValueChanged(); + } + + /** + * The y component of the vector. + */ + public get y() { + return this._y; + } + + public set y(value: number) { + this._y = value; + this._onValueChanged && this._onValueChanged(); + } /** * Constructor of Vector2. @@ -180,8 +216,8 @@ export class Vector2 implements IClone { * @param y - The y component of the vector, default 0 */ constructor(x: number = 0, y: number = 0) { - this.x = x; - this.y = y; + this._x = x; + this._y = y; } /** @@ -191,8 +227,9 @@ export class Vector2 implements IClone { * @returns This vector */ setValue(x: number, y: number): Vector2 { - this.x = x; - this.y = y; + this._x = x; + this._y = y; + this._onValueChanged && this._onValueChanged(); return this; } @@ -203,8 +240,9 @@ export class Vector2 implements IClone { * @returns This vector */ setValueByArray(array: ArrayLike, offset: number = 0): Vector2 { - this.x = array[offset]; - this.y = array[offset + 1]; + this._x = array[offset]; + this._y = array[offset + 1]; + this._onValueChanged && this._onValueChanged(); return this; } @@ -214,8 +252,9 @@ export class Vector2 implements IClone { * @returns This vector */ add(right: Vector2): Vector2 { - this.x += right.x; - this.y += right.y; + this._x += right.x; + this._y += right.y; + this._onValueChanged && this._onValueChanged(); return this; } @@ -225,8 +264,9 @@ export class Vector2 implements IClone { * @returns This vector */ subtract(right: Vector2): Vector2 { - this.x -= right.x; - this.y -= right.y; + this._x -= right.x; + this._y -= right.y; + this._onValueChanged && this._onValueChanged(); return this; } @@ -236,8 +276,9 @@ export class Vector2 implements IClone { * @returns This vector */ multiply(right: Vector2): Vector2 { - this.x *= right.x; - this.y *= right.y; + this._x *= right.x; + this._y *= right.y; + this._onValueChanged && this._onValueChanged(); return this; } @@ -247,8 +288,9 @@ export class Vector2 implements IClone { * @returns This vector */ divide(right: Vector2): Vector2 { - this.x /= right.x; - this.y /= right.y; + this._x /= right.x; + this._y /= right.y; + this._onValueChanged && this._onValueChanged(); return this; } @@ -257,8 +299,8 @@ export class Vector2 implements IClone { * @returns The length of this vector */ length(): number { - const { x, y } = this; - return Math.sqrt(x * x + y * y); + const { _x, _y } = this; + return Math.sqrt(_x * _x + _y * _y); } /** @@ -266,8 +308,8 @@ export class Vector2 implements IClone { * @returns The squared length of this vector */ lengthSquared(): number { - const { x, y } = this; - return x * x + y * y; + const { _x, _y } = this; + return _x * _x + _y * _y; } /** @@ -275,8 +317,9 @@ export class Vector2 implements IClone { * @returns This vector */ negate(): Vector2 { - this.x = -this.x; - this.y = -this.y; + this._x = -this._x; + this._y = -this._y; + this._onValueChanged && this._onValueChanged(); return this; } @@ -295,8 +338,9 @@ export class Vector2 implements IClone { * @returns This vector */ scale(s: number): Vector2 { - this.x *= s; - this.y *= s; + this._x *= s; + this._y *= s; + this._onValueChanged && this._onValueChanged(); return this; } @@ -306,8 +350,8 @@ export class Vector2 implements IClone { * @param outOffset - The start offset of the array */ toArray(out: number[] | Float32Array | Float64Array, outOffset: number = 0) { - out[outOffset] = this.x; - out[outOffset + 1] = this.y; + out[outOffset] = this._x; + out[outOffset + 1] = this._y; } /** @@ -315,7 +359,7 @@ export class Vector2 implements IClone { * @returns A clone of this vector */ clone(): Vector2 { - return new Vector2(this.x, this.y); + return new Vector2(this._x, this._y); } /** @@ -324,8 +368,9 @@ export class Vector2 implements IClone { * @returns The specified vector */ cloneTo(out: Vector2): Vector2 { - out.x = this.x; - out.y = this.y; + out._x = this._x; + out._y = this._y; + out._onValueChanged && out._onValueChanged(); return out; } } diff --git a/packages/math/src/Vector3.ts b/packages/math/src/Vector3.ts index 3b41f83766..3e83116c6a 100644 --- a/packages/math/src/Vector3.ts +++ b/packages/math/src/Vector3.ts @@ -191,8 +191,7 @@ export class Vector3 implements IClone { static normalize(a: Vector3, out: Vector3): void { const { _x, _y, _z } = a; let len = Math.sqrt(_x * _x + _y * _y + _z * _z); - if (len > 0) { - // TODO + if (len > MathUtil.zeroTolerance) { len = 1 / len; out.setValue(_x * len, _y * len, _z * len); } @@ -257,7 +256,6 @@ export class Vector3 implements IClone { static transformToVec4(v: Vector3, m: Matrix, out: Vector4): void { const { _x, _y, _z } = v; const e = m.elements; - out.x = _x * e[0] + _y * e[4] + _z * e[8] + e[12]; out.y = _x * e[1] + _y * e[5] + _z * e[9] + e[13]; out.z = _x * e[2] + _y * e[6] + _z * e[10] + e[14]; @@ -318,7 +316,6 @@ export class Vector3 implements IClone { _y: number; /** @internal */ _z: number; - /** @internal */ _onValueChanged: () => void = null; diff --git a/packages/math/src/Vector4.ts b/packages/math/src/Vector4.ts index 83f460daeb..5a15e0e84e 100644 --- a/packages/math/src/Vector4.ts +++ b/packages/math/src/Vector4.ts @@ -241,14 +241,64 @@ export class Vector4 implements IClone { out.w = w; } - /** The x component of the vector. */ - x: number; - /** The y component of the vector. */ - y: number; - /** The z component of the vector. */ - z: number; - /** The w component of the vector. */ - w: number; + /** @internal */ + _x: number; + /** @internal */ + _y: number; + /** @internal */ + _z: number; + /** @internal */ + _w: number; + /** @internal */ + _onValueChanged: () => void = null; + + /** + * The x component of the vector. + */ + public get x() { + return this._x; + } + + public set x(value: number) { + this._x = value; + this._onValueChanged && this._onValueChanged(); + } + + /** + * The y component of the vector. + */ + public get y() { + return this._y; + } + + public set y(value: number) { + this._y = value; + this._onValueChanged && this._onValueChanged(); + } + + /** + * The z component of the vector. + */ + public get z() { + return this._z; + } + + public set z(value: number) { + this._z = value; + this._onValueChanged && this._onValueChanged(); + } + + /** + * The w component of the vector. + */ + public get w() { + return this._w; + } + + public set w(value: number) { + this._w = value; + this._onValueChanged && this._onValueChanged(); + } /** * Constructor of Vector4. From 9c2f46dc19f9b1ded02b123c90faa06d894f0d71 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Wed, 26 Jan 2022 14:43:14 +0800 Subject: [PATCH 12/21] refactor: opt vector4 --- packages/math/src/Quaternion.ts | 13 +- packages/math/src/Vector4.ts | 257 +++++++++++++++++--------------- 2 files changed, 147 insertions(+), 123 deletions(-) diff --git a/packages/math/src/Quaternion.ts b/packages/math/src/Quaternion.ts index 6fe644464c..e254b4a502 100644 --- a/packages/math/src/Quaternion.ts +++ b/packages/math/src/Quaternion.ts @@ -418,11 +418,14 @@ export class Quaternion implements IClone { out.w = a.w * s; } - private _x: number; - private _y: number; - private _z: number; - private _w: number; - + /** @internal */ + _x: number; + /** @internal */ + _y: number; + /** @internal */ + _z: number; + /** @internal */ + _w: number; /** @internal */ _onValueChanged: () => void = null; diff --git a/packages/math/src/Vector4.ts b/packages/math/src/Vector4.ts index 5a15e0e84e..9fd44527a0 100644 --- a/packages/math/src/Vector4.ts +++ b/packages/math/src/Vector4.ts @@ -19,10 +19,11 @@ export class Vector4 implements IClone { * @param out - The sum of two vectors */ static add(left: Vector4, right: Vector4, out: Vector4): void { - out.x = left.x + right.x; - out.y = left.y + right.y; - out.z = left.z + right.z; - out.w = left.w + right.w; + out._x = left._x + right._x; + out._y = left._y + right._y; + out._z = left._z + right._z; + out._w = left._w + right._w; + out._onValueChanged && out._onValueChanged(); } /** @@ -32,10 +33,11 @@ export class Vector4 implements IClone { * @param out - The difference between two vectors */ static subtract(left: Vector4, right: Vector4, out: Vector4): void { - out.x = left.x - right.x; - out.y = left.y - right.y; - out.z = left.z - right.z; - out.w = left.w - right.w; + out._x = left._x - right._x; + out._y = left._y - right._y; + out._z = left._z - right._z; + out._w = left._w - right._w; + out._onValueChanged && out._onValueChanged(); } /** @@ -45,10 +47,11 @@ export class Vector4 implements IClone { * @param out - The product of two vectors */ static multiply(left: Vector4, right: Vector4, out: Vector4): void { - out.x = left.x * right.x; - out.y = left.y * right.y; - out.z = left.z * right.z; - out.w = left.w * right.w; + out._x = left._x * right._x; + out._y = left._y * right._y; + out._z = left._z * right._z; + out._w = left._w * right._w; + out._onValueChanged && out._onValueChanged(); } /** @@ -58,10 +61,11 @@ export class Vector4 implements IClone { * @param out - The divisor of two vectors */ static divide(left: Vector4, right: Vector4, out: Vector4): void { - out.x = left.x / right.x; - out.y = left.y / right.y; - out.z = left.z / right.z; - out.w = left.w / right.w; + out._x = left._x / right._x; + out._y = left._y / right._y; + out._z = left._z / right._z; + out._w = left._w / right._w; + out._onValueChanged && out._onValueChanged(); } /** @@ -71,7 +75,7 @@ export class Vector4 implements IClone { * @returns The dot product of two vectors */ static dot(left: Vector4, right: Vector4): number { - return left.x * right.x + left.y * right.y + left.z * right.z + left.w * right.w; + return left._x * right._x + left._y * right._y + left._z * right._z + left._w * right._w; } /** @@ -81,10 +85,10 @@ export class Vector4 implements IClone { * @returns The distance of two vectors */ static distance(a: Vector4, b: Vector4): number { - const x = b.x - a.x; - const y = b.y - a.y; - const z = b.z - a.z; - const w = b.w - a.w; + const x = b._x - a._x; + const y = b._y - a._y; + const z = b._z - a._z; + const w = b._w - a._w; return Math.sqrt(x * x + y * y + z * z + w * w); } @@ -95,10 +99,10 @@ export class Vector4 implements IClone { * @returns The squared distance of two vectors */ static distanceSquared(a: Vector4, b: Vector4): number { - const x = b.x - a.x; - const y = b.y - a.y; - const z = b.z - a.z; - const w = b.w - a.w; + const x = b._x - a._x; + const y = b._y - a._y; + const z = b._z - a._z; + const w = b._w - a._w; return x * x + y * y + z * z + w * w; } @@ -110,10 +114,10 @@ export class Vector4 implements IClone { */ static equals(left: Vector4, right: Vector4): boolean { return ( - MathUtil.equals(left.x, right.x) && - MathUtil.equals(left.y, right.y) && - MathUtil.equals(left.z, right.z) && - MathUtil.equals(left.w, right.w) + MathUtil.equals(left._x, right._x) && + MathUtil.equals(left._y, right._y) && + MathUtil.equals(left._z, right._z) && + MathUtil.equals(left._w, right._w) ); } @@ -125,11 +129,12 @@ export class Vector4 implements IClone { * @param out - The result of linear blending between two vectors */ static lerp(start: Vector4, end: Vector4, t: number, out: Vector4): void { - const { x, y, z, w } = start; - out.x = x + (end.x - x) * t; - out.y = y + (end.y - y) * t; - out.z = z + (end.z - z) * t; - out.w = w + (end.w - w) * t; + const { _x, _y, _z, _w } = start; + out._x = _x + (end._x - _x) * t; + out._y = _y + (end._y - _y) * t; + out._z = _z + (end._z - _z) * t; + out._w = _w + (end._w - _w) * t; + out._onValueChanged && out._onValueChanged(); } /** @@ -139,10 +144,11 @@ export class Vector4 implements IClone { * @param out - The vector containing the largest components of the specified vectors */ static max(left: Vector4, right: Vector4, out: Vector4): void { - out.x = Math.max(left.x, right.x); - out.y = Math.max(left.y, right.y); - out.z = Math.max(left.z, right.z); - out.w = Math.max(left.w, right.w); + out._x = Math.max(left._x, right._x); + out._y = Math.max(left._y, right._y); + out._z = Math.max(left._z, right._z); + out._w = Math.max(left._w, right._w); + out._onValueChanged && out._onValueChanged(); } /** @@ -152,10 +158,11 @@ export class Vector4 implements IClone { * @param out - The vector containing the smallest components of the specified vectors */ static min(left: Vector4, right: Vector4, out: Vector4): void { - out.x = Math.min(left.x, right.x); - out.y = Math.min(left.y, right.y); - out.z = Math.min(left.z, right.z); - out.w = Math.min(left.w, right.w); + out._x = Math.min(left._x, right._x); + out._y = Math.min(left._y, right._y); + out._z = Math.min(left._z, right._z); + out._w = Math.min(left._w, right._w); + out._onValueChanged && out._onValueChanged(); } /** @@ -164,10 +171,11 @@ export class Vector4 implements IClone { * @param out - The vector facing in the opposite direction */ static negate(a: Vector4, out: Vector4): void { - out.x = -a.x; - out.y = -a.y; - out.z = -a.z; - out.w = -a.w; + out._x = -a._x; + out._y = -a._y; + out._z = -a._z; + out._w = -a._w; + out._onValueChanged && out._onValueChanged(); } /** @@ -176,14 +184,15 @@ export class Vector4 implements IClone { * @param out - The normalized vector */ static normalize(a: Vector4, out: Vector4): void { - const { x, y, z, w } = a; - let len: number = Math.sqrt(x * x + y * y + z * z + w * w); + const { _x, _y, _z, _w } = a; + let len = Math.sqrt(_x * _x + _y * _y + _z * _z + _w * _w); if (len > MathUtil.zeroTolerance) { len = 1 / len; - out.x = x * len; - out.y = y * len; - out.z = z * len; - out.w = w * len; + out._x = _x * len; + out._y = _y * len; + out._z = _z * len; + out._w = _w * len; + out._onValueChanged && out._onValueChanged(); } } @@ -194,10 +203,11 @@ export class Vector4 implements IClone { * @param out - The scaled vector */ static scale(a: Vector4, s: number, out: Vector4): void { - out.x = a.x * s; - out.y = a.y * s; - out.z = a.z * s; - out.w = a.w * s; + out._x = a._x * s; + out._y = a._y * s; + out._z = a._z * s; + out._w = a._w * s; + out._onValueChanged && out._onValueChanged(); } /** @@ -207,12 +217,13 @@ export class Vector4 implements IClone { * @param out - The transformed vector3 */ static transform(v: Vector4, m: Matrix, out: Vector4): void { - const { x, y, z, w } = v; + const { _x, _y, _z, _w } = v; const e = m.elements; - out.x = x * e[0] + y * e[4] + z * e[8] + w * e[12]; - out.y = x * e[1] + y * e[5] + z * e[9] + w * e[13]; - out.z = x * e[2] + y * e[6] + z * e[10] + w * e[14]; - out.w = x * e[3] + y * e[7] + z * e[11] + w * e[15]; + out._x = _x * e[0] + _y * e[4] + _z * e[8] + _w * e[12]; + out._y = _x * e[1] + _y * e[5] + _z * e[9] + _w * e[13]; + out._z = _x * e[2] + _y * e[6] + _z * e[10] + _w * e[14]; + out._w = _x * e[3] + _y * e[7] + _z * e[11] + _w * e[15]; + out._onValueChanged && out._onValueChanged(); } /** @@ -223,10 +234,10 @@ export class Vector4 implements IClone { */ static transformByQuat(v: Vector4, q: Quaternion, out: Vector4): void { const { x, y, z, w } = v; - const qx = q.x; - const qy = q.y; - const qz = q.z; - const qw = q.w; + const qx = q._x; + const qy = q._y; + const qz = q._z; + const qw = q._w; // calculate quat * vec const ix = qw * x + qy * z - qz * y; @@ -235,10 +246,11 @@ export class Vector4 implements IClone { const iw = -qx * x - qy * y - qz * z; // calculate result * inverse quat - out.x = ix * qw - iw * qx - iy * qz + iz * qy; - out.y = iy * qw - iw * qy - iz * qx + ix * qz; - out.z = iz * qw - iw * qz - ix * qy + iy * qx; - out.w = w; + out._x = ix * qw - iw * qx - iy * qz + iz * qy; + out._y = iy * qw - iw * qy - iz * qx + ix * qz; + out._z = iz * qw - iw * qz - ix * qy + iy * qx; + out._w = w; + out._onValueChanged && out._onValueChanged(); } /** @internal */ @@ -308,10 +320,10 @@ export class Vector4 implements IClone { * @param w - The w component of the vector, default 0 */ constructor(x: number = 0, y: number = 0, z: number = 0, w: number = 0) { - this.x = x; - this.y = y; - this.z = z; - this.w = w; + this._x = x; + this._y = y; + this._z = z; + this._w = w; } /** @@ -323,10 +335,11 @@ export class Vector4 implements IClone { * @returns This vector */ setValue(x: number, y: number, z: number, w: number): Vector4 { - this.x = x; - this.y = y; - this.z = z; - this.w = w; + this._x = x; + this._y = y; + this._z = z; + this._w = w; + this._onValueChanged && this._onValueChanged(); return this; } @@ -337,10 +350,11 @@ export class Vector4 implements IClone { * @returns This vector */ setValueByArray(array: ArrayLike, offset: number = 0): Vector4 { - this.x = array[offset]; - this.y = array[offset + 1]; - this.z = array[offset + 2]; - this.w = array[offset + 3]; + this._x = array[offset]; + this._y = array[offset + 1]; + this._z = array[offset + 2]; + this._w = array[offset + 3]; + this._onValueChanged && this._onValueChanged(); return this; } @@ -350,10 +364,11 @@ export class Vector4 implements IClone { * @returns This vector */ add(right: Vector4): Vector4 { - this.x += right.x; - this.y += right.y; - this.z += right.z; - this.w += right.w; + this._x += right.x; + this._y += right.y; + this._z += right.z; + this._w += right.w; + this._onValueChanged && this._onValueChanged(); return this; } @@ -363,10 +378,11 @@ export class Vector4 implements IClone { * @returns This vector */ subtract(right: Vector4): Vector4 { - this.x -= right.x; - this.y -= right.y; - this.z -= right.z; - this.w -= right.w; + this._x -= right.x; + this._y -= right.y; + this._z -= right.z; + this._w -= right.w; + this._onValueChanged && this._onValueChanged(); return this; } @@ -376,10 +392,11 @@ export class Vector4 implements IClone { * @returns This vector */ multiply(right: Vector4): Vector4 { - this.x *= right.x; - this.y *= right.y; - this.z *= right.z; - this.w *= right.w; + this._x *= right.x; + this._y *= right.y; + this._z *= right.z; + this._w *= right.w; + this._onValueChanged && this._onValueChanged(); return this; } @@ -389,10 +406,11 @@ export class Vector4 implements IClone { * @returns This vector */ divide(right: Vector4): Vector4 { - this.x /= right.x; - this.y /= right.y; - this.z /= right.z; - this.w /= right.w; + this._x /= right.x; + this._y /= right.y; + this._z /= right.z; + this._w /= right.w; + this._onValueChanged && this._onValueChanged(); return this; } @@ -401,8 +419,8 @@ export class Vector4 implements IClone { * @returns The length of this vector */ length(): number { - const { x, y, z, w } = this; - return Math.sqrt(x * x + y * y + z * z + w * w); + const { _x, _y, _z, _w } = this; + return Math.sqrt(_x * _x + _y * _y + _z * _z + _w * _w); } /** @@ -410,8 +428,8 @@ export class Vector4 implements IClone { * @returns The squared length of this vector */ lengthSquared(): number { - const { x, y, z, w } = this; - return x * x + y * y + z * z + w * w; + const { _x, _y, _z, _w } = this; + return _x * _x + _y * _y + _z * _z + _w * _w; } /** @@ -419,10 +437,11 @@ export class Vector4 implements IClone { * @returns This vector */ negate(): Vector4 { - this.x = -this.x; - this.y = -this.y; - this.z = -this.z; - this.w = -this.w; + this._x = -this._x; + this._y = -this._y; + this._z = -this._z; + this._w = -this._w; + this._onValueChanged && this._onValueChanged(); return this; } @@ -441,10 +460,11 @@ export class Vector4 implements IClone { * @returns This vector */ scale(s: number): Vector4 { - this.x *= s; - this.y *= s; - this.z *= s; - this.w *= s; + this._x *= s; + this._y *= s; + this._z *= s; + this._w *= s; + this._onValueChanged && this._onValueChanged(); return this; } @@ -454,10 +474,10 @@ export class Vector4 implements IClone { * @param outOffset - The start offset of the array */ toArray(out: number[] | Float32Array | Float64Array, outOffset: number = 0) { - out[outOffset] = this.x; - out[outOffset + 1] = this.y; - out[outOffset + 2] = this.z; - out[outOffset + 3] = this.w; + out[outOffset] = this._x; + out[outOffset + 1] = this._y; + out[outOffset + 2] = this._z; + out[outOffset + 3] = this._w; } /** @@ -465,7 +485,7 @@ export class Vector4 implements IClone { * @returns A clone of this vector */ clone(): Vector4 { - let ret = new Vector4(this.x, this.y, this.z, this.w); + let ret = new Vector4(this._x, this._y, this._z, this._w); return ret; } @@ -475,10 +495,11 @@ export class Vector4 implements IClone { * @returns The specified vector */ cloneTo(out: Vector4): Vector4 { - out.x = this.x; - out.y = this.y; - out.z = this.z; - out.w = this.w; + out._x = this._x; + out._y = this._y; + out._z = this._z; + out._w = this._w; + out._onValueChanged && out._onValueChanged(); return out; } } From 3ac40571ef25cb785d7eb44004303db1675a0ca1 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Wed, 26 Jan 2022 15:12:28 +0800 Subject: [PATCH 13/21] refactor: opt Quaternion --- packages/math/src/Quaternion.ts | 154 +++++++++++++++++--------------- 1 file changed, 83 insertions(+), 71 deletions(-) diff --git a/packages/math/src/Quaternion.ts b/packages/math/src/Quaternion.ts index e254b4a502..caae91def7 100644 --- a/packages/math/src/Quaternion.ts +++ b/packages/math/src/Quaternion.ts @@ -98,10 +98,11 @@ export class Quaternion implements IClone { Vector3.normalize(axis, normalAxis); rad *= 0.5; const s = Math.sin(rad); - out.x = normalAxis.x * s; - out.y = normalAxis.y * s; - out.z = normalAxis.z * s; - out.w = Math.cos(rad); + out._x = normalAxis._x * s; + out._y = normalAxis._y * s; + out._z = normalAxis._z * s; + out._w = Math.cos(rad); + out._onValueChanged && out._onValueChanged(); } /** @@ -137,10 +138,11 @@ export class Quaternion implements IClone { const cosYawPitch = cosYaw * cosPitch; const sinYawPitch = sinYaw * sinPitch; - out.x = cosYaw * sinPitch * cosRoll + sinYaw * cosPitch * sinRoll; - out.y = sinYaw * cosPitch * cosRoll - cosYaw * sinPitch * sinRoll; - out.z = cosYawPitch * sinRoll - sinYawPitch * cosRoll; - out.w = cosYawPitch * cosRoll + sinYawPitch * sinRoll; + out._x = cosYaw * sinPitch * cosRoll + sinYaw * cosPitch * sinRoll; + out._y = sinYaw * cosPitch * cosRoll - cosYaw * sinPitch * sinRoll; + out._z = cosYawPitch * sinRoll - sinYawPitch * cosRoll; + out._w = cosYawPitch * cosRoll + sinYawPitch * sinRoll; + out._onValueChanged && out._onValueChanged(); } /** @@ -207,10 +209,11 @@ export class Quaternion implements IClone { const dot = x * x + y * y + z * z + w * w; if (dot > MathUtil.zeroTolerance) { const invDot = 1.0 / dot; - out.x = -x * invDot; - out.y = -y * invDot; - out.z = -z * invDot; - out.w = w * invDot; + out._x = -x * invDot; + out._y = -y * invDot; + out._z = -z * invDot; + out._w = w * invDot; + out._onValueChanged && out._onValueChanged(); } } @@ -224,15 +227,15 @@ export class Quaternion implements IClone { static lerp(start: Quaternion, end: Quaternion, t: number, out: Quaternion): void { const inv = 1.0 - t; if (Quaternion.dot(start, end) >= 0) { - out.x = start.x * inv + end.x * t; - out.y = start.y * inv + end.y * t; - out.z = start.z * inv + end.z * t; - out.w = start.w * inv + end.w * t; + out._x = start._x * inv + end._x * t; + out._y = start._y * inv + end._y * t; + out._z = start._z * inv + end._z * t; + out._w = start._w * inv + end._w * t; } else { - out.x = start.x * inv - end.x * t; - out.y = start.y * inv - end.y * t; - out.z = start.z * inv - end.z * t; - out.w = start.w * inv - end.w * t; + out._x = start._x * inv - end._x * t; + out._y = start._y * inv - end._y * t; + out._z = start._z * inv - end._z * t; + out._w = start._w * inv - end._w * t; } out.normalize(); @@ -246,16 +249,16 @@ export class Quaternion implements IClone { * @param out - The result of spherical linear blending between two quaternions */ static slerp(start: Quaternion, end: Quaternion, t: number, out: Quaternion): void { - const ax = start.x; - const ay = start.y; - const az = start.z; - const aw = start.w; - let bx = end.x; - let by = end.y; - let bz = end.z; - let bw = end.w; - - let scale0, scale1; + const ax = start._x; + const ay = start._y; + const az = start._z; + const aw = start._w; + let bx = end._x; + let by = end._y; + let bz = end._z; + let bw = end._w; + + let scale0: number, scale1: number; // calc cosine let cosom = ax * bx + ay * by + az * bz + aw * bw; // adjust signs (if necessary) @@ -280,10 +283,11 @@ export class Quaternion implements IClone { scale1 = t; } // calculate final values - out.x = scale0 * ax + scale1 * bx; - out.y = scale0 * ay + scale1 * by; - out.z = scale0 * az + scale1 * bz; - out.w = scale0 * aw + scale1 * bw; + out._x = scale0 * ax + scale1 * bx; + out._y = scale0 * ay + scale1 * by; + out._z = scale0 * az + scale1 * bz; + out._w = scale0 * aw + scale1 * bw; + out._onValueChanged && out._onValueChanged(); } /** @@ -292,14 +296,15 @@ export class Quaternion implements IClone { * @param out - The normalized quaternion */ static normalize(a: Quaternion, out: Quaternion): void { - const { x, y, z, w } = a; - let len: number = Math.sqrt(x * x + y * y + z * z + w * w); + const { _x, _y, _z, _w } = a; + let len = Math.sqrt(_x * _x + _y * _y + _z * _z + _w * _w); if (len > MathUtil.zeroTolerance) { len = 1 / len; - out.x = x * len; - out.y = y * len; - out.z = z * len; - out.w = w * len; + out._x = _x * len; + out._y = _y * len; + out._z = _z * len; + out._w = _w * len; + out._onValueChanged && out._onValueChanged(); } } @@ -313,10 +318,11 @@ export class Quaternion implements IClone { const s = Math.sin(rad); const c = Math.cos(rad); - out.x = s; - out.y = 0; - out.z = 0; - out.w = c; + out._x = s; + out._y = 0; + out._z = 0; + out._w = c; + out._onValueChanged && out._onValueChanged(); } /** @@ -329,10 +335,11 @@ export class Quaternion implements IClone { const s = Math.sin(rad); const c = Math.cos(rad); - out.x = 0; - out.y = s; - out.z = 0; - out.w = c; + out._x = 0; + out._y = s; + out._z = 0; + out._w = c; + out._onValueChanged && out._onValueChanged(); } /** @@ -345,10 +352,11 @@ export class Quaternion implements IClone { const s = Math.sin(rad); const c = Math.cos(rad); - out.x = 0; - out.y = 0; - out.z = s; - out.w = c; + out._x = 0; + out._y = 0; + out._z = s; + out._w = c; + out._onValueChanged && out._onValueChanged(); } /** @@ -358,15 +366,16 @@ export class Quaternion implements IClone { * @param out - The calculated quaternion */ static rotateX(quaternion: Quaternion, rad: number, out: Quaternion): void { - const { x, y, z, w } = quaternion; + const { _x, _y, _z, _w } = quaternion; rad *= 0.5; const bx = Math.sin(rad); const bw = Math.cos(rad); - out.x = x * bw + w * bx; - out.y = y * bw + z * bx; - out.z = z * bw - y * bx; - out.w = w * bw - x * bx; + out._x = _x * bw + _w * bx; + out._y = _y * bw + _z * bx; + out._z = _z * bw - _y * bx; + out._w = _w * bw - _x * bx; + out._onValueChanged && out._onValueChanged(); } /** @@ -376,15 +385,16 @@ export class Quaternion implements IClone { * @param out - The calculated quaternion */ static rotateY(quaternion: Quaternion, rad: number, out: Quaternion): void { - const { x, y, z, w } = quaternion; + const { _x, _y, _z, _w } = quaternion; rad *= 0.5; const by = Math.sin(rad); const bw = Math.cos(rad); - out.x = x * bw - z * by; - out.y = y * bw + w * by; - out.z = z * bw + x * by; - out.w = w * bw - y * by; + out.x = _x * bw - _z * by; + out.y = _y * bw + _w * by; + out.z = _z * bw + _x * by; + out.w = _w * bw - _y * by; + out._onValueChanged && out._onValueChanged(); } /** @@ -394,15 +404,16 @@ export class Quaternion implements IClone { * @param out - The calculated quaternion */ static rotateZ(quaternion: Quaternion, rad: number, out: Quaternion): void { - const { x, y, z, w } = quaternion; + const { _x, _y, _z, _w } = quaternion; rad *= 0.5; const bz = Math.sin(rad); const bw = Math.cos(rad); - out.x = x * bw + y * bz; - out.y = y * bw - x * bz; - out.z = z * bw + w * bz; - out.w = w * bw - z * bz; + out.x = _x * bw + _y * bz; + out.y = _y * bw - _x * bz; + out.z = _z * bw + _w * bz; + out.w = _w * bw - _z * bz; + out._onValueChanged && out._onValueChanged(); } /** @@ -412,10 +423,11 @@ export class Quaternion implements IClone { * @param out - The scaled quaternion */ static scale(a: Quaternion, s: number, out: Quaternion): void { - out.x = a.x * s; - out.y = a.y * s; - out.z = a.z * s; - out.w = a.w * s; + out._x = a._x * s; + out._y = a._y * s; + out._z = a._z * s; + out._w = a._w * s; + out._onValueChanged && out._onValueChanged(); } /** @internal */ From a128998c6e43875df26dbb73a11dcf836245dc9b Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Wed, 26 Jan 2022 15:25:34 +0800 Subject: [PATCH 14/21] refactor: opt code --- packages/math/src/Quaternion.ts | 54 ++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/packages/math/src/Quaternion.ts b/packages/math/src/Quaternion.ts index caae91def7..2282490e5d 100644 --- a/packages/math/src/Quaternion.ts +++ b/packages/math/src/Quaternion.ts @@ -166,37 +166,38 @@ export class Quaternion implements IClone { if (scale > 0) { sqrt = Math.sqrt(scale + 1.0); - out.w = sqrt * 0.5; + out._w = sqrt * 0.5; sqrt = 0.5 / sqrt; - out.x = (m23 - m32) * sqrt; - out.y = (m31 - m13) * sqrt; - out.z = (m12 - m21) * sqrt; + out._x = (m23 - m32) * sqrt; + out._y = (m31 - m13) * sqrt; + out._z = (m12 - m21) * sqrt; } else if (m11 >= m22 && m11 >= m33) { sqrt = Math.sqrt(1.0 + m11 - m22 - m33); half = 0.5 / sqrt; - out.x = 0.5 * sqrt; - out.y = (m12 + m21) * half; - out.z = (m13 + m31) * half; - out.w = (m23 - m32) * half; + out._x = 0.5 * sqrt; + out._y = (m12 + m21) * half; + out._z = (m13 + m31) * half; + out._w = (m23 - m32) * half; } else if (m22 > m33) { sqrt = Math.sqrt(1.0 + m22 - m11 - m33); half = 0.5 / sqrt; - out.x = (m21 + m12) * half; - out.y = 0.5 * sqrt; - out.z = (m32 + m23) * half; - out.w = (m31 - m13) * half; + out._x = (m21 + m12) * half; + out._y = 0.5 * sqrt; + out._z = (m32 + m23) * half; + out._w = (m31 - m13) * half; } else { sqrt = Math.sqrt(1.0 + m33 - m11 - m22); half = 0.5 / sqrt; - out.x = (m13 + m31) * half; - out.y = (m23 + m32) * half; - out.z = 0.5 * sqrt; - out.w = (m12 - m21) * half; + out._x = (m13 + m31) * half; + out._y = (m23 + m32) * half; + out._z = 0.5 * sqrt; + out._w = (m12 - m21) * half; } + out._onValueChanged && out._onValueChanged(); } /** @@ -390,10 +391,10 @@ export class Quaternion implements IClone { const by = Math.sin(rad); const bw = Math.cos(rad); - out.x = _x * bw - _z * by; - out.y = _y * bw + _w * by; - out.z = _z * bw + _x * by; - out.w = _w * bw - _y * by; + out._x = _x * bw - _z * by; + out._y = _y * bw + _w * by; + out._z = _z * bw + _x * by; + out._w = _w * bw - _y * by; out._onValueChanged && out._onValueChanged(); } @@ -409,10 +410,10 @@ export class Quaternion implements IClone { const bz = Math.sin(rad); const bw = Math.cos(rad); - out.x = _x * bw + _y * bz; - out.y = _y * bw - _x * bz; - out.z = _z * bw + _w * bz; - out.w = _w * bw - _z * bz; + out._x = _x * bw + _y * bz; + out._y = _y * bw - _x * bz; + out._z = _z * bw + _w * bz; + out._w = _w * bw - _z * bz; out._onValueChanged && out._onValueChanged(); } @@ -619,10 +620,13 @@ export class Quaternion implements IClone { */ toEuler(out: Vector3): Vector3 { this.toYawPitchRoll(out); - // @todo: + const t = out._x; out._x = out._y; out._y = t; + + // @todo: + out._onValueChanged && out._onValueChanged(); return out; } From 1ee1b946459301e6775b0efd6088d11d2f095fa1 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Wed, 26 Jan 2022 15:46:06 +0800 Subject: [PATCH 15/21] refactor: opt code --- packages/core/src/Transform.ts | 1 - packages/math/src/Matrix.ts | 41 +++++++++++++++++----------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/packages/core/src/Transform.ts b/packages/core/src/Transform.ts index b7d3d46572..5fb02e8d86 100644 --- a/packages/core/src/Transform.ts +++ b/packages/core/src/Transform.ts @@ -533,7 +533,6 @@ export class Transform extends Component { worldUp = worldUp ?? Transform._tempVec3.setValue(0, 1, 0); Matrix.lookAt(position, worldPosition, worldUp, rotMat); rotMat.getRotation(worldRotationQuaternion).invert(); - this.worldRotationQuaternion = worldRotationQuaternion; } /** diff --git a/packages/math/src/Matrix.ts b/packages/math/src/Matrix.ts index cc5b93ba2b..c52e84c7fd 100644 --- a/packages/math/src/Matrix.ts +++ b/packages/math/src/Matrix.ts @@ -1117,31 +1117,32 @@ export class Matrix implements IClone { let trace = e[0] + e[5] + e[10]; if (trace > MathUtil.zeroTolerance) { - let S = Math.sqrt(trace + 1.0) * 2; - out.w = 0.25 * S; - out.x = (e[6] - e[9]) / S; - out.y = (e[8] - e[2]) / S; - out.z = (e[1] - e[4]) / S; + let s = Math.sqrt(trace + 1.0) * 2; + out._w = 0.25 * s; + out._x = (e[6] - e[9]) / s; + out._y = (e[8] - e[2]) / s; + out._z = (e[1] - e[4]) / s; } else if (e[0] > e[5] && e[0] > e[10]) { - let S = Math.sqrt(1.0 + e[0] - e[5] - e[10]) * 2; - out.w = (e[6] - e[9]) / S; - out.x = 0.25 * S; - out.y = (e[1] + e[4]) / S; - out.z = (e[8] + e[2]) / S; + let s = Math.sqrt(1.0 + e[0] - e[5] - e[10]) * 2; + out._w = (e[6] - e[9]) / s; + out._x = 0.25 * s; + out._y = (e[1] + e[4]) / s; + out._z = (e[8] + e[2]) / s; } else if (e[5] > e[10]) { - let S = Math.sqrt(1.0 + e[5] - e[0] - e[10]) * 2; - out.w = (e[8] - e[2]) / S; - out.x = (e[1] + e[4]) / S; - out.y = 0.25 * S; - out.z = (e[6] + e[9]) / S; + let s = Math.sqrt(1.0 + e[5] - e[0] - e[10]) * 2; + out._w = (e[8] - e[2]) / s; + out._x = (e[1] + e[4]) / s; + out._y = 0.25 * s; + out._z = (e[6] + e[9]) / s; } else { - let S = Math.sqrt(1.0 + e[10] - e[0] - e[5]) * 2; - out.w = (e[1] - e[4]) / S; - out.x = (e[8] + e[2]) / S; - out.y = (e[6] + e[9]) / S; - out.z = 0.25 * S; + let s = Math.sqrt(1.0 + e[10] - e[0] - e[5]) * 2; + out._w = (e[1] - e[4]) / s; + out._x = (e[8] + e[2]) / s; + out._y = (e[6] + e[9]) / s; + out._z = 0.25 * s; } + out._onValueChanged && out._onValueChanged(); return out; } From ca07821c7fab7828b43bfb84febf1ee2497684ae Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Wed, 26 Jan 2022 17:27:46 +0800 Subject: [PATCH 16/21] refactor: opt code --- packages/core/src/Transform.ts | 7 ------- 1 file changed, 7 deletions(-) diff --git a/packages/core/src/Transform.ts b/packages/core/src/Transform.ts index 5fb02e8d86..191af0b11a 100644 --- a/packages/core/src/Transform.ts +++ b/packages/core/src/Transform.ts @@ -49,7 +49,6 @@ export class Transform extends Component { /** * Local position. - * @remarks Need to re-assign after modification to ensure that the modification takes effect. */ get position(): Vector3 { return this._position; @@ -63,7 +62,6 @@ export class Transform extends Component { /** * World position. - * @remarks Need to re-assign after modification to ensure that the modification takes effect. */ get worldPosition(): Vector3 { if (this._isContainDirtyFlag(TransformFlag.WorldPosition)) { @@ -86,7 +84,6 @@ export class Transform extends Component { /** * Local rotation, defining the rotation value in degrees. * Rotations are performed around the Y axis, the X axis, and the Z axis, in that order. - * @remarks Need to re-assign after modification to ensure that the modification takes effect. */ get rotation(): Vector3 { if (this._isContainDirtyFlag(TransformFlag.LocalEuler)) { @@ -106,7 +103,6 @@ export class Transform extends Component { /** * World rotation, defining the rotation value in degrees. * Rotations are performed around the Y axis, the X axis, and the Z axis, in that order. - * @remarks Need to re-assign after modification to ensure that the modification takes effect. */ get worldRotation(): Vector3 { if (this._isContainDirtyFlag(TransformFlag.WorldEuler)) { @@ -125,7 +121,6 @@ export class Transform extends Component { /** * Local rotation, defining the rotation by using a unit quaternion. - * @remarks Need to re-assign after modification to ensure that the modification takes effect. */ get rotationQuaternion(): Quaternion { if (this._isContainDirtyFlag(TransformFlag.LocalQuat)) { @@ -148,7 +143,6 @@ export class Transform extends Component { /** * World rotation, defining the rotation by using a unit quaternion. - * @remarks Need to re-assign after modification to ensure that the modification takes effect. */ get worldRotationQuaternion(): Quaternion { if (this._isContainDirtyFlag(TransformFlag.WorldQuat)) { @@ -171,7 +165,6 @@ export class Transform extends Component { /** * Local scaling. - * @remarks Need to re-assign after modification to ensure that the modification takes effect. */ get scale(): Vector3 { return this._scale; From 26cedd1f14217c3dfc39cb556d21f2127e1330b0 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Thu, 27 Jan 2022 17:27:31 +0800 Subject: [PATCH 17/21] refactor: opt math --- packages/math/src/Quaternion.ts | 48 +++++++++++++++++---------------- packages/math/src/Vector3.ts | 9 ++++--- packages/math/src/Vector4.ts | 32 +++++++++++----------- 3 files changed, 46 insertions(+), 43 deletions(-) diff --git a/packages/math/src/Quaternion.ts b/packages/math/src/Quaternion.ts index 2282490e5d..e2385b3f82 100644 --- a/packages/math/src/Quaternion.ts +++ b/packages/math/src/Quaternion.ts @@ -619,13 +619,11 @@ export class Quaternion implements IClone { * @returns Euler x->pitch y->yaw z->roll */ toEuler(out: Vector3): Vector3 { - this.toYawPitchRoll(out); + this._toYawPitchRoll(out); const t = out._x; out._x = out._y; out._y = t; - - // @todo: out._onValueChanged && out._onValueChanged(); return out; } @@ -636,26 +634,7 @@ export class Quaternion implements IClone { * @returns Euler x->yaw y->pitch z->roll */ toYawPitchRoll(out: Vector3): Vector3 { - const { _x, _y, _z, _w } = this; - const xx = _x * _x; - const yy = _y * _y; - const zz = _z * _z; - const xy = _x * _y; - const zw = _z * _w; - const zx = _z * _x; - const yw = _y * _w; - const yz = _y * _z; - const xw = _x * _w; - - out._y = Math.asin(2.0 * (xw - yz)); - if (Math.cos(out.y) > MathUtil.zeroTolerance) { - out._z = Math.atan2(2.0 * (xy + zw), 1.0 - 2.0 * (zz + xx)); - out._x = Math.atan2(2.0 * (zx + yw), 1.0 - 2.0 * (yy + xx)); - } else { - out._z = Math.atan2(-2.0 * (xy - zw), 1.0 - 2.0 * (yy + zz)); - out._x = 0.0; - } - + this._toYawPitchRoll(out); out._onValueChanged && out._onValueChanged(); return out; } @@ -785,4 +764,27 @@ export class Quaternion implements IClone { this.multiply(Quaternion._tempQuat1); return this; } + + private _toYawPitchRoll(out: Vector3): Vector3 { + const { _x, _y, _z, _w } = this; + const xx = _x * _x; + const yy = _y * _y; + const zz = _z * _z; + const xy = _x * _y; + const zw = _z * _w; + const zx = _z * _x; + const yw = _y * _w; + const yz = _y * _z; + const xw = _x * _w; + + out._y = Math.asin(2.0 * (xw - yz)); + if (Math.cos(out.y) > MathUtil.zeroTolerance) { + out._z = Math.atan2(2.0 * (xy + zw), 1.0 - 2.0 * (zz + xx)); + out._x = Math.atan2(2.0 * (zx + yw), 1.0 - 2.0 * (yy + xx)); + } else { + out._z = Math.atan2(-2.0 * (xy - zw), 1.0 - 2.0 * (yy + zz)); + out._x = 0.0; + } + return out; + } } diff --git a/packages/math/src/Vector3.ts b/packages/math/src/Vector3.ts index 3e83116c6a..7c2b70d2f7 100644 --- a/packages/math/src/Vector3.ts +++ b/packages/math/src/Vector3.ts @@ -295,7 +295,7 @@ export class Vector3 implements IClone { */ static transformByQuat(v: Vector3, quaternion: Quaternion, out: Vector3): void { const { _x, _y, _z } = v; - const { x: qx, y: qy, z: qz, w: qw } = quaternion; + const { _x: qx, _y: qy, _z: qz, _w: qw } = quaternion; // calculate quat * vec const ix = qw * _x + qy * _z - qz * _y; @@ -389,9 +389,10 @@ export class Vector3 implements IClone { * @returns This vector */ setValueByArray(array: ArrayLike, offset: number = 0): Vector3 { - this.x = array[offset]; - this.y = array[offset + 1]; - this.z = array[offset + 2]; + this._x = array[offset]; + this._y = array[offset + 1]; + this._z = array[offset + 2]; + this._onValueChanged && this._onValueChanged(); return this; } diff --git a/packages/math/src/Vector4.ts b/packages/math/src/Vector4.ts index 9fd44527a0..972913e22d 100644 --- a/packages/math/src/Vector4.ts +++ b/packages/math/src/Vector4.ts @@ -364,10 +364,10 @@ export class Vector4 implements IClone { * @returns This vector */ add(right: Vector4): Vector4 { - this._x += right.x; - this._y += right.y; - this._z += right.z; - this._w += right.w; + this._x += right._x; + this._y += right._y; + this._z += right._z; + this._w += right._w; this._onValueChanged && this._onValueChanged(); return this; } @@ -378,10 +378,10 @@ export class Vector4 implements IClone { * @returns This vector */ subtract(right: Vector4): Vector4 { - this._x -= right.x; - this._y -= right.y; - this._z -= right.z; - this._w -= right.w; + this._x -= right._x; + this._y -= right._y; + this._z -= right._z; + this._w -= right._w; this._onValueChanged && this._onValueChanged(); return this; } @@ -392,10 +392,10 @@ export class Vector4 implements IClone { * @returns This vector */ multiply(right: Vector4): Vector4 { - this._x *= right.x; - this._y *= right.y; - this._z *= right.z; - this._w *= right.w; + this._x *= right._x; + this._y *= right._y; + this._z *= right._z; + this._w *= right._w; this._onValueChanged && this._onValueChanged(); return this; } @@ -406,10 +406,10 @@ export class Vector4 implements IClone { * @returns This vector */ divide(right: Vector4): Vector4 { - this._x /= right.x; - this._y /= right.y; - this._z /= right.z; - this._w /= right.w; + this._x /= right._x; + this._y /= right._y; + this._z /= right._z; + this._w /= right._w; this._onValueChanged && this._onValueChanged(); return this; } From a602d896ee210aaf74e9ce5a51e227abb18abf92 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Thu, 27 Jan 2022 17:47:55 +0800 Subject: [PATCH 18/21] refactor: opt transform --- packages/core/src/Transform.ts | 7 ------- 1 file changed, 7 deletions(-) diff --git a/packages/core/src/Transform.ts b/packages/core/src/Transform.ts index 191af0b11a..226f9becdf 100644 --- a/packages/core/src/Transform.ts +++ b/packages/core/src/Transform.ts @@ -328,7 +328,6 @@ export class Transform extends Component { */ setPosition(x: number, y: number, z: number): void { this._position.setValue(x, y, z); - this.position = this._position; } /** @@ -340,7 +339,6 @@ export class Transform extends Component { */ setRotation(x: number, y: number, z: number): void { this._rotation.setValue(x, y, z); - this.rotation = this._rotation; } /** @@ -352,7 +350,6 @@ export class Transform extends Component { */ setRotationQuaternion(x: number, y: number, z: number, w: number): void { this._rotationQuaternion.setValue(x, y, z, w); - this.rotationQuaternion = this._rotationQuaternion; } /** @@ -363,7 +360,6 @@ export class Transform extends Component { */ setScale(x: number, y: number, z: number): void { this._scale.setValue(x, y, z); - this.scale = this._scale; } /** @@ -374,7 +370,6 @@ export class Transform extends Component { */ setWorldPosition(x: number, y: number, z: number): void { this._worldPosition.setValue(x, y, z); - this.worldPosition = this._worldPosition; } /** @@ -385,7 +380,6 @@ export class Transform extends Component { */ setWorldRotation(x: number, y: number, z: number): void { this._worldRotation.setValue(x, y, z); - this.worldRotation = this._worldRotation; } /** @@ -397,7 +391,6 @@ export class Transform extends Component { */ setWorldRotationQuaternion(x: number, y: number, z: number, w: number): void { this._worldRotationQuaternion.setValue(x, y, z, w); - this.worldRotationQuaternion = this._worldRotationQuaternion; } /** From e444be203039c4a97529076f144ca77ba34a1b5a Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Thu, 27 Jan 2022 18:43:06 +0800 Subject: [PATCH 19/21] refactor: opt code --- packages/core/src/Transform.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/core/src/Transform.ts b/packages/core/src/Transform.ts index 226f9becdf..99091e5bdf 100644 --- a/packages/core/src/Transform.ts +++ b/packages/core/src/Transform.ts @@ -707,9 +707,9 @@ export class Transform extends Component { private _translate(translation: Vector3, relativeToLocal: boolean = true): void { if (relativeToLocal) { - this.position = this._position.add(translation); + this._position.add(translation); } else { - this.worldPosition = this._worldPosition.add(translation); + this._worldPosition.add(translation); } } From eb5563cd382dcc352df09da5cffed2db4b70e127 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Fri, 28 Jan 2022 13:43:11 +0800 Subject: [PATCH 20/21] fix: clear dirty code --- packages/core/src/Transform.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/core/src/Transform.ts b/packages/core/src/Transform.ts index 99091e5bdf..cf3edd0780 100644 --- a/packages/core/src/Transform.ts +++ b/packages/core/src/Transform.ts @@ -268,7 +268,6 @@ export class Transform extends Component { } else { worldPosition.cloneTo(this._position); } - this.position = this._position; this._setDirtyFlagFalse(TransformFlag.WorldPosition); }; @@ -288,7 +287,6 @@ export class Transform extends Component { MathUtil.degreeToRadian(worldRotation.z), this._worldRotationQuaternion ); - this.worldRotationQuaternion = this._worldRotationQuaternion; this._setDirtyFlagFalse(TransformFlag.WorldEuler); }; @@ -309,7 +307,6 @@ export class Transform extends Component { } else { worldRotationQuaternion.cloneTo(this._rotationQuaternion); } - this.rotationQuaternion = this._rotationQuaternion; this._setDirtyFlagFalse(TransformFlag.WorldQuat); }; From 75c5eeb0fc561451e021be98b9358e1f88856092 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Wed, 2 Mar 2022 16:25:36 +0800 Subject: [PATCH 21/21] fix: opt transformToVec4 --- packages/math/src/Vector3.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/math/src/Vector3.ts b/packages/math/src/Vector3.ts index 7c2b70d2f7..a28a47ec15 100644 --- a/packages/math/src/Vector3.ts +++ b/packages/math/src/Vector3.ts @@ -256,10 +256,11 @@ export class Vector3 implements IClone { static transformToVec4(v: Vector3, m: Matrix, out: Vector4): void { const { _x, _y, _z } = v; const e = m.elements; - out.x = _x * e[0] + _y * e[4] + _z * e[8] + e[12]; - out.y = _x * e[1] + _y * e[5] + _z * e[9] + e[13]; - out.z = _x * e[2] + _y * e[6] + _z * e[10] + e[14]; - out.w = _x * e[3] + _y * e[7] + _z * e[11] + e[15]; + out._x = _x * e[0] + _y * e[4] + _z * e[8] + e[12]; + out._y = _x * e[1] + _y * e[5] + _z * e[9] + e[13]; + out._z = _x * e[2] + _y * e[6] + _z * e[10] + e[14]; + out._w = _x * e[3] + _y * e[7] + _z * e[11] + e[15]; + out._onValueChanged && out._onValueChanged(); } /**