Atlaslog
Atlaslog / Examples / Cesium / Clipping Plane
Cesium · Runnable Example

Clipping Plane

Runnable Cesium example: Clipping Plane. 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-clipping-plane.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/cesium-clipping-plane.html
<!--
  示例:用裁剪平面剖开地球
  要点:globe.clippingPlanes 接收 ClippingPlaneCollection,滑杆改变平面到地心的距离
  依赖: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%}
  .hud{position:absolute;left:12px;top:12px;z-index:5;background:rgba(23,28,35,.9);
       border:1px solid #2a313b;border-radius:6px;padding:9px 12px;font-size:12px;
       color:#a2a9b6;max-width:252px}
  .hud b{color:#e6e8ec;font-weight:600}
  .hud .row{margin-top:7px}
  .hud button{font:inherit;font-size:12px;padding:3px 9px;margin-right:4px;border-radius:4px;
       cursor:pointer;border:1px solid #343b46;background:#1d232b;color:#cfd6e2}
  .hud button.on{background:#1d3a5c;border-color:#3d6f9e;color:#cfe4ff}
  input[type=range]{width:100%}
  .cesium-viewer-bottom .cesium-widget-credits,.cesium-viewer-animationContainer,
  .cesium-viewer-timelineContainer{display:none!important}
</style>
</head>
<body>
<div id="c"></div>
<div class="hud">
  <div><b>裁剪平面</b></div>
  <div class="row" id="info">已启用 · 切面位置 0 km</div>
  <div class="row">
    <button id="toggle" class="on">启用</button>
    <button data-axis="0">X 轴</button>
    <button data-axis="1">Y 轴</button>
    <button data-axis="2">Z 轴</button>
  </div>
  <div class="row"><input id="pos" type="range" min="-6371000" max="6371000" step="10000" value="0"></div>
</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('#25455f');
  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 axis = 0;
  var distance = 0;
  var enabled = true;
  var info = document.getElementById('info');

  function normalVector() {
    if (axis === 0) return new Cesium.Cartesian3(1, 0, 0);
    if (axis === 1) return new Cesium.Cartesian3(0, 1, 0);
    return new Cesium.Cartesian3(0, 0, 1);
  }

  function rebuild() {
    if (!enabled) {
      scene.globe.clippingPlanes = undefined;
      return;
    }
    scene.globe.clippingPlanes = new Cesium.ClippingPlaneCollection({
      planes: [new Cesium.ClippingPlane(normalVector(), distance)],
      edgeWidth: 2.0,
      edgeColor: Cesium.Color.fromCssColorString('#4d9bff')
    });
  }

  function refreshInfo() {
    info.textContent = (enabled ? '已启用' : '已关闭') +
      ' · 切面位置 ' + Math.round(distance / 1000) + ' km';
  }

  var axisButtons = document.querySelectorAll('[data-axis]');
  for (var i = 0; i < axisButtons.length; i++) {
    axisButtons[i].addEventListener('click', function () {
      axis = parseInt(this.getAttribute('data-axis'), 10);
      for (var k = 0; k < axisButtons.length; k++) axisButtons[k].className = '';
      this.className = 'on';
      rebuild();
      refreshInfo();
    });
  }

  axisButtons[0].className = 'on';

  var toggle = document.getElementById('toggle');
  toggle.addEventListener('click', function () {
    enabled = !enabled;
    this.className = enabled ? 'on' : '';
    this.textContent = enabled ? '启用' : '已关闭';
    rebuild();
    refreshInfo();
  });

  document.getElementById('pos').addEventListener('input', function () {
    distance = parseFloat(this.value);
    rebuild();
    refreshInfo();
  });

  /* 参考点:赤道上的几个位置,裁剪只作用于球体本身,实体不会被切 */
  var lngs = [-150, -90, -30, 30, 90, 150];
  lngs.forEach(function (lng) {
    viewer.entities.add({
      position: Cesium.Cartesian3.fromDegrees(lng, 0, 0),
      point: {
        pixelSize: 6,
        color: Cesium.Color.fromCssColorString('#ffb13d'),
        disableDepthTestDistance: Number.POSITIVE_INFINITY
      },
      label: {
        text: lng + '°',
        font: '500 11px sans-serif',
        fillColor: Cesium.Color.fromCssColorString('#ffb13d'),
        pixelOffset: new Cesium.Cartesian2(0, -14),
        disableDepthTestDistance: Number.POSITIVE_INFINITY
      }
    });
  });

  rebuild();

  viewer.camera.flyTo({
    destination: Cesium.Cartesian3.fromDegrees(105, 22, 18000000),
    orientation: { heading: 0, pitch: Cesium.Math.toRadians(-85), 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 · EXAMPLEClipping Regions Cesium · EXAMPLEClock Control Cesium · EXAMPLECloud Parameters