diff --git a/README.md b/README.md index 31d02eb..065d0cc 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,13 @@ a gap between the object and its outline. cargo run --example pieces ``` +Many instances of the same mesh, with two different outline configurations, flying towards the +camera. + +```shell +cargo run --example flying_objects +``` + An outlined torus viewed through four cameras with different combinations of render layers enabled. diff --git a/examples/flying_objects.rs b/examples/flying_objects.rs new file mode 100644 index 0000000..6e6cc20 --- /dev/null +++ b/examples/flying_objects.rs @@ -0,0 +1,125 @@ +use std::{f32::consts::TAU, num::Wrapping, time::Duration}; + +use bevy::{ + prelude::{shape::Capsule, *}, + window::close_on_esc, +}; + +use bevy_mod_outline::*; + +#[bevy_main] +fn main() { + App::new() + .insert_resource(Msaa::Sample4) + .insert_resource(ClearColor(Color::BLACK)) + .add_plugins((DefaultPlugins, OutlinePlugin)) + .add_systems(Startup, setup) + .add_systems( + Update, + (close_on_esc, spawn_objects, move_objects, despawn_objects), + ) + .run(); +} + +#[derive(Resource)] +struct MyAssets { + mesh: Handle, + material: Handle, +} + +#[derive(Component)] +struct FlyingObject; + +fn setup( + mut commands: Commands, + mut meshes: ResMut>, + mut materials: ResMut>, +) { + commands.insert_resource(MyAssets { + mesh: meshes.add( + Capsule { + radius: 1.0, + rings: 10, + depth: 2.0, + latitudes: 15, + longitudes: 15, + ..default() + } + .into(), + ), + material: materials.add(Color::BEIGE.into()), + }); + + // Add light source and camera + commands.spawn(DirectionalLightBundle { + directional_light: DirectionalLight { + illuminance: 750.0, + ..default() + }, + ..default() + }); + commands.spawn(Camera3dBundle { + transform: Transform::from_xyz(0.0, 0.0, 50.0).looking_at(Vec3::ZERO, Vec3::Y), + ..default() + }); +} + +struct SpawnState(Timer, Wrapping); + +impl Default for SpawnState { + fn default() -> Self { + let mut timer = Timer::from_seconds(0.75, TimerMode::Repeating); + timer.tick(timer.duration() - Duration::from_nanos(1)); + Self(timer, Wrapping(0)) + } +} + +fn spawn_objects( + mut commands: Commands, + mut timer: Local, + time: Res