diff --git a/crates/lambda-rs/src/render/mesh.rs b/crates/lambda-rs/src/render/mesh.rs index c96525da..ea6c0484 100644 --- a/crates/lambda-rs/src/render/mesh.rs +++ b/crates/lambda-rs/src/render/mesh.rs @@ -64,7 +64,7 @@ impl MeshBuilder { /// Allocates memory for the given number of vertices and fills /// the mesh with empty vertices. - pub fn with_capacity(&mut self, size: usize) -> &mut Self { + pub fn with_capacity(mut self, size: usize) -> Self { self.vertices.resize( size, Vertex { @@ -77,34 +77,32 @@ impl MeshBuilder { } /// Adds a vertex to the mesh. - pub fn with_vertex(&mut self, vertex: Vertex) -> &mut Self { + pub fn with_vertex(mut self, vertex: Vertex) -> Self { self.vertices.push(vertex); return self; } /// Specify the attributes of the mesh. This is used to map the vertex data to /// the input of the vertex shader. - pub fn with_attributes( - &mut self, - attributes: Vec, - ) -> &mut Self { + pub fn with_attributes(mut self, attributes: Vec) -> Self { self.attributes = attributes; return self; } /// Builds a mesh from the vertices and indices that have been added to the /// builder and allocates the memory for the mesh on the GPU. - pub fn build(&self) -> Mesh { + pub fn build(self) -> Mesh { return Mesh { - vertices: self.vertices.clone(), - attributes: self.attributes.clone(), + vertices: self.vertices, + attributes: self.attributes, }; } /// Builds a mesh from the vertices of an OBJ file. The mesh will have the same /// attributes as the OBJ file and can be allocated on to the GPU with /// `BufferBuilder::build_from_mesh`. - pub fn build_from_obj(&self, file_path: &str) -> Mesh { + pub fn build_from_obj(self, file_path: &str) -> Mesh { + let _ = self; let obj = load_textured_obj_from_file(file_path); let vertices = obj @@ -170,17 +168,16 @@ mod tests { /// the built mesh. #[test] fn mesh_builder_capacity_and_attributes_are_applied() { - let mut builder = MeshBuilder::new(); - builder.with_capacity(2); + let builder = MeshBuilder::new().with_capacity(2); assert_eq!(builder.vertices.len(), 2); - builder.with_vertex(Vertex { - position: [1.0, 2.0, 3.0], - normal: [0.0, 1.0, 0.0], - color: [0.5, 0.5, 0.5], - }); - - let mesh = builder.build(); + let mesh = builder + .with_vertex(Vertex { + position: [1.0, 2.0, 3.0], + normal: [0.0, 1.0, 0.0], + color: [0.5, 0.5, 0.5], + }) + .build(); assert_eq!(mesh.vertices().len(), 3); } diff --git a/demos/physics/src/bin/physics_falling_quad.rs b/demos/physics/src/bin/physics_falling_quad.rs index ed0073ff..594b6027 100644 --- a/demos/physics/src/bin/physics_falling_quad.rs +++ b/demos/physics/src/bin/physics_falling_quad.rs @@ -175,12 +175,12 @@ impl Component for FallingQuadDemo { .build(), ]; - let mut mesh_builder = MeshBuilder::new(); - vertices.iter().for_each(|vertex| { - mesh_builder.with_vertex(*vertex); - }); - - let mesh = mesh_builder + let mesh = vertices + .iter() + .copied() + .fold(MeshBuilder::new(), |builder, vertex| { + return builder.with_vertex(vertex); + }) .with_attributes(vec![ VertexAttribute { location: 0, diff --git a/demos/physics/src/bin/physics_rigid_bodies_2d.rs b/demos/physics/src/bin/physics_rigid_bodies_2d.rs index 335f959d..5b08e8df 100644 --- a/demos/physics/src/bin/physics_rigid_bodies_2d.rs +++ b/demos/physics/src/bin/physics_rigid_bodies_2d.rs @@ -350,12 +350,12 @@ impl Component for RigidBodies2DDemo { .build(), ]; - let mut mesh_builder = MeshBuilder::new(); - vertices.iter().for_each(|vertex| { - mesh_builder.with_vertex(*vertex); - }); - - let mesh = mesh_builder + let mesh = vertices + .iter() + .copied() + .fold(MeshBuilder::new(), |builder, vertex| { + return builder.with_vertex(vertex); + }) .with_attributes(vec![ VertexAttribute { location: 0, diff --git a/demos/render/src/bin/immediates.rs b/demos/render/src/bin/immediates.rs index 9d512fc4..8895fa39 100644 --- a/demos/render/src/bin/immediates.rs +++ b/demos/render/src/bin/immediates.rs @@ -149,12 +149,12 @@ impl Component for ImmediatesExample { .build(), ]; - let mut mesh_builder = MeshBuilder::new(); - vertices.iter().for_each(|vertex| { - mesh_builder.with_vertex(*vertex); - }); - - let mesh = mesh_builder + let mesh = vertices + .iter() + .copied() + .fold(MeshBuilder::new(), |builder, vertex| { + return builder.with_vertex(vertex); + }) .with_attributes(vec![ VertexAttribute { location: 0, diff --git a/demos/render/src/bin/offscreen_post.rs b/demos/render/src/bin/offscreen_post.rs index 31291cee..db14aa20 100644 --- a/demos/render/src/bin/offscreen_post.rs +++ b/demos/render/src/bin/offscreen_post.rs @@ -338,12 +338,11 @@ impl OffscreenPostExample { .build(), ]; - let mut mesh_builder = MeshBuilder::new(); - for v in vertices { - mesh_builder.with_vertex(v); - } - - return mesh_builder + return vertices + .into_iter() + .fold(MeshBuilder::new(), |builder, vertex| { + return builder.with_vertex(vertex); + }) .with_attributes(vec![ VertexAttribute { location: 0, diff --git a/demos/render/src/bin/reflective_room.rs b/demos/render/src/bin/reflective_room.rs index e31ace8a..5c33bc5f 100644 --- a/demos/render/src/bin/reflective_room.rs +++ b/demos/render/src/bin/reflective_room.rs @@ -870,11 +870,11 @@ fn build_unit_cube_mesh() -> Mesh { [(h, -h, -h), (-h, -h, -h), (-h, h, -h), (h, h, -h)], ); - let mut mesh_builder = MeshBuilder::new(); - for v in verts.into_iter() { - mesh_builder.with_vertex(v); - } - let mesh = mesh_builder + let mesh = verts + .into_iter() + .fold(MeshBuilder::new(), |builder, vertex| { + return builder.with_vertex(vertex); + }) .with_attributes(vec![ VertexAttribute { location: 0, @@ -914,18 +914,16 @@ fn build_floor_quad_mesh(extent: f32) -> Mesh { let p2 = v(h, h); let p3 = v(-h, h); - let mut mesh_builder = MeshBuilder::new(); // Tri winding flipped to face +Y (avoid back-face cull) - // Tri 1 - mesh_builder.with_vertex(p0); - mesh_builder.with_vertex(p2); - mesh_builder.with_vertex(p1); - // Tri 2 - mesh_builder.with_vertex(p0); - mesh_builder.with_vertex(p3); - mesh_builder.with_vertex(p2); - - let mesh = mesh_builder + let mesh = MeshBuilder::new() + // Tri 1 + .with_vertex(p0) + .with_vertex(p2) + .with_vertex(p1) + // Tri 2 + .with_vertex(p0) + .with_vertex(p3) + .with_vertex(p2) .with_attributes(vec![ VertexAttribute { location: 0, diff --git a/demos/render/src/bin/textured_cube.rs b/demos/render/src/bin/textured_cube.rs index 8a4fc8a4..cdf57f76 100644 --- a/demos/render/src/bin/textured_cube.rs +++ b/demos/render/src/bin/textured_cube.rs @@ -262,11 +262,11 @@ impl Component for TexturedCubeExample { [(h, -h, -h), (-h, -h, -h), (-h, h, -h), (h, h, -h)], ); - let mut mesh_builder = MeshBuilder::new(); - for v in verts.into_iter() { - mesh_builder.with_vertex(v); - } - let mesh = mesh_builder + let mesh = verts + .into_iter() + .fold(MeshBuilder::new(), |builder, vertex| { + return builder.with_vertex(vertex); + }) .with_attributes(vec![ VertexAttribute { location: 0, diff --git a/demos/render/src/bin/textured_quad.rs b/demos/render/src/bin/textured_quad.rs index a015ccca..da5a9a65 100644 --- a/demos/render/src/bin/textured_quad.rs +++ b/demos/render/src/bin/textured_quad.rs @@ -163,11 +163,12 @@ impl Component for TexturedQuadExample { .build(), // uv (0,1) ]; - let mut mesh_builder = MeshBuilder::new(); - vertices.iter().for_each(|v| { - mesh_builder.with_vertex(*v); - }); - let mesh = mesh_builder + let mesh = vertices + .iter() + .copied() + .fold(MeshBuilder::new(), |builder, vertex| { + return builder.with_vertex(vertex); + }) .with_attributes(vec![ VertexAttribute { location: 0, diff --git a/demos/render/src/bin/uniform_buffer_triangle.rs b/demos/render/src/bin/uniform_buffer_triangle.rs index 276107a0..c503bf18 100644 --- a/demos/render/src/bin/uniform_buffer_triangle.rs +++ b/demos/render/src/bin/uniform_buffer_triangle.rs @@ -148,12 +148,12 @@ impl Component for UniformBufferExample { .build(), ]; - let mut mesh_builder = MeshBuilder::new(); - vertices.iter().for_each(|vertex| { - mesh_builder.with_vertex(*vertex); - }); - - let mesh = mesh_builder + let mesh = vertices + .iter() + .copied() + .fold(MeshBuilder::new(), |builder, vertex| { + return builder.with_vertex(vertex); + }) .with_attributes(vec![ VertexAttribute { location: 0, diff --git a/docs/tutorials/physics/basics/falling-quad-kinematic.md b/docs/tutorials/physics/basics/falling-quad-kinematic.md index ba15c580..d8997779 100644 --- a/docs/tutorials/physics/basics/falling-quad-kinematic.md +++ b/docs/tutorials/physics/basics/falling-quad-kinematic.md @@ -468,12 +468,10 @@ fn on_attach( .build(), ]; - let mut mesh_builder = MeshBuilder::new(); - vertices.iter().for_each(|vertex| { - mesh_builder.with_vertex(*vertex); - }); - - let mesh = mesh_builder + let mesh = vertices + .iter() + .copied() + .fold(MeshBuilder::new(), |builder, vertex| builder.with_vertex(vertex)) .with_attributes(vec![ VertexAttribute { location: 0, diff --git a/docs/tutorials/rendering/resources/textured-cube.md b/docs/tutorials/rendering/resources/textured-cube.md index 649e1eff..4cb5c682 100644 --- a/docs/tutorials/rendering/resources/textured-cube.md +++ b/docs/tutorials/rendering/resources/textured-cube.md @@ -265,10 +265,9 @@ add_face( 0.0, -1.0, 0.0, [(-h, -h, -h), ( h, -h, -h), ( h, -h, h), (-h, -h, add_face( 0.0, 0.0, 1.0, [(-h, -h, h), ( h, -h, h), ( h, h, h), (-h, h, h)]); add_face( 0.0, 0.0, -1.0, [( h, -h, -h), (-h, -h, -h), (-h, h, -h), ( h, h, -h)]); -let mut mesh_builder = MeshBuilder::new(); -verts.into_iter().for_each(|v| { mesh_builder.with_vertex(v); }); - -let mesh: Mesh = mesh_builder +let mesh: Mesh = verts + .into_iter() + .fold(MeshBuilder::new(), |builder, vertex| builder.with_vertex(vertex)) .with_attributes(vec![ VertexAttribute { // position @ location 0 location: 0, offset: 0, diff --git a/docs/tutorials/rendering/resources/textured-quad.md b/docs/tutorials/rendering/resources/textured-quad.md index f914ef5e..fbddff36 100644 --- a/docs/tutorials/rendering/resources/textured-quad.md +++ b/docs/tutorials/rendering/resources/textured-quad.md @@ -271,10 +271,10 @@ let vertices: [Vertex; 6] = [ VertexBuilder::new().with_position([-0.5, 0.5, 0.0]).with_normal([0.0, 0.0, 1.0]).with_color([0.0, 1.0, 0.0]).build(), // uv (0,1) ]; -let mut mesh_builder = MeshBuilder::new(); -vertices.iter().for_each(|v| { mesh_builder.with_vertex(*v); }); - -let mesh: Mesh = mesh_builder +let mesh: Mesh = vertices + .iter() + .copied() + .fold(MeshBuilder::new(), |builder, vertex| builder.with_vertex(vertex)) .with_attributes(vec![ VertexAttribute { // position @ location 0 location: 0, offset: 0, diff --git a/docs/tutorials/rendering/resources/uniform-buffers.md b/docs/tutorials/rendering/resources/uniform-buffers.md index b28fd5ac..48b51d37 100644 --- a/docs/tutorials/rendering/resources/uniform-buffers.md +++ b/docs/tutorials/rendering/resources/uniform-buffers.md @@ -200,10 +200,10 @@ let vertices = [ VertexBuilder::new().with_position([ 0.0, -1.0, 0.0]).with_normal([0.0,0.0,0.0]).with_color([0.0,0.0,1.0]).build(), ]; -let mut mesh_builder = MeshBuilder::new(); -vertices.iter().for_each(|v| { mesh_builder.with_vertex(v.clone()); }); - -let mesh: Mesh = mesh_builder +let mesh: Mesh = vertices + .iter() + .copied() + .fold(MeshBuilder::new(), |builder, vertex| builder.with_vertex(vertex)) .with_attributes(vec![ VertexAttribute { // position @ location 0 location: 0, offset: 0, diff --git a/docs/tutorials/rendering/techniques/offscreen-post.md b/docs/tutorials/rendering/techniques/offscreen-post.md index 5ff8112f..034a7011 100644 --- a/docs/tutorials/rendering/techniques/offscreen-post.md +++ b/docs/tutorials/rendering/techniques/offscreen-post.md @@ -524,12 +524,9 @@ impl OffscreenPostExample { .build(), ]; - let mut mesh_builder = MeshBuilder::new(); - for v in vertices { - mesh_builder.with_vertex(v); - } - - return mesh_builder + return vertices + .into_iter() + .fold(MeshBuilder::new(), |builder, vertex| builder.with_vertex(vertex)) .with_attributes(vec![ VertexAttribute { location: 0,