简体   繁体   English

libgdx纹理过滤器和mipmap

[英]libgdx texture filters and mipmap

When I try to use mipmap filtering in LibGDX, none of the images appear. 当我尝试在LibGDX中使用mipmap过滤时,不会出现任何图像。

I'm new to LibGDX, and I have a simple 2d scene with three rotating, scaled circles. 我是LibGDX的新手,我有一个简单的2D场景,有三个旋转的缩放圆圈。 In order to anti-alias them, I wanted to use linear filtering. 为了对它们进行反混淆,我想使用线性过滤。 For advice, I looked to this article ,which said that, for heavily scaled images, a mipmap can be used to improve speed or quality. 对于建议,我查看了这篇文章该文章说,对于大规模缩放的图像,可以使用mipmap来提高速度或质量。

The first unexpected appearance was that, despite the fact that all of my images were scaled down, I would only see a linear filter if the magFilter was linear. 第一个意想不到的表现是,尽管我的所有图像都按比例缩小,但我只会看到一个线性过滤器,如果magFilter是线性的。 In other words: 换一种说法:

This code will show a linear filter for minified images: 此代码将显示缩小图像的线性过滤器:

parentTexture.setFilter(TextureFilter.Nearest, TextureFilter.Linear);

whlie this code will not: 这个代码不会:

parentTexture.setFilter(TextureFilter.Linear, TextureFilter.Nearest);

which seems opposite to the libGDX function: 这似乎与libGDX函数相反:

void com.badlogic.gdx.graphics.Texture.setFilter(TextureFilter minFilter, TextureFilter magFilter)

This would not bother me, except that it indicates that either libgdx is wrong (unlikely), the article is wrong (unlikely), or I don't understand texture filters. 这不会打扰我,除了它表明libgdx是错误的(不太可能),文章是错误的(不太可能),或者我不理解纹理过滤器。 The latter seems especially likely when I try mipmap filters. 当我尝试使用mipmap过滤器时,后者似乎特别有可能。

This code causes nothing to display 此代码不会显示任何内容

parentTexture.setFilter(TextureFilter.MipMapLinearLinear, TextureFilter.Linear);

This code displays, but with nearest filtering 此代码显示,但具有最近的过滤

parentTexture.setFilter(TextureFilter.Linear, TextureFilter.MipMapLinearLinear);

Any explanation of where I'm wrong would be greatly appreciated. 任何解释我错的地方都将不胜感激。 I have searched elsewhere, but texture filters in libGDX is pretty specific, so aside from the article, I haven't found much to help. 我在其他地方搜索过,但是libGDX中的纹理过滤器非常具体,所以除了文章之外,我还没有找到太多帮助。

I had this same problem, and the fix turned out to be insanely simple. 我遇到了同样的问题,修复结果非常简单。 When you create a Texture , you need to specify that it uses mipmaps. 创建Texture ,需要指定它使用mipmap。

All you have to do is pass a second parameter to the Texture constructor like this: 您所要做的就是将第二个参数传递给Texture构造函数,如下所示:

Texture myTexture = new Texture(Gdx.files.internal("myImage.png"), true);

You can view all the Texture class constructors in the API docs here: http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/Texture.html 您可以在此处查看API文档中的所有Texture类构造函数: http//libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/Texture.html

Just like Mitesh said in his answer mipmaps filters doesn't work because you are not telling Libgdx to generate mipmaps. 就像Mitesh在他的回答中所说的那样,mipmaps过滤器不起作用,因为你没有告诉Libgdx生成mipmap。

if you are using assets manager the code will be something like this 如果您使用资产管理器,代码将是这样的

 TextureParameter param = new TextureParameter();
 param.genMipMaps = true; // enabling mipmaps

 manager.load("path/to/texfile.png", Texture.class, param);

 Texture tex = manager.get("path/to/texfile.png", Texture.class);
 tex.setFilter(TextureFilter.MipMap, TextureFilter.Nearest);

There can be multiple issues with your image: 您的图片可能存在多个问题:

  1. It should be power of 2, if you are using an image with size like 354X420, it won't work. 它应该是2的幂,如果你使用像354X420大小的图像,它将无法工作。 In this case you need to take an image of 512X512 or any other power of 2. 在这种情况下,您需要拍摄512X512的图像或2的任何其他功率。

  2. When you want to enable Mipmap filtering, then you need to enable it using boolean genMipMaps which tells libgdx whether to generate mapmaps. 如果要启用Mipmap过滤,则需要使用boolean genMipMaps启用它,告知libgdx是否生成mapmaps。

Try using the same minFilter and maxFilter. 尝试使用相同的minFilter和maxFilter。 I had a similiar problem and if I put TextureFilter.Linear, TextureFilter.Linear or both MipMap the problem is solved. 我有一个类似的问题,如果我把TextureFilter.Linear,TextureFilter.Linear或两个MipMap都解决了问题。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM