Skip to content

Repository files navigation

ktx2-encoder

Latest NPM release License

A lightweight JavaScript library for converting images to KTX2 (.ktx2) format. Powered by BinomialLLC/basis_universal.

Features

  • Convert images to KTX2 format
  • Support for both 2D images and cubemaps
  • Integration with gltf-transform
  • Support both Browser and Node.js
  • Optional multithreaded encoding in cross-origin isolated browsers

Quick Start

Install:

npm install --save ktx2-encoder

Import and encode an image:

import { encodeToKTX2 } from "ktx2-encoder";

const source = new Uint8Array(await file.arrayBuffer());
const ktx2Data = await encodeToKTX2(source, {
  isUASTC: true,
  generateMipmap: true
});

In browsers, encodeToKTX2 also accepts exactly six Uint8Array images to encode a cubemap, ordered as [posx, negx, posy, negy, posz, negz].

To speed up encoding of large textures in a cross-origin isolated page, opt into multithreading with useThreads: true. See Multithreaded Encoding.

See the API guide for more details.

For gltf-transform

For users of glTF Transform, use the provided ktx2 transform. In Node.js, pass an imageDecoder; browser builds provide one automatically.

import { ktx2 } from "ktx2-encoder/gltf-transform";
import sharp from "sharp";

const imageDecoder = async (buffer: Uint8Array) => {
  const { data, info } = await sharp(buffer).ensureAlpha().raw().toBuffer({ resolveWithObject: true });
  return { data: new Uint8Array(data), width: info.width, height: info.height };
};

await document.transform(
  ktx2({
    isUASTC: true,
    enableDebug: false,
    generateMipmap: true,
    imageDecoder
  })
);

The browser build loads its bundled, version-matched basis_encoder.wasm automatically, with no CDN fallback. To serve a matching WASM file from a custom location, set wasmUrl explicitly. See the advanced guide.

Tool

Open the page of ktx2 encoder tool, you can use it to encode your image to ktx2.

About

KTX2(.ktx2) encoder for images and gltf

Resources

Stars

Watchers

Forks

Releases

Used by

Contributors

Languages