Beispiel #1
0
    public SpPixels GetSubset(int x, int y, int w, int h)
    {
        if (w < 0)
        {
            w  = -w;
            x -= w;
        }

        if (h < 0)
        {
            h  = -h;
            y -= h;
        }

        var o = new SpPixels(w, h);

        for (var oy = 0; oy < h; oy++)
        {
            for (var ox = 0; ox < w; ox++)
            {
                o.SetPixel(ox, oy, GetPixel(ox + x, oy + y));
            }
        }

        return(o);
    }
    private static List <SpRect> CompileSources(List <SpSource> sources)
    {
        var rects = new List <SpRect>();

        for (var i = sources.Count - 1; i >= 0; i--)
        {
            var source        = sources[i];
            var sourceTexture = source.Texture;

            if (sourceTexture != null)
            {
                var sourcePath     = source.Path;
                var sourcePixels   = default(SpPixels);
                var sourceSprites  = source.Sprites;
                var sourceImporter = SpHelper.GetAssetImporter <TextureImporter>(sourcePath);

                if (sourceImporter != null)
                {
                    // Make the texture temporarily readable, or directly read pixels
                    if (sourceImporter.isReadable == false)
                    {
                        sourceImporter.isReadable = true; SpHelper.ReimportAsset(sourcePath);
                        {
                            sourcePixels = new SpPixels(sourceTexture);
                        }
                        sourceImporter.isReadable = false; SpHelper.ReimportAsset(sourcePath);
                    }
                    else
                    {
                        sourcePixels = new SpPixels(sourceTexture);
                    }

                    // Add sprites or whole texture
                    if (sourceSprites.Count > 0)
                    {
                        for (var j = 0; j < sourceSprites.Count; j++)
                        {
                            var sourceSprite = sourceSprites[j];

                            CompileRect(rects, source, sourcePixels.GetSubset(sourceSprite.rect), sourceSprite.name, sourceSprite);
                        }
                    }
                    else
                    {
                        CompileRect(rects, source, sourcePixels, sourceTexture.name);
                    }

                    continue;
                }
            }

            sources.RemoveAt(i);
        }

        rects.Sort((a, b) => Mathf.Max(b.W, b.H) - Mathf.Max(a.W, a.H));

        return(rects);
    }
Beispiel #3
0
 public void SetPixelsClamp(int x, int y, SpPixels s)
 {
     for (var sy = 0; sy < s.height; sy++)
     {
         for (var sx = 0; sx < s.width; sx++)
         {
             SetPixelClamp(x + sx, y + sy, s.GetPixelClamp(sx, sy));
         }
     }
 }
Beispiel #4
0
    public SpPixels GetFlippedVertically()
    {
        var o = new SpPixels(width, height);

        for (var y = 0; y < height; y++)
        {
            for (var x = 0; x < width; x++)
            {
                o.SetPixel(x, height - y - 1, GetPixel(x, y));
            }
        }

        return(o);
    }
Beispiel #5
0
    public SpPixels GetFlippedHorizontally()
    {
        var o = new SpPixels(width, height);

        for (var y = 0; y < height; y++)
        {
            for (var x = 0; x < width; x++)
            {
                o.SetPixel(width - x - 1, y, GetPixel(x, y));
            }
        }

        return(o);
    }
Beispiel #6
0
    public SpPixels GetRotated270()
    {
        var o = new SpPixels(height, width);

        for (var y = 0; y < height; y++)
        {
            for (var x = 0; x < width; x++)
            {
                o.SetPixel(height - y - 1, x, GetPixel(x, y));
            }
        }

        return(o);
    }
Beispiel #7
0
    public void Update()
    {
        var newWidth  = Width;
        var newHeight = Height;
        var newRects  = default(List <SpRect>);

        // Remove deleted textures
        Sources.RemoveAll(s => s.Flag == SpFlag.MarkedForDestruction);

        EditorUtility.DisplayProgressBar("Updating " + name, "Packing...", 0.25f);

        // Try to pack
        if (SpPacker.AutoPack(Sources, ForceSquare, ref newWidth, ref newHeight, ref newRects) == true)
        {
            EditorUtility.DisplayProgressBar("Updating " + name, "Pasting...", 0.5f);

            var pixels    = new SpPixels(newWidth, newHeight);
            var metaDatas = new SpriteMetaData[newRects.Count];

            for (var i = newRects.Count - 1; i >= 0; i--)
            {
                var rect     = newRects[i];
                var metaData = default(SpriteMetaData);

                rect.PasteInto(pixels);

                metaData.name      = rect.Name;
                metaData.rect      = rect.Rect;
                metaData.pivot     = rect.Pivot;
                metaData.border    = rect.Border;
                metaData.alignment = (int)SpriteAlignment.Custom;

                metaDatas[i] = metaData;
            }

            EditorUtility.SetDirty(this);

            EditorUtility.DisplayProgressBar("Updating " + name, "Reimporting...", 0.75f);

            UpdateTextureAsset(metaDatas, pixels.Apply());
        }
        else
        {
            Debug.LogError("Failed to pack atlas, because the source textures are too large!");
        }

        EditorUtility.ClearProgressBar();
    }
Beispiel #8
0
    public SpPixels GetRotated90()
    {
        var o = new SpPixels(height, width);

        for (var y = 0; y < height; y++)
        {
            for (var x = 0; x < width; x++)
            {
                var c = GetPixel(x, y);

                o.SetPixel(y, width - x - 1, c);
            }
        }

        return(o);
    }
Beispiel #9
0
    public void Trim()
    {
        if (Source.Trim == true && Source.PadStyle == SpPadStyle.Transparent)
        {
            var sourceRect  = new Rect(0.0f, 0.0f, Pixels.Width, Pixels.Height);
            var trimmedRect = default(Rect);
            var pivotX      = Pivot.x * sourceRect.width;
            var pivotY      = Pivot.y * sourceRect.height;

            Pixels = Pixels.GetTrimmed(ref trimmedRect, ref Border);

            pivotX = SpHelper.Divide(pivotX - trimmedRect.xMin, trimmedRect.width);
            pivotY = SpHelper.Divide(pivotY - trimmedRect.yMin, trimmedRect.height);

            Pivot = new Vector2(pivotX, pivotY);
        }

        W = Pixels.Width + Source.PadSize * 2;
        H = Pixels.Height + Source.PadSize * 2;
    }
    private static void CompileRect(List <SpRect> rects, SpSource source, SpPixels pixels, string name, Sprite sprite = null)
    {
        var newRect = new SpRect();

        newRect.Name   = name;
        newRect.Source = source;
        newRect.Pixels = pixels;

        // Read pivot and border from sprite
        if (sprite != null)
        {
            newRect.Pivot  = SpHelper.GetSpritePivot(sprite);
            newRect.Border = sprite.border;
        }
        // Use default pivot and border settings
        else
        {
            newRect.Pivot  = new Vector2(0.5f, 0.5f);
            newRect.Border = new Vector4(0.0f, 0.0f, 0.0f, 0.0f);
        }

        // Override the pivot?
        if (source.UseCustomPivot == true)
        {
            newRect.Pivot = source.CustomPivot;
        }

        // Override the border?
        if (source.UseCustomBorder == true)
        {
            newRect.Border = source.CustomBorder;
        }

        newRect.Trim();

        rects.Add(newRect);
    }
Beispiel #11
0
 public void PasteInto(SpPixels atlas)
 {
     atlas.SetPixels(X, Y, ExpandedPixels);
 }