Ejemplo n.º 1
0
        public IEnumerable <HeightmapTile> GetVisibleTiles()
        {
            // calculate visible rect between -1 and 1
            float size        = 1.0f / Mathf.Pow(2, Mathf.CeilToInt(zoomLevel));
            Rect  visibleArea = new Rect(-center.x, -center.y, size, size);

            // Calculate all visible tiles from the visible area:
            Vector2    resolution       = new Vector2(settings.GetResolvedWidth(graph), settings.GetResolvedHeight(graph));
            float      scaledResolution = Mathf.Lerp(256, 128, 1 - (Mathf.CeilToInt(zoomLevel) - zoomLevel));
            Vector2Int tileCount        = new Vector2Int(Mathf.CeilToInt(resolution.x / scaledResolution), Mathf.CeilToInt(resolution.y / scaledResolution));
            Vector2    offset           = visibleArea.position - Vector2.one * (1 / Mathf.Pow(2, zoomLevel));

            // Iterate over possibly visible tiles
            var minTile = LocalToWorld(new Rect(offset.x, offset.y, size, size));

            for (int x = 0; x < tileCount.x; x++)
            {
                for (int y = 0; y < tileCount.y; y++)
                {
                    var tile = new HeightmapTile {
                        x = minTile.x + x, y = minTile.y + y, zoom = minTile.zoom
                    };

                    if (IsOutOfBounds(tile))
                    {
                        continue;
                    }

                    yield return(tile);
                }
            }
        }
Ejemplo n.º 2
0
        public Rect WorldToLocal(HeightmapTile worldPos)
        {
            float size    = 1 / Mathf.Pow(2, worldPos.zoom);
            int   tileMax = (int)Mathf.Pow(2, worldPos.zoom);

            float x = worldPos.x / (float)tileMax * 2.0f - 1.0f;
            float y = worldPos.y / (float)tileMax * 2.0f - 1.0f;

            return(new Rect(x, y, size, size));
        }
Ejemplo n.º 3
0
        public bool IsOutOfBounds(HeightmapTile tile)
        {
            // x is the zoom
            if (tile.zoom < 0 || tile.zoom > 15)
            {
                return(true);
            }

            if (tile.x < 0 || tile.y < 0)
            {
                return(true);
            }

            int max = (int)Mathf.Pow(2, tile.zoom);

            if (tile.x >= max || tile.y >= max)
            {
                return(true);
            }

            return(false);
        }