Atlaslog
Atlaslog / Examples / Cesium / 3D Tiles Compare
Cesium · Runnable Example

3D Tiles Compare

Runnable Cesium example: 3D Tiles Compare. 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-3d-tiles-compare.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/cesium-3d-tiles-compare.html
<!--
  示例:双视口对比
  说明:左侧视口裁剪掉北侧,右侧视口保持完整,两个视口共用相机姿态
  核心逻辑:两个 Viewer → 左侧 postRender 中把 positionWC 与欧拉角抄给右侧 setView
  依赖: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}
  .cesium-viewer-bottom .cesium-widget-credits,.cesium-viewer-animationContainer,
  .cesium-viewer-timelineContainer{display:none!important}
  #c,#c2{position:absolute;top:0;width:50%;height:100%}
  #c{left:0}
  #c2{right:0;border-left:1px solid #2a313b}
</style>
</head>
<body>
<div id="c"></div>
<div class="tip">拖动视角时两侧同步。左边模型被北向平面剖开并描蓝边,右边是完整模型。</div>
<div id="c2"></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;

  function glbBytes(json, bin) {
    var jb = new TextEncoder().encode(JSON.stringify(json));
    var jpad = (4 - jb.length % 4) % 4;
    var bpad = (4 - bin.length % 4) % 4;
    var jl = jb.length + jpad, bl = bin.length + bpad;
    var total = 12 + 8 + jl + 8 + bl;
    var out = new Uint8Array(total), dv = new DataView(out.buffer);
    dv.setUint32(0, 0x46546c67, true);
    dv.setUint32(4, 2, true);
    dv.setUint32(8, total, true);
    dv.setUint32(12, jl, true);
    dv.setUint32(16, 0x4e4f534a, true);
    out.set(jb, 20);
    for (var i = 0; i < jpad; i++) out[20 + jb.length + i] = 0x20;
    dv.setUint32(20 + jl, bl, true);
    dv.setUint32(24 + jl, 0x004e4942, true);
    out.set(bin, 28 + jl);
    return out;
  }

  function boxGlb(w, h, d, color) {
    var hx = w / 2, hz = d / 2;
    var faces = [
      [[hx, 0, hz], [hx, 0, -hz], [hx, h, -hz], [hx, h, hz], [1, 0, 0]],
      [[-hx, 0, -hz], [-hx, 0, hz], [-hx, h, hz], [-hx, h, -hz], [-1, 0, 0]],
      [[-hx, h, hz], [hx, h, hz], [hx, h, -hz], [-hx, h, -hz], [0, 1, 0]],
      [[-hx, 0, -hz], [hx, 0, -hz], [hx, 0, hz], [-hx, 0, hz], [0, -1, 0]],
      [[-hx, 0, hz], [hx, 0, hz], [hx, h, hz], [-hx, h, hz], [0, 0, 1]],
      [[hx, 0, -hz], [-hx, 0, -hz], [-hx, h, -hz], [hx, h, -hz], [0, 0, -1]]
    ];
    var pos = [], nor = [], idx = [], base = 0;
    faces.forEach(function (f) {
      for (var i = 0; i < 4; i++) {
        pos.push(f[i][0], f[i][1], f[i][2]);
        nor.push(f[4][0], f[4][1], f[4][2]);
      }
      idx.push(base, base + 1, base + 2, base, base + 2, base + 3);
      base += 4;
    });
    var p = new Float32Array(pos), n = new Float32Array(nor), ix = new Uint16Array(idx);
    var pb = p.byteLength, nb = n.byteLength, ib = ix.byteLength;
    var bin = new Uint8Array(pb + nb + ib);
    bin.set(new Uint8Array(p.buffer), 0);
    bin.set(new Uint8Array(n.buffer), pb);
    bin.set(new Uint8Array(ix.buffer), pb + nb);

    var json = {
      asset: { version: '2.0' },
      scene: 0,
      scenes: [{ nodes: [0] }],
      nodes: [{ mesh: 0 }],
      meshes: [{ primitives: [{ attributes: { POSITION: 0, NORMAL: 1 }, indices: 2, material: 0 }] }],
      materials: [{ pbrMetallicRoughness: {
        baseColorFactor: [color[0], color[1], color[2], 1],
        metallicFactor: 0, roughnessFactor: 0.85 }, doubleSided: true }],
      accessors: [
        { bufferView: 0, componentType: 5126, count: 24, type: 'VEC3',
          min: [-hx, 0, -hz], max: [hx, h, hz] },
        { bufferView: 1, componentType: 5126, count: 24, type: 'VEC3' },
        { bufferView: 2, componentType: 5123, count: 36, type: 'SCALAR' }
      ],
      bufferViews: [
        { buffer: 0, byteOffset: 0, byteLength: pb, target: 34962 },
        { buffer: 0, byteOffset: pb, byteLength: nb, target: 34962 },
        { buffer: 0, byteOffset: pb + nb, byteLength: ib, target: 34963 }
      ],
      buffers: [{ byteLength: pb + nb + ib }]
    };
    return new Blob([glbBytes(json, bin)], { type: 'model/gltf-binary' });
  }  var url = URL.createObjectURL(boxGlb(30, 42, 24, [0.55, 0.62, 0.72]));
  var origin = Cesium.Cartesian3.fromDegrees(116.391, 39.907, 0);
  var enu = Cesium.Transforms.eastNorthUpToFixedFrame(origin);

  /* 右侧视口:完整模型 */
  var right = new Cesium.Viewer('c2', {
    baseLayer: false,
    baseLayerPicker: false, geocoder: false, homeButton: false,
    sceneModePicker: false, navigationHelpButton: false,
    animation: false, timeline: false, fullscreenButton: false,
    infoBox: false, selectionIndicator: false
  });
  var rs = right.scene;
  rs.globe.baseColor = Cesium.Color.fromCssColorString('#1c2733');
  rs.backgroundColor = Cesium.Color.fromCssColorString('#0e1116');
  rs.skyBox.show = false; rs.sun.show = false; rs.moon.show = false;
  if (rs.skyAtmosphere) rs.skyAtmosphere.show = false;

  /* 左侧视口:带裁剪的模型 */
  Cesium.Model.fromGltfAsync({ url: url, modelMatrix: enu }).then(function (model) {
    scene.primitives.add(model);
    model.clippingPlanes = new Cesium.ClippingPlaneCollection({
      planes: [new Cesium.ClippingPlane(new Cesium.Cartesian3(0, 1, 0), 0)],
      edgeColor: Cesium.Color.fromCssColorString('#4d9bff'),
      edgeWidth: 2
    });
  });

  Cesium.Model.fromGltfAsync({ url: url, modelMatrix: enu }).then(function (model) {
    rs.primitives.add(model);
  });

  /* 相机同步:左侧每帧渲染后把姿态抄给右侧 */
  var tmpPos = new Cesium.Cartesian3();
  scene.postRender.addEventListener(function () {
    Cesium.Cartesian3.clone(viewer.camera.positionWC, tmpPos);
    right.camera.setView({
      destination: tmpPos,
      orientation: {
        heading: viewer.camera.heading,
        pitch: viewer.camera.pitch,
        roll: viewer.camera.roll
      }
    });
  });

  viewer.camera.flyTo({
    destination: Cesium.Cartesian3.fromDegrees(116.3850, 39.9025, 160),
    orientation: { heading: Cesium.Math.toRadians(26), pitch: Cesium.Math.toRadians(-14), 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 · EXAMPLE3D Tiles Edge Visibility Cesium · EXAMPLE3D Tiles Feature Picking Cesium · EXAMPLE3D Tiles Feature Styling