Cesium 中阻止镜头飞至地表以下

Cesium 镜头飞至地表下后会看到破损的地表,影响用户体验,github上有人给出了解决方法,记录如下:

var viewer = new Cesium.Viewer('cesiumContainer');

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;
        }
    }
);
posted @ 2019-03-07 17:23  flypopo  阅读(1428)  评论(0编辑  收藏  举报