diff --git a/src/nodes/drawable/sky.rs b/src/nodes/drawable/sky.rs new file mode 100644 index 0000000..df255c3 --- /dev/null +++ b/src/nodes/drawable/sky.rs @@ -0,0 +1,63 @@ +use bevy::{ + app::{Plugin, Update}, + core_pipeline::{Skybox, core_3d::Camera3d}, + ecs::{ + entity::Entity, + query::With, + system::{Commands, Query, ResMut}, + }, + pbr::environment_map::EnvironmentMapLight, +}; +use bevy_equirect::EquirectManager; +use glam::Quat; + +pub struct SkyPlugin; + +impl Plugin for SkyPlugin { + fn build(&self, app: &mut bevy::app::App) { + app.add_systems(Update, apply_sky); + } +} + +// TODO: make this work with cameras spawned after setting the sky texture +fn apply_sky( + mut equirect: ResMut, + cameras: Query>, + mut cmds: Commands, +) { + if let Some(tex) = super::QUEUED_SKYTEX.lock().take() { + if let Some(path) = tex { + let image_handle = equirect.load_equirect_as_cubemap(path, 1024); + for cam in cameras { + cmds.entity(cam).insert(Skybox { + image: image_handle.clone(), + brightness: 1000.0, + rotation: Quat::IDENTITY, + }); + } + } else { + for cam in cameras { + cmds.entity(cam).remove::(); + } + } + } + if let Some(light) = super::QUEUED_SKYLIGHT.lock().take() { + if let Some(path) = light { + let image_handle = equirect.load_equirect_as_cubemap(path, 1024); + for cam in cameras { + cmds.entity(cam).insert(EnvironmentMapLight { + diffuse_map: image_handle.clone(), + // we might want to use the SkyTex for this? + specular_map: image_handle.clone(), + intensity: 1000.0, + rotation: Quat::IDENTITY, + affects_lightmapped_mesh_diffuse: false, + }); + } + } else { + for cam in cameras { + cmds.entity(cam).remove::(); + } + } + } +}