pub struct Mesh {
pub positions: Vec<f64>,
pub vertex_color: Vec<f64>,
pub normals: Vec<f64>,
pub texcoords: Vec<f64>,
pub indices: Vec<u32>,
pub face_arities: Vec<u32>,
pub vertex_color_indices: Vec<u32>,
pub texcoord_indices: Vec<u32>,
pub normal_indices: Vec<u32>,
pub material_id: Option<usize>,
}
Expand description
A mesh made up of triangles loaded from some OBJ
file.
It is assumed that all meshes will at least have positions, but normals and
texture coordinates are optional. If no normals or texture coordinates where
found then the corresponding Vec
s in the Mesh
will be empty. Values are
stored packed as f32
s (or f64
s with the use_f64 feature) in flat
Vec
s.
For examples the positions
member of a loaded mesh will contain [x, y, z, x, y, z, ...]
which you can then use however you like. Indices are also
loaded and may re-use vertices already existing in the mesh. This data is
stored in the indices
member.
§Example:
Load the Cornell box and get the attributes of the first vertex. It’s
assumed all meshes will have positions (required), but normals and texture
coordinates are optional, in which case the corresponding Vec
will be
empty.
let cornell_box = tobj::load_obj("obj/cornell_box.obj", &tobj::GPU_LOAD_OPTIONS);
assert!(cornell_box.is_ok());
let (models, materials) = cornell_box.unwrap();
let mesh = &models[0].mesh;
let i = mesh.indices[0] as usize;
// pos = [x, y, z]
let pos = [
mesh.positions[i * 3],
mesh.positions[i * 3 + 1],
mesh.positions[i * 3 + 2],
];
if !mesh.normals.is_empty() {
// normal = [x, y, z]
let normal = [
mesh.normals[i * 3],
mesh.normals[i * 3 + 1],
mesh.normals[i * 3 + 2],
];
}
if !mesh.texcoords.is_empty() {
// texcoord = [u, v];
let texcoord = [mesh.texcoords[i * 2], mesh.texcoords[i * 2 + 1]];
}
Fields§
§positions: Vec<f64>
Flattened 3 component floating point vectors, storing positions of vertices in the mesh.
vertex_color: Vec<f64>
Flattened 3 component floating point vectors, storing the color associated with the vertices in the mesh.
Most meshes do not have vertex colors. If no vertex colors are specified this will be empty.
normals: Vec<f64>
Flattened 3 component floating point vectors, storing normals of vertices in the mesh.
Not all meshes have normals. If no normals are specified this will be empty.
texcoords: Vec<f64>
Flattened 2 component floating point vectors, storing texture coordinates of vertices in the mesh.
Not all meshes have texture coordinates. If no texture coordinates are specified this will be empty.
indices: Vec<u32>
Indices for vertices of each face. If loaded with
triangulate
set to true
each face in the
mesh is a triangle.
Otherwise face_arities
indicates how many
indices are used by each face.
When single_index
is set to true
,
these indices are for all of the data in the mesh. Positions,
normals and texture coordinaes.
Otherwise normals and texture coordinates have their own indices,
each.
face_arities: Vec<u32>
The number of vertices (arity) of each face. Empty if loaded with
triangulate
set to true
or if the mesh constists only of
triangles.
The offset for the starting index of a face can be found by iterating
through the face_arities
until reaching the desired face, accumulating
the number of vertices used so far.
vertex_color_indices: Vec<u32>
The indices for vertex colors. Only present when the
merging
feature is enabled, and
empty unless the corresponding load option is set to true
.
texcoord_indices: Vec<u32>
The indices for texture coordinates. Can be omitted by setting
single_index
to true
.
normal_indices: Vec<u32>
The indices for normals. Can be omitted by setting single_index
to
true
.
material_id: Option<usize>
Optional material id associated with this mesh. The material id indexes
into the Vec of Materials loaded from the associated MTL
file