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 camera aspect ratio #1872

Merged
merged 8 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
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
48 changes: 29 additions & 19 deletions packages/core/src/Camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ export class Camera extends Component {
@deepClone
private _inverseProjectionMatrix: Matrix = new Matrix();
@deepClone
private _lastAspectSize: Vector2 = new Vector2(0, 0);
@deepClone
private _invViewProjMat: Matrix = new Matrix();

/**
Expand Down Expand Up @@ -159,8 +157,8 @@ export class Camera extends Component {
* the manual value will be kept. Call resetAspectRatio() to restore it.
*/
get aspectRatio(): number {
const canvas = this._entity.engine.canvas;
return this._customAspectRatio ?? (canvas.width * this._viewport.z) / (canvas.height * this._viewport.w);
const pixelViewport = this.pixelViewport;
return this._customAspectRatio ?? pixelViewport.width / pixelViewport.height;
}

set aspectRatio(value: number) {
Expand All @@ -181,8 +179,6 @@ export class Camera extends Component {
if (value !== this._viewport) {
this._viewport.copyFrom(value);
}
this._projMatChange();
this._updatePixelViewport();
}

/**
Expand Down Expand Up @@ -261,18 +257,11 @@ export class Camera extends Component {
get projectionMatrix(): Matrix {
const virtualCamera = this._virtualCamera;
const projectionMatrix = virtualCamera.projectionMatrix;
const canvas = this._entity.engine.canvas;

if (
(!this._isProjectionDirty || this._isProjMatSetting) &&
this._lastAspectSize.x === canvas.width &&
this._lastAspectSize.y === canvas.height
) {
if (!this._isProjectionDirty || this._isProjMatSetting) {
return projectionMatrix;
}
this._isProjectionDirty = false;
this._lastAspectSize.x = canvas.width;
this._lastAspectSize.y = canvas.height;
const aspectRatio = this.aspectRatio;
if (!virtualCamera.isOrthographic) {
Matrix.perspective(
Expand Down Expand Up @@ -315,7 +304,7 @@ export class Camera extends Component {
this._renderTarget && this._addResourceReferCount(this._renderTarget, -1);
value && this._addResourceReferCount(value, 1);
this._renderTarget = value;
this._updatePixelViewport();
this._onPixelViewportChanged();
}
}

Expand All @@ -333,6 +322,10 @@ export class Camera extends Component {
this._renderPipeline = new BasicRenderPipeline(this);
this._addResourceReferCount(this.shaderData, 1);
this._updatePixelViewport();

this._onPixelViewportChanged = this._onPixelViewportChanged.bind(this);
//@ts-ignore
this._viewport._onValueChanged = this._onPixelViewportChanged;
}

/**
Expand Down Expand Up @@ -592,6 +585,9 @@ export class Camera extends Component {
this._isViewMatrixDirty.destroy();
this._addResourceReferCount(this.shaderData, -1);

//@ts-ignore
this._viewport._onValueChanged = null;

this._entity = null;
this._globalShaderMacro = null;
this._frustum = null;
Expand All @@ -604,15 +600,23 @@ export class Camera extends Component {
this._isInvViewProjDirty = null;
this._viewport = null;
this._inverseProjectionMatrix = null;
this._lastAspectSize = null;
this._invViewProjMat = null;
}

private _updatePixelViewport(): void {
const viewport = this._viewport;
const width = this._renderTarget?.width ?? this.engine.canvas.width;
const height = this._renderTarget?.height ?? this.engine.canvas.height;
let width: number, height: number;

const renderTarget = this._renderTarget;
if (renderTarget) {
width = renderTarget.width;
height = renderTarget.height;
} else {
const canvas = this.engine.canvas;
width = canvas.width;
height = canvas.height;
}

const viewport = this._viewport;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The _lastAspectSize attribute can be optimized.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

this._pixelViewport.set(viewport.x * width, viewport.y * height, viewport.z * width, viewport.w * height);
}

Expand Down Expand Up @@ -668,4 +672,10 @@ export class Camera extends Component {
}
return this._inverseProjectionMatrix;
}

@ignoreClone
private _onPixelViewportChanged(): void {
this._updatePixelViewport();
this._customAspectRatio ?? this._projMatChange();
}
}
6 changes: 3 additions & 3 deletions tests/src/core/Camera.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe("camera test", function () {
// Test default values
expect(camera.aspectRatio).to.eq(1);
expect(camera.entity.transform.worldPosition).not.to.be.undefined;
expect(camera.viewport).to.deep.eq(new Vector4(0, 0, 1, 1));
expect(camera.viewport).to.deep.include(new Vector4(0, 0, 1, 1).toJSON());
expect(camera.fieldOfView).to.eq(45);
expect(camera.isOrthographic).to.eq(false);

Expand Down Expand Up @@ -65,9 +65,9 @@ describe("camera test", function () {
// Test viewport
const originViewPort = new Vector4(0, 0, 1, 1);
const expectedViewPort = new Vector4(0, 1, 0, 1);
expect(camera.viewport).to.deep.eq(originViewPort);
expect(camera.viewport).to.deep.include(originViewPort.toJSON());
camera.viewport = expectedViewPort;
expect(camera.viewport).to.deep.eq(expectedViewPort);
expect(camera.viewport).to.deep.include(expectedViewPort.toJSON());
camera.viewport = originViewPort;

// Test orthographicSize
Expand Down