Java – libgdx changes the color of the texture at run time
In a game made with libgdx, I have a textureatlas in which I store all textureregions for player animation By default, the player has a blue T-shirt (for example)
Thank you.
Solution
I face the same problem of using the same texture to generate random color weapons
So I wrote this Basically, I made a pixel map of the texture you want to edit
Then all pixels are iterated, and some colors are checked during iteration, which are specific parts of the texture (I suggest using different grayscale because RGB is the same) then when it is on the pixels that need to change color, I use the color selector method to grab the colors of those pixel groups. The color selector method is basically random, get the colors from the prefabricated color array, and then change the specific pixel to a new color
/** * Requires a asset's 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,1); } else if (colorInt[0] == colorAffinity && colorInt[1] == colorAffinity && colorInt[2] == colorAffinity) { pixmap.setColor(tintAffinity); pixmap.fillRectangle(x,1); } else if (colorInt[0] == colorGrip && colorInt[1] == colorGrip && colorInt[2] == colorGrip) { pixmap.setColor(tintGrip); pixmap.fillRectangle(x,1); } else if (colorInt[0] == colorExtra && colorInt[1] == colorExtra && colorInt[2] == colorExtra) { pixmap.setColor(tintExtra); pixmap.fillRectangle(x,1); } } } tex = new Texture(pixmap); textureData.disposePixmap(); pixmap.dispose(); return tex; }
I hope it helps Please don't just copy and paste, try to rebuild it to meet your needs, otherwise you won't learn anything