Exemplo n.º 1
0
        public Texture2D BakeTexture(string outputFile)
        {
            // Get all triangles and its colors
            Texture2D texture = Instantiate(transform.Find("WorldMapGlobeEarth").GetComponent <Renderer>().sharedMaterial.mainTexture) as Texture2D;

            texture.hideFlags = HideFlags.DontSave;
            int width  = texture.width;
            int height = texture.height;

            Color[] colors = texture.GetPixels();

            if (_surfacesLayer != null)
            {
                Transform[] surfaces = _surfacesLayer.GetComponentsInChildren <Transform>();
                // Antartica k = 16
                for (int k = 0; k < surfaces.Length; k++)
                {
                    // Get the color
                    Color    color;
                    Renderer rr = surfaces[k].GetComponent <Renderer>();
                    if (rr != null)
                    {
                        color = rr.sharedMaterial.color;
                    }
                    else
                    {
                        continue;                 // not valid
                    }
                    // Get triangles and paint over the texture
                    MeshFilter mf = surfaces[k].GetComponent <MeshFilter>();
                    if (mf == null || mf.sharedMesh.GetTopology(0) != MeshTopology.Triangles)
                    {
                        continue;
                    }
                    Vector3[] vertex = mf.sharedMesh.vertices;
                    int[]     index  = mf.sharedMesh.GetTriangles(0);

                    float maxEdge = width * 0.8f;
                    float minEdge = width * 0.2f;
                    for (int i = 0; i < index.Length; i += 3)
                    {
                        Vector3 p1 = Conversion.ConvertToTextureCoordinates(vertex[index[i]], width, height);
                        Vector3 p2 = Conversion.ConvertToTextureCoordinates(vertex[index[i + 1]], width, height);
                        Vector3 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());
            return(texture);
        }