Skip to content

beicause/bevy_basisu_loader

Repository files navigation

Bevy KTX2 BasisU texture loader

Build License Cargo Documentation

A cross-platform, size-optimized Bevy plugin that provides a KTX2 Basis Universal texture loader.

Although Bevy's ImageLoader has built-in support for Basis Universal textures via the basis-universal-rs crate, it has some limitations:

  1. It uses a relatively old version of Basis Universal
  2. No support for UASTC HDR yet
  3. No support for Web. Bevy can't be compiled to wasm32-unknown-emscripten and basis-universal-rs can't be compiled to wasm32-unknown-unknown
  4. It compiles both the encoder and transcoder and includes transcoding formats not supported by wgpu, which increases binary size

This plugin adds a loader for Basis Universal KTX2 textures with support for ETC1S, UASTC LDR and USATC HDR, and web support through JavaScript glue to call Basis Universal C++ library compiled with Emscripten which includes only the transcoder and necessary transcoding formats.

This doesn't include BasisU encoder. To encode textures to .ktx2, use the command line tool in Basis Universal repo.

Web demo: https://beicause.github.io/bevy_basisu_loader/

Usage

  1. Add the Cargo dependency:
bevy_basisu_loader = "0.2"
  1. Add BasisuLoaderPlugin:
use bevy_basisu_loader::BasisuLoaderPlugin;

pub fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_plugins(BasisuLoaderPlugin);
}
  1. Load ktx2 basis universal textures. Supports ETC1S and UASTC and D2, D2Array and Cube texture types. Only supports KTX2 format (zstd compression is supported). No support for .basis format.
    let image_handle = asset_server.load("gl_skybox_etc1s_cubemap_mips_12.basisu_ktx2");

⚠️Note: you have to rename the file extension to .basisu_ktx2 to load it with this BasisuLoader. This is a limitations of bevy because otherwise bevy will load .ktx2 file with its ImageLoader.

⚠️Note: The compressed texture dimensions must be a multiplier of block size. See gfx-rs/wgpu#7677 for more context.
block_size = 4, for etc1s/uastc_ldr/uastc_hdr_4x4
block_size = 4 or 6 (so both of them need to be satisfied), for uastc_hdr_6x6

Implementation details

To run on web, this repo uses a solution:

The crates/basisu_sys/ builds a high level wrapper of the basis universal C++ library. For native platforms, it builds and statically links the C++ dependency. For web, it contains a tool to build basisu vendor using Emscripten and produce js and wasm files. The basisu wrapper is designed so that it does not need to share memory with main Wasm module. Instead, its memory will be copied from/into main Wasm module through js. When building this plugin to wasm32-unknown-unknown, the basisu vendor js and wasm files will be embedded into binary and is called through wasm-bindgen and js-sys.

Run on web

TLDR: Just build your bevy application to wasm32-unknown-unknown normally.

The prebuilt wasm in crates/basisu_sys/wasm is automatically embedded in binary when building. It was prebuilt through CI with:

cargo r -p bevy_basisu_loader_sys --bin build-wasm-cli --features build-wasm-cli -- --emcc-flags="-sEVAL_CTORS -Os -flto=full" --wasm-opt-flags="-Os"

Bevy version compatibility

bevy bevy_basisu_loader
0.17 0.1, 0.2

License

Except where noted (below and/or in individual files), all code in this repository is dual-licensed under either:

at your option.

About

No description, website, or topics provided.

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Packages

No packages published