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 4ab3925
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 53 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
2 changes: 1 addition & 1 deletion examples/3d/msaa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn setup(
commands
// 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()),
..Default::default()
})
Expand Down
2 changes: 1 addition & 1 deletion examples/3d/parenting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn setup(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let cube_handle = meshes.add(Mesh::from(shape::Cube { size: 1.0 }));
let cube_handle = meshes.add(Mesh::from(shape::Cuboid::new(1.0, 1.0, 1.0)));
let cube_material_handle = materials.add(StandardMaterial {
albedo: Color::rgb(0.8, 0.7, 0.6),
..Default::default()
Expand Down
2 changes: 1 addition & 1 deletion examples/3d/spawner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn setup(
});

let mut rng = StdRng::from_entropy();
let cube_handle = meshes.add(Mesh::from(shape::Cube { size: 1.0 }));
let cube_handle = meshes.add(Mesh::from(shape::Cuboid::new(1.0, 1.0, 1.0)));
for _ in 0..10000 {
commands.spawn(PbrBundle {
mesh: cube_handle.clone(),
Expand Down
2 changes: 1 addition & 1 deletion examples/3d/z_sort_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn setup(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let cube_handle = meshes.add(Mesh::from(shape::Cube { size: 1.0 }));
let cube_handle = meshes.add(Mesh::from(shape::Cuboid::new(1.0, 1.0, 1.0)));
commands
// parent cube
.spawn(PbrBundle {
Expand Down
2 changes: 1 addition & 1 deletion examples/android/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,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
2 changes: 1 addition & 1 deletion examples/ios/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,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.5, 0.4, 0.3).into()),
transform: Transform::from_translation(Vec3::new(0.0, 1.0, 0.0)),
..Default::default()
Expand Down
2 changes: 1 addition & 1 deletion examples/shader/mesh_custom_attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ fn setup(
let material = materials.add(MyMaterialWithVertexColorSupport {});

// create a generic cube
let mut cube_with_vertex_colors = Mesh::from(shape::Cube { size: 1.0 });
let mut cube_with_vertex_colors = Mesh::from(shape::Cuboid::new(1.0, 1.0, 1.0));

// insert our custom color attribute with some nice colors!
cube_with_vertex_colors.set_attribute(
Expand Down
2 changes: 1 addition & 1 deletion examples/shader/shader_custom_material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn setup(
commands
// cube
.spawn(MeshBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
mesh: meshes.add(Mesh::from(shape::Cuboid::new(1.0, 1.0, 1.0))),
render_pipelines: RenderPipelines::from_pipelines(vec![RenderPipeline::new(
pipeline_handle,
)]),
Expand Down
2 changes: 1 addition & 1 deletion examples/shader/shader_defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ fn setup(
});

// Create a cube mesh which will use our materials
let cube_handle = meshes.add(Mesh::from(shape::Cube { size: 1.0 }));
let cube_handle = meshes.add(Mesh::from(shape::Cuboid::new(1.0, 1.0, 1.0)));

commands
// cube
Expand Down

0 comments on commit 4ab3925

Please sign in to comment.