From 28ef69e5429c47e7c3055c9d76f50a54309d93c3 Mon Sep 17 00:00:00 2001 From: cptbtptpbcptdtptp Date: Thu, 3 Mar 2022 11:40:42 +0800 Subject: [PATCH 1/3] 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/3] 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/3] 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; }