From 2eccca9302c04acb18e96739b631ab7d447c0902 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Thu, 17 Feb 2022 16:15:38 +0800 Subject: [PATCH 01/20] fix: normal attributer is need in unlit (#637) * fix: normal attributer is need in unlit --- .../core/src/shaderlib/blendShape_vert.glsl | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/packages/core/src/shaderlib/blendShape_vert.glsl b/packages/core/src/shaderlib/blendShape_vert.glsl index bf83659c8d..237371e07d 100644 --- a/packages/core/src/shaderlib/blendShape_vert.glsl +++ b/packages/core/src/shaderlib/blendShape_vert.glsl @@ -7,18 +7,20 @@ position.xyz += POSITION_BS2 * u_blendShapeWeights[2]; position.xyz += POSITION_BS3 * u_blendShapeWeights[3]; - #if defined( O3_HAS_NORMAL ) && defined( OASIS_BLENDSHAPE_NORMAL ) - normal.xyz += NORMAL_BS0 * u_blendShapeWeights[0]; - normal.xyz += NORMAL_BS1 * u_blendShapeWeights[1]; - normal.xyz += NORMAL_BS2 * u_blendShapeWeights[2]; - normal.xyz += NORMAL_BS3 * u_blendShapeWeights[3]; - #endif + #ifndef OMIT_NORMAL + #if defined( O3_HAS_NORMAL ) && defined( OASIS_BLENDSHAPE_NORMAL ) + normal.xyz += NORMAL_BS0 * u_blendShapeWeights[0]; + normal.xyz += NORMAL_BS1 * u_blendShapeWeights[1]; + normal.xyz += NORMAL_BS2 * u_blendShapeWeights[2]; + normal.xyz += NORMAL_BS3 * u_blendShapeWeights[3]; + #endif - #if defined( O3_HAS_TANGENT ) && defined( O3_NORMAL_TEXTURE ) && defined(OASIS_BLENDSHAPE_TANGENT) - tangent.xyz += TANGENT_BS0 * u_blendShapeWeights[0]; - tangent.xyz += TANGENT_BS1 * u_blendShapeWeights[1]; - tangent.xyz += TANGENT_BS2 * u_blendShapeWeights[2]; - tangent.xyz += TANGENT_BS3 * u_blendShapeWeights[3]; + #if defined( O3_HAS_TANGENT ) && defined( O3_NORMAL_TEXTURE ) && defined(OASIS_BLENDSHAPE_TANGENT) + tangent.xyz += TANGENT_BS0 * u_blendShapeWeights[0]; + tangent.xyz += TANGENT_BS1 * u_blendShapeWeights[1]; + tangent.xyz += TANGENT_BS2 * u_blendShapeWeights[2]; + tangent.xyz += TANGENT_BS3 * u_blendShapeWeights[3]; + #endif #endif #endif -#endif \ No newline at end of file +#endif From b28c936db920b19e3a665a461929c6f3c1777dea Mon Sep 17 00:00:00 2001 From: yangfengzzz Date: Thu, 17 Feb 2022 17:17:11 +0800 Subject: [PATCH 02/20] fix: physx bug (#651) * fix: physx bug * fix: opt collider shape scale --- .../core/src/physics/shape/CapsuleColliderShape.ts | 1 + .../src/shape/PhysXBoxColliderShape.ts | 4 +++- .../src/shape/PhysXCapsuleColliderShape.ts | 6 ++++-- .../physics-physx/src/shape/PhysXColliderShape.ts | 14 ++++++++------ .../src/shape/PhysXPlaneColliderShape.ts | 9 ++++++--- .../src/shape/PhysXSphereColliderShape.ts | 4 +++- 6 files changed, 25 insertions(+), 13 deletions(-) diff --git a/packages/core/src/physics/shape/CapsuleColliderShape.ts b/packages/core/src/physics/shape/CapsuleColliderShape.ts index ee34e86f09..0fb253d570 100644 --- a/packages/core/src/physics/shape/CapsuleColliderShape.ts +++ b/packages/core/src/physics/shape/CapsuleColliderShape.ts @@ -53,5 +53,6 @@ export class CapsuleColliderShape extends ColliderShape { this._height, this._material._nativeMaterial ); + (this._nativeShape).setUpAxis(ColliderShapeUpAxis.Y); } } diff --git a/packages/physics-physx/src/shape/PhysXBoxColliderShape.ts b/packages/physics-physx/src/shape/PhysXBoxColliderShape.ts index 65cb0c0d8f..08b430e622 100644 --- a/packages/physics-physx/src/shape/PhysXBoxColliderShape.ts +++ b/packages/physics-physx/src/shape/PhysXBoxColliderShape.ts @@ -28,7 +28,7 @@ export class PhysXBoxColliderShape extends PhysXColliderShape implements IBoxCol this._halfSize.z * this._scale.z ); this._allocShape(material); - this._setLocalPose(); + this._setLocalPose(this._scale); this.setUniqueID(uniqueID); } @@ -46,6 +46,8 @@ export class PhysXBoxColliderShape extends PhysXColliderShape implements IBoxCol * {@inheritDoc IColliderShape.setWorldScale } */ setWorldScale(scale: Vector3): void { + this._setLocalPose(this._scale); + scale.cloneTo(this._scale); Vector3.multiply(this._halfSize, this._scale, PhysXBoxColliderShape._tempHalfExtents); this._pxGeometry.halfExtents = PhysXBoxColliderShape._tempHalfExtents; diff --git a/packages/physics-physx/src/shape/PhysXCapsuleColliderShape.ts b/packages/physics-physx/src/shape/PhysXCapsuleColliderShape.ts index b3e373b831..46fbce4e49 100644 --- a/packages/physics-physx/src/shape/PhysXCapsuleColliderShape.ts +++ b/packages/physics-physx/src/shape/PhysXCapsuleColliderShape.ts @@ -27,7 +27,7 @@ export class PhysXCapsuleColliderShape extends PhysXColliderShape implements ICa this._pxGeometry = new PhysXPhysics._physX.PxCapsuleGeometry(this._radius, this._halfHeight); this._allocShape(material); - this._setLocalPose(); + this._setLocalPose(this._scale); this.setUniqueID(uniqueID); } @@ -85,13 +85,15 @@ export class PhysXCapsuleColliderShape extends PhysXColliderShape implements ICa this._rotation.setValue(0, PhysXColliderShape.halfSqrt, 0, PhysXColliderShape.halfSqrt); break; } - this._setLocalPose(); + this._setLocalPose(this._scale); } /** * {@inheritDoc IColliderShape.setWorldScale } */ setWorldScale(scale: Vector3): void { + this._setLocalPose(this._scale); + switch (this._upAxis) { case ColliderShapeUpAxis.X: this._pxGeometry.radius = this._radius * Math.max(scale.y, scale.z); diff --git a/packages/physics-physx/src/shape/PhysXColliderShape.ts b/packages/physics-physx/src/shape/PhysXColliderShape.ts index d72b3f9e82..0ae06697e6 100644 --- a/packages/physics-physx/src/shape/PhysXColliderShape.ts +++ b/packages/physics-physx/src/shape/PhysXColliderShape.ts @@ -21,11 +21,11 @@ export enum ShapeFlag { export abstract class PhysXColliderShape implements IColliderShape { static readonly halfSqrt: number = 0.70710678118655; static transform = { - translation: null, + translation: new Vector3(), rotation: null }; protected _position: Vector3 = new Vector3(); - protected _rotation: Quaternion = new Quaternion(0, 0, PhysXColliderShape.halfSqrt, PhysXColliderShape.halfSqrt); + protected _rotation: Quaternion = new Quaternion(); protected _scale: Vector3 = new Vector3(1, 1, 1); private _shapeFlags: ShapeFlag = ShapeFlag.SCENE_QUERY_SHAPE | ShapeFlag.SIMULATION_SHAPE; @@ -42,8 +42,10 @@ export abstract class PhysXColliderShape implements IColliderShape { * {@inheritDoc IColliderShape.setPosition } */ setPosition(value: Vector3): void { - value.cloneTo(this._position); - this._setLocalPose(); + if (value !== this._position) { + value.cloneTo(this._position); + } + this._setLocalPose(this._scale); } /** @@ -92,9 +94,9 @@ export abstract class PhysXColliderShape implements IColliderShape { this._pxShape.setFlags(new PhysXPhysics._physX.PxShapeFlags(this._shapeFlags)); } - protected _setLocalPose(): void { + protected _setLocalPose(scale: Vector3): void { const transform = PhysXColliderShape.transform; - transform.translation = this._position; + Vector3.multiply(this._position, scale, transform.translation); transform.rotation = this._rotation; this._pxShape.setLocalPose(PhysXColliderShape.transform); } diff --git a/packages/physics-physx/src/shape/PhysXPlaneColliderShape.ts b/packages/physics-physx/src/shape/PhysXPlaneColliderShape.ts index 9dd59e8e3c..5c78fba4a3 100644 --- a/packages/physics-physx/src/shape/PhysXPlaneColliderShape.ts +++ b/packages/physics-physx/src/shape/PhysXPlaneColliderShape.ts @@ -15,10 +15,11 @@ export class PhysXPlaneColliderShape extends PhysXColliderShape implements IPlan */ constructor(uniqueID: number, material: PhysXPhysicsMaterial) { super(); + this._rotation.setValue(0, 0, PhysXColliderShape.halfSqrt, PhysXColliderShape.halfSqrt); this._pxGeometry = new PhysXPhysics._physX.PxPlaneGeometry(); this._allocShape(material); - this._setLocalPose(); + this._setLocalPose(this._scale); this.setUniqueID(uniqueID); } @@ -29,11 +30,13 @@ export class PhysXPlaneColliderShape extends PhysXColliderShape implements IPlan Quaternion.rotationYawPitchRoll(value.x, value.y, value.z, this._rotation); Quaternion.rotateZ(this._rotation, Math.PI * 0.5, this._rotation); this._rotation.normalize(); - this._setLocalPose(); + this._setLocalPose(this._scale); } /** * {@inheritDoc IColliderShape.setWorldScale } */ - setWorldScale(scale: Vector3): void {} + setWorldScale(scale: Vector3): void { + this._setLocalPose(this._scale); + } } diff --git a/packages/physics-physx/src/shape/PhysXSphereColliderShape.ts b/packages/physics-physx/src/shape/PhysXSphereColliderShape.ts index 71a4c2a32f..28df217123 100644 --- a/packages/physics-physx/src/shape/PhysXSphereColliderShape.ts +++ b/packages/physics-physx/src/shape/PhysXSphereColliderShape.ts @@ -24,7 +24,7 @@ export class PhysXSphereColliderShape extends PhysXColliderShape implements ISph this._pxGeometry = new PhysXPhysics._physX.PxSphereGeometry(this._radius * this._maxScale); this._allocShape(material); - this._setLocalPose(); + this._setLocalPose(this._scale); this.setUniqueID(uniqueID); } @@ -41,6 +41,8 @@ export class PhysXSphereColliderShape extends PhysXColliderShape implements ISph * {@inheritDoc IColliderShape.setWorldScale } */ setWorldScale(scale: Vector3): void { + this._setLocalPose(this._scale); + this._maxScale = Math.max(scale.x, Math.max(scale.x, scale.y)); this._pxGeometry.radius = this._radius * this._maxScale; this._pxShape.setGeometry(this._pxGeometry); From 55ce80f450093eb81cecd3b2b466b3a17820b054 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Thu, 17 Feb 2022 17:19:34 +0800 Subject: [PATCH 03/20] v0.6.7 --- lerna.json | 2 +- packages/controls/package.json | 4 ++-- packages/core/package.json | 6 +++--- packages/design/package.json | 4 ++-- packages/draco/package.json | 4 ++-- packages/framebuffer-picker/package.json | 4 ++-- packages/loader/package.json | 10 +++++----- packages/math/package.json | 4 ++-- packages/oasis-engine/package.json | 10 +++++----- packages/physics-lite/package.json | 6 +++--- packages/physics-physx/package.json | 6 +++--- packages/rhi-webgl/package.json | 8 ++++---- packages/stats/package.json | 4 ++-- 13 files changed, 36 insertions(+), 36 deletions(-) diff --git a/lerna.json b/lerna.json index 4933ed73f2..763bae7337 100644 --- a/lerna.json +++ b/lerna.json @@ -1,6 +1,6 @@ { "npmClient": "npm", - "version": "0.6.6", + "version": "0.6.7", "bootstrap": { "hoist": true }, diff --git a/packages/controls/package.json b/packages/controls/package.json index 3d582e29c6..efdd499dc0 100644 --- a/packages/controls/package.json +++ b/packages/controls/package.json @@ -1,6 +1,6 @@ { "name": "@oasis-engine/controls", - "version": "0.6.6", + "version": "0.6.7", "license": "MIT", "scripts": { "b:types": "tsc", @@ -15,6 +15,6 @@ "types/**/*" ], "dependencies": { - "oasis-engine": "0.6.6" + "oasis-engine": "0.6.7" } } diff --git a/packages/core/package.json b/packages/core/package.json index 2e57b9dcce..2f99225e40 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -1,6 +1,6 @@ { "name": "@oasis-engine/core", - "version": "0.6.6", + "version": "0.6.7", "license": "MIT", "main": "dist/main.js", "module": "dist/module.js", @@ -14,9 +14,9 @@ "types/**/*" ], "dependencies": { - "@oasis-engine/math": "0.6.6" + "@oasis-engine/math": "0.6.7" }, "devDependencies": { - "@oasis-engine/design": "0.6.6" + "@oasis-engine/design": "0.6.7" } } diff --git a/packages/design/package.json b/packages/design/package.json index 6a45186d1b..2bc5d084d3 100755 --- a/packages/design/package.json +++ b/packages/design/package.json @@ -1,6 +1,6 @@ { "name": "@oasis-engine/design", - "version": "0.6.6", + "version": "0.6.7", "license": "MIT", "main": "dist/main.js", "module": "dist/module.js", @@ -13,6 +13,6 @@ ], "types": "types/index.d.ts", "dependencies": { - "@oasis-engine/math": "0.6.6" + "@oasis-engine/math": "0.6.7" } } diff --git a/packages/draco/package.json b/packages/draco/package.json index 059c0c8bee..0090b77d8c 100644 --- a/packages/draco/package.json +++ b/packages/draco/package.json @@ -1,6 +1,6 @@ { "name": "@oasis-engine/draco", - "version": "0.6.6", + "version": "0.6.7", "license": "MIT", "scripts": { "b:types": "tsc" @@ -13,6 +13,6 @@ "types/**/*" ], "dependencies": { - "@oasis-engine/core": "0.6.6" + "@oasis-engine/core": "0.6.7" } } diff --git a/packages/framebuffer-picker/package.json b/packages/framebuffer-picker/package.json index e5fe2aa33e..a3a3e6e55a 100755 --- a/packages/framebuffer-picker/package.json +++ b/packages/framebuffer-picker/package.json @@ -1,6 +1,6 @@ { "name": "@oasis-engine/framebuffer-picker", - "version": "0.6.6", + "version": "0.6.7", "license": "MIT", "main": "dist/main.js", "module": "dist/module.js", @@ -14,6 +14,6 @@ "types/**/*" ], "dependencies": { - "oasis-engine": "0.6.6" + "oasis-engine": "0.6.7" } } diff --git a/packages/loader/package.json b/packages/loader/package.json index e11b75ea09..1944f80c69 100755 --- a/packages/loader/package.json +++ b/packages/loader/package.json @@ -1,6 +1,6 @@ { "name": "@oasis-engine/loader", - "version": "0.6.6", + "version": "0.6.7", "license": "MIT", "types": "types/index.d.ts", "scripts": { @@ -14,9 +14,9 @@ "types/**/*" ], "dependencies": { - "@oasis-engine/core": "0.6.6", - "@oasis-engine/draco": "0.6.6", - "@oasis-engine/math": "0.6.6", - "@oasis-engine/rhi-webgl": "0.6.6" + "@oasis-engine/core": "0.6.7", + "@oasis-engine/draco": "0.6.7", + "@oasis-engine/math": "0.6.7", + "@oasis-engine/rhi-webgl": "0.6.7" } } diff --git a/packages/math/package.json b/packages/math/package.json index 317b2c213f..9ac1dc026b 100755 --- a/packages/math/package.json +++ b/packages/math/package.json @@ -1,6 +1,6 @@ { "name": "@oasis-engine/math", - "version": "0.6.6", + "version": "0.6.7", "license": "MIT", "main": "dist/main.js", "module": "dist/module.js", @@ -15,6 +15,6 @@ "types/**/*" ], "devDependencies": { - "@oasis-engine/design": "0.6.6" + "@oasis-engine/design": "0.6.7" } } diff --git a/packages/oasis-engine/package.json b/packages/oasis-engine/package.json index d5e8f6749c..0430451b8a 100755 --- a/packages/oasis-engine/package.json +++ b/packages/oasis-engine/package.json @@ -1,6 +1,6 @@ { "name": "oasis-engine", - "version": "0.6.6", + "version": "0.6.7", "license": "MIT", "scripts": { "b:types": "tsc" @@ -14,9 +14,9 @@ "types/**/*" ], "dependencies": { - "@oasis-engine/core": "0.6.6", - "@oasis-engine/loader": "0.6.6", - "@oasis-engine/math": "0.6.6", - "@oasis-engine/rhi-webgl": "0.6.6" + "@oasis-engine/core": "0.6.7", + "@oasis-engine/loader": "0.6.7", + "@oasis-engine/math": "0.6.7", + "@oasis-engine/rhi-webgl": "0.6.7" } } diff --git a/packages/physics-lite/package.json b/packages/physics-lite/package.json index 58ef5691be..51e510c5a8 100644 --- a/packages/physics-lite/package.json +++ b/packages/physics-lite/package.json @@ -1,6 +1,6 @@ { "name": "@oasis-engine/physics-lite", - "version": "0.6.6", + "version": "0.6.7", "license": "MIT", "main": "dist/main.js", "module": "dist/module.js", @@ -15,8 +15,8 @@ "types/**/*" ], "dependencies": { - "@oasis-engine/design": "0.6.6", - "oasis-engine": "0.6.6" + "@oasis-engine/design": "0.6.7", + "oasis-engine": "0.6.7" }, "publishConfig": { "access": "public" diff --git a/packages/physics-physx/package.json b/packages/physics-physx/package.json index d6f08f35ff..16bf9504ce 100644 --- a/packages/physics-physx/package.json +++ b/packages/physics-physx/package.json @@ -1,6 +1,6 @@ { "name": "@oasis-engine/physics-physx", - "version": "0.6.6", + "version": "0.6.7", "license": "MIT", "main": "dist/main.js", "module": "dist/module.js", @@ -15,8 +15,8 @@ "types/**/*" ], "dependencies": { - "@oasis-engine/design": "0.6.6", - "oasis-engine": "0.6.6" + "@oasis-engine/design": "0.6.7", + "oasis-engine": "0.6.7" }, "publishConfig": { "access": "public" diff --git a/packages/rhi-webgl/package.json b/packages/rhi-webgl/package.json index d82564fbe7..69d21f3d80 100755 --- a/packages/rhi-webgl/package.json +++ b/packages/rhi-webgl/package.json @@ -1,6 +1,6 @@ { "name": "@oasis-engine/rhi-webgl", - "version": "0.6.6", + "version": "0.6.7", "license": "MIT", "main": "dist/main.js", "module": "dist/module.js", @@ -14,10 +14,10 @@ "types/**/*" ], "dependencies": { - "@oasis-engine/core": "0.6.6", - "@oasis-engine/math": "0.6.6" + "@oasis-engine/core": "0.6.7", + "@oasis-engine/math": "0.6.7" }, "devDependencies": { - "@oasis-engine/design": "0.6.6" + "@oasis-engine/design": "0.6.7" } } diff --git a/packages/stats/package.json b/packages/stats/package.json index e8bae0d2d9..ed2a98c9ec 100755 --- a/packages/stats/package.json +++ b/packages/stats/package.json @@ -1,6 +1,6 @@ { "name": "@oasis-engine/stats", - "version": "0.6.6", + "version": "0.6.7", "license": "MIT", "scripts": { "b:types": "tsc" @@ -14,6 +14,6 @@ "types/**/*" ], "dependencies": { - "oasis-engine": "0.6.6" + "oasis-engine": "0.6.7" } } From a704854601f9b2a153f008296cd001c6e85fd21c Mon Sep 17 00:00:00 2001 From: Hu Song Date: Wed, 2 Mar 2022 15:15:57 +0800 Subject: [PATCH 04/20] fix: get pixel buffer use mip level should right shift (#656) Co-authored-by: gz65555 --- packages/core/src/texture/Texture2D.ts | 4 ++-- packages/core/src/texture/TextureCubeMap.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/core/src/texture/Texture2D.ts b/packages/core/src/texture/Texture2D.ts index 4585145fa0..0057e5a028 100644 --- a/packages/core/src/texture/Texture2D.ts +++ b/packages/core/src/texture/Texture2D.ts @@ -143,8 +143,8 @@ export class Texture2D extends Texture { (this._platformTexture as IPlatformTexture2D).getPixelBuffer( 0, 0, - this._width, - this._height, + this._width >> xOrMipLevelOrOut, + this._height >> xOrMipLevelOrOut, xOrMipLevelOrOut, yOrMipLevel ); diff --git a/packages/core/src/texture/TextureCubeMap.ts b/packages/core/src/texture/TextureCubeMap.ts index 19cd803f73..9a5ff00a88 100644 --- a/packages/core/src/texture/TextureCubeMap.ts +++ b/packages/core/src/texture/TextureCubeMap.ts @@ -168,8 +168,8 @@ export class TextureCubeMap extends Texture { face, 0, 0, - this._width, - this._height, + this._width >> xOrMipLevelOrOut, + this._height >> xOrMipLevelOrOut, xOrMipLevelOrOut, yOrMipLevel ); From 070e621e49e806a69f6de5efc72e963c852cdac5 Mon Sep 17 00:00:00 2001 From: luzhuang <364439895@qq.com> Date: Wed, 2 Mar 2022 15:21:30 +0800 Subject: [PATCH 05/20] Update AnimatorState.ts (#657) fix: fix comment --- packages/core/src/animation/AnimatorState.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/animation/AnimatorState.ts b/packages/core/src/animation/AnimatorState.ts index 04bfd3ef6c..7dd7d20abf 100644 --- a/packages/core/src/animation/AnimatorState.ts +++ b/packages/core/src/animation/AnimatorState.ts @@ -55,7 +55,7 @@ export class AnimatorState { } /** - * The start time of the clip, the range is 0 to 1, default is 1. + * The end time of the clip, the range is 0 to 1, default is 1. */ get clipEndTime() { return this._clipEndTime; From ffe530c68919751f2b438800124c64f01d419299 Mon Sep 17 00:00:00 2001 From: Bob <768442443@qq.com> Date: Wed, 2 Mar 2022 15:28:35 +0800 Subject: [PATCH 06/20] Rename `ability` to `component`. (#608) --- packages/core/src/ComponentsDependencies.ts | 4 ++-- packages/core/src/Entity.ts | 6 +++--- packages/core/src/lighting/KHR_lights.ts | 14 +++++++------- packages/core/src/shadow/ShadowFeature.ts | 14 +++++++------- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/packages/core/src/ComponentsDependencies.ts b/packages/core/src/ComponentsDependencies.ts index 51ef0c0b35..dd24d813f4 100644 --- a/packages/core/src/ComponentsDependencies.ts +++ b/packages/core/src/ComponentsDependencies.ts @@ -70,8 +70,8 @@ export class ComponentsDependencies { private constructor() {} } -export function dependencies(...abilityClass: ComponentConstructor[]) { +export function dependencies(...componentClass: ComponentConstructor[]) { return function (target: T): void { - abilityClass.forEach((ability) => ComponentsDependencies.register(target, ability)); + componentClass.forEach((component) => ComponentsDependencies.register(target, component)); }; } diff --git a/packages/core/src/Entity.ts b/packages/core/src/Entity.ts index e43315d68d..e289f0ed66 100644 --- a/packages/core/src/Entity.ts +++ b/packages/core/src/Entity.ts @@ -343,9 +343,9 @@ export class Entity extends EngineObject { if (this._destroyed) return; super.destroy(); - const abilityArray = this._components; - for (let i = abilityArray.length - 1; i >= 0; i--) { - abilityArray[i].destroy(); + const components = this._components; + for (let i = components.length - 1; i >= 0; i--) { + components[i].destroy(); } this._components.length = 0; diff --git a/packages/core/src/lighting/KHR_lights.ts b/packages/core/src/lighting/KHR_lights.ts index 30ff8dc5fe..5b4927d085 100644 --- a/packages/core/src/lighting/KHR_lights.ts +++ b/packages/core/src/lighting/KHR_lights.ts @@ -11,25 +11,25 @@ class KHR_lights { for (let i = 0; i < lights.length; i++) { const { name, type, spot } = lights[i]; let { color, intensity } = lights[i]; - let ability; + let component; let props; color = color ? color : [1, 1, 1]; intensity = intensity === undefined ? 1 : intensity; switch (type) { case "ambient": - ability = AmbientLight; + component = AmbientLight; props = { name, color, intensity }; break; case "directional": - ability = DirectLight; + component = DirectLight; props = { name, color, intensity }; break; case "point": - ability = PointLight; + component = PointLight; props = { name, color, intensity }; break; case "spot": - ability = SpotLight; + component = SpotLight; props = { name, color, intensity, angle: spot.outerConeAngle }; break; default: @@ -37,8 +37,8 @@ class KHR_lights { break; } - if (ability) { - results[i] = { ability, props }; + if (component) { + results[i] = { component, props }; } } return results; diff --git a/packages/core/src/shadow/ShadowFeature.ts b/packages/core/src/shadow/ShadowFeature.ts index 41b919990b..96b3079559 100644 --- a/packages/core/src/shadow/ShadowFeature.ts +++ b/packages/core/src/shadow/ShadowFeature.ts @@ -90,20 +90,20 @@ export class ShadowFeature extends SceneFeature { const items = renderQueue.items; for (let i = 0, len = items.length; i < len; i++) { const item = items[i]; - const ability: Component = item.component; + const component: Component = item.component; - const receiveShadow = (ability as any).recieveShadow; - const castShadow = (ability as any).castShadow; + const receiveShadow = (component as any).recieveShadow; + const castShadow = (component as any).castShadow; if (receiveShadow === true) { - ability.entity.layer |= Layer.Layer30; //SHADOW; + component.entity.layer |= Layer.Layer30; //SHADOW; } else if (receiveShadow === false) { - ability.entity.layer &= ~Layer.Layer30; //SHADOW; + component.entity.layer &= ~Layer.Layer30; //SHADOW; } if (castShadow === true) { - ability.entity.layer |= Layer.Layer31; //SHADOW_MAP; + component.entity.layer |= Layer.Layer31; //SHADOW_MAP; } else if (castShadow === false) { - ability.entity.layer &= ~Layer.Layer31; //SHADOW_MAP; + component.entity.layer &= ~Layer.Layer31; //SHADOW_MAP; } } } From bec99aac8e50ddf00f378c413057a46420ac9a96 Mon Sep 17 00:00:00 2001 From: zhuxudong Date: Wed, 2 Mar 2022 16:08:26 +0800 Subject: [PATCH 07/20] fix: resize max uniform vector count (#660) * fix: resize max uniform vector count Co-authored-by: shensi.zxd --- packages/core/src/mesh/SkinnedMeshRenderer.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/core/src/mesh/SkinnedMeshRenderer.ts b/packages/core/src/mesh/SkinnedMeshRenderer.ts index 225e583b31..1469e4ff06 100644 --- a/packages/core/src/mesh/SkinnedMeshRenderer.ts +++ b/packages/core/src/mesh/SkinnedMeshRenderer.ts @@ -123,23 +123,24 @@ export class SkinnedMeshRenderer extends MeshRenderer { const rhi = this.entity.engine._hardwareRenderer; if (!rhi) return; const maxAttribUniformVec4 = rhi.renderStates.getParameter(rhi.gl.MAX_VERTEX_UNIFORM_VECTORS); - const maxJoints = Math.floor((maxAttribUniformVec4 - 20) / 4); + const maxJoints = Math.floor((maxAttribUniformVec4 - 30) / 4); const shaderData = this.shaderData; - const jointCount = this.jointNodes?.length; + const jointCount = jointNodes.length; + if (jointCount) { shaderData.enableMacro("O3_HAS_SKIN"); shaderData.setInt(SkinnedMeshRenderer._jointCountProperty, jointCount); - if (joints.length > maxJoints) { + if (jointCount > maxJoints) { if (rhi.canIUseMoreJoints) { this._useJointTexture = true; } else { Logger.error( - `component's joints count(${joints}) greater than device's MAX_VERTEX_UNIFORM_VECTORS number ${maxAttribUniformVec4}, and don't support jointTexture in this device. suggest joint count less than ${maxJoints}.`, + `component's joints count(${jointCount}) greater than device's MAX_VERTEX_UNIFORM_VECTORS number ${maxAttribUniformVec4}, and don't support jointTexture in this device. suggest joint count less than ${maxJoints}.`, this ); } } else { - const maxJoints = Math.max(SkinnedMeshRenderer._maxJoints, joints.length); + const maxJoints = Math.max(SkinnedMeshRenderer._maxJoints, jointCount); SkinnedMeshRenderer._maxJoints = maxJoints; shaderData.disableMacro("O3_USE_JOINT_TEXTURE"); shaderData.enableMacro("O3_JOINTS_NUM", maxJoints.toString()); From 339a4a533bf6ab7014c308b22419034ec33fb6c3 Mon Sep 17 00:00:00 2001 From: singlecoder Date: Thu, 3 Mar 2022 15:07:30 +0800 Subject: [PATCH 08/20] =?UTF-8?q?fix(2d):=20can=20not=20update=20world=20p?= =?UTF-8?q?osition=20for=20sprite=20renderer=20when=20call=20=E2=80=A6=20(?= =?UTF-8?q?#635)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(2d): can not update wrold position for sprite renderer when call bounds in the same frame --- packages/core/src/2d/sprite/Sprite.ts | 69 +++++++++---------- packages/core/src/2d/sprite/SpriteMask.ts | 28 +++++--- packages/core/src/2d/sprite/SpriteRenderer.ts | 32 ++++++--- 3 files changed, 76 insertions(+), 53 deletions(-) diff --git a/packages/core/src/2d/sprite/Sprite.ts b/packages/core/src/2d/sprite/Sprite.ts index 1454680535..cd2600de9e 100644 --- a/packages/core/src/2d/sprite/Sprite.ts +++ b/packages/core/src/2d/sprite/Sprite.ts @@ -2,6 +2,8 @@ import { BoundingBox, MathUtil, Rect, Vector2, Vector4 } from "@oasis-engine/mat import { RefObject } from "../../asset/RefObject"; import { Engine } from "../../Engine"; import { Texture2D } from "../../texture/Texture2D"; +import { UpdateFlag } from "../../UpdateFlag"; +import { UpdateFlagManager } from "../../UpdateFlagManager"; /** * 2D sprite. @@ -31,6 +33,7 @@ export class Sprite extends RefObject { private _atlasRegion: Rect = new Rect(0, 0, 1, 1); private _atlasRegionOffset: Vector4 = new Vector4(0, 0, 0, 0); private _dirtyFlag: DirtyFlag = DirtyFlag.all; + private _updateFlagManager: UpdateFlagManager = new UpdateFlagManager(); /** * The reference to the used texture. @@ -146,26 +149,6 @@ export class Sprite extends RefObject { } } - /** - * Clone. - * @returns Cloned sprite - */ - clone(): Sprite { - const cloneSprite = new Sprite( - this._engine, - this._texture, - this._region, - this._pivot, - this._pixelsPerUnit, - this.name - ); - cloneSprite._assetID = this._assetID; - cloneSprite._atlasRotated = this._atlasRotated; - this._atlasRegion.cloneTo(cloneSprite._atlasRegion); - this._atlasRegionOffset.cloneTo(cloneSprite._atlasRegionOffset); - return cloneSprite; - } - /** * Constructor a Sprite. * @param engine - Engine to which the sprite belongs @@ -195,6 +178,33 @@ export class Sprite extends RefObject { this._triangles = Sprite._rectangleTriangles; } + /** + * Clone. + * @returns Cloned sprite + */ + clone(): Sprite { + const cloneSprite = new Sprite( + this._engine, + this._texture, + this._region, + this._pivot, + this._pixelsPerUnit, + this.name + ); + cloneSprite._assetID = this._assetID; + cloneSprite._atlasRotated = this._atlasRotated; + this._atlasRegion.cloneTo(cloneSprite._atlasRegion); + this._atlasRegionOffset.cloneTo(cloneSprite._atlasRegionOffset); + return cloneSprite; + } + + /** + * @internal + */ + _registerUpdateFlag(): UpdateFlag { + return this._updateFlagManager.register(); + } + /** * @override */ @@ -271,9 +281,9 @@ export class Sprite extends RefObject { } /** - * Update mesh. + * @internal */ - private _updateMesh(): void { + _updateMesh(): void { if (this._isContainDirtyFlag(DirtyFlag.positions)) { this._updatePositionsAndBounds(); } @@ -343,20 +353,8 @@ export class Sprite extends RefObject { uv[3].setValue(left, bottom); } } - } - /** - * @internal - * Update mesh data of the sprite. - * @returns True if the data is refreshed, false otherwise. - */ - _updateMeshData(): boolean { - if (this._isContainDirtyFlag(DirtyFlag.all)) { - this._updateMesh(); - this._setDirtyFlagFalse(DirtyFlag.all); - return true; - } - return false; + this._setDirtyFlagFalse(DirtyFlag.all); } private _isContainDirtyFlag(type: number): boolean { @@ -365,6 +363,7 @@ export class Sprite extends RefObject { private _setDirtyFlagTrue(type: number): void { this._dirtyFlag |= type; + this._updateFlagManager.distribute(); } private _setDirtyFlagFalse(type: number): void { diff --git a/packages/core/src/2d/sprite/SpriteMask.ts b/packages/core/src/2d/sprite/SpriteMask.ts index c7646265e0..796fa65b34 100644 --- a/packages/core/src/2d/sprite/SpriteMask.ts +++ b/packages/core/src/2d/sprite/SpriteMask.ts @@ -1,6 +1,7 @@ import { Vector3 } from "@oasis-engine/math"; import { Camera } from "../../Camera"; import { assignmentClone, deepClone, ignoreClone } from "../../clone/CloneManager"; +import { ICustomClone } from "../../clone/ComponentCloner"; import { Entity } from "../../Entity"; import { Renderer } from "../../Renderer"; import { SpriteMaskElement } from "../../RenderPipeline/SpriteMaskElement"; @@ -13,7 +14,7 @@ import { Sprite } from "./Sprite"; /** * A component for masking Sprites. */ -export class SpriteMask extends Renderer { +export class SpriteMask extends Renderer implements ICustomClone { /** @internal */ static _textureProperty: ShaderProperty = Shader.getPropertyByName("u_maskTexture"); /** @internal */ @@ -27,13 +28,13 @@ export class SpriteMask extends Renderer { @deepClone private _positions: Vector3[] = [new Vector3(), new Vector3(), new Vector3(), new Vector3()]; @ignoreClone - private _isSpriteDirty: boolean = true; - @ignoreClone private _worldMatrixDirtyFlag: UpdateFlag; - @assignmentClone + @ignoreClone private _sprite: Sprite = null; @assignmentClone private _alphaCutoff: number = 0.5; + @ignoreClone + private _spriteDirty: UpdateFlag; /** The mask layers the sprite mask influence to. */ @assignmentClone @@ -48,8 +49,11 @@ export class SpriteMask extends Renderer { set sprite(value: Sprite) { if (this._sprite !== value) { + this._spriteDirty && this._spriteDirty.destroy(); this._sprite = value; - this._isSpriteDirty = true; + if (value) { + this._spriteDirty = value._registerUpdateFlag(); + } } } @@ -83,6 +87,7 @@ export class SpriteMask extends Renderer { */ _onDestroy(): void { this._worldMatrixDirtyFlag.destroy(); + this._spriteDirty && this._spriteDirty.destroy(); super._onDestroy(); } @@ -104,9 +109,9 @@ export class SpriteMask extends Renderer { const transform = this.entity.transform; // Update sprite data. - const localDirty = sprite._updateMeshData(); + sprite._updateMesh(); - if (this._worldMatrixDirtyFlag.flag || localDirty || this._isSpriteDirty) { + if (this._worldMatrixDirtyFlag.flag || this._spriteDirty.flag) { const localPositions = sprite._positions; const localVertexPos = SpriteMask._tempVec3; const worldMatrix = transform.worldMatrix; @@ -117,7 +122,7 @@ export class SpriteMask extends Renderer { Vector3.transformToVec3(localVertexPos, worldMatrix, positions[i]); } - this._isSpriteDirty = false; + this._spriteDirty.flag = false; this._worldMatrixDirtyFlag.flag = false; } @@ -130,4 +135,11 @@ export class SpriteMask extends Renderer { camera._renderPipeline._allSpriteMasks.add(this); this._maskElement = maskElement; } + + /** + * @internal + */ + _cloneTo(target: SpriteMask): void { + target.sprite = this._sprite; + } } diff --git a/packages/core/src/2d/sprite/SpriteRenderer.ts b/packages/core/src/2d/sprite/SpriteRenderer.ts index 045c1672f1..4d94bd32cd 100644 --- a/packages/core/src/2d/sprite/SpriteRenderer.ts +++ b/packages/core/src/2d/sprite/SpriteRenderer.ts @@ -1,6 +1,7 @@ import { BoundingBox, Color, Vector3 } from "@oasis-engine/math"; import { Camera } from "../../Camera"; import { assignmentClone, deepClone, ignoreClone } from "../../clone/CloneManager"; +import { ICustomClone } from "../../clone/ComponentCloner"; import { Entity } from "../../Entity"; import { Renderer } from "../../Renderer"; import { CompareFunction } from "../../shader/enums/CompareFunction"; @@ -14,7 +15,7 @@ import { Sprite } from "./Sprite"; /** * Renders a Sprite for 2D graphics. */ -export class SpriteRenderer extends Renderer { +export class SpriteRenderer extends Renderer implements ICustomClone { private static _textureProperty: ShaderProperty = Shader.getPropertyByName("u_spriteTexture"); private static _tempVec3: Vector3 = new Vector3(); @@ -27,7 +28,7 @@ export class SpriteRenderer extends Renderer { @deepClone private _positions: Vector3[] = [new Vector3(), new Vector3(), new Vector3(), new Vector3()]; - @assignmentClone + @ignoreClone private _sprite: Sprite = null; @deepClone private _color: Color = new Color(1, 1, 1, 1); @@ -40,9 +41,11 @@ export class SpriteRenderer extends Renderer { @assignmentClone private _cacheFlipY: boolean = false; @ignoreClone - private _dirtyFlag: number = DirtyFlag.All; + private _dirtyFlag: number = 0; @ignoreClone private _isWorldMatrixDirty: UpdateFlag; + @ignoreClone + private _spriteDirty: UpdateFlag; @assignmentClone private _maskInteraction: SpriteMaskInteraction = SpriteMaskInteraction.None; @assignmentClone @@ -57,8 +60,11 @@ export class SpriteRenderer extends Renderer { set sprite(value: Sprite | null) { if (this._sprite !== value) { + this._spriteDirty && this._spriteDirty.destroy(); this._sprite = value; - this._setDirtyFlagTrue(DirtyFlag.Sprite); + if (value) { + this._spriteDirty = value._registerUpdateFlag(); + } } } @@ -154,9 +160,9 @@ export class SpriteRenderer extends Renderer { const { transform } = this.entity; // Update sprite data. - const localDirty = sprite._updateMeshData(); + sprite._updateMesh(); - if (this._isWorldMatrixDirty.flag || localDirty || this._isContainDirtyFlag(DirtyFlag.Sprite)) { + if (this._isWorldMatrixDirty.flag || this._spriteDirty.flag) { const localPositions = sprite._positions; const localVertexPos = SpriteRenderer._tempVec3; const worldMatrix = transform.worldMatrix; @@ -169,8 +175,8 @@ export class SpriteRenderer extends Renderer { } this._setDirtyFlagFalse(DirtyFlag.Flip); - this._setDirtyFlagFalse(DirtyFlag.Sprite); this._isWorldMatrixDirty.flag = false; + this._spriteDirty.flag = false; this._cacheFlipX = flipX; this._cacheFlipY = flipY; } else if (this._isContainDirtyFlag(DirtyFlag.Flip)) { @@ -217,6 +223,7 @@ export class SpriteRenderer extends Renderer { */ _onDestroy(): void { this._isWorldMatrixDirty.destroy(); + this._spriteDirty && this._spriteDirty.destroy(); super._onDestroy(); } @@ -232,6 +239,13 @@ export class SpriteRenderer extends Renderer { this._dirtyFlag &= ~type; } + /** + * @internal + */ + _cloneTo(target: SpriteRenderer): void { + target.sprite = this._sprite; + } + /** * @override */ @@ -279,7 +293,5 @@ export class SpriteRenderer extends Renderer { enum DirtyFlag { Flip = 0x1, - Sprite = 0x2, - All = 0x3, - MaskInteraction = 0x4 + MaskInteraction = 0x2 } From 4050bfd4909ea72a3e3a7cfe40c6808f14854b89 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Mon, 7 Mar 2022 11:54:40 +0800 Subject: [PATCH 09/20] refactor: temp --- packages/core/src/Transform.ts | 16 +- packages/core/tests/Transform.test.ts | 502 +++++++++++--------------- packages/math/src/Quaternion.ts | 55 +-- packages/math/src/Vector3.ts | 54 +-- 4 files changed, 271 insertions(+), 356 deletions(-) diff --git a/packages/core/src/Transform.ts b/packages/core/src/Transform.ts index cf3edd0780..5837a340e9 100644 --- a/packages/core/src/Transform.ts +++ b/packages/core/src/Transform.ts @@ -65,13 +65,16 @@ export class Transform extends Component { */ get worldPosition(): Vector3 { if (this._isContainDirtyFlag(TransformFlag.WorldPosition)) { + this._worldPosition._disableValueChanged = true; if (this._getParentTransform()) { this.worldMatrix.getTranslation(this._worldPosition); } else { this._position.cloneTo(this._worldPosition); } + this._worldPosition._disableValueChanged = false; this._setDirtyFlagFalse(TransformFlag.WorldPosition); } + return this._worldPosition; } @@ -87,10 +90,13 @@ export class Transform extends Component { */ get rotation(): Vector3 { if (this._isContainDirtyFlag(TransformFlag.LocalEuler)) { + this._rotation._disableValueChanged = true; this._rotationQuaternion.toEuler(this._rotation); + this._rotation._disableValueChanged = false; this._rotation.scale(MathUtil.radToDegreeFactor); // radians to degrees this._setDirtyFlagFalse(TransformFlag.LocalEuler); } + return this._rotation; } @@ -106,8 +112,10 @@ export class Transform extends Component { */ get worldRotation(): Vector3 { if (this._isContainDirtyFlag(TransformFlag.WorldEuler)) { + this._worldRotation._disableValueChanged = true; this.worldRotationQuaternion.toEuler(this._worldRotation); this._worldRotation.scale(MathUtil.radToDegreeFactor); // Radian to angle + this._worldRotation._disableValueChanged = false; this._setDirtyFlagFalse(TransformFlag.WorldEuler); } return this._worldRotation; @@ -124,12 +132,14 @@ export class Transform extends Component { */ get rotationQuaternion(): Quaternion { if (this._isContainDirtyFlag(TransformFlag.LocalQuat)) { + this._rotationQuaternion._disableValueChanged = true; Quaternion.rotationEuler( MathUtil.degreeToRadian(this._rotation.x), MathUtil.degreeToRadian(this._rotation.y), MathUtil.degreeToRadian(this._rotation.z), this._rotationQuaternion ); + this._rotationQuaternion._disableValueChanged = false; this._setDirtyFlagFalse(TransformFlag.LocalQuat); } return this._rotationQuaternion; @@ -146,12 +156,14 @@ export class Transform extends Component { */ get worldRotationQuaternion(): Quaternion { if (this._isContainDirtyFlag(TransformFlag.WorldQuat)) { + this._worldRotationQuaternion._disableValueChanged = true; const parent = this._getParentTransform(); if (parent != null) { Quaternion.multiply(parent.worldRotationQuaternion, this.rotationQuaternion, this._worldRotationQuaternion); } else { this.rotationQuaternion.cloneTo(this._worldRotationQuaternion); } + this._worldRotationQuaternion._disableValueChanged = false; this._setDirtyFlagFalse(TransformFlag.WorldQuat); } return this._worldRotationQuaternion; @@ -183,6 +195,7 @@ export class Transform extends Component { */ get lossyWorldScale(): Vector3 { if (this._isContainDirtyFlag(TransformFlag.WorldScale)) { + this._lossyWorldScale._disableValueChanged = true; if (this._getParentTransform()) { const scaleMat = this._getScaleMatrix(); const e = scaleMat.elements; @@ -190,6 +203,7 @@ export class Transform extends Component { } else { this._scale.cloneTo(this._lossyWorldScale); } + this._lossyWorldScale._disableValueChanged = false; this._setDirtyFlagFalse(TransformFlag.WorldScale); } return this._lossyWorldScale; @@ -695,10 +709,8 @@ export class Transform extends Component { private _rotateByQuat(rotateQuat: Quaternion, relativeToLocal: boolean) { if (relativeToLocal) { Quaternion.multiply(this.rotationQuaternion, rotateQuat, this._rotationQuaternion); - this.rotationQuaternion = this._rotationQuaternion; } else { Quaternion.multiply(this.worldRotationQuaternion, rotateQuat, this._worldRotationQuaternion); - this.worldRotationQuaternion = this._worldRotationQuaternion; } } diff --git a/packages/core/tests/Transform.test.ts b/packages/core/tests/Transform.test.ts index eb4dea95d8..9a100dcf07 100644 --- a/packages/core/tests/Transform.test.ts +++ b/packages/core/tests/Transform.test.ts @@ -1,82 +1,60 @@ -import { Entity, Transform } from "../src/index"; -import { Vector3, Quaternion, Matrix } from "@oasis-engine/math"; +import { Matrix, Quaternion, Vector3 } from "@oasis-engine/math"; +import { Entity } from "../src/index"; describe("Transform", () => { describe("no parent", () => { it("constructor", () => { - const node = new Entity(); + const node = new Entity(null); const transform = node.transform; - expect(transform.position).toEqual(new Vector3()); - expect(transform.rotation).toEqual(new Vector3()); - expect(transform.rotationQuaternion).toEqual(new Quaternion()); + vector3CloseTo(transform.position, new Vector3()); + vector3CloseTo(transform.rotation, new Vector3()); + quaternionCloseTo(transform.rotationQuaternion, new Quaternion()); expect(transform.worldMatrix).toEqual(new Matrix()); - expect(transform.worldPosition).toEqual(new Vector3()); - expect(transform.worldRotation).toEqual(new Vector3()); + vector3CloseTo(transform.worldPosition, new Vector3()); + vector3CloseTo(transform.worldRotation, new Vector3()); }); it("set position", () => { - const node = new Entity(); + const node = new Entity(null); const transform = node.transform; transform.position = new Vector3(10, 20, 30); - arrayCloseTo(transform.position, new Vector3(10, 20, 30)); - arrayCloseTo(transform.worldPosition, new Vector3(10, 20, 30)); + vector3CloseTo(transform.position, new Vector3(10, 20, 30)); + vector3CloseTo(transform.worldPosition, new Vector3(10, 20, 30)); arrayCloseTo(transform.localMatrix.elements, [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 10, 20, 30, 1]); arrayCloseTo(transform.worldMatrix.elements, [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 10, 20, 30, 1]); }); it("set rotation", () => { - const node = new Entity(); + const node = new Entity(null); const transform = node.transform; transform.rotation = new Vector3(10, 20, 30); - arrayCloseTo(transform.worldRotation, new Vector3(10, 20, 30)); - arrayCloseTo( + vector3CloseTo(transform.worldRotation, new Vector3(10, 20, 30)); + quaternionCloseTo( transform.rotationQuaternion, new Quaternion(0.12767944069578063, 0.14487812541736914, 0.2392983377447303, 0.9515485246437885) ); - arrayCloseTo( + quaternionCloseTo( transform.worldRotationQuaternion, new Quaternion(0.12767944069578063, 0.14487812541736914, 0.2392983377447303, 0.9515485246437885) ); - arrayCloseTo(transform.localMatrix.elements, [ - 0.8434932827949524, - 0.49240386486053467, - -0.21461017429828644, - 0, - -0.4184120297431946, - 0.8528685569763184, - 0.31232455372810364, - 0, - 0.33682408928871155, - -0.1736481785774231, - 0.9254165887832642, - 0, - 0, - 0, - 0, - 1 - ]); - arrayCloseTo(transform.worldMatrix.elements, [ - 0.8434932827949524, - 0.49240386486053467, - -0.21461017429828644, - 0, - -0.4184120297431946, - 0.8528685569763184, - 0.31232455372810364, - 0, - 0.33682408928871155, - -0.1736481785774231, - 0.9254165887832642, - 0, - 0, - 0, - 0, - 1 - ]); + arrayCloseTo( + transform.localMatrix.elements, + [ + 0.8434932827949524, 0.49240386486053467, -0.21461017429828644, 0, -0.4184120297431946, 0.8528685569763184, + 0.31232455372810364, 0, 0.33682408928871155, -0.1736481785774231, 0.9254165887832642, 0, 0, 0, 0, 1 + ] + ); + arrayCloseTo( + transform.worldMatrix.elements, + [ + 0.8434932827949524, 0.49240386486053467, -0.21461017429828644, 0, -0.4184120297431946, 0.8528685569763184, + 0.31232455372810364, 0, 0.33682408928871155, -0.1736481785774231, 0.9254165887832642, 0, 0, 0, 0, 1 + ] + ); }); it("set quat", () => { - const node = new Entity(); + const node = new Entity(null); const transform = node.transform; transform.rotationQuaternion = new Quaternion( 0.12767944069578063, @@ -84,120 +62,76 @@ describe("Transform", () => { 0.2392983377447303, 0.9515485246437885 ); - arrayCloseTo(transform.rotation, new Vector3(10, 20, 30)); - arrayCloseTo(transform.worldRotation, new Vector3(10, 20, 30)); - arrayCloseTo( + vector3CloseTo(transform.rotation, new Vector3(10, 20, 30)); + vector3CloseTo(transform.worldRotation, new Vector3(10, 20, 30)); + quaternionCloseTo( transform.worldRotationQuaternion, new Quaternion(0.12767944069578063, 0.14487812541736914, 0.2392983377447303, 0.9515485246437885) ); - arrayCloseTo(transform.localMatrix.elements, [ - 0.8434932827949524, - 0.49240386486053467, - -0.21461017429828644, - 0, - -0.4184120297431946, - 0.8528685569763184, - 0.31232455372810364, - 0, - 0.33682408928871155, - -0.1736481785774231, - 0.9254165887832642, - 0, - 0, - 0, - 0, - 1 - ]); - arrayCloseTo(transform.worldMatrix.elements, [ - 0.8434932827949524, - 0.49240386486053467, - -0.21461017429828644, - 0, - -0.4184120297431946, - 0.8528685569763184, - 0.31232455372810364, - 0, - 0.33682408928871155, - -0.1736481785774231, - 0.9254165887832642, - 0, - 0, - 0, - 0, - 1 - ]); + arrayCloseTo( + transform.localMatrix.elements, + [ + 0.8434932827949524, 0.49240386486053467, -0.21461017429828644, 0, -0.4184120297431946, 0.8528685569763184, + 0.31232455372810364, 0, 0.33682408928871155, -0.1736481785774231, 0.9254165887832642, 0, 0, 0, 0, 1 + ] + ); + arrayCloseTo( + transform.worldMatrix.elements, + [ + 0.8434932827949524, 0.49240386486053467, -0.21461017429828644, 0, -0.4184120297431946, 0.8528685569763184, + 0.31232455372810364, 0, 0.33682408928871155, -0.1736481785774231, 0.9254165887832642, 0, 0, 0, 0, 1 + ] + ); }); it("set scale", () => { - const node = new Entity(); + const node = new Entity(null); const transform = node.transform; transform.scale = new Vector3(1, 2, 3); - arrayCloseTo(transform.lossyWorldScale, new Vector3(1, 2, 3)); + vector3CloseTo(transform.lossyWorldScale, new Vector3(1, 2, 3)); arrayCloseTo(transform.localMatrix.elements, [1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 3, 0, 0, 0, 0, 1]); arrayCloseTo(transform.worldMatrix.elements, [1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 3, 0, 0, 0, 0, 1]); }); it("set world position", () => { - const node = new Entity(); + const node = new Entity(null); node.transform.worldPosition = new Vector3(10, 20, 30); - arrayCloseTo(node.transform.position, new Vector3(10, 20, 30)); + vector3CloseTo(node.transform.position, new Vector3(10, 20, 30)); arrayCloseTo(node.transform.localMatrix.elements, [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 10, 20, 30, 1]); arrayCloseTo(node.transform.worldMatrix.elements, [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 10, 20, 30, 1]); }); it("set world rotation", () => { - const node = new Entity(); + const node = new Entity(null); const transform = node.transform; transform.worldRotation = new Vector3(10, 20, 30); - arrayCloseTo(transform.rotation, new Vector3(10, 20, 30)); - arrayCloseTo( + vector3CloseTo(transform.rotation, new Vector3(10, 20, 30)); + quaternionCloseTo( transform.rotationQuaternion, new Quaternion(0.12767944069578063, 0.14487812541736914, 0.2392983377447303, 0.9515485246437885) ); - arrayCloseTo( + quaternionCloseTo( transform.worldRotationQuaternion, new Quaternion(0.12767944069578063, 0.14487812541736914, 0.2392983377447303, 0.9515485246437885) ); - arrayCloseTo(transform.localMatrix.elements, [ - 0.8434932827949524, - 0.49240386486053467, - -0.21461017429828644, - 0, - -0.4184120297431946, - 0.8528685569763184, - 0.31232455372810364, - 0, - 0.33682408928871155, - -0.1736481785774231, - 0.9254165887832642, - 0, - 0, - 0, - 0, - 1 - ]); - arrayCloseTo(transform.worldMatrix.elements, [ - 0.8434932827949524, - 0.49240386486053467, - -0.21461017429828644, - 0, - -0.4184120297431946, - 0.8528685569763184, - 0.31232455372810364, - 0, - 0.33682408928871155, - -0.1736481785774231, - 0.9254165887832642, - 0, - 0, - 0, - 0, - 1 - ]); + arrayCloseTo( + transform.localMatrix.elements, + [ + 0.8434932827949524, 0.49240386486053467, -0.21461017429828644, 0, -0.4184120297431946, 0.8528685569763184, + 0.31232455372810364, 0, 0.33682408928871155, -0.1736481785774231, 0.9254165887832642, 0, 0, 0, 0, 1 + ] + ); + arrayCloseTo( + transform.worldMatrix.elements, + [ + 0.8434932827949524, 0.49240386486053467, -0.21461017429828644, 0, -0.4184120297431946, 0.8528685569763184, + 0.31232455372810364, 0, 0.33682408928871155, -0.1736481785774231, 0.9254165887832642, 0, 0, 0, 0, 1 + ] + ); }); it("set world quat", () => { - const node = new Entity(); + const node = new Entity(null); const transform = node.transform; transform.worldRotationQuaternion = new Quaternion( 0.12767944069578063, @@ -205,52 +139,30 @@ describe("Transform", () => { 0.2392983377447303, 0.9515485246437885 ); - arrayCloseTo(transform.rotation, new Vector3(10, 20, 30)); - arrayCloseTo( + vector3CloseTo(transform.rotation, new Vector3(10, 20, 30)); + quaternionCloseTo( transform.rotationQuaternion, new Quaternion(0.12767944069578063, 0.14487812541736914, 0.2392983377447303, 0.9515485246437885) ); - arrayCloseTo(transform.worldRotation, new Vector3(10, 20, 30)); - arrayCloseTo(transform.localMatrix.elements, [ - 0.8434932827949524, - 0.49240386486053467, - -0.21461017429828644, - 0, - -0.4184120297431946, - 0.8528685569763184, - 0.31232455372810364, - 0, - 0.33682408928871155, - -0.1736481785774231, - 0.9254165887832642, - 0, - 0, - 0, - 0, - 1 - ]); - arrayCloseTo(transform.worldMatrix.elements, [ - 0.8434932827949524, - 0.49240386486053467, - -0.21461017429828644, - 0, - -0.4184120297431946, - 0.8528685569763184, - 0.31232455372810364, - 0, - 0.33682408928871155, - -0.1736481785774231, - 0.9254165887832642, - 0, - 0, - 0, - 0, - 1 - ]); + vector3CloseTo(transform.worldRotation, new Vector3(10, 20, 30)); + arrayCloseTo( + transform.localMatrix.elements, + [ + 0.8434932827949524, 0.49240386486053467, -0.21461017429828644, 0, -0.4184120297431946, 0.8528685569763184, + 0.31232455372810364, 0, 0.33682408928871155, -0.1736481785774231, 0.9254165887832642, 0, 0, 0, 0, 1 + ] + ); + arrayCloseTo( + transform.worldMatrix.elements, + [ + 0.8434932827949524, 0.49240386486053467, -0.21461017429828644, 0, -0.4184120297431946, 0.8528685569763184, + 0.31232455372810364, 0, 0.33682408928871155, -0.1736481785774231, 0.9254165887832642, 0, 0, 0, 0, 1 + ] + ); }); it("set local matrix", () => { - const node = new Entity(); + const node = new Entity(null); const transform = node.transform; transform.localMatrix = new Matrix( 0.8434932827949524, @@ -270,40 +182,29 @@ describe("Transform", () => { 30, 1 ); - arrayCloseTo(transform.position, new Vector3(10, 20, 30)); - arrayCloseTo(transform.worldPosition, new Vector3(10, 20, 30)); - arrayCloseTo(transform.rotation, new Vector3(10, 20, 30)); - arrayCloseTo(transform.worldRotation, new Vector3(10, 20, 30)); - arrayCloseTo( + vector3CloseTo(transform.position, new Vector3(10, 20, 30)); + vector3CloseTo(transform.worldPosition, new Vector3(10, 20, 30)); + vector3CloseTo(transform.rotation, new Vector3(10, 20, 30)); + vector3CloseTo(transform.worldRotation, new Vector3(10, 20, 30)); + quaternionCloseTo( transform.rotationQuaternion, new Quaternion(0.12767944069578063, 0.14487812541736914, 0.2392983377447303, 0.9515485246437885) ); - arrayCloseTo( + quaternionCloseTo( transform.worldRotationQuaternion, new Quaternion(0.12767944069578063, 0.14487812541736914, 0.2392983377447303, 0.9515485246437885) ); - arrayCloseTo(transform.worldMatrix.elements, [ - 0.8434932827949524, - 0.49240386486053467, - -0.21461017429828644, - 0, - -0.4184120297431946, - 0.8528685569763184, - 0.31232455372810364, - 0, - 0.33682408928871155, - -0.1736481785774231, - 0.9254165887832642, - 0, - 10, - 20, - 30, - 1 - ]); + arrayCloseTo( + transform.worldMatrix.elements, + [ + 0.8434932827949524, 0.49240386486053467, -0.21461017429828644, 0, -0.4184120297431946, 0.8528685569763184, + 0.31232455372810364, 0, 0.33682408928871155, -0.1736481785774231, 0.9254165887832642, 0, 10, 20, 30, 1 + ] + ); }); it("set world matrix", () => { - const node = new Entity(); + const node = new Entity(null); const transform = node.transform; transform.worldMatrix = new Matrix( 0.8434932827949524, @@ -323,102 +224,91 @@ describe("Transform", () => { 30, 1 ); - arrayCloseTo(transform.position, new Vector3(10, 20, 30)); - arrayCloseTo(transform.worldPosition, new Vector3(10, 20, 30)); - arrayCloseTo(transform.rotation, new Vector3(10, 20, 30)); - arrayCloseTo(transform.worldRotation, new Vector3(10, 20, 30)); - arrayCloseTo( + vector3CloseTo(transform.position, new Vector3(10, 20, 30)); + vector3CloseTo(transform.worldPosition, new Vector3(10, 20, 30)); + vector3CloseTo(transform.rotation, new Vector3(10, 20, 30)); + vector3CloseTo(transform.worldRotation, new Vector3(10, 20, 30)); + quaternionCloseTo( transform.rotationQuaternion, new Quaternion(0.12767944069578063, 0.14487812541736914, 0.2392983377447303, 0.9515485246437885) ); - arrayCloseTo( + quaternionCloseTo( transform.worldRotationQuaternion, new Quaternion(0.12767944069578063, 0.14487812541736914, 0.2392983377447303, 0.9515485246437885) ); - arrayCloseTo(transform.localMatrix.elements, [ - 0.8434932827949524, - 0.49240386486053467, - -0.21461017429828644, - 0, - -0.4184120297431946, - 0.8528685569763184, - 0.31232455372810364, - 0, - 0.33682408928871155, - -0.1736481785774231, - 0.9254165887832642, - 0, - 10, - 20, - 30, - 1 - ]); + arrayCloseTo( + transform.localMatrix.elements, + [ + 0.8434932827949524, 0.49240386486053467, -0.21461017429828644, 0, -0.4184120297431946, 0.8528685569763184, + 0.31232455372810364, 0, 0.33682408928871155, -0.1736481785774231, 0.9254165887832642, 0, 10, 20, 30, 1 + ] + ); }); it("get up, right, forward", () => { - const node = new Entity(); + const node = new Entity(null); const up = new Vector3(); const right = new Vector3(); const forward = new Vector3(); node.transform.getWorldUp(up); node.transform.getWorldRight(right); node.transform.getWorldForward(forward); - arrayCloseTo(up, new Vector3(0, 1, 0)); - arrayCloseTo(right, new Vector3(1, 0, 0)); - arrayCloseTo(forward, new Vector3(0, 0, -1)); + vector3CloseTo(up, new Vector3(0, 1, 0)); + vector3CloseTo(right, new Vector3(1, 0, 0)); + vector3CloseTo(forward, new Vector3(0, 0, -1)); }); }); describe("with parent", () => { it("set parent position, child position", () => { - const parent = new Entity("parent"); - const child = new Entity("child"); + const parent = new Entity(null, "parent"); + const child = new Entity(null, "child"); parent.addChild(child); parent.transform.position = new Vector3(10, 20, 30); child.transform.position = new Vector3(10, 20, 30); - arrayCloseTo(child.worldPosition, new Vector3(20, 40, 60)); + vector3CloseTo(child.transform.worldPosition, new Vector3(20, 40, 60)); }); it("set parent rotation, child rotation", () => { - const parent = new Entity("parent"); - const child = new Entity("child"); + const parent = new Entity(null, "parent"); + const child = new Entity(null, "child"); parent.addChild(child); parent.transform.rotation = new Vector3(90, 0, 0); child.transform.rotation = new Vector3(90, 0, 0); - arrayCloseTo(child.transform.worldRotation, new Vector3(0, 180, 180)); + vector3CloseTo(child.transform.worldRotation, new Vector3(0, 180, 180)); }); it("set parent rotation, child position", () => { - const parent = new Entity("parent"); - const child = new Entity("child"); + const parent = new Entity(null, "parent"); + const child = new Entity(null, "child"); parent.addChild(child); parent.transform.rotation = new Vector3(90, 0, 0); child.transform.position = new Vector3(0, 0, 10); - arrayCloseTo(child.worldPosition, new Vector3(0, -10, 0)); - arrayCloseTo(child.transform.worldRotation, new Vector3(90, 0, 0)); + vector3CloseTo(child.transform.worldPosition, new Vector3(0, -10, 0)); + vector3CloseTo(child.transform.worldRotation, new Vector3(90, 0, 0)); }); it("set parent scale, child position", () => { - const parent = new Entity("parent"); - const child = new Entity("child"); + const parent = new Entity(null, "parent"); + const child = new Entity(null, "child"); parent.addChild(child); - parent.scale = new Vector3(2, 2, 2); + parent.transform.scale = new Vector3(2, 2, 2); child.transform.position = new Vector3(0, 0, 10); - arrayCloseTo(child.transform.worldPosition, new Vector3(0, 0, 20)); + vector3CloseTo(child.transform.worldPosition, new Vector3(0, 0, 20)); }); it("set parent position, rotation, scale, child lossy world scale", () => { - const parent = new Entity("parent"); - const child = new Entity("child"); + const parent = new Entity(null, "parent"); + const child = new Entity(null, "child"); parent.addChild(child); - parent.scale = new Vector3(2, 2, 2); + parent.transform.scale = new Vector3(2, 2, 2); child.transform.rotation = new Vector3(60, 60, 60); - arrayCloseTo(child.transform.lossyWorldScale, new Vector3(2, 2, 2)); + vector3CloseTo(child.transform.lossyWorldScale, new Vector3(2, 2, 2)); }); it("set parent local matrix, child position", () => { - const parent = new Entity("parent"); - const child = new Entity("child"); + const parent = new Entity(null, "parent"); + const child = new Entity(null, "child"); parent.addChild(child); child.transform.position = new Vector3(0, 0, 10); parent.transform.localMatrix = new Matrix( @@ -439,17 +329,17 @@ describe("Transform", () => { 30, 1 ); - arrayCloseTo( + vector3CloseTo( child.transform.worldPosition, new Vector3(13.368241310119629, 18.263517379760742, 39.25416564941406) ); - arrayCloseTo(child.transform.worldRotation, new Vector3(10, 20, 30)); - arrayCloseTo(child.transform.lossyWorldScale, new Vector3(1, 1, 1)); + vector3CloseTo(child.transform.worldRotation, new Vector3(10, 20, 30)); + vector3CloseTo(child.transform.lossyWorldScale, new Vector3(1, 1, 1)); }); it("set parent world matrix, child position", () => { - const parent = new Entity("parent"); - const child = new Entity("child"); + const parent = new Entity(null, "parent"); + const child = new Entity(null, "child"); parent.addChild(child); child.transform.position = new Vector3(0, 0, 10); parent.transform.worldMatrix = new Matrix( @@ -470,44 +360,44 @@ describe("Transform", () => { 30, 1 ); - arrayCloseTo( + vector3CloseTo( child.transform.worldPosition, new Vector3(13.368241310119629, 18.263517379760742, 39.25416564941406) ); - arrayCloseTo(child.transform.worldRotation, new Vector3(10, 20, 30)); - arrayCloseTo(child.transform.lossyWorldScale, new Vector3(1, 1, 1)); + vector3CloseTo(child.transform.worldRotation, new Vector3(10, 20, 30)); + vector3CloseTo(child.transform.lossyWorldScale, new Vector3(1, 1, 1)); }); it("set parent position, child world position", () => { - const parent = new Entity("parent"); - const child = new Entity("child"); + const parent = new Entity(null, "parent"); + const child = new Entity(null, "child"); parent.addChild(child); parent.transform.position = new Vector3(10, 0, 0); child.transform.worldPosition = new Vector3(20, 10, 0); - arrayCloseTo(child.transform.position, new Vector3(10, 10, 0)); + vector3CloseTo(child.transform.position, new Vector3(10, 10, 0)); }); it("set parent position, child world rotation", () => { - const parent = new Entity("parent"); - const child = new Entity("child"); + const parent = new Entity(null, "parent"); + const child = new Entity(null, "child"); parent.addChild(child); parent.transform.position = new Vector3(10, 0, 0); child.transform.worldRotation = new Vector3(0, 90, 0); - arrayCloseTo(child.transform.rotation, new Vector3(0, 90, 0)); + vector3CloseTo(child.transform.rotation, new Vector3(0, 90, 0)); }); it("set parent rotation, child world rotation", () => { - const parent = new Entity("parent"); - const child = new Entity("child"); + const parent = new Entity(null, "parent"); + const child = new Entity(null, "child"); parent.addChild(child); parent.transform.rotation = new Vector3(0, 90, 0); child.transform.worldRotation = new Vector3(0, 100, 0); - arrayCloseTo(child.transform.rotation, new Vector3(0, 10, 0)); + vector3CloseTo(child.transform.rotation, new Vector3(0, 10, 0)); }); it("set parent rotation, child world quat", () => { - const parent = new Entity("parent"); - const child = new Entity("child"); + const parent = new Entity(null, "parent"); + const child = new Entity(null, "child"); parent.addChild(child); parent.transform.rotation = new Vector3(10, 20, 30); child.transform.worldRotationQuaternion = new Quaternion( @@ -516,12 +406,12 @@ describe("Transform", () => { 0.2392983377447303, 0.9515485246437885 ); - arrayCloseTo(child.transform.rotation, new Vector3()); + vector3CloseTo(child.transform.rotation, new Vector3()); }); it("set parent local matrix, child world matrix", () => { - const parent = new Entity("parent"); - const child = new Entity("child"); + const parent = new Entity(null, "parent"); + const child = new Entity(null, "child"); parent.addChild(child); parent.transform.localMatrix = new Matrix( 0.813797652721405, @@ -559,52 +449,49 @@ describe("Transform", () => { 30, 1 ); - arrayCloseTo(child.transform.position, new Vector3()); - arrayCloseTo(child.transform.rotation, new Vector3()); - arrayCloseTo(child.transform.scale, new Vector3(1, 1, 1)); + vector3CloseTo(child.transform.position, new Vector3()); + vector3CloseTo(child.transform.rotation, new Vector3()); + vector3CloseTo(child.transform.scale, new Vector3(1, 1, 1)); }); }); describe("func", () => { it("translate", () => { - const parent = new Entity("parent"); - const child = new Entity("child"); + const parent = new Entity(null, "parent"); + const child = new Entity(null, "child"); parent.addChild(child); parent.transform.translate(new Vector3(10, 20, 30), false); child.transform.translate(new Vector3(10, 20, 30), true); - arrayCloseTo(child.transform.position, new Vector3(10, 20, 30)); + vector3CloseTo(child.transform.position, new Vector3(10, 20, 30)); child.transform.translate(new Vector3(10, 20, 30), false); - arrayCloseTo(child.transform.position, new Vector3(0, 0, 0)); + vector3CloseTo(child.transform.position, new Vector3(0, 0, 0)); }); - it("rotate", () => { - const parent = new Entity("parent"); - const child = new Entity("child"); + const parent = new Entity(null, "parent"); + const child = new Entity(null, "child"); parent.addChild(child); parent.transform.rotate(new Vector3(0, 0, 180), false); child.transform.rotate(new Vector3(0, 0, 45), true); - arrayCloseTo(child.transform.rotation, new Vector3(0, 0, 45)); + vector3CloseTo(child.transform.rotation, new Vector3(0, 0, 45)); child.transform.rotate(new Vector3(0, 0, 45), false); - arrayCloseTo(child.transform.worldRotation, new Vector3(0, 0, -90)); - }); - - it("rotateByAxis", () => { - const parent = new Entity("parent"); - const child = new Entity("child"); - parent.addChild(child); - child.transform.position = new Vector3(10, 0, 0); - parent.transform.rotateByAxis(new Vector3(0, 0, 1), 90, true); - arrayCloseTo(child.transform.worldPosition, new Vector3(0, 10, 0)); - child.transform.rotateByAxis(new Vector3(0, 0, 1), 180, false); - arrayCloseTo(child.transform.worldPosition, new Vector3(0, 10, 0)); - }); - - it("lookAt", () => { - const node = new Entity(); - node.transform.position = new Vector3(0, 0, 1); - node.transform.lookAt(new Vector3(), new Vector3(0, 1, 0)); - arrayCloseTo(node.transform.worldRotation, new Vector3(0, 0, 0)); + vector3CloseTo(child.transform.worldRotation, new Vector3(0, 0, -90)); }); + // it("rotateByAxis", () => { + // const parent = new Entity(null,"parent"); + // const child = new Entity(null,"child"); + // parent.addChild(child); + // child.transform.position = new Vector3(10, 0, 0); + // parent.transform.rotateByAxis(new Vector3(0, 0, 1), 90, true); + // vector3CloseTo(child.transform.worldPosition, new Vector3(0, 10, 0)); + // child.transform.rotateByAxis(new Vector3(0, 0, 1), 180, false); + // vector3CloseTo(child.transform.worldPosition, new Vector3(0, 10, 0)); + // }); + // it("lookAt", () => { + // const node = new Entity(null); + // node.transform.position = new Vector3(0, 0, 1); + // node.transform.lookAt(new Vector3(), new Vector3(0, 1, 0)); + // vector3CloseTo(node.transform.worldRotation, new Vector3(0, 0, 0)); + // }); }); }); @@ -629,6 +516,19 @@ function arrayCloseTo(arr1, arr2) { } } +function vector3CloseTo(vec1, vec2) { + expect(vec1.x).toBeCloseTo(vec2.x); + expect(vec1.y).toBeCloseTo(vec2.y); + expect(vec1.z).toBeCloseTo(vec2.z); +} + +function quaternionCloseTo(qua1, qua2) { + expect(qua1.x).toBeCloseTo(qua2.x); + expect(qua1.y).toBeCloseTo(qua2.y); + expect(qua1.z).toBeCloseTo(qua2.z); + expect(qua1.w).toBeCloseTo(qua2.w); +} + function isFloat32Array(arr) { if (Object.prototype.toString.call(arr) === "[object Float32Array]") return true; return false; diff --git a/packages/math/src/Quaternion.ts b/packages/math/src/Quaternion.ts index e2385b3f82..1a4c745879 100644 --- a/packages/math/src/Quaternion.ts +++ b/packages/math/src/Quaternion.ts @@ -23,7 +23,7 @@ export class Quaternion implements IClone { out._y = left._y + right._y; out._z = left._z + right._z; out._w = left._w + right._w; - out._onValueChanged && out._onValueChanged(); + out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); } /** @@ -46,7 +46,7 @@ export class Quaternion implements IClone { 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(); + out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); } /** @@ -59,7 +59,7 @@ export class Quaternion implements IClone { out._y = -a._y; out._z = -a._z; out._w = a._w; - out._onValueChanged && out._onValueChanged(); + out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); } /** @@ -102,7 +102,7 @@ export class Quaternion implements IClone { out._y = normalAxis._y * s; out._z = normalAxis._z * s; out._w = Math.cos(rad); - out._onValueChanged && out._onValueChanged(); + out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); } /** @@ -142,7 +142,7 @@ export class Quaternion implements IClone { out._y = sinYaw * cosPitch * cosRoll - cosYaw * sinPitch * sinRoll; out._z = cosYawPitch * sinRoll - sinYawPitch * cosRoll; out._w = cosYawPitch * cosRoll + sinYawPitch * sinRoll; - out._onValueChanged && out._onValueChanged(); + out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); } /** @@ -197,7 +197,7 @@ export class Quaternion implements IClone { out._z = 0.5 * sqrt; out._w = (m12 - m21) * half; } - out._onValueChanged && out._onValueChanged(); + out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); } /** @@ -214,7 +214,7 @@ export class Quaternion implements IClone { out._y = -y * invDot; out._z = -z * invDot; out._w = w * invDot; - out._onValueChanged && out._onValueChanged(); + out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); } } @@ -288,7 +288,7 @@ export class Quaternion implements IClone { out._y = scale0 * ay + scale1 * by; out._z = scale0 * az + scale1 * bz; out._w = scale0 * aw + scale1 * bw; - out._onValueChanged && out._onValueChanged(); + out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); } /** @@ -305,7 +305,7 @@ export class Quaternion implements IClone { out._y = _y * len; out._z = _z * len; out._w = _w * len; - out._onValueChanged && out._onValueChanged(); + out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); } } @@ -323,7 +323,7 @@ export class Quaternion implements IClone { out._y = 0; out._z = 0; out._w = c; - out._onValueChanged && out._onValueChanged(); + out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); } /** @@ -340,7 +340,7 @@ export class Quaternion implements IClone { out._y = s; out._z = 0; out._w = c; - out._onValueChanged && out._onValueChanged(); + out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); } /** @@ -357,7 +357,7 @@ export class Quaternion implements IClone { out._y = 0; out._z = s; out._w = c; - out._onValueChanged && out._onValueChanged(); + out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); } /** @@ -376,7 +376,7 @@ export class Quaternion implements IClone { out._y = _y * bw + _z * bx; out._z = _z * bw - _y * bx; out._w = _w * bw - _x * bx; - out._onValueChanged && out._onValueChanged(); + out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); } /** @@ -395,7 +395,7 @@ export class Quaternion implements IClone { out._y = _y * bw + _w * by; out._z = _z * bw + _x * by; out._w = _w * bw - _y * by; - out._onValueChanged && out._onValueChanged(); + out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); } /** @@ -414,7 +414,7 @@ export class Quaternion implements IClone { out._y = _y * bw - _x * bz; out._z = _z * bw + _w * bz; out._w = _w * bw - _z * bz; - out._onValueChanged && out._onValueChanged(); + out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); } /** @@ -428,7 +428,7 @@ export class Quaternion implements IClone { out._y = a._y * s; out._z = a._z * s; out._w = a._w * s; - out._onValueChanged && out._onValueChanged(); + out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); } /** @internal */ @@ -439,6 +439,7 @@ export class Quaternion implements IClone { _z: number; /** @internal */ _w: number; + _disableValueChanged: boolean = false; /** @internal */ _onValueChanged: () => void = null; @@ -451,7 +452,7 @@ export class Quaternion implements IClone { public set x(value: number) { this._x = value; - this._onValueChanged && this._onValueChanged(); + this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); } /** @@ -463,7 +464,7 @@ export class Quaternion implements IClone { public set y(value: number) { this._y = value; - this._onValueChanged && this._onValueChanged(); + this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); } /** @@ -475,7 +476,7 @@ export class Quaternion implements IClone { public set z(value: number) { this._z = value; - this._onValueChanged && this._onValueChanged(); + this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); } /** @@ -487,7 +488,7 @@ export class Quaternion implements IClone { public set w(value: number) { this._w = value; - this._onValueChanged && this._onValueChanged(); + this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); } /** @@ -517,7 +518,7 @@ export class Quaternion implements IClone { this._y = y; this._z = z; this._w = w; - this._onValueChanged && this._onValueChanged(); + this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); return this; } @@ -532,7 +533,7 @@ export class Quaternion implements IClone { this._y = array[offset + 1]; this._z = array[offset + 2]; this._w = array[offset + 3]; - this._onValueChanged && this._onValueChanged(); + this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); return this; } @@ -544,7 +545,7 @@ export class Quaternion implements IClone { this._x *= -1; this._y *= -1; this._z *= -1; - this._onValueChanged && this._onValueChanged(); + this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); return this; } @@ -582,7 +583,7 @@ export class Quaternion implements IClone { this._y = 0; this._z = 0; this._w = 1; - this._onValueChanged && this._onValueChanged(); + this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); return this; } @@ -624,7 +625,7 @@ export class Quaternion implements IClone { const t = out._x; out._x = out._y; out._y = t; - out._onValueChanged && out._onValueChanged(); + out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); return out; } @@ -635,7 +636,7 @@ export class Quaternion implements IClone { */ toYawPitchRoll(out: Vector3): Vector3 { this._toYawPitchRoll(out); - out._onValueChanged && out._onValueChanged(); + out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); return out; } @@ -669,7 +670,7 @@ export class Quaternion implements IClone { out._y = this._y; out._z = this._z; out._w = this._w; - out._onValueChanged && out._onValueChanged(); + out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); return out; } diff --git a/packages/math/src/Vector3.ts b/packages/math/src/Vector3.ts index a28a47ec15..80f5a8d18a 100644 --- a/packages/math/src/Vector3.ts +++ b/packages/math/src/Vector3.ts @@ -23,7 +23,7 @@ export class Vector3 implements IClone { out._x = left._x + right._x; out._y = left._y + right._y; out._z = left._z + right._z; - out._onValueChanged && out._onValueChanged(); + out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); } /** @@ -36,7 +36,7 @@ export class Vector3 implements IClone { out._x = left._x - right._x; out._y = left._y - right._y; out._z = left._z - right._z; - out._onValueChanged && out._onValueChanged(); + out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); } /** @@ -49,7 +49,7 @@ export class Vector3 implements IClone { out._x = left._x * right._x; out._y = left._y * right._y; out._z = left._z * right._z; - out._onValueChanged && out._onValueChanged(); + out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); } /** @@ -62,7 +62,7 @@ export class Vector3 implements IClone { out._x = left._x / right._x; out._y = left._y / right._y; out._z = left._z / right._z; - out._onValueChanged && out._onValueChanged(); + out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); } /** @@ -142,7 +142,7 @@ export class Vector3 implements IClone { 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(); + out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); } /** @@ -155,7 +155,7 @@ export class Vector3 implements IClone { 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(); + out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); } /** @@ -168,7 +168,7 @@ export class Vector3 implements IClone { 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(); + out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); } /** @@ -180,7 +180,7 @@ export class Vector3 implements IClone { out._x = -a._x; out._y = -a._y; out._z = -a._z; - out._onValueChanged && out._onValueChanged(); + out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); } /** @@ -207,7 +207,7 @@ export class Vector3 implements IClone { out._x = a._x * s; out._y = a._y * s; out._z = a._z * s; - out._onValueChanged && out._onValueChanged(); + out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); } /** @@ -228,7 +228,7 @@ export class Vector3 implements IClone { 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(); + out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); } /** @@ -244,7 +244,7 @@ export class Vector3 implements IClone { 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(); + out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); } /** @@ -260,7 +260,7 @@ export class Vector3 implements IClone { 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(); + // out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); } /** @@ -285,7 +285,7 @@ export class Vector3 implements IClone { 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(); + out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); } /** @@ -308,7 +308,7 @@ export class Vector3 implements IClone { 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(); + out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); } /** @internal */ @@ -317,6 +317,8 @@ export class Vector3 implements IClone { _y: number; /** @internal */ _z: number; + + _disableValueChanged: boolean = false; /** @internal */ _onValueChanged: () => void = null; @@ -329,7 +331,7 @@ export class Vector3 implements IClone { public set x(value: number) { this._x = value; - this._onValueChanged && this._onValueChanged(); + this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); } /** @@ -341,7 +343,7 @@ export class Vector3 implements IClone { public set y(value: number) { this._y = value; - this._onValueChanged && this._onValueChanged(); + this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); } /** @@ -353,7 +355,7 @@ export class Vector3 implements IClone { public set z(value: number) { this._z = value; - this._onValueChanged && this._onValueChanged(); + this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); } /** @@ -379,7 +381,7 @@ export class Vector3 implements IClone { this._x = x; this._y = y; this._z = z; - this._onValueChanged && this._onValueChanged(); + this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); return this; } @@ -393,7 +395,7 @@ export class Vector3 implements IClone { this._x = array[offset]; this._y = array[offset + 1]; this._z = array[offset + 2]; - this._onValueChanged && this._onValueChanged(); + this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); return this; } @@ -406,7 +408,7 @@ export class Vector3 implements IClone { this._x += right._x; this._y += right._y; this._z += right._z; - this._onValueChanged && this._onValueChanged(); + this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); return this; } @@ -419,7 +421,7 @@ export class Vector3 implements IClone { this._x -= right._x; this._y -= right._y; this._z -= right._z; - this._onValueChanged && this._onValueChanged(); + this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); return this; } @@ -432,7 +434,7 @@ export class Vector3 implements IClone { this._x *= right._x; this._y *= right._y; this._z *= right._z; - this._onValueChanged && this._onValueChanged(); + this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); return this; } @@ -445,7 +447,7 @@ export class Vector3 implements IClone { this._x /= right._x; this._y /= right._y; this._z /= right._z; - this._onValueChanged && this._onValueChanged(); + this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); return this; } @@ -475,7 +477,7 @@ export class Vector3 implements IClone { this._x = -this._x; this._y = -this._y; this._z = -this._z; - this._onValueChanged && this._onValueChanged(); + this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); return this; } @@ -497,7 +499,7 @@ export class Vector3 implements IClone { this._x *= s; this._y *= s; this._z *= s; - this._onValueChanged && this._onValueChanged(); + this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); return this; } @@ -529,7 +531,7 @@ export class Vector3 implements IClone { out._x = this._x; out._y = this._y; out._z = this._z; - out._onValueChanged && out._onValueChanged(); + out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); return out; } From bcbe3b24a51e29c8cbfbdae55a93ff9b47182ba6 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Mon, 7 Mar 2022 14:20:00 +0800 Subject: [PATCH 10/20] refactor: opt code --- packages/core/src/Transform.ts | 153 ++++++++++++++++++-------------- packages/math/src/Quaternion.ts | 55 ++++++------ packages/math/src/Vector3.ts | 54 ++++++----- 3 files changed, 137 insertions(+), 125 deletions(-) diff --git a/packages/core/src/Transform.ts b/packages/core/src/Transform.ts index 5837a340e9..00ff8b5d4c 100644 --- a/packages/core/src/Transform.ts +++ b/packages/core/src/Transform.ts @@ -65,13 +65,15 @@ export class Transform extends Component { */ get worldPosition(): Vector3 { if (this._isContainDirtyFlag(TransformFlag.WorldPosition)) { - this._worldPosition._disableValueChanged = true; + //@ts-ignore + this._worldPosition._onValueChanged = null; if (this._getParentTransform()) { this.worldMatrix.getTranslation(this._worldPosition); } else { this._position.cloneTo(this._worldPosition); } - this._worldPosition._disableValueChanged = false; + //@ts-ignore + this._worldPosition._onValueChanged = this._onWorldPositionChanged; this._setDirtyFlagFalse(TransformFlag.WorldPosition); } @@ -90,9 +92,11 @@ export class Transform extends Component { */ get rotation(): Vector3 { if (this._isContainDirtyFlag(TransformFlag.LocalEuler)) { - this._rotation._disableValueChanged = true; + //@ts-ignore + this._rotation._onValueChanged = null; this._rotationQuaternion.toEuler(this._rotation); - this._rotation._disableValueChanged = false; + //@ts-ignore + this._rotation._onValueChanged = this._onRotationChanged; this._rotation.scale(MathUtil.radToDegreeFactor); // radians to degrees this._setDirtyFlagFalse(TransformFlag.LocalEuler); } @@ -112,10 +116,12 @@ export class Transform extends Component { */ get worldRotation(): Vector3 { if (this._isContainDirtyFlag(TransformFlag.WorldEuler)) { - this._worldRotation._disableValueChanged = true; + //@ts-ignore + this._worldRotation._onValueChanged = null; this.worldRotationQuaternion.toEuler(this._worldRotation); this._worldRotation.scale(MathUtil.radToDegreeFactor); // Radian to angle - this._worldRotation._disableValueChanged = false; + //@ts-ignore + this._worldRotation._onValueChanged = this._onWorldRotationChanged; this._setDirtyFlagFalse(TransformFlag.WorldEuler); } return this._worldRotation; @@ -132,14 +138,16 @@ export class Transform extends Component { */ get rotationQuaternion(): Quaternion { if (this._isContainDirtyFlag(TransformFlag.LocalQuat)) { - this._rotationQuaternion._disableValueChanged = true; + //@ts-ignore + this._rotationQuaternion._onValueChanged = null; Quaternion.rotationEuler( MathUtil.degreeToRadian(this._rotation.x), MathUtil.degreeToRadian(this._rotation.y), MathUtil.degreeToRadian(this._rotation.z), this._rotationQuaternion ); - this._rotationQuaternion._disableValueChanged = false; + //@ts-ignore + this._rotationQuaternion._onValueChanged = this._onRotationQuaternionChanged; this._setDirtyFlagFalse(TransformFlag.LocalQuat); } return this._rotationQuaternion; @@ -156,14 +164,16 @@ export class Transform extends Component { */ get worldRotationQuaternion(): Quaternion { if (this._isContainDirtyFlag(TransformFlag.WorldQuat)) { - this._worldRotationQuaternion._disableValueChanged = true; + //@ts-ignore + this._worldRotationQuaternion._onValueChanged = null; const parent = this._getParentTransform(); if (parent != null) { Quaternion.multiply(parent.worldRotationQuaternion, this.rotationQuaternion, this._worldRotationQuaternion); } else { this.rotationQuaternion.cloneTo(this._worldRotationQuaternion); } - this._worldRotationQuaternion._disableValueChanged = false; + //@ts-ignore + this._worldRotationQuaternion._onValueChanged = this._onWorldRotationQuaternionChanged; this._setDirtyFlagFalse(TransformFlag.WorldQuat); } return this._worldRotationQuaternion; @@ -195,7 +205,6 @@ export class Transform extends Component { */ get lossyWorldScale(): Vector3 { if (this._isContainDirtyFlag(TransformFlag.WorldScale)) { - this._lossyWorldScale._disableValueChanged = true; if (this._getParentTransform()) { const scaleMat = this._getScaleMatrix(); const e = scaleMat.elements; @@ -203,7 +212,6 @@ export class Transform extends Component { } else { this._scale.cloneTo(this._lossyWorldScale); } - this._lossyWorldScale._disableValueChanged = false; this._setDirtyFlagFalse(TransformFlag.WorldScale); } return this._lossyWorldScale; @@ -267,68 +275,19 @@ export class Transform extends Component { super(entity); //@ts-ignore - this._position._onValueChanged = () => { - this._setDirtyFlagTrue(TransformFlag.LocalMatrix); - this._updateWorldPositionFlag(); - }; - + this._position._onValueChanged = this._onPositionChanged; //@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._setDirtyFlagFalse(TransformFlag.WorldPosition); - }; - + this._worldPosition._onValueChanged = this._onWorldPositionChanged; //@ts-ignore - this._rotation._onValueChanged = () => { - this._setDirtyFlagTrue(TransformFlag.LocalMatrix | TransformFlag.LocalQuat); - this._setDirtyFlagFalse(TransformFlag.LocalEuler); - this._updateWorldRotationFlag(); - }; - + this._rotation._onValueChanged = this._onRotationChanged; //@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._setDirtyFlagFalse(TransformFlag.WorldEuler); - }; - + this._worldRotation._onValueChanged = this._onWorldRotationChanged; //@ts-ignore - this._rotationQuaternion._onValueChanged = () => { - this._setDirtyFlagTrue(TransformFlag.LocalMatrix | TransformFlag.LocalEuler); - this._setDirtyFlagFalse(TransformFlag.LocalQuat); - this._updateWorldRotationFlag(); - }; - + this._rotationQuaternion._onValueChanged = this._onRotationQuaternionChanged; //@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._setDirtyFlagFalse(TransformFlag.WorldQuat); - }; - + this._worldRotationQuaternion._onValueChanged = this._onWorldRotationQuaternionChanged; //@ts-ignore - this._scale._onValueChanged = () => { - this._setDirtyFlagTrue(TransformFlag.LocalMatrix); - this._updateWorldScaleFlag(); - }; + this._scale._onValueChanged = this._onScaleChanged; } /** @@ -728,8 +687,64 @@ export class Transform extends Component { Quaternion.rotationEuler(x * radFactor, y * radFactor, z * radFactor, rotQuat); this._rotateByQuat(rotQuat, relativeToLocal); } -} + private _onPositionChanged: () => void = () => { + this._setDirtyFlagTrue(TransformFlag.LocalMatrix); + this._updateWorldPositionFlag(); + }; + + private _onWorldPositionChanged: () => void = () => { + 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._setDirtyFlagFalse(TransformFlag.WorldPosition); + }; + + private _onRotationChanged: () => void = () => { + this._setDirtyFlagTrue(TransformFlag.LocalMatrix | TransformFlag.LocalQuat); + this._setDirtyFlagFalse(TransformFlag.LocalEuler); + this._updateWorldRotationFlag(); + }; + + private _onWorldRotationChanged: () => void = () => { + const worldRotation = this._worldRotation; + Quaternion.rotationEuler( + MathUtil.degreeToRadian(worldRotation.x), + MathUtil.degreeToRadian(worldRotation.y), + MathUtil.degreeToRadian(worldRotation.z), + this._worldRotationQuaternion + ); + this._setDirtyFlagFalse(TransformFlag.WorldEuler); + }; + + private _onRotationQuaternionChanged: () => void = () => { + this._setDirtyFlagTrue(TransformFlag.LocalMatrix | TransformFlag.LocalEuler); + this._setDirtyFlagFalse(TransformFlag.LocalQuat); + this._updateWorldRotationFlag(); + }; + + private _onWorldRotationQuaternionChanged: () => void = () => { + 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._setDirtyFlagFalse(TransformFlag.WorldQuat); + }; + + private _onScaleChanged: () => void = () => { + this._setDirtyFlagTrue(TransformFlag.LocalMatrix); + this._updateWorldScaleFlag(); + }; +} /** * Dirty flag of transform. */ diff --git a/packages/math/src/Quaternion.ts b/packages/math/src/Quaternion.ts index 1a4c745879..e2385b3f82 100644 --- a/packages/math/src/Quaternion.ts +++ b/packages/math/src/Quaternion.ts @@ -23,7 +23,7 @@ export class Quaternion implements IClone { out._y = left._y + right._y; out._z = left._z + right._z; out._w = left._w + right._w; - out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); + out._onValueChanged && out._onValueChanged(); } /** @@ -46,7 +46,7 @@ export class Quaternion implements IClone { 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._disableValueChanged && out._onValueChanged(); + out._onValueChanged && out._onValueChanged(); } /** @@ -59,7 +59,7 @@ export class Quaternion implements IClone { out._y = -a._y; out._z = -a._z; out._w = a._w; - out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); + out._onValueChanged && out._onValueChanged(); } /** @@ -102,7 +102,7 @@ export class Quaternion implements IClone { out._y = normalAxis._y * s; out._z = normalAxis._z * s; out._w = Math.cos(rad); - out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); + out._onValueChanged && out._onValueChanged(); } /** @@ -142,7 +142,7 @@ export class Quaternion implements IClone { out._y = sinYaw * cosPitch * cosRoll - cosYaw * sinPitch * sinRoll; out._z = cosYawPitch * sinRoll - sinYawPitch * cosRoll; out._w = cosYawPitch * cosRoll + sinYawPitch * sinRoll; - out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); + out._onValueChanged && out._onValueChanged(); } /** @@ -197,7 +197,7 @@ export class Quaternion implements IClone { out._z = 0.5 * sqrt; out._w = (m12 - m21) * half; } - out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); + out._onValueChanged && out._onValueChanged(); } /** @@ -214,7 +214,7 @@ export class Quaternion implements IClone { out._y = -y * invDot; out._z = -z * invDot; out._w = w * invDot; - out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); + out._onValueChanged && out._onValueChanged(); } } @@ -288,7 +288,7 @@ export class Quaternion implements IClone { out._y = scale0 * ay + scale1 * by; out._z = scale0 * az + scale1 * bz; out._w = scale0 * aw + scale1 * bw; - out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); + out._onValueChanged && out._onValueChanged(); } /** @@ -305,7 +305,7 @@ export class Quaternion implements IClone { out._y = _y * len; out._z = _z * len; out._w = _w * len; - out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); + out._onValueChanged && out._onValueChanged(); } } @@ -323,7 +323,7 @@ export class Quaternion implements IClone { out._y = 0; out._z = 0; out._w = c; - out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); + out._onValueChanged && out._onValueChanged(); } /** @@ -340,7 +340,7 @@ export class Quaternion implements IClone { out._y = s; out._z = 0; out._w = c; - out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); + out._onValueChanged && out._onValueChanged(); } /** @@ -357,7 +357,7 @@ export class Quaternion implements IClone { out._y = 0; out._z = s; out._w = c; - out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); + out._onValueChanged && out._onValueChanged(); } /** @@ -376,7 +376,7 @@ export class Quaternion implements IClone { out._y = _y * bw + _z * bx; out._z = _z * bw - _y * bx; out._w = _w * bw - _x * bx; - out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); + out._onValueChanged && out._onValueChanged(); } /** @@ -395,7 +395,7 @@ export class Quaternion implements IClone { out._y = _y * bw + _w * by; out._z = _z * bw + _x * by; out._w = _w * bw - _y * by; - out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); + out._onValueChanged && out._onValueChanged(); } /** @@ -414,7 +414,7 @@ export class Quaternion implements IClone { out._y = _y * bw - _x * bz; out._z = _z * bw + _w * bz; out._w = _w * bw - _z * bz; - out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); + out._onValueChanged && out._onValueChanged(); } /** @@ -428,7 +428,7 @@ export class Quaternion implements IClone { out._y = a._y * s; out._z = a._z * s; out._w = a._w * s; - out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); + out._onValueChanged && out._onValueChanged(); } /** @internal */ @@ -439,7 +439,6 @@ export class Quaternion implements IClone { _z: number; /** @internal */ _w: number; - _disableValueChanged: boolean = false; /** @internal */ _onValueChanged: () => void = null; @@ -452,7 +451,7 @@ export class Quaternion implements IClone { public set x(value: number) { this._x = value; - this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); + this._onValueChanged && this._onValueChanged(); } /** @@ -464,7 +463,7 @@ export class Quaternion implements IClone { public set y(value: number) { this._y = value; - this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); + this._onValueChanged && this._onValueChanged(); } /** @@ -476,7 +475,7 @@ export class Quaternion implements IClone { public set z(value: number) { this._z = value; - this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); + this._onValueChanged && this._onValueChanged(); } /** @@ -488,7 +487,7 @@ export class Quaternion implements IClone { public set w(value: number) { this._w = value; - this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); + this._onValueChanged && this._onValueChanged(); } /** @@ -518,7 +517,7 @@ export class Quaternion implements IClone { this._y = y; this._z = z; this._w = w; - this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); + this._onValueChanged && this._onValueChanged(); return this; } @@ -533,7 +532,7 @@ export class Quaternion implements IClone { this._y = array[offset + 1]; this._z = array[offset + 2]; this._w = array[offset + 3]; - this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); + this._onValueChanged && this._onValueChanged(); return this; } @@ -545,7 +544,7 @@ export class Quaternion implements IClone { this._x *= -1; this._y *= -1; this._z *= -1; - this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); + this._onValueChanged && this._onValueChanged(); return this; } @@ -583,7 +582,7 @@ export class Quaternion implements IClone { this._y = 0; this._z = 0; this._w = 1; - this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); + this._onValueChanged && this._onValueChanged(); return this; } @@ -625,7 +624,7 @@ export class Quaternion implements IClone { const t = out._x; out._x = out._y; out._y = t; - out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); + out._onValueChanged && out._onValueChanged(); return out; } @@ -636,7 +635,7 @@ export class Quaternion implements IClone { */ toYawPitchRoll(out: Vector3): Vector3 { this._toYawPitchRoll(out); - out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); + out._onValueChanged && out._onValueChanged(); return out; } @@ -670,7 +669,7 @@ export class Quaternion implements IClone { out._y = this._y; out._z = this._z; out._w = this._w; - out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); + out._onValueChanged && out._onValueChanged(); return out; } diff --git a/packages/math/src/Vector3.ts b/packages/math/src/Vector3.ts index 80f5a8d18a..a28a47ec15 100644 --- a/packages/math/src/Vector3.ts +++ b/packages/math/src/Vector3.ts @@ -23,7 +23,7 @@ export class Vector3 implements IClone { out._x = left._x + right._x; out._y = left._y + right._y; out._z = left._z + right._z; - out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); + out._onValueChanged && out._onValueChanged(); } /** @@ -36,7 +36,7 @@ export class Vector3 implements IClone { out._x = left._x - right._x; out._y = left._y - right._y; out._z = left._z - right._z; - out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); + out._onValueChanged && out._onValueChanged(); } /** @@ -49,7 +49,7 @@ export class Vector3 implements IClone { out._x = left._x * right._x; out._y = left._y * right._y; out._z = left._z * right._z; - out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); + out._onValueChanged && out._onValueChanged(); } /** @@ -62,7 +62,7 @@ export class Vector3 implements IClone { out._x = left._x / right._x; out._y = left._y / right._y; out._z = left._z / right._z; - out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); + out._onValueChanged && out._onValueChanged(); } /** @@ -142,7 +142,7 @@ export class Vector3 implements IClone { out._x = _x + (end._x - _x) * t; out._y = _y + (end._y - _y) * t; out._z = _z + (end._z - _z) * t; - out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); + out._onValueChanged && out._onValueChanged(); } /** @@ -155,7 +155,7 @@ export class Vector3 implements IClone { 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._disableValueChanged && out._onValueChanged(); + out._onValueChanged && out._onValueChanged(); } /** @@ -168,7 +168,7 @@ export class Vector3 implements IClone { 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._disableValueChanged && out._onValueChanged(); + out._onValueChanged && out._onValueChanged(); } /** @@ -180,7 +180,7 @@ export class Vector3 implements IClone { out._x = -a._x; out._y = -a._y; out._z = -a._z; - out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); + out._onValueChanged && out._onValueChanged(); } /** @@ -207,7 +207,7 @@ export class Vector3 implements IClone { out._x = a._x * s; out._y = a._y * s; out._z = a._z * s; - out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); + out._onValueChanged && out._onValueChanged(); } /** @@ -228,7 +228,7 @@ export class Vector3 implements IClone { 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._disableValueChanged && out._onValueChanged(); + out._onValueChanged && out._onValueChanged(); } /** @@ -244,7 +244,7 @@ export class Vector3 implements IClone { 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._disableValueChanged && out._onValueChanged(); + out._onValueChanged && out._onValueChanged(); } /** @@ -260,7 +260,7 @@ export class Vector3 implements IClone { 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._disableValueChanged && out._onValueChanged(); + out._onValueChanged && out._onValueChanged(); } /** @@ -285,7 +285,7 @@ export class Vector3 implements IClone { 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._disableValueChanged && out._onValueChanged(); + out._onValueChanged && out._onValueChanged(); } /** @@ -308,7 +308,7 @@ export class Vector3 implements IClone { 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._disableValueChanged && out._onValueChanged(); + out._onValueChanged && out._onValueChanged(); } /** @internal */ @@ -317,8 +317,6 @@ export class Vector3 implements IClone { _y: number; /** @internal */ _z: number; - - _disableValueChanged: boolean = false; /** @internal */ _onValueChanged: () => void = null; @@ -331,7 +329,7 @@ export class Vector3 implements IClone { public set x(value: number) { this._x = value; - this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); + this._onValueChanged && this._onValueChanged(); } /** @@ -343,7 +341,7 @@ export class Vector3 implements IClone { public set y(value: number) { this._y = value; - this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); + this._onValueChanged && this._onValueChanged(); } /** @@ -355,7 +353,7 @@ export class Vector3 implements IClone { public set z(value: number) { this._z = value; - this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); + this._onValueChanged && this._onValueChanged(); } /** @@ -381,7 +379,7 @@ export class Vector3 implements IClone { this._x = x; this._y = y; this._z = z; - this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); + this._onValueChanged && this._onValueChanged(); return this; } @@ -395,7 +393,7 @@ export class Vector3 implements IClone { this._x = array[offset]; this._y = array[offset + 1]; this._z = array[offset + 2]; - this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); + this._onValueChanged && this._onValueChanged(); return this; } @@ -408,7 +406,7 @@ export class Vector3 implements IClone { this._x += right._x; this._y += right._y; this._z += right._z; - this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); + this._onValueChanged && this._onValueChanged(); return this; } @@ -421,7 +419,7 @@ export class Vector3 implements IClone { this._x -= right._x; this._y -= right._y; this._z -= right._z; - this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); + this._onValueChanged && this._onValueChanged(); return this; } @@ -434,7 +432,7 @@ export class Vector3 implements IClone { this._x *= right._x; this._y *= right._y; this._z *= right._z; - this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); + this._onValueChanged && this._onValueChanged(); return this; } @@ -447,7 +445,7 @@ export class Vector3 implements IClone { this._x /= right._x; this._y /= right._y; this._z /= right._z; - this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); + this._onValueChanged && this._onValueChanged(); return this; } @@ -477,7 +475,7 @@ export class Vector3 implements IClone { this._x = -this._x; this._y = -this._y; this._z = -this._z; - this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); + this._onValueChanged && this._onValueChanged(); return this; } @@ -499,7 +497,7 @@ export class Vector3 implements IClone { this._x *= s; this._y *= s; this._z *= s; - this._onValueChanged && !this._disableValueChanged && this._onValueChanged(); + this._onValueChanged && this._onValueChanged(); return this; } @@ -531,7 +529,7 @@ export class Vector3 implements IClone { out._x = this._x; out._y = this._y; out._z = this._z; - out._onValueChanged && !out._disableValueChanged && out._onValueChanged(); + out._onValueChanged && out._onValueChanged(); return out; } From a2cc6e244712b5c0d483e66200a8e17bdcfca0e2 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Mon, 7 Mar 2022 14:24:15 +0800 Subject: [PATCH 11/20] refactor: opt code --- packages/core/tests/Transform.test.ts | 32 +++++++++++++-------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/packages/core/tests/Transform.test.ts b/packages/core/tests/Transform.test.ts index 9a100dcf07..0da026fb22 100644 --- a/packages/core/tests/Transform.test.ts +++ b/packages/core/tests/Transform.test.ts @@ -476,22 +476,22 @@ describe("Transform", () => { child.transform.rotate(new Vector3(0, 0, 45), false); vector3CloseTo(child.transform.worldRotation, new Vector3(0, 0, -90)); }); - // it("rotateByAxis", () => { - // const parent = new Entity(null,"parent"); - // const child = new Entity(null,"child"); - // parent.addChild(child); - // child.transform.position = new Vector3(10, 0, 0); - // parent.transform.rotateByAxis(new Vector3(0, 0, 1), 90, true); - // vector3CloseTo(child.transform.worldPosition, new Vector3(0, 10, 0)); - // child.transform.rotateByAxis(new Vector3(0, 0, 1), 180, false); - // vector3CloseTo(child.transform.worldPosition, new Vector3(0, 10, 0)); - // }); - // it("lookAt", () => { - // const node = new Entity(null); - // node.transform.position = new Vector3(0, 0, 1); - // node.transform.lookAt(new Vector3(), new Vector3(0, 1, 0)); - // vector3CloseTo(node.transform.worldRotation, new Vector3(0, 0, 0)); - // }); + it("rotateByAxis", () => { + const parent = new Entity(null,"parent"); + const child = new Entity(null,"child"); + parent.addChild(child); + child.transform.position = new Vector3(10, 0, 0); + parent.transform.rotateByAxis(new Vector3(0, 0, 1), 90, true); + vector3CloseTo(child.transform.worldPosition, new Vector3(0, 10, 0)); + child.transform.rotateByAxis(new Vector3(0, 0, 1), 180, false); + vector3CloseTo(child.transform.worldPosition, new Vector3(0, 10, 0)); + }); + it("lookAt", () => { + const node = new Entity(null); + node.transform.position = new Vector3(0, 0, 1); + node.transform.lookAt(new Vector3(), new Vector3(0, 1, 0)); + vector3CloseTo(node.transform.worldRotation, new Vector3(0, 0, 0)); + }); }); }); From 6b13c7a94ea8cdbb31c52004ef780b528accae2b Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Mon, 7 Mar 2022 15:00:25 +0800 Subject: [PATCH 12/20] fix: camera unit test --- packages/core/tests/Camera.test.ts | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/core/tests/Camera.test.ts b/packages/core/tests/Camera.test.ts index d3ec01b148..a4e83a2ad4 100644 --- a/packages/core/tests/Camera.test.ts +++ b/packages/core/tests/Camera.test.ts @@ -24,7 +24,7 @@ describe("camera test", function () { expect(camera.aspectRatio).toEqual(1); expect(camera._renderPipeline).not.toBeUndefined(); expect(camera.entity.transform.worldPosition).not.toBeUndefined(); - expect(camera.viewport).toEqual({ x: 0, y: 0, z: 1, w: 1 }); + vector4CloseTo(camera.viewport, new Vector4(0, 0, 1, 1)); expect(camera.fieldOfView).toEqual(45); expect(camera.isOrthographic).toEqual(false); }); @@ -94,12 +94,10 @@ describe("camera test", function () { camera.projectionMatrix; //@ts-ignore camera._orthographicSize = 4; - const width = (camera.orthographicSize * 400) / 400; const height = camera.orthographicSize; const result = new Matrix(); Matrix.ortho(-width, width, -height, height, camera.nearClipPlane, camera.farClipPlane, result); - expect(camera.projectionMatrix).not.toEqual(result); }); @@ -138,7 +136,7 @@ describe("camera test", function () { ); camera.entity.transform.worldMatrix = new Matrix(); const out = camera.worldToViewportPoint(new Vector3(1, 1, -100), new Vector3()); - expect(out).toEqual({ x: 0.5154036617279053, y: 0.4913397705554962, z: 100 }); + vector3CloseTo(out, new Vector3(0.5154036617279053, 0.4913397705554962, 100)); }); it("viewport to world", () => { @@ -186,7 +184,6 @@ describe("camera test", function () { ); camera.entity.transform.worldMatrix = mat; const ray = camera.viewportPointToRay(new Vector2(0.4472140669822693, 0.4436090290546417), new Ray()); - arrayCloseTo( [ray.origin.x, ray.origin.y, ray.origin.z], Float32Array.from([0.0017142786925635912, 5.017240249493299, 17.047073454417177]) @@ -221,3 +218,16 @@ function arrayCloseTo(arr1: ArrayLike, arr2: ArrayLike) { expect(arr1[i]).toBeCloseTo(arr2[i]); } } + +function vector3CloseTo(vec1: Vector3, vec2: Vector3): void { + expect(vec1.x).toBeCloseTo(vec2.x); + expect(vec1.y).toBeCloseTo(vec2.y); + expect(vec1.z).toBeCloseTo(vec2.z); +} + +function vector4CloseTo(vec1: Vector4, vec2: Vector4): void { + expect(vec1.x).toBeCloseTo(vec2.x); + expect(vec1.y).toBeCloseTo(vec2.y); + expect(vec1.z).toBeCloseTo(vec2.z); + expect(vec1.w).toBeCloseTo(vec2.w); +} From b659d8ff166ae4a98dd3d384afc14527dc6002e1 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Mon, 7 Mar 2022 15:26:42 +0800 Subject: [PATCH 13/20] refactor: opt code --- packages/core/src/Transform.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/core/src/Transform.ts b/packages/core/src/Transform.ts index 00ff8b5d4c..b1b5eacb79 100644 --- a/packages/core/src/Transform.ts +++ b/packages/core/src/Transform.ts @@ -233,7 +233,20 @@ export class Transform extends Component { if (this._localMatrix !== value) { value.cloneTo(this._localMatrix); } + //@ts-ignore + this._position._onValueChanged = null; + //@ts-ignore + this._rotationQuaternion._onValueChanged = null; + //@ts-ignore + this._scale._onValueChanged = null; this._localMatrix.decompose(this._position, this._rotationQuaternion, this._scale); + //@ts-ignore + this._position._onValueChanged = this._onPositionChanged; + //@ts-ignore + this._rotationQuaternion._onValueChanged = this._onRotationQuaternionChanged; + //@ts-ignore + this._scale._onValueChanged = this._onScaleChanged; + this._setDirtyFlagTrue(TransformFlag.LocalEuler); this._setDirtyFlagFalse(TransformFlag.LocalMatrix); this._updateAllWorldFlag(); @@ -484,11 +497,10 @@ export class Transform extends Component { return; } const rotMat = Transform._tempMat43; - const worldRotationQuaternion = this._worldRotationQuaternion; worldUp = worldUp ?? Transform._tempVec3.setValue(0, 1, 0); Matrix.lookAt(position, worldPosition, worldUp, rotMat); - rotMat.getRotation(worldRotationQuaternion).invert(); + Quaternion.invert(rotMat.getRotation(Transform._tempQuat0), this._worldRotationQuaternion); } /** From c54ba5bbdc5eb93a443858976c1aa329995eff28 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Mon, 7 Mar 2022 15:44:45 +0800 Subject: [PATCH 14/20] refactor: opt code --- packages/core/src/Transform.ts | 36 +++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/packages/core/src/Transform.ts b/packages/core/src/Transform.ts index b1b5eacb79..5122e05b06 100644 --- a/packages/core/src/Transform.ts +++ b/packages/core/src/Transform.ts @@ -287,6 +287,14 @@ export class Transform extends Component { constructor(entity: Entity) { super(entity); + this._onPositionChanged = this._onPositionChanged.bind(this); + this._onWorldPositionChanged = this._onWorldPositionChanged.bind(this); + this._onRotationChanged = this._onRotationChanged.bind(this); + this._onWorldRotationChanged = this._onWorldRotationChanged.bind(this); + this._onRotationQuaternionChanged = this._onRotationQuaternionChanged.bind(this); + this._onWorldRotationQuaternionChanged = this._onWorldRotationQuaternionChanged.bind(this); + this._onScaleChanged = this._onScaleChanged.bind(this); + //@ts-ignore this._position._onValueChanged = this._onPositionChanged; //@ts-ignore @@ -700,12 +708,12 @@ export class Transform extends Component { this._rotateByQuat(rotQuat, relativeToLocal); } - private _onPositionChanged: () => void = () => { + private _onPositionChanged(): void { this._setDirtyFlagTrue(TransformFlag.LocalMatrix); this._updateWorldPositionFlag(); - }; + } - private _onWorldPositionChanged: () => void = () => { + private _onWorldPositionChanged(): void { const worldPosition = this._worldPosition; const parent = this._getParentTransform(); if (parent) { @@ -715,15 +723,15 @@ export class Transform extends Component { worldPosition.cloneTo(this._position); } this._setDirtyFlagFalse(TransformFlag.WorldPosition); - }; + } - private _onRotationChanged: () => void = () => { + private _onRotationChanged(): void { this._setDirtyFlagTrue(TransformFlag.LocalMatrix | TransformFlag.LocalQuat); this._setDirtyFlagFalse(TransformFlag.LocalEuler); this._updateWorldRotationFlag(); - }; + } - private _onWorldRotationChanged: () => void = () => { + private _onWorldRotationChanged(): void { const worldRotation = this._worldRotation; Quaternion.rotationEuler( MathUtil.degreeToRadian(worldRotation.x), @@ -732,15 +740,15 @@ export class Transform extends Component { this._worldRotationQuaternion ); this._setDirtyFlagFalse(TransformFlag.WorldEuler); - }; + } - private _onRotationQuaternionChanged: () => void = () => { + private _onRotationQuaternionChanged(): void { this._setDirtyFlagTrue(TransformFlag.LocalMatrix | TransformFlag.LocalEuler); this._setDirtyFlagFalse(TransformFlag.LocalQuat); this._updateWorldRotationFlag(); - }; + } - private _onWorldRotationQuaternionChanged: () => void = () => { + private _onWorldRotationQuaternionChanged(): void { const worldRotationQuaternion = this._worldRotationQuaternion; const parent = this._getParentTransform(); if (parent) { @@ -750,12 +758,12 @@ export class Transform extends Component { worldRotationQuaternion.cloneTo(this._rotationQuaternion); } this._setDirtyFlagFalse(TransformFlag.WorldQuat); - }; + } - private _onScaleChanged: () => void = () => { + private _onScaleChanged(): void { this._setDirtyFlagTrue(TransformFlag.LocalMatrix); this._updateWorldScaleFlag(); - }; + } } /** * Dirty flag of transform. From 7be24dd19512546f7022947ffbcd97a6d04318d8 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Mon, 7 Mar 2022 16:02:10 +0800 Subject: [PATCH 15/20] refactor: opt code --- packages/core/src/Transform.ts | 53 +++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/packages/core/src/Transform.ts b/packages/core/src/Transform.ts index 5122e05b06..a8eb419ebf 100644 --- a/packages/core/src/Transform.ts +++ b/packages/core/src/Transform.ts @@ -64,20 +64,21 @@ export class Transform extends Component { * World position. */ get worldPosition(): Vector3 { + const worldPosition = this._worldPosition; if (this._isContainDirtyFlag(TransformFlag.WorldPosition)) { //@ts-ignore - this._worldPosition._onValueChanged = null; + worldPosition._onValueChanged = null; if (this._getParentTransform()) { - this.worldMatrix.getTranslation(this._worldPosition); + this.worldMatrix.getTranslation(worldPosition); } else { - this._position.cloneTo(this._worldPosition); + this._position.cloneTo(worldPosition); } //@ts-ignore - this._worldPosition._onValueChanged = this._onWorldPositionChanged; + worldPosition._onValueChanged = this._onWorldPositionChanged; this._setDirtyFlagFalse(TransformFlag.WorldPosition); } - return this._worldPosition; + return worldPosition; } set worldPosition(value: Vector3) { @@ -91,17 +92,18 @@ export class Transform extends Component { * Rotations are performed around the Y axis, the X axis, and the Z axis, in that order. */ get rotation(): Vector3 { + const rotation = this._rotation; if (this._isContainDirtyFlag(TransformFlag.LocalEuler)) { //@ts-ignore - this._rotation._onValueChanged = null; - this._rotationQuaternion.toEuler(this._rotation); + rotation._onValueChanged = null; + this._rotationQuaternion.toEuler(rotation); //@ts-ignore - this._rotation._onValueChanged = this._onRotationChanged; - this._rotation.scale(MathUtil.radToDegreeFactor); // radians to degrees + rotation._onValueChanged = this._onRotationChanged; + rotation.scale(MathUtil.radToDegreeFactor); // radians to degrees this._setDirtyFlagFalse(TransformFlag.LocalEuler); } - return this._rotation; + return rotation; } set rotation(value: Vector3) { @@ -115,16 +117,17 @@ export class Transform extends Component { * Rotations are performed around the Y axis, the X axis, and the Z axis, in that order. */ get worldRotation(): Vector3 { + const worldRotation = this._worldRotation; if (this._isContainDirtyFlag(TransformFlag.WorldEuler)) { //@ts-ignore - this._worldRotation._onValueChanged = null; - this.worldRotationQuaternion.toEuler(this._worldRotation); - this._worldRotation.scale(MathUtil.radToDegreeFactor); // Radian to angle + worldRotation._onValueChanged = null; + this.worldRotationQuaternion.toEuler(worldRotation); + worldRotation.scale(MathUtil.radToDegreeFactor); // Radian to angle //@ts-ignore - this._worldRotation._onValueChanged = this._onWorldRotationChanged; + worldRotation._onValueChanged = this._onWorldRotationChanged; this._setDirtyFlagFalse(TransformFlag.WorldEuler); } - return this._worldRotation; + return worldRotation; } set worldRotation(value: Vector3) { @@ -137,20 +140,21 @@ export class Transform extends Component { * Local rotation, defining the rotation by using a unit quaternion. */ get rotationQuaternion(): Quaternion { + const rotationQuaternion = this._rotationQuaternion; if (this._isContainDirtyFlag(TransformFlag.LocalQuat)) { //@ts-ignore - this._rotationQuaternion._onValueChanged = null; + rotationQuaternion._onValueChanged = null; Quaternion.rotationEuler( MathUtil.degreeToRadian(this._rotation.x), MathUtil.degreeToRadian(this._rotation.y), MathUtil.degreeToRadian(this._rotation.z), - this._rotationQuaternion + rotationQuaternion ); //@ts-ignore - this._rotationQuaternion._onValueChanged = this._onRotationQuaternionChanged; + rotationQuaternion._onValueChanged = this._onRotationQuaternionChanged; this._setDirtyFlagFalse(TransformFlag.LocalQuat); } - return this._rotationQuaternion; + return rotationQuaternion; } set rotationQuaternion(value: Quaternion) { @@ -163,20 +167,21 @@ export class Transform extends Component { * World rotation, defining the rotation by using a unit quaternion. */ get worldRotationQuaternion(): Quaternion { + const worldRotationQuaternion = this._worldRotationQuaternion; if (this._isContainDirtyFlag(TransformFlag.WorldQuat)) { //@ts-ignore - this._worldRotationQuaternion._onValueChanged = null; + worldRotationQuaternion._onValueChanged = null; const parent = this._getParentTransform(); if (parent != null) { - Quaternion.multiply(parent.worldRotationQuaternion, this.rotationQuaternion, this._worldRotationQuaternion); + Quaternion.multiply(parent.worldRotationQuaternion, this.rotationQuaternion, worldRotationQuaternion); } else { - this.rotationQuaternion.cloneTo(this._worldRotationQuaternion); + this.rotationQuaternion.cloneTo(worldRotationQuaternion); } //@ts-ignore - this._worldRotationQuaternion._onValueChanged = this._onWorldRotationQuaternionChanged; + worldRotationQuaternion._onValueChanged = this._onWorldRotationQuaternionChanged; this._setDirtyFlagFalse(TransformFlag.WorldQuat); } - return this._worldRotationQuaternion; + return worldRotationQuaternion; } set worldRotationQuaternion(value: Quaternion) { From 5f25f1ef3f68a7cb87fd8bad804a6170880df8d5 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Mon, 7 Mar 2022 16:09:31 +0800 Subject: [PATCH 16/20] refactor: opt code --- packages/core/src/Transform.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/core/src/Transform.ts b/packages/core/src/Transform.ts index a8eb419ebf..7f4f5bb657 100644 --- a/packages/core/src/Transform.ts +++ b/packages/core/src/Transform.ts @@ -244,7 +244,9 @@ export class Transform extends Component { this._rotationQuaternion._onValueChanged = null; //@ts-ignore this._scale._onValueChanged = null; + this._localMatrix.decompose(this._position, this._rotationQuaternion, this._scale); + //@ts-ignore this._position._onValueChanged = this._onPositionChanged; //@ts-ignore From 446a90518a5463e9b0cf0ce76277b5f4f30a7381 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Mon, 7 Mar 2022 16:16:53 +0800 Subject: [PATCH 17/20] refactor: opt cpde --- packages/core/src/Transform.ts | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/packages/core/src/Transform.ts b/packages/core/src/Transform.ts index 7f4f5bb657..e24300caf6 100644 --- a/packages/core/src/Transform.ts +++ b/packages/core/src/Transform.ts @@ -238,21 +238,8 @@ export class Transform extends Component { if (this._localMatrix !== value) { value.cloneTo(this._localMatrix); } - //@ts-ignore - this._position._onValueChanged = null; - //@ts-ignore - this._rotationQuaternion._onValueChanged = null; - //@ts-ignore - this._scale._onValueChanged = null; this._localMatrix.decompose(this._position, this._rotationQuaternion, this._scale); - - //@ts-ignore - this._position._onValueChanged = this._onPositionChanged; - //@ts-ignore - this._rotationQuaternion._onValueChanged = this._onRotationQuaternionChanged; - //@ts-ignore - this._scale._onValueChanged = this._onScaleChanged; this._setDirtyFlagTrue(TransformFlag.LocalEuler); this._setDirtyFlagFalse(TransformFlag.LocalMatrix); From 1e2800a8b925e688ba979468ae88fb96b61a5a94 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Mon, 7 Mar 2022 16:24:54 +0800 Subject: [PATCH 18/20] refactor: opt code --- packages/core/src/Transform.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/core/src/Transform.ts b/packages/core/src/Transform.ts index e24300caf6..f2b48007a1 100644 --- a/packages/core/src/Transform.ts +++ b/packages/core/src/Transform.ts @@ -746,8 +746,9 @@ export class Transform extends Component { const worldRotationQuaternion = this._worldRotationQuaternion; const parent = this._getParentTransform(); if (parent) { - Quaternion.invert(parent.worldRotationQuaternion, Transform._tempQuat0); - Quaternion.multiply(worldRotationQuaternion, Transform._tempQuat0, this._rotationQuaternion); + const invParentQuaternion = Transform._tempQuat0; + Quaternion.invert(parent.worldRotationQuaternion, invParentQuaternion); + Quaternion.multiply(worldRotationQuaternion, invParentQuaternion, this._rotationQuaternion); } else { worldRotationQuaternion.cloneTo(this._rotationQuaternion); } From 73fcc3fe36f03bd9220b8ae49ebb3588caccdaa5 Mon Sep 17 00:00:00 2001 From: cptbtptpbcptdtptp Date: Wed, 9 Mar 2022 11:45:28 +0800 Subject: [PATCH 19/20] fix: Filling in the gaps --- packages/math/src/Matrix.ts | 62 +++++++++++++++------------------- packages/math/src/Matrix3x3.ts | 8 ++--- packages/math/src/Vector2.ts | 20 +++++------ 3 files changed, 41 insertions(+), 49 deletions(-) diff --git a/packages/math/src/Matrix.ts b/packages/math/src/Matrix.ts index c52e84c7fd..4f6aec8cbe 100644 --- a/packages/math/src/Matrix.ts +++ b/packages/math/src/Matrix.ts @@ -263,9 +263,9 @@ export class Matrix implements IClone { Matrix.rotationQuaternion(quaternion, out); const oe = out.elements; - oe[12] = translation.x; - oe[13] = translation.y; - oe[14] = translation.z; + oe[12] = translation._x; + oe[13] = translation._y; + oe[14] = translation._z; } /** @@ -291,9 +291,9 @@ export class Matrix implements IClone { let wx = w * x2; let wy = w * y2; let wz = w * z2; - let sx = scale.x; - let sy = scale.y; - let sz = scale.z; + let sx = scale._x; + let sy = scale._y; + let sz = scale._z; oe[0] = (1 - (yy + zz)) * sx; oe[1] = (xy + wz) * sx; @@ -310,9 +310,9 @@ export class Matrix implements IClone { oe[10] = (1 - (xx + yy)) * sz; oe[11] = 0; - oe[12] = translation.x; - oe[13] = translation.y; - oe[14] = translation.z; + oe[12] = translation._x; + oe[13] = translation._y; + oe[14] = translation._z; oe[15] = 1; } @@ -323,19 +323,19 @@ export class Matrix implements IClone { */ static scaling(s: Vector3, out: Matrix): void { const oe = out.elements; - oe[0] = s.x; + oe[0] = s._x; oe[1] = 0; oe[2] = 0; oe[3] = 0; oe[4] = 0; - oe[5] = s.y; + oe[5] = s._y; oe[6] = 0; oe[7] = 0; oe[8] = 0; oe[9] = 0; - oe[10] = s.z; + oe[10] = s._z; oe[11] = 0; oe[12] = 0; @@ -366,9 +366,9 @@ export class Matrix implements IClone { oe[10] = 1; oe[11] = 0; - oe[12] = translation.x; - oe[13] = translation.y; - oe[14] = translation.z; + oe[12] = translation._x; + oe[13] = translation._y; + oe[14] = translation._z; oe[15] = 1; } @@ -457,19 +457,19 @@ export class Matrix implements IClone { xAxis.normalize(); Vector3.cross(zAxis, xAxis, yAxis); - oe[0] = xAxis.x; - oe[1] = yAxis.x; - oe[2] = zAxis.x; + oe[0] = xAxis._x; + oe[1] = yAxis._x; + oe[2] = zAxis._x; oe[3] = 0; - oe[4] = xAxis.y; - oe[5] = yAxis.y; - oe[6] = zAxis.y; + oe[4] = xAxis._y; + oe[5] = yAxis._y; + oe[6] = zAxis._y; oe[7] = 0; - oe[8] = xAxis.z; - oe[9] = yAxis.z; - oe[10] = zAxis.z; + oe[8] = xAxis._z; + oe[9] = yAxis._z; + oe[10] = zAxis._z; oe[11] = 0; oe[12] = -Vector3.dot(xAxis, eye); @@ -1064,10 +1064,7 @@ export class Matrix implements IClone { const m32 = e[9]; const m33 = e[10]; const m34 = e[11]; - - translation.x = e[12]; - translation.y = e[13]; - translation.z = e[14]; + translation.setValue(e[12], e[13], e[14]); const xs = Math.sign(m11 * m12 * m13 * m14) < 0 ? -1 : 1; const ys = Math.sign(m21 * m22 * m23 * m24) < 0 ? -1 : 1; @@ -1076,10 +1073,7 @@ export class Matrix implements IClone { const sx = xs * Math.sqrt(m11 * m11 + m12 * m12 + m13 * m13); const sy = ys * Math.sqrt(m21 * m21 + m22 * m22 + m23 * m23); const sz = zs * Math.sqrt(m31 * m31 + m32 * m32 + m33 * m33); - - scale.x = sx; - scale.y = sy; - scale.z = sz; + scale.setValue(sx, sy, sz); if ( Math.abs(sx) < MathUtil.zeroTolerance || @@ -1164,9 +1158,7 @@ export class Matrix implements IClone { m32 = e[9], m33 = e[10]; - out.x = Math.sqrt(m11 * m11 + m12 * m12 + m13 * m13); - out.y = Math.sqrt(m21 * m21 + m22 * m22 + m23 * m23); - out.z = Math.sqrt(m31 * m31 + m32 * m32 + m33 * m33); + out.setValue(Math.sqrt(m11 * m11 + m12 * m12 + m13 * m13), Math.sqrt(m21 * m21 + m22 * m22 + m23 * m23), Math.sqrt(m31 * m31 + m32 * m32 + m33 * m33)); return out; } diff --git a/packages/math/src/Matrix3x3.ts b/packages/math/src/Matrix3x3.ts index e5ee0e607c..3f26efacde 100644 --- a/packages/math/src/Matrix3x3.ts +++ b/packages/math/src/Matrix3x3.ts @@ -191,12 +191,12 @@ export class Matrix3x3 implements IClone { static scaling(s: Vector2, out: Matrix3x3): void { const oe = out.elements; - oe[0] = s.x; + oe[0] = s._x; oe[1] = 0; oe[2] = 0; oe[3] = 0; - oe[4] = s.y; + oe[4] = s._y; oe[5] = 0; oe[6] = 0; @@ -220,8 +220,8 @@ export class Matrix3x3 implements IClone { oe[4] = 1; oe[5] = 0; - oe[6] = translation.x; - oe[7] = translation.y; + oe[6] = translation._x; + oe[7] = translation._y; oe[8] = 1; } diff --git a/packages/math/src/Vector2.ts b/packages/math/src/Vector2.ts index 2927ba19f2..3ab23e0986 100644 --- a/packages/math/src/Vector2.ts +++ b/packages/math/src/Vector2.ts @@ -111,8 +111,8 @@ export class Vector2 implements IClone { */ 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; + out._x = _x + (right._x - _x) * t; + out._y = _y + (right._y - _y) * t; out._onValueChanged && out._onValueChanged(); } @@ -252,8 +252,8 @@ 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; } @@ -264,8 +264,8 @@ 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; } @@ -276,8 +276,8 @@ 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; } @@ -288,8 +288,8 @@ 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; } From 3e6d602ae14df1f027f03a695cae928e0c445819 Mon Sep 17 00:00:00 2001 From: cptbtptpbcptdtptp Date: Wed, 9 Mar 2022 17:08:50 +0800 Subject: [PATCH 20/20] fix: transform --- packages/math/src/Matrix.ts | 12 ++++++------ packages/math/src/Matrix3x3.ts | 6 +++--- packages/math/src/Quaternion.ts | 2 +- packages/math/src/SphericalHarmonics3.ts | 4 ++-- packages/math/src/Vector4.ts | 2 +- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/math/src/Matrix.ts b/packages/math/src/Matrix.ts index 4f6aec8cbe..a76f5cd732 100644 --- a/packages/math/src/Matrix.ts +++ b/packages/math/src/Matrix.ts @@ -170,7 +170,7 @@ export class Matrix implements IClone { */ static rotationQuaternion(quaternion: Quaternion, out: Matrix): void { const oe = out.elements; - const { x, y, z, w } = quaternion; + const { _x: x, _y: y, _z: z, _w: w } = quaternion; let x2 = x + x; let y2 = y + y; let z2 = z + z; @@ -214,7 +214,7 @@ export class Matrix implements IClone { */ static rotationAxisAngle(axis: Vector3, r: number, out: Matrix): void { const oe = out.elements; - let { x, y, z } = axis; + let { _x: x, _y: y, _z: z } = axis; let len = Math.sqrt(x * x + y * y + z * z); let s, c, t; @@ -277,7 +277,7 @@ export class Matrix implements IClone { */ static affineTransformation(scale: Vector3, rotation: Quaternion, translation: Vector3, out: Matrix): void { const oe = out.elements; - const { x, y, z, w } = rotation; + const { _x: x, _y: y, _z: z, _w: w } = rotation; let x2 = x + x; let y2 = y + y; let z2 = z + z; @@ -557,7 +557,7 @@ export class Matrix implements IClone { * @param out - The rotated matrix */ static rotateAxisAngle(m: Matrix, axis: Vector3, r: number, out: Matrix): void { - let { x, y, z } = axis; + let { _x: x, _y: y, _z: z } = axis; let len = Math.sqrt(x * x + y * y + z * z); if (Math.abs(len) < MathUtil.zeroTolerance) { @@ -635,7 +635,7 @@ export class Matrix implements IClone { static scale(m: Matrix, s: Vector3, out: Matrix): void { const me = m.elements; const oe = out.elements; - const { x, y, z } = s; + const { _x: x, _y: y, _z: z } = s; oe[0] = me[0] * x; oe[1] = me[1] * x; @@ -667,7 +667,7 @@ export class Matrix implements IClone { static translate(m: Matrix, v: Vector3, out: Matrix): void { const me = m.elements; const oe = out.elements; - const { x, y, z } = v; + const { _x: x, _y: y, _z: z } = v; if (m === out) { oe[12] = me[0] * x + me[4] * y + me[8] * z + me[12]; diff --git a/packages/math/src/Matrix3x3.ts b/packages/math/src/Matrix3x3.ts index 3f26efacde..6219c344c7 100644 --- a/packages/math/src/Matrix3x3.ts +++ b/packages/math/src/Matrix3x3.ts @@ -156,7 +156,7 @@ export class Matrix3x3 implements IClone { */ static rotationQuaternion(quaternion: Quaternion, out: Matrix3x3): void { const oe = out.elements; - const { x, y, z, w } = quaternion; + const { _x: x, _y: y, _z: z, _w: w } = quaternion; const x2 = x + x; const y2 = y + y; const z2 = z + z; @@ -368,7 +368,7 @@ export class Matrix3x3 implements IClone { * @param out - The scaled matrix */ static scale(m: Matrix3x3, s: Vector2, out: Matrix3x3): void { - const { x, y } = s; + const { _x: x, _y: y } = s; const ae = m.elements; const oe = out.elements; @@ -392,7 +392,7 @@ export class Matrix3x3 implements IClone { * @param out - The translated matrix */ static translate(m: Matrix3x3, translation: Vector2, out: Matrix3x3): void { - const { x, y } = translation; + const { _x: x, _y: y } = translation; const ae = m.elements; const oe = out.elements; diff --git a/packages/math/src/Quaternion.ts b/packages/math/src/Quaternion.ts index e2385b3f82..77fed39fb3 100644 --- a/packages/math/src/Quaternion.ts +++ b/packages/math/src/Quaternion.ts @@ -206,7 +206,7 @@ export class Quaternion implements IClone { * @param out - The inverse of the specified quaternion */ static invert(a: Quaternion, out: Quaternion): void { - const { x, y, z, w } = a; + const { _x: x, _y: y, _z: z, _w: w } = a; const dot = x * x + y * y + z * z + w * w; if (dot > MathUtil.zeroTolerance) { const invDot = 1.0 / dot; diff --git a/packages/math/src/SphericalHarmonics3.ts b/packages/math/src/SphericalHarmonics3.ts index e60db6c0a9..177de80d1e 100644 --- a/packages/math/src/SphericalHarmonics3.ts +++ b/packages/math/src/SphericalHarmonics3.ts @@ -41,7 +41,7 @@ export class SphericalHarmonics3 implements IClone { const coe = this.coefficients; - const { x, y, z } = direction; + const { _x: x, _y: y, _z: z } = direction; const { r, g, b } = color; const bv0 = 0.282095; // basis0 = 0.886227 @@ -98,7 +98,7 @@ export class SphericalHarmonics3 implements IClone { */ const coe = this.coefficients; - const { x, y, z } = direction; + const { _x: x, _y: y, _z: z } = direction; const bv0 = 0.886227; // kernel0 * basis0 = 0.886227 const bv1 = -1.023327 * y; // kernel1 * basis1 = -1.023327 diff --git a/packages/math/src/Vector4.ts b/packages/math/src/Vector4.ts index 972913e22d..0e37444bab 100644 --- a/packages/math/src/Vector4.ts +++ b/packages/math/src/Vector4.ts @@ -233,7 +233,7 @@ export class Vector4 implements IClone { * @param out - The transformed vector */ static transformByQuat(v: Vector4, q: Quaternion, out: Vector4): void { - const { x, y, z, w } = v; + const { _x: x, _y: y, _z: z, _w: w } = v; const qx = q._x; const qy = q._y; const qz = q._z;