Atlaslog
Atlaslog / Examples / Cesium / Terrain Exaggeration
Cesium · Runnable Example

Terrain Exaggeration

Runnable Cesium example: Terrain Exaggeration. 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-terrain-exaggeration.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/cesium-terrain-exaggeration.html
<!--
  示例:程序生成地形并调整夸张系数
  要点:CustomHeightmapTerrainProvider 按瓦片行列算高程,terrainExaggeration 实时放大起伏
  依赖: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}
  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">系数 1.0 · 最高约 3200 米</div>
  <div class="row"><input id="ex" type="range" min="1" max="12" step="0.5" value="1"></div>
  <div class="row">地形由 32×32 高度图程序生成</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;
  scene.globe.enableLighting = true;

  var SIZE = 32;
  var CENTER_LNG = 103.0;
  var CENTER_LAT = 30.6;

  function heightAt(lng, lat) {
    var dx = (lng - CENTER_LNG) / 1.4;
    var dy = (lat - CENTER_LAT) / 0.95;
    var r = Math.sqrt(dx * dx + dy * dy);
    if (r > 1.8) return 0;

    /* 主山脊 + 两组高频起伏,做出沟谷与次峰 */
    var base = 3400 * Math.cos(r * Math.PI / 3.6);
    var ridge = 900 * Math.sin(lng * 9.0) * Math.cos(lat * 11.0);
    var detail = 320 * Math.sin(lng * 26.0 + lat * 7.0);
    return Math.max(0, base + ridge + detail);
  }

  var terrainProvider = new Cesium.CustomHeightmapTerrainProvider({
    width: SIZE,
    height: SIZE,
    tilingScheme: new Cesium.GeographicTilingScheme(),
    callback: function (x, y, level) {
      var map = new Float32Array(SIZE * SIZE);
      var tiles = Math.pow(2, level);
      for (var j = 0; j < SIZE; j++) {
        for (var i = 0; i < SIZE; i++) {
          var lng = ((x + (i + 0.5) / SIZE) / tiles) * 360 - 180;
          var lat = 90 - ((y + (j + 0.5) / SIZE) / tiles) * 180;
          map[j * SIZE + i] = heightAt(lng, lat);
        }
      }
      return map;
    }
  });

  viewer.terrainProvider = terrainProvider;

  var info = document.getElementById('info');

  function setExaggeration(value) {
    var globe = scene.globe;
    if (typeof globe.terrainExaggeration === 'number') globe.terrainExaggeration = value;
    if (typeof globe.terrainExaggerationRelativeHeight === 'number') {
      globe.terrainExaggerationRelativeHeight = 0;
    }
    if (typeof scene.verticalExaggeration === 'number') scene.verticalExaggeration = value;
    info.textContent = '系数 ' + value.toFixed(1) + ' · 最高约 ' +
      Math.round(3200 * value) + ' 米';
  }

  document.getElementById('ex').addEventListener('input', function () {
    setExaggeration(parseFloat(this.value));
  });

  setExaggeration(1);

  /* 三个贴地标记,用来看地形抬升后点位是否跟随 */
  var PEAKS = [
    { lng: 103.0, lat: 30.6, name: '主峰' },
    { lng: 102.4, lat: 30.1, name: '南脊' },
    { lng: 103.5, lat: 31.0, name: '北脊' }
  ];

  PEAKS.forEach(function (p) {
    viewer.entities.add({
      position: Cesium.Cartesian3.fromDegrees(p.lng, p.lat, 0),
      point: {
        pixelSize: 8,
        color: Cesium.Color.fromCssColorString('#ffb13d'),
        heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
        disableDepthTestDistance: 0
      },
      label: {
        text: p.name,
        font: '500 12px "Microsoft YaHei", sans-serif',
        fillColor: Cesium.Color.fromCssColorString('#ffb13d'),
        pixelOffset: new Cesium.Cartesian2(0, -20),
        heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
        disableDepthTestDistance: 0
      }
    });
  });

  viewer.camera.flyTo({
    destination: Cesium.Cartesian3.fromDegrees(102.35, 29.95, 52000),
    orientation: {
      heading: Cesium.Math.toRadians(28),
      pitch: Cesium.Math.toRadians(-18),
      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 · EXAMPLETime Availability Cesium · EXAMPLETime Convert Cesium · EXAMPLETime Dynamic Point Cloud