Skip to content

Commit

Permalink
Add 3d shapes example
Browse files Browse the repository at this point in the history
  • Loading branch information
rparrett committed Apr 27, 2022
1 parent 328c26d commit a462f9b
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Binary file added assets/textures/uv_debug.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
88 changes: 88 additions & 0 deletions examples/3d/shapes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use bevy::prelude::*;

fn main() {
App::new()
.insert_resource(Msaa { samples: 4 })
.add_plugins(DefaultPlugins)
.add_startup_system(setup)
.add_system(rotate)
.run();
}

#[derive(Component)]
struct Shape;

const X_EXTENT: f32 = 14.;

fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
asset_server: ResMut<AssetServer>,
) {
let debug_material = materials.add(StandardMaterial {
base_color_texture: Some(asset_server.load("textures/uv_debug.png")),
..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()
});

// plane
commands.spawn_bundle(PbrBundle {
mesh: meshes.add(shape::Plane { size: 50. }.into()),
material: materials.add(Color::SILVER.into()),
..Default::default()
});

// camera
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<Shape>>, time: Res<Time>) {
for mut transform in query.iter_mut() {
transform.rotation = Quat::from_rotation_y(
std::f32::consts::PI / -2. + time.seconds_since_startup() as f32 / 2.,
) * Quat::from_rotation_x(-std::f32::consts::PI / 4.)
}
}

0 comments on commit a462f9b

Please sign in to comment.