代码
import {Default3DTiles, DynamicSky, Engine} 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, 5],
range: (aspect > 2 ? 365 : 318) * Math.max(1, 1.02 / aspect),
pitch: 47,
heading: 32,
},
clock: {
currentTime: new Date('2026-06-21T02:30:00Z'),
},
rendering: {
sky: null,
enableAnimationLoop: false,
},
});
const sky = engine.add(new DynamicSky());
sky.cloudsCoverage = 0.22;
const transform = makeLocalFrame();
const tileset = new Default3DTiles({
url: await makeTileset(),
});
engine.add(tileset);
const features = tileset.featureManager;
info.textContent = '等待构件加载…';
const usage = operate.querySelector('#usage');
const style = operate.querySelector('#style');
let colored = true;
function matches(feature) {
return usage.value === 'all' || feature.properties.usage === usage.value;
}
function update() {
features.setFilter(usage.value === 'all' ? null : matches);
features.setStyle(
colored
? (feature) => ({
color: feature.properties.usage === 'office' ? '#f8b272' : '#76e6c2',
})
: null,
);
info.textContent = `显示 ${features.query(matches).length} / ${features.query().length} 个构件`;
style.textContent = colored ? '恢复原色' : '按用途设色';
}
usage.onchange = update;
style.onclick = () => {
colored = !colored;
update();
};
tileset.addEventListener('loaded', update);
// 东、北、天三个单位轴将演示场景放到地球坐标系中。
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]);
}
// 内联 B3DM 包含四栋合成建筑,其中 A-01/A-04 为科研用途,A-02/A-03 为办公用途。
async function makeTileset() {
const compressed = [
'H4sIAAAAAAAAE9VV3XLTRhg90PJfaPltoYWabaFAFXd3tVqtVP7kkEBo4oQkpEBqGMdREs0kdsZyaJmMZ3iJ3vcFOtPL3JXJk/Sy',
'F1z0AbjorGzFm0Tt2JOGKd+MRkffnnO+sys5mbFnl/YAWD4JnEKrHrXvq6QQTPbfezY8ULw7eY/4opnLrZJolvjTJOijjFj6xls3',
'u3UTpGSRlbg8H2pWPYzDcr2yQCxSm5uLKqEJNhZLzVxufnFycC+AP08A1gHg/sRocZWU4zhsEH+VPA/rcVSrEp/wPCVNi8yszM2F',
'9Zj406tk5kUjHA6r840F4nPPlc1Suj4VhT+2Ockz8amV0Efn5hLn9mOqZszhTesf2Xq5JwG3qdgssJX4N75UajOfK6W3U65Uwjiu',
'1c3N6M0lDpXa0nKtGlYbky+WQ+I7jEvdXKk2iO9JizSSNpka6NcvaSmqEn+6j3HHolafR0sWWSr/RPxp3XGl5dFSJ2FrButtxlY5',
'71o+0R8MB+PbDOxtL227n73hZ/fq52YG3IEhE+I/duRM9uyYvNhGWI/Ki63PZnmmPhI2youLUWW8tjK/UA3jWP+4Zspx2F9brNUH',
'y5VGrU78aZpnzHNsJbjrucpzHM4smhfMVcqhLlW2otyWuuUxyqkSnrKVUI60mJ7aHpLaUYvU03lpjzX1jntKJATjSrhUCcWU3jvN',
'C25z6Urpea6kVOqW4zLGheKecKSjHLGLibgjmMOpJ2xOKfeS85Cuq2zXk4xyR3AlLJqXSkrl2IwLrpSSzi4m2vY6aF7a1HVcZkvb',
'Vq6bHJFrUyGpKyhVwmWO6iVQwowXwvYXVY+Wokb0vP1YbjTq0cxKI0wCjo1ODE0OjRYTr+Lo+EgwnPwpeZb8Vxm6Q3zetEhUnY0q',
'WmB3PlfiU73zHdkJ047t2M4x7fiO7aRpZzdL+lyrtdn2OeoT1mdQskhcCavtbrpOSxsLmpRL6pcjQGGoCGBsHfgrAHJBBx9cb2Fd',
'aV+X7q8ZnDVDu7aFk2rXtnhmaccyfMy5W/2z8nTrk5UhyzPFCICXBeC3oIPR7utK+0mtA7n1DifFmpNb38xJtSbf9De1ZgZTa2Yw',
'/bPydOuTlSHLM8U/F4DrmvOqg6+0+0m96mDdfx10OCnWnNfBZk6qNfmmv6k1M5haM4Ppn5WnW5+sDFmeKf61fd7LhQ7+I2hhXWlf',
'l+6/CTqcFGvOm2AzJ9WafNPf1JoZTK2ZwfTPytOtT1aGLM8Uv7yFjeoK/94b7tr3fzrjXc7+Nma8y9nfxox3OfvbnrEbpTPt5oXb',
'u3vd3uVrL/ZgL97DHryP/diH/TiAfTiIwziEwziCQ/gAx3AUx/AhjuIjnMBxnMBJHMcpnMFpnMHHOI1PcA5ncQ6f4iw+wwWcxwV8',
'jvPIgeAiCL7ARXyJy7iEy/gKl3AF13AV1/A1rsJCHn3I4xv0gYKDgcMGg4CEAwkXDhR8ePDxLTxcx03cwE3cwg3cRgEBCuhHgDsY',
'xAAGcRcDuIf7GMJ9fIchDKOIERQxihGMYRwPMI4JPMAkpvAQU/geD/EIT/AYTzCNx/gBT1HCUzxDCX8DEQxpN3ATAAA=',
].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));
}
按 B3DM 构件属性筛选科研或办公建筑,也可为两种用途换色。下拉菜单控制显隐,设色按钮控制材质。 建筑为带真实 3D Tiles 格式和地理参考的演示数据。
预览