Libgdx TextureRegion to Pixmap

How to create a Pixmap from TextureRegion or from Sprite? I need this to change the color of some pixels, and then create a new texture from the Pixmap (during the loading screen).

+6
source share
2 answers
Texture texture = textureRegion.getTexture(); if (!texture.getTextureData().isPrepared()) { texture.getTextureData().prepare(); } Pixmap pixmap = texture.getTextureData().consumePixmap(); 

If you need only part of this texture (region), you will 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) } } 
+16
source

If you don’t want to go through the TextureRegion pixel after pixel, you can also draw an area on the 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; } 
0
source

All Articles