Skip to content

Commit

Permalink
add cube primitive wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Bendzae committed Oct 5, 2024
1 parent d2580d1 commit 98227ef
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/smesh/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ pub mod iterators;
pub mod loops;
pub mod mesh_query;
pub mod model;
pub mod primitives;
pub mod selection;
pub mod topological_operations;
pub mod transform;
pub mod util;

49 changes: 49 additions & 0 deletions src/smesh/primitives.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use glam::vec3;

use crate::prelude::*;

pub struct Cube {
pub subdivisions: usize,
}

pub struct CubeData {
pub front_bottom_left_vertex: VertexId,
}

impl Cube {
pub fn generate(self) -> SMeshResult<(SMesh, CubeData)> {
// Construct SMesh
let mut smesh = SMesh::new();
let half_extent = 0.5;
let v0 = smesh.add_vertex(vec3(-half_extent, -half_extent, half_extent));
let v1 = smesh.add_vertex(vec3(half_extent, -half_extent, half_extent));
let v2 = smesh.add_vertex(vec3(half_extent, half_extent, half_extent));
let v3 = smesh.add_vertex(vec3(-half_extent, half_extent, half_extent));

let v4 = smesh.add_vertex(vec3(-half_extent, -half_extent, -half_extent));
let v5 = smesh.add_vertex(vec3(half_extent, -half_extent, -half_extent));
let v6 = smesh.add_vertex(vec3(half_extent, half_extent, -half_extent));
let v7 = smesh.add_vertex(vec3(-half_extent, half_extent, -half_extent));

// Front
smesh.make_face(vec![v0, v1, v2, v3])?;
// Right
smesh.make_face(vec![v1, v5, v6, v2])?;
// Back
smesh.make_face(vec![v5, v4, v7, v6])?;
// Left
smesh.make_face(vec![v4, v0, v3, v7])?;
// Top
smesh.make_face(vec![v3, v2, v6, v7])?;
// Bottom
smesh.make_face(vec![v4, v5, v1, v0])?;

smesh.recalculate_normals()?;
Ok((
smesh,
CubeData {
front_bottom_left_vertex: v0,
},
))
}
}

0 comments on commit 98227ef

Please sign in to comment.