use bevy::prelude::*;
use bevy::render::mesh::Indices;
fn add_cube_mesh(commands: &mut Commands, meshes: &mut ResMut<Assets<Mesh>>) {
// Define cube mesh vertices
let cube_verts = vec![
// front face
([0.5, 0.5, 0.5], [0.0, 0.0], [0.0, 0.0, 1.0]), // top right
([-0.5, 0.5, 0.5], [1.0, 0.0], [0.0, 0.0, 1.0]), // top left
([0.5, -0.5, 0.5], [0.0, 1.0], [0.0, 0.0, 1.0]), // bottom right
([-0.5, -0.5, 0.5], [1.0, 1.0], [0.0, 0.0, 1.0]), // bottom left
// back face
([0.5, 0.5, -0.5], [0.0, 0.0], [0.0, 0.0, -1.0]), // top right
([0.5, -0.5, -0.5], [0.0, 1.0], [0.0, 0.0, -1.0]), // bottom right
([-0.5, 0.5, -0.5], [1.0, 0.0], [0.0, 0.0, -1.0]), // top left
([-0.5, -0.5, -0.5], [1.0, 1.0], [0.0, 0.0, -1.0]), // bottom left
// top face
([0.5, 0.5, 0.5], [0.0, 0.0], [0.0, 1.0, 0.0]), // top right
([0.5, 0.5, -0.5], [0.0, 1.0], [0.0, 1.0, 0.0]), // bottom right
([-0.5, 0.5, 0.5], [1.0, 0.0], [0.0, 1.0, 0.0]), // top left
([-0.5, 0.5, -0.5], [1.0, 1.0], [0.0, 1.0, 0.0]), // bottom left
// bottom face
([0.5, -0.5, 0.5], [0.0, 0.0], [0.0, -1.0, 0.0]), // top right
([-0.5, -0.5, 0.5], [1.0, 0.0], [0.0, -1.0, 0.0]), // top left
([0.5, -0.5, -0.5], [0.0, 1.0], [0.0, -1.0, 0.0]), // bottom right
([-0.5, -0.5, -0.5], [1.0, 1.0], [0.0, -1.0, 0.0]), // bottom left
// right face
([0.5, 0.5, 0.5], [1.0, 0.0], [1.0, 0.0, 0.0]), // top back
([0.5, -0.5, 0.5], [1.0, 1.0], [1.0, 0.0, 0.0]), // bottom back
([0.5, 0.5, -0.5], [0.0, 0.0], [1.0, 0.0, 0.0]), // top front
([0.5, -0.5, -0.5], [0.0, 1.0], [1.0, 0.0, 0.0]), // bottom front
// left face
([-0.5, 0.5, -0.5], [1.0, 0.0], [-1.0, 0.0, 0.0]), // top back
([-0.5, -0.5, -0.5], [1.0, 1.0], [-1.0, 0.0, 0.0]), // bottom back
([-0.5, 0.5, 0.5], [0.0, 0.0], [-1.0, 0.0, 0.0]), // top front
([-0.5, -0.5, 0.5], [0.0, 1.0], [-1.0, 0.0, 0.0]), // bottom front
];
// Define cube mesh indices
let cube_indices = vec![
0u32, 1, 2, // front face
2, 1, 3,
4, 5, 6, // back face
6, 5, 7,
8, 9, 10, // top face
10, 9, 11,
12, 13, 14, // bottom face
14, 13, 15,
16, 17, 18, // right face
18, 17, 19,
20, 21, 22, // left face
22, 21, 23,
];
// Create the cube mesh
let cube_mesh = Mesh::new(PrimitiveTopology::TriangleList, cube_verts, None, None, cube_indices, None);
// Add the mesh to the asset manager
let cube_handle = meshes.add(cube_mesh);
// Spawn an entity with the cube mesh as a component
commands.spawn_bundle(PbrBundle {
mesh: cube_handle,
..Default::default()
});
}
When you run the app, you should see a spinning cube on your screen. Congratulations, you've created a spinning cube using Bevy in Rust!