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

Viewed 52

I made a sprite sheet with a small sprite(32x32) for an animation. I run into a problem where the animation appears very blurry. I have been told to try adding .setFilter(TextureFilter.Nearest, TextureFilter.Nearest); I don't quite know how to add a filter to an animation so if anyone could help me out it would be much appreciated, thanks.

1 Answers

You can't set a TextureFilter to an animation directly, since an Animation doesn't know about what's animated. But since the animation consists of Textures you can set a TextureFilter to the Textures inside the animation.

How this is done depends on how you create you animation. If you load your textures and create an animation of them you could do it like this:

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

Or if you load your Textures differently you could get the Textures from the Animation and change the filters on them:

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