代码
import {
  Default3DTiles,
  DynamicSky,
  EmptySky,
  Engine,
  GeoJSONDataSource,
  Polyline,
  Tiles3DVolumeAnalysis,
} from '@baidumap/mapv-three';
import {Matrix4} 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, 18],
    range: 350 * Math.max(1, 1.06 / aspect),
    pitch: 48,
    heading: 28,
  },
  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 tileset = new Default3DTiles({
  url: await makeTileset(),
  forceUnlit: true,
});
engine.add(tileset);
tileset.enableClip = true;

const area = [toGeo(-82, -44), toGeo(82, -44), toGeo(82, 44), toGeo(-82, 44)];
const outline = engine.add(
  new Polyline({
    color: '#f9c977',
    width: 3,
    vertexHeights: true,
  }),
);
outline.dataSource = GeoJSONDataSource.fromGeoJSON([
  {
    type: 'Feature',
    geometry: {
      type: 'LineString',
      coordinates: [...area, area[0]].map(([lng, lat]) => [lng, lat, 54]),
    },
  },
]);

const analysis = engine.add(
  new Tiles3DVolumeAnalysis({
    area,
    surfaceHeight: 28,
    interpolateNum: 15,
    showSamplePoint: true,
    showWall: false,
    showBottom: false,
  }),
);
const height = operate.querySelector('#surface-height');
info.textContent = '等待表面瓦片加载…';
function showResult(success) {
  info.textContent = success
    ? `基准 ${analysis.surfaceHeight} m · 挖 ${Math.round(analysis.cutVolume)} m³ · 填 ${Math.round(analysis.fillVolume)} m³ · 未命中 ${Math.round(analysis.missRatio * 100)}%`
    : '采样未完成,请保留分析区在视野内';
}
tileset.addEventListener('loaded', async () => {
  info.textContent = '正在采样表面…';
  try {
    const success = await analysis.build();
    showResult(success);
    height.disabled = !success;
  } catch {
    showResult(false);
  }
});
height.onchange = async () => {
  analysis.surfaceHeight = height.valueAsNumber;
  showResult(await analysis.recompute());
};

// 东、北、天三个单位轴将演示场景放到地球坐标系中。
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 toGeo(x, y, height = 0) {
  return [
    center[0] + x / (111320 * Math.cos((center[1] * Math.PI) / 180)),
    center[1] + y / 111050,
    height,
  ];
}

// 内联 B3DM 包含一块连续斜面,高度从 5.5 米线性升至 50.5 米。
async function makeTileset() {
  const compressed = [
    'H4sIAAAAAAAAE9VTzVLaYBQ9YqXVVq1W7Za5qy6ikz+SkE0LVCsOQkcZNwzjBPjAzEDiJMHWYTLjA3TRR+kbdMZn6EP4GJ0vJIhC',
    'O7joonfD+c537jn3JqGptPsLAO7SwBZG9S7+HVIhXysenpf3K59qh2RKYSYzJLtNZp1qu6JEDYEGvtVlnAiY51m2Q40ww6vbqx2k',
    'AHxPA98WgaPTamVIlu+zgMwhXTHPt12HTJL3RAoFag46Heb5ZNaH1LwOWJk53eCCTF1Vw0ZyfWazL7EkOpMpCpG62ulExvExaZYN',
    'IxT+KJYN4ynyrK49lOe0v6g1XX6o1mW+h9VqMd93vckt+FZRf8vtX7oOc4La9SUjMyvJGicHDh9WFSiIaDrbLyokUN92yKzvSnJW',
    'EIXdnNgQqG99JbPOGV0TcmLjfr5RhvS0jMft8tztp8V8OX8yZaBMva1pP2Xsp2hTftGOAfNsqzd6gpdN75gFVq9nt07cQffCYb7P',
    'v6+m5bOi23O9A6sVuB6ZdXFPMiRJU2VVlZWcaiiiLIh7imRkVU3XDUlSlayS1Tgn5lTNyMmyIWq6KuuaIPHcOCYxFAXyksSEk8Jo',
    'QuZfsHg8z+7bgX0VH60g8OzmIGDRjJ+rp6VaqVqJvCrVk+N8OXpF59G/rvSRTDkUyHbados3KPe7kymGDR7luO3YmodGtEB+izkx',
    'm9yLjfEFF2UymbsUUChVAPwqAEcF4KYwgW9HmFfCR5jzt8CPD49wYYQnNePeSf2k/2Tv5Ay3M3If+8+aZ16fWTPM8EzwzXuMay78',
    '82l4bt//IONfVAoLSGERC3iGNJaQxnMs4QVWsIwVvMQyXmENq1jDOlbxGpvYwCbeYANb2ME2dvAW2/gN7OG2sugGAAA=',
  ].join('');
  const bytes = Uint8Array.from(atob(compressed), (char) => char.charCodeAt(0));
  const stream = new Blob([bytes]).stream().pipeThrough(new DecompressionStream('gzip'));
  const binary = new Uint8Array(await new Response(stream).arrayBuffer());
  const uri = 'data:application/octet-stream;base64,' + btoa(String.fromCharCode(...binary));
  const manifest = {
    asset: {
      version: '1.0',
    },
    geometricError: 240,
    root: {
      transform: transform.toArray(),
      geometricError: 0,
      refine: 'ADD',
      boundingVolume: {
        box: [0, 0, 38, 125, 0, 0, 0, 90, 0, 0, 0, 38],
      },
      content: {
        uri,
      },
    },
  };
  return 'data:application/json;base64,' + btoa(JSON.stringify(manifest));
}

以合成斜面 3D Tiles 为地表,采样估算平整前需挖除和填入的体积。拖动基准高程可复用采样数据重算。

预览