Skip to content

Instantly share code, notes, and snippets.

@wewindy
Created September 19, 2023 20:01
Show Gist options
  • Save wewindy/67441c96d4d60c08d68022a212d2d162 to your computer and use it in GitHub Desktop.
Save wewindy/67441c96d4d60c08d68022a212d2d162 to your computer and use it in GitHub Desktop.
Limit CesiumJS Camera Tilt
const viewer = new Cesium.Viewer("cesiumContainer");
const globe = viewer.scene.globe;
const camera = viewer.scene.camera;
const scratchNormal = new Cesium.Cartesian3();
const previousPosition = new Cesium.Cartesian3();
const previousDirection = new Cesium.Cartesian3();
const previousUp = new Cesium.Cartesian3();
const previousRight = new Cesium.Cartesian3();
viewer.scene.postUpdate.addEventListener(function() {
const normal = globe.ellipsoid.geodeticSurfaceNormal(
camera.position,
scratchNormal
);
const dotProduct = Cesium.Cartesian3.dot(camera.direction, normal);
if (dotProduct >= -0.3) {
camera.position = Cesium.Cartesian3.clone(previousPosition, camera.position);
camera.direction = Cesium.Cartesian3.clone(previousDirection, camera.direction);
camera.up = Cesium.Cartesian3.clone(previousUp, camera.up);
camera.right = Cesium.Cartesian3.clone(previousRight, camera.right);
} else {
previousPosition = Cesium.Cartesian3.clone(camera.position, previousPosition);
previousDirection = Cesium.Cartesian3.clone(camera.direction, previousDirection);
previousUp = Cesium.Cartesian3.clone(camera.up, previousUp);
previousRight = Cesium.Cartesian3.clone(camera.right, previousRight);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment