如何从TextureRegion或Sprite创建Pixmap?我需要这一点,以改变一些像素的颜色,然后创建新的纹理从像素(在加载屏幕期间)。
发布于 2015-04-04 22:10:05
Texture texture = textureRegion.getTexture();
if (!texture.getTextureData().isPrepared()) {
texture.getTextureData().prepare();
}
Pixmap pixmap = texture.getTextureData().consumePixmap();如果您只想要该纹理的一部分(区域),则必须进行一些手动处理:
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)
}
}发布于 2019-06-18 17:42:10
如果不想按像素遍历TextureRegion像素,也可以将该区域绘制到新的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
复制相似问题