Atlaslog
Atlaslog / Examples / Cesium / Voxel Picking
Cesium · Runnable Example

Voxel Picking

Runnable Cesium example: Voxel Picking. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.

Open standalone → Chinese notes All Cesium examples
/demos/cesium-voxel-picking.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/cesium-voxel-picking.html
<!--
  示例:体素拾取
  说明:点击体素方块,被选中的格子消失,再点一次恢复
  核心逻辑:GeometryInstance 带 id → scene.pick 拿到实例 → 重建几何实例集合
  依赖:CesiumJS 1.121 · jsDelivr CDN
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>体素拾取</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/cesium@1.121.0/Build/Cesium/Widgets/widgets.css">
<style>
  html,body{height:100%;margin:0}
  body{background:#0e1116;color:#e6e8ec;overflow:hidden;
       font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif}
  #c{height:100%}
  .tip{position:absolute;left:12px;top:12px;z-index:5;
       background:rgba(23,28,35,.9);border:1px solid #2a313b;border-radius:6px;
       padding:8px 12px;font-size:12px;color:#a2a9b6;max-width:320px;line-height:1.6}

  .info{position:absolute;right:12px;top:12px;background:rgba(23,28,35,.92);
        border:1px solid #2a313b;border-radius:6px;padding:8px 12px;font-size:12px;color:#cfe2f5}
  .cesium-viewer-bottom .cesium-widget-credits,.cesium-viewer-animationContainer,
  .cesium-viewer-timelineContainer{display:none!important}
</style>
</head>
<body>
<div id="c"></div>
<div class="tip">点击任意方块会让它消失,模拟体素编辑中的擦除操作。</div>
<script src="https://cdn.jsdelivr.net/npm/cesium@1.121.0/Build/Cesium/Cesium.js"></script>
<script>
(function () {
  'use strict';

  var viewer = new Cesium.Viewer('c', {
    baseLayer: false,
    baseLayerPicker: false, geocoder: false, homeButton: false,
    sceneModePicker: false, navigationHelpButton: false,
    animation: false, timeline: false, fullscreenButton: false,
    infoBox: false, selectionIndicator: false
  });

  var scene = viewer.scene;
  scene.globe.baseColor = Cesium.Color.fromCssColorString('#1c2733');
  scene.backgroundColor = Cesium.Color.fromCssColorString('#0e1116');
  scene.skyBox.show = false;
  scene.sun.show = false;
  scene.moon.show = false;
  if (scene.skyAtmosphere) scene.skyAtmosphere.show = false;

  var NX = 10, NY = 10, NZ = 5, S = 8;
  var origin = Cesium.Transforms.eastNorthUpToFixedFrame(
    Cesium.Cartesian3.fromDegrees(116.3895, 39.9035, 0)
  );
  var removed = {};
  var primitive;

  function build() {
    var instances = [];
    for (var ix = 0; ix < NX; ix++) {
      for (var iy = 0; iy < NY; iy++) {
        for (var iz = 0; iz < NZ; iz++) {
          var key = ix + '_' + iy + '_' + iz;
          if (removed[key]) continue;
          var local = Cesium.Matrix4.multiplyByPoint(
            origin,
            new Cesium.Cartesian3((ix - NX / 2) * S, (iy - NY / 2) * S, iz * S + S / 2),
            new Cesium.Cartesian3()
          );
          instances.push(new Cesium.GeometryInstance({
            id: key,
            geometry: new Cesium.BoxGeometry({
              dimensions: new Cesium.Cartesian3(S * 0.9, S * 0.9, S * 0.9),
              vertexFormat: Cesium.PerInstanceColorAppearance.VERTEX_FORMAT
            }),
            modelMatrix: Cesium.Matrix4.fromTranslation(local),
            attributes: {
              color: Cesium.ColorGeometryInstanceAttribute.fromColor(
                Cesium.Color.fromHsl(0.08 + iz * 0.06, 0.5, 0.45, 1.0)
              )
            }
          }));
        }
      }
    }
    if (primitive) scene.primitives.remove(primitive);
    primitive = scene.primitives.add(new Cesium.Primitive({
      geometryInstances: instances,
      appearance: new Cesium.PerInstanceColorAppearance({ flat: true }),
      asynchronous: false
    }));
  }
  build();

  var info = document.createElement('div');
  info.className = 'info';
  info.style.display = 'none';
  document.body.appendChild(info);

  var handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
  handler.setInputAction(function (click) {
    var picked = scene.pick(click.position);
    if (!Cesium.defined(picked) || !picked.id) return;
    var key = picked.id;
    removed[key] = !removed[key];
    build();
    info.style.display = 'block';
    info.textContent = (removed[key] ? '已擦除 ' : '已恢复 ') + '体素 ' + key;
  }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  viewer.camera.flyTo({
    destination: Cesium.Cartesian3.fromDegrees(116.3836, 39.8990, 160),
    orientation: { heading: Cesium.Math.toRadians(40), pitch: Cesium.Math.toRadians(-16), roll: 0 },
    duration: 0
  });
})();
</script>
</body>
</html>
Language: the code and this page are in English; the full tutorial and API notes for this example are in Chinese. The demo itself is the authoritative version — it runs exactly as shown.

More Cesium examples

Cesium · EXAMPLEVoxels Cesium · EXAMPLEWall Extrude Cesium · EXAMPLEWashington Dc 2017