What is ktx-parse?
The ktx-parse npm package is a utility for parsing KTX (Khronos Texture) files, which are used for storing textures in a standardized format. This package allows developers to read and manipulate KTX files in JavaScript, making it useful for applications that need to handle texture data, such as game engines, 3D rendering applications, and other graphics-related software.
What are ktx-parse's main functionalities?
Parsing KTX Files
This feature allows you to parse a KTX file and extract its contents. The code sample demonstrates how to read a KTX file from the filesystem and parse it using the ktx-parse package.
const ktxParse = require('ktx-parse');
const fs = require('fs');
const ktxData = fs.readFileSync('path/to/texture.ktx');
const parsedKTX = ktxParse(ktxData);
console.log(parsedKTX);
Accessing Texture Data
This feature allows you to access the raw texture data from a parsed KTX file. The code sample shows how to extract the texture data from the first mip level of the parsed KTX file.
const ktxParse = require('ktx-parse');
const fs = require('fs');
const ktxData = fs.readFileSync('path/to/texture.ktx');
const parsedKTX = ktxParse(ktxData);
const textureData = parsedKTX.levels[0].levelData;
console.log(textureData);
Extracting Metadata
This feature allows you to extract metadata from a KTX file. The code sample demonstrates how to read the key-value metadata pairs from a parsed KTX file.
const ktxParse = require('ktx-parse');
const fs = require('fs');
const ktxData = fs.readFileSync('path/to/texture.ktx');
const parsedKTX = ktxParse(ktxData);
const metadata = parsedKTX.keyValue;
console.log(metadata);
Other packages similar to ktx-parse
ktx
The ktx package is another utility for working with KTX files. It provides similar functionality to ktx-parse, including parsing and manipulating KTX files. However, ktx-parse is often preferred for its simplicity and ease of use.
three
The three package is a popular 3D library that includes support for loading and using KTX textures. While it offers a broader range of features for 3D rendering, it may be more complex to use if you only need to work with KTX files.
gl-texture2d
The gl-texture2d package is a WebGL utility for handling 2D textures, including KTX files. It provides functionality for creating and managing textures in WebGL, making it a good choice for web-based graphics applications.
ktx-parse
KTX 2.0 (.ktx2) parser and serializer.
Quickstart
Install:
npm install --save ktx-parse
Import:
import { read, write } from 'ktx-parse';
Usage:
const container = read(data );
const data = write(container);
See API documentation for more details:
Encoding / Decoding
KTX-Parse reads/writes KTX 2.0 containers, and provides access to the compressed texture data within the container. To decompress that texture data, or to compress existing texture data into GPU texture formats used by KTX 2.0, you'll need to use additional libraries such as encoders or transcoders.
Encoding:
Encoding GPU textures is a slow process, and should be completed at development/authoring time so that the compressed texture can be transmitted to the viewing device. GPU textures require much less GPU memory than image formats like PNG or JPEG, and can be uploaded to the GPU quickly with less impact on framerate. GPU textures can also have smaller filesizes in many, but not all, cases. See the Basis documentation for details on this process.
The following tools may be used to produce Basis Universal compressed textures in KTX 2.0 (.ktx2
) containers, which ktx-parse
can then read or edit:
Transcoding / Decoding:
Basis Universal texture formats (ETC1S and UASTC) cannot be directly read by a GPU, but are designed to be very efficiently rewritten into many of the specific GPU texture formats that different GPUs require. This process is called transcoding, and typically happens on the viewing device after a target output format (e.g. ETC1, ASTC, BC1, ...) is chosen. These transcoders can also fully decode texture data to uncompressed RGBA formats, if raw pixel data is required.
- BinomialLLC/basis_universal provides official C++ and WebAssembly transcoders, which support all Basis Universal input formats and can transcode to any output format (with appropriate compilation flags). With common settings, a transcoder will likely be > 200kb on web. This transcoder can read KTX 2.0 files directly.
- KhronosGroup/Universal-Texture-Transcoders provides very small, fast WebAssembly transcoders each supporting only a single output texture format. Each transcoder is roughly 10-20kb, and the viewing device can choose which transcoder to download, as appropriate. These transcoders cannot read KTX 2.0 files directly. Instead, unpack the KTX 2.0 files with
ktx-parse
first, then transcode the mip levels using a low-level transcoder. Only UASTC texture formats currently supported.