diff --git a/Cargo.toml b/Cargo.toml index 35276aeb441be..afbd45eda1c71 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -200,6 +200,10 @@ path = "examples/3d/pbr.rs" name = "shadow_biases" path = "examples/3d/shadow_biases.rs" +[[example]] +name = "3d_shapes" +path = "examples/3d/shapes.rs" + [[example]] name = "shadow_caster_receiver" path = "examples/3d/shadow_caster_receiver.rs" diff --git a/examples/3d/shapes.rs b/examples/3d/shapes.rs new file mode 100644 index 0000000000000..13f04bb69e89b --- /dev/null +++ b/examples/3d/shapes.rs @@ -0,0 +1,121 @@ +//! This example demonstrates the built-in 3d shapes in Bevy. +//! The scene includes a patterned texture and a rotation for visualizing the normals and UVs. + +use bevy::{ + prelude::*, + render::render_resource::{Extent3d, TextureDimension, TextureFormat}, +}; + +fn main() { + App::new() + .insert_resource(Msaa { samples: 4 }) + .add_plugins(DefaultPlugins) + .add_startup_system(setup) + .add_system(rotate) + .run(); +} + +/// A marker component for our shapes so we can query them separately from the ground plane +#[derive(Component)] +struct Shape; + +const X_EXTENT: f32 = 14.; + +fn setup( + mut commands: Commands, + mut meshes: ResMut>, + mut images: ResMut>, + mut materials: ResMut>, +) { + let debug_material = materials.add(StandardMaterial { + base_color_texture: Some(images.add(uv_debug_texture())), + ..default() + }); + + let shapes = [ + meshes.add(shape::Cube::default().into()), + meshes.add(shape::Box::default().into()), + meshes.add(shape::Capsule::default().into()), + meshes.add(shape::Torus::default().into()), + meshes.add(shape::Icosphere::default().into()), + meshes.add(shape::UVSphere::default().into()), + ]; + + let num_shapes = shapes.len(); + + for (i, shape) in shapes.into_iter().enumerate() { + commands + .spawn_bundle(PbrBundle { + mesh: shape, + material: debug_material.clone(), + transform: Transform { + translation: Vec3::new( + -X_EXTENT / 2. + i as f32 / (num_shapes - 1) as f32 * X_EXTENT, + 2.0, + 0.0, + ), + ..default() + }, + ..Default::default() + }) + .insert(Shape); + } + + commands.spawn_bundle(PointLightBundle { + point_light: PointLight { + intensity: 9000.0, + range: 100., + shadows_enabled: true, + ..Default::default() + }, + transform: Transform::from_xyz(8.0, 16.0, 8.0), + ..Default::default() + }); + + // ground plane + commands.spawn_bundle(PbrBundle { + mesh: meshes.add(shape::Plane { size: 50. }.into()), + material: materials.add(Color::SILVER.into()), + ..Default::default() + }); + + commands.spawn_bundle(PerspectiveCameraBundle { + transform: Transform::from_xyz(0.0, 6., 12.0).looking_at(Vec3::new(0., 1., 0.), Vec3::Y), + ..Default::default() + }); +} + +fn rotate(mut query: Query<&mut Transform, With>, time: Res