如何防止cesium相机视角翻转至地底下,以下是方案,亲测可用
// 禁止相机翻转到地下 start
var minPitch = -Cesium.Math.PI_OVER_TWO
var maxPitch = 0
var minHeight = 200
viewer.camera.changed.addEventListener(
function() {
if (viewer.camera._suspendTerrainAdjustment && viewer.scene.mode === Cesium.SceneMode.SCENE3D) {
viewer.camera._suspendTerrainAdjustment = false
viewer.camera._adjustHeightForTerrain()
}
// Keep camera in a reasonable pitch range
var pitch = viewer.camera.pitch;
if (pitch > maxPitch || pitch < minPitch) {
viewer.scene.screenSpaceCameraController.enableTilt = false;
// clamp the pitch
if (pitch > maxPitch) {
pitch = maxPitch
} else if (pitch < minPitch) {
pitch = minPitch
}
var destination = Cesium.Cartesian3.fromRadians(
viewer.camera.positionCartographic.longitude,
viewer.camera.positionCartographic.latitude,
Math.max(viewer.camera.positionCartographic.height, minHeight))
viewer.camera.setView({
destination: destination,
orientation: { pitch: pitch }
})
viewer.scene.screenSpaceCameraController.enableTilt = true;
}
}
)
// 禁止相机翻转到地下 end