2
\$\begingroup\$

Can someone please help me resolve this issue. I've tried searching myself and the common answer suggested is to use a TextureFilter. I've tried doing that multiple times but nothing works. My png resolution is 1024x1024, and I'm trying to shrink it's size to fit on screen. This is the kind of result I'm getting, you can see that the circle looks very pixelated, despite the actual image having anti-aliased edges.

enter image description here

[The original image is here.]

Here's the code:

    SpriteBatch batch;
    Texture img;
    Sprite sprite;

    @Override
    public void create () {
        batch = new SpriteBatch();

        img = new Texture("circle.png");
        img.setFilter(TextureFilter.Linear, TextureFilter.Linear);

        sprite = new Sprite();
        sprite.setRegion(img);
    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(0,0,0,0);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        batch.begin();
        batch.draw(img, 0, 0, 128, 128); //draw texture -- reduced size
        batch.draw(sprite, 200, 200, 128, 128); //draw sprite -- reduced size
        batch.draw(img, 250, 250); //draw texture -- original size
        batch.end();
    }
\$\endgroup\$
3
  • \$\begingroup\$ Have you explored the other options in TextureFilter, such as Nearest, MipMap, etc.? \$\endgroup\$ Commented Jan 19, 2016 at 16:53
  • \$\begingroup\$ @AlexandreDesbiens Yes I have. If I try, for example, MipMapLinear, Linear, my image turns into a black box for some reason. Other options either result in a black box or the same images I have above. \$\endgroup\$
    – Jonathan
    Commented Jan 19, 2016 at 16:54
  • 1
    \$\begingroup\$ MipMap linear is probably exactly what you're looking for, but you need to make sure to generate the mip maps on the texture first. \$\endgroup\$ Commented Jan 19, 2016 at 17:30

1 Answer 1

3
\$\begingroup\$

As suggested in the comments, you should try to use Tri-Linear filtering and MipMaps. The reason using TextureFilter.MipMapLinearLinear results in a black box is that you haven't generated the texture with MipMaps. In order to do so, simply pass a second parameter to the Texture constructor as true like so:

img = new Texture("circle.png", true);
\$\endgroup\$
1
  • 2
    \$\begingroup\$ Actually, that should be img = new Texture(Gdx.files.internal("circle.png", true)); It worked though, so thank you! \$\endgroup\$
    – Jonathan
    Commented Jan 19, 2016 at 17:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .