public Texture2D GenerateAtlas(Texture2D [] sprites)
        {
            var area = size.x * size.y * sprites.Length;
            var sqrt = Mathf.Sqrt(area);

            cols = Mathf.CeilToInt(sqrt / size.x);
            rows = Mathf.CeilToInt(sqrt / size.y);

            var width  = cols * (size.x + padding * 2);
            var height = rows * (size.y + padding * 2);
            var atlas  = Texture2DUtil.CreateTransparentTexture(width, height);

            var index = 0;

            for (var row = 0; row < rows; row++)
            {
                for (var col = 0; col < cols; col++)
                {
                    if (index == sprites.Length)
                    {
                        break;
                    }

                    var sprite = sprites[index];
                    var rect   = new RectInt(col * (size.x + padding * 2) + padding,
                                             height - (row + 1) * (size.y + padding * 2) + padding,
                                             size.x,
                                             size.y);
                    CopyColors(sprite, atlas, rect);
                    index++;
                }
            }

            return(atlas);
        }
예제 #2
0
        private void GenerateAtlas(Texture2D sprite)
        {
            var spriteSizeW = Settings.tileSize.x + padding * 2;
            var spriteSizeH = Settings.tileSize.y + padding * 2;
            var cols        = sprite.width / Settings.tileSize.x;
            var rows        = sprite.height / Settings.tileSize.y;
            var width       = cols * spriteSizeW;
            var height      = rows * spriteSizeH;

            atlas = Texture2DUtil.CreateTransparentTexture(width, height);
            for (var row = 0; row < rows; row++)
            {
                for (var col = 0; col < cols; col++)
                {
                    RectInt from = new RectInt(col * Settings.tileSize.x,
                                               row * Settings.tileSize.y,
                                               Settings.tileSize.x,
                                               Settings.tileSize.y);
                    RectInt to = new RectInt(col * spriteSizeW + padding,
                                             row * spriteSizeH + padding,
                                             Settings.tileSize.x,
                                             Settings.tileSize.y);
                    CopyColors(sprite, atlas, from, to);
                    atlas.Apply();
                }
            }
        }
예제 #3
0
        public Texture2D GetTextureFromCel(CelChunk cel)
        {
            Texture2D texture = Texture2DUtil.CreateTransparentTexture(Header.Width, Header.Height);

            int i     = 0;
            int x     = 0;
            int y     = 0;
            int index = 0;

            int width  = Mathf.Min(cel.Width, Header.Width);
            int height = Mathf.Min(cel.Height, Header.Height);

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

            for (y = 0; y < height; y++)
            {
                for (x = 0; x < width; x++)
                {
                    i     = y * cel.Width + x;
                    index = (height - (y + 1)) * width + x;


                    colors[index] = cel.RawPixelData[i].GetColor();
                }
            }

            texture.SetPixels(cel.X, Header.Height - cel.Y - Mathf.Min(cel.Height, Header.Height), Mathf.Min(cel.Width, Header.Width), Mathf.Min(cel.Height, Header.Height), colors);
            texture.Apply();

            return(texture);
        }
