Atlaslog
Atlaslog / Examples / Cesium / 3D Models Node Explorer Dev
Cesium · Runnable Example

3D Models Node Explorer Dev

Runnable Cesium example: 3D Models Node Explorer Dev. 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-models-node-explorer-dev.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/cesium-3d-models-node-explorer-dev.html
<!--
  示例:浏览模型内部节点
  说明:GLB 由三层楼板拼成,每层是独立节点(level-1 … level-3),按节点名循环高亮
  核心逻辑:多节点 GLB → Model.fromGltfAsync → model.getNode(name) 取节点 → 叠加高亮盒
  依赖: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}
  .tip b{color:#4d9bff}
  .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">模型里三个节点轮流高亮:<b id="cur">level-1</b><br>节点名写在 GLB 的 nodes 里,用 model.getNode 取回。</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;
  }

  /* 每层一个节点:返回 GLB 与楼层信息 */
  function floorsGlb(w, floorH, d, count, color) {
    var pos = [], nor = [], idx = [], views = [], accs = [], meshes = [], nodes = [];
    var offset = 0, vert = 0;
    for (var f = 0; f < count; f++) {
      var y0 = f * floorH, y1 = y0 + floorH * 0.7;
      var hx = w / 2, hz = d / 2;
      var faces = [
        [[hx, y0, hz], [hx, y0, -hz], [hx, y1, -hz], [hx, y1, hz], [1, 0, 0]],
        [[-hx, y0, -hz], [-hx, y0, hz], [-hx, y1, hz], [-hx, y1, -hz], [-1, 0, 0]],
        [[-hx, y1, hz], [hx, y1, hz], [hx, y1, -hz], [-hx, y1, -hz], [0, 1, 0]],
        [[-hx, y0, -hz], [hx, y0, -hz], [hx, y0, hz], [-hx, y0, hz], [0, -1, 0]],
        [[-hx, y0, hz], [hx, y0, hz], [hx, y1, hz], [-hx, y1, hz], [0, 0, 1]],
        [[hx, y0, -hz], [-hx, y0, -hz], [-hx, y1, -hz], [hx, y1, -hz], [0, 0, -1]]
      ];
      var start = pos.length / 3;
      faces.forEach(function (fc) {
        for (var i = 0; i < 4; i++) {
          pos.push(fc[i][0], fc[i][1], fc[i][2]);
          nor.push(fc[4][0], fc[4][1], fc[4][2]);
        }
        idx.push(vert, vert + 1, vert + 2, vert, vert + 2, vert + 3);
        vert += 4;
      });
      accs.push(
        { bufferView: views.length, componentType: 5126, count: 24, type: 'VEC3',
          min: [-hx, y0, -hz], max: [hx, y1, hz] },
        { bufferView: views.length + 1, componentType: 5126, count: 24, type: 'VEC3' },
        { bufferView: views.length + 2, componentType: 5123, count: 36, type: 'SCALAR' }
      );
      views.push(
        { buffer: 0, byteOffset: offset, byteLength: 288, target: 34962 },
        { buffer: 0, byteOffset: offset + 288, byteLength: 288, target: 34962 },
        { buffer: 0, byteOffset: offset + 576, byteLength: 72, target: 34963 }
      );
      offset += 648;
      meshes.push({ primitives: [{ attributes: { POSITION: accs.length - 3, NORMAL: accs.length - 2 },
        indices: accs.length - 1, material: 0 }] });
      nodes.push({ mesh: f, name: 'level-' + (f + 1) });
    }
    var p = new Float32Array(pos), n = new Float32Array(nor), ix = new Uint16Array(idx);
    var bin = new Uint8Array(p.byteLength + n.byteLength + ix.byteLength);
    bin.set(new Uint8Array(p.buffer), 0);
    bin.set(new Uint8Array(n.buffer), p.byteLength);
    bin.set(new Uint8Array(ix.buffer), p.byteLength + n.byteLength);

    var json = {
      asset: { version: '2.0' },
      scene: 0,
      scenes: [{ nodes: nodes.map(function (_, i) { return i; }) }],
      nodes: nodes,
      meshes: meshes,
      materials: [{ pbrMetallicRoughness: {
        baseColorFactor: [color[0], color[1], color[2], 1],
        metallicFactor: 0.1, roughnessFactor: 0.8 }, doubleSided: true }],
      accessors: accs,
      bufferViews: views,
      buffers: [{ byteLength: bin.length }]
    };
    return new Blob([glbBytes(json, bin)], { type: 'model/gltf-binary' });
  }

  var floorH = 4;
  var url = URL.createObjectURL(floorsGlb(16, floorH, 12, 3, [0.5, 0.58, 0.68]));
  var origin = Cesium.Cartesian3.fromDegrees(116.391, 39.907, 0);
  var enu = Cesium.Transforms.eastNorthUpToFixedFrame(origin);
  var names = ['level-1', 'level-2', 'level-3'];

  /* 叠在高亮层上的半透明盒子 */
  var mark = viewer.entities.add({
    position: origin,
    box: {
      dimensions: new Cesium.Cartesian3(17, 17, floorH),
      material: Cesium.Color.fromCssColorString('#4d9bff').withAlpha(0.25),
      outline: true,
      outlineColor: Cesium.Color.fromCssColorString('#4d9bff')
    },
    show: false
  });
  var label = document.getElementById('cur');

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

    var i = 0;
    function tick() {
      var name = names[i % names.length];
      var node = model.getNode(name);              /* 节点存在时返回 ModelNode */
      var y = (i % names.length) * floorH + floorH / 2;
      var p = Cesium.Cartesian3.fromDegrees(116.391, 39.907, y);
      mark.position = new Cesium.ConstantPositionProperty(p);
      mark.show = !!node;
      label.textContent = node ? name : name + '(未找到)';
      i++;
      setTimeout(tick, 1600);
    }
    tick();
  });

  viewer.camera.flyTo({
    destination: Cesium.Cartesian3.fromDegrees(116.3875, 39.9002, 60),
    orientation: { heading: Cesium.Math.toRadians(35), 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 1.1 Cdb Yemen Cesium · EXAMPLE3D Tiles 1.1 Photogrammetry Cesium · EXAMPLE3D Tiles 1.1 Photogrammetry Classification