Hello.
I'm making a platformer game and planning to use shared sprites for different enemies just by applying different palettes to them.
So... does anyone know how to perform a palette swap in LÖVE ?
Palette swap
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 11
- Joined: Sun Apr 06, 2014 4:43 pm
- Location: Novokuznetsk, Russia
Palette swap
Last edited by Daniel_Cortez on Mon Jun 01, 2015 7:52 am, edited 1 time in total.
↑↑↓↓↑↑↑↑
Re: Palette swap
If you're talking about changing the color of the enemy, use this code before drawing a sprite:
Where r, g, b, a and the red, greed, blue and alpha values that you want to apply to the sprite.
Code: Select all
love.graphics.setColor(r, g, b, a)
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
personal page and a raycaster
- zorg
- Party member
- Posts: 3468
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: Palette swap
Unfortunately i have had very little experience with shaders as of yet, but i think that they could be of use to you in this regard.
consider your enemy sprites having a set number of unique colors (rgb triplets), let's say 16. Then, you make a fragment shader (working on pixels) that gets an external parameter, a palette (selected out of many), mapping those 16 colors to 16 others. Calling this shader when you're drawing the enemy sprite will probably color it to what you want. If you have a variable for each monster, defining the index of which palette it should use, then sending those to the shader and drawing them like that, you can achieve what you want.
consider your enemy sprites having a set number of unique colors (rgb triplets), let's say 16. Then, you make a fragment shader (working on pixels) that gets an external parameter, a palette (selected out of many), mapping those 16 colors to 16 others. Calling this shader when you're drawing the enemy sprite will probably color it to what you want. If you have a variable for each monster, defining the index of which palette it should use, then sending those to the shader and drawing them like that, you can achieve what you want.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Re: Palette swap
Here's a shader:zorg wrote:Unfortunately i have had very little experience with shaders as of yet, but i think that they could be of use to you in this regard.
consider your enemy sprites having a set number of unique colors (rgb triplets), let's say 16. Then, you make a fragment shader (working on pixels) that gets an external parameter, a palette (selected out of many), mapping those 16 colors to 16 others. Calling this shader when you're drawing the enemy sprite will probably color it to what you want. If you have a variable for each monster, defining the index of which palette it should use, then sending those to the shader and drawing them like that, you can achieve what you want.
Code: Select all
#ifdef VERTEX
vec4 position( mat4 transform_projection, vec4 vertex_position )
{
return transform_projection * vertex_position; // default vertex shader
}
#endif
#ifdef PIXEL
extern vec4 palette_colors[2]; // size of color palette (2 colors)
vec4 effect( vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords ) {
return palette_colors[int(floor(VaryingTexCoord.x))]; // abuse u (texture x) coordinate as color index
}
#endif
]]
Code: Select all
function love.load()
shader = love.graphics.newShader(
[[
#ifdef VERTEX
vec4 position( mat4 transform_projection, vec4 vertex_position )
{
return transform_projection * vertex_position; // default vertex shader
}
#endif
#ifdef PIXEL
extern vec4 palette_colors[2]; // size of color palette (2 colors)
vec4 effect( vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords ) {
return palette_colors[int(floor(VaryingTexCoord.x))]; // abuse u (texture x) coordinate as color index
}
#endif
]] )
mesh = love.graphics.newMesh( {
{ -1, -1, 0 }, { 1, -1, 0 }, { 0, 1.4, 0 }, -- triangle #1 uses first color (0)
{ 3, 0, 1 }, { 5, -3, 1 }, { 2, 2, 1 } -- triangle #2 uses second color (1)
}, nil, "triangles" )
end
function love.draw()
love.graphics.setShader( shader )
love.graphics.translate( 150, 300 )
love.graphics.push()
love.graphics.scale( 50 )
shader:send( "palette_colors", { 255, 0, 0, 255 }, { 0, 255, 0, 255 } ) -- red and green
love.graphics.draw( mesh )
love.graphics.pop()
love.graphics.translate( 250, 0 )
love.graphics.scale( 50 )
shader:send( "palette_colors", { 255, 255, 255, 255 }, { 0, 0, 255, 255 } ) -- white and blue
love.graphics.draw( mesh )
end
- Attachments
-
- palette_shader.love
- (686 Bytes) Downloaded 300 times
- CaptainMaelstrom
- Party member
- Posts: 163
- Joined: Sat Jan 05, 2013 10:38 pm
Re: Palette swap
Hey S0III0s, that's pretty dope. Thanks for uploading.
I tried to figure out how to make a general purpose pallete swap shader earlier, but settled for color specific. Until now.
I tried to figure out how to make a general purpose pallete swap shader earlier, but settled for color specific. Until now.
Who is online
Users browsing this forum: Bing [Bot], Google Adsense [Bot] and 3 guests