예제 #4
0
        // replaces color to transparent
        public Texture2D GenerateAtlas(Texture2D[] sprites, out UnityEditor.AssetImporters.SpriteImportData[] spriteData, int cols, int rows,
                                       Color mask, bool baseTwo = true)
        {
            var spriteImportData = new List <UnityEditor.AssetImporters.SpriteImportData>();

            var width  = cols * spriteSize.x;
            var height = rows * spriteSize.y;

            if (baseTwo)
            {
                var baseTwoValue = CalculateNextBaseTwoValue(Math.Max(width, height));
                width  = baseTwoValue;
                height = baseTwoValue;
            }

            // blank transparent canvas
            var atlas = Texture2DUtil.CreateTransparentTexture(width, height);
            var index = 0;

            // step through each pixel
            for (int row = 0; row < rows; row++)
            {
                for (int col = 0; col < cols; col++)
                {
                    Rect spriteRect = new Rect(col * spriteSize.x, atlas.height - ((row + 1) * spriteSize.y),
                                               spriteSize.x, spriteSize.y);

                    // change pixel mask to transparent
                    Color[] pixelPallete = ReplaceMaskToTransparent(mask, sprites[index].GetPixels());
                    atlas.SetPixels((int)spriteRect.x, (int)spriteRect.y, (int)spriteRect.width,
                                    (int)spriteRect.height, pixelPallete);
                    atlas.Apply();

                    var importData = new UnityEditor.AssetImporters.SpriteImportData
                    {
                        rect   = spriteRect,
                        pivot  = textureSettings.spritePivot,
                        border = Vector4.zero,
                        name   = index.ToString()
                    };

                    spriteImportData.Add(importData);

                    index++;
                    if (index >= sprites.Length)
                    {
                        break;
                    }
                }

                if (index >= sprites.Length)
                {
                    break;
                }
            }

            spriteData = spriteImportData.ToArray();
            return(atlas);
        }
예제 #5
0
        public Texture2D GenerateAtlas(Texture2D[] sprites, out SpriteImportData[] spriteData, int cols, int rows, bool baseTwo = true)
        {
            var spriteImportData = new List <SpriteImportData>();

            var width  = cols * spriteSize.x;
            var height = rows * spriteSize.y;

            if (baseTwo)
            {
                var baseTwoValue = CalculateNextBaseTwoValue(Math.Max(width, height));
                width  = baseTwoValue;
                height = baseTwoValue;
            }


            var atlas = Texture2DUtil.CreateTransparentTexture(width, height);
            var index = 0;

            for (var row = 0; row < rows; row++)
            {
                for (var col = 0; col < cols; col++)
                {
                    Rect spriteRect = new Rect(col * spriteSize.x, atlas.height - ((row + 1) * spriteSize.y), spriteSize.x, spriteSize.y);
                    atlas.SetPixels((int)spriteRect.x, (int)spriteRect.y, (int)spriteRect.width, (int)spriteRect.height, sprites[index].GetPixels());
                    atlas.Apply();

                    var importData = new SpriteImportData
                    {
                        rect   = spriteRect,
                        pivot  = textureSettings.spritePivot,
                        border = Vector4.zero,
                        name   = index.ToString()
                    };

                    spriteImportData.Add(importData);

                    index++;
                    if (index >= sprites.Length)
                    {
                        break;
                    }
                }
                if (index >= sprites.Length)
                {
                    break;
                }
            }

            spriteData = spriteImportData.ToArray();
            return(atlas);
        }
예제 #6
0
        public Texture2D GetTextureFromCel(CelChunk cel)
        {
            int canvasWidth  = Header.Width;
            int canvasHeight = Header.Height;

            Texture2D texture = Texture2DUtil.CreateTransparentTexture(canvasWidth, canvasHeight);

            // Sometimes cell width/height can be larger than image (pixels stored off-screen), adjust our rect to fit canvas viewport
            //有些贴图中像素会超过画布范围,裁剪到画布内
            int celWidth  = Mathf.Min(canvasWidth - cel.X, cel.Width);
            int celHeight = Mathf.Min(canvasHeight - cel.Y, cel.Height);

            // Only need to render as large as viewport or cel, whichever is smaller
            int renderRectWidth  = Math.Min(canvasWidth, celWidth);
            int renderRectHeight = Math.Min(canvasHeight, celHeight);

            // If cel offset is positive, displace the same amount on our texture
            int destX = Mathf.Max(0, cel.X);
            //renderRectWidth = celWidth - destX;
            int destY = Mathf.Max(0, canvasHeight - cel.Height - cel.Y); // Aseprite is upper left origin, Unity textures are lower left, so perform flip

            Color[] colors = new Color[renderRectWidth * renderRectHeight];

            // If cell offset is negative, displace the same same amount on cel data
            int celX = Mathf.Max(0, -cel.X);
            int celY = Mathf.Max(0, -cel.Y);

            //Debug.Log(renderRectWidth + "," + renderRectHeight);
            for (int y = 0; y < renderRectHeight; y++)
            {
                for (int x = 0; x < renderRectWidth; x++)
                {
                    int celDataIndex = (y + celY) * cel.Width + (x + celX);
                    int index        = (renderRectHeight - 1 - (y)) * renderRectWidth + (x);

                    if (cel.RawPixelData.Length <= celDataIndex)
                    {
                        continue;
                    }

                    colors[index] = cel.RawPixelData[celDataIndex].GetColor();
                }
            }

            //Debug.Log(destX + "," + destY + " . " + renderRectWidth + "," + renderRectHeight);

            texture.SetPixels(destX, destY, renderRectWidth, renderRectHeight, colors);
            texture.Apply();

            return(texture);
        }
