簡體   English   中英

Libgdx在運行時更改Texture的顏色

[英]Libgdx change color of Texture at runtime

在用libgdx制作的游戲中,我有一個TextureAtlas ,我在其中存儲了所有的TextureRegion用於我的Player Animation 默認情況下, Player有一個藍色T恤(例如)。 現在我希望能夠擁有一個以上的Player ,每個Player都應該擁有另一種T恤顏色。 所以基本上,我想用紅色來代替藍色為第二個Player ,並與綠色為第三Player等。 我相信我可以用PixMap做到這一點,但不會。 因為那時我失去了TextureAtlas (?)的優勢。 還有另一種方法嗎? 或者,我需要有充分的“色版”作為TextureRegionTextureAtlas

另一個小問題:
使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM