[LibGDX] Tint part of sprite

I’m making a strategy game. In the game, all players have the same units, differentiated by army color.
The problem is that I don’t want to make the same texture ten times with ten different colors; instead of that, I want to use ONE texture and tint the sprite by code.

I know that I can change the spriteBatch color or use sprite.setColor. However, that change the color of the whole image, and I don’t want that. The idea is to tint only a part of the sprite, like its t-shirt.
I’ve read about shaders, but I don’t know if they can provide a solution to my problem. Also, I have no idea of shaders xD

I’ve thought that a solution can be split my texture in two textures. One texture will have the immutable colors of my texture, and the other one will be in grayscale, and tinted using sprite.setColor. However I think this is not the best solution.

Ideas are welcome :slight_smile: Thanks in advance.

Make a few images for different parts of the sprite (Sleeves and body in T-shirt case) and draw 2 sprites with different colors.

I would do exactly that. It is simple and it works. Why bother looking for a more complicated solution.

[quote]I would do exactly that. It is simple and it works. Why bother looking for a more complicated solution.
[/quote]
The solution is not bad I think, however if there’s another solution with better perfomance (and not very difficult to implement) I would use it.

If I can’t find more options, I will simply do that. But I want to know what other things can I do.

Another way is to draw directly on/over your sprite, only certain pixels. The problem with this is that it gets pretty involved.

This is really easy problem if you can provide mask for a shader. Mask only need one bit so you could encode that to certain color. Coder pink aka red255 green0 blue 255 could be used. Then green channel could be intensity of tinted color.


if (color.r == 1.0 && color.g == 1.0)
  color.rgb = v_color * color.g;

+1 for shader. If you are not using the alpha channel for anything real, you can use that to flag the shader coloring, and even blend with the sprite rgb (so the color added by the shader is modulated and is not one flat color).

The problem is that I know NOTHING about shaders xD
I think they can be the most efficient solution but I don’t know how to use them. I’m reading some tutorials, like this, http://www.alcove-games.com/opengl-es-2-tutorials/lightmap-shader-fire-effect-glsl/ and somethings here in jgo, but I don’t understand the shader code u.u

If you know a very very basic tutorial or you can post some basic code I would appreciate it.
Thanks for the recommendations. :slight_smile:

EDIT: another problem is that using that method I wouldn’t be able to manage shadows. If I substitute (255,0,255) by another color, how I get shadows? Then using an alpha trick would be a solution, but looks like I will have to do it completely manual…

Like in the code I posted you use green channel as intesity.