|
| 1 | +import { MVTTilesMeshPlugin } from './MVTTilesMeshPlugin.js'; |
| 2 | +import { PMTilesLoaderBase } from '../../core/renderer/loaders/PMTilesLoaderBase.js'; |
| 3 | +import { ProjectionScheme } from './images/utils/ProjectionScheme.js'; |
| 4 | + |
| 5 | +export class PMTilesMeshPlugin extends MVTTilesMeshPlugin { |
| 6 | + |
| 7 | + constructor( options = {} ) { |
| 8 | + |
| 9 | + super( options ); |
| 10 | + |
| 11 | + this.name = 'PMTILES_MESH_PLUGIN'; |
| 12 | + this.pmtilesLoader = new PMTilesLoaderBase(); |
| 13 | + this._pmtilesUrl = options.url; |
| 14 | + |
| 15 | + } |
| 16 | + |
| 17 | + async loadRootTileset() { |
| 18 | + |
| 19 | + // Initialize PMTiles and get header |
| 20 | + const header = await this.pmtilesLoader.init( this._pmtilesUrl ); |
| 21 | + |
| 22 | + // Configure tiling from header |
| 23 | + this.imageSource.tiling.flipY = true; |
| 24 | + this.imageSource.tiling.setProjection( new ProjectionScheme( 'EPSG:3857' ) ); |
| 25 | + this.imageSource.tiling.generateLevels( |
| 26 | + header.maxZoom, |
| 27 | + this.imageSource.tiling.projection.tileCountX, |
| 28 | + this.imageSource.tiling.projection.tileCountY, |
| 29 | + { |
| 30 | + tilePixelWidth: this.imageSource.tileDimension, |
| 31 | + tilePixelHeight: this.imageSource.tileDimension, |
| 32 | + } |
| 33 | + ); |
| 34 | + |
| 35 | + // Override getUrl to use pmtiles:// scheme |
| 36 | + this.imageSource.getUrl = ( x, y, level ) => this.pmtilesLoader.getUrl( level, x, y ); |
| 37 | + |
| 38 | + return this.getTileset( this._pmtilesUrl ); |
| 39 | + |
| 40 | + } |
| 41 | + |
| 42 | + // Intercept pmtiles:// URLs and fetch from the PMTiles archive |
| 43 | + fetchData( url, options ) { |
| 44 | + |
| 45 | + if ( url.startsWith( 'pmtiles://' ) ) { |
| 46 | + |
| 47 | + const { z, x, y } = PMTilesLoaderBase.parseUrl( url ); |
| 48 | + |
| 49 | + return this.pmtilesLoader.getTile( z, x, y, options?.signal ) |
| 50 | + .then( buffer => buffer || new ArrayBuffer( 0 ) ); |
| 51 | + |
| 52 | + } |
| 53 | + |
| 54 | + return null; |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + // Override to handle pmtiles:// URLs (no file extension) |
| 59 | + async parseToMesh( buffer, tile, extension, uri, abortSignal ) { |
| 60 | + |
| 61 | + if ( abortSignal.aborted ) { |
| 62 | + |
| 63 | + return null; |
| 64 | + |
| 65 | + } |
| 66 | + |
| 67 | + // Handle pmtiles:// URLs OR standard .pbf/.mvt extensions |
| 68 | + if ( uri.startsWith( 'pmtiles://' ) || extension === 'pbf' || extension === 'mvt' ) { |
| 69 | + |
| 70 | + const result = await this.loader.parse( buffer ); |
| 71 | + const group = result.scene; |
| 72 | + |
| 73 | + this._projectGroupToGlobe( group, tile ); |
| 74 | + |
| 75 | + return group; |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + return null; |
| 80 | + |
| 81 | + } |
| 82 | + |
| 83 | +} |
0 commit comments