Jump to content
How to Install Plugins ×

Nine Slicer


Recommended Posts

This effect discards middle parts of the image to make it require less space. Main purpose of this effect is discard the unnecessary parts of the image to effectively use that image in sprite atlas. This way you can use the image with 9-sliced technique without any loss (such as used in Unity).

 

I was tired of manually doing this over and over again (you have to modify most of the game assets published in the internet to pack tightly in sprite atlas). And it is time consuming to make all the border size's consistent. Since Paint.NET stores last used effect parameters, you can make every sprite consistent (Same size as the previous sprite)

 

Preview

EffectPreview.png.9b54b9b1043a0dc6801f02a85b31b6f4.png

How to use

  1. Download the plugin NineSlicer.zip
  2. Extract the zip file
  3. Install into Paint.NET (Either double clicking "Install_NineSlicer.bat" or place "NineSlicer.dll" in "Effects" folder, by default it should be located at "Program Files/paint.net/Effects")
  4. Open Paint.NET and go into Effects->Object->Nine Slicer

 

Parameters

NineSlicerOptions.png.cfee20a2b78c2f7bdce14816c619764d.png

  • Left: Left border size
  • Left-Right Symmetry: If checked right border size will be equal to left border size
  • Right: Right border size (You have to uncheck Left-Right Symmetry to change this parameter)
  • Top: Top border size
  • Top-Bottom Symmetry: If checked bottom border size will be equal to top border size
  • Bottom: Bottom border size (You have to uncheck Top-Bottom Symmetry to change this parameter)

 

Source Code

License CC0 1.0 https://creativecommons.org/publicdomain/zero/1.0/deed.tr

 

Spoiler
#region UICode
IntSliderControl BorderLeft = 16; // [1,256] Left
CheckboxControl SymmetryX = true; // Left-Right Symmetry
IntSliderControl BorderRight = 16; // [1,256] {!SymmetryX} Right
IntSliderControl BorderTop = 16; // [1,256] Top
CheckboxControl SymmetryY = true; // Top-Bottom Symmetry
IntSliderControl BorderBottom = 16; // [1,256] {!SymmetryY} Bottom
#endregion

protected override void OnRender(IBitmapEffectOutput output)
{
    using IEffectInputBitmap<ColorBgra32> sourceBitmap = Environment.GetSourceBitmapBgra32();
    using IBitmapLock<ColorBgra32> sourceLock = sourceBitmap.Lock(new RectInt32(0, 0, sourceBitmap.Size));
    RegionPtr<ColorBgra32> sourceRegion = sourceLock.AsRegionPtr();

    RectInt32 outputBounds = output.Bounds;
    using IBitmapLock<ColorBgra32> outputLock = output.LockBgra32();
    RegionPtr<ColorBgra32> outputSubRegion = outputLock.AsRegionPtr();
    var outputRegion = outputSubRegion.OffsetView(-outputBounds.Location);

    // Delete any of these lines you don't need
    ColorBgra32 primaryColor = Environment.PrimaryColor;
    ColorBgra32 secondaryColor = Environment.SecondaryColor;
    var selection = Environment.Selection.RenderBounds;
    int selectionWidth = selection.Right - selection.Left;
    int selectionHeight = selection.Bottom - selection.Top;

    if(SymmetryX)
        BorderRight = BorderLeft;
    if(SymmetryY)
        BorderBottom = BorderTop;

    // Horizontal distance between borders
    int borderWidth = BorderLeft + BorderRight;
    int borderHeight = BorderTop + BorderBottom;
    int borderHorDistance = selectionWidth - borderWidth;
    int borderVerDistance = selectionHeight - borderHeight;

    if(borderHorDistance < 0)
        borderHorDistance = 0;
    if(borderVerDistance < 0)
        borderVerDistance = 0;

    // Loop through the output canvas tile
    for (int y = outputBounds.Top; y < outputBounds.Bottom; ++y)
    {
        if (IsCancelRequested) return;

        for (int x = outputBounds.Left; x < outputBounds.Right; ++x)
        {
            // Get your source pixel
            ColorBgra32 sourcePixel = sourceRegion[x,y];

            if(y >= selection.Top && y < selection.Bottom
            && x >= selection.Left && x < selection.Right)
            {
                int offsetY = 0;
                int offsetX = 0;

                int yDiff = y - selection.Top;
                int xDiff = x - selection.Left;
                // Check if we are in border area
                if(yDiff < borderHeight && xDiff < borderWidth)
                {
                    if(xDiff > BorderLeft)
                        offsetX = borderHorDistance;

                    if(yDiff > BorderTop)
                        offsetY = borderVerDistance;

                    sourcePixel = sourceRegion[x + offsetX, y + offsetY];
                }
                else
                {
                    sourcePixel = SrgbColors.Transparent;
                }
                
            }


            // Save your pixel to the output canvas
            outputRegion[x,y] = sourcePixel;
        }
    }
}

 

  • Upvote 5
Link to comment
Share on other sites

Welcome to the forum @XLIVE99 :)

 

This is an excellent write-up! I'm impressed

  • Thanks 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...