Пример #1
0
        public static Texture2D Extrude(this Texture2D source, Vector2 tileSize, Vector2 borderSize, TextureExtrude extrudeColor = TextureExtrude.Default)
        {
            if (source == null)
            {
                return(null);
            }
            int   cols   = (int)Mathf.Floor(source.width / tileSize.x);
            int   rows   = (int)Mathf.Floor(source.height / tileSize.y);
            Color border = Color.black;

            switch (extrudeColor)
            {
            case TextureExtrude.White:
                border = Color.white;
                break;

            case TextureExtrude.Red:
                border = Color.red;
                break;

            case TextureExtrude.Green:
                border = Color.green;
                break;

            case TextureExtrude.Blue:
                border = Color.blue;
                break;

            case TextureExtrude.Yellow:
                border = Color.yellow;
                break;

            case TextureExtrude.Gray:
                border = Color.gray;
                break;

            default:
                border = Color.black;
                break;
            }
            Texture2D texture = new Texture2D((int)(cols * (tileSize.x + borderSize.x * 2f)), source.height, source.format, false);

            texture.filterMode = source.filterMode;
            for (int i = 0; i < cols; i++)
            {
                Color[] c1 = source.GetPixels((int)(i * tileSize.x), 0, 1, source.height);
                Color[] c2 = source.GetPixels((int)((i + 1) * tileSize.x - 1), 0, 1, source.height);
                // Format border pixels
                if (extrudeColor != TextureExtrude.Default && extrudeColor != TextureExtrude.Mirror)
                {
                    for (int index = 0; index < c1.Length; index++)
                    {
                        c1[index] = border;
                    }
                    for (int index = 0; index < c2.Length; index++)
                    {
                        c2[index] = border;
                    }
                }
                else if (extrudeColor == TextureExtrude.Mirror)
                {
                    // TODO: Mirror Edge Pixels
                }
                for (int j = 0; j < borderSize.x; j++)
                {
                    texture.SetPixels((int)(i * (tileSize.x + borderSize.x * 2) + j), 0, 1, source.height, c1);
                    texture.SetPixels((int)(i * (tileSize.x + borderSize.x * 2) + j + tileSize.x + borderSize.x), 0, 1, source.height, c2);
                }
                texture.SetPixels((int)(i * (tileSize.x + borderSize.x * 2) + borderSize.x), 0, (int)tileSize.x, source.height, source.GetPixels((int)(i * tileSize.x), 0, (int)tileSize.x, source.height));
            }

            Texture2D temp = texture;

            texture            = new Texture2D(temp.width, (int)(rows * (tileSize.y + borderSize.y * 2f)), source.format, false);
            texture.filterMode = source.filterMode;
            for (int i = 0; i < rows; i++)
            {
                Color [] c1 = temp.GetPixels(0, (int)(i * tileSize.y), temp.width, 1);
                Color [] c2 = temp.GetPixels(0, (int)((i + 1) * tileSize.y - 1), temp.width, 1);
                // Format border pixels
                if (extrudeColor != TextureExtrude.Default && extrudeColor != TextureExtrude.Mirror)
                {
                    for (int index = 0; index < c1.Length; index++)
                    {
                        c1[index] = border;
                    }
                    for (int index = 0; index < c2.Length; index++)
                    {
                        c2[index] = border;
                    }
                }
                else if (extrudeColor == TextureExtrude.Mirror)
                {
                    // TODO: Mirror Edge Pixels
                }
                for (int j = 0; j < borderSize.y; j++)
                {
                    texture.SetPixels(0, (int)(i * (tileSize.y + borderSize.y * 2) + j), temp.width, 1, c1);
                    texture.SetPixels(0, (int)(i * (tileSize.y + borderSize.y * 2) + j + tileSize.y + borderSize.y), temp.width, 1, c2);
                }
                texture.SetPixels(0, (int)(i * (tileSize.y + borderSize.y * 2) + borderSize.y), temp.width, (int)tileSize.y, temp.GetPixels(0, (int)(i * tileSize.y), temp.width, (int)tileSize.y));
            }
            texture.Apply();
            return(texture);
        }
Пример #2
0
        public static Texture2D Extrude(this Texture2D source, int padding, bool bilinearScaling = true, TextureExtrude extrudeColor = TextureExtrude.Default)
        {
            if (source == null)
            {
                return(null);
            }
            int     ratio      = padding * 2;
            int     width      = source.width - ratio;
            int     height     = source.height - ratio;
            Vector2 tileSize   = new Vector2(width, height);
            Vector2 borderSize = new Vector2(padding, padding);

            source.Scale(width, height, bilinearScaling);
            return(source.Extrude(tileSize, borderSize, extrudeColor));
        }