Achieve Pixel Art Look in Three.js with NearestFilter
This article explains how to use the NearestFilter
property in Three.js to prevent low-resolution textures from becoming
blurry when scaled up. By forcing the renderer to use nearest-neighbor
interpolation, you can preserve sharp edges and achieve a clean,
authentic pixel-art aesthetic in your 3D scenes.
By default, Three.js applies bilinear filtering to textures to make them look smooth when stretched or viewed from a distance. While this is ideal for high-resolution, realistic materials, it ruins the crisp, blocky look required for pixel art.
To fix this, you must change the texture’s magnification
(magFilter) and minification (minFilter)
properties to THREE.NearestFilter after loading the
texture.
Step-by-Step Code Implementation
Here is how to configure a texture for a pixel-art style:
import * as THREE from 'three';
// 1. Initialize the Texture Loader
const textureLoader = new THREE.TextureLoader();
// 2. Load your low-resolution texture
textureLoader.load('pixel-art-texture.png', (texture) => {
// 3. Set magnification filter to NearestFilter (prevents blur when close up)
texture.magFilter = THREE.NearestFilter;
// 4. Set minification filter to NearestFilter (prevents blur when far away)
texture.minFilter = THREE.NearestFilter;
// 5. Disable mipmapping (recommended for consistent pixel sizes)
texture.generateMipmaps = false;
// 6. Apply the texture to a material
const material = new THREE.MeshBasicMaterial({
map: texture,
transparent: true // Useful if your pixel art has transparent backgrounds
});
// Create mesh and add to scene
const geometry = new THREE.BoxGeometry(1, 1, 1);
const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
});Understanding the Filter Properties
texture.magFilter: Controls how the texture behaves when it is scaled up (magnified) on the screen. Setting this toTHREE.NearestFilterensures that as you get closer to the 3D object, the pixels simply scale up as solid blocks rather than blending together.texture.minFilter: Controls how the texture behaves when it is scaled down (minified) in the distance. Setting this toTHREE.NearestFilterkeeps the texture sharp even at a distance, though it may introduce some aliasing (shimmering) if the camera moves rapidly.texture.generateMipmaps = false: Mipmaps are pre-calculated, lower-resolution versions of an image used to optimize rendering at a distance. For pixel art, generating mipmaps is usually unnecessary and can introduce unwanted bilinear filtering. Turning this off saves GPU memory and keeps your pixels crisp.