Libgdx在运行时更改Texture的颜色 [英] Libgdx change color of Texture at runtime
问题描述
在游戏中,使用 Libgdx
我有一个 TextureAtlas
,其中我存储了所有 TextureRegion
s用于我的动画
的播放器
。 播放器
默认情况下有一个蓝色T恤(例如)。
现在我希望能够有一个以上播放器
,每个播放器
都应该有另一种T恤颜色。
基本上我想要更换第二个播放器
为蓝色,第三个播放器
为绿色,等等。
我相信我可以用 PixMap
来做到这一点,但是我不会失去 TextureAtlas
的优势吗?
还有其他可能吗?或者我需要在 TextureAtlas
中将每个颜色版本作为 TextureRegion
?
另一个小问题:
使用Gimp(可能还有一些其他程序),您可以使用.gif文件的颜色索引。
这会减少所有<$ b $的大小c $ c> Texture s,通过为文件中的每种颜色保存索引,然后使用此索引来描述像素。因此,对于每个红色像素,您将拥有1而不是#FF0000,并且文件中的某处有1 =#FF0000。
如果您再打包.gif文件使用 TextureAtlas
中的颜色索引,索引是否会丢失并恢复默认的RGB颜色或会产生问题?
in a game, made with Libgdx
i have a TextureAtlas
, in which i store all the TextureRegion
s for my Animation
s of the Player
. The Player
by default has a blue T-Shirt (for example).
Now i would like to be able to have more then one Player
, and every Player
should have another T-Shirt color.
So basicly i want to replace the blue with red for the second Player
, with green for the 3rd Player
and so on.
I am sure i can do this with PixMap
, but wouldn't i then loose the advantage of the TextureAtlas
?
Is there another possibility to do this? Or do i need to have every "color version" as a TextureRegion
in the TextureAtlas
?
Another little question:
With Gimp (and maybe a few other programs) you can use color indexes for ".gif" files.
This reduces the size of all your Texture
s, by saving an index for every color in the file and then using this index to describe the pixels. So for every red pixel you would have a "1" instead of "#FF0000" and somewhere in the file you have a "1=#FF0000".
If you then pack the ".gif" files with the color indexes inside a TextureAtlas
, is the index then lost and it restores the default RGB colors or will that give problems?
非常感谢!
推荐答案
我面临着使用相同纹理生成随机颜色武器的相同问题。
I faced the same Issue for generating weapon with random colors using the same texture.
所以我写了这个。
基本上我制作了你要编辑的纹理的像素图。
So I wrote this.
Basically I make a pixmap of the texture you want to edit.
然后迭代所有像素,迭代时检查某些颜色,这些颜色是纹理的一部分特定部分。 (我建议使用不同的灰色阴影,因为RGB是相同的)
然后当它在一个需要改变颜色的像素上时,我会使用一种颜色抓取那些像素组的颜色拾取器方法,它基本上是随机的,从预制的颜色数组中获取颜色,然后将该特定像素更改为新颜色。
Then you iterate over all of the pixels, while iterating I check for certain colors which are a part specific part of the texture. (I suggest using different shades of gray since the RGB is the same)
Then when it is on a pixel where the color needs to be changed I grab a color for those pixel groups using a color picker method which is basically random which gets a color from a prefabbed color array,
and then changes that specific pixel to the 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, 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;
}
我希望这会有所帮助。
请不要只需复制粘贴,尝试重建它以满足您的需求,否则您将无法学到任何东西。
I hope this helps.
Please don't just copy paste, try to rebuild this to suit your needs or you won't learn anything.
这篇关于Libgdx在运行时更改Texture的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!