mod environment;
mod node;
pub mod resources;
use bevy_app::{App, Plugin, Update};
use bevy_asset::{embedded_asset, AssetId};
use bevy_camera::Camera3d;
use bevy_core_pipeline::{
core_3d::{main_opaque_pass_3d, main_transparent_pass_3d},
schedule::{Core3d, Core3dSystems},
};
use bevy_ecs::{
component::Component,
query::{Changed, QueryItem, With},
schedule::IntoScheduleConfigs,
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},
sync_component::SyncComponent,
sync_world::RenderEntity,
Extract, ExtractSchedule, RenderStartup,
};
use bevy_render::{
extract_component::{ExtractComponent, ExtractComponentPlugin},
render_resource::{TextureFormat, TextureUsages},
renderer::RenderAdapter,
Render, RenderApp, RenderSystems,
};
use bevy_shader::load_shader_library;
use environment::{
atmosphere_environment, init_atmosphere_probe_layout, init_atmosphere_probe_pipeline,
prepare_atmosphere_probe_bind_groups, prepare_atmosphere_probe_components,
prepare_probe_textures, AtmosphereEnvironmentMap,
};
use node::{atmosphere_luts, render_sky};
use resources::{
prepare_atmosphere_transforms, prepare_atmosphere_uniforms, queue_render_sky_pipelines,
AtmosphereTransforms, GpuAtmosphere, RenderSkyBindGroupLayouts,
};
use tracing::warn;
use crate::resources::{init_atmosphere_buffer, write_atmosphere_buffer};
use self::resources::{
prepare_atmosphere_bind_groups, prepare_atmosphere_textures, AtmosphereBindGroupLayouts,
AtmosphereLutPipelines, AtmosphereSampler,
};
#[doc(hidden)]
pub struct AtmospherePlugin;
impl Plugin for AtmospherePlugin {
fn build(&self, app: &mut App) {
load_shader_library!(app, "types.wgsl");
load_shader_library!(app, "functions.wgsl");
load_shader_library!(app, "bruneton_functions.wgsl");
load_shader_library!(app, "bindings.wgsl");
embedded_asset!(app, "transmittance_lut.wgsl");
embedded_asset!(app, "multiscattering_lut.wgsl");
embedded_asset!(app, "sky_view_lut.wgsl");
embedded_asset!(app, "aerial_view_lut.wgsl");
embedded_asset!(app, "render_sky.wgsl");
embedded_asset!(app, "environment.wgsl");
app.add_plugins((
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) {
let Some(render_app) = app.get_sub_app_mut(RenderApp) else {
return;
};
let render_adapter = render_app.world().resource::<RenderAdapter>();
if !render_adapter
.get_downlevel_capabilities()
.flags
.contains(DownlevelFlags::COMPUTE_SHADERS)
{
warn!("AtmospherePlugin not loaded. GPU lacks support for compute shaders.");
return;
}
if !render_adapter
.get_texture_format_features(TextureFormat::Rgba16Float)
.allowed_usages
.contains(TextureUsages::STORAGE_BINDING)
{
warn!("AtmospherePlugin not loaded. GPU lacks support: TextureFormat::Rgba16Float does not support TextureUsages::STORAGE_BINDING.");
return;
}
render_app
.insert_resource(AtmosphereBindGroupLayouts::new())
.init_resource::<RenderSkyBindGroupLayouts>()
.init_resource::<AtmosphereSampler>()
.init_resource::<AtmosphereLutPipelines>()
.init_resource::<AtmosphereTransforms>()
.init_resource::<SpecializedRenderPipelines<RenderSkyBindGroupLayouts>>()
.add_systems(
RenderStartup,
(
init_atmosphere_probe_layout,
init_atmosphere_probe_pipeline,
init_atmosphere_buffer,
)
.chain(),
)
.add_systems(
Render,
(
configure_camera_depth_usages.in_set(RenderSystems::ManageViews),
queue_render_sky_pipelines.in_set(RenderSystems::Queue),
prepare_atmosphere_textures.in_set(RenderSystems::PrepareResources),
prepare_probe_textures
.in_set(RenderSystems::PrepareResources)
.after(prepare_atmosphere_textures),
prepare_atmosphere_uniforms
.before(RenderSystems::PrepareResources)
.after(RenderSystems::PrepareAssets),
prepare_atmosphere_probe_bind_groups.in_set(RenderSystems::PrepareBindGroups),
prepare_atmosphere_transforms.in_set(RenderSystems::PrepareResources),
prepare_atmosphere_bind_groups.in_set(RenderSystems::PrepareBindGroups),
write_atmosphere_buffer.in_set(RenderSystems::PrepareResources),
),
)
.add_systems(
Core3d,
(
(atmosphere_luts, atmosphere_environment)
.chain()
.after(Core3dSystems::Prepass)
.before(Core3dSystems::MainPass),
render_sky
.after(main_opaque_pass_3d)
.before(main_transparent_pass_3d),
),
);
}
}
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);
}
#[derive(Clone, Component)]
pub struct ExtractedAtmosphere {
pub bottom_radius: f32,
pub top_radius: f32,
pub ground_albedo: Vec3,
pub medium: AssetId<ScatteringMedium>,
}
#[derive(Clone, Component, Reflect)]
#[reflect(Clone, Default)]
pub struct AtmosphereSettings {
pub transmittance_lut_size: UVec2,
pub multiscattering_lut_size: UVec2,
pub sky_view_lut_size: UVec2,
pub aerial_view_lut_size: UVec3,
pub transmittance_lut_samples: u32,
pub multiscattering_lut_dirs: u32,
pub multiscattering_lut_samples: u32,
pub sky_view_lut_samples: u32,
pub aerial_view_lut_samples: u32,
pub aerial_view_lut_max_distance: f32,
pub scene_units_to_m: f32,
pub sky_max_samples: u32,
pub rendering_method: AtmosphereMode,
}
impl Default for AtmosphereSettings {
fn default() -> Self {
Self {
transmittance_lut_size: UVec2::new(256, 128),
transmittance_lut_samples: 40,
multiscattering_lut_size: UVec2::new(32, 32),
multiscattering_lut_dirs: 64,
multiscattering_lut_samples: 20,
sky_view_lut_size: UVec2::new(400, 200),
sky_view_lut_samples: 16,
aerial_view_lut_size: UVec3::new(32, 32, 32),
aerial_view_lut_samples: 10,
aerial_view_lut_max_distance: 3.2e4,
scene_units_to_m: 1.0,
sky_max_samples: 16,
rendering_method: AtmosphereMode::LookupTexture,
}
}
}
#[derive(Clone, Component, Reflect, ShaderType)]
#[reflect(Default)]
pub struct GpuAtmosphereSettings {
pub transmittance_lut_size: UVec2,
pub multiscattering_lut_size: UVec2,
pub sky_view_lut_size: UVec2,
pub aerial_view_lut_size: UVec3,
pub transmittance_lut_samples: u32,
pub multiscattering_lut_dirs: u32,
pub multiscattering_lut_samples: u32,
pub sky_view_lut_samples: u32,
pub aerial_view_lut_samples: u32,
pub aerial_view_lut_max_distance: f32,
pub scene_units_to_m: f32,
pub sky_max_samples: u32,
pub rendering_method: u32,
}
impl Default for GpuAtmosphereSettings {
fn default() -> Self {
AtmosphereSettings::default().into()
}
}
impl From<AtmosphereSettings> for GpuAtmosphereSettings {
fn from(s: AtmosphereSettings) -> Self {
Self {
transmittance_lut_size: s.transmittance_lut_size,
multiscattering_lut_size: s.multiscattering_lut_size,
sky_view_lut_size: s.sky_view_lut_size,
aerial_view_lut_size: s.aerial_view_lut_size,
transmittance_lut_samples: s.transmittance_lut_samples,
multiscattering_lut_dirs: s.multiscattering_lut_dirs,
multiscattering_lut_samples: s.multiscattering_lut_samples,
sky_view_lut_samples: s.sky_view_lut_samples,
aerial_view_lut_samples: s.aerial_view_lut_samples,
aerial_view_lut_max_distance: s.aerial_view_lut_max_distance,
scene_units_to_m: s.scene_units_to_m,
sky_max_samples: s.sky_max_samples,
rendering_method: s.rendering_method as u32,
}
}
}
impl SyncComponent for GpuAtmosphereSettings {
type Out = Self;
}
impl ExtractComponent for GpuAtmosphereSettings {
type QueryData = Read<AtmosphereSettings>;
type QueryFilter = (With<Camera3d>, With<Atmosphere>);
fn extract_component(item: QueryItem<'_, '_, Self::QueryData>) -> Option<Self::Out> {
Some(item.clone().into())
}
}
fn configure_camera_depth_usages(
mut cameras: Query<&mut Camera3d, (Changed<Camera3d>, With<ExtractedAtmosphere>)>,
) {
for mut camera in &mut cameras {
camera.depth_texture_usages.0 |= TextureUsages::TEXTURE_BINDING.bits();
}
}
#[repr(u32)]
#[derive(Clone, Default, Reflect, Copy)]
pub enum AtmosphereMode {
#[default]
LookupTexture = 0,
Raymarched = 1,
}