将 TextureRegion 翻转为纹理

Flipped TextureRegion to Texture

我正在使用 FBO 并尝试在 y 轴上翻转纹理。 我记得我可以用 TextureRegion.flip() 做到这一点。

但是,当我现在尝试翻转时,它不起作用。

    GLFrameBuffer.FrameBufferBuilder frameBufferBuilder = new GLFrameBuffer.FrameBufferBuilder(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    frameBufferBuilder.addColorTextureAttachment(GL30.GL_RGBA8, GL30.GL_RGBA, GL30.GL_UNSIGNED_BYTE);
    FrameBuffer frameBuffer = frameBufferBuilder.build();
    frameBuffer.begin();
    Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT | GL30.GL_DEPTH_BUFFER_BIT | (Gdx.graphics.getBufferFormat().coverageSampling ? GL30.GL_COVERAGE_BUFFER_BIT_NV : 0));
    batch.begin();

    if (textures[1] != null) {
        batch.draw(textures[1], x + marginx[1], y + marginy[1]);
    }

    batch.end();

    frameBuffer.end();

    Texture texturefbo = frameBuffer.getColorBufferTexture();
    framebuffer.dispose(); 
    textreg = new TextureRegion(texturefbo);
    textreg.flip(false, true);
    texturedraw = textreg.getTexture();

我知道我可以使用 TextureRegion 代替 Texture,但我仍然想了解为什么从 textureregion 转换为纹理而不传输 uvu2v2。

翻转 TextureRegion 不会对其所属的 Texture 做任何事情。纹理对象不包含任何 UV 数据。它只是一个图像,仅此而已。当您将纹理传递给 SpriteBatch.draw(Texture) 时,SpriteBatch 在内部使用 UV 将其拉伸到矩形 X-right 和 Y-up。例如,您还可以将负宽度传递给重载的 SpriteBatch.draw() 方法以水平翻转它。翻转对Texture对象没有影响,只影响SpriteBatch的内部绘图数据。

此外,如果您处置拥有纹理的 FrameBuffer,则无法保证您的纹理能够正常工作。事实上,我认为根本不应该。

如果您真的需要翻转纹理,您可以执行以下操作之一。

  1. 提前翻转源图像。
  2. 将图像作为 Pixmap 加载,遍历像素以将它们绘制到反向行中的另一个 Pixmap,处理第一个 Pixmap,最后将第二个 Pixmap 传递到 Texture 构造函数中。
  3. 如果纹理不是来自图像文件,您可以使用 GLUtils 将像素从纹理对象复制到像素图中,然后执行步骤 2。

当然,2 和 3 比简单地使用翻转的 TextureRegion 要复杂得多。