【问题标题】:LibGDX texture bleeding issueLibGDX 纹理出血问题
【发布时间】:2015-09-17 09:33:17
【问题描述】:

我是 LibGDX 的新手,正在尝试实现视差背景。 一切都很顺利,直到我遇到这样的问题:滚动背景时出现一些条纹。您可以在附图中看到它:

所以我更深入地研究了一个问题并发现这是某种纹理渗出。但情况是我的纹理已经有 [Linear, Nearest] 过滤器集,TexturePacker 使用duplicatePadding。实际上,我不知道任何其他方法可以解决此问题。请帮忙!

这是我的一些代码:

TexturePacker

TexturePacker.Settings settings = new TexturePacker.Settings();
settings.minWidth = 256;
settings.minHeight = 256;
settings.duplicatePadding = true;
TexturePacker.process(settings, "../../design", "./", "textures");

资产加载器

textureAtlas = new TextureAtlas(Gdx.files.internal("textures.atlas"));

for (int i = 0; i < 2; i++) {
    Background.skies.add(textureAtlas.findRegion("background/sky", i));
    Background.skies.get(i).getTexture().setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Nearest);
}

for (int i = 0; i < 2; i++) {
    Background.clouds.add(textureAtlas.findRegion("background/cloud", i));
    Background.clouds.get(i).getTexture().setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Nearest);
}

for (int i = 0; i < 8; i++) {
    Background.cities.add(textureAtlas.findRegion("background/city", i));
    Background.cities.get(i).getTexture().setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Nearest);
}

Background.moon = textureAtlas.findRegion("background/moon");
Background.forest = textureAtlas.findRegion("background/forest");
Background.road = textureAtlas.findRegion("background/road");

Background.moon.getTexture().setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Nearest);
Background.forest.getTexture().setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Nearest);
Background.road.getTexture().setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Nearest);

BackgroundDrawer

private void drawParallaxTextureList(Batch batch, List<TextureAtlas.AtlasRegion> list,
                                     float moveX, float posY) {
    for (int i = 0; i < list.size(); i++) {
        boolean needDraw = false;

        float shift = GameScreen.VIEWPORT_WIDTH * i;
        float drawX = 0.0f;

        if (shift - moveX <= -(GameScreen.VIEWPORT_WIDTH)) { // If it's behind the screen
            if (i == 0) { // If it's first element
                if (moveX >= GameScreen.VIEWPORT_WIDTH * (list.size() - 1)) { // We need to show first after last
                    needDraw = true;
                    drawX = (GameScreen.VIEWPORT_WIDTH) - (moveX - ((GameScreen
                            .VIEWPORT_WIDTH) * (list.size() - 1)));
                }
            }
        } else if (shift - moveX < (GameScreen.VIEWPORT_WIDTH - 1)) {
            needDraw = true;
            drawX = shift - moveX;
        }

        if (needDraw) {
            batch.draw(list.get(i), (int) drawX, (int) posY);
        }
    }
}

注意:我现在不使用任何相机进行绘图。我只使用大小为1920x1280FitViewport。此外,即使在 FullHD 分辨率下,有时也会出现渗色。

更新:设置Nearest 过滤器进行缩小和放大,增加paddingX 并禁用抗锯齿解决了问题,但最终图像变得太丑了!有没有办法避免禁用抗锯齿?因为没有它,缩小版看起来很糟糕。

【问题讨论】:

  • 如果您将过滤器保持为线性,禁用抗锯齿是否有帮助?为什么还要在 2D 游戏中使用抗锯齿?

标签: android opengl-es libgdx texturepacker


【解决方案1】:

尝试将 min 和 mag 过滤器都设置为 Nearest

    .setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest);

在 GUI TexturePacker 中有一个用于挤压图形的选项 - 这意味着 重复纹理的每个边框像素。然后您可以将两个过滤器都设置为线性

    .setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);

但不幸的是,我在您正在使用的 TexturePacker.Settings 对象中看不到此选项。您可以尝试将线性设置为两者,但我很确定它不会工作(线性过滤器需要最近的 4 个纹素来生成一个,所以它可能仍然会产生问题)。

尝试使用 GUI Texturepacker,然后可以使用挤出选项

【讨论】:

  • 请参阅帖子中的更新。同时设置 Nearest 会导致缩小比例时的渲染质量很差。
  • 我无法启动我在 Mac 上的谷歌代码上找到的这个 jar。它说Could not find or load main class texturepacker-gui.jar
  • 试试这个 - codeandweb.com/texturepacker/download。免费版本对您来说应该足够了,因为它具有您需要 afaik 的所有选项。我正在使用付费版本,我推荐它
  • 您是否设置了减少边框伪影选项?请提供您在 TP 中的设置截图
  • 他们是:onetwo
【解决方案2】:

这个工件的几个可能原因:

  1. 当精灵分辨率被缩小时,填充可能不够大。尝试将纹理打包器的 filterMin 更改为 MipMapLinearNearest。并尝试增加paddingXpaddingY 的大小。

  2. 也许您看到精灵边缘的像素变暗或变亮,因为您没有使用预乘 alpha 并且纹理的背景颜色(其 alpha 为零)为白色。尝试设置premultiplyAlpha: true。如果这样做,还需要将 SpriteBatch 的混合函数更改为 (GL20.GL_ONE, GL20.GL_ONE_MINUS_SRC_ALPHA) 才能正确渲染。

  3. 在绘制精灵位置和大小时,您似乎将它们四舍五入为整数。这适用于像素完美的游戏,您可以确定精灵正以 1:1 的分辨率准确地渲染到屏幕上。但是一旦屏幕尺寸不完全匹配,您的四舍五入可能会产生小于 1 像素宽的间隙,看起来像半透明像素。

【讨论】:

  • 1:没有帮助。仅对 Nearest 过滤器有帮助。在原始分辨率上仍然喘不过气来,但在缩小范围内还可以。 2:没有帮助。 3:对原始分辨率和缩小都没有影响。
猜你喜欢
  • 2011-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-20
  • 1970-01-01
  • 1970-01-01
  • 2017-05-16
相关资源
最近更新 更多