【问题标题】:LibGdx stretched / deformated pixels (for example in pixel art)LibGdx 拉伸/变形像素(例如在像素艺术中)
【发布时间】:2014-05-28 15:15:56
【问题描述】:

我是 Libgdx 的新手。我试图制作像素艺术游戏,但我遇到了问题。当我用纹理渲染精灵时,像素被变形拉伸(我在第二张图像上添加了红色箭头)。 我制作了 400x400 的窗口并按 1:1 比例缩放精灵以适应屏幕宽度(精灵宽度 = 屏幕宽度,精灵高度 = 屏幕宽度)

图片:

这是主游戏类的代码:

public class Pixel implements ApplicationListener {

    SpriteBatch batch;
    Texture testTexture;
    Sprite testSprite;
    int w;
    int h;

    @Override
    public void create() {
        batch = new SpriteBatch();
        testTexture = new Texture(Gdx.files.internal("test.png"));
        testSprite = new Sprite(testTexture);
        testSprite.setSize(w, w);
    }

    @Override
    public void render() {
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();
        testSprite.draw(batch);
        batch.end();
    }

    @Override
    public void resize(int width, int height) {
        w = width;
        h = height;
        create();
    }

    @Override
    public void pause() {
        // TODO Auto-generated method stub
    }

    @Override
    public void resume() {
        // TODO Auto-generated method stub
    }

    @Override
    public void dispose() {
        // TODO Auto-generated method stub
    }
}

这是桌面入门类:

public class DesktopLauncher {
    public static void main(String[] arg) {
        LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
        config.width = 400;
        config.height = 400;
        new LwjglApplication(new Pixel(), config);
    }
}

【问题讨论】:

    标签: java android libgdx desktop


    【解决方案1】:

    首先,每次调整游戏大小时调用 create 并不是一个好主意,因为您创建了一个 spritebatch 和一个 sprite。 制作其他会改变精灵大小的函数。

    你的创建方法:

     @Override
    public void create() {
        batch = new SpriteBatch();
        testTexture = new Texture(Gdx.files.internal("test.png"));
        testSprite = new Sprite(testTexture);
        testSprite.setSize(w, w);
    }
    

    如您所见,您将精灵大小设置为 w 和 w,您可能希望将其设置为 w 和 h。 制作其他只会执行此操作的功能,或者甚至不费心制作其他功能,只需在 resize 方法中更改您的精灵大小。

    public void resize(int width, int height) {
        w = width;
        h = height;
        testSprite.setSize(w, h);
    }
    

    【讨论】:

    • 感谢您的快速回复!我使用 w 和 w 只是因为我想让精灵保持 1:1 的纵横比。我是java新手,我是libgdx,所以你帮了我很多。 :-)
    【解决方案2】:

    拉伸伪影是由于没有按整数倍缩放精灵。 400 不是纹理尺寸 (64) 的整数倍。通过最近的过滤,您可以向各个方向进行四舍五入。没有办法避免这种情况。您的窗口比纹理大 6.25 倍,因此其中一些像素必须比其他像素放大更多才能填充整个窗口。如果您想解决此问题,请考虑使用 FitViewport。

    正如 Paul 所说,testSprite.setSize 应该在 resize 方法中。您不应从 resize 方法调用 create。除了浪费时间重新创建所有内容之外,您还会泄漏原始精灵批处理中的着色器程序之类的东西。

    【讨论】:

      猜你喜欢
      • 2021-03-13
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-23
      • 1970-01-01
      • 2016-10-29
      • 1970-01-01
      相关资源
      最近更新 更多