use bevy::{pbr::light_consts::lux::AMBIENT_DAYLIGHT, prelude::*};
use bevy_atmosphere::prelude::*;
use bevy_spectator::{Spectator, SpectatorPlugin};
fn main() {
App::new()
.insert_resource(AtmosphereModel::default()) .insert_resource(CycleTimer(Timer::new(
std::time::Duration::from_millis(50), TimerMode::Repeating,
)))
.add_plugins((
DefaultPlugins,
SpectatorPlugin, AtmospherePlugin, ))
.add_systems(Startup, setup_environment)
.add_systems(Update, daylight_cycle)
.run();
}
#[derive(Component)]
struct Sun;
#[derive(Resource)]
struct CycleTimer(Timer);
fn daylight_cycle(
mut atmosphere: AtmosphereMut<Nishita>,
mut query: Query<(&mut Transform, &mut DirectionalLight), With<Sun>>,
mut timer: ResMut<CycleTimer>,
time: Res<Time>,
) {
timer.0.tick(time.delta());
if timer.0.finished() {
let t = time.elapsed_secs_wrapped() / 2.0;
atmosphere.sun_position = Vec3::new(0., t.sin(), t.cos());
if let Some(Ok((mut light_trans, mut directional))) = query.single_mut().into() {
light_trans.rotation = Quat::from_rotation_x(-t);
directional.illuminance = t.sin().max(0.0).powf(2.0) * AMBIENT_DAYLIGHT;
}
}
}
fn setup_environment(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands.spawn((
DirectionalLight::default(),
Sun, ));
commands.spawn((
Mesh3d(meshes.add(Cuboid::default())),
MeshMaterial3d(materials.add(StandardMaterial::from(Color::srgb(0.8, 0.8, 0.8)))),
));
commands.spawn((
Mesh3d(meshes.add(Cuboid::new(0.5, 0.5, 0.5))),
MeshMaterial3d(materials.add(StandardMaterial::from(Color::srgb(0.8, 0.0, 0.0)))),
Transform::from_xyz(1., 0., 0.),
));
commands.spawn((
Mesh3d(meshes.add(Cuboid::new(0.5, 0.5, 0.5))),
MeshMaterial3d(materials.add(StandardMaterial::from(Color::srgb(0.0, 0.8, 0.0)))),
Transform::from_xyz(0., 1., 0.),
));
commands.spawn((
Mesh3d(meshes.add(Cuboid::new(0.5, 0.5, 0.5))),
MeshMaterial3d(materials.add(StandardMaterial::from(Color::srgb(0.0, 0.0, 0.8)))),
Transform::from_xyz(0., 0., 1.),
));
commands.spawn((
Camera3d::default(),
Transform::from_xyz(5., 0., 5.),
Msaa::Sample4,
AtmosphereCamera::default(), Spectator, ));
}