예제 #7
0
        public Texture2D GenerateAtlas(Texture2D[] sprites, int cols, int rows, out AseFileSpriteImportData[] spriteImportData, bool baseTwo = false)
        {
            spriteImportData = new AseFileSpriteImportData[sprites.Length];

            var width  = cols * spriteSize.x;
            var height = rows * spriteSize.y;

            if (baseTwo)
            {
                var baseTwoValue = CalculateNextBaseTwoValue(Math.Max(width, height));
                width  = baseTwoValue;
                height = baseTwoValue;
            }

            var atlas = Texture2DUtil.CreateTransparentTexture(width, height);
            var index = 0;

            for (var row = 0; row < rows; ++row)
            {
                for (var col = 0; col < cols; ++col)
                {
                    Rect    spriteRect = new Rect(col * spriteSize.x, atlas.height - ((row + 1) * spriteSize.y), spriteSize.x, spriteSize.y);
                    Color[] colors     = sprites[index].GetPixels();
                    atlas.SetPixels((int)spriteRect.x, (int)spriteRect.y, (int)spriteRect.width, (int)spriteRect.height, sprites[index].GetPixels());
                    atlas.Apply();

                    List <Vector2[]> outline = GenerateRectOutline(spriteRect);
                    spriteImportData[index] = CreateSpriteImportData(index.ToString(), spriteRect, outline);

                    index++;
                    if (index >= sprites.Length)
                    {
                        break;
                    }
                }
                if (index >= sprites.Length)
                {
                    break;
                }
            }

            return(atlas);
        }
예제 #8
0
        public Texture2D GetTextureFromCel(CelChunk cel)
        {
            int canvasWidth  = Header.Width;
            int canvasHeight = Header.Height;

            Texture2D texture = Texture2DUtil.CreateTransparentTexture(canvasWidth, canvasHeight);

            Color[] colors = new Color[canvasWidth * canvasHeight];

            int pixelIndex = 0;
            int celXEnd    = cel.Width + cel.X;
            int celYEnd    = cel.Height + cel.Y;


            for (int y = cel.Y; y < celYEnd; y++)
            {
                if (y < 0 || y >= canvasHeight)
                {
                    pixelIndex += cel.Width;
                    continue;
                }

                for (int x = cel.X; x < celXEnd; x++)
                {
                    if (x >= 0 && x < canvasWidth)
                    {
                        int index = (canvasHeight - 1 - y) * canvasWidth + x;
                        colors[index] = cel.RawPixelData[pixelIndex].GetColor();
                    }

                    ++pixelIndex;
                }
            }

            texture.SetPixels(0, 0, canvasWidth, canvasHeight, colors);
            texture.Apply();

            return(texture);
        }
예제 #9
0
        public Texture2D GetTextureAtlas()
        {
            Texture2D[] frames = this.GetFrames();

            Texture2D   atlas       = Texture2DUtil.CreateTransparentTexture(Header.Width * frames.Length, Header.Height);
            List <Rect> spriteRects = new List <Rect>();

            int col = 0;
            int row = 0;

            foreach (Texture2D frame in frames)
            {
                Rect spriteRect = new Rect(col * Header.Width, atlas.height - ((row + 1) * Header.Height), Header.Width, Header.Height);
                atlas.SetPixels((int)spriteRect.x, (int)spriteRect.y, (int)spriteRect.width, (int)spriteRect.height, frame.GetPixels());
                atlas.Apply();

                spriteRects.Add(spriteRect);

                col++;
            }

            return(atlas);
        }
