tobj
– Tiny OBJ Loader
Inspired by Syoyo’s excellent tinyobjloader
.
Aims to be a simple and lightweight option for loading OBJ
files.
Just returns two Vec
s containing loaded models and materials.
Triangulation
Meshes can be triangulated on the fly or left as-is.
Only polygons that are trivially convertible to triangle fans are supported. Arbitrary polygons may not behave as expected. The best solution would be to convert your mesh to solely consist of triangles in your modeling software.
Optional – Normals, Texture Coordinates and Vertex Colors
It is assumed that all meshes will at least have positions, but normals, texture coordinates and vertex colors are optional.
If no normals, texture coordinates or vertex colors are found, then the
corresponding Vec
s for the Mesh
will be empty.
Flat Data
Values are stored packed as floats in flat Vec
s.
For example, the positions
member of a Mesh
will contain [x, y, z, x, y, z, ...]
which you can then use however you like.
Indices
Indices are also loaded and may re-use vertices already existing in the mesh,
this data is stored in the indices
member.
When a Mesh
contains per vertex per face normals or texture coordinates,
positions can be duplicated to be per vertex per face too via the
single_index
flag. This potentially changes the topology (faces may become
disconnected even though their vertices still share a position in space).
By default separate indices for normals and texture coordinates are created.
This also guarantees that the topology of the Mesh
does not change when
either of the latter are specified per vertex per face.
Materials
Standard MTL
attributes are supported too. Any unrecognized parameters will be
stored in a HashMap
containing the key-value pairs of the unrecognized
parameter and its value.
Features
-
ahash
– On by default. UseAHashMap
for hashing when reading files and merging vertices. To disable and use the slowerHashMap
instead, unset default features inCargo.toml
:[] = false
-
merging
– Adds support for merging identical vertex positions on disconnected faces during import.Warning: this feature uses const generics and thus requires at least a
beta
toolchain to build. -
reordering
– Adds support for reordering the normal- and texture coordinate indices. -
async
– Adds support for async loading of obj files from a buffer, with an async material loader. Useful in environments that do not support blocking IO (e.g. WebAssembly).
Documentation
Rust docs can be found here.
Installation
Add the crate as a dependency in your
Cargo.toml
and you’re all set!
Example
The print mesh example (also below) loads an OBJ
file from the command line and prints out some information about its faces,
vertices, and materials.