代码
import {DynamicSky, Engine, SightAnalysis} 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, 17],
    range: 325 * Math.max(1, 1.02 / aspect),
    pitch: 55,
    heading: 20,
  },
  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 sight = engine.add(new SightAnalysis());
sight.visibleColor = '#6cf7b3';
sight.invisibleColor = '#ff8773';
sight.distanceBias = 4;

const targets = [
  [86, 38],
  [80, -9],
  [43, -56],
];
for (const [x, y] of targets) {
  sight.addTargetPoint(toGeo(x, y, 5));
  addBlock(x, y, 1, 3, 3, 2, '#ffd58c');
}
const observer = new Mesh(
  new BoxGeometry(4, 4, 4),
  new MeshBasicMaterial({
    color: '#ffe4a6',
  }),
);
stage.add(observer);

const height = operate.querySelector('#observer-height');
function analyze() {
  observer.position.set(-89, -19, height.valueAsNumber);
  sight.viewPoint = toGeo(-89, -19, height.valueAsNumber);
  engine.scene.updateMatrixWorld(true);
  sight.build();
  info.textContent = `观察高度 ${height.value} 米 · ${sight.occlusionPoints.length} 处遮挡`;
}
height.oninput = 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() {
  // 用受光屋顶和遮挡侧面区分校园建筑。
  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,
  ];
}

对比观察点到三个目标的通视情况。绿色线段代表可见,橙红色线段代表遮挡;抬高观察点可对比遮挡数量。

预览