Example #1
0
        public void ReadProvincePackedString(Province province)
        {
            string[] regions     = province.packedRegions.Split(new char[] { '*' }, StringSplitOptions.RemoveEmptyEntries);
            int      regionCount = regions.Length;

            province.regions = new List <Region> (regionCount);
            float   maxVol       = float.MinValue;
            Vector2 minProvince  = Misc.Vector2one * 1000;
            Vector2 maxProvince  = -minProvince;
            Vector2 min          = Misc.Vector2one * 1000;
            Vector2 max          = -min;
            Vector2 latlonCenter = new Vector2();

            for (int r = 0; r < regionCount; r++)
            {
                string[] coordinates = regions [r].Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                int      coorCount   = coordinates.Length;
                min.x = min.y = 1000;
                max.x = max.y = -1000;
                Region    provinceRegion = new Region(province, province.regions.Count);
                Vector2[] latlon         = new Vector2[coorCount];
                for (int c = 0; c < coorCount; c++)
                {
                    float lat, lon;
                    GetPointFromPackedString(coordinates [c], out lat, out lon);
                    if (lat < min.x)
                    {
                        min.x = lat;
                    }
                    if (lat > max.x)
                    {
                        max.x = lat;
                    }
                    if (lon < min.y)
                    {
                        min.y = lon;
                    }
                    if (lon > max.y)
                    {
                        max.y = lon;
                    }
                    latlon [c] = new Vector2(lat, lon);
                }
                provinceRegion.latlon = latlon;
                FastVector.Average(ref min, ref max, ref latlonCenter);                  //  (min + max) * 0.5f;
                provinceRegion.latlonCenter = latlonCenter;

                province.regions.Add(provinceRegion);

                // Calculate province bounding rect
                if (min.x < minProvince.x)
                {
                    minProvince.x = min.x;
                }
                if (min.y < minProvince.y)
                {
                    minProvince.y = min.y;
                }
                if (max.x > maxProvince.x)
                {
                    maxProvince.x = max.x;
                }
                if (max.y > maxProvince.y)
                {
                    maxProvince.y = max.y;
                }
                provinceRegion.latlonRect2D = new Rect(min.x, min.y, max.x - min.x, max.y - min.y);
                provinceRegion.rect2DArea   = provinceRegion.latlonRect2D.width * provinceRegion.latlonRect2D.height;
                float vol = FastVector.SqrDistance(ref min, ref max);                  // (max - min).sqrMagnitude;
                if (vol > maxVol)
                {
                    maxVol = vol;
                    province.mainRegionIndex = r;
                    province.latlonCenter    = provinceRegion.latlonCenter;
                }
            }
            province.regionsRect2D = new Rect(minProvince.x, minProvince.y, maxProvince.x - minProvince.x, maxProvince.y - minProvince.y);
        }
