import Kernel from '../Kernel' ; import Extent from '../Extent' ; import Camera from '../Camera' ; import MathUtils from '../math/Utils' ; import MeshTextureGraphic from '../graphics/MeshTextureGraphic' ; import TileMaterial from '../materials/TileMaterial' ; import TileGeometry from '../geometries/TileGeometry' ; import Vertice from '../geometries/MeshVertice' ; import Triangle from '../geometries/Triangle' ; import SubTiledLayer from '../layers/SubTiledLayer' ; import TileGrid from '../TileGrid' ; class TileInfo { minLon : number = null ; minLat : number = null ; maxLon : number = null ; maxLat : number = null ; minX : number = null ; minY : number = null ; maxX : number = null ; maxY : number = null ; segment : number = 1 ; geometry : TileGeometry ; material : TileMaterial ; visible : boolean ; constructor ( public level , public row , public column , public url ) { this . _setTileInfo ( ) ; this . _handleGlobeTile ( ) ; this . material = new TileMaterial ( this . level , this . url ) ; } _setTileInfo ( ) { var Egeo = MathUtils . getTileGeographicEnvelopByGrid ( this . level , this . row , this . column ) ; this . minLon = Egeo . minLon ; this . minLat = Egeo . minLat ; this . maxLon = Egeo . maxLon ; this . maxLat = Egeo . maxLat ; var minCoord = MathUtils . degreeGeographicToWebMercator ( this . minLon , this . minLat ) ; var maxCoord = MathUtils . degreeGeographicToWebMercator ( this . maxLon , this . maxLat ) ; this . minX = minCoord [ 0 ] ; this . minY = minCoord [ 1 ] ; this . maxX = maxCoord [ 0 ] ; this . maxY = maxCoord [ 1 ] ; } _handleGlobeTile ( ) { var BASE_LEVEL = Kernel . BASE_LEVEL ; if ( this . level < BASE_LEVEL ) { var changeLevel = BASE_LEVEL - this . level ; this . segment = Math . pow ( 2 , changeLevel ) ; } else { this . segment = 1 ; } this . _handleTile ( ) ; } _handleTile ( ) { this . visible = true ; var verticeArray = [ ] ; var triangleArray = [ ] ; var vertices = [ ] ; var indices = [ ] ; var textureCoords = [ ] ; var deltaX = ( this . maxX - this . minX ) / this . segment ; var deltaY = ( this . maxY - this . minY ) / this . segment ; var deltaTextureCoord = 1.0 / this . segment ; var levelDeltaR = 0 ; var mercatorXs = [ ] ; var mercatorYs = [ ] ; var textureSs = [ ] ; var textureTs = [ ] ; var i , j ; for ( i = 0 ; i <= this . segment ; i ++ ) { mercatorXs . push ( this . minX + i * deltaX ) ; mercatorYs . push ( this . maxY - i * deltaY ) ; var b = i * deltaTextureCoord ; textureSs . push ( b ) ; textureTs . push ( 1 - b ) ; } var index = 0 ; for ( i = 0 ; i <= this . segment ; i ++ ) { for ( j = 0 ; j <= this . segment ; j ++ ) { var merX = mercatorXs [ j ] ; var merY = mercatorYs [ i ] ; var ele = 0 ; var lonlat = MathUtils . webMercatorToDegreeGeographic ( merX , merY ) ; var p = MathUtils . geographicToCartesianCoord ( lonlat [ 0 ] , lonlat [ 1 ] , Kernel . EARTH_RADIUS + ele + levelDeltaR ) . getArray ( ) ; vertices = vertices . concat ( p ) ; textureCoords = textureCoords . concat ( textureSs [ j ] , textureTs [ i ] ) ; var v = new Vertice ( { p : p , i : index , uv : [ textureSs [ j ] , textureTs [ i ] ] } ) ; verticeArray . push ( v ) ; index ++ ; } } for ( i = 0 ; i < this . segment ; i ++ ) { for ( j = 0 ; j < this . segment ; j ++ ) { var idx0 = ( this . segment + 1 ) * i + j ; var idx1 = ( this . segment + 1 ) * ( i + 1 ) + j ; var idx2 = idx1 + 1 ; var idx3 = idx0 + 1 ; indices = indices . concat ( idx0 , idx1 , idx2 ) ; indices = indices . concat ( idx2 , idx3 , idx0 ) ; var v0 = verticeArray [ idx0 ] ; var v1 = verticeArray [ idx1 ] ; var v2 = verticeArray [ idx2 ] ; var v3 = verticeArray [ idx3 ] ; var triangle1 = new Triangle ( v0 , v1 , v2 ) ; var triangle2 = new Triangle ( v2 , v3 , v0 ) ; triangleArray . push ( triangle1 , triangle2 ) ; } } this . geometry = new TileGeometry ( verticeArray , triangleArray ) ; } } export default class Tile extends MeshTextureGraphic { subTiledLayer : SubTiledLayer ; private constructor ( public geometry , public material , public tileInfo ) { super ( geometry , material ) ; } static getInstance ( level , row , column , url ) { var tileInfo = new TileInfo ( level , row , column , url ) ; return new Tile ( tileInfo . geometry , tileInfo . material , tileInfo ) ; } protected updateShaderUniforms ( camera ) { } getExtent ( ) { var tileInfo = this . tileInfo ; var tileGrid = new TileGrid ( tileInfo . level , tileInfo . row , tileInfo . column ) ; return new Extent ( this . tileInfo . minLon , this . tileInfo . minLat , this . tileInfo . maxLon , this . tileInfo . maxLat ) ; } shouldDraw ( camera ) { return this . tileInfo . visible && super . shouldDraw ( camera ) ; } destroy ( ) { super . destroy ( ) ; this . subTiledLayer = null ; } } ;