Postprocess Color
Runnable Cesium example: Postprocess Color. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.
single HTML file · CDN dependencies onlyinterface may contain Chinese labels
Source
<!--
示例:自己写一个调色后处理阶段
要点:PostProcessStage 用 colorTexture 采样上一帧,按 uMode 做灰度、反相或棕褐
依赖: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%}
select{font:inherit;font-size:12px;background:#1d232b;color:#cfd6e2;
border:1px solid #343b46;border-radius:4px;padding:2px 4px;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="aVal">0.60</span></div>
<div class="row"><input id="amt" type="range" min="0" max="1" step="0.05" value="0.6"></div>
<div class="row">模式</div>
<div class="row">
<select id="mode">
<option value="0">灰度</option>
<option value="1">反相</option>
<option value="2">棕褐</option>
</select>
</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('#1f2f40');
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;
/* 三色方块 + 地面条带,方便判断调色是否生效 */
var COLORS = ['#4d9bff', '#ffb13d', '#5fd0a8', '#e35d6a'];
COLORS.forEach(function (c, i) {
viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(116.374 + i * 0.010, 39.904, 130),
box: {
dimensions: new Cesium.Cartesian3(160, 160, 260),
material: Cesium.Color.fromCssColorString(c)
}
});
});
viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(116.389, 39.886, 0),
box: {
dimensions: new Cesium.Cartesian3(4600, 120, 10),
material: Cesium.Color.fromCssColorString('#8fa3b8')
}
});
var SHADER = [
'uniform sampler2D colorTexture;',
'uniform float uAmount;',
'uniform float uMode;',
'in vec2 v_textureCoordinates;',
'',
'void main() {',
' vec4 color = texture(colorTexture, v_textureCoordinates);',
' float gray = dot(color.rgb, vec3(0.299, 0.587, 0.114));',
' vec3 target = uMode < 0.5',
' ? vec3(gray)',
' : (uMode < 1.5',
' ? vec3(1.0) - color.rgb',
' : vec3(',
' dot(color.rgb, vec3(0.393, 0.769, 0.189)),',
' dot(color.rgb, vec3(0.349, 0.686, 0.168)),',
' dot(color.rgb, vec3(0.272, 0.534, 0.131))',
' ));',
' out_FragColor = vec4(mix(color.rgb, target, uAmount), color.a);',
'}'
].join('\n');
var stage = scene.postProcessStages.add(new Cesium.PostProcessStage({
fragmentShader: SHADER,
uniforms: {
uAmount: 0.6,
uMode: 0
}
}));
document.getElementById('amt').addEventListener('input', function () {
stage.uniforms.uAmount = parseFloat(this.value);
document.getElementById('aVal').textContent = parseFloat(this.value).toFixed(2);
});
document.getElementById('mode').addEventListener('change', function () {
stage.uniforms.uMode = parseFloat(this.value);
});
document.getElementById('toggle').addEventListener('click', function () {
stage.enabled = !stage.enabled;
this.textContent = stage.enabled ? '关闭后处理' : '开启后处理';
});
viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(116.386, 39.888, 1900),
orientation: {
heading: 0,
pitch: Cesium.Math.toRadians(-22),
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.