Skip to main content

Crate draco_io

Crate draco_io 

Source
Expand description

Format I/O layer for Draco geometry.

draco-io maps external 3D formats onto the geometry types from draco-core. It handles file/container concerns such as OBJ, PLY, FBX, glTF, GLB, scene hierarchy, and KHR_draco_mesh_compression; raw .drc bitstream encoding and decoding stays in draco-core.

§Supported Formats

FormatReadWriteDraco compression
OBJyesyesno
PLYyesyesno
FBXyesyesno
glTFyesyesyes
GLByesyesyes

§Feature Model

The default feature set enables all readers, all writers, optional compression support, and point-cloud Draco decoding. For smaller builds, use default-features = false and enable only the format features needed, such as gltf-reader, gltf-writer, obj-reader, or ply-writer.

§Geometry Contract

draco-io maps source formats onto the Draco geometry model. Meshes use triangle faces, Position is the required attribute, and Normal, Color, TexCoord, and Generic are preserved when the file format can represent them as Draco attributes. Scene support in the geometry model is limited to names, hierarchy, transforms, and mesh parts; materials, textures, cameras, lights, animation, skinning, structural metadata, and arbitrary format extras are not represented in that model.

§Document-preserving glTF compression

Decoding into the geometry model and re-emitting a fresh glTF necessarily drops anything the model does not represent (materials, textures, and so on). When the goal is to Draco-compress an existing glTF/GLB in place, use compress_gltf_bytes: it rewrites only the compressible mesh geometry and carries the rest of the document through untouched — materials, textures, images, samplers, cameras, nodes, animations, skins, extras, and unknown extensions all survive.

§Unified Trait API

All readers implement Reader and all writers implement Writer:

use draco_io::{Reader, Writer, ObjReader, ObjWriter};

// Generic read function
fn load<R: Reader>(path: &str) -> io::Result<Mesh> {
    let mut reader = R::open(path)?;
    reader.read_mesh()
}

// Generic write function
fn save<W: Writer>(mut writer: W, mesh: &Mesh) -> io::Result<()> {
    writer.add_mesh(mesh, Some("Model"))?;
    writer.write("output.ext")
}

// Works with any format
let mesh = load::<ObjReader>("input.obj")?;
save(ObjWriter::new(), &mesh)?;
save(PlyWriter::new(), &mesh)?;

§Format-Specific Features

While the trait provides a common interface, each writer has format-specific methods:

// OBJ: Named groups
let mut obj = ObjWriter::new();
obj.add_mesh(&mesh, Some("Cube"));

// PLY: Point clouds with colors
let mut ply = PlyWriter::new();
ply.add_points_with_colors(&points, &colors);

// FBX: Optional compression
let mut fbx = FbxWriter::new().with_compression(true);
fbx.add_mesh(&mesh, Some("Model"));

// glTF: Custom quantization, multiple output formats
let mut gltf = GltfWriter::new();
gltf.add_draco_mesh(&mesh, Some("Model"), None)?;  // Use default quantization
gltf.write_glb("output.glb")?;              // Binary GLB
gltf.write_gltf("out.gltf", "out.bin")?;   // Separate files
gltf.write_gltf_embedded("embedded.gltf")?; // Pure text

§glTF/GLB with Draco Compression

The gltf_reader and gltf_writer modules provide focused support for Draco triangle meshes through KHR_draco_mesh_compression. They validate the container enough to avoid silently dropping required glTF features; they are not a full glTF SDK.

Three output formats are available:

  • GLB: Binary container (single .glb file)
  • glTF + .bin: JSON with separate binary file
  • glTF (embedded): Pure text JSON with base64 data URIs

§Reading Draco-compressed glTF

use draco_io::gltf_reader::GltfReader;

let reader = GltfReader::open("model.glb")?;
for (info, mesh) in reader.decode_all_draco_meshes()? {
    println!("Mesh '{}' has {} faces",
        info.mesh_name.unwrap_or_default(),
        mesh.num_faces());
}

§Writing Draco-compressed GLB

use draco_io::gltf_writer::GltfWriter;

let mut writer = GltfWriter::new();
writer.add_draco_mesh(&mesh, Some("MyMesh"), None)?;  // Use default quantization

// Option 1: Binary GLB (most compact)
writer.write_glb("output.glb")?;

// Option 2: Separate JSON and binary
writer.write_gltf("output.gltf", "output.bin")?;

// Option 3: Pure text with embedded data (no external files)
writer.write_gltf_embedded("output.gltf")?;

Re-exports§

pub use fbx_reader::FbxMemoryReader;fbx-reader
pub use fbx_reader::FbxReader;fbx-reader
pub use fbx_writer::FbxWriter;fbx-writer
pub use gltf_geometry::decode_geometry;gltf-reader or gltf-writer
pub use gltf_geometry::AccessorSource;gltf-reader or gltf-writer
pub use gltf_geometry::DecodedAccessor;gltf-reader or gltf-writer
pub use gltf_geometry::GltfError;gltf-reader or gltf-writer
pub use gltf_compress::compress_gltf_value;gltf-writer
pub use gltf_compress::compress_gltf_bytes;gltf-reader and gltf-writer
pub use gltf_compress::compress_gltf_bytes_with_base_path;gltf-reader and gltf-writer
pub use gltf_reader::DracoPrimitiveInfo;gltf-reader
pub use gltf_reader::GltfReader;gltf-reader
pub use gltf_writer::GltfWriteError;gltf-writer
pub use gltf_writer::GltfWriter;gltf-writer
pub use obj_reader::ObjReader;obj-reader
pub use obj_writer::ObjWriter;obj-writer
pub use ply_format::PlyFormat;
pub use ply_reader::PlyReader;ply-reader
pub use ply_writer::PlyWriter;ply-writer
pub use scene::flatten_to_scene;scene
pub use scene::MeshInstance;scene
pub use scene::Scene;scene
pub use scene::SceneNode;scene
pub use scene::SceneReader;scene
pub use scene::SceneWriter;scene
pub use scene::Transform;scene
pub use traits::PointCloudReader;
pub use traits::PointCloudWriter;
pub use traits::ReadFromBytes;
pub use traits::Reader;
pub use traits::WriteToBytes;
pub use traits::Writer;

Modules§

fbx_readerfbx-reader
FBX binary format reader for meshes.
fbx_writerfbx-writer
FBX binary format writer for meshes.
gltf_compressgltf-writer
Document-preserving glTF Draco compression (keeps materials, textures, etc.).
gltf_geometrygltf-reader or gltf-writer
Reader-agnostic glTF geometry decoding.
gltf_readergltf-reader
glTF/GLB reader with full scene graph and mesh decoding support.
gltf_writergltf-writer
glTF/GLB writer with Draco mesh compression support. glTF/GLB writer with Draco mesh compression support.
obj_readerobj-reader
OBJ format reader for meshes and point clouds.
obj_writerobj-writer
OBJ format writer for meshes and point clouds.
ply_format
Shared PLY storage-format enum.
ply_readerply-reader
PLY format reader for meshes and point clouds.
ply_writerply-writer
PLY format writer for meshes and point clouds.
scenescene
Scene-graph layer: data model and traits for formats that represent hierarchy.
traits
Common traits for readers and writers.