Cesium 实战 11 - 调整色调、对比度等参数,加载渲染暗黑底图

2026年7月17日更新:搞了一个 Cesium 中文 API 镜像,这是相关介绍(Cesium-1.143 中文版 API),欢迎使用。

暗黑底图一般用于三维地图大屏展示,也是现在比较主流的数据表现形式。

本文通过调整图层色调、对比度等参数实现暗黑底图。

本文包括渲染暗黑底图核心代码、完整代码以及在线示例。


渲染暗黑底图核心代码

这里放上核心代码:

javascript 复制代码
/**
 * @todo 开启暗黑底图
 * @param {Object} options - 参数
 * @param {number} options.hue - 色调
 * @param {number} options.contrast - 对比度
 */
function openDarken(options) {
    options = {
        // 色调
        hue: 3,
        // 对比度
        contrast: -1.2,
        ...options
    };

    const _beautifyLayer = imageLayer;

    origin = {
        hue: _beautifyLayer.hue,
        contrast: _beautifyLayer.contrast,
    };

    if (_beautifyLayer) {
        _beautifyLayer.hue = options.hue;
        _beautifyLayer.contrast = options.contrast;
    }
}

/**
 * @todo 关闭暗黑底图
 */
function closeDarken() {
    const _beautifyLayer = imageLayer;

    if (_beautifyLayer) {
        _beautifyLayer.hue = origin.hue;
        _beautifyLayer.contrast = origin.contrast;
    }
}

完整代码:

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <!-- Use correct character set. -->
    <meta charset="utf-8"/>
    <!-- Tell IE to use the latest, best version. -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <!-- Make the application on mobile take up the full browser screen and disable user scaling. -->
    <meta
            name="viewport"
            content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"
    />
    <title>Cesium darken layer</title>
    <script src="http://openlayers.vip/examples/csdn/Cesium.js"></script>
    <script src="./cesium_init.js"></script>
    <script src="http://www.openlayers.vip/examples/resources/jquery-3.5.1.min.js"></script>
    <style>
        @import url(./Widgets/widgets.css);

        html,
        body,
        #cesiumContainer {
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
            overflow: hidden;
        }
    </style>

    <script>
        var _hmt = _hmt || [];
        (function () {
            var hm = document.createElement("script");
            hm.src = "https://hm.baidu.com/hm.js?f80a36f14f8a73bb0f82e0fdbcee3058";
            var s = document.getElementsByTagName("script")[0];
            s.parentNode.insertBefore(hm, s);
        })();
    </script>
</head>
<body>
<button id="openBeautify" onClick="openDarken()">暗黑地球</button>
<button id="closeBeautifyFunc" onClick="closeDarken()">还原</button>

<div id="cesiumContainer"></div>
<script>

    // 创建三维球
    const viewer = init();

    // 这个 tk 只能在本域名下使用
    var token = '2b7cbf61123cbe4e9ec6267a87e7442f';
    // 服务域名
    var tdtUrl = 'https://t{s}.tianditu.gov.cn/';

    var subdomains = ['0', '1', '2', '3', '4', '5', '6', '7'];

    // // 叠加矢量图
    var vecMap = viewer.imageryLayers.addImageryProvider(new window.Cesium.UrlTemplateImageryProvider({
        url: tdtUrl + 'DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=' + token,
        subdomains: subdomains,
        tilingScheme: new window.Cesium.WebMercatorTilingScheme(),
        maximumLevel: 18
    }));

    // 美化影像图层
    const imageLayer = vecMap;
    // 原始参数
    let origin;

    /**
     * @todo 开启暗黑底图
     * @param {Object} options - 参数
     * @param {number} options.hue - 色调
     * @param {number} options.contrast - 对比度
     */
    function openDarken(options) {
        options = {
            // 色调
            hue: 3,
            // 对比度
            contrast: -1.2,
            ...options
        };

        const _beautifyLayer = imageLayer;

        origin = {
            hue: _beautifyLayer.hue,
            contrast: _beautifyLayer.contrast,
        };

        if (_beautifyLayer) {
            _beautifyLayer.hue = options.hue;
            _beautifyLayer.contrast = options.contrast;
        }
    }

    /**
     * @todo 关闭暗黑底图
     */
    function closeDarken() {
        const _beautifyLayer = imageLayer;

        if (_beautifyLayer) {
            _beautifyLayer.hue = origin.hue;
            _beautifyLayer.contrast = origin.contrast;
        }
    }

</script>
</body>
</html>

在线示例

Cesium 在线示例:调整色调、对比度等参数,加载渲染暗黑底图

相关推荐
Li.map6 天前
CesiumJS 地图主题系统:用着色器打造实时滤镜
javascript·arcgis·cesium·着色器
全栈项目管理程序猿7 天前
Cesium-1.143 中文版 API
javascript·cesium
用户831348593069810 天前
Cesium自定义可调节星空夜景+星星闪烁
vue.js·cesium
用户831348593069822 天前
Cesium实现黄昏效果:按钮一键控制打开/关闭黄昏效果,滑块拖动实时控制黄昏浓烈度
vue.js·cesium
青山Coding23 天前
Cesium应用(八):物体运动的实现思路
前端·cesium
用户83134859306981 个月前
Cesium实现雾气效果:按钮一键控制打开/关闭雾气效果,滑块拖动实时控制雾气浓度
vue.js·cesium
用户83134859306981 个月前
Cesium实现实时联动鹰眼缩略图
vue.js·cesium
整点可乐1 个月前
cesium实现全景图加载
javascript·cesium
用户83134859306981 个月前
Vue3+Cesium实现3DTiles模型实时调节(离地面高度/xyz轴旋转/模型经纬度偏移)
vue.js·cesium