Atlaslog
Atlaslog / Examples / Cesium / Czml Box
Cesium · Runnable Example

Czml Box

Runnable Cesium example: Czml Box. 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-czml-box.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/cesium-czml-box.html
<!--
  示例:用 CZML 描述两个盒子,一个正放一个绕地轴旋转
  说明:尺寸写在 box.dimensions,朝向写在 box.orientation.unitQuaternion
  核心逻辑:CzmlDataSource.load → dataSources.add → 相机俯视对比
  依赖: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>CZML 盒子</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%}
  .cesium-viewer-bottom .cesium-widget-credits,.cesium-viewer-animationContainer,
  .cesium-viewer-timelineContainer{display:none!important}
</style>
</head>
<body>
<div id="c"></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.moon.show = false;
  if (scene.skyAtmosphere) scene.skyAtmosphere.show = false;
  scene.globe.enableLighting = true;

  /* 绕地轴转 30 度:q = [0, 0, sin(θ/2), cos(θ/2)] */
  var half = Cesium.Math.toRadians(30) / 2;

  var data = [
    { id: 'document', name: '盒子', version: '1.0' },
    {
      id: 'box-plain',
      name: '正放',
      position: { cartographicDegrees: [116.380, 39.900, 120] },
      box: {
        dimensions: { cartesian: [260, 180, 160] },
        material: { solidColor: { color: { rgba: [90, 150, 235, 235] } } },
        outline: true,
        outlineColor: { rgba: [235, 245, 255, 255] }
      }
    },
    {
      id: 'box-rotated',
      name: '绕地轴旋转 30 度',
      position: { cartographicDegrees: [116.402, 39.900, 120] },
      orientation: {
        unitQuaternion: [0, 0, Math.sin(half), Math.cos(half)]
      },
      box: {
        dimensions: { cartesian: [260, 180, 160] },
        material: { solidColor: { color: { rgba: [240, 170, 70, 235] } } },
        outline: true,
        outlineColor: { rgba: [255, 245, 230, 255] }
      }
    },
    {
      id: 'label-plain',
      position: { cartographicDegrees: [116.380, 39.900, 420] },
      label: {
        text: '正放',
        font: '500 13px "Microsoft YaHei", sans-serif',
        fillColor: { rgba: [230, 240, 255, 255] },
        disableDepthTestDistance: Number.POSITIVE_INFINITY
      }
    },
    {
      id: 'label-rotated',
      position: { cartographicDegrees: [116.402, 39.900, 420] },
      label: {
        text: '旋转 30 度',
        font: '500 13px "Microsoft YaHei", sans-serif',
        fillColor: { rgba: [255, 232, 200, 255] },
        disableDepthTestDistance: Number.POSITIVE_INFINITY
      }
    }
  ];

  Cesium.CzmlDataSource.load(data).then(function (ds) {
    viewer.dataSources.add(ds);
  });

  viewer.camera.flyTo({
    destination: Cesium.Cartesian3.fromDegrees(116.391, 39.894, 2400),
    orientation: {
      heading: 0,
      pitch: Cesium.Math.toRadians(-30),
      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 · EXAMPLECzml Circles Ellipses Cesium · EXAMPLECzml Colors Cesium · EXAMPLECzml Cones Cylinders