static void ExportProvincesMap(string outputFile)
        {
            WMSK map = WMSK.instance;

            if (map == null)
            {
                return;
            }

            // Get all triangles and its colors
            const int width   = 8192;
            const int height  = 4096;
            Texture2D texture = new Texture2D(width, height, TextureFormat.ARGB32, false);

            Color[] colors = new Color[width * height];

            int             provincesCount = map.provinces.Length;
            HashSet <Color> provinceColors = new HashSet <Color> ();

//			float maxEdge = width * 0.8f;
//			float minEdge = width * 0.2f;

            for (int prov = 0; prov < provincesCount; prov++)
            {
                Color color;
                do
                {
                    float g = UnityEngine.Random.Range(0.1f, 1f);                    // avoids full black (used by background)
                    color = new Color(UnityEngine.Random.value, g, UnityEngine.Random.value);
                } while (provinceColors.Contains(color));
                provinceColors.Add(color);
                Province province = map.provinces [prov];
                if (province.regions == null)
                {
                    map.ReadProvincePackedString(province);
                }
                int regionsCount = province.regions.Count;
                for (int pr = 0; pr < regionsCount; pr++)
                {
                    GameObject surf = map.ToggleProvinceRegionSurface(prov, pr, true, color);
                    // Get triangles and paint over the texture
                    MeshFilter mf = surf.GetComponent <MeshFilter> ();
                    if (mf == null || mf.sharedMesh.GetTopology(0) != MeshTopology.Triangles)
                    {
                        continue;
                    }
                    Vector3[] vertex = mf.sharedMesh.vertices;
                    int[]     index  = mf.sharedMesh.GetTriangles(0);

                    for (int i = 0; i < index.Length; i += 3)
                    {
                        Vector2 p1 = Conversion.ConvertToTextureCoordinates(vertex [index [i]], width, height);
                        Vector2 p2 = Conversion.ConvertToTextureCoordinates(vertex [index [i + 1]], width, height);
                        Vector2 p3 = Conversion.ConvertToTextureCoordinates(vertex [index [i + 2]], width, height);
                        // Sort points
                        if (p2.x > p3.x)
                        {
                            Vector3 p = p2;
                            p2 = p3;
                            p3 = p;
                        }
                        if (p1.x > p2.x)
                        {
                            Vector3 p = p1;
                            p1 = p2;
                            p2 = p;
                            if (p2.x > p3.x)
                            {
                                p  = p2;
                                p2 = p3;
                                p3 = p;
                            }
                        }
//						if (p1.x < minEdge && p2.x < minEdge && p3.x > maxEdge) {
//							if (p1.x < 1 && p2.x < 1) {
//								p1.x = width - p1.x;
//								p2.x = width - p2.x;
//							} else
//								p3.x = width - p3.x;
//						} else if (p1.x < minEdge && p2.x > maxEdge && p3.x > maxEdge) {
//							p1.x = width + p1.x;
//						}
                        Drawing.DrawTriangle(colors, width, height, p1, p2, p3, color);
                    }
                }
            }
            texture.SetPixels(colors);
            texture.Apply();

            if (File.Exists(outputFile))
            {
                File.Delete(outputFile);
            }
            File.WriteAllBytes(outputFile, texture.EncodeToPNG());
            AssetDatabase.Refresh();

            map.HideProvinceSurfaces();
            TextureImporter imp = (TextureImporter)AssetImporter.GetAtPath(outputFile);

            if (imp != null)
            {
                imp.maxTextureSize = 8192;
                imp.SaveAndReimport();
            }
        }
예제 #2
0
        /// <summary>
        /// Paints the region into a given texture color array.
        /// </summary>
        /// <param name="region">Region.</param>
        /// <param name="color">Color.</param>
        public void RegionPaintHeights(Color[] colors, int textureWidth, int textureHeight, Region region, float[] heights, float minHeight, int heightsWidth, int heightsHeight, Gradient gradient)
        {
            // Get the region mesh
            int        entityIndex, regionIndex;
            bool       isCountry   = region.entity is Country;
            bool       hideSurface = false;
            GameObject surf;

            if (isCountry)
            {
                entityIndex = GetCountryIndex((Country)region.entity);
                regionIndex = GetCountryRegionIndex(entityIndex, region);
                surf        = GetCountryRegionSurfaceGameObject(entityIndex, regionIndex);
                if (surf == null)
                {
                    surf        = ToggleCountryRegionSurface(entityIndex, regionIndex, true, Color.white);
                    hideSurface = true;
                }
            }
            else
            {
                entityIndex = GetProvinceIndex((Province)region.entity);
                regionIndex = GetProvinceRegionIndex(entityIndex, region);
                surf        = GetCountryRegionSurfaceGameObject(entityIndex, regionIndex);
                if (surf == null)
                {
                    surf        = ToggleProvinceRegionSurface(entityIndex, regionIndex, true, Color.white);
                    hideSurface = true;
                }
            }
            if (surf == null)
            {
                return;
            }

            // Get triangles and paint over the texture
            MeshFilter mf = surf.GetComponent <MeshFilter>();

            if (mf == null || mf.sharedMesh.GetTopology(0) != MeshTopology.Triangles)
            {
                return;
            }
            Vector3[] vertices = mf.sharedMesh.vertices;
            int[]     indices  = mf.sharedMesh.GetTriangles(0);

            float maxEdge = textureWidth * 0.8f;
            float minEdge = textureWidth * 0.2f;

            for (int i = 0; i < indices.Length; i += 3)
            {
                Vector2 p1 = ConvertToTextureCoordinates(vertices[indices[i]], textureWidth, textureHeight);
                Vector2 p2 = ConvertToTextureCoordinates(vertices[indices[i + 1]], textureWidth, textureHeight);
                Vector2 p3 = ConvertToTextureCoordinates(vertices[indices[i + 2]], textureWidth, textureHeight);
                // Sort points
                if (p2.x > p3.x)
                {
                    Vector2 p = p2;
                    p2 = p3;
                    p3 = p;
                }
                if (p1.x > p2.x)
                {
                    Vector2 p = p1;
                    p1 = p2;
                    p2 = p;
                    if (p2.x > p3.x)
                    {
                        p  = p2;
                        p2 = p3;
                        p3 = p;
                    }
                }
                if (p1.x < minEdge && p2.x < minEdge && p3.x > maxEdge)
                {
                    if (p1.x < 1 && p2.x < 1)
                    {
                        p1.x = textureWidth - p1.x;
                        p2.x = textureWidth - p2.x;
                    }
                    else
                    {
                        p3.x = textureWidth - p3.x;
                    }
                }
                else if (p1.x < minEdge && p2.x > maxEdge && p3.x > maxEdge)
                {
                    p1.x = textureWidth + p1.x;
                }
                Drawing.DrawTriangle(colors, textureWidth, textureHeight, p1, p2, p3, heights, minHeight, heightsWidth, heightsHeight, gradient);
            }

            if (hideSurface)
            {
                if (isCountry)
                {
                    ToggleCountryRegionSurface(entityIndex, regionIndex, false, Color.white);
                }
                else
                {
                    ToggleProvinceRegionSurface(entityIndex, regionIndex, false, Color.white);
                }
            }
        }