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

Camera: Add code to update View and Projection Matrices in update function #13552

Merged
merged 2 commits into from
Feb 22, 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
6 changes: 6 additions & 0 deletions packages/dev/core/src/Cameras/camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,12 @@ export class Camera extends Node {
if (this.cameraRigMode !== Camera.RIG_MODE_NONE) {
this._updateRigCameras();
}

// Attempt to update the camera's view and projection matrices.
// This call is being made because these matrices are no longer being updated
// as a part of the picking ray process (in addition to scene.render).
this.getViewMatrix();
this.getProjectionMatrix();
}

/** @internal */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,4 +525,91 @@ describe("InputManager", () => {
}
expect(tapCt).toBe(1);
});

it("can fire onViewMatrixObservable on camera.update", () => {
let viewMatrixChangedCt = 0;

if (deviceInputSystem && camera && scene) {
// Setting inertia to 0 so that all movements are immediate with no carry over
camera.inertia = 0;
camera.onViewMatrixChangedObservable.add(() => {
viewMatrixChangedCt++;
});
// Perform basic mouse move (should trigger observable because of down pick)
deviceInputSystem.changeInput(DeviceType.Mouse, 0, PointerInput.LeftClick, 1);
deviceInputSystem.changeInput(DeviceType.Mouse, 0, PointerInput.Horizontal, 64, false);
deviceInputSystem.changeInput(DeviceType.Mouse, 0, PointerInput.Vertical, 64, false);
deviceInputSystem.changeInput(DeviceType.Mouse, 0, PointerInput.Move, 1);
deviceInputSystem.changeInput(DeviceType.Mouse, 0, PointerInput.LeftClick, 0);

// Perform basic mouse move (shouldn't trigger observable because neither update() or render() were called)
deviceInputSystem.changeInput(DeviceType.Mouse, 0, PointerInput.LeftClick, 1);
deviceInputSystem.changeInput(DeviceType.Mouse, 0, PointerInput.Horizontal, 127, false);
deviceInputSystem.changeInput(DeviceType.Mouse, 0, PointerInput.Vertical, 127, false);
deviceInputSystem.changeInput(DeviceType.Mouse, 0, PointerInput.Move, 1);
deviceInputSystem.changeInput(DeviceType.Mouse, 0, PointerInput.LeftClick, 0);

// Perform mouse move and then run render() (should trigger observable)
deviceInputSystem.changeInput(DeviceType.Mouse, 0, PointerInput.LeftClick, 1);
deviceInputSystem.changeInput(DeviceType.Mouse, 0, PointerInput.Horizontal, 64, false);
deviceInputSystem.changeInput(DeviceType.Mouse, 0, PointerInput.Vertical, 64, false);
deviceInputSystem.changeInput(DeviceType.Mouse, 0, PointerInput.Move, 1);
deviceInputSystem.changeInput(DeviceType.Mouse, 0, PointerInput.LeftClick, 0);
scene.render();

// Perform basic mouse move (shouldn't trigger observable because neither update() or render() were called)
deviceInputSystem.changeInput(DeviceType.Mouse, 0, PointerInput.LeftClick, 1);
deviceInputSystem.changeInput(DeviceType.Mouse, 0, PointerInput.Horizontal, 127, false);
deviceInputSystem.changeInput(DeviceType.Mouse, 0, PointerInput.Vertical, 127, false);
deviceInputSystem.changeInput(DeviceType.Mouse, 0, PointerInput.Move, 1);
deviceInputSystem.changeInput(DeviceType.Mouse, 0, PointerInput.LeftClick, 0);

// Perform mouse move and then run update() (should trigger observable)
deviceInputSystem.changeInput(DeviceType.Mouse, 0, PointerInput.LeftClick, 1);
deviceInputSystem.changeInput(DeviceType.Mouse, 0, PointerInput.Horizontal, 96, false);
deviceInputSystem.changeInput(DeviceType.Mouse, 0, PointerInput.Vertical, 96, false);
deviceInputSystem.changeInput(DeviceType.Mouse, 0, PointerInput.Move, 1);
deviceInputSystem.changeInput(DeviceType.Mouse, 0, PointerInput.LeftClick, 0);
camera.update();

// Perform basic mouse move and then call update() and render() (should trigger observable ONLY ONCE)
deviceInputSystem.changeInput(DeviceType.Mouse, 0, PointerInput.LeftClick, 1);
deviceInputSystem.changeInput(DeviceType.Mouse, 0, PointerInput.Horizontal, 127, false);
deviceInputSystem.changeInput(DeviceType.Mouse, 0, PointerInput.Vertical, 127, false);
deviceInputSystem.changeInput(DeviceType.Mouse, 0, PointerInput.Move, 1);
deviceInputSystem.changeInput(DeviceType.Mouse, 0, PointerInput.LeftClick, 0);
// This should trigger the observable
camera.update();
// This should NOT trigger the observable
scene.render();
}

expect(viewMatrixChangedCt).toBe(4);
});

it("can fire onProjectionMatrixObservable on camera.update", () => {
let projectionMatrixChangedCt = 0;

if (deviceInputSystem && camera && scene) {
camera.onProjectionMatrixChangedObservable.add(() => {
projectionMatrixChangedCt++;
});

// Change FOV and then run render() (should trigger observable)
camera.fov = 0.8;
scene.render();

// Change FOV and then run update() (should trigger observable)
camera.fov = 0.7;
camera.update();

// Change FOV and then run update() then render() (should trigger observable only once)
camera.fov = 0.6;
camera.update();
// This should NOT trigger the observable
scene.render();
}

expect(projectionMatrixChangedCt).toBe(3);
});
});