From ebe6b317f2af4a492cbfa0312f0f89889ceeb03a Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Fri, 22 Sep 2023 11:14:15 +0100 Subject: [PATCH] Add basic cylinder brush generator --- editor/src/main.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/editor/src/main.rs b/editor/src/main.rs index 6363b1a..a557dfb 100644 --- a/editor/src/main.rs +++ b/editor/src/main.rs @@ -55,6 +55,38 @@ fn generate_pyramid_brush(position: Vec3, height: f32, side_count: u32) -> Brush Brush::new(&brush_planes) } +fn generate_cylinder_brush(position: Vec3, height: f32, radius: f32, side_count: u32) -> Brush { + let mut brush_planes = vec![]; + + // Top/bottom planes + for normal in &[Vec3::Y, Vec3::NEG_Y] { + let point = position + *normal * (height * 0.5); + brush_planes.push(BrushPlane { + plane: Plane::from_point_normal(point, *normal), + ..Default::default() + }); + } + + // Side planes + for i in 0..side_count { + let angle = (i as f32 / side_count as f32) * 360.0_f32.to_radians(); + let rot_mat = Mat3::from_cols( + Vec3::new(angle.cos(), 0.0, -angle.sin()), + Vec3::Y, + Vec3::new(angle.sin(), 0.0, angle.cos()), + ); + let normal = rot_mat * Vec3::new(1.0, 0.0, 0.0); + let point = position + radius * normal; + + brush_planes.push(BrushPlane { + plane: Plane::from_point_normal(point, normal), + ..Default::default() + }); + } + + Brush::new(&brush_planes) +} + fn main() { let mut brush = generate_box_brush(Vec3::new(1.0, 0.0, 0.0), Vec3::new(1.0, 2.0, 1.0)); brush.rebuild(); @@ -67,4 +99,10 @@ fn main() { let vs = brush.get_vertices(); println!("Vertices: {vs:?}"); + + let mut brush = generate_cylinder_brush(Vec3::ZERO, 2.0, 1.0, 64); + brush.rebuild(); + let vs = brush.get_vertices(); + + println!("Vertices: {vs:?}"); }