Atlaslog
Atlaslog / Examples / Cesium / Lighting Time
Cesium · Runnable Example

Lighting Time

Runnable Cesium example: Lighting Time. 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-lighting-time.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/cesium-lighting-time.html
<!--
  示例:改变平行光方向,看昼夜分界线移动
  要点:scene.light 换成 DirectionalLight,globe.enableLighting 打开后由光照决定明暗
  依赖: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,.92);
       border:1px solid #2a313b;border-radius:6px;padding:9px 12px;font-size:12px;
       color:#a2a9b6;width:250px}
  .hud b{color:#e6e8ec;font-weight:600}
  .hud .row{margin-top:7px}
  input[type=range]{width:100%}
  .hud button{font:inherit;font-size:12px;padding:3px 9px;border-radius:4px;cursor:pointer;
       border:1px solid #343b46;background:#1d232b;color:#cfd6e2;margin-top:8px}
  .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="azVal">60</span>°</div>
  <div class="row"><input id="az" type="range" min="0" max="360" step="5" value="60"></div>
  <div class="row">高度角 <span id="elVal">25</span>°</div>
  <div class="row"><input id="el" type="range" min="5" max="85" step="5" value="25"></div>
  <div class="row"><button id="toggle">关闭光照</button></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('#3b4c60');
  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;

  /* 平行光:direction 表示光线传播方向,用方位角与高度角算成世界坐标向量 */
  var light = new Cesium.DirectionalLight({
    direction: new Cesium.Cartesian3(-1, 0, 0)
  });
  scene.light = light;

  var AZ = Cesium.Math.toRadians(60);
  var EL = Cesium.Math.toRadians(25);

  function updateLight() {
    var cosEl = Math.cos(EL);
    light.direction = new Cesium.Cartesian3(
      -cosEl * Math.sin(AZ),
      -cosEl * Math.cos(AZ),
      -Math.sin(EL)
    );
  }
  updateLight();

  /* 球面上放几块高差明显的方块,光照变化时影子感最直接 */
  var BLOCKS = [
    [116.30, 39.95, '#4d9bff'],
    [116.48, 39.95, '#5fd0a8'],
    [116.30, 39.86, '#ffb13d'],
    [116.48, 39.86, '#e35d6a']
  ];
  BLOCKS.forEach(function (b) {
    viewer.entities.add({
      position: Cesium.Cartesian3.fromDegrees(b[0], b[1], 15000),
      box: {
        dimensions: new Cesium.Cartesian3(26000, 26000, 30000),
        material: Cesium.Color.fromCssColorString(b[2])
      }
    });
  });

  document.getElementById('az').addEventListener('input', function () {
    AZ = Cesium.Math.toRadians(parseFloat(this.value));
    document.getElementById('azVal').textContent = this.value;
    updateLight();
  });

  document.getElementById('el').addEventListener('input', function () {
    EL = Cesium.Math.toRadians(parseFloat(this.value));
    document.getElementById('elVal').textContent = this.value;
    updateLight();
  });

  document.getElementById('toggle').addEventListener('click', function () {
    scene.globe.enableLighting = !scene.globe.enableLighting;
    this.textContent = scene.globe.enableLighting ? '关闭光照' : '开启光照';
  });

  viewer.camera.flyTo({
    destination: Cesium.Cartesian3.fromDegrees(116.39, 39.60, 2600000),
    orientation: {
      heading: 0,
      pitch: Cesium.Math.toRadians(-52),
      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 · EXAMPLELocaltofixedframe Cesium · EXAMPLEManually Controlled Animation Cesium · EXAMPLEMaterial Canvas