首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >libgdx TextureRegion到Pixmap

libgdx TextureRegion到Pixmap
EN

Stack Overflow用户
提问于 2015-04-04 21:52:33
回答 2查看 7.8K关注 0票数 12

如何从TextureRegion或Sprite创建Pixmap?我需要这一点,以改变一些像素的颜色,然后创建新的纹理从像素(在加载屏幕期间)。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-04-04 22:10:05

代码语言:javascript
复制
Texture texture = textureRegion.getTexture();
if (!texture.getTextureData().isPrepared()) {
    texture.getTextureData().prepare();
}
Pixmap pixmap = texture.getTextureData().consumePixmap();

如果您只想要该纹理的一部分(区域),则必须进行一些手动处理:

代码语言:javascript
复制
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)
    }
}
票数 22
EN

Stack Overflow用户

发布于 2019-06-18 17:42:10

如果不想按像素遍历TextureRegion像素,也可以将该区域绘制到新的Pixmap上。

代码语言:javascript
复制
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;
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29451787

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档