Picking and scene interaction
Quick answer
This tutorial distinguishes the four CesiumJS picking entry points and what each returns: scene.pick, scene.drillPick, scene.pickPosition and camera.pickEllipsoid. It shows how picked.id exposes an Entity while Cesium3DTileFeature exposes getProperty, and how drillPick cycles stacked features. It also covers throttling MOUSE_MOVE, falling back from pickPosition to pickEllipsoid, and picking caveats such as allowPicking and pickTranslucentDepth. Readers can implement reliable click and hover interactions.
Key points
- Branch on picked.id instanceof Entity versus Cesium3DTileFeature before reading properties.
- Use drillPick with a limit to cycle through stacked features at one pixel.
- Call pickPosition first and fall back to pickEllipsoid when the depth buffer misses.
Example
var handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
handler.setInputAction(function (movement) {
var picked = viewer.scene.pick(movement.position);
if (!Cesium.defined(picked)) {
console.log('点到了空白处');
return;
}
if (picked.id instanceof Cesium.Entity) {
console.log('实体', picked.id.name, picked.id.properties);
} else if (picked instanceof Cesium.Cesium3DTileFeature) {
console.log('瓦片要素', picked.getProperty('name'));
} else {
console.log('图元', picked.primitive);
}
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);var cycle = 0;
var lastPixel = null;
handler.setInputAction(function (movement) {
var pos = movement.position;
if (!lastPixel || lastPixel.x !== pos.x || lastPixel.y !== pos.y) {
cycle = 0;
lastPixel = pos;
}
var stack = viewer.scene.drillPick(pos, 6);
if (stack.length === 0) return;
cycle = (cycle + 1) % stack.length;
var target = stack[cycle];
console.log('第', cycle + 1, '层', target.id && target.id.name);
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
Notes
- Picking uses the previous frame, so an entity added in the same tick may return undefined.
- Translucent geometry needs scene.pickTranslucentDepth = true to be pickable.
English edition written for Atlaslog. Code is taken from the original Chinese page and runs unchanged.
Atlaslog (2026). Picking and scene interaction. https://www.jingweizhi.com/en/cesium/tutorials/picking.html (accessed 2026-11-03).