Libgdx 使用 findRegion 从 Texture Atlas 获取纹理
Libgdx get texture from Texture Atlas with findRegion
我有这个代码
textureAtlas = TextureAtlas("atlas.atlas")
val box = textureAtlas.findRegion("box")
我想用 "box" 创建一个纹理。可能吗? box.texture return 原始纹理,不是区域化的。哦,我不想使用 Sprite 和 SpriteBatch。我需要 3D,而不是 2D。
谢谢
你可以这样做:
Texture boxTexture = new TextureRegion(textureAtlas.findRegion("box")).getTexture();
TextureAtlas 实际上并没有分块。当你从 atlas 中获取区域时,它只是说这是你要使用的区域 (u,v,u2,v2),这是对整个纹理的原始引用。
这就是为什么 batch.draw(Texture) 和 batch.draw(TextureRegion) 在使用上不一样。
不过可以把图片的一部分作为贴图。
你可以用pixmap来做。
首先从图集纹理生成像素图。然后创建新的空像素图,大小为您想要的 "box" 区域。然后分配像素阵列并从新的像素图中生成纹理。
由于您的 Textureatlas 大小,它可能非常昂贵。
- 您可以使用帧缓冲区。
创建 FBbuilder 并将新帧 buffer.Draw 纹理区域构建到此缓冲区并从中获取纹理。
这里的问题是纹理的大小将与 viewport/screen sizes.I 猜想您可以创建新的相机以将其更改为您想要的大小。
GLFrameBuffer.FrameBufferBuilder frameBufferBuilder = new GLFrameBuffer.FrameBufferBuilder(widthofBox, heightofBox);
frameBufferBuilder.addColorTextureAttachment(GL30.GL_RGBA8, GL30.GL_RGBA, GL30.GL_UNSIGNED_BYTE);
frameBuffer = frameBufferBuilder.build();
OrthographicCamera c = new OrthographicCamera(widthofBox, heightofBox);
c.up.set(0, 1, 0);
c.direction.set(0, 0, -1);
c.position.set(widthofBox / 2, heightofBox / 2, 0f);
c.update();
batch.setProjectionMatrix(c.combined);
frameBuffer.begin();
batch.begin();
batch.draw(boxregion...)
batch.end();
frameBuffer.end();
Texture texturefbo = frameBuffer.getColorBufferTexture();
Texturefbo 将被翻转。您可以通过将 scaleY 设置为 -1 来使用纹理绘制方法解决此问题,或者您可以在帧缓冲区上绘制时将 scaleY 缩放为 -1 或者可以像这样更改相机
up.set(0, -1, 0);
direction.set(0, 0, 1);
在 y 轴上翻转到相机。
我最后想到的是 mipmap 这个 texture.Its 也不是那么难。
texturefbo.bind();
Gdx.gl.glGenerateMipmap(GL20.GL_TEXTURE_2D);
texturefbo.setFilter(Texture.TextureFilter.MipMapLinearLinear,
Texture.TextureFilter.MipMapLinearLinear);
我有这个代码
textureAtlas = TextureAtlas("atlas.atlas")
val box = textureAtlas.findRegion("box")
我想用 "box" 创建一个纹理。可能吗? box.texture return 原始纹理,不是区域化的。哦,我不想使用 Sprite 和 SpriteBatch。我需要 3D,而不是 2D。
谢谢
你可以这样做:
Texture boxTexture = new TextureRegion(textureAtlas.findRegion("box")).getTexture();
TextureAtlas 实际上并没有分块。当你从 atlas 中获取区域时,它只是说这是你要使用的区域 (u,v,u2,v2),这是对整个纹理的原始引用。
这就是为什么 batch.draw(Texture) 和 batch.draw(TextureRegion) 在使用上不一样。
不过可以把图片的一部分作为贴图。
你可以用pixmap来做。
首先从图集纹理生成像素图。然后创建新的空像素图,大小为您想要的 "box" 区域。然后分配像素阵列并从新的像素图中生成纹理。
由于您的 Textureatlas 大小,它可能非常昂贵。
- 您可以使用帧缓冲区。 创建 FBbuilder 并将新帧 buffer.Draw 纹理区域构建到此缓冲区并从中获取纹理。
这里的问题是纹理的大小将与 viewport/screen sizes.I 猜想您可以创建新的相机以将其更改为您想要的大小。
GLFrameBuffer.FrameBufferBuilder frameBufferBuilder = new GLFrameBuffer.FrameBufferBuilder(widthofBox, heightofBox);
frameBufferBuilder.addColorTextureAttachment(GL30.GL_RGBA8, GL30.GL_RGBA, GL30.GL_UNSIGNED_BYTE);
frameBuffer = frameBufferBuilder.build();
OrthographicCamera c = new OrthographicCamera(widthofBox, heightofBox);
c.up.set(0, 1, 0);
c.direction.set(0, 0, -1);
c.position.set(widthofBox / 2, heightofBox / 2, 0f);
c.update();
batch.setProjectionMatrix(c.combined);
frameBuffer.begin();
batch.begin();
batch.draw(boxregion...)
batch.end();
frameBuffer.end();
Texture texturefbo = frameBuffer.getColorBufferTexture();
Texturefbo 将被翻转。您可以通过将 scaleY 设置为 -1 来使用纹理绘制方法解决此问题,或者您可以在帧缓冲区上绘制时将 scaleY 缩放为 -1 或者可以像这样更改相机
up.set(0, -1, 0);
direction.set(0, 0, 1);
在 y 轴上翻转到相机。
我最后想到的是 mipmap 这个 texture.Its 也不是那么难。
texturefbo.bind();
Gdx.gl.glGenerateMipmap(GL20.GL_TEXTURE_2D);
texturefbo.setFilter(Texture.TextureFilter.MipMapLinearLinear,
Texture.TextureFilter.MipMapLinearLinear);