Introduction#
Mipmaps stop distant textures from shimmering, but they leave a problem: floors, roads and walls seen at a shallow angle turn into a smeary blur long before they should. If you’ve ever turned on anisotropic filtering in a game’s settings and watched the ground suddenly sharpen, this post explains what changed:
- Why a pixel’s footprint on a texture gets stretched when you look at a surface at an angle
- Why trilinear filtering has to choose between blurry and shimmery in that case
- How anisotropic filtering takes several samples along the footprint to get both right
“Anisotropic” means “not the same in every direction”, and that’s exactly the problem.
Side by Side#
Both halves of the demo below show the same floor and the same texture. The left uses trilinear filtering only; the right adds anisotropic filtering. Lower the camera to make the viewing angle shallower.
Trilinear (left) and trilinear plus anisotropic filtering (right)
On the left, the words become unreadable only a short way into the distance, and the lines running away from you dissolve. On the right, they stay sharp much further. Raise the camera to look straight down and the two halves look almost identical: anisotropic filtering only matters at shallow angles.
Why Trilinear Gets Blurry#
Imagine a single square pixel projected onto the floor. Looking straight down, it covers a square patch of texture. But looking along the floor at a shallow angle, the same pixel covers a long, thin patch, stretched away from you: perhaps 1 texel wide but 8 texels long. That patch is the pixel’s footprint.
Mipmapping assumes the footprint is square. It has to pick a single level, and the graphics card picks it from the longest side of the footprint (measured by comparing neighbouring pixels’ UVs), because choosing from the short side would leave 8 texels along the long side unaccounted for, and the shimmering would come back. So for our 1 Ă— 8 footprint it reads level 3, where each texel is 8 texels across. That’s right along the footprint’s length but 8 times too blurry across its width.
How Anisotropic Filtering Fixes It#
Anisotropic filtering takes several samples along the long side of the footprint instead of one. For a 1 Ă— 8 footprint, it can take 8 samples spaced along it, each from a sharper level that suits the short side, and average them. The result is filtered correctly in both directions.
The diagram below shows a pixel’s footprint (orange) on a grid of level-0 texels, and the area each method samples (blue).
One pixel’s footprint on the texture, and what each method samples
The anisotropic level setting (2Ă—, 4Ă—, 8Ă—, 16Ă—) is the maximum number of samples per pixel. Footprints more stretched than that are given the maximum and slightly blurred, as trilinear would. Most graphics cards support up to 16Ă—, which covers all but the most extreme angles.
How the diagram works out its numbers
Imagine a pixel as a small square window looking down at the floor. Looking straight down (90°), it sees a square patch of texture. Tilt the view to a lower angle and the same window sees a patch stretched away from you, roughly 1 ÷ sin(angle) times longer than it is wide: about 2× at 30°, 5.8× at 10° and 14× at 4°. (Angles, Sine and Cosine explains sin.) The diagram assumes the short side is one texel and caps the long side at 16.
- Trilinear picks its mipmap level from the long side: level = logâ‚‚(long side), meaning the power of 2 that gives the long side. A footprint 8 texels long gets level 3 (2Âł = 8), where one texel covers 8 level-0 texels in both directions.
- Anisotropic takes N = (long side Ă· short side), rounded up, samples, but never more than the maximum you chose. Each sample covers long side Ă· N, so it uses level logâ‚‚(long side Ă· N): level 0, full sharpness, whenever the maximum is big enough.
The real hardware rule isn’t fixed by the standard, but it follows this idea.
The extra samples are taken by the texture unit itself, not your shader: the shader still makes one texture() call. Each sample is a full trilinear lookup, so 16Ă— can mean up to 16 times the texture reads for those pixels. In practice it’s much cheaper than that sounds: the number of samples adapts per pixel, so surfaces facing the camera use just one; neighbouring pixels read overlapping texels that are already in the texture cache; and hardware vendors use clever approximations to cut the work further. That’s why modern games often turn it on by default with only a small performance cost. The exact method isn’t standardised: the OpenGL and WebGL description suggests an approach (take up to N samples along the long side, choosing the mip level from the long side divided by N, roughly as in the diagram) but lets hardware approximate it.
In WebGL, anisotropic filtering is an optional extra feature (an extension) that nearly every browser supports. A program switches it on for each texture, choosing the maximum level.
Summary#
- At shallow viewing angles, a pixel’s footprint on a texture becomes long and thin.
- Trilinear filtering chooses its mip level from the long side, so it’s too blurry across the short side.
- Anisotropic filtering takes several samples along the long side from a sharper level.
- The setting (2Ă— to 16Ă—) is the maximum number of samples; the cost is usually modest.
Next: sRGB and gamma, the reason a texture’s numbers don’t mean what you’d expect.
Further Reading#
- Khronos, EXT_texture_filter_anisotropic: the extension specification, including the suggested sampling method.
- MDN, EXT_texture_filter_anisotropic: using it in WebGL.
- Wikipedia, Anisotropic filtering: history and comparison images.
- Paul Heckbert, Fundamentals of Texture Mapping and Image Warping (1989): the classic thesis on texture footprints and filtering.