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.
NOTICE: Work in progress.
Quickstart
npm install --save ktx-parse
const fs = require('fs');
const { read, write } = require('ktx-parse');
const inData = fs.readFileSync('./input.ktx2');
const inData = await fetch('./input.ktx2')
.then((r) => r.arrayBuffer())
.then((buffer) => new Uint8Array(buffer));
const container = read(inData);
const outData = write(container);
API
TODO
Encoding / Decoding
This library reads/writes KTX 2.0 containers, and provides access to the compressed texture data within the container. To decompress that data, or to compress existing texture data into GPU texture formats used by KTX 2.0, you'll need to use additional libraries, described below.
TODO: Describe options for compressing, transcoding, and decompressing KTX 2.0 payloads using Basis Universal.