LibGDX Texture to ByteBuffer
我想将纹理转换为LibGDX中的Pixmap,以便我可以将所有像素数据转换为ByteBuffer进行一些实验,并且从我所知道的情况来看,我应该可以通过执行以下操作:LibGDX Texture to ByteBuffer
Pixmap pixmap = textureName.getTexture().getTextureData().consumePixmap();
ByteBuffer data = pixmap.getPixels();
这似乎并返回一个适当大小的像素图,和字节缓冲区似乎要创建得很好,但它是用零完全充满,造成一片空白,透明图像。这一点以及其他一些测试让我相信Pixmap本身只是被创建为一个完全透明的图像,但我无法找到可能导致这种情况发生的原因。是否有某种限制阻止这种工作,或者我只是错过了明显的东西?
我相信API(consumePixmap()
)是为Texture
类提取数据时从Pixmap
提取数据时将Pixmap推到GPU。
Libgdx Texture
对象表示GPU上的纹理数据,因此将底层数据提供给CPU通常不是微不足道的(它是渲染API的“错误”方向)。请参阅http://code.google.com/p/libgdx/wiki/GraphicsPixmap
此外,consumePixmap()
上的文档在它可以工作之前表示它requires a prepare()
call。
要从纹理中提取像素信息,您需要构建一个FrameBuffer
对象,为其渲染纹理,然后提取像素(请参阅ScreenUtils
)。
它不清楚你想完成什么,但走向另一个方向(字节数组 - >Pixmap
- >Texture
),然后修改字节数组并重新进行转换可能会奏效。
当我想要在我的关卡编辑器中创建一个关卡的PNG时,遇到了类似的问题。 级别由创建者放置的图块制作而成,然后将整个级别保存为.png以用于游戏。
解决了P.T.解释。我希望它对遇到同样问题的其他人有用。
而在你的应用程序配置中:cfg.useGL20 = true; GL20需要能够建立帧缓冲
public boolean exportLevel(){
//width and height in pixels
int width = (int)lba.getPrefWidth();
int height = (int)lba.getPrefHeight();
//Create a SpriteBatch to handle the drawing.
SpriteBatch sb = new SpriteBatch();
//Set the projection matrix for the SpriteBatch.
Matrix4 projectionMatrix = new Matrix4();
//because Pixmap has its origin on the topleft and everything else in LibGDX has the origin left bottom
//we flip the projection matrix on y and move it -height. So it will end up side up in the .png
projectionMatrix.setToOrtho2D(0, -height, width, height).scale(1,-1,1);
//Set the projection matrix on the SpriteBatch
sb.setProjectionMatrix(projectionMatrix);
//Create a frame buffer.
FrameBuffer fb = new FrameBuffer(Pixmap.Format.RGBA8888, width, height, false);
//Call begin(). So all next drawing will go to the new FrameBuffer.
fb.begin();
//Set up the SpriteBatch for drawing.
sb.begin();
//Draw all the tiles.
BuildTileActor[][] btada = lba.getTiles();
for(BuildTileActor[] btaa: btada){
for(BuildTileActor bta: btaa){
bta.drawTileOnly(sb);
}
}
//End drawing on the SpriteBatch. This will flush() any sprites remaining to be drawn as well.
sb.end();
//Then retrieve the Pixmap from the buffer.
Pixmap pm = ScreenUtils.getFrameBufferPixmap(0, 0, width, height);
//Close the FrameBuffer. Rendering will resume to the normal buffer.
fb.end();
//Save the pixmap as png to the disk.
FileHandle levelTexture = Gdx.files.local("levelTexture.png");
PixmapIO.writePNG(levelTexture, pm);
//Dispose of the resources.
fb.dispose();
sb.dispose();
注:我是新来LibGDX所以这可能不会是这样做的最巧妙的方法。
这正是我在找的东西。谢谢Toast。 – LukTar 2015-01-01 23:17:25
纹理是由用户在应用程序中创建的一个简单的类似油漆的界面,所以我手边没有任何数据。如果我可以从FrameBuffer中提取信息来创建完美的纹理,但我的印象是,您只能从绘制到屏幕的默认FrameBuffer中执行此操作。在我的情况下,纹理可能会超出屏幕的分辨率,所以它会切断或扭曲此路线上的图像。不幸的是,使用Pixmaps自己绘图也不是一种选择。 – Shamrock 2013-02-25 02:33:50