@orca-fe/x-map
Version:
58 lines (57 loc) • 2.52 kB
JavaScript
import * as THREE from 'three';
import ThreeObject from './ThreeObject';
import { lonLat2Mercator } from '../../utils/coord';
export default class BuildingObject extends ThreeObject {
constructor(options) {
super(options);
this.object3D = new THREE.Group();
this.createObject = () => {
const { fill, buildings, layer, z = 0 } = this;
if (layer === null || layer === void 0 ? void 0 : layer.map) {
const { threeCenter } = layer.map;
this.object3D.clear();
this.object3D.renderOrder = Math.round(z);
const color = new THREE.Color(fill.color);
const polygonMaterial = new THREE.MeshLambertMaterial({
color,
opacity: fill.opacity,
depthWrite: false,
depthTest: false,
transparent: true,
});
// let geometry = new THREE.BufferGeometry();
const shapes = {};
for (let i = 0; i < buildings.length; i++) {
const shape = new THREE.Shape();
const path = buildings[i].path
.map(lonLat2Mercator)
.map(([x, y]) => [x - threeCenter[0], y - threeCenter[1]]);
path.forEach(([x, y], j) => {
if (j === 0) {
shape.moveTo(x, y);
}
shape.lineTo(x, y);
});
const { height } = buildings[i];
if (!shapes[height])
shapes[height] = [];
shapes[height].push(shape);
}
Object.entries(shapes).forEach(([height, shapes]) => {
const polygonGeometry = new THREE.ExtrudeGeometry(shapes, {
steps: 1,
bevelEnabled: false,
depth: Math.max(Number(height) * 6, 0.001),
});
const polygonMesh = new THREE.Mesh(polygonGeometry, polygonMaterial);
this.object3D.add(polygonMesh);
});
}
};
const { fill, buildings } = options;
this.buildings = buildings;
this.fill = fill;
this.object3D.position.set(0, 0, this.z);
}
updatePosition() { }
}