Libgdx在运行时更改Texture的颜色

在一个用Libgdx制作的游戏中我有一个TextureAtlas ,我在其中存储了所有的TextureRegion用于Player Animation TextureRegion 。 默认情况下, Player有一个蓝色T恤(例如)。
现在我希望能够拥有一个以上的Player ,每个Player都应该拥有另一种T恤颜色。
所以基本上我想用第二个Player用红色代替蓝色,用第三个Player代替绿色等等。
我相信我可以用PixMap做到这一点,但是我不会失去TextureAtlas的优势吗?
还有其他可能吗? 或者我需要在TextureAtlas每个“颜色版本”作为TextureAtlas
另一个小问题:
使用Gimp(可能还有一些其他程序),您可以使用“.gif”文件的颜色索引。
通过为文件中的每种颜色保存索引,然后使用此索引来描述像素,这会减小所有Texture的大小。 因此,对于每个红色像素,您将拥有“1”而不是“#FF0000”,并且文件中的某处有“1 =#FF0000”。
如果然后在TextureAtlas打包带有颜色索引的“.gif”文件,那么索引是否会丢失并恢复默认的RGB颜色或会产生问题?

非常感谢!

我面临着使用相同纹理生成随机颜色武器的相同问题。

所以我写了这个。
基本上我制作了你想要编辑的纹理的像素图。

然后迭代所有像素,迭代时检查某些颜色,这些颜色是纹理的一部分特定部分。 (我建议使用不同的灰度,因为RGB是相同的)

然后当它在需要改变颜色的像素上时,我使用颜色选择器方法抓取那些像素组的颜色,颜色选择器方法基本上是随机的,从预制的颜色arrays获得颜色,
然后将该特定像素更改为新颜色。

 /** * 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; } 

我希望这有帮助。
请不要只是复制粘贴,尝试重建它以满足您的需求,否则您将无法学到任何东西。

一种方法是设置最大数量的玩家,并为每个人制作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恤衫体积不是很大,而且你的最大球员数量很小,我会推荐第一种方法。

Rose Blax做得非常好。 但是如果你想通过使用SpriteBatch或者有相同问题却不想使用Pixmap的人来做到这一点…

当然我真的很喜欢使用Pixmap,但我已经看到“spritebatch / sprite.setcolor(r,g,b,a)”的用法给了一些挑战,因为改变所有的纹理颜色而不只是T恤……那是为什么我来到这里是的,可以用这种方式改变T恤。 你准备好了吗? :d

1 – 使部件想要绘制灰度或白色2 – 拆分部件将在纹理包或不同的纹理中绘制:如果是T恤,你需要一个T恤不在你的字符中的包。 ..是的,只有头部,arm和腿部的人。 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); }