Last active
February 1, 2020 08:40
-
-
Save ruccho/0e8340319357bd736488ec4d3edd35f2 to your computer and use it in GitHub Desktop.
A Lua script for Aseprite that shows auto tiling preview for FangAutoTile ( https://assetstore.unity.com/packages/tools/sprite-management/fang-auto-tile-132602 ).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
---------------------------------------------------------------------- | |
-- Auto tiling preview for FangAutoTile (https://assetstore.unity.com/packages/tools/sprite-management/fang-auto-tile-132602). | |
-- | |
-- It works for all color modes (RGB/GRAY/INDEXED). | |
---------------------------------------------------------------------- | |
if app.apiVersion < 1 then | |
return app.alert("This script requires Aseprite v1.2.10-beta3") | |
end | |
local cel = app.activeCel | |
if not cel then | |
return app.alert("There is no active image") | |
end | |
if cel.sprite.width % 2 ~= 0 then | |
return app.alert("Invalid size. Width must be even.") | |
end | |
if cel.sprite.width * 5 ~= cel.sprite.height then | |
return app.alert("Invalid size. Height must be 5 times the width.") | |
end | |
local tileSize = cel.sprite.width | |
local partSize = tileSize / 2 | |
-- setup sprite pattern table | |
local pattern = | |
{ | |
1, 1, 1, 1, 0, 0, 0, 0, 1, 3, 1, 3, 3, 1, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 2, 4, 3, 3, 3, 3, 3, 1, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 2, 5, 3, 1, 5, 2, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 3, 4, 4, 4, 4, 3, 1, 3, 1, 0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0, 1, 3, 1, 3, 4, 5, 4, 5, 5, 4, 5, 4, 3, 1, 3, 1, | |
1, 1, 2, 2, 0, 0, 0, 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3, 5, 5, 3, 3, 5, 2, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 5, 1, 3, 5, 2, 3, 1, 0, 0, 0, 0 | |
} | |
local horizontalTilesNumber = 17 | |
local verticalTilesNumber = #pattern / (horizontalTilesNumber * 4) | |
-- prepare source sprite | |
local sourceFrameNumber = cel.frameNumber | |
local sourceSprite = Sprite(cel.sprite) | |
sourceSprite:flatten() | |
local sourceCel = sourceSprite.layers[1]:cel(sourceFrameNumber) | |
local sourceImage = sourceCel.image | |
local sourceCelBounds = sourceCel.bounds | |
-- prepare destination sprite | |
local destinationSprite = Sprite(tileSize * horizontalTilesNumber, tileSize * verticalTilesNumber, sourceSprite.colorMode) | |
local destinationCel = destinationSprite.cels[1] | |
local destination = destinationCel.image | |
-- copy palette | |
local palette = Palette(sourceSprite.palettes[1]) | |
destinationSprite:setPalette(palette) | |
-- draw | |
-- tile loop | |
for tileY=1, verticalTilesNumber do | |
for tileX=1, horizontalTilesNumber do | |
-- inner tile loop | |
local i = 0 | |
for innerY=1, 2 do | |
for innerX=1, 2 do | |
-- index of pattern table | |
local index = (tileY - 1) * horizontalTilesNumber * 4 + (tileX - 1) * 4 + i | |
-- part id of target tile | |
local partIndex = pattern[index + 1] | |
-- origin position of destination tile | |
local destX = (tileX - 1) * tileSize + (innerX - 1) * partSize | |
local destY = (tileY - 1) * tileSize + (innerY - 1) * partSize | |
-- origin position of source tile | |
local sourceX = (innerX - 1) * partSize - sourceCelBounds.x | |
local sourceY = (partIndex - 1) * tileSize + (innerY - 1) * partSize - sourceCelBounds.y | |
-- copy from source sprite to destination | |
local pix = 0 | |
for it in destination:pixels(Rectangle(destX, destY, partSize, partSize)) do | |
local sourcePixelX = sourceX + (pix % partSize) | |
local sourcePixelY = sourceY + math.floor(pix / partSize) | |
if sourcePixelX >= 0 and sourcePixelY >= 0 and sourcePixelX < sourceCelBounds.width and sourcePixelY < sourceCelBounds.height then | |
it(sourceImage:getPixel(sourcePixelX, sourcePixelY)) | |
end | |
pix = pix + 1 | |
end | |
i = i + 1 | |
end | |
end | |
end | |
end | |
sourceSprite:close() | |
app.refresh() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment