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

Added maximumTiltAngle to limit how much the camera can tilt #12169

Merged
merged 7 commits into from
Oct 9, 2024
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
3 changes: 3 additions & 0 deletions Apps/Sandcastle/gallery/Bathymetry.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@

const scene = viewer.scene;

// Prevent the user from tilting beyond the ellipsoid surface
scene.screenSpaceCameraController.maximumTiltAngle = Math.PI / 2.0;

const globe = scene.globe;
globe.enableLighting = true;
globe.maximumScreenSpaceError = 1.0; // Load higher resolution tiles for better seafloor shading
Expand Down
8 changes: 8 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Change Log

### 1.123 - 2024-11-01

#### @cesium/engine

##### Additions :tada:

- Added `ScreenSpaceCameraController.maximumTiltAngle` to limit how much the camera can tilt. [#12169](https://github.com/CesiumGS/cesium/pull/12169)

### 1.122 - 2024-10-01

#### @cesium/engine
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ See [CONTRIBUTING.md](CONTRIBUTING.md) for details on how to contribute to Cesiu
- [Tang Xiaofei](https://github.com/vtxf)
- [Maxar](https://www.maxar.com)
- [Erik Dahlström](https://github.com/erikdahlstrom)
- [Albin Ekberg](https://github.com/albek446)
- [Novatron Oy](https://novatron.fi/en/)
- [Jussi Hirvonen](https://github.com/VsKatshuma)
- [Sierra Nevada Corp](https://www.sncorp.com/)
Expand Down
21 changes: 20 additions & 1 deletion packages/engine/Source/Scene/ScreenSpaceCameraController.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,16 @@ function ScreenSpaceCameraController(scene) {
* @default true
*/
this.enableCollisionDetection = true;
/**
* The angle, relative to the ellipsoid normal, restricting the maximum amount that the user can tilt the camera. If <code>undefined</code>, the angle of the camera tilt is unrestricted.
* @type {number|undefined}
* @default undefined
*
* @example
* // Prevent the camera from tilting below the ellipsoid surface
* viewer.scene.screenSpaceCameraController.maximumTiltAngle = Math.PI / 2.0;
*/
lukemckinstry marked this conversation as resolved.
Show resolved Hide resolved
this.maximumTiltAngle = undefined;

this._scene = scene;
this._globe = undefined;
Expand Down Expand Up @@ -2063,7 +2073,16 @@ function rotate3D(
);

const deltaPhi = rotateRate * phiWindowRatio * Math.PI * 2.0;
const deltaTheta = rotateRate * thetaWindowRatio * Math.PI;
let deltaTheta = rotateRate * thetaWindowRatio * Math.PI;

if (defined(constrainedAxis) && defined(controller.maximumTiltAngle)) {
const maximumTiltAngle = controller.maximumTiltAngle;
const dotProduct = Cartesian3.dot(camera.direction, constrainedAxis);
const tilt = Math.PI - Math.acos(dotProduct) + deltaTheta;
if (tilt > maximumTiltAngle) {
deltaTheta -= tilt - maximumTiltAngle;
}
}

if (!rotateOnlyVertical) {
camera.rotateRight(deltaPhi);
Expand Down
73 changes: 73 additions & 0 deletions packages/engine/Specs/Scene/ScreenSpaceCameraControllerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1568,6 +1568,79 @@ describe("Scene/ScreenSpaceCameraController", function () {
expect(camera.direction).not.toEqual(direction);
});

it("maximum tilt angle is 0 in 3D", function () {
setUp3D();
const originalCameraPosition = Cartesian3.clone(camera.position);
const originalCameraDirection = Cartesian3.clone(camera.direction);
controller.maximumTiltAngle = 0;
const startPosition = new Cartesian2(
canvas.clientWidth / 2,
canvas.clientHeight / 2,
);
const endPosition = new Cartesian2(
canvas.clientWidth / 2,
canvas.clientHeight / 4,
);

moveMouse(MouseButtons.MIDDLE, startPosition, endPosition);
updateController();

expect(camera.position).toEqualEpsilon(
originalCameraPosition,
CesiumMath.EPSILON8,
);
expect(camera.position).not.toEqualEpsilon(
originalCameraDirection,
CesiumMath.EPSILON8,
);

const dotProduct = Cartesian3.dot(
camera.direction,
originalCameraDirection,
);
const acos = Math.acos(dotProduct);
const angle = (acos * 180) / Math.PI;
expect(angle).toEqual(0);
});

it("maximum tilt angle is 45 degrees in 3D", function () {
setUp3D();
const originalCameraPosition = Cartesian3.clone(camera.position);
const originalCameraDirection = Cartesian3.clone(camera.direction);
controller.maximumTiltAngle = (45 * Math.PI) / 180;
const startPosition = new Cartesian2(
canvas.clientWidth / 2,
canvas.clientHeight / 2,
);
const endPosition = new Cartesian2(
canvas.clientWidth / 2,
canvas.clientHeight / 4,
);

moveMouse(MouseButtons.MIDDLE, startPosition, endPosition);
Copy link
Contributor

Choose a reason for hiding this comment

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

why repeat this mouse move? If it is valid can you add a comment to explain?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Certainly.
The reason why I repeated the mouse move was because I could not find a way to get the tilting angle to 45 degrees (0.25π radians) in just one move. I tried to set the endPosition to different values but could not get the final tilt angle high enough to the limit I wanted to test. Maybe I am missing something, and there is a better way?

updateController();
moveMouse(MouseButtons.MIDDLE, startPosition, endPosition);
updateController();

expect(camera.position).not.toEqualEpsilon(
originalCameraPosition,
CesiumMath.EPSILON8,
);
expect(camera.position).not.toEqualEpsilon(
originalCameraDirection,
CesiumMath.EPSILON8,
);

const dotProduct = Cartesian3.dot(
camera.direction,
originalCameraDirection,
);
const acos = Math.acos(dotProduct);
const angle = (acos * 180) / Math.PI;
expect(angle).toBeLessThanOrEqual(45);
expect(angle).toBeGreaterThan(44);
});

it("looks in 3D", function () {
setUp3D();
const position = Cartesian3.clone(camera.position);
Expand Down