3D Tiles Property LOD River
Runnable Cesium example: 3D Tiles Property LOD River. 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
<!--
示例:河流的属性驱动样式
说明:河宽与颜色随流量属性变化,流量按正弦涨落,分段标注按视距显隐
核心逻辑:CallbackProperty 驱动 width 与 color → postRender 更新流量 → 视距控制标注
依赖: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}
.cesium-viewer-bottom .cesium-widget-credits,.cesium-viewer-animationContainer,
.cesium-viewer-timelineContainer{display:none!important}
.hud{position:absolute;right:12px;top:12px;z-index:5;background:rgba(23,28,35,.9);
border:1px solid #2a313b;border-radius:6px;padding:6px 10px;font-size:12px;color:#a2a9b6}
</style>
</head>
<body>
<div id="c"></div>
<div class="tip">河流的宽度与颜色在缓慢变化。拉远到 900 米以上,分段流量标注会消失。</div>
<div id="hud" class="hud">近景:显示分段流量</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;
/* 河道中心线,从西向东 */
var path = [
[116.3852, 39.9042], [116.3872, 39.9048], [116.3892, 39.9052],
[116.3912, 39.9050], [116.3932, 39.9056], [116.3952, 39.9062],
[116.3972, 39.9060]
];
/* 每段的流量属性,随时间起伏 */
var flow = [12, 26, 44, 30, 52, 34, 18];
var t = 0;
var positions = [];
path.forEach(function (p) {
positions.push(Cesium.Cartesian3.fromDegrees(p[0], p[1], 0.5));
});
/* 主河道:宽度与颜色都由流量属性驱动 */
viewer.entities.add({
polyline: {
positions: positions,
width: new Cesium.CallbackProperty(function () {
return 4 + flow[3] / 8;
}, false),
material: new Cesium.ColorMaterialProperty(
new Cesium.CallbackProperty(function () {
var v = flow[3] / 60;
return Cesium.Color.fromHsl(0.55, 0.7, 0.35 + v * 0.3, 1);
}, false)
),
clampToGround: false
}
});
/* 分段标注,只在近处显示(属性 LOD) */
var labels = [];
path.forEach(function (p, i) {
labels.push(viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(p[0], p[1], 6),
label: {
text: flow[i] + ' m³/s',
font: '500 11px sans-serif',
fillColor: Cesium.Color.WHITE,
showBackground: true,
backgroundColor: Cesium.Color.fromCssColorString('#1b232e').withAlpha(0.9),
disableDepthTestDistance: Number.POSITIVE_INFINITY
}
}));
});
scene.postRender.addEventListener(function () {
t += 0.016;
for (var i = 0; i < flow.length; i++) {
flow[i] = 20 + Math.sin(t * 0.8 + i * 0.7) * 16 + i * 3;
}
/* 视距超过 900 米就隐藏分段数值,模拟属性随距离降级 */
var d = Cesium.Cartesian3.distance(viewer.camera.positionWC,
Cesium.Cartesian3.fromDegrees(116.3912, 39.9050, 0));
var show = d < 900;
labels.forEach(function (l) { l.label.show = show; });
document.getElementById('hud').textContent = show ? '近景:显示分段流量' : '远景:只保留河流主体';
});
viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(116.3888, 39.8998, 420),
orientation: { heading: Cesium.Math.toRadians(8), 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.