ktx2-encoder
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
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].
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 basis_encoder.wasm by default and falls back to the project's CDN copy if the bundled asset cannot be fetched. To serve a matching WASM file from a custom location or CDN, 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.