From be20a4498ddea5f95e6228b5d467f8b09282aaf5 Mon Sep 17 00:00:00 2001 From: Valentin Date: Tue, 17 Nov 2020 20:55:52 +0100 Subject: [PATCH] Add rectangular cuboid shape Co-authored-by: Jason Lessard --- crates/bevy_render/src/mesh/mesh.rs | 103 ++++++++++++++++------------ examples/3d/3d_scene.rs | 2 +- 2 files changed, 61 insertions(+), 44 deletions(-) diff --git a/crates/bevy_render/src/mesh/mesh.rs b/crates/bevy_render/src/mesh/mesh.rs index 0f1ba85f23d185..3bd72cfb0759bf 100644 --- a/crates/bevy_render/src/mesh/mesh.rs +++ b/crates/bevy_render/src/mesh/mesh.rs @@ -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 for Mesh { - fn from(cube: Cube) -> Self { - let size = cube.size; + impl From 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); diff --git a/examples/3d/3d_scene.rs b/examples/3d/3d_scene.rs index fb5be88b72be86..10652be06dbd36 100644 --- a/examples/3d/3d_scene.rs +++ b/examples/3d/3d_scene.rs @@ -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()