13

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

Mickey Tin
  • 3,408
  • 10
  • 42
  • 71

2 Answers2

22
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)
    }
}
dStulle
  • 609
  • 5
  • 24
noone
  • 19,520
  • 5
  • 61
  • 76
  • thanks, but afaik `textureRegion.getTexture()` will return the whole texrure(atlas)? – Mickey Tin Apr 04 '15 at 22:18
  • 3
    Yes, it does exactly that. There is no other way, since there is only that one texture (that's the whole point of having an atlas). You will have to create your own smaller pixmaps and copy the parts of the regions. – noone Apr 04 '15 at 22:30
  • I've addad a small code snippet (untested) of how you could do that. – noone Apr 04 '15 at 22:34
  • > "I've addad a small code snippet (untested)" Tested, it works :) (ending with new Pixmap, and after that of course 'texture.getTextureData().dispose()' and 'pixmap.dispose'. Or am I wrong?) – adamsko Aug 05 '15 at 10:46
  • 1
    That depends on whether you still want to use the source texture afterwards for something else. If you don't want that, then you should dispose it. – noone Aug 05 '15 at 16:40
6

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;
}
dStulle
  • 609
  • 5
  • 24
  • 1
    Actually this method works fine and has that general touch to be put in some helper class. I like this way more than the manual copy of the accepted answer. Thank you. – Grisgram May 29 '20 at 05:51
  • 2
    ... that moment when you search for something and find your own answer -_- – dStulle Nov 30 '20 at 00:40