TExcelTAD
Home › 3D Web Games with Three.js › DRACO, KTX2 & Meshopt

DRACO, KTX2 & Meshopt

A web game's assets travel over the network before anyone can play, and then they sit in GPU memory on devices that may have very little of it. Two separate problems, with separate fixes:

Problem Fix glTF extension Three.js side
Geometry is big to download Draco (strong mesh compression) or meshopt (fast, compresses well with gzip/brotli) KHR_draco_mesh_compression, EXT_meshopt_compression DRACOLoader, MeshoptDecoder
Vertex data is big in memory quantization: store positions/normals/UVs as 16- or 8-bit integers KHR_mesh_quantization built into GLTFLoader
Textures are big to download and in GPU memory KTX2 / Basis Universal: GPU-compressed textures that stay compressed in VRAM KHR_texture_basisu KTX2Loader
Textures are big to download only WebP / AVIF images EXT_texture_webp, EXT_texture_avif built into GLTFLoader

The key insight is the last two rows. A PNG or JPEG is small on disk, but the GPU can't sample it compressed: the browser decodes it to raw RGBA, 4 bytes per pixel, before upload. A KTX2 texture is transcoded to a GPU block-compression format (BC, ETC, ASTC, depending on the device) and stays compressed in memory at roughly 1 byte per pixel or less.

js · try it yourself
// GPU memory for one 2048x2048 texture (+1/3 for mipmaps) in different forms.
const px = 2048 * 2048;
const mip = 4 / 3;
const forms = [
  ['PNG/JPEG/WebP (decoded to RGBA8)', 4],
  ['KTX2 UASTC -> BC7 / ASTC 4x4', 1],
  ['KTX2 ETC1S -> BC1 / ETC1 (no alpha)', 0.5],
];
for (const [name, bytesPerPixel] of forms) {
  const mb = (px * bytesPerPixel * mip) / (1024 * 1024);
  console.log(`${name.padEnd(36)} ${mb.toFixed(1).padStart(5)} MB`);
}
// Vertex data for a 50,000-vertex character: position + normal + uv.
const verts = 50000;
const float32 = verts * (12 + 12 + 8);
const quantized = verts * (8 + 4 + 4);   // int16 xyz (padded to 8), int8 normal (padded to 4), int16 uv
console.log(`vertex data: float32 ${(float32 / 1024).toFixed(0)} KB, quantized ${(quantized / 1024).toFixed(0)} KB`);
PNG/JPEG/WebP (decoded to RGBA8)      21.3 MB
KTX2 UASTC -> BC7 / ASTC 4x4           5.3 MB
KTX2 ETC1S -> BC1 / ETC1 (no alpha)    2.7 MB
vertex data: float32 1563 KB, quantized 781 KB

A level with thirty 2K textures is over 600 MB of VRAM as PNGs, which crashes many phones and integrated GPUs, and about 160 MB as UASTC KTX2. Texture compression is the most effective memory optimization in most web games.

Setting up the loaders

The decoders are WebAssembly modules that GLTFLoader loads on demand, so you tell it where they live. Three.js ships them in examples/jsm/libs/; copy that folder into your public assets (or point at a CDN path of the same three version):

js
import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js';
import { MeshoptDecoder } from 'three/addons/libs/meshopt_decoder.module.js';

const draco = new DRACOLoader().setDecoderPath('/libs/draco/');      // draco_decoder.wasm etc.
const ktx2 = new KTX2Loader()
  .setTranscoderPath('/libs/basis/')                                  // basis_transcoder.js + .wasm
  .detectSupport(renderer);                                           // which GPU formats this device has

const loader = new GLTFLoader()
  .setDRACOLoader(draco)
  .setKTX2Loader(ktx2)
  .setMeshoptDecoder(MeshoptDecoder);

const { scene: level } = await loader.loadAsync('/models/level.glb');

Create these loaders once and reuse them: each one starts worker threads. Call draco.dispose() / ktx2.dispose() when you no longer load anything. Standalone .ktx2 textures (not inside a glTF) load with ktx2.loadAsync('/textures/rock.ktx2').

Note: With Vite, copy node_modules/three/examples/jsm/libs/draco and .../basis into public/libs/ (manually or with a copy plugin) so the paths above resolve. The decoder version must match the loader's version, which is why copying from your installed three package is safer than a random CDN URL.

Compressing assets: glTF Transform

The usual tool is glTF Transform, a command-line program and library (@gltf-transform/cli, version 4.x at the time of writing). One command handles most cases:

bash
npm install --global @gltf-transform/cli

gltf-transform inspect level.glb                      # tables of meshes, textures, sizes
gltf-transform optimize level.glb level.opt.glb \
  --compress meshopt \
  --texture-compress webp \
  --texture-size 2048

optimize merges and de-duplicates data, welds vertices, removes unused nodes, compresses geometry (--compress draco | meshopt | quantize, default meshopt) and textures (--texture-compress ktx2 | webp | avif | auto), and caps texture size. For KTX2 there are dedicated commands, which need the KTX-Software tools (toktx/ktx) installed:

bash
gltf-transform uastc  level.glb level.ktx2.glb --slots "{normalTexture,occlusionTexture}"   # high quality for data maps
gltf-transform etc1s  level.ktx2.glb level.final.glb --slots "{baseColorTexture,emissiveTexture}"   # smaller for color

Check the options with gltf-transform <command> --help; they change between releases. Other good tools: gltfpack (from meshoptimizer: meshopt compression + simplification in one fast binary), Blender's glTF exporter (has a Draco option), and the KTX-Software ktx CLI for single textures.

Which compression to pick

Draco meshopt
File size alone smallest larger, but close after gzip/brotli
Decode speed slower (WASM, in workers) very fast
Compresses geometry only geometry, morph targets, animation data
Good for big static meshes over slow networks everything else, especially animated characters
UASTC ETC1S
Quality high (near the original) lower, blocky on gradients
Size larger files, ~1 byte/px in VRAM small files, ~0.5 byte/px in VRAM
Good for normal maps, hero textures, UI albedo of props and terrain, anything seen small

Tip: Measure, don't guess: compare the Network panel (download), renderer.info.memory (counts of geometries/textures) and the browser's GPU memory in the task manager before and after. Keep the uncompressed originals in source control; compression is a build step.

Test yourself

Why does a 300 KB JPEG texture take about 21 MB of GPU memory at 2048 × 2048 with mipmaps?

  1. Three.js stores it twice
  2. The GPU can't sample JPEG data, so it is decoded to raw RGBA (4 bytes per pixel) before upload
  3. WebGL converts it to float
  4. Mipmaps are 10× the size

Which GLTFLoader method enables loading files compressed with EXT_meshopt_compression?

  1. setDRACOLoader
  2. setMeshoptDecoder
  3. setKTX2Loader
  4. None; it is built in

What does ktx2Loader.detectSupport(renderer) do?

  1. Checks whether WebGL 2 is available
  2. Finds which GPU texture formats the device supports, so Basis textures are transcoded to one of them
  3. Downloads the transcoder
  4. Enables mipmaps