代码
import {DynamicSky, Engine, FloodAnalysis, GeoJSONDataSource} from '@baidumap/mapv-three';
import {Group, Matrix4, Mesh, MeshStandardMaterial, PlaneGeometry} 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, 12],
    range: 380 * Math.max(1, 1.08 / aspect),
    pitch: 62,
    heading: 22,
  },
  clock: {
    currentTime: new Date('2026-06-21T02:30:00Z'),
  },
  rendering: {
    sky: null,
    enableAnimationLoop: true,
  },
});
const sky = engine.add(new DynamicSky());
sky.cloudsCoverage = 0.22;

const transform = makeLocalFrame();

const stage = engine.add(new Group());
stage.applyMatrix4(transform);

placeScenery();

const flood = engine.add(new FloodAnalysis());
flood.dataSource = GeoJSONDataSource.fromGeoJSON([
  {
    type: 'Feature',
    geometry: {
      type: 'Polygon',
      coordinates: [
        [toGeo(-102, -59), toGeo(102, -59), toGeo(102, 59), toGeo(-102, 59), toGeo(-102, -59)],
      ],
    },
  },
]);
flood.setHeightRange(0, 35);
flood.floodSpeed = 5;
// 按水位与地表的高差设色,浅水为青色,深水为深蓝色。
flood.colorRamp = {
  0: '#6cefff',
  0.5: '#168bd5',
  1: '#092951',
};
flood.opacity = 0.68;

const pause = operate.querySelector('#pause');
function updateHeight(height) {
  info.textContent = `当前水位 ${height.toFixed(1)} 米`;
}
flood.addEventListener('tick', ({currentHeight}) => updateHeight(currentHeight));
flood.addEventListener('finish', () => {
  updateHeight(flood.currentHeight);
  pause.disabled = true;
});
pause.onclick = () => {
  if (flood.isPaused) {
    flood.resume();
  } else {
    flood.pause();
  }
  pause.textContent = flood.isPaused ? '继续' : '暂停';
};
operate.querySelector('#replay').onclick = () => {
  flood.reset();
  flood.start();
  pause.disabled = false;
  pause.textContent = '暂停';
};
flood.start();

// 东、北、天三个单位轴将演示场景放到地球坐标系中。
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() {
  // 连续的低洼河谷提供高程,蓝色水面从谷底向两侧扩展。
  const geometry = new PlaneGeometry(1120, 980, 148, 124);
  const position = geometry.getAttribute('position');
  for (let i = 0; i < position.count; i++) {
    const x = position.getX(i);
    const y = position.getY(i);
    const river = (x - 19 * Math.sin(y / 48)) / 104;
    const height =
      0.8 + 29 * (1 - Math.exp(-1.2 * river ** 2)) + 4 * (1 - Math.exp(-((y / 110) ** 2)));
    position.setZ(i, height);
  }
  geometry.computeVertexNormals();
  const surface = new Mesh(
    geometry,
    new MeshStandardMaterial({
      color: '#7b926f',
      roughness: 0.94,
    }),
  );
  stage.add(surface);
}

// 小范围示例用局部米坐标布置景物,再转换为经纬度及高度。
function toGeo(x, y, height = 0) {
  return [
    center[0] + x / (111320 * Math.cos((center[1] * Math.PI) / 180)),
    center[1] + y / 111050,
    height,
  ];
}

播放合成河谷中的水位上涨。水面在多边形边界内逐步覆盖低洼处,并按水深着色,浅水为青色,深水为深蓝色。可暂停查看当前水位,再继续上涨。

预览