Material Water
Runnable Cesium example: Material Water. 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
<!--
示例:用自定义着色器材質做水波面
要点:Material 的 fabric.source 写 czm_getMaterial,靠 czm_frameNumber 自动随时间流动
依赖: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:250px}
.hud b{color:#e6e8ec;font-weight:600}
.hud .row{margin-top:6px;color:#7f8896}
.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">表面颜色与波纹由 GLSL 计算</div>
<div class="row">时间取自 czm_frameNumber</div>
<div class="row">池底与堤岸用图元铺出</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;
/* 片元着色器:两组正弦波叠加,按波峰给颜色插值 */
var SOURCE = [
'czm_material czm_getMaterial(czm_materialInput materialInput) {',
' czm_material material = czm_getDefaultMaterial(materialInput);',
' vec2 st = materialInput.st;',
' float t = czm_frameNumber * 0.02;',
' float w1 = sin(st.x * 26.0 + st.y * 8.0 + t);',
' float w2 = sin(st.x * 9.0 - st.y * 34.0 - t * 1.7);',
' float wave = (w1 + w2) * 0.5;',
' vec3 deep = vec3(0.02, 0.17, 0.30);',
' vec3 crest = vec3(0.40, 0.80, 0.96);',
' material.diffuse = mix(deep, crest, smoothstep(-0.85, 0.95, wave));',
' material.alpha = 0.92;',
' return material;',
'}'
].join('\n');
var waterMaterial = new Cesium.Material({
fabric: {
type: 'WaterRipple',
uniforms: {},
source: SOURCE
}
});
/* 水面几何:一块贴地的多边形,带 st 坐标才能用材质着色器 */
var water = new Cesium.Primitive({
geometryInstances: new Cesium.GeometryInstance({
geometry: Cesium.PolygonGeometry.createGeometry(new Cesium.PolygonGeometry({
polygonHierarchy: new Cesium.PolygonHierarchy(
Cesium.Cartesian3.fromDegreesArray([
116.376, 39.900,
116.402, 39.900,
116.402, 39.918,
116.376, 39.918
])
),
height: 4,
vertexFormat: Cesium.EllipsoidSurfaceAppearance.VERTEX_FORMAT
}))
}),
appearance: new Cesium.EllipsoidSurfaceAppearance({
material: waterMaterial,
aboveGround: true
})
});
scene.primitives.add(water);
/* 池底:稍低一点的深色面,衬托水面 */
viewer.entities.add({
polygon: {
hierarchy: Cesium.Cartesian3.fromDegreesArray([
116.374, 39.898, 116.404, 39.898, 116.404, 39.920, 116.374, 39.920
]),
height: 0,
material: Cesium.Color.fromCssColorString('#101820')
}
});
/* 堤岸:四条盒子围边 */
var EDGES = [
[116.376, 39.900, 116.402, 39.900],
[116.402, 39.900, 116.402, 39.918],
[116.402, 39.918, 116.376, 39.918],
[116.376, 39.918, 116.376, 39.900]
];
EDGES.forEach(function (e) {
viewer.entities.add({
polylineVolume: {
positions: Cesium.Cartesian3.fromDegreesArrayHeights([
e[0], e[1], 0, e[2], e[3], 0
]),
shape: [
new Cesium.Cartesian2(-6, 0), new Cesium.Cartesian2(6, 0),
new Cesium.Cartesian2(6, 14), new Cesium.Cartesian2(-6, 14)
],
material: Cesium.Color.fromCssColorString('#2c3a4a')
}
});
});
viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(116.389, 39.889, 1500),
orientation: {
heading: Cesium.Math.toRadians(0),
pitch: Cesium.Math.toRadians(-26),
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.