[英]Libgdx change color of Texture at runtime
在用libgdx制作的游戏中,我有一个TextureAtlas
,我在其中存储了所有的TextureRegion
用于我的Player
Animation
。 默认情况下, Player
有一个蓝色T恤(例如)。 现在我希望能够拥有一个以上的Player
,每个Player
都应该拥有另一种T恤颜色。 所以基本上,我想用红色来代替蓝色为第二个Player
,并与绿色为第三Player
等。 我相信我可以用PixMap
做到这一点,但不会。 因为那时我失去了TextureAtlas
(?)的优势。 还有另一种方法吗? 或者,我需要有充分的“色版”作为TextureRegion
在TextureAtlas
?
另一个小问题:
使用Gimp(可能还有一些其他程序),您可以使用“.gif”文件的颜色索引。 通过为文件中的每种颜色保存索引,然后使用此索引描述像素,可以减小所有Texture
的大小。 因此,对于每个红色像素,您将拥有“1”而不是“#FF0000”,并且文件中的某处有“1 =#FF0000”。 如果我们然后在TextureAtlas
打包带有颜色索引的“.gif”文件,那么索引是否会丢失并恢复默认的RGB颜色或会产生问题?
非常感谢!
一种方法是设置最大数量的玩家,并为每个人制作T恤,并用纹理图集打包并将它们添加到一个TextureRegion数组中。
现在您只需要在索引之间切换。
另一种方法是使用batch.setColor
void setColor(Color tint)
void setColor(float color)
void setColor(float r, float g, float b, float a)
只需为您绘制的每个T恤设置精灵批次的颜色,并在绘制完所有T-Shirt后再将spritebatch颜色再次设置为白色。
您也可以使用带有setColor函数的Sprite类来完成此操作。
如果你的T恤没有很大的尺码,并且你的最大球员数量很小,我会推荐第一种方法。
我面临着使用相同纹理生成随机颜色武器的相同问题。
所以我写了这个。
基本上我制作了你想要编辑的纹理的像素图。
然后迭代所有像素,迭代时检查某些颜色,这些颜色是纹理的一部分特定部分。 (我建议使用不同的灰度,因为RGB是相同的)
然后,当它在需要改变颜色的像素上时,我使用颜色选择器方法抓取那些像素组的颜色,颜色选择器方法基本上是随机的,从预制的颜色阵列中获取颜色,
然后将该特定像素更改为新颜色。
/**
* 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;
}
我希望这有帮助。
请不要只是复制粘贴,尝试重建它以满足您的需求,否则您将无法学到任何东西。
Rose Blax做得非常好。 但是如果你想通过使用SpriteBatch或者那些有相同问题但又不想使用Pixmap的人来做到这一点...
当然我真的很喜欢使用Pixmap,但我已经看到“spritebatch / sprite.setcolor(r,g,b,a)”的用法给了一些挑战,因为改变所有的纹理颜色而不只是T恤......那是为什么我来到这里是的,可以用这种方式改变T恤。 你准备好了吗? :d
1 - 让部件想要绘制灰度或白色2 - 拆分部件会在纹理包中涂上或涂上不同的纹理:如果是T恤,你需要一个T恤不在你的字符中的包。 ..是的,只有头部,手臂和腿部的人。 t-short可以在同一包装中。 但是在此之间保持一个洞,以便将T恤放在未来。
它看起来像那样:
不需要害怕。 我知道图像已经分裂了整个字符,但是如果你只想改变T恤,那么你只会将它从身体的其他部分分开。
3 - 您将绘制它们,但不是相同的TextureRegion,您的代码将如下所示:
public void draw(SpriteBatch batch){
//First we draw the player with a hole in his body
batch.draw(playerA,0,0,75,100);
//Here we set the color of next texture to be drawn as RED.
batch.setColor(Color.RED);
//Now we draw the t-shirt of the player in same x and half y.
//That t-short has to be drawn in right position. That's why calc it.
batch.draw(tshirtA, 0,50,50);
//For don't affect others textures then make it white back.
batch.setColor(Color.WHITE);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.