void CreateTile(TileInfo ti) { Vector2 latLonTL = ti.latlons [0]; Vector2 latLonBR; ZoomLevelInfo zi = zoomLevelsInfo [ti.zoomLevel]; int tileCode = GetTileHashCode(ti.x + 1, ti.y + 1, ti.zoomLevel); TileInfo cachedTile; if (cachedTiles.TryGetValue(tileCode, out cachedTile)) { latLonBR = cachedTile.latlons [0]; } else { latLonBR = Conversion.GetLatLonFromTile(ti.x + 1, ti.y + 1, ti.zoomLevel); } // Avoid seams on very close distance to surface if (ti.zoomLevel >= 16) { float tao = 0.000002f * (ti.zoomLevel - 15); latLonTL.x += tao; latLonTL.y -= tao; latLonBR.x -= tao; latLonBR.y += tao; } // Create container GameObject parentObj; if (ti.parent == null) { parentObj = zi.tilesContainer; if (parentObj == null) { parentObj = new GameObject("Tiles" + ti.zoomLevel); parentObj.transform.SetParent(tilesRoot, false); zi.tilesContainer = parentObj; } } else { parentObj = ti.parent.gameObject; } // Prepare mesh vertices Vector3[] tileCorners = new Vector3[4]; tileCorners [0] = Conversion.GetSpherePointFromLatLon(latLonTL); tileCorners [1] = Conversion.GetSpherePointFromLatLon(new Vector2(latLonTL.x, latLonBR.y)); tileCorners [2] = Conversion.GetSpherePointFromLatLon(latLonBR); tileCorners [3] = Conversion.GetSpherePointFromLatLon(new Vector2(latLonBR.x, latLonTL.y)); // Setup tile materials TileInfo parent = ti.parent != null ? ti.parent : ti; if (parent.normalMat == null) { parent.normalMat = Instantiate(tileMatRef); parent.normalMat.hideFlags = HideFlags.DontSave; } if (parent.transMat == null) { parent.transMat = Instantiate(tileMatTransRef); parent.transMat.hideFlags = HideFlags.DontSave; } Material tileMat = ti.zoomLevel < TILE_MIN_ZOOM_LEVEL ? parent.normalMat : parent.transMat; // UVs wrt Earth texture Vector2 tl = new Vector2((latLonTL.y + 180) / 360f, (latLonTL.x + 90) / 180f); Vector2 br = new Vector2((latLonBR.y + 180) / 360f, (latLonBR.x + 90) / 180f); if (tl.x > 0.5f && br.x < 0.5f) { br.x = 1f; } ti.worldTextureCoords = new Vector4(tl.x, br.y, br.x, tl.y); ti.ClearPlaceholderImage(); if (ti.zoomLevel < TILE_MIN_ZOOM_LEVEL) { ti.loadStatus = TILE_LOAD_STATUS.Loaded; } ti.texture = currentEarthTexture; ti.renderer = CreateObject(parentObj.transform, "Tile", tileCorners, tileIndices, tileUV, tileMat, ti.subquadIndex); ti.gameObject = ti.renderer.gameObject; ti.renderer.enabled = false; ti.created = true; #if DEBUG_TILES ti.gameObject.AddComponent <TileInfoEx> (); #endif }
void MonitorInactiveTiles() { int inactiveCount = inactiveTiles.Count; bool changes = false; bool releasedMemory = false; for (int k = 0; k < inactiveCount; k++) { TileInfo ti = inactiveTiles [k]; if (ti == null || ti.gameObject == null || ti.visible || ti.texture == currentEarthTexture || ti.loadStatus != TILE_LOAD_STATUS.Loaded) { inactiveTiles [k] = null; ti.isAddedToInactive = false; changes = true; continue; } if (Time.time - ti.inactiveTime > _tileKeepAlive) { inactiveTiles [k] = null; ti.isAddedToInactive = false; ti.loadStatus = TILE_LOAD_STATUS.Inactive; // tile is now invisible, setup material for when it appears again: ti.ClearPlaceholderImage(); if (ti.source != TILE_SOURCE.Resources) { Destroy(ti.texture); } ti.texture = currentEarthTexture; // Reset parentcoords on children if (ti.children != null) { int cCount = ti.children.Count; for (int c = 0; c < cCount; c++) { TileInfo tiChild = ti.children [c]; if (!tiChild.animationFinished) { tiChild.ClearPlaceholderImage(); } } } changes = true; releasedMemory = true; } } if (changes) { List <TileInfo> newInactiveList = new List <TileInfo> (); for (int k = 0; k < inactiveCount; k++) { if (inactiveTiles [k] != null) { newInactiveList.Add(inactiveTiles [k]); } } inactiveTiles.Clear(); inactiveTiles = newInactiveList; if (releasedMemory) { Resources.UnloadUnusedAssets(); GC.Collect(); } } }