Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/bevy_light/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ bevy_gizmos = { path = "../bevy_gizmos", version = "0.19.0-dev", optional = true
tracing = { version = "0.1", default-features = false }
wgpu-types = { version = "28", default-features = false }
half = "2.7.1"
smallvec = { version = "1", default-features = false }

[features]
default = []
Expand Down
400 changes: 400 additions & 0 deletions crates/bevy_light/src/atmosphere.rs

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion crates/bevy_light/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#![expect(missing_docs, reason = "Not all docs are written yet, see #3492.")]

extern crate alloc;

use bevy_app::{App, Plugin, PostUpdate};
use bevy_asset::AssetApp;
use bevy_camera::{
primitives::{Aabb, CascadesFrusta, CubemapFrusta, Frustum, Sphere},
visibility::{
Expand Down Expand Up @@ -33,6 +36,8 @@ pub use probe::{
AtmosphereEnvironmentMapLight, EnvironmentMapLight, GeneratedEnvironmentMapLight,
IrradianceVolume, LightProbe, NoParallaxCorrection, Skybox,
};
pub mod atmosphere;
pub use atmosphere::Atmosphere;
mod volumetric;
pub use volumetric::{FogVolume, VolumetricFog, VolumetricLight};
pub mod cascade;
Expand Down Expand Up @@ -70,7 +75,7 @@ pub mod prelude {
pub use crate::gizmos::{LightGizmoColor, LightGizmoConfigGroup, ShowLightGizmo};
}

use crate::directional_light::validate_shadow_map_size;
use crate::{atmosphere::ScatteringMedium, directional_light::validate_shadow_map_size};

/// Constants for operating with the light units: lumens, and lux.
pub mod light_consts {
Expand Down Expand Up @@ -144,6 +149,7 @@ impl Plugin for LightPlugin {
.init_resource::<GlobalAmbientLight>()
.init_resource::<DirectionalLightShadowMap>()
.init_resource::<PointLightShadowMap>()
.init_asset::<ScatteringMedium>()
.configure_sets(
PostUpdate,
SimulationLightSystems::UpdateDirectionalLightCascades
Expand Down
95 changes: 32 additions & 63 deletions crates/bevy_pbr/src/atmosphere/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,23 @@ mod node;
pub mod resources;

use bevy_app::{App, Plugin, Update};
use bevy_asset::{embedded_asset, AssetId, Handle};
use bevy_camera::{Camera3d, Hdr};
use bevy_asset::{embedded_asset, AssetId};
use bevy_camera::Camera3d;
use bevy_core_pipeline::core_3d::graph::Node3d;
use bevy_ecs::{
component::Component,
query::{Changed, QueryItem, With},
schedule::IntoScheduleConfigs,
system::{lifetimeless::Read, Query},
system::{lifetimeless::Read, Commands, Local, Query},
};
use bevy_light::{atmosphere::ScatteringMedium, Atmosphere};
use bevy_math::{UVec2, UVec3, Vec3};
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
use bevy_render::{
extract_component::UniformComponentPlugin,
render_resource::{DownlevelFlags, ShaderType, SpecializedRenderPipelines},
RenderStartup,
sync_world::RenderEntity,
Extract, ExtractSchedule, RenderStartup,
};
use bevy_render::{
extract_component::{ExtractComponent, ExtractComponentPlugin},
Expand All @@ -75,10 +77,7 @@ use resources::{
};
use tracing::warn;

use crate::{
medium::ScatteringMedium,
resources::{init_atmosphere_buffer, write_atmosphere_buffer},
};
use crate::resources::{init_atmosphere_buffer, write_atmosphere_buffer};

use self::{
node::{AtmosphereLutsNode, AtmosphereNode, RenderSkyNode},
Expand Down Expand Up @@ -106,13 +105,17 @@ impl Plugin for AtmospherePlugin {
embedded_asset!(app, "environment.wgsl");

app.add_plugins((
ExtractComponentPlugin::<Atmosphere>::default(),
ExtractComponentPlugin::<GpuAtmosphereSettings>::default(),
ExtractComponentPlugin::<AtmosphereEnvironmentMap>::default(),
UniformComponentPlugin::<GpuAtmosphere>::default(),
UniformComponentPlugin::<GpuAtmosphereSettings>::default(),
))
.register_required_components::<Atmosphere, AtmosphereSettings>()
.add_systems(Update, prepare_atmosphere_probe_components);

if let Some(render_app) = app.get_sub_app_mut(RenderApp) {
render_app.add_systems(ExtractSchedule, extract_atmosphere);
}
}

fn finish(&self, app: &mut App) {
Expand Down Expand Up @@ -203,61 +206,27 @@ impl Plugin for AtmospherePlugin {
}
}

/// Enables atmospheric scattering for an HDR camera.
#[derive(Clone, Component)]
#[require(AtmosphereSettings, Hdr)]
pub struct Atmosphere {
/// Radius of the planet
///
/// units: m
pub bottom_radius: f32,

/// Radius at which we consider the atmosphere to 'end' for our
/// calculations (from center of planet)
///
/// units: m
pub top_radius: f32,

/// An approximation of the average albedo (or color, roughly) of the
/// planet's surface. This is used when calculating multiscattering.
///
/// units: N/A
pub ground_albedo: Vec3,

/// A handle to a [`ScatteringMedium`], which describes the substance
/// of the atmosphere and how it scatters light.
pub medium: Handle<ScatteringMedium>,
}

impl Atmosphere {
pub fn earthlike(medium: Handle<ScatteringMedium>) -> Self {
const EARTH_BOTTOM_RADIUS: f32 = 6_360_000.0;
const EARTH_TOP_RADIUS: f32 = 6_460_000.0;
const EARTH_ALBEDO: Vec3 = Vec3::splat(0.3);
Self {
bottom_radius: EARTH_BOTTOM_RADIUS,
top_radius: EARTH_TOP_RADIUS,
ground_albedo: EARTH_ALBEDO,
medium,
}
}
}

impl ExtractComponent for Atmosphere {
type QueryData = Read<Atmosphere>;

type QueryFilter = With<Camera3d>;

type Out = ExtractedAtmosphere;

fn extract_component(item: QueryItem<'_, '_, Self::QueryData>) -> Option<Self::Out> {
Some(ExtractedAtmosphere {
bottom_radius: item.bottom_radius,
top_radius: item.top_radius,
ground_albedo: item.ground_albedo,
medium: item.medium.id(),
})
// This is needed because of the orphan rule not allowing implementing
// foreign trait ExtractComponent on foreign type Atmosphere
pub fn extract_atmosphere(
mut commands: Commands,
mut previous_len: Local<usize>,
query: Extract<Query<(RenderEntity, &Atmosphere), With<Camera3d>>>,
) {
let mut values = Vec::with_capacity(*previous_len);
for (entity, item) in &query {
values.push((
entity,
ExtractedAtmosphere {
bottom_radius: item.bottom_radius,
top_radius: item.top_radius,
ground_albedo: item.ground_albedo,
medium: item.medium.id(),
},
));
}
*previous_len = values.len();
commands.try_insert_batch(values);
}

/// The render-world representation of an `Atmosphere`, but which
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_pbr/src/atmosphere/resources.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{
ExtractedAtmosphere, GpuLights, GpuScatteringMedium, LightMeta, ScatteringMedium,
ScatteringMediumSampler,
ExtractedAtmosphere, GpuLights, GpuScatteringMedium, LightMeta, ScatteringMediumSampler,
};
use bevy_asset::{load_embedded_asset, AssetId, Handle};
use bevy_camera::{Camera, Camera3d};
Expand All @@ -16,6 +15,7 @@ use bevy_ecs::{
world::{FromWorld, World},
};
use bevy_image::ToExtents;
use bevy_light::atmosphere::ScatteringMedium;
use bevy_math::{Affine3A, Mat4, Vec3, Vec3A};
use bevy_render::{
extract_component::ComponentUniforms,
Expand Down
Loading
Loading