bevy_mod_outline/examples/torus.rs

85 lines
2.3 KiB
Rust
Raw Normal View History

2022-06-10 21:29:51 +00:00
use std::f32::consts::TAU;
2022-08-01 21:10:53 +00:00
use bevy::prelude::{shape::Torus, *};
2022-06-10 21:29:51 +00:00
use bevy_mod_outline::*;
#[bevy_main]
fn main() {
App::new()
.insert_resource(Msaa { samples: 4 })
.insert_resource(ClearColor(Color::BLACK))
2022-06-10 21:29:51 +00:00
.add_plugins(DefaultPlugins)
.add_plugin(OutlinePlugin)
.add_startup_system(setup)
.add_system(rotate_cube)
.run();
}
#[derive(Component)]
struct TheCube();
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut outlines: ResMut<Assets<Outline>>,
2022-06-10 21:29:51 +00:00
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// Spawn cube et al.
commands.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(bevy::prelude::shape::Plane { size: 5.0 })),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..default()
});
commands
.spawn_bundle(PbrBundle {
2022-08-01 21:10:53 +00:00
mesh: meshes.add(Mesh::from(Torus {
radius: 0.6,
ring_radius: 0.2,
subdivisions_segments: 20,
subdivisions_sides: 10,
2022-06-10 21:29:51 +00:00
})),
material: materials.add(Color::rgb(0.1, 0.1, 0.9).into()),
transform: Transform::from_xyz(0.0, 1.0, 0.0),
..default()
})
.insert(outlines.add(Outline {
colour: Color::rgba(0.0, 1.0, 0.0, 0.5),
width: 25.0,
}))
2022-06-10 21:29:51 +00:00
.insert(TheCube());
commands.spawn_bundle(PointLightBundle {
point_light: PointLight {
intensity: 1500.0,
shadows_enabled: true,
..default()
},
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..default()
});
2022-08-01 21:10:53 +00:00
commands.spawn_bundle(Camera3dBundle {
2022-06-10 21:29:51 +00:00
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
}
fn rotate_cube(
mut cubes: Query<&mut Transform, With<TheCube>>,
timer: Res<Time>,
mut t: Local<f32>,
) {
let ta = *t;
*t = (ta + 0.5 * timer.delta_seconds()) % TAU;
let tb = *t;
let i1 = tb.cos() - ta.cos();
let i2 = ta.sin() - tb.sin();
for mut transform in cubes.iter_mut() {
transform.rotate(Quat::from_rotation_z(
TAU * 20.0 * i1 * timer.delta_seconds(),
));
transform.rotate(Quat::from_rotation_y(
TAU * 20.0 * i2 * timer.delta_seconds(),
));
}
}