代码
import {DynamicSky, Engine, ViewShedAnalysis} from '@baidumap/mapv-three';
import {BoxGeometry, Group, Matrix4, Mesh, MeshBasicMaterial, MeshStandardMaterial} from 'three';
const container = document.querySelector('#map');
const aspect = container.clientWidth / container.clientHeight;
const info = document.querySelector('.info');
const operate = document.querySelector('.operate');
const center = [116.404, 39.915];
const engine = new Engine(container, {
map: {
projection: 'ecef',
provider: null,
center: [...center, 20],
range: 320 * Math.max(1, 1.08 / aspect),
pitch: 54,
heading: 23,
},
clock: {
currentTime: new Date('2026-06-21T02:30:00Z'),
},
rendering: {
sky: null,
enableAnimationLoop: false,
},
});
const sky = engine.add(new DynamicSky());
sky.cloudsCoverage = 0.22;
const transform = makeLocalFrame();
const stage = engine.add(new Group());
stage.applyMatrix4(transform);
placeScenery();
const analysis = engine.add(new ViewShedAnalysis());
analysis.visibleColor = '#51efaf';
analysis.invisibleColor = '#fa7267';
analysis.fov = 65;
const height = operate.querySelector('#view-height');
const enabled = operate.querySelector('#view-enabled');
function analyze() {
const visible = enabled.getAttribute('aria-pressed') === 'true';
height.disabled = !visible;
if (visible) {
analysis.viewPoint = toGeo(-78, -30, height.valueAsNumber);
analysis.targetPoint = toGeo(65, 7, 1);
analysis.build({
opacity: 0.68,
});
} else {
analysis.close();
}
info.textContent = visible ? `观察高度 ${height.value} 米 · 绿可见 / 红遮挡` : '';
}
height.onchange = analyze;
enabled.onclick = () => {
enabled.setAttribute('aria-pressed', String(enabled.getAttribute('aria-pressed') !== 'true'));
analyze();
};
analyze();
// 东、北、天三个单位轴将演示场景放到地球坐标系中。
function makeLocalFrame() {
const [longitude, latitude] = center.map((degrees) => (degrees * Math.PI) / 180);
const origin = engine.map.projectArrayCoordinate([...center, 0]);
const east = [-Math.sin(longitude), Math.cos(longitude), 0];
const north = [
-Math.sin(latitude) * Math.cos(longitude),
-Math.sin(latitude) * Math.sin(longitude),
Math.cos(latitude),
];
const up = [
Math.cos(latitude) * Math.cos(longitude),
Math.cos(latitude) * Math.sin(longitude),
Math.sin(latitude),
];
return new Matrix4().fromArray([...east, 0, ...north, 0, ...up, 0, ...origin, 1]);
}
function placeScenery() {
// 平面只作为视域着色的接收面。
addBlock(0, 0, -0.6, 210, 150, 1, '#c8d5cd');
// 用受光屋顶和遮挡侧面区分校园建筑。
addBuilding(-11, -4, 32, 28, 38, '#b9b49f');
addBuilding(29, 27, 27, 38, 20, '#96b0b3');
addBuilding(61, -28, 29, 33, 52, '#9bb1c0');
addBuilding(-53, 47, 28, 24, 14, '#a9ad9f');
}
function addBuilding(x, y, width, depth, height, color) {
const mesh = new Mesh(
new BoxGeometry(width, depth, height),
new MeshStandardMaterial({
color,
roughness: 0.86,
}),
);
mesh.position.set(x, y, height / 2);
stage.add(mesh);
}
function addBlock(x, y, z, width, depth, height, color, angle = 0) {
const block = new Mesh(
new BoxGeometry(width, depth, height),
new MeshBasicMaterial({
color,
}),
);
block.position.set(x, y, z);
block.rotation.z = angle;
stage.add(block);
}
// 小范围示例用局部米坐标布置景物,再转换为经纬度及高度。
function toGeo(x, y, height = 0) {
return [
center[0] + x / (111320 * Math.cos((center[1] * Math.PI) / 180)),
center[1] + y / 111050,
height,
];
}
在三维园区内开展视域分析。视锥内绿色区域可见,红色区域被遮挡;改变观察高度可以看到建筑阴影范围随之变化。
预览