2

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

纹理出血

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

这是我的一些代码:

纹理打包器

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);

背景抽屉

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);
        }
    }
}

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

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

4

2 回答 2

0

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

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

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

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

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

尝试使用 GUI Texturepacker,然后使用 extrude 选项

于 2015-09-17T10:56:11.983 回答
0

此工件的几个可能原因:

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

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

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

于 2015-09-17T13:18:36.543 回答