use bevy::input::common_conditions::input_just_pressed;
use bevy::prelude::*;
use bevy::sprite::MaterialMesh2dBundle;
use bevy_light_2d::prelude::*;
fn main() {
App::new()
.insert_resource(GlobalBrightness::default())
.add_plugins((DefaultPlugins, Light2dPlugin))
.add_systems(Startup, setup)
.add_systems(Update, update_brightness)
.add_systems(
Update,
(reset, setup)
.chain()
.run_if(input_just_pressed(KeyCode::KeyR)),
)
.run();
}
#[derive(Resource)]
struct GlobalBrightness {
value: f32,
transition_timer: Timer,
transition_direction: f32,
}
impl Default for GlobalBrightness {
fn default() -> Self {
Self {
value: 1.0,
transition_timer: Timer::from_seconds(3.0, TimerMode::Repeating),
transition_direction: -1.0,
}
}
}
fn reset(query: Query<Entity, Without<Window>>, mut commands: Commands) {
println!("Reset");
for entity in query.iter() {
commands.entity(entity).despawn_recursive();
}
}
fn setup(
brightness: Res<GlobalBrightness>,
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
println!("Setup");
for i in 0..20 {
let pos_x = -500.0 + (i as f32 * 50.0);
let pos_y = 50.0;
commands.spawn((MaterialMesh2dBundle {
mesh: meshes.add(Circle::new(16.0)).into(),
material: materials.add(Color::from(bevy::color::palettes::css::BLUE)),
transform: Transform::from_translation(Vec3::ZERO.with_x(pos_x).with_y(pos_y)),
..default()
},));
}
commands.spawn((
Camera2dBundle {
camera: Camera {
hdr: true,
..Default::default()
},
..Default::default()
},
bevy_light_2d::light::AmbientLight2d {
brightness: brightness.value,
..default()
},
));
for i in 0..20 {
let pos_x = -500.0 + (i as f32 * 50.0);
let pos_y = 0.0;
commands.spawn((MaterialMesh2dBundle {
mesh: meshes.add(Circle::new(16.0)).into(),
material: materials.add(Color::from(bevy::color::palettes::css::GREEN)),
transform: Transform::from_translation(Vec3::ZERO.with_x(pos_x).with_y(pos_y)),
..default()
},));
}
// Weak light
commands.spawn((PointLight2dBundle {
transform: Transform::from_xyz(-200.0, 0.0, 0.0),
point_light: PointLight2d {
color: Color::srgba(1.0, 0.0, 0.0, 1.0),
intensity: 5.0,
radius: 100.0,
..default()
},
..default()
},));
// Medium light
commands.spawn((PointLight2dBundle {
transform: Transform::from_xyz(0.0, 0.0, 0.0),
point_light: PointLight2d {
color: Color::srgba(0.0, 1.0, 0.0, 1.0),
intensity: 25.0,
radius: 100.0,
..default()
},
..default()
},));
// Strong light
commands.spawn((PointLight2dBundle {
transform: Transform::from_xyz(200.0, 0.0, 0.0),
point_light: PointLight2d {
color: Color::srgba(0.0, 0.0, 1.0, 1.0),
intensity: 50.0,
radius: 100.0,
..default()
},
..default()
},));
commands.spawn(
TextBundle::from_section("", TextStyle::default()).with_style(Style {
position_type: PositionType::Absolute,
top: Val::Px(10.0),
left: Val::Px(10.0),
..default()
}),
);
for i in 0..20 {
let pos_x = -500.0 + (i as f32 * 50.0);
let pos_y = -50.0;
commands.spawn((MaterialMesh2dBundle {
mesh: meshes.add(Circle::new(16.0)).into(),
material: materials.add(Color::from(bevy::color::palettes::css::RED)),
transform: Transform::from_translation(Vec3::ZERO.with_x(pos_x).with_y(pos_y)),
..default()
},));
}
}
fn update_brightness(
time: Res<Time>,
mut brightness: ResMut<GlobalBrightness>,
mut ambient_query: Query<&mut bevy_light_2d::light::AmbientLight2d>,
mut text: Query<&mut Text>,
) {
let mut ambient_light = ambient_query
.get_single_mut()
.expect("Expected exactly 1 AmbientLight2d");
brightness.transition_timer.tick(time.delta());
if brightness.transition_timer.just_finished() {
brightness.transition_direction *= -1.0;
}
if brightness.transition_timer.elapsed_secs() < 1.0
|| brightness.transition_timer.just_finished()
{
brightness.value += brightness.transition_direction * time.delta_seconds();
brightness.value = brightness.value.clamp(0.0001, 0.9999);
ambient_light.brightness = brightness.value;
}
let mut text_component = text.single_mut();
text_component.sections[0].value = format!(
"Ambient Brightness: {:.3}, Until Switch: {:.3}",
ambient_light.brightness,
brightness.transition_timer.remaining_secs()
);
}
With ambient brightness 1.0:
With ambient brightness ~0.0:
Example code:
Details
Device details: