代码
import {DynamicSky, Engine, SlopeAnalysis} from '@baidumap/mapv-three';
import {
  Color,
  Float32BufferAttribute,
  Group,
  Matrix4,
  Mesh,
  MeshStandardMaterial,
  PlaneGeometry,
} from 'three';

const container = document.querySelector('#map');
const center = [116.404, 39.915];
const narrow = container.clientWidth / container.clientHeight < 1;
const engine = new Engine(container, {
  map: {
    projection: 'ecef',
    provider: null,
    center: [...center, 85],
    range: narrow ? 2120 : 1170,
    pitch: narrow ? 72 : 73,
    heading: -15,
  },
  rendering: {
    sky: null,
  },
  clock: {
    currentTime: new Date('2026-06-21T02:30:00Z'),
  },
});
const sky = engine.add(new DynamicSky());
sky.cloudsCoverage = 0.22;

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

const bands = [
  [0, '#2a817e'],
  [10, '#6bb79a'],
  [20, '#efce82'],
  [30, '#dd8a54'],
  [45, '#ad4a3c'],
];
const colorStop = Object.fromEntries(
  bands.map(([degrees, color]) => {
    // SDK 1.8.0 以两倍角度索引色带,45° 起共用最高一档。
    const linear = new Color(color).toArray().map((channel) => Math.round(channel * 255));
    return [degrees * 2, `rgb(${linear.join(', ')})`];
  }),
);
const slope = engine.add(
  new SlopeAnalysis({
    colorStop,
  }),
);
slope.coverageArea = [toGeo(-405, -370), toGeo(290, -345), toGeo(410, 300), toGeo(-310, 360)];

const toggle = document.querySelector('#toggle-slope');
const legend = document.querySelector('#slope-legend');
const gradient = bands.map(([angle, color]) => `${color} ${(angle / 45) * 100}%`).join(', ');
legend.style.setProperty('--ramp', `linear-gradient(to right, ${gradient})`);

function showSlope(visible) {
  if (visible) {
    slope.enabled = true;
    slope.build();
  } else {
    slope.close();
  }
  toggle.setAttribute('aria-pressed', String(visible));
  legend.hidden = !visible;
  engine.requestRender();
}

toggle.onclick = () => showSlope(!slope.enabled);
showSlope(true);

// 低缓的谷地承接两条狭长山脊,东面的短坡更陡。
function elevation(x, y) {
  const westRidge = 169 * Math.exp(-(((x + 170 - 0.2 * y) / 180) ** 2) - ((y - 55) / 410) ** 2);
  const eastRidge = 172 * Math.exp(-(((x - 155 - 0.15 * y) / 116) ** 2) - ((y - 65) / 280) ** 2);
  const foothill = 62 * Math.exp(-(((x + 410) / 270) ** 2) - ((y + 160) / 460) ** 2);
  const farRidge =
    250 * Math.exp(-(((y - 820) / 380) ** 2)) * (0.49 + 0.51 * Math.cos((x - 155) / 565) ** 2);
  const hills = 22 + 11 * Math.sin(x / 208) * Math.sin(y / 190);
  return hills + westRidge + eastRidge + foothill + farRidge;
}

// 谷地密网格减少分析边缘的插值误差,远处的网格逐渐变疏。
function stretch(value, inner, outer) {
  return inner * value + outer * value * value ** 4;
}

function makeTerrain() {
  const geometry = new PlaneGeometry(2, 2, 240, 230);
  const position = geometry.getAttribute('position');
  const colors = [];
  const valley = new Color('#426e5a');
  const upperSlope = new Color('#a19164');
  const aerialHaze = new Color('#8da3a2');
  const color = new Color();
  for (let i = 0; i < position.count; i++) {
    const x = stretch(position.getX(i), 650, 2200);
    const y = stretch(position.getY(i), 640, 2000) + 130;
    const height = elevation(x, y);
    position.setXYZ(i, x, y, height);
    const t = Math.min(1, Math.max(0, (height - 28) / 195));
    color.copy(valley).lerp(upperSlope, t);
    color.multiplyScalar(0.94 + 0.06 * Math.sin((x + y * 0.6) / 55));
    if (y > 420) {
      color.lerp(aerialHaze, Math.min(0.8, (y - 420) / 1100));
    }
    colors.push(color.r, color.g, color.b);
  }
  geometry.setAttribute('color', new Float32BufferAttribute(colors, 3));
  geometry.computeVertexNormals();
  return new Mesh(
    geometry,
    new MeshStandardMaterial({
      vertexColors: true,
      roughness: 1,
    }),
  );
}

function toGeo(east, north) {
  const latitude = (center[1] * Math.PI) / 180;
  return [center[0] + east / (111320 * Math.cos(latitude)), center[1] + north / 111050];
}

function localFrame() {
  const [lng, lat] = center.map((value) => (value * Math.PI) / 180);
  const origin = engine.map.projectArrayCoordinate([...center, 0]);
  return new Matrix4().fromArray([
    -Math.sin(lng),
    Math.cos(lng),
    0,
    0,
    -Math.sin(lat) * Math.cos(lng),
    -Math.sin(lat) * Math.sin(lng),
    Math.cos(lat),
    0,
    Math.cos(lat) * Math.cos(lng),
    Math.cos(lat) * Math.sin(lng),
    Math.sin(lat),
    0,
    ...origin,
    1,
  ]);
}

在连续山谷地形上用 SlopeAnalysis 按坡度角设色。青色谷底坡度低,暖色山脊侧面坡度高;用「坡度着色」对照同一视角下的原始地形。

预览