代码
import {
BaiduMapConfig,
BaiduVectorTileProvider,
Engine,
GeoJSONDataSource,
Polygon,
} from '@baidumap/mapv-three';
const container = document.getElementById('map');
const center = [116.71913, 40.179925];
const range = (880 * 1.42) / Math.min(1.42, container.clientWidth / container.clientHeight);
BaiduMapConfig.ak = '您的密钥';
const provider = new BaiduVectorTileProvider({
displayOptions: {
building: false,
poi: false,
},
});
const engine = new Engine(container, {
map: {
projection: 'EPSG:3857',
center,
range,
pitch: 0,
heading: 0,
provider,
},
rendering: {
sky: null,
enableAnimationLoop: false,
},
});
const bounds = [
[116.71623, 40.178105],
[116.72203, 40.181745],
];
const [west, south] = bounds[0];
const [east, north] = bounds[1];
const [minX, minY] = engine.map.projectArrayCoordinate(bounds[0]);
const [maxX, maxY] = engine.map.projectArrayCoordinate(bounds[1]);
const textureWidth = 1240;
// 纹理尺寸匹配投影后的场地宽高,使一张纹理恰好覆盖整个多边形。
const mapScale = (maxX - minX) / textureWidth;
const textureHeight = Math.round((maxY - minY) / mapScale);
const field = GeoJSONDataSource.fromGeoJSON([
{
type: 'Feature',
geometry: {
type: 'Polygon',
coordinates: [
[
[west, south],
[east, south],
[east, north],
[west, north],
[west, south],
],
],
},
properties: {},
},
]);
const fields = [
{x: 145, y: 175, width: 440, height: 350},
{x: 660, y: 175, width: 440, height: 350},
];
const specifications = [
{columns: 8, rows: 6, gap: 8},
{columns: 13, rows: 9, gap: 6},
];
function paintArray({columns, rows, gap}) {
const canvas = document.createElement('canvas');
canvas.width = textureWidth;
canvas.height = textureHeight;
const context = canvas.getContext('2d');
context.scale(1, textureHeight / 690);
context.fillStyle = '#666e53';
context.fillRect(98, 134, 1044, 429);
context.fillStyle = '#b9b28c';
context.fillRect(588, 134, 64, 429);
context.fillStyle = '#817e61';
context.fillRect(603, 134, 34, 429);
for (const field of fields) {
const {x, y, width, height} = field;
context.fillStyle = '#9b9e79';
context.fillRect(x - 13, y - 13, width + 26, height + 26);
context.fillStyle = '#777a5d';
context.fillRect(x, y, width, height);
// 各规格都使用同一块场地,独立计算格网和组件间隙。
const columnWidth = width / columns;
const rowHeight = height / rows;
for (let row = 0; row < rows; row++) {
for (let column = 0; column < columns; column++) {
const left = x + column * columnWidth + gap / 2;
const top = y + row * rowHeight + gap / 2;
const panelWidth = columnWidth - gap;
const panelHeight = rowHeight - gap;
context.fillStyle = '#354f58';
context.fillRect(left + 2, top + 5, panelWidth, panelHeight);
context.fillStyle = '#8bb4c6';
context.fillRect(left, top, panelWidth, panelHeight);
context.fillStyle = '#244d6b';
context.fillRect(left + 2, top + 2, panelWidth - 4, panelHeight - 4);
context.strokeStyle = '#5f89a4';
context.beginPath();
context.moveTo(left + panelWidth / 2, top + 2);
context.lineTo(left + panelWidth / 2, top + panelHeight - 2);
context.stroke();
}
}
}
return canvas.toDataURL();
}
const arrays = specifications.map((specification) => {
const surface = engine.add(
new Polygon({
// 在真实几何上抬高 5 米,保留深度测试并避开与矢量底图的共面冲突。
zOffset: 5,
transparent: true,
mapScale,
}),
);
// 显式开启 UV,再绑定场地数据,使贴图沿多边形坐标排布。
surface.geometry.useUV = true;
surface.dataSource = field;
surface.mapSrc = paintArray(specification);
return surface;
});
const buttons = [document.getElementById('standard'), document.getElementById('dense')];
const modules = document.getElementById('modules');
function selectArray(selected) {
arrays.forEach((array, index) => {
const visible = index === selected;
array.visible = visible;
buttons[index].setAttribute('aria-pressed', String(visible));
});
const {columns, rows} = specifications[selected];
modules.textContent = columns * rows * fields.length + ' 块组件';
engine.requestRender();
}
buttons.forEach((button, index) => {
button.addEventListener('click', () => selectArray(index));
});
selectArray(0);
用 Polygon.mapSrc 将 Canvas 光伏纹理映射到北京顺义的示意场地。多边形略高于百度矢量底图,避免斜视时的深度冲突。可切换标准阵列与高密阵列。
预览