代码
import {Color} from 'three';
import {Engine, GeoJSONDataSource, SimplePoint} from '@baidumap/mapv-three';
const container = document.getElementById('map');
const aspect = container.clientWidth / container.clientHeight;
const center = [0, 0];
const bottom = 0;
const side = 420;
const steps = 6;
const engine = new Engine(container, {
map: {
projection: 'EPSG:3857',
center: [...center, bottom + side / 2],
range: 1_560 / Math.min(1, aspect * 1.27),
pitch: 53,
heading: 35,
provider: null,
},
rendering: {sky: null},
});
engine.rendering.scene.background = new Color('#172b43');
const [centerX, centerY] = engine.map.projectArrayCoordinate(center);
const startX = centerX - side / 2;
const startY = centerY - side / 2;
const spacing = side / steps;
const features = [];
const frontX = engine.camera.position.x < centerX ? 0 : steps;
const frontY = engine.camera.position.y < centerY ? 0 : steps;
const faceColors = {
top: '#6de9e1',
x: '#8dbbff',
y: '#c0a9f4',
back: '#5d7490',
};
// 生成立方体的棱线与朝向镜头的三面。交汇处只保留一个点。
const isBoundary = (index) => index === 0 || index === steps;
for (let x = 0; x <= steps; x++) {
for (let y = 0; y <= steps; y++) {
for (let z = 0; z <= steps; z++) {
const boundaries = Number(isBoundary(x)) + Number(isBoundary(y)) + Number(isBoundary(z));
if (boundaries === 0) continue;
const face = z === steps ? 'top' : x === frontX ? 'x' : y === frontY ? 'y' : 'back';
if (face === 'back' && boundaries === 1) continue;
const coordinates = engine.map.unprojectArrayCoordinate([
startX + x * spacing,
startY + y * spacing,
bottom + z * spacing,
]);
features.push({
type: 'Feature',
geometry: {type: 'Point', coordinates},
properties: {
grid: [x + 1, y + 1, z + 1].join('、'),
size: boundaries === 3 ? 13 : boundaries === 2 ? 9 : 7,
color: boundaries === 3 && face !== 'back' ? '#ffd48a' : faceColors[face],
},
});
}
}
}
const source = GeoJSONDataSource.fromGeoJSON(features);
source.defineAttribute('grid').defineAttribute('color').defineAttribute('size');
const points = engine.add(
new SimplePoint({
vertexColors: true,
vertexSizes: true,
}),
);
points.dataSource = source;
const reading = document.getElementById('reading');
points.addEventListener('click', ({entity}) => {
if (entity?.pairs.grid) {
reading.textContent = '格点(' + entity.pairs.grid + ')';
}
});
在场景坐标中生成三维点阵立方体,并用 SimplePoint 的逐点颜色和尺寸突出棱线与顶点。点击任意格点查看它在点阵中的位置。
预览