draco_decoder
draco_decoder is a Rust library for decoding Draco compressed meshes. It provides native and WebAssembly (WASM) support with efficient bindings to the official Draco C++ library.
Overview
-
Native:
The native part usescxxto create safe and ergonomic FFI bindings that directly connect to Draco's C++ decoding library. This allows efficient and zero-copy mesh decoding in native environments. -
WASM:
For WebAssembly targets,draco_decoderleverages the official Draco Emscripten build. It uses a JavaScript Worker to run the Draco decoder asynchronously, enabling non-blocking mesh decoding in the browser. The JavaScript implementation is available in a separate repository:
https://github.com/jiangheng90/draco_decoder_js.git
This design provides a unified Rust API while seamlessly switching between native and WASM implementations under the hood.
Build Guide
- Install essential tools for C++ development (cmake, C++ compiler, etc.)
cargo build
This crate has passed builds on the latest platforms. On Windows, only MSVC is supported.
Usage
Async API
use decode_mesh_with_config;
// Your Draco-encoded binary mesh data
let data: & = /* your Draco encoded data here */;
// Decode the mesh data asynchronously
if let Some = decode_mesh_with_config.await
Sync API (Native only)
use decode_mesh_with_config_sync;
// Your Draco-encoded binary mesh data
let data: & = /* your Draco encoded data here */;
// Decode the mesh data synchronously
if let Some = decode_mesh_with_config_sync
DracoDecodeConfig
The DracoDecodeConfig provides metadata about the decoded mesh:
// Access mesh information
let vertex_count = config.vertex_count;
let index_count = config.index_count;
let buffer_size = config.buffer_size;
let index_length = config.index_length;
// Access attributes
for attr in config.attributes
How It Works
The decoder uses a caching mechanism within the FFI that splits the decoding process into:
- Decode - Parse the Draco data
- Generate Config - Extract mesh metadata (vertex count, attributes, buffer size)
- Allocate & Copy - Allocate exact memory and copy decoded data
This approach achieves zero-copy data transfer since Rust can allocate the exact required memory based on the decoded metadata.
Performance
| Environment | Typical Decoding Time |
|---|---|
| Native (Release Build) | 3 ms – 7 ms |
| WebAssembly (WASM) | 30 ms – 50 ms |
Warnings
- This crate is work in progress and has not been extensively tested across all platforms.
- On WASM, data transfer between Rust and JS Worker incurs copy overhead. Using SharedArrayBuffer would avoid this but requires cross-origin isolation in browsers.