Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

libgdx TextureRegion to Pixmap

Tags:

libgdx

pixmap

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

like image 230
Mickey Tin Avatar asked Apr 04 '15 21:04

Mickey Tin


1 Answers

If you don't want to walk through the TextureRegion Pixel by Pixel you also can draw the Region onto a new 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;
}
like image 143
dStulle Avatar answered Sep 25 '22 11:09

dStulle