Skip to content

Commit

Permalink
support updating positions and normals for mesh (#436)
Browse files Browse the repository at this point in the history
* support updating positions and normals for mesh

* Minor clean-up + added vertex_count method

---------

Co-authored-by: Asger Nyman Christiansen <asgernyman@gmail.com>
  • Loading branch information
wlgys8 and asny committed Feb 26, 2024
1 parent a239ca4 commit 5b434fa
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/renderer/geometry/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,43 @@ impl Mesh {
pub fn set_animation(&mut self, animation: impl Fn(f32) -> Mat4 + Send + Sync + 'static) {
self.animation = Some(Box::new(animation));
}

///
/// Returns the number of vertices in this mesh.
///
pub fn vertex_count(&self) -> u32 {
self.base_mesh.positions.vertex_count()
}

/// Updates the vertex positions of the mesh.
///
/// # Panics
///
/// Panics if the number of positions does not match the number of vertices in the mesh.
pub fn update_positions(&mut self, positions: &[Vector3<f32>]) {
if positions.len() as u32 != self.vertex_count() {
panic!("Failed updating positions: The number of positions {} does not match the number of vertices {} in the mesh.", positions.len(), self.vertex_count())
}
self.base_mesh.positions.fill(positions);
}

///
/// Updates the vertex normals of the mesh.
///
/// # Panics
///
/// Panics if the number of normals does not match the number of vertices in the mesh.
pub fn update_normals(&mut self, normals: &[Vector3<f32>]) {
if normals.len() as u32 != self.vertex_count() {
panic!("Failed updating normals: The number of normals {} does not match the number of vertices {} in the mesh.", normals.len(), self.vertex_count())
}

if let Some(normal_buffer) = &mut self.base_mesh.normals {
normal_buffer.fill(normals);
} else {
self.base_mesh.normals = Some(VertexBuffer::new_with_data(&self.context, normals));
}
}
}

impl<'a> IntoIterator for &'a Mesh {
Expand Down

0 comments on commit 5b434fa

Please sign in to comment.