Exemplo n.º 1
0
 protected void Awake()
 {
     // create the tile template if needed
     if (tileTemplate == null)
     {
         tileTemplate           = Tile.CreateTileTemplate();
         tileTemplate.hideFlags = HideFlags.HideAndDontSave;
         tileTemplate.GetComponent <Renderer>().enabled = false;
     }
     ++tileTemplateUseCount;
 }
Exemplo n.º 2
0
        // <summary>
        // Removes the tiles outside of the camera frustum and zoom level.
        // </summary>
        private void CleanUpTiles(Plane[] frustum, int roundedZoom)
        {
            List <string> tilesToRemove = new List <string>();

            foreach (KeyValuePair <string, Tile> pair in tiles)
            {
                Tile   tile    = pair.Value;
                string tileKey = pair.Key;

                string[] tileAddressTokens = tileKey.Split('_');
                int      tileRoundedZoom   = Int32.Parse(tileAddressTokens[0]);
                int      tileX             = Int32.Parse(tileAddressTokens[1]);
                int      tileY             = Int32.Parse(tileAddressTokens[2]);

                int  roundedZoomDif = tileRoundedZoom - roundedZoom;
                bool inFrustum      = GeometryUtility.TestPlanesAABB(frustum, tile.GetComponent <Collider>().bounds);

                if (!inFrustum || roundedZoomDif != 0)
                {
                    CancelTileRequest(tileX, tileY, tileRoundedZoom);

                    if (!inFrustum ||
                        (roundedZoomDif > 0 && CheckTileOutExistence(roundedZoom, tileRoundedZoom, tileX, tileY)) ||
                        (roundedZoomDif < 0 && CheckTileInExistence(roundedZoom, tileRoundedZoom, tileX, tileY)))
                    {
                        tilesToRemove.Add(tileKey);
                    }
                }
            }

            foreach (string tileAddress in tilesToRemove)
            {
                Tile tile = tiles[tileAddress];

                Renderer renderer = tile.GetComponent <Renderer>();
                if (renderer != null)
                {
                    GameObject.DestroyImmediate(renderer.material.mainTexture);
                    //TextureAtlasManager.Instance.RemoveTexture(pair.Value.TextureId);
                    renderer.material.mainTexture = null;

                    renderer.enabled = false;
                }

#if DEBUG_LOG
                Debug.Log("DEBUG: remove tile: " + pair.Key);
#endif

                tiles.Remove(tileAddress);
                tileCache.Add(tile);
            }
        }
Exemplo n.º 3
0
        // check if a tile is fully visible
        private bool CheckTileExistence(int tileRoundedZoom, int tileX, int tileY)
        {
            string key = Tile.GetTileKey(tileRoundedZoom, tileX, tileY);

            if (!tiles.ContainsKey(key))
            {
                return(true);        // the tile is out of the frustum
            }
            Tile     tile = tiles[key];
            Renderer r    = tile.GetComponent <Renderer>();

            return(r.enabled && r.material.mainTexture != null && !tile.Showing);
        }
Exemplo n.º 4
0
        // <summary>
        // A recursive method that grows tiles starting from the map's center in all four directions.
        // </summary>
        void GrowTiles(Plane[] frustum, int tileX, int tileY, int tileCountOnX, int tileCountOnY, float offsetX, float offsetZ)
        {
            tileTemplate.transform.position = new Vector3(offsetX, tileTemplate.transform.position.y, offsetZ);
            if (GeometryUtility.TestPlanesAABB(frustum, tileTemplate.GetComponent <Collider>().bounds) == true)
            {
                if (tileX < 0)
                {
                    tileX += tileCountOnX;
                }
                else if (tileX >= tileCountOnX)
                {
                    tileX -= tileCountOnX;
                }

                string tileAddress = Tile.GetTileKey(Map.RoundedZoom, tileX, tileY);
                //Debug.Log("DEBUG: tile address: " + tileAddress);
                if (tiles.ContainsKey(tileAddress) == false)
                {
                    Tile tile = null;
                    if (tileCache.Count > 0)
                    {
                        tile = tileCache[0];
                        tileCache.Remove(tile);
                        tile.transform.position   = tileTemplate.transform.position;
                        tile.transform.localScale = new Vector3(Map.RoundedHalfMapScale, 1.0f, Map.RoundedHalfMapScale);
                        //tile.gameObject.active = this.gameObject.active;
                    }
                    else
                    {
                        tile = (GameObject.Instantiate(tileTemplate.gameObject) as GameObject).GetComponent <Tile>();
                        tile.transform.parent = this.gameObject.transform;
                    }

                    tile.name = "tile_" + tileAddress;
                    tiles.Add(tileAddress, tile);
                    //MeshRenderer tileMeshRenderer = tile.GetComponent<MeshRenderer>();
                    //tileMeshRenderer.enabled = true;

                    RequestTile(tileX, tileY, Map.RoundedZoom, tile);
                }

                tileAddressLookedFor = tileAddress;
                if (visitedTiles.Exists(visitedTilesMatchPredicate) == false)
                {
                    visitedTiles.Add(tileAddress);

                    // grow tiles in the four directions without getting outside of the coordinate range of the zoom level
                    int   nTileX, nTileY;
                    float nOffsetX, nOffsetZ;

                    if (GetNeighbourTile(tileX, tileY, offsetX, offsetZ, tileCountOnX, tileCountOnY, NeighbourTileDirection.South, out nTileX, out nTileY, out nOffsetX, out nOffsetZ))
                    {
                        GrowTiles(frustum, nTileX, nTileY, tileCountOnX, tileCountOnY, nOffsetX, nOffsetZ);
                    }

                    if (GetNeighbourTile(tileX, tileY, offsetX, offsetZ, tileCountOnX, tileCountOnY, NeighbourTileDirection.North, out nTileX, out nTileY, out nOffsetX, out nOffsetZ))
                    {
                        GrowTiles(frustum, nTileX, nTileY, tileCountOnX, tileCountOnY, nOffsetX, nOffsetZ);
                    }

                    if (GetNeighbourTile(tileX, tileY, offsetX, offsetZ, tileCountOnX, tileCountOnY, NeighbourTileDirection.East, out nTileX, out nTileY, out nOffsetX, out nOffsetZ))
                    {
                        GrowTiles(frustum, nTileX, nTileY, tileCountOnX, tileCountOnY, nOffsetX, nOffsetZ);
                    }

                    if (GetNeighbourTile(tileX, tileY, offsetX, offsetZ, tileCountOnX, tileCountOnY, NeighbourTileDirection.West, out nTileX, out nTileY, out nOffsetX, out nOffsetZ))
                    {
                        GrowTiles(frustum, nTileX, nTileY, tileCountOnX, tileCountOnY, nOffsetX, nOffsetZ);
                    }
                }
            }
        }