libgdx TextureRegion到Pixmap

时间:2015-04-04 21:52:34

标签: libgdx pixmap

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

2 个答案:

答案 0 :(得分:16)

Texture texture = textureRegion.getTexture();
if (!texture.getTextureData().isPrepared()) {
    texture.getTextureData().prepare();
}
Pixmap pixmap = texture.getTextureData().consumePixmap();

如果您只想要该纹理的一部分(区域),那么您必须进行一些手动处理:

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)
    }
}

答案 1 :(得分:0)

如果您不想逐像素浏览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;
}
相关问题