Skip to content
On this page

Filtering & Mipmaps

Your UV lands between texels almost all the time. Filtering is the rule for what the GPU answers when that happens, and mipmaps are the answer to what the GPU should answer when the whole texture has shrunk to a few pixels on screen. Both are decided at import time, and both are the reason a texture looks right at distance instead of shimmering.

// The sample picks the rule; the import settings define it:
half4 tex = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv);

One texel, two texels, or a neighborhood

When a pixel needs a value from a texture, the UV rarely points exactly at a texel center. The GPU has to decide which texel, or blend of texels, to return. That decision is the filter, and there are two levels of it.

Magnification is when the texture is bigger on screen than it is in memory, one texel covers several pixels. The choices are point, which picks the single nearest texel and gives a crisp, aliased look, and bilinear, which blends the four texels around the UV and gives a smooth one. Point is a style choice, pixels and retro looks love it. Bilinear is the safe default.

Minification is when the texture is smaller on screen than it is in memory, several texels fight over one pixel. This is where aliasing gets ugly: a naive nearest texel pick flickers as the camera moves, because the “nearest” choice jumps from texel to texel. Mipmaps are the cure, and they are worth understanding properly, because they are the single most important quality setting on a texture.

Shader Story: Textures - Filtering and Mipmaps

The mip chain

A mipmap chain is a pyramid of copies of the texture, each one half the linear size of the last: the original, then half, then a quarter, down to a single texel. For a 1024 by 1024 texture the chain is about 1.33 times the original size, a third of extra memory, which is the price of the effect.

The sampler picks the level of the chain that matches the on screen size of the texture at that pixel, and filters within that level. Distant surfaces read the small, blurry copies, where one pixel honestly covers many texels, and close surfaces read the sharp original. The blur of a mip is not a defect, it is an honest average, and that is exactly why it kills the shimmer.

The final setting is anisotropic filtering, which fixes the last case: a large surface seen at a shallow angle, a floor stretching to the horizon. There the texture is minified strongly in one direction but not the other, and a square average would smear it. Anisotropy samples an elongated neighborhood along the stretch instead, and it is why distant floors and roads stay sharp. It costs a little, and the quality difference at grazing angles is large enough that modern projects turn it on almost everywhere.

Choosing the settings

The pattern that covers 90% of cases: color and detail textures get bilinear with trilinear mips and moderate anisotropy. Retro and pixel art get point filtering with no mips, because the aliasing is the look. Data textures, masks, normal maps, get the same filtering as the texture they belong to, usually bilinear, so they blur in step with the color they modify.


What the shader does

The shader samples the same fine texture with a point, bilinear, trilinear or anisotropic mode. The distant part of the surface goes from shimmering to stable while the near part stays sharp, which shows exactly what each setting is for.

Shader Story: Textures: Filtering & Mipmaps - schematic

Illustrative schematic: Near sharp, far stable.


The filtering behavior lives on the sampler, which the pipeline builds from the import settings, so the shader side is just a sample. To make the effect explicit, this pass also gives a fixed level of detail through SAMPLE_TEXTURE2D_LOD, which samples a chosen mip directly, handy for previews and for effects that want a specific amount of blur.

Shader code
Shader "DecompiledArt/Textures/Basics/FilteringMipmaps/FilteringMipmaps"
{
    Properties
    {
        _MainTex("Texture", 2D) = "white" {}
        [MainColor] _BaseColor("Base Color", Color) = (1, 1, 1, 1)
        _MipLevel("Fixed Mip Level", Range(0, 8)) = 0
        [Enum(Standard,0,FixedLOD,1)] _Mode("Mode", Float) = 0
    }

    SubShader
    {
        Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" "Queue" = "Geometry" }

        Pass
        {
            Name "ForwardOnly"
            Tags { "LightMode" = "UniversalForward" }

            HLSLPROGRAM
            #pragma vertex Vert
            #pragma fragment Frag

            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

            TEXTURE2D(_MainTex);   SAMPLER(sampler_MainTex);

            struct Attributes
            {
                float4 positionOS : POSITION;
                float2 uv         : TEXCOORD0;
            };

            struct Varyings
            {
                float4 positionCS : SV_POSITION;
                float2 uv         : TEXCOORD0;
            };

            CBUFFER_START(UnityPerMaterial)
                half4 _BaseColor;
                half  _MipLevel;
                half  _Mode;
            CBUFFER_END

            Varyings Vert(Attributes IN)
            {
                Varyings OUT;
                OUT.positionCS = TransformObjectToHClip(IN.positionOS.xyz);
                OUT.uv = IN.uv;
                return OUT;
            }

            half4 Frag(Varyings IN) : SV_Target
            {
                half4 tex;
                if (_Mode < 0.5h)
                    tex = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, IN.uv);
                else
                    tex = SAMPLE_TEXTURE2D_LOD(_MainTex, sampler_MainTex, IN.uv, _MipLevel);
                return half4(_BaseColor.rgb * tex.rgb, _BaseColor.a);
            }

            ENDHLSL
        }
    }

    FallBack Off
}

Set the texture import to the mode you are comparing against, and the slider shows you what each mip level actually looks like when you force it.


Sampling & Tinting • Manual Filtering

Related functions: Lerp


❤️ Support Shader Story

If this article helped you, consider supporting the project on Patreon: you’ll get access to the related source files and reference cheat-sheets.

DecompiledArt on Patreon DecompiledArt on Buy Me a Coffee

Your support helps keep this library open and free for everyone.