Webgl Shaded Relief
Runnable OpenLayers example: Webgl Shaded Relief. 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
<!--
示例:OpenLayers WebGL 山体阴影
要点:Terrarium 高程经 WebGL 表达式计算坡度与坡向,实时生成山体阴影
依赖:OpenLayers 10.3.1 · 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>WebGL 山体阴影</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@10.3.1/ol.css">
<style>
html,body{height:100%;margin:0}
body{display:flex;flex-direction:column;overflow:hidden;
font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif}
.bar{flex:0 0 auto;padding:8px 12px;background:#f6f7f9;border-bottom:1px solid #e3e6ea;
display:flex;gap:10px;align-items:center;flex-wrap:wrap;font-size:12px;color:#58606e}
.bar label{display:flex;gap:5px;align-items:center;cursor:pointer}
.bar select,.bar input[type=range]{font-size:12px}
.bar .val{font-family:ui-monospace,Consolas,monospace;color:#1f5fd0;min-width:44px}
.bar .sep{width:1px;height:16px;background:#d8dce2}
#map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
<label>垂直夸张 <input type="range" id="vert" min="0.5" max="3" step="0.1" value="1.2"><span class="val" id="vertOut">1.2</span></label>
<label>太阳高度 <input type="range" id="sunEl" min="5" max="85" value="45"><span class="val" id="sunElOut">45°</span></label>
<label>太阳方位 <input type="range" id="sunAz" min="0" max="360" value="315"><span class="val" id="sunAzOut">315°</span></label>
</div>
<div id="map"></div>
<script src="https://cdn.jsdelivr.net/npm/ol@10.3.1/dist/ol.js"></script>
<script>
(function () {
'use strict';
if (typeof ol === 'undefined') {
document.getElementById('map').textContent = 'OpenLayers 加载失败,请检查能否访问 cdn.jsdelivr.net';
return;
}
var demGrid = ol.tilegrid.createXYZ({tileSize: 256, maxZoom: 12});
var size = 256;
function elevationAt(lon, lat) {
var hills =
520 * Math.exp(-(Math.pow(lon - 115.55, 2) + Math.pow(lat - 39.95, 2)) / 0.09) +
360 * Math.exp(-(Math.pow(lon - 116.05, 2) + Math.pow(lat - 40.25, 2)) / 0.05) +
300 * Math.exp(-(Math.pow(lon - 115.85, 2) + Math.pow(lat - 39.35, 2)) / 0.06);
var folds = 85 * Math.sin(lon * 32 + lat * 6) * Math.cos(lat * 29) +
38 * Math.sin(lon * 78 + lat * 21) * Math.cos(lat * 66);
var valley = -240 * Math.exp(-Math.pow((lon - 116.2) / 0.07, 2)) *
Math.exp(-Math.pow((lat - 39.9) / 0.35, 2));
return 180 + hills + folds + valley;
}
var terrain = new ol.source.DataTile({
tileGrid: demGrid,
projection: 'EPSG:3857',
transition: 0,
wrapX: true,
loader: function (z, x, y) {
var extent = demGrid.getTileCoordExtent([z, x, y]);
var resolution = demGrid.getResolution(z);
var data = new Uint8Array(size * size * 3);
var offset = 0;
for (var row = 0; row < size; ++row) {
var mapY = extent[3] - (row + 0.5) * resolution;
for (var col = 0; col < size; ++col) {
var mapX = extent[0] + (col + 0.5) * resolution;
var lonlat = ol.proj.toLonLat([mapX, mapY]);
var height = elevationAt(lonlat[0], lonlat[1]);
var value = Math.max(0, Math.round((height + 32768) * 256));
data[offset] = Math.floor(value / 65536) % 256;
data[offset + 1] = Math.floor(value / 256) % 256;
data[offset + 2] = value % 256;
offset += 3;
}
}
return data;
}
});
function elevation(xOffset, yOffset) {
return [
'+',
['*', 255 * 256, ['band', 1, xOffset, yOffset]],
['*', 255, ['band', 2, xOffset, yOffset]],
['*', 255 / 256, ['band', 3, xOffset, yOffset]],
-32768
];
}
var dp = ['*', 2, ['resolution']];
var z0x = ['*', ['var', 'vert'], elevation(-1, 0)];
var z1x = ['*', ['var', 'vert'], elevation(1, 0)];
var dzdx = ['/', ['-', z1x, z0x], dp];
var z0y = ['*', ['var', 'vert'], elevation(0, -1)];
var z1y = ['*', ['var', 'vert'], elevation(0, 1)];
var dzdy = ['/', ['-', z1y, z0y], dp];
var slope = ['atan', ['sqrt', ['+', ['^', dzdx, 2], ['^', dzdy, 2]]]];
var aspect = ['clamp', ['atan', ['-', 0, dzdx], dzdy], -Math.PI, Math.PI];
var sunEl = ['*', Math.PI / 180, ['var', 'sunEl']];
var sunAz = ['*', Math.PI / 180, ['var', 'sunAz']];
var cosIncidence = [
'+',
['*', ['sin', sunEl], ['cos', slope]],
['*', ['cos', sunEl], ['sin', slope], ['cos', ['-', sunAz, aspect]]]
];
var variables = {vert: 1.2, sunEl: 45, sunAz: 315};
var relief = new ol.layer.WebGLTile({
opacity: 0.55,
source: terrain,
style: {
variables: variables,
color: ['color', ['*', 255, cosIncidence]]
}
});
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({source: new ol.source.XYZ({url: 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}', attributions: 'Tiles © Esri'}), opacity: 0.55}),
relief
],
view: new ol.View({
center: ol.proj.fromLonLat([116.05, 40.02]),
zoom: 9
})
});
['vert', 'sunEl', 'sunAz'].forEach(function (id) {
var control = document.getElementById(id);
var output = document.getElementById(id + 'Out');
function update() {
var value = Number(control.value);
variables[id] = value;
output.textContent = id === 'vert' ? String(value) : value + '°';
relief.updateStyleVariables(variables);
}
update();
control.addEventListener('input', update);
});
})();
</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.