Example #2
0
        void CheckTiles(TileInfo parent, int currentZoomLevel, int xTile, int yTile, int zoomLevel, int subquadIndex)
        {
            // Is this tile visible?
            TileInfo ti;
            int      tileCode = GetTileHashCode(xTile, yTile, zoomLevel);

            if (!cachedTiles.TryGetValue(tileCode, out ti))
            {
                ti        = new TileInfo(xTile, yTile, zoomLevel, subquadIndex, currentEarthTexture);
                ti.parent = parent;
                if (parent != null)
                {
                    if (parent.children == null)
                    {
                        parent.children = new List <TileInfo> ();
                    }
                    parent.children.Add(ti);
                }
                for (int k = 0; k < 4; k++)
                {
                    Vector2 latlon = Conversion.GetLatLonFromTile(xTile + offsets [k].x, yTile + offsets [k].y, zoomLevel);
                    ti.latlons [k] = latlon;
                    Vector3 spherePos = Conversion.GetSpherePointFromLatLon(latlon);
                    ti.spherePos [k] = spherePos;
                }
                cachedTiles [tileCode] = ti;
            }
            // Check if any tile corner is visible
            // Phase I
#if DEBUG_TILES
            if (ti.gameObject != null && ti.gameObject.GetComponent <TileInfoEx> ().debug)
            {
                Debug.Log("this");
            }
#endif

            bool    cornersOccluded = true;
            Vector3 minWorldPos     = Misc.Vector3Max;
            Vector3 maxWorldPos     = Misc.Vector3Min;
            Vector3 tmp             = Misc.Vector3zero;
            for (int c = 0; c < 4; c++)
            {
                Vector3 wpos = transform.TransformPoint(ti.spherePos [c]);
                ti.cornerWorldPos [c] = wpos;
                if (wpos.x < minWorldPos.x)
                {
                    minWorldPos.x = wpos.x;
                }
                if (wpos.y < minWorldPos.y)
                {
                    minWorldPos.y = wpos.y;
                }
                if (wpos.z < minWorldPos.z)
                {
                    minWorldPos.z = wpos.z;
                }
                if (wpos.x > maxWorldPos.x)
                {
                    maxWorldPos.x = wpos.x;
                }
                if (wpos.y > maxWorldPos.y)
                {
                    maxWorldPos.y = wpos.y;
                }
                if (wpos.z > maxWorldPos.z)
                {
                    maxWorldPos.z = wpos.z;
                }
                if (cornersOccluded)
                {
                    float radiusSqr = (wpos.x - globePos.x) * (wpos.x - globePos.x) + (wpos.y - globePos.y) * (wpos.y - globePos.y) + (wpos.z - globePos.z) * (wpos.z - globePos.z);                     //  Vector3.SqrMagnitude (wpos - globePos);
//																				Vector3 camDir = (currentCameraPosition - wpos).normalized;
                    FastVector.NormalizedDirection(ref wpos, ref currentCameraPosition, ref tmp);
//																				Vector3 st = wpos + ndir * (0.01f * transform.localScale.x);
                    Vector3 st = wpos;
                    FastVector.Add(ref st, ref tmp, localScaleFactor);
                    float mag = (st.x - globePos.x) * (st.x - globePos.x) + (st.y - globePos.y) * (st.y - globePos.y) + (st.z - globePos.z) * (st.z - globePos.z);
                    if (mag > radiusSqr)
                    {
                        cornersOccluded = false;
                    }
                }
            }

//												Bounds bounds = new Bounds ((minWorldPos + maxWorldPos) * 0.5f, maxWorldPos - minWorldPos);
            FastVector.Average(ref minWorldPos, ref maxWorldPos, ref tmp);
            Bounds  bounds       = new Bounds(tmp, maxWorldPos - minWorldPos);
            Vector3 tileMidPoint = bounds.center;
            // Check center of quad
            if (cornersOccluded)
            {
                float radiusSqr = (tileMidPoint.x - globePos.x) * (tileMidPoint.x - globePos.x) + (tileMidPoint.y - globePos.y) * (tileMidPoint.y - globePos.y) + (tileMidPoint.z - globePos.z) * (tileMidPoint.z - globePos.z);                 // Vector3.SqrMagnitude (tileMidPoint - globePos);
//																Vector3 camDir = (currentCameraPosition - tileMidPoint).normalized;
                FastVector.NormalizedDirection(ref tileMidPoint, ref currentCameraPosition, ref tmp);
//																Vector3 st = tileMidPoint + tmp * (0.01f * transform.localScale.x);
                Vector3 st = tileMidPoint;
                FastVector.Add(ref st, ref tmp, localScaleFactor);
                float mag = (st.x - globePos.x) * (st.x - globePos.x) + (st.y - globePos.y) * (st.y - globePos.y) + (st.z - globePos.z) * (st.z - globePos.z);
                if (mag > radiusSqr)
                {
                    cornersOccluded = false;
                }
            }

#if DEBUG_TILES
            if (root == null)
            {
                root = new GameObject();
                root.transform.SetParent(transform);
                root.transform.localPosition = Vector3.zero;
                root.transform.localRotation = Misc.QuaternionZero;                 //Quaternion.Euler (0, 0, 0);
            }
#endif

            bool  insideViewport = false;
            float minX = currentCamera.pixelWidth * 2f, minY = currentCamera.pixelHeight * 2f;
            float maxX = -minX, maxY = -minY;
            if (!cornersOccluded)
            {
                // Phase II
                for (int c = 0; c < 4; c++)
                {
                    Vector3 scrPos = currentCamera.WorldToScreenPoint(ti.cornerWorldPos [c]);
                    insideViewport = insideViewport || (scrPos.z > 0 && scrPos.x >= 0 && scrPos.x < currentCamera.pixelWidth && scrPos.y >= 0 && scrPos.y < currentCamera.pixelHeight);
                    if (scrPos.x < minX)
                    {
                        minX = scrPos.x;
                    }
                    if (scrPos.x > maxX)
                    {
                        maxX = scrPos.x;
                    }
                    if (scrPos.y < minY)
                    {
                        minY = scrPos.y;
                    }
                    if (scrPos.y > maxY)
                    {
                        maxY = scrPos.y;
                    }
                }
                if (!insideViewport)
                {
                    insideViewport = GeometryUtility.TestPlanesAABB(cameraPlanes, bounds);
                }
            }

            ti.insideViewport = insideViewport;
            ti.visible        = false;
            if (insideViewport)
            {
                if (!ti.created)
                {
                    CreateTile(ti);
                }

                if (!ti.gameObject.activeSelf)
                {
                    ti.gameObject.SetActive(true);
                }

                // Manage hierarchy of tiles
                bool tileIsBig = false;
                FastVector.NormalizedDirection(ref globePos, ref tileMidPoint, ref tmp);
//																float dd = Vector3.Dot (currentCameraForward, (tileMidPoint - globePos).normalized);
                float dd = Vector3.Dot(currentCameraForward, tmp);
                if (dd > -0.8f || currentZoomLevel > 9)                   // prevents big seams on initial zooms
                {
                    float aparentSize = Mathf.Max(maxX - minX, maxY - minY);
                    tileIsBig = aparentSize > currentTileSize;
                }
                else
                {
                    tileIsBig = ti.zoomLevel < currentZoomLevel;
                }

                                #if DEBUG_TILES
                if (ti.gameObject != null)
                {
                    ti.gameObject.GetComponent <TileInfoEx> ().bigTile   = tileIsBig;
                    ti.gameObject.GetComponent <TileInfoEx> ().zoomLevel = ti.zoomLevel;
                }
                                #endif

                if ((tileIsBig || zoomLevel < TILE_MIN_ZOOM_LEVEL) && zoomLevel < _tileMaxZoomLevel)
                {
                    // Load nested tiles
                    CheckTiles(ti, currentZoomLevel, xTile * 2, yTile * 2, zoomLevel + 1, 0);
                    CheckTiles(ti, currentZoomLevel, xTile * 2 + 1, yTile * 2, zoomLevel + 1, 1);
                    CheckTiles(ti, currentZoomLevel, xTile * 2, yTile * 2 + 1, zoomLevel + 1, 2);
                    CheckTiles(ti, currentZoomLevel, xTile * 2 + 1, yTile * 2 + 1, zoomLevel + 1, 3);
                    ti.renderer.enabled = false;
                }
                else
                {
                    ti.visible = true;

                    // Show tile renderer
                    if (!ti.renderer.enabled)
                    {
                        ti.renderer.enabled = true;
                    }

                    // If parent tile is loaded then use that as placeholder texture
                    if (ti.zoomLevel > TILE_MIN_ZOOM_LEVEL && ti.parent.loadStatus == TILE_LOAD_STATUS.Loaded && !ti.placeholderImageSet)
                    {
                        ti.placeholderImageSet = true;
                        ti.parentTextureCoords = placeHolderUV [ti.subquadIndex];
                        ti.SetPlaceholderImage(ti.parent.texture);
                    }

                    if (ti.loadStatus == TILE_LOAD_STATUS.Loaded)
                    {
                        if (!ti.hasAnimated)
                        {
                            ti.hasAnimated = true;
                            ti.Animate(1f, AnimationEnded);
                        }
                    }
                    else if (ti.loadStatus == TILE_LOAD_STATUS.Inactive)
                    {
                        ti.distToCamera = FastVector.SqrDistance(ref ti.cornerWorldPos [0], ref currentCameraPosition) * ti.zoomLevel;
                        ti.loadStatus   = TILE_LOAD_STATUS.InQueue;
                        ti.queueTime    = Time.time;
                        loadQueue.Add(ti);
                    }
                    if (ti.children != null)
                    {
                        for (int k = 0; k < 4; k++)
                        {
                            TileInfo tiChild = ti.children [k];
                            HideTile(tiChild);
                        }
                    }
                }
            }
            else
            {
                HideTile(ti);
            }
        }