I have a sprite with three layers and I need to merge them into one single layer in a way that:
In the resulting new layer One layer is used the red channel, second is for green and the third is for blue.
So that colors #120000 + #003400 + #000056 would make the final pixel be #123456
This is needed for craming three eight bit heightmaps into one for use in a shader to save resources.
This can be done without a script, but I would like to simplify the process to make it less easy to mess up and overwrite one layer’s colors completely (I did that a lot and it made debuging the shader itself a pain).
Im not familar enough on LUA so this script is partly copied without understanding the excat purpose of parts of it. And Im definetly not at all familar with Aseprite’s functions.
So here’s what I made so far:
if app.apiVersion < 1 then
return app.alert(“This script requires Aseprite v1.2.10-beta3”)
endlocal sprite = app.activeSprite
local frame = app.activeFramelocal currentLayer = app.activeLayer
local currentCel = currentLayer:cel(frame)
local img = currentCel.imagelocal l = sprite.layers[1]
local c = l:cel(frame)
local img_R = c.imagel = sprite.layers[2]
c = l:cel(frame)
local img_G = c.imagel = sprite.layers[3]
c = l:cel(frame)
local img_B = c.imagelocal img_w = sprite.width
local img_h = sprite.height
local colorMode = sprite.colorModelocal layer_output = sprite:newLayer()
layer_output.name = currentLayer.name .. “_READY”app.refresh()
local img_output = Image(img_w, img_h, colorMode)
for y = 0, img_h - 1, 1 do
for x = 0, img_w - 1, 1 do
– get pixel values
local r = Color(img_R:getPixel(x, y))
local g = Color(img_G:getPixel(x, y))
local b = Color(img_B:getPixel(x, y))
img_output:drawPixel(x, y, Color( math.max(r.red,r.green,r.blue),math.max(g.red,g.green,g.blue),math.max(b.red,b.green,b.blue),255))
end
endsprite:newCel(layer_output, frame, img_output, Point(0,0))
app.refresh()
There are at least two problems:
The layers are not in the same order that they are set on the editor and it seems to vary between sprites. Am I reading them from the wrong place? Im not sure if it matters what layer is currently selected, but it shouldn’t.
For some reason the layers when being read, do not line up with each other as they should and instead the resulting layer has their colors in wildly wrong positions. Everything seems to be end up piled into a mess in the top left corner of the sprite? Should I use something else than getPixel()?
What am I doing wrong?
