Simple palette swap

C

Captaintoottoot

Guest
Is there a way to do a very simple palette swap? Like, just adding some code to an object's draw command to use a different image/shader for the color table? I'm trying to get player 2's units to use the same images as player 1 but use a different palette.

I've been looking at:

It just seems like a lot of moving parts for something simple.
 
pallete swapping with a shader is actually really easy.

Let me show you an example of a pallete swap fragment shader.

Code:
varying vec2 v_vTexcoord;
uniform sampler2D palette;
uniform float row;
void main() {
    vec4 base_color = texture2D( gm_BaseTexture, v_vTexcoord );
    gl_FragColor = vec4( texture2D( palette, vec2( base_color.r, row ) ).rgb, base_color.a );
}

That's it! It's very short and super efficient.

The idea here is to use the red channel (actually you can use any one of them) of the base color to index a color from the color palette.

This would require you to modify the red channel of your images so that they contain the color index for each pixel.
This is not very difficult, you could make a little program to do that automatically. It could be done at runtime, though I would suggest the image resources be converted. (keep backups of the original images).

There are other technical details which I won't go into for the moment.

...

Alternatively you could try using a color look up table shader, which is a little more complicated (only the shader itself actually), but has the advantage that you wouldn't need to modify any of your images in any way.
 
Last edited:
B

bojack29

Guest
I did. A trick you can use, that I did, is use a simple shader to modify certain pixels of a sprite when it's drawn. In my case, I simply made a man with a white shirt and passed the whole sprite into a shader. The shader targetted the white pixels of his shirt and swapped them with a different color that I specificed using 3 float uniforms. (RGB). That way only a single part of the sprite was colorized while the remainder remained untouched.
 

jonjons

Member
Code:
varying vec2 v_vTexcoord;
uniform sampler2D palette;
uniform float row;
void main() {
    vec4 base_color = texture2D( gm_BaseTexture, v_vTexcoord );
    gl_FragColor = vec4( texture2D( palette, vec2( base_color.r, row ) ).rgb, base_color.a );
}
Do anyone know how to apply this code to the object ?
i just get more confused watching the video...
 
  • Like
Reactions: JAG
Top