ScreenSpaceCameraController是CesiumJS中的摄像机控制器,可以通过鼠标和触摸屏交互来控制场景中的摄像机。
要使用ScreenSpaceCameraController,请在Cesium Viewer对象上调用screenSpaceCameraController属性。例如:
var viewer = new Cesium.Viewer('cesiumContainer');
viewer.screenSpaceCameraController.enableTranslate = true;
ScreenSpaceCameraController具有以下属性:
enableRotateBooleantrueenableTranslateBooleantrueenableZoomBooleantrueenableTiltBooleantrueenableLookBooleantrueminimumZoomDistanceNumber0.0maximumZoomDistanceNumberInfinitymaximumPickingDistanceNumberNumber.POSITIVE_INFINITYminimumCollisionTerrainHeightNumber0.0minimumCollisionObjectHeightNumberNumber.NEGATIVE_INFINITYminimumTrackBallHeightNumber0.0enableCollisionDetectionBooleantrueenableLookAroundBooleantrueenableDistanceResetBooleantrueenableCompassBooleantrueenableZoomOutBooleantrueenableZoomInBooleantrueenableRotateLeftBooleantrueenableRotateRightBooleantrueenableRotateUpBooleantrueenableRotateDownBooleantrueenableTiltLeftBooleantrueenableTiltRightBooleantrueenableTiltUpBooleantrueenableTiltDownBooleantrueminimumPickingTerrainHeightNumberNumber.NEGATIVE_INFINITYtiltEventTypesArray[Cesium.CameraEventType.LEFT_DRAG, Cesium.CameraEventType.PINCH]rotateEventTypesArray[Cesium.CameraEventType.LEFT_DRAG, Cesium.CameraEventType.PINCH]translateEventTypesArray[Cesium.CameraEventType.RIGHT_DRAG]zoomEventTypesArray[Cesium.CameraEventType.WHEEL, Cesium.CameraEventType.PINCH]lookEventTypesArray[Cesium.CameraEventType.RIGHT_DRAG]ScreenSpaceCameraController具有以下方法:
lookAtdestination: 待查看位置的Cartesian3,或与direction一起用于确定目标轴向的Matrix4。offset: 相机与目标位置的偏移量。默认为Cesium.Cartesian3.ZERO。orientation: 相机的方向角。默认为Cesium.Matrix4.IDENTITY。getPickRaywindowPosition: 屏幕位置。ScreenSpaceCameraController定义了以下事件:
tiltStartEventtiltEndEventrotateStartEventrotateEndEventtranslateStartEventtranslateEndEventzoomStartEventzoomEndEventlookStartEventlookEndEventactivateEventdeactivateEvent以下是一个使用ScreenSpaceCameraController的简单示例:
var viewer = new Cesium.Viewer('cesiumContainer');
viewer.screenSpaceCameraController.rotateEventTypes = [Cesium.CameraEventType.RIGHT_DRAG];
viewer.screenSpaceCameraController.translateEventTypes = [Cesium.CameraEventType.LEFT_DRAG];
viewer.screenSpaceCameraController.zoomEventTypes = [Cesium.CameraEventType.MOUSE_WHEEL, Cesium.CameraEventType.PINCH];
viewer.screenSpaceCameraController.enableTranslate = true;
viewer.screenSpaceCameraController.enableZoom = true;
viewer.screenSpaceCameraController.enableTilt = true;
viewer.screenSpaceCameraController.enableRotate = true;
viewer.screenSpaceCameraController.tiltEventTypes = [Cesium.CameraEventType.RIGHT_DRAG, Cesium.CameraEventType.PINCH, Cesium.CameraEventType.LEFT_DRAG];
viewer.screenSpaceCameraController.rotateEventTypes = [Cesium.CameraEventType.RIGHT_DRAG, Cesium.CameraEventType.PINCH];
viewer.screenSpaceCameraController.translateEventTypes = [Cesium.CameraEventType.RIGHT_DRAG];
viewer.screenSpaceCameraController.changed.addEventListener(function() {
console.log("Camera changed.");
});
viewer.camera.setView({
destination: Cesium.Cartesian3.fromDegrees(-122.4194, 37.7749, 10000.0)
});
在这个例子中,我们将rotateEventTypes设置为RIGHT_DRAG,将translateEventTypes设置为LEFT_DRAG,将zoomEventTypes设置为MOUSE_WHEEL和PINCH,以使这些事件触发相应的控制器操作。我们还启用了平移、缩放、倾斜和旋转控制器,设置了支持倾斜和旋转的事件类型,并注册了一个回调函数,以便在相机状态更改时记录消息。最后,我们设置了相机的初始视图并创建了一个新的Cesium Viewer。