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,readandLoader: the ways in.Formatnames 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, XGL, VTK, 3D Tiles, Gmsh and Medit, and for game and animation assetsMikuMikuDance(PMX, PMD, VMD),DirectX.x, MD2, MD3, MD5, IQM, SMD,MilkShape3D,Blitz3D, ASF/AMC, C3D, TRC, HTR and OGRE, each behind a cargo feature.document: theDocumentthey produce, a glTF 2.0 document (JSON and buffers) written out as a GLB as it is, flattened into a posedMeshon demand, and pruned to what aKeepasks 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: theEXT_meshopt_compressionandKHR_meshopt_compressionbitstreams.
Re-exports§
pub use document::FbxOptions;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
.gltfand 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_bytesoff leaves, as handles fromDocument::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_compressionandKHR_meshopt_compressionbitstreams, 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::Imageholds, from thebytescrate: 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
openandreaddon’t choose: what tokeep, where its files are found (assets), and itsformat. One loader reads any number of models, from any thread.
Enums§
- Error
Kind - 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 aFolderrooted there finds them.Loaderkeeps 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;namecan be a bare extension’s worth ("model.glb") or empty. The model can’t refer to other files: aLoaderwithAssetsfinds them.