Texture filtering is a technique that is used to reduce the aliasing that occurs when sampling from textures.
Aliasing occurs where the pixel you are rendering on the screen does not lie exactly on the grid of pixels within the texture that is being mapped onto the object that you are rendering. Instead, it is offset to some degree as the object the texture is mapped to is at an arbitrary distance and orientation to the viewer.
There are two problematic situations that can occur when mapping a texture pixel, or texel, to a screen pixel, either the texel is larger than the screen pixel, in this case it must be minified to fit the screen pixel. Alternatively, a situation can occur where the texel is smaller than the screen pixel, therefore multiple texels must be combined to fit the screen pixel.
Texture filtering relies heavily on mipmapping. This is because that, during magnification, the number of texels to be fetched never exceeds four. However, during minification, as the textured object moves further away, the entire texture can fit within one pixel. In this case, every texel would have to be fetched and then merged for an accurate result, which would be too expensive to implement for a GPU.
Instead, four samples are selected, resulting in an unpredictable under-sampling of the data. Mipmapping overcomes this by pre-filtering the texture at different sizes so that as the object moves away a smaller texture size is applied instead.
Widely implemented methods of filtering include bilinear, trilinear, and anisotropic filtering, with the difference between them being how many texels are sampled, how these texels are combined, and whether mipmapping is utilized in the filtering process.
The performance cost of each filtering method varies. So, selecting the type of filtering to be used must be on a case-by-case process where the performance cost of the filtering method is weighed against the visual benefits that it provides.
For example, trilinear filtering is twice the cost of bilinear filtering yet the visual advantages are not always apparent especially on textures being applied to distant objects. Instead, the use of 2x anisotropic filtering is recommended as it often gives better image quality and increased performance.
When using anisotropic filtering, the maximum number of samples must be set. However, due to the performance impact incurred on mobile, we recommend that you exercise caution when using higher numbers of samples, especially eight samples or higher. This technique is best suited for textures on slanted surfaces, such as ground in the distance.
The following set of images displays the difference between the different types of texture filtering. Of particular note, is the indiscernible difference between bilinear and trilinear filtering when 2x sample anisotropic filtering is enabled. Even though trilinear filtering costs more resources to use.
Further information on texture filtering can be found in the Real-Time 3D Art Best Practices: Texturing guide.
Implementing texture filtering in Unity
In Unity, texture filtering is set in the Project Quality Settings.
To set the texture filtering settings for a texture in Unit:
- Go to Edit.
- Choose Project Settings.
- Select Quality to open the Project Quality Settings panel.
- Choose the Anisotropic Textures drop down from this window.
- Choose the Per Texture option.
Then for each texture, select it within the Assets section of the Project window so that the Texture Inspector window opens. When the window is open, set both the Filter Mode and the Aniso Level settings for that texture.