UNPKG

@orca-fe/x-map

Version:
64 lines (63 loc) 2.5 kB
import { Group } from 'three'; import { LineMaterial } from 'three/examples/jsm/lines/LineMaterial'; import { LineGeometry } from 'three/examples/jsm/lines/LineGeometry'; import { Line2 } from 'three/examples/jsm/lines/Line2'; import { lonLat2Mercator } from '../../utils/coord'; import ThreeObject from './ThreeObject'; export default class LineObject extends ThreeObject { constructor(options) { super(options); this.object3D = new Group(); const { lineGeoJson, config = {} } = options; this.lineGeoJson = lineGeoJson; this.lineMaterial = new LineMaterial(Object.assign({ transparent: true, depthWrite: false, depthTest: false }, config)); this.object3D.position.set(0, 0, this.z); } createObject() { var _a; if (!((_a = this.layer) === null || _a === void 0 ? void 0 : _a.map)) return; if (!this.lineGeoJson) return; const { threeCenter } = this.layer.map; let multiLineGeoJson; if (this.lineGeoJson.type === 'MultiLineString') { multiLineGeoJson = this.lineGeoJson; } else { multiLineGeoJson = { coordinates: [this.lineGeoJson.coordinates], type: 'MultiLineString' }; } this.object3D.clear(); this.object3D.renderOrder = Math.round(this.z); multiLineGeoJson.coordinates.forEach((lineJson) => { const points = []; lineJson.forEach((point) => { const [x, y] = lonLat2Mercator(point).map((value, i) => value - threeCenter[i]); points.push(x, y, 0); }); if (points.length === 0) return; const linGeometry = new LineGeometry(); linGeometry.setPositions(points); const line = new Line2(linGeometry, this.lineMaterial); line.computeLineDistances(); line.scale.set(1, 1, 1); this.object3D.add(line); }); } setLine(line) { this.lineGeoJson = line; this.createObject(); } updatePosition() { if (this.layer) { const [width, height] = this.layer.getSize(); this.lineMaterial.resolution.set(width, height); } } updateConfig(config) { var _a; this.lineMaterial.setValues(config); (_a = this.layer) === null || _a === void 0 ? void 0 : _a.updatePosition(); } }