Libgdx changes texture color at runtime

in a game made using Libgdx i, there is a TextureAtlas in which I save all the TextureRegion for my Animation Player . Player by default has a blue t-shirt (for example).
Now I would like to have more than one Player , and each Player should have a different T-shirt color.
So I want to replace blue with red for the second Player , green for the third Player , etc.
I'm sure I can do this with PixMap , but will I not lose the advantage of TextureAtlas ?
Is there another way to do this? Or do I need each โ€œcolor versionโ€ to be a TextureRegion in a TextureAtlas ?
Another little question:
With Gimp (and possibly several other programs), you can use color indices for ".gif" files.
This reduces the size of all your Texture s by storing the index for each color in the file, and then using that index to describe the pixels. Therefore, for each red pixel you will have "1" instead of "# FF0000", and somewhere in the file you have "1 = # FF0000".
If you then pack the ".gif" files with the color indices inside the TextureAtlas , is the index lost and it restores the standard RGB colors or can it cause problems?

Thank you so much!

+7
java colors textures libgdx
source share
2 answers

I ran into the same problem for creating weapons with random colors using the same texture.

So I wrote this.
Basically I create a pixmap texture that you want to edit.

Then you iterate over all the pixels, iterations I check for specific colors that are part of a specific part of the texture. (I suggest using different shades of gray, since RGB is the same)

Then, when it is on the pixel where you want to change the color, I get the color for these groups of pixels using a color selection method that is mostly random, which gets the color from a pre-packaged array of colors,
and then changes that particular pixel to a new color.

 /** * Requires a asset textureName, and requires gray scale colors of the * parts * * @param texturename * @param colorBlade * @param colorEdge * @param colorAffinity * @param colorGrip * @return */ private static Texture genTexture(String texturename, int colorBlade, int colorEdge, int colorAffinity, int colorGrip, int colorExtra) { Texture tex = Game.res.getTexture(texturename); TextureData textureData = tex.getTextureData(); textureData.prepare(); Color tintBlade = chooseColor(mainColors); Color tintEdge = new Color(tintBlade.r + 0.1f, tintBlade.g + 0.1f, tintBlade.b + 0.1f, 1); Color tintAffinity = chooseColor(affinityColors); Color tintGrip; Color tintExtra = chooseColor(extraColors); boolean colorsAreSet = false; do { tintGrip = chooseColor(mainColors); if (tintAffinity != tintBlade && tintAffinity != tintGrip && tintGrip != tintBlade) { colorsAreSet = true; } } while (!colorsAreSet); Pixmap pixmap = tex.getTextureData().consumePixmap(); for (int y = 0; y < pixmap.getHeight(); y++) { for (int x = 0; x < pixmap.getWidth(); x++) { Color color = new Color(); Color.rgba8888ToColor(color, pixmap.getPixel(x, y)); int colorInt[] = getColorFromHex(color); if (colorInt[0] == colorBlade && colorInt[1] == colorBlade && colorInt[2] == colorBlade) { pixmap.setColor(tintBlade); pixmap.fillRectangle(x, y, 1, 1); } else if (colorInt[0] == colorEdge && colorInt[1] == colorEdge && colorInt[2] == colorEdge) { pixmap.setColor(tintEdge); pixmap.fillRectangle(x, y, 1, 1); } else if (colorInt[0] == colorAffinity && colorInt[1] == colorAffinity && colorInt[2] == colorAffinity) { pixmap.setColor(tintAffinity); pixmap.fillRectangle(x, y, 1, 1); } else if (colorInt[0] == colorGrip && colorInt[1] == colorGrip && colorInt[2] == colorGrip) { pixmap.setColor(tintGrip); pixmap.fillRectangle(x, y, 1, 1); } else if (colorInt[0] == colorExtra && colorInt[1] == colorExtra && colorInt[2] == colorExtra) { pixmap.setColor(tintExtra); pixmap.fillRectangle(x, y, 1, 1); } } } tex = new Texture(pixmap); textureData.disposePixmap(); pixmap.dispose(); return tex; } 

Hope this helps. Please do not just copy the paste, try rebuilding it according to your needs or you wonโ€™t know anything.

+3
source share

The way to do this is simply to set the maximum number of players and make t-shirts for everyone, and pack them with a texture atlas and add them to a single TextureRegion array.

Now you only need to switch between the indexes.

Another way to do it with batch.setColor

 void setColor(Color tint) void setColor(float color) void setColor(float r, float g, float b, float a) 

And just set the color of the sprite lot for each t-shirt that you draw, and after you draw all of them, return the sprite color to white again.

You can also do this with the Sprite class using the setColor function.

If your t-shirts are not large and your maximum player number is small, I would recommend the first method.

+2
source share

All Articles