UNPKG

@giro3d/giro3d

Version:

A JS/WebGL framework for 3D geospatial data visualization

69 lines (66 loc) 3.6 kB
import { DataTexture, FloatType, LinearFilter, RGFormat } from 'three'; import WorkerPool from '../utils/WorkerPool'; import ImageFormat from './ImageFormat'; import { decodeMapboxTerrainImage } from './mapboxWorker'; let workerPool = null; function createWorker() { return new Worker(URL.createObjectURL(new Blob([atob('InVzZSBzdHJpY3QiOygoKT0+e2Z1bmN0aW9uIGgoZSx0KXtyZXR1cm57cmVxdWVzdElkOmUsZXJyb3I6dCBpbnN0YW5jZW9mIEVycm9yP3QubWVzc2FnZToidW5rbm93biBlcnJvciJ9fWZ1bmN0aW9uIG0oZSl7bGV0IHQ9bmV3IE9mZnNjcmVlbkNhbnZhcyhlLndpZHRoLGUuaGVpZ2h0KTt0LndpZHRoPWUud2lkdGgsdC5oZWlnaHQ9ZS5oZWlnaHQ7bGV0IHI9dC5nZXRDb250ZXh0KCIyZCIse3dpbGxSZWFkRnJlcXVlbnRseTohMH0pO2lmKCFyKXRocm93IGNvbnNvbGUuZXJyb3IoImNvdWxkIG5vdCBhY3F1aXJlIDJEIGNvbnRleHQgb24gY2FudmFzIiksbmV3IEVycm9yKCJjb3VsZCBub3QgYWNxdWlyZSAyRCBjb250ZXh0IG9uIGNhbnZhcyIpO3JldHVybiByLmRyYXdJbWFnZShlLDAsMCksci5nZXRJbWFnZURhdGEoMCwwLGUud2lkdGgsZS5oZWlnaHQpLmRhdGF9YXN5bmMgZnVuY3Rpb24gZihlLHQpe2xldCByPWF3YWl0IGNyZWF0ZUltYWdlQml0bWFwKGUpLG89bShyKTtyZXR1cm57Li4uZyhvLHQpLHdpZHRoOnIud2lkdGgsaGVpZ2h0OnIuaGVpZ2h0fX1mdW5jdGlvbiBnKGUsdCl7bGV0IHI9ZS5sZW5ndGglMz09PTA/Mzo0LG89ZS5sZW5ndGgvcixzPW5ldyBGbG9hdDMyQXJyYXkobyoyKSxpPTAsYz0xLzAsZD0tMS8wO2ZvcihsZXQgbj0wO248ZS5sZW5ndGg7bis9cil7bGV0IHU9ZVtuKzBdLGw9ZVtuKzFdLHA9ZVtuKzJdLGE9LTFlNCsodSoyNTYqMjU2K2wqMjU2K3ApKi4xO3NbaSoyKzBdPWEsdCE9bnVsbCYmdD09PWE/c1tpKjIrMV09MDooc1tpKjIrMV09MSxjPU1hdGgubWluKGMsYSksZD1NYXRoLm1heChkLGEpKSxpKz0xfXJldHVybntkYXRhOnMuYnVmZmVyLG1pbjpjLG1heDpkfX1vbm1lc3NhZ2U9YXN5bmMgZnVuY3Rpb24oZSl7bGV0IHQ9ZS5kYXRhO3RyeXtpZih0LnR5cGU9PT0iRGVjb2RlTWFwYm94VGVycmFpbk1lc3NhZ2UiKXtsZXQgcj1uZXcgQmxvYihbdC5wYXlsb2FkLmJ1ZmZlcl0se3R5cGU6ImltYWdlL3BuZyJ9KSxvPWF3YWl0IGYocix0LnBheWxvYWQubm9EYXRhKSxzPXtyZXF1ZXN0SWQ6dC5pZCxwYXlsb2FkOm99O3RoaXMucG9zdE1lc3NhZ2Uocyx7dHJhbnNmZXI6W3MucGF5bG9hZC5kYXRhXX0pfX1jYXRjaChyKXt0aGlzLnBvc3RNZXNzYWdlKGgodC5pZCxyKSl9fTt9KSgpOwo=')], {type: "text/javascript"})), { type: 'module', name: 'mapbox' }); } /** * Decoder for [Mapbox Terrain](https://docs.mapbox.com/data/tilesets/reference/mapbox-terrain-dem-v1/) images. */ class MapboxTerrainFormat extends ImageFormat { isMapboxTerrainFormat = true; type = 'MapboxTerrainFormat'; _enableWorkers = true; /** * @param options - Decoder options. */ constructor(options) { super(true, FloatType); this._enableWorkers = options?.enableWorkers ?? true; } /** * Decode a Mapbox Terrain blob into a * [DataTexture](https://threejs.org/docs/?q=texture#api/en/textures/DataTexture) containing * the elevation data. * * @param blob - the data to decode * @param options - the decoding options */ async decode(blob, options) { let result; if (this._enableWorkers) { result = await this.getHeightValuesUsingWorker(blob, options?.noDataValue); } else { result = await decodeMapboxTerrainImage(blob, options?.noDataValue); } const texture = new DataTexture(new Float32Array(result.data), result.width, result.height, RGFormat, FloatType); texture.needsUpdate = true; texture.generateMipmaps = false; texture.magFilter = LinearFilter; texture.minFilter = LinearFilter; return { texture, min: result.min, max: result.max }; } async getHeightValuesUsingWorker(blob, noData) { if (workerPool == null) { workerPool = new WorkerPool({ createWorker }); } const buffer = await blob.arrayBuffer(); const result = await workerPool.queue('DecodeMapboxTerrainMessage', { buffer, noData }, [buffer]); return result; } } export default MapboxTerrainFormat;