Palette swapping is in my game! Thanks @Pkeod !
(I was going to record a GIF of this in action, players joining and changing their colours, but Linux, specifically the KDE Plasma Desktop Environment, made an update which forces Wayland over X11 which means no screen recorders work and it’s times like this I wonder why I put up with Linux… anyway…)
I tried to replicate this in my GUI for the score bar (the palette coloured characters need to show up there) I thought it would work since I can specify the sprite material for the box node, and run the same function calls, but instead of getting coloured, the sprite just disappeared. Any ideas, anyone?
This is my GUI collection:
This sprite.material is the same one being used to render the palette swapped bunnies in the screenshot above, and same with the animal bunny.atlas They are both loaded into the GUI collection.
On this box GUI node in the same collection, I’ve specified that same atlas, and sprite material:
And here’s the relevant code in my gui.script - it’s the only script on the GUI because I know GUI’s can only have one script.
This code is identical to what’s in my palette script that’s successfully working in the screenshot, I’ve just changed the syntax to work with gui nodes instead of game objects:
local palettes = {
{
vmath.vector4(0.000000, 0.000000, 0.000000, 0.000000),
vmath.vector4(1.000000, 0.435294, 0.396078, 1.000000),
vmath.vector4(0.325490, 0.309804, 0.388235, 1.000000),
vmath.vector4(0.482353, 0.474510, 0.611765, 1.000000),
vmath.vector4(0.666667, 0.666667, 0.674510, 1.000000),
vmath.vector4(0.831373, 0.831373, 0.831373, 1.000000),
vmath.vector4(1.000000, 1.000000, 1.000000, 1.000000),
vmath.vector4(0.070588, 0.070588, 0.070588, 1.000000),
vmath.vector4(0.964706, 0.815686, 0.533333, 1.000000),
vmath.vector4(0.866667, 0.584314, 0.133333, 1.000000),
vmath.vector4(0.560784, 0.270588, 0.160784, 1.000000)
},
{
vmath.vector4(0.000000, 0.000000, 0.000000, 0.000000),
vmath.vector4(0.858824, 0.733333, 0.121569, 1.000000),
vmath.vector4(0.388235, 0.160784, 0.070588, 1.000000),
vmath.vector4(0.560784, 0.270588, 0.160784, 1.000000),
vmath.vector4(0.690196, 0.419608, 0.250980, 1.000000),
vmath.vector4(0.909804, 0.588235, 0.392157, 1.000000),
vmath.vector4(1.000000, 1.000000, 1.000000, 1.000000),
vmath.vector4(0.070588, 0.070588, 0.070588, 1.000000),
vmath.vector4(0.964706, 0.815686, 0.533333, 1.000000),
vmath.vector4(0.866667, 0.584314, 0.133333, 1.000000),
vmath.vector4(0.560784, 0.270588, 0.160784, 1.000000)
},
{
vmath.vector4(0.000000, 0.000000, 0.000000, 0.000000),
vmath.vector4(1.000000, 0.435294, 0.396078, 1.000000),
vmath.vector4(0.152941, 0.129412, 0.164706, 1.000000),
vmath.vector4(0.235294, 0.192157, 0.274510, 1.000000),
vmath.vector4(0.325490, 0.309804, 0.388235, 1.000000),
vmath.vector4(0.474510, 0.466667, 0.572549, 1.000000),
vmath.vector4(1.000000, 1.000000, 1.000000, 1.000000),
vmath.vector4(0.070588, 0.070588, 0.070588, 1.000000),
vmath.vector4(0.964706, 0.815686, 0.533333, 1.000000),
vmath.vector4(0.866667, 0.584314, 0.133333, 1.000000),
vmath.vector4(0.560784, 0.270588, 0.160784, 1.000000)
},
{
vmath.vector4(0.000000, 0.000000, 0.000000, 0.000000),
vmath.vector4(0.858824, 0.733333, 0.121569, 1.000000),
vmath.vector4(0.423529, 0.047059, 0.172549, 1.000000),
vmath.vector4(0.584314, 0.098039, 0.054902, 1.000000),
vmath.vector4(0.839216, 0.235294, 0.196078, 1.000000),
vmath.vector4(1.000000, 0.435294, 0.396078, 1.000000),
vmath.vector4(1.000000, 1.000000, 1.000000, 1.000000),
vmath.vector4(0.070588, 0.070588, 0.070588, 1.000000),
vmath.vector4(0.964706, 0.815686, 0.533333, 1.000000),
vmath.vector4(0.866667, 0.584314, 0.133333, 1.000000),
vmath.vector4(0.560784, 0.270588, 0.160784, 1.000000)
},
{
vmath.vector4(0.000000, 0.000000, 0.000000, 0.000000),
vmath.vector4(1.000000, 0.435294, 0.396078, 1.000000),
vmath.vector4(0.560784, 0.270588, 0.160784, 1.000000),
vmath.vector4(0.713725, 0.380392, 0.086275, 1.000000),
vmath.vector4(0.858824, 0.733333, 0.121569, 1.000000),
vmath.vector4(0.917647, 0.894118, 0.400000, 1.000000),
vmath.vector4(1.000000, 1.000000, 1.000000, 1.000000),
vmath.vector4(0.070588, 0.070588, 0.070588, 1.000000),
vmath.vector4(0.964706, 0.815686, 0.533333, 1.000000),
vmath.vector4(0.866667, 0.584314, 0.133333, 1.000000),
vmath.vector4(0.560784, 0.270588, 0.160784, 1.000000)
},
{
vmath.vector4(0.000000, 0.000000, 0.000000, 0.000000),
vmath.vector4(1.000000, 0.435294, 0.396078, 1.000000),
vmath.vector4(0.360784, 0.015686, 0.545098, 1.000000),
vmath.vector4(0.596078, 0.137255, 0.882353, 1.000000),
vmath.vector4(0.776471, 0.325490, 0.925490, 1.000000),
vmath.vector4(0.854902, 0.619608, 1.000000, 1.000000),
vmath.vector4(1.000000, 1.000000, 1.000000, 1.000000),
vmath.vector4(0.070588, 0.070588, 0.070588, 1.000000),
vmath.vector4(0.964706, 0.815686, 0.533333, 1.000000),
vmath.vector4(0.866667, 0.584314, 0.133333, 1.000000),
vmath.vector4(0.560784, 0.270588, 0.160784, 1.000000)
},
{
vmath.vector4(0.000000, 0.000000, 0.000000, 0.000000),
vmath.vector4(1.000000, 0.435294, 0.396078, 1.000000),
vmath.vector4(0.015686, 0.188235, 0.505882, 1.000000),
vmath.vector4(0.062745, 0.305882, 0.698039, 1.000000),
vmath.vector4(0.231373, 0.419608, 0.996078, 1.000000),
vmath.vector4(0.325490, 0.580392, 1.000000, 1.000000),
vmath.vector4(1.000000, 1.000000, 1.000000, 1.000000),
vmath.vector4(0.070588, 0.070588, 0.070588, 1.000000),
vmath.vector4(0.964706, 0.815686, 0.533333, 1.000000),
vmath.vector4(0.866667, 0.584314, 0.133333, 1.000000),
vmath.vector4(0.560784, 0.270588, 0.160784, 1.000000)
},
{
vmath.vector4(0.000000, 0.000000, 0.000000, 0.000000),
vmath.vector4(1.000000, 0.435294, 0.396078, 1.000000),
vmath.vector4(0.066667, 0.274510, 0.258824, 1.000000),
vmath.vector4(0.047059, 0.439216, 0.435294, 1.000000),
vmath.vector4(0.156863, 0.760784, 0.458824, 1.000000),
vmath.vector4(0.576471, 1.000000, 0.474510, 1.000000),
vmath.vector4(1.000000, 1.000000, 1.000000, 1.000000),
vmath.vector4(0.070588, 0.070588, 0.070588, 1.000000),
vmath.vector4(0.964706, 0.815686, 0.533333, 1.000000),
vmath.vector4(0.866667, 0.584314, 0.133333, 1.000000),
vmath.vector4(0.560784, 0.270588, 0.160784, 1.000000)
},
}
local function set_constant(url, constant_name, constant_table)
local node = gui.get_node(url)
for k,v in ipairs(constant_table) do
if k <= 32 then
gui.set(node, constant_name, v, {index=k})
else
print("Error! Trying to set value outside of 32 range.")
end
end
end
function init(self)
-- Add initialization code here
-- Learn more: https://defold.com/manuals/script/
-- Remove this function if not needed
menu.setup(msg.url())
banner.setup(msg.url())
self.cur_index = 1
set_constant("box", "palette", palettes[self.cur_index])
end
The set_constant function runs without errors, but the “box” bunny just doesn’t show up, it’s invisible.
If I remove the sprite material, so it renders with the default material, I do get the greyscale version of the bunny (and the set_constant errors saying there’s no “palette” property), so that proves it should be showing up:
So the palette swapping shader code is just not working in the GUI for some reason.