Skip to content

@web-engine-dev/texture-compression

GPU-compressed texture transcoding using KTX2 container format with Basis Universal compression. Transcodes universal textures to the optimal GPU format for the current device (BC7, ASTC, ETC2, or uncompressed fallback).

Layer 8 · Content

Features

  • KTX2 Container: Industry-standard GPU texture container format
  • Basis Universal: Supercompressed textures that transcode to any GPU format
  • Adaptive Transcoding: Automatically selects the best GPU format for the target device:
    • BC7 (Desktop GPUs: Windows, macOS, Linux)
    • ASTC (Mobile GPUs: iOS, Android, Apple Silicon)
    • ETC2 (Older mobile GPUs, WebGL 2 fallback)
    • RGBA8 (Uncompressed fallback for unsupported devices)
  • UASTC: Ultra-high quality universal texture format (near-lossless)
  • ETC1S: Smaller file size format for lower-quality requirements
  • Mipmaps: Transcode all mip levels from a single supercompressed source
  • WASM Transcoder: Basis Universal transcoder compiled to WebAssembly
  • Worker Thread: Transcoding runs in a Web Worker to avoid blocking the main thread
  • Cubemap & Array: Support for cubemap faces and texture arrays

Installation

bash
npm install @web-engine-dev/texture-compression
# or
pnpm add @web-engine-dev/texture-compression

Quick Start

typescript
import { KTX2Loader, detectGPUFormat } from '@web-engine-dev/texture-compression';

// Initialize with WebWorker-based transcoder
const loader = await KTX2Loader.create({
  workerCount: navigator.hardwareConcurrency || 4,
});

// Detect optimal GPU format for current device
const targetFormat = detectGPUFormat(gpuDevice);
// e.g., 'bc7-rgba-unorm' on desktop, 'astc-4x4-unorm' on mobile

// Load and transcode a KTX2 texture
const texture = await loader.load('/textures/brick_wall.ktx2', {
  targetFormat,
  generateMipmaps: false, // Already in KTX2 file
});

// Upload to GPU
gpuDevice.queue.writeTexture({ texture: gpuTexture }, texture.data, texture.layout, texture.size);

Format Selection

GPUFormatBlock SizeQualitySize vs RGBA8
Desktop (Nvidia/AMD/Intel)BC74×4Excellent~25%
Apple Silicon / MobileASTC 4×44×4Excellent~25%
Older Mobile / WebGL 2ETC24×4Good~25%
FallbackRGBA81×1Lossless100%

Use Cases

  • glTF Textures: KTX2 textures via KHR_texture_basisu extension
  • Asset Pipeline: Convert PNG/JPEG to KTX2 at build time
  • Streaming: Progressively load mip levels from KTX2 files
  • Memory Savings: GPU-compressed textures use 4× less VRAM

Dependencies

  • basis_universal — Basis Universal WASM transcoder

Proprietary software. All rights reserved.