Libgdx在运行时更改Texture的颜色

问题描述 投票:8回答:3

在用libgdx制作的游戏中,我有一个TextureAtlas,其中我存储了所有TextureRegions为AnimationPlayers。 Player默认有蓝色T恤(例如)。现在我希望能够有一个以上的Player,每个都应该有另一种T恤颜色。基本上,我想用第二个Player用红色代替蓝色,用第三个Player代替绿色等等。我相信我可以用PixMap这样做,但不会。因为那时我失去了TextureAtlas(?)的优势。还有另一种方法吗?或者我需要在TextureRegion中将每个“颜色版本”作为TextureAtlas吗?

另一个小问题: 使用Gimp(可能还有一些其他程序),您可以使用“.gif”文件的颜色索引。这会通过为文件中的每种颜色保存索引然后使用此索引来描述像素来减小所有Textures的大小。因此,对于每个红色像素,您将拥有“1”而不是“#FF0000”,并且文件中的某处有“1 =#FF0000”。如果然后我们将带有颜色索引的“.gif”文件打包在TextureAtlas中,那么索引会丢失并恢复默认的RGB颜色或会产生问题吗?

非常感谢!

java colors libgdx textures
3个回答
3
投票

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

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

然后迭代所有像素,迭代时我检查某些颜色,这些颜色是纹理的一部分特定部分。 (我建议使用不同的灰度,因为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;
}

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


3
投票

一种方法是设置最大数量的玩家,并为每个人制作T恤,并用纹理图集打包并将它们添加到一个TextureRegion数组中。

现在您只需要在索引之间切换。

另一种方法是使用batch.setColor

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

只需为您绘制的每个T恤设置精灵批次的颜色,并在绘制完所有的T恤后,再将spritebatch颜色设置为白色。

您也可以使用带有setColor函数的Sprite类来完成此操作。

如果你的T恤没有很大的尺码,并且你的最大球员数量很小,我会推荐第一种方法。


0
投票

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

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

1 - 使部件想要绘制灰度或白色2 - 拆分部件将在纹理包中绘制或使用不同的纹理:如果是T恤,则需要一个T恤不在您的字符中的包。 ..是的,只有头部,手臂和腿部的人。 t-short可以在同一包装中。但是在此之间保持一个洞,以便将T恤放在未来。

它看起来像那样:

Check out this asset

不需要害怕。我知道图像已经分裂了整个角色,但是如果你只想改变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);
    }
© www.soinside.com 2019 - 2024. All rights reserved.