From e7d42dc9d077e93f1546732c803a427ab0ea3043 Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Thu, 22 Oct 2026 17:41:17 +0100 Subject: [PATCH] Fix in-world menu background blur --- crates/horizon-client/src/game.rs | 13 +++-- crates/horizon-client/src/game/screens.rs | 47 ++++++++++++++++- crates/horizon-client/src/gfx.rs | 19 +++++-- crates/horizon-client/src/hud.rs | 62 ++++++++++++++++++++++- 4 files changed, 130 insertions(+), 11 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index f706e9348..be7010c61 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -4838,6 +4838,9 @@ impl Game<'_> { self.draw_chat(&mut batch); self.draw_tab_list(&mut batch); } + // Screen.extractBlurredBackground blurs the world and HUD, while the + // screen's background, widgets, toasts and debug overlay stay sharp. + batch.blur_at = batch.vertices.len() as u32; match self.screen { Screen::Paused if self.pause_menu => self.draw_pause_menu(&mut batch), // PauseScreen(false): only its title, at the top. @@ -10007,9 +10010,8 @@ impl Game<'_> { }; level_loading::draw(batch, atlas, Some(&status), Some(self.loading_progress)); } - /// `Screen.extractBlurredBackground` over the panorama: every classic - /// screen of the title menus but the title itself (`TitleScreen` - /// doesn't blur), when `menuBackgroundBlurriness` is at least 1. + /// `Screen.extractBlurredBackground` over the panorama or the world and + /// HUD, when `menuBackgroundBlurriness` is at least 1. fn menu_blur(&self) -> Option { let loading = self.loading_reason() == Some(horizon_client::level_loading::Reason::Other); let blurred = !horizon_client::ui_theme::horizon() @@ -10017,7 +10019,10 @@ impl Game<'_> { || self.title.as_ref().is_some_and(|t| { self.screen == Screen::Options || !matches!(t.mode, horizon_client::title::Mode::Main) - })); + }) + || (self.title.is_none() + && self.player.is_some() + && self.screen.blurs_background(self.pause_menu))); let radius = self.settings.menu_blur as f32; (blurred && radius >= 1.0).then_some(radius) } diff --git a/crates/horizon-client/src/game/screens.rs b/crates/horizon-client/src/game/screens.rs index cecd8d46f..07eacfb49 100644 --- a/crates/horizon-client/src/game/screens.rs +++ b/crates/horizon-client/src/game/screens.rs @@ -7,6 +7,24 @@ use horizon_client::{ ui, }; +impl Screen { + /// Screen.extractBackground for non-isInGameUi screens, and + /// PauseScreen.extractBackground only when showPauseMenu is true. + pub(super) fn blurs_background(self, pause_menu: bool) -> bool { + match self { + Self::Paused => pause_menu, + Self::Options | Self::Stats | Self::Advancements | Self::WorldOptions => true, + Self::Playing + | Self::GameModeSwitcher + | Self::Chat + | Self::Creative + | Self::Inventory + | Self::Dead + | Self::CommandBlock => false, + } + } +} + /// PauseScreen buttons: (x, y, width, label key, action id, active). /// https://mcsrc.dev/2/26.3/net/minecraft/client/gui/screens/PauseScreen fn pause_buttons(w: f32, h: f32, remote: bool) -> Vec<(f32, f32, f32, &'static str, u8, bool)> { @@ -334,7 +352,34 @@ impl Game<'_> { #[cfg(test)] mod tests { - use super::pause_buttons; + use super::{Screen, pause_buttons}; + + #[test] + fn menu_blur_matches_vanilla_screen_backgrounds() { + for screen in [ + Screen::Options, + Screen::Stats, + Screen::Advancements, + Screen::WorldOptions, + ] { + assert!(screen.blurs_background(false)); + assert!(screen.blurs_background(true)); + } + assert!(Screen::Paused.blurs_background(true)); + assert!(!Screen::Paused.blurs_background(false)); + for screen in [ + Screen::Playing, + Screen::GameModeSwitcher, + Screen::Chat, + Screen::Creative, + Screen::Inventory, + Screen::Dead, + Screen::CommandBlock, + ] { + assert!(!screen.blurs_background(true)); + assert!(!screen.blurs_background(false)); + } + } #[test] fn pause_menu_uses_disconnect_for_servers_and_keeps_world_options() { diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 14c040c5c..3de07b17b 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -3841,9 +3841,19 @@ impl Renderer { let first = debug_depth + debug_top; pass.draw(0..4, first..first + crosshair); } - if let Some(radius) = frame.menu_blur - && self.config.usage.contains(wgpu::TextureUsages::COPY_SRC) - { + let menu_blur = frame + .menu_blur + .filter(|_| self.config.usage.contains(wgpu::TextureUsages::COPY_SRC)); + let blur_at = menu_blur.map_or(0, |_| frame.hud.blur_at); + self.hud.draw_range( + &mut encoder, + &view_target, + &self.depth, + frame.hud, + [self.config.width, self.config.height], + 0..blur_at, + ); + if let Some(radius) = menu_blur { let format = self.config.format; let device = &self.device; self.menu_blur @@ -3875,12 +3885,13 @@ impl Renderer { grade, ); } - self.hud.draw( + self.hud.draw_range( &mut encoder, &view_target, &self.depth, frame.hud, [self.config.width, self.config.height], + blur_at..frame.hud.vertices.len() as u32, ); if culling && let Some(cull) = &mut self.cull { cull.end(&self.device, &mut encoder); diff --git a/crates/horizon-client/src/hud.rs b/crates/horizon-client/src/hud.rs index 0cc99b07b..0c3bde1d1 100644 --- a/crates/horizon-client/src/hud.rs +++ b/crates/horizon-client/src/hud.rs @@ -363,6 +363,8 @@ pub struct HudBatch { pub patches: Vec, /// Vertex ranges with another blend or depth/scissor state, in order. pub blends: Vec<(std::ops::Range, Blend)>, + /// Screen's first vertex: the preceding HUD is part of its blurred background. + pub blur_at: u32, } /// How a run of HUD vertices blends and tests depth. #[derive(Clone, Copy, PartialEq, Eq, Debug)] @@ -398,6 +400,7 @@ impl HudBatch { height: (window[1] as f32 / scale as f32).ceil(), patches: Vec::new(), blends: Vec::new(), + blur_at: 0, } } /// Draw the vertices added since `start` with the inverting blend. @@ -450,6 +453,14 @@ impl HudBatch { } out } + fn runs_in(&self, vertices: std::ops::Range) -> Vec<(std::ops::Range, Blend)> { + let mut runs = self.runs(); + for (range, _) in &mut runs { + *range = range.start.max(vertices.start)..range.end.min(vertices.end); + } + runs.retain(|(range, _)| !range.is_empty()); + runs + } /// `AbstractContainerScreen.init`: center in integer GUI coordinates. pub fn centered_origin(&self, width: f32, height: f32) -> (f32, f32) { ( @@ -1078,7 +1089,26 @@ impl HudRenderer { batch: &HudBatch, size: [u32; 2], ) { - if batch.vertices.is_empty() || size.contains(&0) { + self.draw_range( + encoder, + target, + depth, + batch, + size, + 0..batch.vertices.len() as u32, + ); + } + /// Draw part of the prepared batch, preserving its blend/depth runs. + pub fn draw_range( + &self, + encoder: &mut wgpu::CommandEncoder, + target: &wgpu::TextureView, + depth: &wgpu::TextureView, + batch: &HudBatch, + size: [u32; 2], + vertices: std::ops::Range, + ) { + if vertices.is_empty() || size.contains(&0) { return; } let attachments = [Some(wgpu::RenderPassColorAttachment { @@ -1090,7 +1120,7 @@ impl HudRenderer { store: wgpu::StoreOp::Store, }, })]; - let runs = batch.runs(); + let runs = batch.runs_in(vertices); let mut at = 0; while at < runs.len() { let clip = match runs[at].1 { @@ -1329,6 +1359,33 @@ mod tests { ] ); } + + #[test] + fn blur_boundary_splits_blend_runs_without_losing_or_repeating_vertices() { + let mut batch = HudBatch::new([854, 480]); + for _ in 0..5 { + batch.quad([[0.0; 2]; 4], [[0.0; 2]; 4], 0); + } + batch.blends = vec![ + (6..18, Blend::Invert), + (18..24, Blend::Depth([0, 0, 10, 10])), + ]; + for split in [0, 6, 12, 18, 24, 30] { + let before = batch.runs_in(0..split); + let after = batch.runs_in(split..30); + let actual: Vec<_> = before + .into_iter() + .chain(after) + .flat_map(|(range, blend)| range.map(move |v| (v, blend))) + .collect(); + let expected: Vec<_> = batch + .runs() + .into_iter() + .flat_map(|(range, blend)| range.map(move |v| (v, blend))) + .collect(); + assert_eq!(actual, expected, "blur at {split}"); + } + } #[test] fn container_origins_use_java_integer_division() { // Odd scaled dimensions, odd image sizes and an oversized panel. @@ -1340,6 +1397,7 @@ mod tests { height: height as f32, patches: Vec::new(), blends: Vec::new(), + blur_at: 0, }; for (image_width, image_height) in [(195, 136), (176, 166), (176, 222)] { assert_eq!(