bevy_mod_outline/examples/pieces.rs

109 lines
3.6 KiB
Rust
Raw Normal View History

use std::f32::consts::TAU;
use bevy::{prelude::*, window::close_on_esc};
use bevy_mod_outline::*;
#[bevy_main]
fn main() {
App::new()
2023-03-10 18:43:13 +00:00
.insert_resource(Msaa::Sample4)
.insert_resource(ClearColor(Color::BLACK))
.add_plugins((DefaultPlugins, OutlinePlugin))
.add_systems(Startup, setup)
.add_systems(Update, (close_on_esc, rotates))
.run();
}
#[derive(Component)]
struct Rotates;
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// Add sphere with child meshes sticking out of it
commands
2022-11-22 00:05:47 +00:00
.spawn(PbrBundle {
mesh: meshes.add(Sphere::new(0.75).mesh().uv(30, 30)),
2024-02-16 00:30:51 +00:00
material: materials.add(StandardMaterial::from(Color::rgb(0.9, 0.1, 0.1))),
transform: Transform::from_translation(Vec3::new(0.0, 1.0, 0.0)),
..default()
})
.insert(OutlineBundle {
outline: OutlineVolume {
visible: true,
colour: Color::WHITE,
width: 10.0,
},
stencil: OutlineStencil {
offset: 5.0,
..default()
},
..default()
})
.insert(Rotates)
.with_children(|parent| {
parent
2022-11-22 00:05:47 +00:00
.spawn(PbrBundle {
mesh: meshes.add(
Capsule3d::new(0.2, 1.0)
.mesh()
.rings(15)
.latitudes(15)
.longitudes(15)
.build(),
),
2024-02-16 00:30:51 +00:00
material: materials.add(StandardMaterial::from(Color::rgb(0.1, 0.1, 0.9))),
transform: Transform::from_rotation(Quat::from_axis_angle(Vec3::X, TAU / 4.0))
.with_translation(Vec3::new(0.0, 0.0, 0.75)),
..default()
})
.insert(InheritOutlineBundle::default());
parent
2022-11-22 00:05:47 +00:00
.spawn(PbrBundle {
mesh: meshes.add(
Torus {
minor_radius: 0.1,
major_radius: 0.5,
}
.mesh()
.minor_resolution(15)
.major_resolution(30)
.build(),
),
2024-02-16 00:30:51 +00:00
material: materials.add(StandardMaterial::from(Color::rgb(0.1, 0.1, 0.9))),
transform: Transform::from_rotation(Quat::from_axis_angle(Vec3::Z, TAU / 4.0))
.with_translation(Vec3::new(0.0, 0.0, -0.75)),
..default()
})
.insert(InheritOutlineBundle::default());
});
// Add plane, light source, and camera
2022-11-22 00:05:47 +00:00
commands.spawn(PbrBundle {
mesh: meshes.add(Plane3d::new(Vec3::Y).mesh().size(5.0, 5.0).build()),
2024-02-16 00:30:51 +00:00
material: materials.add(StandardMaterial::from(Color::rgb(0.3, 0.5, 0.3))),
..default()
});
2022-11-22 00:05:47 +00:00
commands.spawn(PointLightBundle {
point_light: PointLight {
shadows_enabled: true,
..default()
},
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..default()
});
2022-11-22 00:05:47 +00:00
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
}
fn rotates(mut query: Query<&mut Transform, With<Rotates>>, timer: Res<Time>) {
for mut transform in query.iter_mut() {
transform.rotate_axis(Vec3::Y, 0.75 * timer.delta_seconds());
}
}