Beispiel #1
0
        GameObject GenerateProvinceRegionSurface(int provinceIndex, int regionIndex, Material material)
        {
            Region region = provinces [provinceIndex].regions [regionIndex];

            Polygon poly = new Polygon(region.latlon);

            // Antarctica, Saskatchewan (Canada), British Columbia (Canada), Krasnoyarsk (Russia) - special cases due to its geometry
            if (provinceIndex == 218 || provinceIndex == 220 || provinceIndex == 224 || provinceIndex == 3423)
            {
                float step = 5;
                List <TriangulationPoint> steinerPoints = new List <TriangulationPoint>();
                for (double x = region.minMaxLat.x + step / 2; x < region.minMaxLat.y - step / 2; x += step)
                {
                    for (double y = region.minMaxLon.x + step / 2; y < region.minMaxLon.y - step / 2; y += step)
                    {
                        if (region.ContainsPoint(x, y))
                        {
                            steinerPoints.Add(new TriangulationPoint(x, y));
                        }
                    }
                }
                poly.AddSteinerPoints(steinerPoints);
            }
            P2T.Triangulate(poly);

            int flip1, flip2;

            if (_earthInvertedMode)
            {
                flip1 = 2; flip2 = 1;
            }
            else
            {
                flip1 = 1; flip2 = 2;
            }
            Vector3[] revisedSurfPoints = new Vector3[poly.Triangles.Count * 3];
            for (int k = 0; k < poly.Triangles.Count; k++)
            {
                DelaunayTriangle dt = poly.Triangles[k];
                revisedSurfPoints[k * 3]         = GetSpherePointFromLatLon(dt.Points[0].X, dt.Points[0].Y);
                revisedSurfPoints[k * 3 + flip1] = GetSpherePointFromLatLon(dt.Points[1].X, dt.Points[1].Y);
                revisedSurfPoints[k * 3 + flip2] = GetSpherePointFromLatLon(dt.Points[2].X, dt.Points[2].Y);
            }

            int revIndex = revisedSurfPoints.Length - 1;

            // Generate surface mesh
            int    cacheIndex    = GetCacheIndexForProvinceRegion(provinceIndex, regionIndex);
            string cacheIndexSTR = cacheIndex.ToString();
            // Deletes potential residual surface
            Transform t = surfacesLayer.transform.FindChild(cacheIndexSTR);

            if (t != null)
            {
                DestroyImmediate(t.gameObject);
            }
            GameObject surf = Drawing.CreateSurface(cacheIndexSTR, revisedSurfPoints, revIndex, material);

            surf.transform.SetParent(transform, false);
            surf.transform.localPosition = MiscVector.Vector3zero;
            if (_earthInvertedMode)
            {
                surf.transform.localScale = MiscVector.Vector3one * 0.998f;
            }
            if (surfaces.ContainsKey(cacheIndex))
            {
                surfaces.Remove(cacheIndex);
            }
            surfaces.Add(cacheIndex, surf);
            return(surf);
        }
Beispiel #2
0
        GameObject GenerateProvinceRegionSurface(int provinceIndex, int regionIndex, Material material, Vector2 textureScale, Vector2 textureOffset, float textureRotation, bool temporary)
        {
            if (provinceIndex < 0 || provinceIndex >= provinces.Length)
            {
                return(null);
            }
            if (provinces [provinceIndex].regions == null)
            {
                ReadProvincePackedString(provinces [provinceIndex]);
            }
            if (provinces [provinceIndex].regions == null || regionIndex < 0 || regionIndex >= provinces [provinceIndex].regions.Count)
            {
                return(null);
            }

            Province province = provinces [provinceIndex];
            Region   region   = province.regions [regionIndex];

            if (!temporary)
            {
                region.customMaterial        = material;
                region.customTextureOffset   = textureOffset;
                region.customTextureRotation = textureRotation;
                region.customTextureScale    = textureScale;
                UpdateSurfaceCount();
            }


            // Triangulate to get the polygon vertex indices
            Poly2Tri.Polygon poly = new Poly2Tri.Polygon(region.latlon);

            if (_enableProvinceEnclaves && regionIndex == province.mainRegionIndex)
            {
                ProvinceSubstractProvinceEnclaves(provinceIndex, region, poly);
            }

            // Antarctica, Saskatchewan (Canada), British Columbia (Canada), Krasnoyarsk (Russia) - special cases due to its geometry
            float step = _frontiersDetail == FRONTIERS_DETAIL.High ? 2f : 5f;

            if (steinerPoints == null)
            {
                steinerPoints = new List <TriangulationPoint> (1000);
            }
            else
            {
                steinerPoints.Clear();
            }
            float x0 = region.latlonRect2D.min.x + step / 2f;
            float x1 = region.latlonRect2D.max.x - step / 2f;
            float y0 = region.latlonRect2D.min.y + step / 2f;
            float y1 = region.latlonRect2D.max.y - step / 2f;

            for (float x = x0; x < x1; x += step)
            {
                for (float y = y0; y < y1; y += step)
                {
                    float xp = x + UnityEngine.Random.Range(-0.0001f, 0.0001f);
                    float yp = y + UnityEngine.Random.Range(-0.0001f, 0.0001f);
                    if (region.Contains(xp, yp))
                    {
                        steinerPoints.Add(new TriangulationPoint(xp, yp));
                        //						GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                        //						obj.transform.SetParent(WorldMapGlobe.instance.transform, false);
                        //						obj.transform.localScale = Vector3.one * 0.01f;
                        //						obj.transform.localPosition = Conversion.GetSpherePointFromLatLon(new Vector2(x,y)) * 1.01f;
                    }
                }
            }
            if (steinerPoints.Count > 0)
            {
                poly.AddSteinerPoints(steinerPoints);
            }
            P2T.Triangulate(poly);

            int flip1, flip2;

            if (_earthInvertedMode)
            {
                flip1 = 2;
                flip2 = 1;
            }
            else
            {
                flip1 = 1;
                flip2 = 2;
            }
            int triCount = poly.Triangles.Count;

            Vector3[] revisedSurfPoints = new Vector3[triCount * 3];
            for (int k = 0; k < triCount; k++)
            {
                DelaunayTriangle dt = poly.Triangles [k];
                revisedSurfPoints [k * 3]         = Conversion.GetSpherePointFromLatLon(dt.Points [0].X, dt.Points [0].Y);
                revisedSurfPoints [k * 3 + flip1] = Conversion.GetSpherePointFromLatLon(dt.Points [1].X, dt.Points [1].Y);
                revisedSurfPoints [k * 3 + flip2] = Conversion.GetSpherePointFromLatLon(dt.Points [2].X, dt.Points [2].Y);
            }

            int revIndex = revisedSurfPoints.Length - 1;

            // Generate surface mesh
            int        cacheIndex = GetCacheIndexForProvinceRegion(provinceIndex, regionIndex);
            GameObject surf       = Drawing.CreateSurface(SURFACE_GAMEOBJECT, revisedSurfPoints, revIndex, material, region.rect2Dbillboard, textureScale, textureOffset, textureRotation);

            surf.transform.SetParent(surfacesLayer.transform, false);
            surf.transform.localPosition = Misc.Vector3zero;
            if (_earthInvertedMode)
            {
                surf.transform.localScale = Misc.Vector3one * 0.998f;
            }
            surfaces [cacheIndex] = surf;
            return(surf);
        }
