@orca-fe/x-map
Version:
62 lines (61 loc) • 2.69 kB
JavaScript
/* eslint-disable no-param-reassign */
import { AdditiveBlending, DoubleSide, Mesh, MeshBasicMaterial, PlaneBufferGeometry, RepeatWrapping, sRGBEncoding, TextureLoader, } from 'three';
import ThreeObject from './ThreeObject';
import { lonLat2Mercator } from '../../utils/coord';
export default class TextureObject extends ThreeObject {
constructor(options) {
super();
this.geometry = new PlaneBufferGeometry();
const { src, bounds, z = 0, geometry, materialParameters } = options;
this.bounds = bounds;
if (geometry)
this.geometry = geometry;
this.material = new MeshBasicMaterial(Object.assign({ side: DoubleSide, blending: AdditiveBlending, depthWrite: false, depthTest: false }, materialParameters));
this.object3D = new 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 = lonLat2Mercator(minPoint);
const maxMercator = 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 TextureLoader();
loader.load(src, (texture) => {
texture.encoding = sRGBEncoding;
texture.repeat.set(10, 10);
texture.wrapT = RepeatWrapping;
texture.wrapS = RepeatWrapping;
this.material.map = texture;
this.material.needsUpdate = true;
// trigger update
// this.layer?.updatePositionDebounce?.();
}, undefined, function (error) {
console.error(error);
});
}
}