5
如何從TextureRegion或Sprite創建Pixmap?我需要這個以改變一些像素的顏色,然後從Pixmap創建新的紋理(在加載屏幕期間)。libgdx TextureRegion to Pixmap
如何從TextureRegion或Sprite創建Pixmap?我需要這個以改變一些像素的顏色,然後從Pixmap創建新的紋理(在加載屏幕期間)。libgdx TextureRegion to Pixmap
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)
}
}
感謝,但據我所知'textureRegion.getTexture()'會返回整個textrure(圖集)? – 2015-04-04 22:18:09
是的,它確實如此。沒有其他辦法,因爲只有一個紋理(這是一個地圖集的全部要點)。您將不得不創建自己的較小的像素圖並複製區域的部分。 – noone 2015-04-04 22:30:19
我已經添加了一個小的代碼片段(未經測試),你可以做到這一點。 – noone 2015-04-04 22:34:20