Three.js Texture Compression Inspector (BC1 / KTX2 Formats)
GPU texture compression explained in Three.js with a real-time JavaScript BC1/DXT1 encoder, CompressedTexture upload with mipmaps, RGB565 and error-heatmap views, PSNR and memory stats, filtering modes and detection of the formats a KTX2 file would transcode to.
Key topics: three.js texture compression, bc1, dxt1, s3tc, ktx2, basis, compressed texture, mipmaps
Game Over
What this code does
- BC1 Blocks: every 4×4 texel block stores two RGB565 endpoints and sixteen 2-bit palette indices, 8 bytes (4 bits per pixel).
- Encoder: endpoints come from the block's colour bounding box, inset slightly, and each texel picks the nearest of 4 palette colours.
- GPU Upload: a full mip chain goes up as a `CompressedTexture` with `RGB_S3TC_DXT1_Format` where WEBGL_compressed_texture_s3tc exists, with a CPU decode otherwise.
- Artefacts: zoom in to see blocky edges and banded gradients, or view the ×8 error heatmap and PSNR.
- KTX2 / Basis: universal textures transcode at load time to the best supported format (ASTC, BC7, BC1/3 or ETC2), shown for your GPU.
- Filtering: compare nearest, bilinear and trilinear with anisotropy on a grazing-angle plane.
- Controls: texture view, filtering, anisotropy, 4× zoom and compressing your own image.
Related Demos
Continue Learning
JavaScript Implementation
// Upload GPU-compressed BC1 (DXT1) data: 8 bytes per 4×4 block = 4 bits per pixel
if (renderer.extensions.has('WEBGL_compressed_texture_s3tc')) {
const mipmaps = levels.map(({ rgba, w, h }) => ({ data: encodeBC1(rgba, w, h), width: w, height: h }))
const texture = new THREE.CompressedTexture(mipmaps, 256, 256, THREE.RGB_S3TC_DXT1_Format)
texture.minFilter = THREE.LinearMipmapLinearFilter
texture.needsUpdate = true
mesh.material.map = texture
}
// BC1 block: two RGB565 endpoints (c0 > c1) + sixteen 2-bit indices into
// { c0, c1, ⅔c0 + ⅓c1, ⅓c0 + ⅔c1 }. KTX2/Basis files transcode to formats like this at load time.