Beispiel #3
0
        GameObject GenerateCountryRegionSurface(int countryIndex, int regionIndex, Material material, bool drawOutline)
        {
            Country country = countries[countryIndex];
            Region  region  = country.regions [regionIndex];

            Polygon poly = new Polygon(region.latlon);

            double maxTriangleSize = 10.0f;

            if (countryIndex == 6)               // Antarctica
            {
                maxTriangleSize = 9.0f;
            }
            if (Mathf.Abs(region.minMaxLat.x - region.minMaxLat.y) > maxTriangleSize ||
                Mathf.Abs(region.minMaxLon.x - region.minMaxLon.y) > maxTriangleSize)                // special case; needs steiner points to reduce the size of triangles
            {
                double step = maxTriangleSize / 2;
                List <TriangulationPoint> steinerPoints = new List <TriangulationPoint>();
                for (double x = region.minMaxLat.x + step / 2; x < region.minMaxLat.y - step / 2; x += step)
                {
                    for (double y = region.minMaxLon.x + step / 2; y < region.minMaxLon.y - step / 2; y += step)
                    {
                        if (ContainsPoint2D(region.latlon, x, y))
                        {
                            steinerPoints.Add(new TriangulationPoint(x, y));
                        }
                    }
                }
                poly.AddSteinerPoints(steinerPoints);
            }

            P2T.Triangulate(poly);

            int flip1, flip2;

            flip1 = 1; flip2 = 2;
            Vector3[] revisedSurfPoints = new Vector3[poly.Triangles.Count * 3];
            for (int k = 0; k < poly.Triangles.Count; k++)
            {
                DelaunayTriangle dt = poly.Triangles[k];
                revisedSurfPoints[k * 3]         = GetSpherePointFromLatLon2(dt.Points[0].X, dt.Points[0].Y);
                revisedSurfPoints[k * 3 + flip1] = GetSpherePointFromLatLon2(dt.Points[1].X, dt.Points[1].Y);
                revisedSurfPoints[k * 3 + flip2] = GetSpherePointFromLatLon2(dt.Points[2].X, dt.Points[2].Y);
            }
            int revIndex = revisedSurfPoints.Length - 1;

            // Generate surface mesh
            int       cacheIndex    = GetCacheIndexForCountryRegion(countryIndex, regionIndex);
            string    cacheIndexSTR = cacheIndex.ToString();
            Transform t             = surfacesLayer.transform.FindChild(cacheIndexSTR);

            if (t != null)
            {
                DestroyImmediate(t.gameObject);
            }
            GameObject surf = Drawing.CreateSurface(cacheIndexSTR, revisedSurfPoints, revIndex, material);

            surf.transform.SetParent(surfacesLayer.transform, false);
            surf.transform.localPosition = MiscVector.Vector3zero;
            surf.transform.localRotation = Quaternion.Euler(MiscVector.Vector3zero);
            if (surfaces.ContainsKey(cacheIndex))
            {
                surfaces.Remove(cacheIndex);
            }
            surfaces.Add(cacheIndex, surf);

            // draw outline
            if (drawOutline)
            {
                DrawCountryRegionOutline(region, surf);
            }
            return(surf);
        }