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() diff --git a/examples/3d/msaa.rs b/examples/3d/msaa.rs index ef3767fde14ddb..d83f492a7b5233 100644 --- a/examples/3d/msaa.rs +++ b/examples/3d/msaa.rs @@ -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() }) diff --git a/examples/3d/parenting.rs b/examples/3d/parenting.rs index 03c0c826835ec9..92cf3b166ba8a2 100644 --- a/examples/3d/parenting.rs +++ b/examples/3d/parenting.rs @@ -27,7 +27,7 @@ fn setup( mut meshes: ResMut>, mut materials: ResMut>, ) { - 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() diff --git a/examples/3d/spawner.rs b/examples/3d/spawner.rs index 20a7552bc43dfd..d16ef7af45edf8 100644 --- a/examples/3d/spawner.rs +++ b/examples/3d/spawner.rs @@ -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(), diff --git a/examples/3d/z_sort_debug.rs b/examples/3d/z_sort_debug.rs index 08c4cd6b762d8a..2ad81c3d97d4af 100644 --- a/examples/3d/z_sort_debug.rs +++ b/examples/3d/z_sort_debug.rs @@ -46,7 +46,7 @@ fn setup( mut meshes: ResMut>, mut materials: ResMut>, ) { - 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 { diff --git a/examples/android/android.rs b/examples/android/android.rs index 15fbd045834673..05ae3f4b3e27f5 100644 --- a/examples/android/android.rs +++ b/examples/android/android.rs @@ -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() diff --git a/examples/ios/src/lib.rs b/examples/ios/src/lib.rs index 56cbd1b0509051..63631edfc038e6 100644 --- a/examples/ios/src/lib.rs +++ b/examples/ios/src/lib.rs @@ -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() diff --git a/examples/shader/mesh_custom_attribute.rs b/examples/shader/mesh_custom_attribute.rs index 013dc9bb199b6d..14648e0d70572d 100644 --- a/examples/shader/mesh_custom_attribute.rs +++ b/examples/shader/mesh_custom_attribute.rs @@ -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( diff --git a/examples/shader/shader_custom_material.rs b/examples/shader/shader_custom_material.rs index 92bcbcad1128b8..c1b9c7af17bd6f 100644 --- a/examples/shader/shader_custom_material.rs +++ b/examples/shader/shader_custom_material.rs @@ -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, )]), diff --git a/examples/shader/shader_defs.rs b/examples/shader/shader_defs.rs index 3a4abfec84f8b5..ae76c2187537b8 100644 --- a/examples/shader/shader_defs.rs +++ b/examples/shader/shader_defs.rs @@ -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