| Crates.io | bevy_sun_move |
| lib.rs | bevy_sun_move |
| version | 0.1.0 |
| created_at | 2025-04-30 06:52:39.716304+00 |
| updated_at | 2025-04-30 06:52:39.716304+00 |
| description | A Bevy plugin for simulating realistic sun movement |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1654550 |
| size | 3,109,551 |
A Bevy plugin for simulating realistic sun movement, integrating well with Bevy's Atmosphere and PBR lighting.
This plugin provides components and systems to:
Add bevy_sun_move to your Cargo.toml:
cargo add bevy_sun_move
Add the plugin
use bevy::prelude::*;
use bevy_sun_move::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(SunMovePlugin)
.add_plugins(RandomStarsPlugin) // Optional for very simple stars on night sky
.run();
}
Then
You have two main ways to configure the SkyCenter:
Using TimedSkyConfig (Recommended): Define your desired day/night lengths and maximum sun height. The plugin will calculate the necessary astronomical parameters for correct sun move.
// In your setup system:
fn setup_scene(mut commands: Commands, ...) {
let sun_id = commands
.spawn((
DirectionalLight {
shadows_enabled: true,
illuminance: light_consts::lux::RAW_SUNLIGHT, // Adjust illuminance as needed
..default()
},
Transform::default(), // Transform will be updated by the plugin
))
.id();
let timed_sky_config = TimedSkyConfig {
sun_entity: sun_id,
day_duration_secs: 10.0, // 10 seconds of daylight
night_duration_secs: 5.0, // 5 seconds of nighttime (15s total cycle)
max_sun_height_deg: 60.0, // Sun reaches 60 degrees altitude at noon
planet_tilt_degrees: 23.5, // Earth's tilt (default)
..default()
};
// Calculate and spawn the SkyCenter
if let Some(sky_center) = SkyCenter::from_timed_config(&timed_sky_config) {
commands.spawn((
sky_center,
// Optional: Add StarSpawner if you want the built-in stars
StarSpawner {
star_count: 1000,
spawn_radius: 5000.0, // Stars distance
},
));
} else {
// Handle case where calculation failed (e.g., impossible parameters)
error!("Failed to create SkyCenter from timed config.");
}
// ... rest of your scene setup
}
Directly using SkyCenter: If you want the specific latitude and year fraction (e.g., simulating a real-world location and date), you can set them directly.
// In your setup system:
fn setup_scene(mut commands: Commands, ...) {
let sun_id = commands
.spawn((
DirectionalLight {
shadows_enabled: true,
illuminance: light_consts::lux::RAW_SUNLIGHT, // Adjust illuminance as needed
..default()
},
Transform::default(), // Transform will be updated by the plugin
))
.id();
commands.spawn((
SkyCenter {
sun: sun_id,
latitude_degrees: 51.5, // e.g., London's approximate latitude
planet_tilt_degrees: 23.5, // Earth's axial tilt
year_fraction: 0.25, // e.g., Summer Solstice
cycle_duration_secs: 60.0, // 60-second day/night cycle
current_cycle_time: 0.0, // Start at midnight
..default()
},
Visibility::Visible,
Transform::default(),
StarSpawner { star_count: 1000, spawn_radius: 5000.0 }, // Optional
));
// ... rest of your scene setup
}
For better results add Atmosphere to your Camera with same way as bevy example describe
use bevy::{
core_pipeline::{bloom::Bloom, tonemapping::Tonemapping},
pbr::{Atmosphere, AtmosphereSettings},
prelude::*,
render::camera::Exposure,
};
// In your setup system:
fn setup_scene(mut commands: Commands) {
commands.spawn((
Camera3d::default(),
// ... camera transform
Camera { hdr: true, ..default() },
Atmosphere::EARTH, // Or customize with AtmosphereSettings
AtmosphereSettings {
aerial_view_lut_max_distance: 3.2e5,
scene_units_to_m: 1e+4, // Scale your scene units to meters
..Default::default()
},
Exposure::SUNLIGHT, // Recommended for good dynamic range
Tonemapping::AcesFitted, // Recommended tonemapping
Bloom::NATURAL, // Optional post-processing
));
// ... rest of your scene setup
}
The update_sky_center system will automatically run in the Update schedule, advancing current_cycle_time and updating the sun's transform based on the SkyCenter parameters.
SkyCenter
A component added to an entity (often the root of your scene or a dedicated sky entity) that defines the parameters of the sky simulation.
TimedSkyConfig
A temporary struct used to calculate SkyCenter parameters based on desired timings.
SkyCenter::from_timed_config(&timed_config) -> Option<SkyCenter>. The function returns None if the requested timings and max height are impossible for the given tilt (e.g., requesting 24-hour day at the equator with 0 tilt, or a max height greater than 90 degrees).Contributions are welcome! Feel free to open issues or pull requests on the GitHub repository.
This project is licensed under the MIT license (or similar, specify your license choice).