예제 #10
0
        public Texture2D GetFrame(int index)
        {
            Frame frame = Frames[index];

            Texture2D texture = Texture2DUtil.CreateTransparentTexture(Header.Width, Header.Height);


            List <LayerChunk> layers = GetChunks <LayerChunk>();
            List <CelChunk>   cels   = frame.GetChunks <CelChunk>();

            cels.Sort((ca, cb) => ca.LayerIndex.CompareTo(cb.LayerIndex));

            for (int i = 0; i < cels.Count; i++)
            {
                LayerChunk layer = layers[cels[i].LayerIndex];

                LayerBlendMode blendMode = layer.BlendMode;
                float          opacity   = Mathf.Min(layer.Opacity / 255f, cels[i].Opacity / 255f);

                bool visibility = layer.Visible;


                LayerChunk parent = GetParentLayer(layer);
                while (parent != null)
                {
                    visibility &= parent.Visible;
                    if (visibility == false)
                    {
                        break;
                    }

                    parent = GetParentLayer(parent);
                }

                if (visibility == false || layer.LayerType == LayerType.Group)
                {
                    continue;
                }

                Texture2D celTex = GetTextureFromCel(cels[i]);

                switch (blendMode)
                {
                case LayerBlendMode.Normal: texture = Texture2DBlender.Normal(texture, celTex); break;

                case LayerBlendMode.Multiply: texture = Texture2DBlender.Multiply(texture, celTex, opacity); break;

                case LayerBlendMode.Screen: texture = Texture2DBlender.Screen(texture, celTex); break;

                case LayerBlendMode.Overlay: texture = Texture2DBlender.Overlay(texture, celTex); break;

                case LayerBlendMode.Darken: texture = Texture2DBlender.Darken(texture, celTex); break;

                case LayerBlendMode.Lighten: texture = Texture2DBlender.Lighten(texture, celTex); break;

                case LayerBlendMode.ColorDodge: texture = Texture2DBlender.ColorDodge(texture, celTex); break;

                case LayerBlendMode.ColorBurn: texture = Texture2DBlender.ColorBurn(texture, celTex); break;

                case LayerBlendMode.HardLight: texture = Texture2DBlender.HardLight(texture, celTex); break;

                case LayerBlendMode.SoftLight: texture = Texture2DBlender.SoftLight(texture, celTex); break;

                case LayerBlendMode.Difference: texture = Texture2DBlender.Difference(texture, celTex); break;

                case LayerBlendMode.Exclusion: texture = Texture2DBlender.Exclusion(texture, celTex); break;

                case LayerBlendMode.Hue: texture = Texture2DBlender.Hue(texture, celTex); break;

                case LayerBlendMode.Saturation: texture = Texture2DBlender.Saturation(texture, celTex); break;

                case LayerBlendMode.Color: texture = Texture2DBlender.Color(texture, celTex); break;

                case LayerBlendMode.Luminosity: texture = Texture2DBlender.Luminosity(texture, celTex); break;

                case LayerBlendMode.Addition: texture = Texture2DBlender.Addition(texture, celTex); break;

                case LayerBlendMode.Subtract: texture = Texture2DBlender.Subtract(texture, celTex); break;

                case LayerBlendMode.Divide: texture = Texture2DBlender.Divide(texture, celTex); break;
                }
            }



            return(texture);
        }
