libgdx TextureRegion到Pixmap

Mic*_*Tin 6 pixmap libgdx

如何从TextureRegion或Sprite创建Pixmap?我需要这个来改变一些像素的颜色,然后从Pixmap创建新的纹理(在加载屏幕期间).

noo*_*one 16

Texture texture = textureRegion.getTexture();
if (!texture.getTextureData().isPrepared()) {
    texture.getTextureData().prepare();
}
Pixmap pixmap = texture.getTextureData().consumePixmap();
Run Code Online (Sandbox Code Playgroud)

如果你只想要那个纹理的一部分(区域),那么你将不得不做一些手工处理:

for (int x = 0; x < textureRegion.getRegionWidth(); x++) {
    for (int y = 0; y < textureRegion.getRegionHeight(); y++) {
        int colorInt = pixmap.getPixel(textureRegion.getRegionX() + x, textureRegion.getRegionY() + y);
        // you could now draw that color at (x, y) of another pixmap of the size (regionWidth, regionHeight)
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 是的,确实如此.没有别的办法,因为只有一个纹理(这就是拥有地图集的重点).您将必须创建自己的较小像素图并复制区域的部分. (3认同)

dSt*_*lle 6

如果您不想逐个TextureRegion像素地遍历,您也可以将区域绘制到新的区域上。Pixmap

public Pixmap extractPixmapFromTextureRegion(TextureRegion textureRegion) {
    TextureData textureData = textureRegion.getTexture().getTextureData()
    if (!textureData.isPrepared()) {
        textureData.prepare();
    }
    Pixmap pixmap = new Pixmap(
            textureRegion.getRegionWidth(),
            textureRegion.getRegionHeight(),
            textureData.getFormat()
    );
    pixmap.drawPixmap(
            textureData.consumePixmap(), // The other Pixmap
            0, // The target x-coordinate (top left corner)
            0, // The target y-coordinate (top left corner)
            textureRegion.getRegionX(), // The source x-coordinate (top left corner)
            textureRegion.getRegionY(), // The source y-coordinate (top left corner)
            textureRegion.getRegionWidth(), // The width of the area from the other Pixmap in pixels
            textureRegion.getRegionHeight() // The height of the area from the other Pixmap in pixels
    );
    return pixmap;
}
Run Code Online (Sandbox Code Playgroud)

  • ...当你寻找某些东西并找到自己的答案的那一刻 -_- (2认同)