|
| 1 | +//! This example demonstrates the built-in 3d shapes in Bevy. |
| 2 | +//! The scene includes a patterned texture and a rotation for visualizing the normals and UVs. |
| 3 | +
|
| 4 | +use bevy::{ |
| 5 | + prelude::*, |
| 6 | + render::render_resource::{Extent3d, TextureDimension, TextureFormat}, |
| 7 | +}; |
| 8 | + |
| 9 | +fn main() { |
| 10 | + App::new() |
| 11 | + .insert_resource(Msaa { samples: 4 }) |
| 12 | + .add_plugins(DefaultPlugins) |
| 13 | + .add_startup_system(setup) |
| 14 | + .add_system(rotate) |
| 15 | + .run(); |
| 16 | +} |
| 17 | + |
| 18 | +/// A marker component for our shapes so we can query them separately from the ground plane |
| 19 | +#[derive(Component)] |
| 20 | +struct Shape; |
| 21 | + |
| 22 | +const X_EXTENT: f32 = 14.; |
| 23 | + |
| 24 | +fn setup( |
| 25 | + mut commands: Commands, |
| 26 | + mut meshes: ResMut<Assets<Mesh>>, |
| 27 | + mut images: ResMut<Assets<Image>>, |
| 28 | + mut materials: ResMut<Assets<StandardMaterial>>, |
| 29 | +) { |
| 30 | + let debug_material = materials.add(StandardMaterial { |
| 31 | + base_color_texture: Some(images.add(uv_debug_texture())), |
| 32 | + ..default() |
| 33 | + }); |
| 34 | + |
| 35 | + let shapes = [ |
| 36 | + meshes.add(shape::Cube::default().into()), |
| 37 | + meshes.add(shape::Box::default().into()), |
| 38 | + meshes.add(shape::Capsule::default().into()), |
| 39 | + meshes.add(shape::Torus::default().into()), |
| 40 | + meshes.add(shape::Icosphere::default().into()), |
| 41 | + meshes.add(shape::UVSphere::default().into()), |
| 42 | + ]; |
| 43 | + |
| 44 | + let num_shapes = shapes.len(); |
| 45 | + |
| 46 | + for (i, shape) in shapes.into_iter().enumerate() { |
| 47 | + commands |
| 48 | + .spawn_bundle(PbrBundle { |
| 49 | + mesh: shape, |
| 50 | + material: debug_material.clone(), |
| 51 | + transform: Transform { |
| 52 | + translation: Vec3::new( |
| 53 | + -X_EXTENT / 2. + i as f32 / (num_shapes - 1) as f32 * X_EXTENT, |
| 54 | + 2.0, |
| 55 | + 0.0, |
| 56 | + ), |
| 57 | + ..default() |
| 58 | + }, |
| 59 | + ..Default::default() |
| 60 | + }) |
| 61 | + .insert(Shape); |
| 62 | + } |
| 63 | + |
| 64 | + commands.spawn_bundle(PointLightBundle { |
| 65 | + point_light: PointLight { |
| 66 | + intensity: 9000.0, |
| 67 | + range: 100., |
| 68 | + shadows_enabled: true, |
| 69 | + ..Default::default() |
| 70 | + }, |
| 71 | + transform: Transform::from_xyz(8.0, 16.0, 8.0), |
| 72 | + ..Default::default() |
| 73 | + }); |
| 74 | + |
| 75 | + // ground plane |
| 76 | + commands.spawn_bundle(PbrBundle { |
| 77 | + mesh: meshes.add(shape::Plane { size: 50. }.into()), |
| 78 | + material: materials.add(Color::SILVER.into()), |
| 79 | + ..Default::default() |
| 80 | + }); |
| 81 | + |
| 82 | + commands.spawn_bundle(PerspectiveCameraBundle { |
| 83 | + transform: Transform::from_xyz(0.0, 6., 12.0).looking_at(Vec3::new(0., 1., 0.), Vec3::Y), |
| 84 | + ..Default::default() |
| 85 | + }); |
| 86 | +} |
| 87 | + |
| 88 | +fn rotate(mut query: Query<&mut Transform, With<Shape>>, time: Res<Time>) { |
| 89 | + for mut transform in query.iter_mut() { |
| 90 | + transform.rotation = Quat::from_rotation_y(time.seconds_since_startup() as f32 / 2.) |
| 91 | + * Quat::from_rotation_x(-std::f32::consts::PI / 4.) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +/// Creates a colorful test pattern |
| 96 | +fn uv_debug_texture() -> Image { |
| 97 | + const TEXTURE_SIZE: usize = 8; |
| 98 | + |
| 99 | + let mut palette: [u8; 32] = [ |
| 100 | + 255, 102, 159, 255, 255, 159, 102, 255, 236, 255, 102, 255, 121, 255, 102, 255, 102, 255, |
| 101 | + 198, 255, 102, 198, 255, 255, 121, 102, 255, 255, 236, 102, 255, 255, |
| 102 | + ]; |
| 103 | + |
| 104 | + let mut texture_data = [0; TEXTURE_SIZE * TEXTURE_SIZE * 4]; |
| 105 | + for y in 0..TEXTURE_SIZE { |
| 106 | + let offset = TEXTURE_SIZE * y * 4; |
| 107 | + texture_data[offset..(offset + TEXTURE_SIZE * 4)].copy_from_slice(&palette); |
| 108 | + palette.rotate_right(4); |
| 109 | + } |
| 110 | + |
| 111 | + Image::new_fill( |
| 112 | + Extent3d { |
| 113 | + width: TEXTURE_SIZE as u32, |
| 114 | + height: TEXTURE_SIZE as u32, |
| 115 | + depth_or_array_layers: 1, |
| 116 | + }, |
| 117 | + TextureDimension::D2, |
| 118 | + &texture_data, |
| 119 | + TextureFormat::Rgba8UnormSrgb, |
| 120 | + ) |
| 121 | +} |
0 commit comments