1 window.viewer.scene拾取
1.1 window.viewer.scene.pick
1.2 window.viewer.scene.pickPosition
1.3 window.viewer.scene.drillPick
2 window.viewer.scene.camera拾取
3 window.viewer.scene.globe拾取
4 window.viewer.imageryLayers.pickImageryLayerFeatures 拾取
在 Cesium 的场景组织中,有那么几个容器构成了三维世界:
Scene:包括了 Globe,除了 Globe 的元素外,还加上了 Primitive、Entity、DataSource 等三维物件
Globe:包括了 Ellipsoid,还包括了所有的影像图层、地形瓦片,可以算是椭球体上面的皮肤
Ellipsoid:一个数学公式所定义的旋转椭球体,代表一个纯粹的地球椭球形状
1 window.viewer.scene拾取
1.1 window.viewer.scene.pick
window.viewer.scene.pick (windowPosition,width , height ) →0bject
拾取cesium场景中的空间对象,且返回最前面的一个空间对象,可以拾取entity实体,primitive图元,数据源datasource,3dtiles瓦片数据。
let handler = new Cesium.ScreenSpaceEventHandler(window.viewer.canvas);
handler.setInputAction(function (event) {
let pickedObject = window.viewer.scene.pick(event.position);
if (!Cesium.defined(pickedObject)) {
console.log("没有拾取到空间对象")
return;
}
console.log(pickedObject)
}, Cesium.ScreenSpaceEventType.LEFT_CLICK)
1.2 window.viewer.scene.pickPosition
window.viewer.scene.pickPosition (windowPosition,result)→Cartesian3
拾取cesium场景中的笛卡尔坐标,并返回笛卡尔坐标值。
注意:获取到笛卡尔坐标存在不准确性:
1.在没有地质的情况下,不开启深度测试,在地球上拾取是准确的;
2.在有地质的情况下,不开启深度测试,在地球上拾取是不准确的;因为存在深度问题,必须开启深度测试。
let handler = new Cesium.ScreenSpaceEventHandler(window.viewer.canvas);
handler.setInputAction(function (event) {
let earthPosition = window.viewer.scene.pickPosition(event.position);
if (Cesium.defined(earthPosition)) {
let cartographic = Cesium.Cartographic.fromCartesian(earthPosition);
let lon = Cesium.Math.toDegrees(cartographic.longitude).toFixed(5);
let lat = Cesium.Math.toDegrees(cartographic.latitude).toFixed(5);
let height = cartographic.height.toFixed(2);
console.log(earthPosition, {
lon: lon,
lat: lat,
height: height,
});
}
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
深度测试开启
window.viewer.scene.globe.depthTestAgainstTerrain = true;
1.3 window.viewer.scene.drillPick
window.viewer.scene.drillPick (windowPosition,limit ,width,height ) →Array.<*>
拾取场景中的对象,并返回对象数组且limit限定的最多数量,可以拾取entity实体,primitive图元,数据源datasource,3dtiles瓦片数据。
let handler = new Cesium.ScreenSpaceEventHandler(window.viewer.canvas);
handler.setInputAction(function (event) {
//event包含坐标信息,3是返回的数量,大于三则为最前面3个,否则实际获取的数量
let pickedObject = viewer.scene.drillPick(event.position, 3);
console.log(pickedObject)
}, Cesium.ScreenSpaceEventType.LEFT_CLICK)
2 window.viewer.scene.camera拾取
window.viewer.scene.camera.pickEllipsoid (windowPosition,ellipsoid , result ) →Cartesian3
获取相机视角下鼠标点的对应椭球面位置
注意:此方法是用于获取椭球面的笛卡尔坐标信息,且不用考虑地质的深度。
如果用该方法获取的坐标拾取点是存在不准确性的,因为该方法只获取与球面相交的坐标点,不包含地形、模型、倾斜摄影表面带有高度的对象值。在有地质数据时,所以无论是否开启深度测试,获取的三维坐标数据都是不准确。反正记住,使用此方法,关掉深度测试。
3 window.viewer.scene.globe拾取
let ray = viewer.camera.getPickRay(event.position);
window.viewer.scene.globe.pick (ray,scene,result ) →Cartesian3
专门用于获取地形加载后,拾取地形上的经纬度(弧度)以及高程
let handler = new Cesium.ScreenSpaceEventHandler(window.viewer.canvas);
handler.setInputAction(function (event) {
//相机到鼠标点的射线
let ray = viewer.camera.getPickRay(event.position);
//相机到鼠标点的射线与地形场景相交
let position = viewer.scene.globe.pick(ray, viewer.scene);
console.log(position);
//笛卡尔坐标转经纬度(弧度)
let cartesian = Cesium.Cartographic.fromCartesian(position);
console.log(cartesian)
}, Cesium.ScreenSpaceEventType.LEFT_CLICK)
注意:此方法无论是否存在地质,无论是否开启地质的深度测试,获取与地形的笛卡尔坐标都是准确的,因为此方法只是与地形的求交,不包括模型、倾斜摄影表面。
4 window.viewer.imageryLayers.pickImageryLayerFeatures 拾取
window.viewer.imageryLayers.pickImageryLayerFeatures (ray,scene)→Promise.<Array.<ImageryLayerFeatureInfo >>
用于获取影像图层信息
let handler = new Cesium.ScreenSpaceEventHandler(window.viewer.canvas);
handler.setInputAction((event) => {
let pickRay = window.viewer.camera.getPickRay(event.position);
let ImageryLayerFeatures = window.viewer.imageryLayers.pickImageryLayerFeatures(pickRay, window.viewer.scene);
if (!Cesium.defined(ImageryLayerFeatures)) {
console.log('没有影像图层要素信息选中');
} else {
Cesium.when(ImageryLayerFeatures, function (LayerFeatures) {
//获取到图层数据
if (LayerFeatures.length > 0) {
console.log(LayerFeatures)
}
});
}
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);