예제 #11
0
        public Texture2D GetTextureFromCel(CelChunk cel)
        {
            Texture2D texture = Texture2DUtil.CreateTransparentTexture(Header.Width, Header.Height);

            int i = 0;
            int x = 0;
            int y = 0;

            int destX   = 0;
            int destY   = 0;
            int offsetX = 0;
            int offsetY = 0;

            int index = 0;

            int width  = Mathf.Min(cel.Width, Header.Width);
            int height = Mathf.Min(cel.Height, Header.Height);

            bool overlappingX = cel.Width + cel.X > Header.Width;
            bool overlappingY = cel.Height + cel.Y > Header.Height;

            if (overlappingX)
            {
                width -= (cel.X + cel.Width) - Header.Width;
            }

            if (overlappingY)
            {
                height -= (cel.Y + cel.Height) - Header.Height;
            }

            if (cel.X < 0)
            {
                offsetX = cel.X * -1;
            }

            if (cel.Y < 0)
            {
                offsetY = cel.Y * -1;
            }


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


            for (y = offsetY; y < height; y++)
            {
                destX = 0;
                for (x = offsetX; x < width; x++)
                {
                    i     = y * cel.Width + x;
                    index = (height - (y + 1)) * width + x;

                    colors[index] = cel.RawPixelData[i].GetColor();
                    destX++;
                }

                destY++;
            }

            texture.SetPixels(cel.X + offsetX, Header.Height - (cel.Y + offsetY) - height, width, height, colors);
            texture.Apply();

            return(texture);
        }
예제 #12
0
        public Texture2D GetFrame(int index)
        {
            Frame frame = Frames[index];


            Texture2D texture = Texture2DUtil.CreateTransparentTexture(Header.Width, Header.Height);


            List <LayerChunk> layers = GetChunks <LayerChunk>();
            List <CelChunk>   cels   = frame.GetChunks <CelChunk>();

            cels.Sort((ca, cb) => ca.LayerIndex.CompareTo(cb.LayerIndex));

            for (int i = 0; i < cels.Count; i++)
            {
                LayerBlendMode blendMode = layers[cels[i].LayerIndex].BlendMode;
                float          opacity   = layers[cels[i].LayerIndex].Opacity / 255f;

                Texture2D celTex = GetTextureFromCel(cels[i]);

                switch (blendMode)
                {
                case LayerBlendMode.Normal: texture = Texture2DBlender.Normal(texture, celTex); break;

                case LayerBlendMode.Multiply: texture = Texture2DBlender.Multiply(texture, celTex, opacity); break;

                case LayerBlendMode.Screen: texture = Texture2DBlender.Screen(texture, celTex); break;

                case LayerBlendMode.Overlay: texture = Texture2DBlender.Overlay(texture, celTex); break;

                case LayerBlendMode.Darken: texture = Texture2DBlender.Darken(texture, celTex); break;

                case LayerBlendMode.Lighten: texture = Texture2DBlender.Lighten(texture, celTex); break;

                case LayerBlendMode.ColorDodge: texture = Texture2DBlender.ColorDodge(texture, celTex); break;

                case LayerBlendMode.ColorBurn: texture = Texture2DBlender.ColorBurn(texture, celTex); break;

                case LayerBlendMode.HardLight: texture = Texture2DBlender.HardLight(texture, celTex); break;

                case LayerBlendMode.SoftLight: texture = Texture2DBlender.SoftLight(texture, celTex); break;

                case LayerBlendMode.Difference: texture = Texture2DBlender.Difference(texture, celTex); break;

                case LayerBlendMode.Exclusion: texture = Texture2DBlender.Exclusion(texture, celTex); break;

                case LayerBlendMode.Hue: texture = Texture2DBlender.Hue(texture, celTex); break;

                case LayerBlendMode.Saturation: texture = Texture2DBlender.Saturation(texture, celTex); break;

                case LayerBlendMode.Color: texture = Texture2DBlender.Color(texture, celTex); break;

                case LayerBlendMode.Luminosity: texture = Texture2DBlender.Luminosity(texture, celTex); break;

                case LayerBlendMode.Addition: texture = Texture2DBlender.Addition(texture, celTex); break;

                case LayerBlendMode.Subtract: texture = Texture2DBlender.Subtract(texture, celTex); break;

                case LayerBlendMode.Divide: texture = Texture2DBlender.Divide(texture, celTex); break;
                }
            }



            return(texture);
        }