UNPKG

@orca-fe/x-map

Version:
66 lines (65 loc) 2.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); /* eslint-disable no-param-reassign */ const three_1 = require("three"); const ThreeObject_1 = tslib_1.__importDefault(require("./ThreeObject")); const coord_1 = require("../../utils/coord"); class TextureObject extends ThreeObject_1.default { constructor(options) { super(); this.geometry = new three_1.PlaneBufferGeometry(); const { src, bounds, z = 0, geometry, materialParameters } = options; this.bounds = bounds; if (geometry) this.geometry = geometry; this.material = new three_1.MeshBasicMaterial(Object.assign({ side: three_1.DoubleSide, blending: three_1.AdditiveBlending, depthWrite: false, depthTest: false }, materialParameters)); this.object3D = new three_1.Mesh(this.geometry, this.material); this.object3D.renderOrder = Math.round(z); this.object3D.scale.set(1, 1, 1); this.object3D.position.set(0, 0, this.z); if (src) { if (typeof src === 'string') { this.loadTexture(src); } else { this.material.map = src; this.material.needsUpdate = true; } } } updatePosition() { var _a; if ((_a = this.layer) === null || _a === void 0 ? void 0 : _a.map) { const { threeCenter } = this.layer.map; if (this.bounds) { const [minPoint, maxPoint] = this.bounds; const minMercator = (0, coord_1.lonLat2Mercator)(minPoint); const maxMercator = (0, coord_1.lonLat2Mercator)(maxPoint); const center = [ 0.5 * (minMercator[0] + maxMercator[0]) - threeCenter[0], 0.5 * (minMercator[1] + maxMercator[1]) - threeCenter[1], ]; this.object3D.position.set(center[0], center[1], this.z); this.object3D.scale.set(Math.abs(maxMercator[0] - minMercator[0]), Math.abs(maxMercator[1] - minMercator[1]), 1); } } } createObject() { } loadTexture(src) { const loader = new three_1.TextureLoader(); loader.load(src, (texture) => { texture.encoding = three_1.sRGBEncoding; texture.repeat.set(10, 10); texture.wrapT = three_1.RepeatWrapping; texture.wrapS = three_1.RepeatWrapping; this.material.map = texture; this.material.needsUpdate = true; // trigger update // this.layer?.updatePositionDebounce?.(); }, undefined, function (error) { console.error(error); }); } } exports.default = TextureObject;