Atlaslog
Atlaslog / Examples / Cesium / Polyline Volume
Cesium · Runnable Example

Polyline Volume

Runnable Cesium example: Polyline Volume. 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-polyline-volume.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/cesium-polyline-volume.html
<!--
  示例:沿折线扫出管状体
  要点:PolylineVolumeGraphics 用二维截面 shape 沿三维路径扫掠,cornerType 控制拐弯
  依赖: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:250px}
  .hud b{color:#e6e8ec;font-weight:600}
  .hud .row{margin-top:6px}
  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">截面半径 <span id="rVal">18</span> 米</div>
  <div class="row"><input id="radius" type="range" min="6" max="40" step="2" value="18"></div>
  <div class="row">圆形截面 24 个顶点</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('#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;

  /* 生成一个半径为 r 的圆形截面 */
  function circleShape(r, segments) {
    var pts = [];
    for (var i = 0; i < segments; i++) {
      var a = i / segments * Math.PI * 2;
      pts.push(new Cesium.Cartesian2(Math.cos(a) * r, Math.sin(a) * r));
    }
    return pts;
  }

  /* 管道走线:从西向东抬升,再向北绕一个弯 */
  var path = [
    Cesium.Cartesian3.fromDegrees(116.362, 39.906, 60),
    Cesium.Cartesian3.fromDegrees(116.374, 39.906, 130),
    Cesium.Cartesian3.fromDegrees(116.386, 39.906, 210),
    Cesium.Cartesian3.fromDegrees(116.396, 39.910, 260),
    Cesium.Cartesian3.fromDegrees(116.404, 39.918, 200),
    Cesium.Cartesian3.fromDegrees(116.412, 39.922, 140)
  ];

  var pipe = viewer.entities.add({
    polylineVolume: {
      positions: path,
      shape: circleShape(18, 24),
      cornerType: Cesium.CornerType.ROUNDED,
      material: Cesium.Color.fromCssColorString('#5fd0a8').withAlpha(0.85),
      outline: true,
      outlineColor: Cesium.Color.fromCssColorString('#d8fff0')
    }
  });

  /* 第二根方截面管道,走另一条线做对比 */
  viewer.entities.add({
    polylineVolume: {
      positions: [
        Cesium.Cartesian3.fromDegrees(116.366, 39.898, 40),
        Cesium.Cartesian3.fromDegrees(116.380, 39.898, 120),
        Cesium.Cartesian3.fromDegrees(116.394, 39.898, 120),
        Cesium.Cartesian3.fromDegrees(116.406, 39.898, 40)
      ],
      shape: [
        new Cesium.Cartesian2(-14, -14), new Cesium.Cartesian2(14, -14),
        new Cesium.Cartesian2(14, 14), new Cesium.Cartesian2(-14, 14)
      ],
      cornerType: Cesium.CornerType.MITERED,
      material: Cesium.Color.fromCssColorString('#4d9bff').withAlpha(0.6)
    }
  });

  /* 路径两端的立柱,强调扫掠体的起止 */
  [path[0], path[path.length - 1]].forEach(function (p) {
    viewer.entities.add({
      position: p,
      cylinder: {
        length: 60, topRadius: 6, bottomRadius: 6,
        material: Cesium.Color.fromCssColorString('#ffb13d')
      }
    });
  });

  document.getElementById('radius').addEventListener('input', function () {
    var r = parseInt(this.value, 10);
    document.getElementById('rVal').textContent = r;
    pipe.polylineVolume.shape = circleShape(r, 24);
  });

  viewer.camera.flyTo({
    destination: Cesium.Cartesian3.fromDegrees(116.386, 39.886, 3100),
    orientation: {
      heading: Cesium.Math.toRadians(6),
      pitch: Cesium.Math.toRadians(-25),
      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 · EXAMPLEPostprocess Bloom Cesium · EXAMPLEPostprocess Color Cesium · EXAMPLEProcedural Terrain