java libgdx 纹理过滤器和 mipmap

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15378791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:23:10  来源:igfitidea点击:

libgdx texture filters and mipmap

javatextureslibgdxmipmaps

提问by Kenkron

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. 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.

我是 LibGDX 的新手,我有一个简单的 2d 场景,其中包含三个旋转、缩放的圆圈。为了抗锯齿,我想使用线性过滤。为了寻求建议,我查看了这篇文章,其中说,对于大量缩放的图像,可以使用 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. In other words:

第一个意想不到的现象是,尽管我的所有图像都按比例缩小了,但如果 magFilter 是线性的,我只会看到线性过滤器。换句话说:

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. The latter seems especially likely when I try mipmap filters.

这不会打扰我,除了它表明 libgdx 错误(不太可能),文章错误(不太可能),或者我不了解纹理过滤器。当我尝试 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 中的纹理过滤器非常具体,所以除了这篇文章之外,我没有找到太多帮助。

回答by twiz

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 Textureconstructor like this:

您所要做的就是将第二个参数传递给Texture构造函数,如下所示:

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

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

You can view all the Textureclass constructors in the API docs here: http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/Texture.html

您可以Texture在此处查看API 文档中的所有类构造函数:http: //libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/Texture.html

回答by Ashraf Saleh

Just like Mitesh said in his answermipmaps 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);

回答by Mitesh Sharma

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. In this case you need to take an image of 512X512 or any other power of 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.

  1. 它应该是 2 的幂,如果您使用的是 354X420 大小的图像,它将不起作用。在这种情况下,您需要拍摄 512X512 或任何其他 2 的幂的图像。

  2. 当你想启用 Mipmap 过滤时,你需要使用布尔 genMipMaps 来启用它,它告诉 libgdx 是否生成地图贴图。

回答by UberLambda

Try using the same minFilter and maxFilter. I had a similiar problem and if I put TextureFilter.Linear, TextureFilter.Linear or both MipMap the problem is solved. Hope this helps.

尝试使用相同的 minFilter 和 maxFilter。我有一个类似的问题,如果我把 TextureFilter.Linear、TextureFilter.Linear 或两者都放在 MipMap 上,问题就解决了。希望这可以帮助。