From 28ef69e5429c47e7c3055c9d76f50a54309d93c3 Mon Sep 17 00:00:00 2001 From: cptbtptpbcptdtptp Date: Thu, 3 Mar 2022 11:40:42 +0800 Subject: [PATCH 1/7] fix: ignore scale --- packages/core/src/Camera.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/core/src/Camera.ts b/packages/core/src/Camera.ts index 9b4baf5ef2..e12faa4904 100644 --- a/packages/core/src/Camera.ts +++ b/packages/core/src/Camera.ts @@ -191,10 +191,27 @@ export class Camera extends Component { * View matrix. */ get viewMatrix(): Readonly { - // Remove scale if (this._isViewMatrixDirty.flag) { this._isViewMatrixDirty.flag = false; Matrix.invert(this._transform.worldMatrix, this._viewMatrix); + // Remove scale + const { x, y, z } = this._transform.scale; + const { elements } = this._viewMatrix; + elements[0] *= x; + elements[1] *= y; + elements[2] *= z; + + elements[4] *= x; + elements[5] *= y; + elements[6] *= z; + + elements[8] *= x; + elements[9] *= y; + elements[10] *= z; + + elements[12] *= x; + elements[13] *= y; + elements[14] *= z; } return this._viewMatrix; } From 6761444b7962f53ab341c4ed1dec61642b4af451 Mon Sep 17 00:00:00 2001 From: cptbtptpbcptdtptp Date: Thu, 3 Mar 2022 14:10:04 +0800 Subject: [PATCH 2/7] fix: scale -> worldScale --- packages/core/src/Camera.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/Camera.ts b/packages/core/src/Camera.ts index e12faa4904..b9e932bac5 100644 --- a/packages/core/src/Camera.ts +++ b/packages/core/src/Camera.ts @@ -195,7 +195,7 @@ export class Camera extends Component { this._isViewMatrixDirty.flag = false; Matrix.invert(this._transform.worldMatrix, this._viewMatrix); // Remove scale - const { x, y, z } = this._transform.scale; + const { x, y, z } = this._transform.lossyWorldScale; const { elements } = this._viewMatrix; elements[0] *= x; elements[1] *= y; From 6b91493f0fe3a967cfece9f13164cd8b7899cfae Mon Sep 17 00:00:00 2001 From: cptbtptpbcptdtptp Date: Thu, 3 Mar 2022 17:27:15 +0800 Subject: [PATCH 3/7] fix: camera ignore scale --- packages/core/src/Camera.ts | 69 ++++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 20 deletions(-) diff --git a/packages/core/src/Camera.ts b/packages/core/src/Camera.ts index b9e932bac5..a089b1c64f 100644 --- a/packages/core/src/Camera.ts +++ b/packages/core/src/Camera.ts @@ -1,4 +1,4 @@ -import { BoundingFrustum, MathUtil, Matrix, Ray, Vector2, Vector3, Vector4 } from "@oasis-engine/math"; +import { BoundingFrustum, MathUtil, Matrix, Quaternion, Ray, Vector2, Vector3, Vector4 } from "@oasis-engine/math"; import { Logger } from "./base"; import { deepClone, ignoreClone } from "./clone/CloneManager"; import { Component } from "./Component"; @@ -35,6 +35,52 @@ export class Camera extends Component { private static _inverseProjectionMatrixProperty = Shader.getPropertyByName("u_projInvMat"); private static _cameraPositionProperty = Shader.getPropertyByName("u_cameraPos"); + /** + * Compute the inverse of the rotation translation matrix. + * @param rotation - The rotation used to calculate matrix + * @param translation - The translation used to calculate matrix + * @param out - The calculated matrix + */ + private static _rotationTranslationInv(rotation: Quaternion, translation: Vector3, out: Matrix) { + const oe = out.elements; + const { x, y, z, w } = rotation; + let x2 = x + x; + let y2 = y + y; + let z2 = z + z; + + let xx = x * x2; + let xy = x * y2; + let xz = x * z2; + let yy = y * y2; + let yz = y * z2; + let zz = z * z2; + let wx = w * x2; + let wy = w * y2; + let wz = w * z2; + + oe[0] = 1 - (yy + zz); + oe[1] = xy + wz; + oe[2] = xz - wy; + oe[3] = 0; + + oe[4] = xy - wz; + oe[5] = 1 - (xx + zz); + oe[6] = yz + wx; + oe[7] = 0; + + oe[8] = xz + wy; + oe[9] = yz - wx; + oe[10] = 1 - (xx + yy); + oe[11] = 0; + + oe[12] = translation.x; + oe[13] = translation.y; + oe[14] = translation.z; + oe[15] = 1; + + out.invert(); + } + /** Shader data. */ readonly shaderData: ShaderData = new ShaderData(ShaderDataGroup.Camera); @@ -193,25 +239,8 @@ export class Camera extends Component { get viewMatrix(): Readonly { if (this._isViewMatrixDirty.flag) { this._isViewMatrixDirty.flag = false; - Matrix.invert(this._transform.worldMatrix, this._viewMatrix); - // Remove scale - const { x, y, z } = this._transform.lossyWorldScale; - const { elements } = this._viewMatrix; - elements[0] *= x; - elements[1] *= y; - elements[2] *= z; - - elements[4] *= x; - elements[5] *= y; - elements[6] *= z; - - elements[8] *= x; - elements[9] *= y; - elements[10] *= z; - - elements[12] *= x; - elements[13] *= y; - elements[14] *= z; + // Ignore scale. + Camera._rotationTranslationInv(this._transform.worldRotationQuaternion, this._transform.worldPosition, this._viewMatrix); } return this._viewMatrix; } From 07f3dac9d64f1dff3c2be10050ab1da6f963c4c5 Mon Sep 17 00:00:00 2001 From: cptbtptpbcptdtptp Date: Tue, 22 Mar 2022 12:37:05 +0800 Subject: [PATCH 4/7] fix:transform lookat --- packages/core/src/Transform.ts | 55 +++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 17 deletions(-) diff --git a/packages/core/src/Transform.ts b/packages/core/src/Transform.ts index f2b48007a1..298a4ae8b8 100644 --- a/packages/core/src/Transform.ts +++ b/packages/core/src/Transform.ts @@ -10,7 +10,9 @@ import { UpdateFlagManager } from "./UpdateFlagManager"; */ export class Transform extends Component { private static _tempQuat0: Quaternion = new Quaternion(); - private static _tempVec3: Vector3 = new Vector3(); + private static _tempVec30: Vector3 = new Vector3(); + private static _tempVec31: Vector3 = new Vector3(); + private static _tempVec32: Vector3 = new Vector3(); private static _tempMat30: Matrix3x3 = new Matrix3x3(); private static _tempMat31: Matrix3x3 = new Matrix3x3(); private static _tempMat32: Matrix3x3 = new Matrix3x3(); @@ -434,7 +436,7 @@ export class Transform extends Component { relativeToLocal?: boolean ): void { if (typeof translationOrX === "number") { - const translate = Transform._tempVec3; + const translate = Transform._tempVec30; translate.setValue(translationOrX, relativeToLocalOrY, z); this._translate(translate, relativeToLocal); } else { @@ -484,25 +486,44 @@ export class Transform extends Component { } /** - * Rotate and ensure that the world front vector points to the target world position. - * @param worldPosition - Target world position + * Adjust the rotation so that the -Z axis is towards the target. + * @param targetPosition - Target world position * @param worldUp - Up direction in world space, default is Vector3(0, 1, 0) */ - lookAt(worldPosition: Vector3, worldUp?: Vector3): void { - const position = this.worldPosition; - const EPSILON = MathUtil.zeroTolerance; - if ( - Math.abs(position.x - worldPosition.x) < EPSILON && - Math.abs(position.y - worldPosition.y) < EPSILON && - Math.abs(position.z - worldPosition.z) < EPSILON - ) { + lookAt(targetPosition: Vector3, worldUp?: Vector3): void { + const eyePosition = this.worldPosition; + const zAxis = Transform._tempVec30; + Vector3.subtract(eyePosition, targetPosition, zAxis); + let axisLen = zAxis.length(); + if (axisLen <= MathUtil.zeroTolerance) { return; } - const rotMat = Transform._tempMat43; - - worldUp = worldUp ?? Transform._tempVec3.setValue(0, 1, 0); - Matrix.lookAt(position, worldPosition, worldUp, rotMat); - Quaternion.invert(rotMat.getRotation(Transform._tempQuat0), this._worldRotationQuaternion); + zAxis.scale(1 / axisLen); + const xAxis = Transform._tempVec31; + if (worldUp) { + Vector3.cross(worldUp, zAxis, xAxis); + } else { + xAxis.setValue(zAxis.z, 0, -zAxis.x); + } + axisLen = xAxis.length(); + if (axisLen <= MathUtil.zeroTolerance) { + return; + } + xAxis.scale(1 / axisLen); + const yAxis = Transform._tempVec32; + Vector3.cross(zAxis, xAxis, yAxis); + + const { elements: oe } = Transform._tempMat41; + oe[0] = xAxis.x; + oe[1] = xAxis.y; + oe[2] = xAxis.z; + oe[4] = yAxis.x; + oe[5] = yAxis.y; + oe[6] = yAxis.z; + oe[8] = zAxis.x; + oe[9] = zAxis.y; + oe[10] = zAxis.z; + Transform._tempMat41.getRotation(this._worldRotationQuaternion); } /** From 14fb56dccb5e164e514e61a93a6c27f36c536eb4 Mon Sep 17 00:00:00 2001 From: cptbtptpbcptdtptp Date: Tue, 22 Mar 2022 12:39:02 +0800 Subject: [PATCH 5/7] fix:transform lookat --- packages/core/src/Transform.ts | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/core/src/Transform.ts b/packages/core/src/Transform.ts index 298a4ae8b8..a072416d02 100644 --- a/packages/core/src/Transform.ts +++ b/packages/core/src/Transform.ts @@ -513,16 +513,16 @@ export class Transform extends Component { const yAxis = Transform._tempVec32; Vector3.cross(zAxis, xAxis, yAxis); - const { elements: oe } = Transform._tempMat41; - oe[0] = xAxis.x; - oe[1] = xAxis.y; - oe[2] = xAxis.z; - oe[4] = yAxis.x; - oe[5] = yAxis.y; - oe[6] = yAxis.z; - oe[8] = zAxis.x; - oe[9] = zAxis.y; - oe[10] = zAxis.z; + const { elements: e } = Transform._tempMat41; + e[0] = xAxis.x; + e[1] = xAxis.y; + e[2] = xAxis.z; + e[4] = yAxis.x; + e[5] = yAxis.y; + e[6] = yAxis.z; + e[8] = zAxis.x; + e[9] = zAxis.y; + e[10] = zAxis.z; Transform._tempMat41.getRotation(this._worldRotationQuaternion); } From 1f0912d2df331e1731abf91c6c89a5321f6861c5 Mon Sep 17 00:00:00 2001 From: cptbtptpbcptdtptp Date: Tue, 22 Mar 2022 15:52:26 +0800 Subject: [PATCH 6/7] fix:transform lookat --- packages/core/src/Transform.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/Transform.ts b/packages/core/src/Transform.ts index a072416d02..3c794ccbb6 100644 --- a/packages/core/src/Transform.ts +++ b/packages/core/src/Transform.ts @@ -486,7 +486,7 @@ export class Transform extends Component { } /** - * Adjust the rotation so that the -Z axis is towards the target. + * Rotate and ensure that the world front vector points to the target world position. * @param targetPosition - Target world position * @param worldUp - Up direction in world space, default is Vector3(0, 1, 0) */ From a4b8cf03bd2597d434ff05dc58e3fa443b4beab0 Mon Sep 17 00:00:00 2001 From: cptbtptpbcptdtptp Date: Tue, 22 Mar 2022 16:06:34 +0800 Subject: [PATCH 7/7] fix:transform lookat --- packages/core/src/Transform.ts | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/packages/core/src/Transform.ts b/packages/core/src/Transform.ts index 3c794ccbb6..613cfcf903 100644 --- a/packages/core/src/Transform.ts +++ b/packages/core/src/Transform.ts @@ -491,11 +491,11 @@ export class Transform extends Component { * @param worldUp - Up direction in world space, default is Vector3(0, 1, 0) */ lookAt(targetPosition: Vector3, worldUp?: Vector3): void { - const eyePosition = this.worldPosition; const zAxis = Transform._tempVec30; - Vector3.subtract(eyePosition, targetPosition, zAxis); + Vector3.subtract(this.worldPosition, targetPosition, zAxis); let axisLen = zAxis.length(); if (axisLen <= MathUtil.zeroTolerance) { + // The current position and the target position are almost the same. return; } zAxis.scale(1 / axisLen); @@ -507,23 +507,21 @@ export class Transform extends Component { } axisLen = xAxis.length(); if (axisLen <= MathUtil.zeroTolerance) { + // @todo: + // 1.worldup is(0,0,0) + // 2.worldUp is parallel to zAxis return; } xAxis.scale(1 / axisLen); const yAxis = Transform._tempVec32; Vector3.cross(zAxis, xAxis, yAxis); - const { elements: e } = Transform._tempMat41; - e[0] = xAxis.x; - e[1] = xAxis.y; - e[2] = xAxis.z; - e[4] = yAxis.x; - e[5] = yAxis.y; - e[6] = yAxis.z; - e[8] = zAxis.x; - e[9] = zAxis.y; - e[10] = zAxis.z; - Transform._tempMat41.getRotation(this._worldRotationQuaternion); + const rotMat = Transform._tempMat41; + const { elements: e } = rotMat; + (e[0] = xAxis.x), (e[1] = xAxis.y), (e[2] = xAxis.z); + (e[4] = yAxis.x), (e[5] = yAxis.y), (e[6] = yAxis.z); + (e[8] = zAxis.x), (e[9] = zAxis.y), (e[10] = zAxis.z); + rotMat.getRotation(this._worldRotationQuaternion); } /**