libgdx TextureRegion to Pixmap

Mickey Tin picture Mickey Tin · Apr 4, 2015 · Viewed 7.1k times · Source

How do I create Pixmap from TextureRegion or from Sprite? I need this in order to change color of some pixels and then create new Texture from Pixmap (during loading screen).

Answer

noone picture noone · Apr 5, 2015
Texture texture = textureRegion.getTexture();
if (!texture.getTextureData().isPrepared()) {
    texture.getTextureData().prepare();
}
Pixmap pixmap = texture.getTextureData().consumePixmap();

If you want only a part of that texture (the region), then you'll have to do some manual processing:

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