pub struct TMFMesh { /* private fields */ }Expand description
Representation of a TMF mesh. Can be loaded from disk, imported from diffrent format, saved to disk, and exported to a diffrent format, or created using special functions. Any mesh created at run time should but does not have to be checked before saving with Self::verify call. If the mesh is known to be OK before saving this step can be skipped(even tough it is still advised).
Implementations§
Source§impl TMFMesh
impl TMFMesh
Sourcepub fn reorder_data(&mut self)
pub fn reorder_data(&mut self)
Reorders mesh data to be more efficiently laid out. Reduces mesh size slightly. Not needed if Self::unify_index_data was called before.
§Example
let old_vertices = tmf_mesh.get_vertices().unwrap();
let old_vertex_triangles = tmf_mesh.get_vertex_triangles().unwrap();
// Reordering data changes how it is laid out.
tmf_mesh.reorder_data();
let new_vertices = tmf_mesh.get_vertices().unwrap();
let new_vertex_triangles = tmf_mesh.get_vertex_triangles().unwrap();
// New data is not the same as the old data
assert_ne!(old_vertices,new_vertices);
assert_ne!(old_vertex_triangles,new_vertex_triangles);Sourcepub fn unify_index_data(&mut self)
pub fn unify_index_data(&mut self)
Changes mesh data to make all index arrays(e.g. vertex_triangle_array,normal_triangle_array, etc.) exactly the same. Does not support custom index segments, and will leave them unaffected.
Very often drastically reduces mesh size.
§Example
// If all index arrays are different at first
assert_ne!(tmf_mesh.get_vertex_triangles(),tmf_mesh.get_normal_triangles());
assert_ne!(tmf_mesh.get_vertex_triangles(),tmf_mesh.get_uv_triangles());
assert_ne!(tmf_mesh.get_normal_triangles(),tmf_mesh.get_uv_triangles());
// Ensure mesh all index arrays in mess are exactly the same.
tmf_mesh.unify_index_data();
// All index arrays are now exactly the same.
assert_eq!(tmf_mesh.get_vertex_triangles(),tmf_mesh.get_normal_triangles());
assert_eq!(tmf_mesh.get_vertex_triangles(),tmf_mesh.get_uv_triangles());
assert_eq!(tmf_mesh.get_normal_triangles(),tmf_mesh.get_uv_triangles());
// If index arrays are exactly the same, only cost of `unify_index_data` call is the
// cost of checking that the index arrays are exactly the same.
tmf_mesh.unify_index_data();Sourcepub fn set_vertices<T: Into<Vec<Vector3>>>(
&mut self,
vertices: T,
) -> Option<Vec<Vector3>>
pub fn set_vertices<T: Into<Vec<Vector3>>>( &mut self, vertices: T, ) -> Option<Vec<Vector3>>
Sets mesh vertex array and returns old vertex array if present. New mesh data is not checked during this function call, so to ensure mesh is valid call Self::verify before saving.
§Examples
// Set the vertices of the mesh
mesh.set_vertices(vertices); // Change the vertices for some other vertices...
let old_vertices = mesh.set_vertices(new_vertices).expect("Mesh had no vertices!");
// ... and the do something with old vertices
do_something(&old_vertices);Sourcepub fn set_normals<T: Into<Vec<Vector3>>>(
&mut self,
normals: T,
) -> Option<Vec<Vector3>>
pub fn set_normals<T: Into<Vec<Vector3>>>( &mut self, normals: T, ) -> Option<Vec<Vector3>>
Sets mesh normal array and returns old normal array if present. New mesh data is not checked during this function call, so to ensure mesh is valid call verify before saving.
§Examples
// Set the normals of the mesh
mesh.set_normals(normals); // Change the normals of this mesh for some other normals...
let old_normals = mesh.set_normals(new_normals).expect("Mesh had no normals!");
// ... and the do something with old normals
do_something(&old_normals);Sourcepub fn set_tangents<T: Into<Vec<Tangent>>>(
&mut self,
tangents: T,
) -> Option<Vec<Tangent>>
pub fn set_tangents<T: Into<Vec<Tangent>>>( &mut self, tangents: T, ) -> Option<Vec<Tangent>>
Sets mesh tangent array and returns old tangent array if present. New mesh data is not checked during this function call, so to ensure mesh is valid call verify before saving.
§Examples
// Set the tangents of the mesh
mesh.set_tangents(tangents); // Change the tangents of this mesh for some other tangents...
let old_tangents = mesh.set_tangents(new_tangents).expect("Mesh had no tangents!");
// ... and the do something with old tangents
do_something(&old_tangents);Sourcepub fn set_uvs<T: Into<Vec<Vector2>>>(&mut self, uvs: T) -> Option<Vec<Vector2>>
pub fn set_uvs<T: Into<Vec<Vector2>>>(&mut self, uvs: T) -> Option<Vec<Vector2>>
Sets mesh uv array and returns old uv array if present. New mesh daata is not checked during this function call, so to ensure mesh is valid call Self::verify before saving.
§Examples
// Set the uvs of the mesh
mesh.set_uvs(uvs); // Change the uvs of this mesh for some other normals...
let old_uvs = mesh.set_uvs(new_uvs).expect("Mesh had no uvs!");
// ... and the do something with old uvs
do_something(&old_uvs);Sourcepub fn set_vertex_triangles<T: Into<Vec<IndexType>>>(
&mut self,
triangles: T,
) -> Option<Vec<IndexType>>
pub fn set_vertex_triangles<T: Into<Vec<IndexType>>>( &mut self, triangles: T, ) -> Option<Vec<IndexType>>
Sets vertex index array to triangles and returns old triangles if present.
§Example
mesh.set_vertex_triangles(triangles);Sourcepub fn set_normal_triangles<T: Into<Vec<IndexType>>>(
&mut self,
triangles: T,
) -> Option<Vec<IndexType>>
pub fn set_normal_triangles<T: Into<Vec<IndexType>>>( &mut self, triangles: T, ) -> Option<Vec<IndexType>>
Sets normal index array to triangles and returns old triangles if present.
§Example
mesh.set_normal_triangles(triangles);Sourcepub fn set_uv_triangles<T: Into<Vec<IndexType>>>(
&mut self,
triangles: T,
) -> Option<Vec<IndexType>>
pub fn set_uv_triangles<T: Into<Vec<IndexType>>>( &mut self, triangles: T, ) -> Option<Vec<IndexType>>
Sets uv index array to triangles and returns old triangles if present.
§Example
mesh.set_uv_triangles(triangles);Sourcepub fn set_tangent_triangles<T: Into<Vec<IndexType>>>(
&mut self,
triangles: T,
) -> Option<Vec<IndexType>>
pub fn set_tangent_triangles<T: Into<Vec<IndexType>>>( &mut self, triangles: T, ) -> Option<Vec<IndexType>>
Sets tangent index array to triangles and returns old triangles if present.
§Example
mesh.set_tangent_triangles(triangles);Sourcepub fn get_vertices(&self) -> Option<&[Vector3]>
pub fn get_vertices(&self) -> Option<&[Vector3]>
Sourcepub fn get_normals(&self) -> Option<&[Vector3]>
pub fn get_normals(&self) -> Option<&[Vector3]>
Sourcepub fn get_tangents(&self) -> Option<&[Tangent]>
pub fn get_tangents(&self) -> Option<&[Tangent]>
Sourcepub fn get_vertex_triangles(&self) -> Option<&[IndexType]>
pub fn get_vertex_triangles(&self) -> Option<&[IndexType]>
Sourcepub fn get_normal_triangles(&self) -> Option<&[IndexType]>
pub fn get_normal_triangles(&self) -> Option<&[IndexType]>
Sourcepub fn get_uv_triangles(&self) -> Option<&[IndexType]>
pub fn get_uv_triangles(&self) -> Option<&[IndexType]>
Sourcepub fn get_tangent_triangles(&self) -> Option<&[IndexType]>
pub fn get_tangent_triangles(&self) -> Option<&[IndexType]>
Sourcepub fn get_vertex_buffer(&self) -> Option<Box<[Vector3]>>
pub fn get_vertex_buffer(&self) -> Option<Box<[Vector3]>>
Returns array containing points laid out in such a way that each 3 points create the next triangle.
If mesh has no vertex array or no vertex triangle array None is returned.
§Example
let vert_buff = mesh.get_vertex_buffer().expect("Could not create the array of points creating triangles!");
// The same number of triangles created by points and triangles created by indices
assert!(vert_buff.len() == vertex_triangles.len());Sourcepub fn get_normal_buffer(&self) -> Option<Box<[Vector3]>>
pub fn get_normal_buffer(&self) -> Option<Box<[Vector3]>>
Returns array containing normals laid out in such a way that each 3 normals create the next triangle.
If mesh has no normal array or no normal triangle array None is returned.
§Example
let normal_buff = mesh.get_normal_buffer().expect("Could not create the array of normals creating triangles!");
// The same number of triangles created by points and triangles created by indices
assert!(normal_buff.len() == normal_triangles.len());Sourcepub fn get_uv_buffer(&self) -> Option<Box<[Vector2]>>
pub fn get_uv_buffer(&self) -> Option<Box<[Vector2]>>
Returns array containing UV coridnates laid out in such a way that each 3 cordiantes create the next triangle.
If mesh has no UV array or no uv triangle array None is returned.
§Example
let uv_buff = mesh.get_uv_buffer().expect("Could not create the array of uvs creating triangles!");
// The same number of triangles created by points and triangles created by indices
assert!(uv_buff.len() == uv_triangles.len());Sourcepub fn normalize(&mut self)
pub fn normalize(&mut self)
Normalizes normal array of the mesh, if it is present.
§Examples
// Some mesh with normals which are not normalzed
mesh.set_normals(normals);
// Normalize all normals
mesh.normalize();
// All normals are normalised (their magnitude is equal to 1)
for normal in mesh.get_normals().expect("Mesh has no normals"){
let mag = magnitude(*normal);
// Magnitude of the normal is not exactly equal to 1
// because of imprecision of floating-point numbers.
assert!(mag > 0.99999 && mag < 1.00001);
}Normalising a mesh without normals is a NOP.
// Mesh has no normals
assert!(mesh.get_normals().is_none());
// This does nothing
mesh.normalize();Sourcepub fn verify(&self) -> Result<(), TMFIntegrityStatus>
pub fn verify(&self) -> Result<(), TMFIntegrityStatus>
Checks if mesh is valid and can be saved.
// Get the tmf mesh form somewhere
mesh.verify().expect("Mesh had errors!");§Errors
Returns a TMFIntegrityStatus if mesh is invalid.
Sourcepub fn read_from_obj<R: Read>(reader: &mut R) -> Result<Vec<(Self, String)>>
pub fn read_from_obj<R: Read>(reader: &mut R) -> Result<Vec<(Self, String)>>
Reads tmf meshes from a .obj file in reader
In order to enable triangulation while importing .obj files feature triangulation must be used. It is still highly experimental so read documentation before enabling. It is highly encouraged to just triangulate .obj files before importing them.
§Example
// Open the file with the .obj model
let mut file = File::open(dot_obj_path).expect("Could not open .obj file!");
// And multiple meshes from it
let meshes = TMFMesh::read_from_obj(&mut file).expect("Could not parse .obj file!");
for (mesh,name) in meshes{
// Do something with the mesh and name
do_something(mesh,name);
}§Errors
Returns IO error if it occurs.
Sourcepub fn read_from_obj_one<R: Read>(reader: &mut R) -> Result<(Self, String)>
pub fn read_from_obj_one<R: Read>(reader: &mut R) -> Result<(Self, String)>
Reads a single named tmf mesh from a .obj file in reader, if more than one mesh present an error will be returned.
In order to enable triangulation while importing .obj files feature triangulation must be used. It is still highly experimental so read documentation before enabling. It is highly encouraged to just triangulate .obj files before importing them.
§Example
// Open the file with the .obj model
let mut file = File::open(dot_obj_path).expect("Could not open .obj file!");
// And read a mesh from it
let (mesh,name) = TMFMesh::read_from_obj_one(&mut file).expect("Could not parse .obj file!");§Errors
Returns IO error if it occurs, or wrong mesh count.
Sourcepub fn write_obj<W: Write, S: Borrow<str>>(
meshes: &[(TMFMesh, S)],
w: &mut W,
) -> Result<()>
pub fn write_obj<W: Write, S: Borrow<str>>( meshes: &[(TMFMesh, S)], w: &mut W, ) -> Result<()>
Sourcepub fn write_tmf_one<W: Write, S: Borrow<str>>(
&self,
w: &mut W,
p_info: &TMFPrecisionInfo,
name: S,
) -> Result<(), TMFExportError>
pub fn write_tmf_one<W: Write, S: Borrow<str>>( &self, w: &mut W, p_info: &TMFPrecisionInfo, name: S, ) -> Result<(), TMFExportError>
Writes this TMF Mesh to w.
§Example
let mut output = File::create(path).expect("Could not create file!");
// Settings dictating how to save the mesh
let precision_info = TMFPrecisionInfo::default();
mesh.write_tmf_one(&mut output,&precision_info,"mesh_name").expect("Could not save .tmf mesh!");;§Errors
Returns IO error if occurs.
Sourcepub fn write_tmf<W: Write, S: Borrow<str>>(
meshes_names: &[(Self, S)],
w: &mut W,
p_info: &TMFPrecisionInfo,
) -> Result<(), TMFExportError>
pub fn write_tmf<W: Write, S: Borrow<str>>( meshes_names: &[(Self, S)], w: &mut W, p_info: &TMFPrecisionInfo, ) -> Result<(), TMFExportError>
Writes a number of TMF meshes into one file.
§Example
let mut output = File::create(path).expect("Could not create file!");
// Settings dictating how to save the mesh
let precision_info = TMFPrecisionInfo::default();
TMFMesh::write_tmf(&meshes,&mut output, &precision_info).expect("Could not save .tmf file!");§Errors
Returns IO error if occurs.
Sourcepub fn empty() -> Self
pub fn empty() -> Self
Creates an empty TMF Mesh(mesh with no data). Equivalent to TMFMesh::default.
§Example
// Creates an empty mesh with no data
let mesh = TMFMesh::empty();Sourcepub fn read_tmf<R: Read>(
reader: &mut R,
) -> Result<Vec<(Self, String)>, TMFImportError>
pub fn read_tmf<R: Read>( reader: &mut R, ) -> Result<Vec<(Self, String)>, TMFImportError>
Reads all meshes from a .tmf file.
§Example
// Open file containg the meshes
let mut file = File::open(tmf_path).expect("Could not open .tmf file");
// Get meshes and their names
let meshes = TMFMesh::read_tmf(&mut file).expect("Could not load .tmf mesh!");
for (mesh,name) in meshes{
// Do some thing with each mesh and name(eg. use in game)
do_something(mesh,name);
}§Errors
Returns: an IO error if it occurs, NotTMFFile if not tmf file, NewerVersionRequired if a newer importer is required for importing the file, and other errors for malformed tmf files.
Sourcepub async fn read_tmf_async<R: Read>(
reader: &mut R,
) -> Result<Vec<(Self, String)>, TMFImportError>
pub async fn read_tmf_async<R: Read>( reader: &mut R, ) -> Result<Vec<(Self, String)>, TMFImportError>
Async version of Self::read_tmf.
§Example
async fn load_meshes_files<R:std::io::Read>(src:&mut [R])->Result<Vec<(TMFMesh,String)>,TMFImportError>{
let mut meshes = Vec::new();
for source in src{
meshes.extend(TMFMesh::read_tmf_async(source).await?);
}
Ok(meshes)
}Sourcepub fn read_tmf_one<R: Read>(
reader: &mut R,
) -> Result<(Self, String), TMFImportError>
pub fn read_tmf_one<R: Read>( reader: &mut R, ) -> Result<(Self, String), TMFImportError>
Reads a single mesh from a .tmf file. Returns Err if no meshes present or more than one mesh present.
// Open the file containing the mesh
let mut file = File::open(tmf_path).expect("Could not open .tmf file");
// Read mesh and mesh name form file
let (mesh,name) = TMFMesh::read_tmf_one(&mut file).expect("Could not load .tmf mesh!");§Errors
Returns: an IO error if it occurs, NotTMFFile if not tmf file, NewerVersionRequired if a newer importer is required for importing the file, and other errors for malformed tmf files. This function also returns NoMeshes or TooManyMeshes if wrong mesh count present.
Sourcepub async fn read_tmf_one_async<R: Read>(
reader: &mut R,
) -> Result<(Self, String), TMFImportError>
pub async fn read_tmf_one_async<R: Read>( reader: &mut R, ) -> Result<(Self, String), TMFImportError>
Async version of Self::read_tmf_one.
§Example
async fn load_meshes_files<R:std::io::Read>(src:&mut [R])->Result<Vec<(TMFMesh,String)>,TMFImportError>{
let mut meshes = Vec::new();
for source in src{
meshes.push(TMFMesh::read_tmf_one_async(source).await?);
}
Ok(meshes)
}Sourcepub fn add_custom_data(
&mut self,
custom_data: CustomData,
name: &str,
) -> Result<(), DataSegmentError>
pub fn add_custom_data( &mut self, custom_data: CustomData, name: &str, ) -> Result<(), DataSegmentError>
Adds custom data array.
§Errors
Returns Err if name is too long (over 255 bytes) or empty.
§Examples
// Add custom index data.
let custom_index_data = [0,1,2,3];
let custom_float_data = [0.0,1.2,2.4,3.5];
let custom_rgba = [(0.2,0.6,0.7,0.1),(0.5,0.6,0.1,0.9),(0.3,0.9,0.3,0.2),(0.4,0.5,0.2,0.6),(0.2,0.8,0.9,0.4)];
// Add custom index data
mesh.add_custom_data(custom_index_data.into(),"custom_indices").expect("Could not add custom data to mesh!");
// Add custom float data.
mesh.add_custom_data(custom_float_data.into(),"custom_floats").expect("Could not add custom data to mesh!");
// Add custom RGBA color data
mesh.add_custom_data(custom_rgba.into(),"custom_rgba").expect("Could not add custom data to mesh!");Sourcepub fn lookup_custom_data(&self, name: &str) -> Option<&CustomData>
pub fn lookup_custom_data(&self, name: &str) -> Option<&CustomData>
Gets a custom data array with name name.
Returns None, if data not present, or name too long(over 255 bytes).
§Examples
// Add custom index data.
let custom_index_data = [0,1,2,3];
let custom_float_data = [0.0,1.2,2.4,3.5];
let custom_rgba = [(0.2,0.6,0.7,0.1),(0.5,0.6,0.1,0.9),(0.3,0.9,0.3,0.2),(0.4,0.5,0.2,0.6),(0.2,0.8,0.9,0.4)];
// Add custom index data
mesh.add_custom_data(custom_index_data.into(),"custom_indices").expect("Could not add custom data to mesh!");
// Add custom float data.
mesh.add_custom_data(custom_float_data.into(),"custom_floats").expect("Could not add custom data to mesh!");
// Add custom RGBA color data
mesh.add_custom_data(custom_rgba.into(),"custom_rgba").expect("Could not add custom data to mesh!");
// lookup data
let indices = mesh.lookup_custom_data("custom_indices").expect("Could not find custom indices");
let floats = mesh.lookup_custom_data("custom_floats").expect("Could not find custom floats");
let rgba = mesh.lookup_custom_data("custom_rgba").expect("Could not find custom rgba colors");Sourcepub fn append_vertices(&mut self, vertices: &[Vector3])
pub fn append_vertices(&mut self, vertices: &[Vector3])
Appends vertices to this meshes vertex array.
§Example
let vertices_len = tmf_mesh.get_vertices().unwrap().len();
tmf_mesh.append_vertices(&[(0.2,4.5,4.4)]);
assert!(vertices_len < tmf_mesh.get_vertices().unwrap().len());Sourcepub fn append_normals(&mut self, normals: &[Vector3])
pub fn append_normals(&mut self, normals: &[Vector3])
Appends normals to this meshes normal array.
§Example
let normals_len = tmf_mesh.get_normals().unwrap().len();
tmf_mesh.append_normals(&[(0.2,4.5,4.4)]);
assert!(normals_len < tmf_mesh.get_normals().unwrap().len());Sourcepub fn append_tangents(&mut self, tangents: &[Tangent])
pub fn append_tangents(&mut self, tangents: &[Tangent])
Appends tangents to this meshes tangent array.
§Example
let tangents_len = tmf_mesh.get_tangents().unwrap().len();
tmf_mesh.append_tangents(&[((0.311649, 0.733673, -0.60382),-1.0)]);
assert!(tangents_len < tmf_mesh.get_tangents().unwrap().len());Sourcepub fn append_uvs(&mut self, uvs: &[Vector2])
pub fn append_uvs(&mut self, uvs: &[Vector2])
Appends uvs to this meshes uv array.
§Example
let uvs_len = tmf_mesh.get_uvs().unwrap().len();
tmf_mesh.append_uvs(&[(0.2,0.5),(0.12,0.78)]);
assert!(uvs_len < tmf_mesh.get_uvs().unwrap().len());Sourcepub fn append_vertex_triangles(&mut self, triangles: &[IndexType])
pub fn append_vertex_triangles(&mut self, triangles: &[IndexType])
Appends indices to this meshes vertex triangle array.
§Example
let triangle_len = tmf_mesh.get_vertex_triangles().unwrap().len();
tmf_mesh.append_vertex_triangles(&[0,4,3,8,7,9]);
assert!(triangle_len < tmf_mesh.get_vertex_triangles().unwrap().len());Sourcepub fn append_normal_triangles(&mut self, triangles: &[IndexType])
pub fn append_normal_triangles(&mut self, triangles: &[IndexType])
Appends indices to this meshes normal triangle array.
§Example
let triangle_len = tmf_mesh.get_normal_triangles().unwrap().len();
tmf_mesh.append_normal_triangles(&[0,4,3,8,7,9]);
assert!(triangle_len < tmf_mesh.get_normal_triangles().unwrap().len());Sourcepub fn append_uv_triangles(&mut self, triangles: &[IndexType])
pub fn append_uv_triangles(&mut self, triangles: &[IndexType])
Appends indices to this meshes uv triangle array.
§Example
let triangle_len = tmf_mesh.get_uv_triangles().unwrap().len();
tmf_mesh.append_uv_triangles(&[0,4,3,8,7,9]);
assert!(triangle_len < tmf_mesh.get_uv_triangles().unwrap().len());Sourcepub fn append_tangent_triangles(&mut self, triangles: &[IndexType])
pub fn append_tangent_triangles(&mut self, triangles: &[IndexType])
Appends indices to this meshes tangent triangle array.
§Example
let triangle_len = tmf_mesh.get_tangent_triangles().unwrap().len();
tmf_mesh.append_tangent_triangles(&[0,4,3,8,7,9]);
assert!(triangle_len < tmf_mesh.get_tangent_triangles().unwrap().len());