/// <summary> /// Constructor /// </summary> /// <param name="x">Tile X</param> /// <param name="y">Tile Y</param> /// <param name="zoom">Tile zoom</param> /// <param name="map">Reference to the map</param> /// <param name="isMapTile">Should this tile be displayed on the map?</param> public OnlineMapsTile(int x, int y, int zoom, OnlineMaps map, bool isMapTile = true) { remainDownloadsAttempts = countDownloadsAttempts; int maxX = 1 << zoom; if (x < 0) { x += maxX; } else if (x >= maxX) { x -= maxX; } this.x = x; this.y = y; this.zoom = zoom; _map = map; this.isMapTile = isMapTile; double tlx, tly, brx, bry; map.projection.TileToCoordinates(x, y, zoom, out tlx, out tly); map.projection.TileToCoordinates(x + 1, y + 1, zoom, out brx, out bry); topLeft = new OnlineMapsVector2d(tlx, tly); bottomRight = new OnlineMapsVector2d(brx, bry); globalPosition = Vector2.Lerp(topLeft, bottomRight, 0.5f); key = OnlineMapsTileManager.GetTileKey(zoom, x, y); if (isMapTile) { map.tileManager.Add(this); } }
public static ulong GetTileKey(int zoom, int x, int y) { return(OnlineMapsTileManager.GetTileKey(zoom, x, y)); }
public string GetCacheKey(string prefix) { return(prefix + OnlineMapsTileManager.GetTileKey(zoom, x, y)); }
protected override float GetUnscaledElevationValue(double x, double z, double tlx, double tly, double brx, double bry) { if (tiles == null) { tiles = new Dictionary <ulong, Tile>(); return(0); } x = x / -sizeInScene.x; z = z / sizeInScene.y; double ttlx, ttly, tbrx, tbry; map.projection.CoordinatesToTile(tlx, tly, map.zoom, out ttlx, out ttly); map.projection.CoordinatesToTile(brx, bry, map.zoom, out tbrx, out tbry); if (tbrx < ttlx) { tbrx += 1 << map.zoom; } double cx = (tbrx - ttlx) * x + ttlx; double cz = (tbry - ttly) * z + ttly; int zoom = map.zoom - zoomOffset; double tx, ty; map.projection.TileToCoordinates(cx, cz, map.zoom, out cx, out cz); map.projection.CoordinatesToTile(cx, cz, zoom, out tx, out ty); int ix = (int)tx; int iy = (int)ty; ulong key = OnlineMapsTileManager.GetTileKey(zoom, ix, iy); Tile tile; bool hasTile = tiles.TryGetValue(key, out tile); if (hasTile && !tile.loaded) { hasTile = false; } if (!hasTile) { int nz = zoom; while (!hasTile && nz < OnlineMaps.MAXZOOM) { nz++; map.projection.CoordinatesToTile(cx, cz, nz, out tx, out ty); ix = (int)tx; iy = (int)ty; key = OnlineMapsTileManager.GetTileKey(nz, ix, iy); hasTile = tiles.TryGetValue(key, out tile) && tile.loaded; } } if (!hasTile) { int nz = zoom; while (!hasTile && nz > 1) { nz--; map.projection.CoordinatesToTile(cx, cz, nz, out tx, out ty); ix = (int)tx; iy = (int)ty; key = OnlineMapsTileManager.GetTileKey(nz, ix, iy); hasTile = tiles.TryGetValue(key, out tile) && tile.loaded; } } if (!hasTile) { return(0); } map.projection.CoordinatesToTile(cx, cz, tile.zoom, out tx, out ty); return(tile.GetElevation(tx, ty)); }
private void OnLateUpdateBefore() { if (!needUpdateTiles) { if (needUpdateMinMax) { UpdateMinMax(); } return; } needUpdateTiles = false; int zoom = map.zoom - zoomOffset; if (zoom < 1) { zoom = 1; } if (!zoomRange.InRange(map.zoom)) { return; } int currentOffset = map.zoom - zoom; int coef = (1 << currentOffset) * OnlineMapsUtils.tileSize; int countX = Mathf.CeilToInt(map.width / 2f / coef) * 2 + 2; int countY = Mathf.CeilToInt(map.height / 2f / coef) * 2 + 2; double sx, sy; map.GetTilePosition(out sx, out sy, zoom); prevTileX = (int)sx; prevTileY = (int)sy; int isx = prevTileX - countX / 2; int isy = prevTileY - countY / 2; int max = 1 << zoom; foreach (KeyValuePair <ulong, Tile> pair in tiles) { pair.Value.used = false; } for (int x = isx; x < isx + countX; x++) { int cx = x; if (cx < 0) { cx += max; } else if (cx >= max) { cx -= max; } for (int y = Mathf.Max(isy, 0); y < Mathf.Min(isy + countY, max); y++) { ulong key = OnlineMapsTileManager.GetTileKey(zoom, cx, y); Tile t; if (tiles.TryGetValue(key, out t)) { t.used = true; continue; } t = new Tile { x = x, y = y, zoom = zoom, width = tileWidth, height = tileHeight, used = true }; tiles.Add(key, t); if (OnDownload != null) { OnDownload(t); } else { StartDownloadElevationTile(t); } } } double tlx, tly, brx, bry; map.GetTileCorners(out tlx, out tly, out brx, out bry); int itlx = (int)tlx; int itly = (int)tly; int ibrx = (int)brx; int ibry = (int)bry; List <ulong> unloadKeys = new List <ulong>(); foreach (KeyValuePair <ulong, Tile> pair in tiles) { Tile tile = pair.Value; if (tile.used) { continue; } int scale = 1 << (map.zoom - tile.zoom); int tx = tile.x * scale; int ty = tile.y * scale; if (ibrx >= tx && itlx <= tx + scale && ibry >= ty && itly <= ty + scale) { if (Mathf.Abs(zoom - tile.zoom) < 3) { continue; } } unloadKeys.Add(pair.Key); } foreach (ulong key in unloadKeys) { tiles.Remove(key); } UpdateMinMax(); }