libgdx TextureRegion to Pixmap

[亡魂溺海] 提交于 2019-12-10 13:23:38

问题


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


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/29451787/libgdx-textureregion-to-pixmap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!