如何为 LibGDX 中的动画设置滤镜?

How do I set a filter to an animation in LibGDX?

我制作了一个带有小精灵 (32x32) 的精灵 sheet 用于动画。我 运行 遇到动画看起来非常模糊的问题。 有人告诉我尝试添加 .setFilter(TextureFilter.Nearest, TextureFilter.Nearest); 我不太清楚如何向动画添加滤镜,所以如果有人能帮助我,我将不胜感激,谢谢。

您不能直接将 TextureFilter 设置为动画,因为 Animation 不知道什么是动画。但是由于动画由 Textures 组成,您可以将 TextureFilter 设置为动画中的 Textures。

如何做到这一点取决于您如何创建动画。如果你加载你的纹理并创建它们的动画,你可以这样做:

Texture texture = new Texture(Gdx.files.internal("path/to/your/image.png"));
texture.setFilter(TextureFilter.Nearest, TextureFilter.Nearest);

// load the other textures of the animation in the same way
Texture[] allTextures = loadOtherTexturesWithFilter();

// create the animation
final float frameDuration = 0.1f;
Animation<Texture> animation = new Animation(frameDuration, allTextures);

或者,如果您加载 Texture 的方式不同,您可以从 Animation 获取 Texture 并更改它们的过滤器:

//here the generic type of the Animation needs to be Texture or some subclass of texture (like TextureRegion, ...)
Animation<Texture> animation = yourAnimation;
for (Texture texture : animation.getKeyFrames()) {
  texture.setFilter(TextureFilter.Nearest, TextureFilter.Nearest);
}