use bevy::prelude::*;
use bevy_atmosphere::*;
use bevy_flycam::PlayerPlugin;
fn main() {
App::new()
.insert_resource(Msaa { samples: 4 })
.insert_resource(AtmosphereMat::default()) .add_plugins(DefaultPlugins)
.add_plugin(PlayerPlugin) .add_plugin(AtmospherePlugin { dynamic: true }) .add_startup_system(setup_environment)
.add_system(daylight_cycle)
.run();
}
#[derive(Component)]
struct Sun;
fn daylight_cycle(mut sky_mat: ResMut<AtmosphereMat>, mut query: Query<(&mut Transform, &mut DirectionalLight), With<Sun>>, time: Res<Time>) {
let mut pos = sky_mat.sun_position;
let t = time.time_since_startup().as_millis() as f32 / 2000.0;
pos.y = t.sin();
pos.z = t.cos();
sky_mat.sun_position = pos;
if let Some((mut light_trans, mut directional)) = query.single_mut().into() {
light_trans.rotation = Quat::from_rotation_x(-pos.y.atan2(pos.z));
directional.illuminance = t.sin().max(0.0).powf(2.0) * 100000.0;
}
}
fn setup_environment(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>, mut materials: ResMut<Assets<StandardMaterial>>) {
commands.spawn_bundle(DirectionalLightBundle {
..Default::default()
})
.insert(Sun);
commands.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
material: materials.add(StandardMaterial::from(Color::rgb(1.0, 0.0, 0.5))),
..Default::default()
});
}