ui: Quality panel + runtime bloom-quality rebuild
Adds a dedicated Quality collapsible section: bloom quality dropdown (Off/Low/Medium/High), bloom threshold/strength, exposure, resolution scale, star AA toggle. Changing bloom quality at runtime triggers a pipeline rebuild (despawn bloom entities + respawn via spawn_bloom_pipeline helper). MSAA honestly labeled as decorative. Note: the rebuild currently re-spawns the full 3-level pyramid whenever bloom is enabled (Off vs On). Partial pyramids for Low/Medium are a future enhancement.
This commit is contained in:
parent
c06f1543db
commit
7591b790b2
@ -1,7 +1,7 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
/// Bloom pyramid depth (number of bloom textures).
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Default)]
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Default, Debug)]
|
||||
pub enum BloomQuality {
|
||||
Off, // no bloom, scene-only ACES composite
|
||||
Low, // 1 level: brightpass → composite (soft halo)
|
||||
|
||||
@ -75,6 +75,11 @@ pub struct BloomTarget2(pub Handle<Image>);
|
||||
#[derive(Component)]
|
||||
pub struct BloomFinalTarget(pub Handle<Image>);
|
||||
|
||||
/// Tracks the bloom quality currently applied to the render pipeline.
|
||||
/// When it differs from `params.bloom_quality`, a rebuild is triggered.
|
||||
#[derive(Resource)]
|
||||
pub struct AppliedBloomQuality(pub crate::params::BloomQuality);
|
||||
|
||||
pub struct BlackHolePlugin;
|
||||
|
||||
impl Plugin for BlackHolePlugin {
|
||||
@ -99,6 +104,7 @@ impl Plugin for BlackHolePlugin {
|
||||
),
|
||||
)
|
||||
.add_systems(Update, crate::scene::planets::upload_planets)
|
||||
.add_systems(Update, rebuild_bloom)
|
||||
// bevy_egui 0.41 requires UI systems to run inside the egui context
|
||||
// pass (fonts/ctx are initialized there); placing them in Update panics.
|
||||
.add_systems(bevy_egui::EguiPrimaryContextPass, crate::ui::ui_system);
|
||||
@ -183,9 +189,79 @@ fn spawn_fullscreen_quad(
|
||||
if levels > 0 {
|
||||
// NOTE: when bloom is enabled the full 3-level pyramid (High) always
|
||||
// runs here. Partial pyramid — Low = brightpass-only, Medium = 1 down +
|
||||
// 1 up — is deferred to the runtime rebuild in Task 8, which can spawn
|
||||
// fewer passes. For now this is a simple Off vs On gate.
|
||||
//
|
||||
// 1 up — is a future enhancement; for now this is a simple Off vs On
|
||||
// gate. spawn_bloom_pipeline always emits the full pyramid when called.
|
||||
bloom_final_handle = spawn_bloom_pipeline(
|
||||
&mut commands,
|
||||
&mut images,
|
||||
&mut bp_materials,
|
||||
&mut blur_materials,
|
||||
&mut meshes,
|
||||
&offscreen,
|
||||
w,
|
||||
h,
|
||||
);
|
||||
}
|
||||
|
||||
// Track the quality tier currently realized in the render graph. Must be
|
||||
// set here (not via init_resource) so it matches the tiered params default
|
||||
// (Off on web, High on desktop) rather than the enum's #[default].
|
||||
commands.insert_resource(AppliedBloomQuality(params.bloom_quality));
|
||||
|
||||
// --- Composite quad (draws HDR scene + bloom to the window, ACES tone-mapped) ---
|
||||
// When bloom is off (BloomQuality::Off), bloom_final_handle is None and we
|
||||
// fall back to a 1x1 black texture: the composite samples black, yielding a
|
||||
// scene-only ACES composite (no bloom contribution).
|
||||
let bloom_handle = bloom_final_handle.unwrap_or_else(|| {
|
||||
// 1x1 black fallback — composite samples black, effectively scene-only ACES.
|
||||
// 8 zero bytes = one Rgba16Float pixel (16 bits/channel × 4).
|
||||
images.add(Image::new_fill(
|
||||
bevy::render::render_resource::Extent3d { width: 1, height: 1, depth_or_array_layers: 1 },
|
||||
bevy::render::render_resource::TextureDimension::D2,
|
||||
&[0, 0, 0, 0, 0, 0, 0, 0],
|
||||
TextureFormat::Rgba16Float,
|
||||
bevy::asset::RenderAssetUsages::default(),
|
||||
))
|
||||
});
|
||||
let composite_mat = composite_materials.add(crate::render::material::CompositeMaterial {
|
||||
uniform: crate::render::material::CompositeUniform {
|
||||
bloom_strength: 0.8, exposure: 1.0, _pad0: 0.0, _pad1: 0.0,
|
||||
},
|
||||
scene: offscreen.clone(),
|
||||
bloom: bloom_handle,
|
||||
});
|
||||
commands.spawn((
|
||||
Mesh2d(meshes.add(Rectangle::new(2.0, 2.0))),
|
||||
MeshMaterial2d(composite_mat),
|
||||
Transform::default().with_scale(Vec3::new(win.width() / 2.0, win.height() / 2.0, 1.0)),
|
||||
UpscaleQuad,
|
||||
CompositeQuad,
|
||||
RenderLayers::layer(1),
|
||||
));
|
||||
commands.spawn((Camera2d, Msaa::Off, UpscaleCamera, Nudgable, RenderLayers::layer(1)));
|
||||
}
|
||||
|
||||
/// Spawns the bloom pipeline (brightpass + blur pyramid + bloom_final).
|
||||
/// Called at startup and when bloom_quality changes at runtime.
|
||||
///
|
||||
/// Pragmatic scope: this always emits the full 3-level pyramid (High) when
|
||||
/// called. Partial pyramids — Low (brightpass-only) and Medium (1 down + 1 up)
|
||||
/// — are a future enhancement; the caller simply does not invoke this for
|
||||
/// `BloomQuality::Off`. The `levels`-driven branching is intentionally absent.
|
||||
///
|
||||
/// Returns the `bloom_final` handle (the half-res texture the composite pass
|
||||
/// samples). Caller must fall back to a 1x1 black texture when it returns None.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn spawn_bloom_pipeline(
|
||||
commands: &mut Commands,
|
||||
images: &mut Assets<Image>,
|
||||
bp_materials: &mut Assets<crate::render::material::BrightPassMaterial>,
|
||||
blur_materials: &mut Assets<crate::render::material::BlurMaterial>,
|
||||
meshes: &mut Assets<Mesh>,
|
||||
offscreen: &Handle<Image>,
|
||||
w: u32,
|
||||
h: u32,
|
||||
) -> Option<Handle<Image>> {
|
||||
// --- Bright-pass (bloom stage [2]): half-res float target ---
|
||||
let bw = ((w as f32 * 0.5) as u32).max(1);
|
||||
let bh = ((h as f32 * 0.5) as u32).max(1);
|
||||
@ -316,40 +392,7 @@ fn spawn_fullscreen_quad(
|
||||
RenderTarget::Image(bloom_final.clone().into()), Msaa::Off, BlurCamera, Nudgable, RenderLayers::layer(6),
|
||||
));
|
||||
|
||||
bloom_final_handle = Some(bloom_final.clone());
|
||||
}
|
||||
|
||||
// --- Composite quad (draws HDR scene + bloom to the window, ACES tone-mapped) ---
|
||||
// When bloom is off (BloomQuality::Off), bloom_final_handle is None and we
|
||||
// fall back to a 1x1 black texture: the composite samples black, yielding a
|
||||
// scene-only ACES composite (no bloom contribution).
|
||||
let bloom_handle = bloom_final_handle.unwrap_or_else(|| {
|
||||
// 1x1 black fallback — composite samples black, effectively scene-only ACES.
|
||||
// 8 zero bytes = one Rgba16Float pixel (16 bits/channel × 4).
|
||||
images.add(Image::new_fill(
|
||||
bevy::render::render_resource::Extent3d { width: 1, height: 1, depth_or_array_layers: 1 },
|
||||
bevy::render::render_resource::TextureDimension::D2,
|
||||
&[0, 0, 0, 0, 0, 0, 0, 0],
|
||||
TextureFormat::Rgba16Float,
|
||||
bevy::asset::RenderAssetUsages::default(),
|
||||
))
|
||||
});
|
||||
let composite_mat = composite_materials.add(crate::render::material::CompositeMaterial {
|
||||
uniform: crate::render::material::CompositeUniform {
|
||||
bloom_strength: 0.8, exposure: 1.0, _pad0: 0.0, _pad1: 0.0,
|
||||
},
|
||||
scene: offscreen.clone(),
|
||||
bloom: bloom_handle,
|
||||
});
|
||||
commands.spawn((
|
||||
Mesh2d(meshes.add(Rectangle::new(2.0, 2.0))),
|
||||
MeshMaterial2d(composite_mat),
|
||||
Transform::default().with_scale(Vec3::new(win.width() / 2.0, win.height() / 2.0, 1.0)),
|
||||
UpscaleQuad,
|
||||
CompositeQuad,
|
||||
RenderLayers::layer(1),
|
||||
));
|
||||
commands.spawn((Camera2d, Msaa::Off, UpscaleCamera, Nudgable, RenderLayers::layer(1)));
|
||||
Some(bloom_final.clone())
|
||||
}
|
||||
|
||||
/// Recreate the offscreen Image and rescale both quads on window resize,
|
||||
@ -437,6 +480,80 @@ fn nudge_camera(
|
||||
}
|
||||
}
|
||||
|
||||
/// Detects a bloom_quality change and rebuilds the bloom pipeline.
|
||||
/// Heavy (despawns and respawns all bloom entities + their cameras/targets)
|
||||
/// but only fires when the user changes the dropdown in the Quality panel.
|
||||
///
|
||||
/// `applied` is read as `Res` (immutable borrow) and re-written via
|
||||
/// `commands.insert_resource` (a queued command applied later in the frame),
|
||||
/// so the read/write do not conflict in Bevy's system parameter borrow checker.
|
||||
#[allow(clippy::too_many_arguments, clippy::type_complexity)]
|
||||
fn rebuild_bloom(
|
||||
params: Res<crate::params::BlackHoleParams>,
|
||||
applied: Res<AppliedBloomQuality>,
|
||||
mut commands: Commands,
|
||||
bloom_entities: Query<Entity, Or<(
|
||||
With<BrightPassCamera>, With<BlurCamera>,
|
||||
With<BrightPassQuad>, With<BlurQuad>,
|
||||
)>>,
|
||||
bloom_targets: Query<Entity, Or<(
|
||||
With<BloomTarget0>, With<BloomTarget1>,
|
||||
With<BloomTarget2>, With<BloomFinalTarget>,
|
||||
)>>,
|
||||
mut composite_materials: ResMut<Assets<crate::render::material::CompositeMaterial>>,
|
||||
offscreen_target: Query<&OffscreenTarget>,
|
||||
window: Query<&Window>,
|
||||
mut images: ResMut<Assets<Image>>,
|
||||
mut bp_materials: ResMut<Assets<crate::render::material::BrightPassMaterial>>,
|
||||
mut blur_materials: ResMut<Assets<crate::render::material::BlurMaterial>>,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
) {
|
||||
if params.bloom_quality == applied.0 {
|
||||
return;
|
||||
}
|
||||
// Despawn all bloom entities (cameras + quads).
|
||||
for e in bloom_entities.iter() {
|
||||
commands.entity(e).despawn();
|
||||
}
|
||||
// Despawn all bloom target markers (they are spawned as bare entities).
|
||||
for e in bloom_targets.iter() {
|
||||
commands.entity(e).despawn();
|
||||
}
|
||||
// Re-spawn at the new quality (or not at all, if Off).
|
||||
let Ok(win) = window.single() else { return; };
|
||||
let scale = params.render_scale.clamp(MIN_RENDER_SCALE, 1.0);
|
||||
let w = ((win.width() * scale) as u32).max(1);
|
||||
let h = ((win.height() * scale) as u32).max(1);
|
||||
let Ok(offscreen_t) = offscreen_target.single() else { return; };
|
||||
let offscreen = offscreen_t.0.clone();
|
||||
let new_bloom = spawn_bloom_pipeline(
|
||||
&mut commands,
|
||||
&mut images,
|
||||
&mut bp_materials,
|
||||
&mut blur_materials,
|
||||
&mut meshes,
|
||||
&offscreen,
|
||||
w,
|
||||
h,
|
||||
);
|
||||
// Update the composite material's bloom handle. When the new quality is
|
||||
// Off, spawn_bloom_pipeline returns None and we fall back to a 1x1 black
|
||||
// texture so the composite samples black (scene-only ACES).
|
||||
let bloom_handle = new_bloom.unwrap_or_else(|| {
|
||||
images.add(Image::new_fill(
|
||||
bevy::render::render_resource::Extent3d { width: 1, height: 1, depth_or_array_layers: 1 },
|
||||
bevy::render::render_resource::TextureDimension::D2,
|
||||
&[0, 0, 0, 0, 0, 0, 0, 0],
|
||||
TextureFormat::Rgba16Float,
|
||||
bevy::asset::RenderAssetUsages::default(),
|
||||
))
|
||||
});
|
||||
for (_, mat) in composite_materials.iter_mut() {
|
||||
mat.bloom = bloom_handle.clone();
|
||||
}
|
||||
commands.insert_resource(AppliedBloomQuality(params.bloom_quality));
|
||||
}
|
||||
|
||||
fn mirror_params(
|
||||
camera: Res<crate::camera::OrbitCamera>,
|
||||
params: Res<crate::params::BlackHoleParams>,
|
||||
|
||||
21
src/ui.rs
21
src/ui.rs
@ -52,6 +52,27 @@ pub fn ui_system(
|
||||
ui.checkbox(&mut params.grid_enabled, "Enabled");
|
||||
ui.add_enabled(params.grid_enabled, egui::Slider::new(&mut params.grid_density, 0.1..=4.0).text("Density"));
|
||||
});
|
||||
egui::CollapsingHeader::new("Quality")
|
||||
.default_open(true)
|
||||
.show(ui, |ui| {
|
||||
use crate::params::BloomQuality;
|
||||
let mut q = params.bloom_quality;
|
||||
egui::ComboBox::from_label("Bloom quality")
|
||||
.selected_text(format!("{:?}", q))
|
||||
.show_ui(ui, |ui| {
|
||||
ui.selectable_value(&mut q, BloomQuality::Off, "Off");
|
||||
ui.selectable_value(&mut q, BloomQuality::Low, "Low");
|
||||
ui.selectable_value(&mut q, BloomQuality::Medium, "Medium");
|
||||
ui.selectable_value(&mut q, BloomQuality::High, "High");
|
||||
});
|
||||
params.bloom_quality = q;
|
||||
ui.add_enabled(q != BloomQuality::Off, egui::Slider::new(&mut params.bloom_threshold, 0.0..=3.0).text("Bloom threshold"));
|
||||
ui.add_enabled(q != BloomQuality::Off, egui::Slider::new(&mut params.bloom_strength, 0.0..=2.0).text("Bloom strength"));
|
||||
ui.add(egui::Slider::new(&mut params.exposure, 0.5..=3.0).text("Exposure"));
|
||||
ui.add(egui::Slider::new(&mut params.render_scale, 0.25..=1.0).text("Resolution scale"));
|
||||
ui.checkbox(&mut params.star_aa, "Anti-aliased stars");
|
||||
ui.label("MSAA is decorative on a fullscreen shader (no geometry edges to sample).");
|
||||
});
|
||||
});
|
||||
// egui captures pointer when the cursor is over a window or being interacted with.
|
||||
wants.0 = ctx.egui_wants_pointer_input();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user