Skip to main content

Crate polysmelt

Crate polysmelt 

Source
Expand description

Pure-Rust 3D asset interchange, with glTF as the hub: 60-some formats read into a glTF 2.0 Document, and written back out as GLB.

§Quick start

use polysmelt::document::Pose;

// Any format Polysmelt reads: told by the extension, or else by the file's first bytes. The
// textures and libraries the model names are found beside it.
let doc = polysmelt::open("models/robot.fbx")?;

// What the file says about itself (format, units, axes, the tool that wrote it), and what
// the reader converted to glTF's conventions.
let info = doc.source_info();
println!("{:?} {:?}, up {:?}", info.format, info.version, info.up_axis);

// What it draws, counted from the JSON alone, or flattened into posed triangles.
println!("{} triangles", doc.triangles());
if let Some(mesh) = doc.flatten(Pose::Rest)? {
    println!("{} vertices", mesh.prims.iter().map(|p| p.positions.len()).sum::<usize>());
}

// And out as a GLB, everything it holds kept.
doc.write_glb(std::io::BufWriter::new(std::fs::File::create("robot.glb")?))?;

A model in memory is read, and a Loader chooses what open and read don’t: what to Keep, where the files a model names are found, and its Format.

use polysmelt::{Keep, Loader, assets::Memory};

let obj = "mtllib box.mtl\nusemtl red\nv 0 0 0\nv 1 0 0\nv 0 1 0\nf 1 2 3\n";
let mtl = "newmtl red\nKd 1 0 0\n";
let doc = Loader::new()
    .assets(Memory::new().with("box.mtl", mtl.as_bytes()))
    .keep(Keep::ALL.with_animations(false))
    .read(obj.as_bytes(), "box.obj")?;
assert_eq!(doc.triangles(), 1);
assert_eq!(doc.json()["materials"][0]["name"], "red");
let glb: Vec<u8> = doc.to_glb()?;

§What’s here

  • open, read and Loader: the ways in. Format names what they read: STL, PLY, MagicaVoxel, OBJ, FBX, 3DS, 3MF, USD, glTF, SketchUp, BVH, COLLADA, VRM, OFF, X3D/VRML, DXF, LAS/LAZ, PCD, E57, text point clouds, AMF, LightWave, AC3D, OpenFlight, OpenGEX, Irrlicht and XGL, and for game and animation assets MikuMikuDance (PMX, PMD, VMD), DirectX .x, MD2, MD3, MD5, IQM, SMD, MilkShape 3D, Blitz3D, ASF/AMC, C3D, TRC, HTR and OGRE, each behind a cargo feature.
  • document: the Document they produce, a glTF 2.0 document (JSON and buffers) written out as a GLB as it is, flattened into a posed Mesh on demand, and pruned to what a Keep asks for.
  • mesh: the flat triangle-list model, written out as a GLB.
  • rig: typed views of a document’s node tree, skins and animations, for playing and retargeting motion.
  • source: what a file said about itself: units, axes, the tool that wrote it.
  • assets: how readers find the textures and material libraries a model refers to.
  • images: handles on a document’s images, for those a load found but didn’t read.
  • gltf: GLB packing, and meshopt compression added to or decoded away from a GLB.
  • meshopt: the EXT_meshopt_compression and KHR_meshopt_compression bitstreams.

Re-exports§

pub use document::Document;
pub use document::Keep;
pub use serde_json;

Modules§

assets
Finding the files a model refers to (MTL libraries, textures) and turning texture bytes into embeddable images.
document
The common form every reader produces: a glTF 2.0 document, its JSON and its buffers’ bytes, carrying whatever the source had (scene graph, animation, skins, morph targets, materials, cameras, lights, names, extras).
gltf
glTF 2.0 containers: packing a .gltf and the files it names into one GLB, and adding or decoding away meshopt compression (EXT_meshopt_compression, KHR_meshopt_compression).
images
Images a document names without having read them: what a load with Keep::image_bytes off leaves, as handles from Document::images.
mesh
A model as plain triangle lists (and line and point lists): what the format readers produce, written out as a GLB.
meshopt
The EXT_meshopt_compression and KHR_meshopt_compression bitstreams, ported from meshoptimizer: the attribute encoder (codec versions 0 and 1, levels 0 to 3), the triangle and index sequence encoders, their bounds, the octahedral, quaternion, exponential and color filter encoders, and decoders for every mode and filter.
rig
Typed views of a Document’s node tree, skins and animations, for code that plays or retargets motion rather than drawing triangles: Document::rig.
source
What a model’s file says about itself, the same way whatever the format: its units, which way is up, its frame rate, who made it and with what, and when.

Structs§

Bytes
The shared byte buffer mesh::Image holds, from the bytes crate: cloning or slicing one doesn’t copy its bytes. A cheaply cloneable and sliceable chunk of contiguous memory.
Error
What can go wrong reading or writing an asset.
Loader
How to read a model, for what open and read don’t choose: what to keep, where its files are found (assets), and its format. One loader reads any number of models, from any thread.

Enums§

ErrorKind
The kind of an Error: what a caller can do about it.
Format
A model format Polysmelt reads, whether or not this build has its cargo feature.

Functions§

open
Reads the model at path, all of it. The format is told by the path’s extension (in any case), or, when that names none, by the file’s first bytes; the files it refers to (MTL libraries, textures, glTF buffers) are found from its folder, as a Folder rooted there finds them. Loader keeps less, finds them elsewhere, or names the format.
read
Reads a model in memory, all of it. Its format is told by name’s extension (in any case), or, when that names none, by its first bytes; name can be a bare extension’s worth ("model.glb") or empty. The model can’t refer to other files: a Loader with Assets finds them.

Type Aliases§

Result
A result with Error.