@orca-fe/x-map
Version:
68 lines (67 loc) • 2.74 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const three_1 = require("three");
const LineMaterial_1 = require("three/examples/jsm/lines/LineMaterial");
const LineGeometry_1 = require("three/examples/jsm/lines/LineGeometry");
const Line2_1 = require("three/examples/jsm/lines/Line2");
const coord_1 = require("../../utils/coord");
const ThreeObject_1 = tslib_1.__importDefault(require("./ThreeObject"));
class LineObject extends ThreeObject_1.default {
constructor(options) {
super(options);
this.object3D = new three_1.Group();
const { lineGeoJson, config = {} } = options;
this.lineGeoJson = lineGeoJson;
this.lineMaterial = new LineMaterial_1.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] = (0, coord_1.lonLat2Mercator)(point).map((value, i) => value - threeCenter[i]);
points.push(x, y, 0);
});
if (points.length === 0)
return;
const linGeometry = new LineGeometry_1.LineGeometry();
linGeometry.setPositions(points);
const line = new Line2_1.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();
}
}
exports.default = LineObject;