Skip to content

Commit

Permalink
Add rectangular cuboid shape
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Lessard <[email protected]>
  • Loading branch information
e00E and Jason Lessard committed Nov 18, 2020
1 parent 4fecb89 commit be20a44
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 44 deletions.
103 changes: 60 additions & 43 deletions crates/bevy_render/src/mesh/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,58 +227,75 @@ pub mod shape {
use bevy_math::*;
use hexasphere::Hexasphere;

/// A cube.
#[derive(Debug)]
pub struct Cube {
/// Half the side length of the cube.
pub size: f32,
pub struct Cuboid {
pub min_x: f32,
pub max_x: f32,

pub min_y: f32,
pub max_y: f32,

pub min_z: f32,
pub max_z: f32,
}

impl Cuboid {
pub fn new(x_length: f32, y_length: f32, z_length: f32) -> Cuboid {
Cuboid {
max_x: x_length / 2.0,
min_x: -x_length / 2.0,
max_y: y_length / 2.0,
min_y: -y_length / 2.0,
max_z: z_length / 2.0,
min_z: -z_length / 2.0,
}
}
}

impl Default for Cube {
impl Default for Cuboid {
fn default() -> Self {
Cube { size: 1.0 }
Cuboid::new(2.0, 1.0, 1.0)
}
}

impl From<Cube> for Mesh {
fn from(cube: Cube) -> Self {
let size = cube.size;
impl From<Cuboid> for Mesh {
fn from(sp: Cuboid) -> Self {
let vertices = &[
// top (0., 0., size)
([-size, -size, size], [0., 0., size], [0., 0.]),
([size, -size, size], [0., 0., size], [size, 0.]),
([size, size, size], [0., 0., size], [size, size]),
([-size, size, size], [0., 0., size], [0., size]),
// bottom (0., 0., -size)
([-size, size, -size], [0., 0., -size], [size, 0.]),
([size, size, -size], [0., 0., -size], [0., 0.]),
([size, -size, -size], [0., 0., -size], [0., size]),
([-size, -size, -size], [0., 0., -size], [size, size]),
// right (size, 0., 0.)
([size, -size, -size], [size, 0., 0.], [0., 0.]),
([size, size, -size], [size, 0., 0.], [size, 0.]),
([size, size, size], [size, 0., 0.], [size, size]),
([size, -size, size], [size, 0., 0.], [0., size]),
// left (-size, 0., 0.)
([-size, -size, size], [-size, 0., 0.], [size, 0.]),
([-size, size, size], [-size, 0., 0.], [0., 0.]),
([-size, size, -size], [-size, 0., 0.], [0., size]),
([-size, -size, -size], [-size, 0., 0.], [size, size]),
// front (0., size, 0.)
([size, size, -size], [0., size, 0.], [size, 0.]),
([-size, size, -size], [0., size, 0.], [0., 0.]),
([-size, size, size], [0., size, 0.], [0., size]),
([size, size, size], [0., size, 0.], [size, size]),
// back (0., -size, 0.)
([size, -size, size], [0., -size, 0.], [0., 0.]),
([-size, -size, size], [0., -size, 0.], [size, 0.]),
([-size, -size, -size], [0., -size, 0.], [size, size]),
([size, -size, -size], [0., -size, 0.], [0., size]),
// Top
([sp.min_x, sp.min_y, sp.max_z], [0., 0., 1.0], [0., 0.]),
([sp.max_x, sp.min_y, sp.max_z], [0., 0., 1.0], [1.0, 0.]),
([sp.max_x, sp.max_y, sp.max_z], [0., 0., 1.0], [1.0, 1.0]),
([sp.min_x, sp.max_y, sp.max_z], [0., 0., 1.0], [0., 1.0]),
// Bottom
([sp.min_x, sp.max_y, sp.min_z], [0., 0., -1.0], [1.0, 0.]),
([sp.max_x, sp.max_y, sp.min_z], [0., 0., -1.0], [0., 0.]),
([sp.max_x, sp.min_y, sp.min_z], [0., 0., -1.0], [0., 1.0]),
([sp.min_x, sp.min_y, sp.min_z], [0., 0., -1.0], [1.0, 1.0]),
// Right
([sp.max_x, sp.min_y, sp.min_z], [1.0, 0., 0.], [0., 0.]),
([sp.max_x, sp.max_y, sp.min_z], [1.0, 0., 0.], [1.0, 0.]),
([sp.max_x, sp.max_y, sp.max_z], [1.0, 0., 0.], [1.0, 1.0]),
([sp.max_x, sp.min_y, sp.max_z], [1.0, 0., 0.], [0., 1.0]),
// Left
([sp.min_x, sp.min_y, sp.max_z], [-1.0, 0., 0.], [1.0, 0.]),
([sp.min_x, sp.max_y, sp.max_z], [-1.0, 0., 0.], [0., 0.]),
([sp.min_x, sp.max_y, sp.min_z], [-1.0, 0., 0.], [0., 1.0]),
([sp.min_x, sp.min_y, sp.min_z], [-1.0, 0., 0.], [1.0, 1.0]),
// Front
([sp.max_x, sp.max_y, sp.min_z], [0., 1.0, 0.], [1.0, 0.]),
([sp.min_x, sp.max_y, sp.min_z], [0., 1.0, 0.], [0., 0.]),
([sp.min_x, sp.max_y, sp.max_z], [0., 1.0, 0.], [0., 1.0]),
([sp.max_x, sp.max_y, sp.max_z], [0., 1.0, 0.], [1.0, 1.0]),
// Back
([sp.max_x, sp.min_y, sp.max_z], [0., -1.0, 0.], [0., 0.]),
([sp.min_x, sp.min_y, sp.max_z], [0., -1.0, 0.], [1.0, 0.]),
([sp.min_x, sp.min_y, sp.min_z], [0., -1.0, 0.], [1.0, 1.0]),
([sp.max_x, sp.min_y, sp.min_z], [0., -1.0, 0.], [0., 1.0]),
];

let mut positions = Vec::new();
let mut normals = Vec::new();
let mut uvs = Vec::new();
let mut positions = Vec::with_capacity(24);
let mut normals = Vec::with_capacity(24);
let mut uvs = Vec::with_capacity(24);

for (position, normal, uv) in vertices.iter() {
positions.push(*position);
normals.push(*normal);
Expand Down
2 changes: 1 addition & 1 deletion examples/3d/3d_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn setup(
})
// cube
.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
mesh: meshes.add(Mesh::from(shape::Cuboid::new(1.0, 1.0, 1.0))),
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
transform: Transform::from_translation(Vec3::new(0.0, 1.0, 0.0)),
..Default::default()
Expand Down

0 comments on commit be20a44

Please sign in to comment.