Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ignore scale #670

Merged
merged 3 commits into from
Mar 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 49 additions & 3 deletions packages/core/src/Camera.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -191,10 +237,10 @@ export class Camera extends Component {
* View matrix.
*/
get viewMatrix(): Readonly<Matrix> {
// Remove scale
if (this._isViewMatrixDirty.flag) {
this._isViewMatrixDirty.flag = false;
Matrix.invert(this._transform.worldMatrix, this._viewMatrix);
// Ignore scale.
Camera._rotationTranslationInv(this._transform.worldRotationQuaternion, this._transform.worldPosition, this._viewMatrix);
}
return this._viewMatrix;
}
Expand Down