Level loading screen like vanilla: panorama, chunk map and progress bar

LevelLoadingScreen while the player waits for its chunks: the title's
panorama, blur and menu background when a world opens (Reason.OTHER),
the nether portal sprite after a nether portal, Loading terrain... above
the 17x17 chunk status map (ChunkLoadStatusView, radius 8, COLORS) and
the 200x2 progress bar easing towards LevelLoadProgressTracker's value.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-16 03:05:21 +02:00
parent d333780478
commit 5373998ee0
5 changed files with 245 additions and 20 deletions
+78 -19
View File
@@ -792,8 +792,8 @@ struct Game<'a> {
book: Option<books::BookScreen>,
/// Lecterns whose page-turn pulse is running (ticks left).
lectern_pulses: Vec<((i32, i32, i32), i32)>,
/// Steps of the world spawn's chunk spiral taken so far.
spawn_search: u32,
/// LevelLoadingScreen.smoothedProgress while the player is not placed yet.
loading_progress: f32,
/// The world spawn search's origin chunk and fallback height, once known.
spawn_origin: Option<((i32, i32), i32)>,
spawn_spiral: horizon_client::world_spawn::ChunkSpiral,
@@ -2946,6 +2946,14 @@ impl Game<'_> {
if self.remote.is_some() {
return self.tick_remote();
}
// LevelLoadingScreen.tick.
self.loading_progress = match self.loading_reason() {
Some(_) => horizon_client::level_loading::smooth(
self.loading_progress,
self.level_load_progress(),
),
None => 0.0,
};
self.ticks += 1;
// `/tick freeze` stops the world but not the player.
let running = self.clock.world_runs();
@@ -3654,16 +3662,8 @@ impl Game<'_> {
if self.remote_overlay(&mut batch) {
return batch;
}
if self.player.is_none() {
let text = if self.spawn_search > 0 {
"Finding a place to spawn..."
} else {
"Loading terrain..."
};
let atlas = &self.res.atlas;
let tw = HudBatch::text_width(atlas, text);
batch.fill(atlas, 0.0, 0.0, w, h, rgba(20, 20, 20, 255));
batch.text(atlas, text, (w - tw) / 2.0, h / 2.0 - 4.0, u32::MAX, true);
if let Some(reason) = self.loading_reason() {
self.draw_level_loading(&mut batch, reason);
return batch;
}
// Hud.extractRenderState: F1 (keyToggleGui) hides camera overlays, the
@@ -6325,7 +6325,6 @@ impl Game<'_> {
yaw: *yaw,
forced: *forced,
});
self.spawn_search = 0;
self.spawn_origin = None;
self.spawn_spiral = Default::default();
self.player_spawn = None;
@@ -8476,7 +8475,9 @@ impl Game<'_> {
let mut hud = horizon_client::spans::time("hud", || self.hud(size));
// Map textures changed since the last drawn frame (game/maps.rs).
hud.patches.append(&mut self.map_patches);
let title = self.title.is_some();
// LevelLoadingScreen (Reason.OTHER) draws the title's panorama.
let title = self.title.is_some()
|| self.loading_reason() == Some(horizon_client::level_loading::Reason::Other);
let (yaw, pitch) = if live {
(view_yaw, view_pitch)
} else if title {
@@ -8660,15 +8661,73 @@ impl Game<'_> {
self.entity_vertices = entities;
result
}
/// The LevelLoadingScreen shown while the player waits for its chunks:
/// opening a world, or after a portal or a respawn into another dimension.
fn loading_reason(&self) -> Option<horizon_client::level_loading::Reason> {
use horizon_client::level_loading::Reason;
if self.title.is_some() || self.remote.is_some() || self.player.is_some() {
return None;
}
Some(
if self.travelling.is_none() || self.respawn_travel.is_some() {
Reason::Other
} else if self.end_travel {
Reason::EndPortal
} else {
Reason::NetherPortal
},
)
}
/// `LevelLoadTracker.serverProgress`: the player's 7x7 chunks loaded.
fn level_load_progress(&self) -> f32 {
let (cx, cz) = self.streaming.center;
let ready = (-3..=3)
.flat_map(|dz| (-3..=3).map(move |dx| (cx + dx, cz + dz)))
.filter(|&(x, z)| self.streaming.is_lit(x, z))
.count();
horizon_client::level_loading::progress(ready)
}
/// `LevelLoadingScreen.extractBackground` and `extractRenderState`.
fn draw_level_loading(
&self,
batch: &mut HudBatch,
reason: horizon_client::level_loading::Reason,
) {
use horizon_client::level_loading::{self, Reason, STATUS_RADIUS};
let atlas = &self.res.atlas;
let (w, h) = (batch.width, batch.height);
match reason {
// The panorama and its blur are drawn by the renderer.
Reason::Other => {
horizon_client::ui::menu_background(batch, atlas, 0.0, 0.0, w, h, false)
}
Reason::NetherPortal => {
let overlay = hud_overlays::portal_overlay(w as i32, h as i32, 1.0);
hud_layout::paint(batch, atlas, &[overlay]);
}
Reason::EndPortal => batch.fill(atlas, 0.0, 0.0, w, h, rgba(20, 20, 20, 255)),
}
let (cx, cz) = self.streaming.center;
let status = |x: i32, z: i32| {
self.streaming.latest_status(
&self.world,
cx + x - STATUS_RADIUS,
cz + z - STATUS_RADIUS,
)
};
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.
fn menu_blur(&self) -> Option<f32> {
let loading = self.loading_reason() == Some(horizon_client::level_loading::Reason::Other);
let blurred = !horizon_client::ui_theme::horizon()
&& self.title.as_ref().is_some_and(|t| {
self.screen == Screen::Options
|| !matches!(t.mode, horizon_client::title::Mode::Main)
});
&& (loading
|| self.title.as_ref().is_some_and(|t| {
self.screen == Screen::Options
|| !matches!(t.mode, horizon_client::title::Mode::Main)
}));
let radius = self.settings.menu_blur as f32;
(blurred && radius >= 1.0).then_some(radius)
}
@@ -9961,7 +10020,7 @@ pub fn run() -> Result<()> {
sign_edit: None,
book: None,
lectern_pulses: Vec::new(),
spawn_search: 0,
loading_progress: 0.0,
spawn_origin: None,
spawn_spiral: Default::default(),
player_spawn: None,
@@ -107,7 +107,6 @@ impl Game<'_> {
None => *self.spawn_origin.insert(self.spawn_origin()?),
};
while let Some((dx, dz)) = self.spawn_spiral.current() {
self.spawn_search = self.spawn_spiral.steps() as u32;
if self.spawn_spiral.tested() {
let (cx, cz) = (ox + dx, oz + dz);
if !self.streaming.is_lit(cx, cz) {
+142
View File
@@ -0,0 +1,142 @@
//! LevelLoadingScreen: "Loading terrain..." over the chunk status map and
//! the server's load progress while a world opens or a portal is crossed.
use crate::{
hud::{HudAtlas, HudBatch},
text::Text,
};
/// `Minecraft.doWorldLoad`: `max(5, 3) + ChunkLevel.RADIUS_AROUND_FULL_CHUNK + 1`.
pub const STATUS_RADIUS: i32 = 8;
/// `LevelLoadProgressTracker.EXPECTED_PLAYER_CHUNKS`: the 7x7 chunks
/// `PrepareSpawnTask` waits for around the player.
const PLAYER_CHUNKS: usize = 49;
/// `LevelLoadProgressTracker.PREPARE_SERVER_WEIGHT`.
const PREPARE_WEIGHT: usize = 10;
/// `ChunkStatus`, as far as the chunk status map tells them apart.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Status {
Empty,
StructureStarts,
StructureReferences,
Biomes,
Terrain,
Features,
InitializeLight,
Light,
Spawn,
Full,
}
impl Status {
/// `LevelLoadingScreen.COLORS` (RGB).
pub fn color(self) -> u32 {
match self {
Self::Empty => 0x545454,
Self::StructureStarts => 0x999999,
Self::StructureReferences => 0x5F6191,
Self::Biomes => 0x80B252,
Self::Terrain => 0x303572,
Self::Features => 0x21C600,
Self::InitializeLight => 0xCCCCCC,
Self::Light => 0xFFE0A0,
Self::Spawn => 0xF26060,
Self::Full => 0xFFFFFF,
}
}
}
/// `LevelLoadingScreen.Reason`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Reason {
NetherPortal,
EndPortal,
Other,
}
/// `LevelLoadProgressTracker.get` once the server is prepared, with `ready`
/// of the player's 49 chunks loaded (no forced chunks to load first).
pub fn progress(ready: usize) -> f32 {
let total = PREPARE_WEIGHT + PLAYER_CHUNKS;
(PREPARE_WEIGHT + ready.min(PLAYER_CHUNKS)) as f32 / total as f32
}
/// `LevelLoadingScreen.tick`: the bar eases a fifth of the way each tick.
pub fn smooth(smoothed: f32, progress: f32) -> f32 {
smoothed + (progress - smoothed) * 0.2
}
/// `ARGB.opaque` as the batch's RGBA.
fn opaque(rgb: u32) -> u32 {
0xFF00_0000 | (rgb & 0xFF) << 16 | (rgb & 0xFF00) | (rgb >> 16 & 0xFF)
}
/// `LevelLoadingScreen.extractRenderState` over its background: the text,
/// the progress bar and the chunk map, `status(x, z)` for x and z in
/// `0..=2 * STATUS_RADIUS` from the map's north-west corner.
pub fn draw(
batch: &mut HudBatch,
atlas: &HudAtlas,
status: Option<&dyn Fn(i32, i32) -> Option<Status>>,
progress: Option<f32>,
) {
let (x_center, y_center) = ((batch.width as i32) / 2, (batch.height as i32) / 2);
let text_top = match status {
Some(status) => {
// extractChunksForRendering(size 2, margin 0).
let diameter = STATUS_RADIUS * 2 + 1;
let start = (x_center - diameter, y_center - diameter);
for x in 0..diameter {
for z in 0..diameter {
let color = opaque(status(x, z).map_or(0, Status::color));
let (cx, cy) = (start.0 + x * 2, start.1 + z * 2);
batch.fill(atlas, cx as f32, cy as f32, 2.0, 2.0, color);
}
}
y_center - STATUS_RADIUS * 2 - 9 * 3
}
None => y_center - 50,
};
let text = Text::translate("multiplayer.downloadingTerrain", &[]);
let tw = atlas.font.width_text(&text) as i32;
batch.text_styled(
atlas,
&text,
(x_center - tw / 2) as f32,
text_top as f32,
u32::MAX,
true,
);
if let Some(progress) = progress {
// drawProgressBar(xCenter - 100, textTop + 12, 200, 2).
let (left, top) = ((x_center - 100) as f32, (text_top + 9 + 3) as f32);
batch.fill(atlas, left, top, 200.0, 2.0, opaque(0));
let done = (progress * 200.0).round();
batch.fill(atlas, left, top, done, 2.0, opaque(0x00FF00));
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn progress_weighs_the_prepared_server_and_the_player_chunks() {
assert_eq!(progress(0), 10.0 / 59.0);
assert_eq!(progress(49), 1.0);
assert_eq!(progress(100), 1.0);
let eased = smooth(0.0, 1.0);
assert_eq!(eased, 0.2);
assert!((smooth(eased, 1.0) - 0.36).abs() < 1e-6);
}
#[test]
fn colours_are_opaque_rgba() {
assert_eq!(opaque(Status::Full.color()), u32::MAX);
assert_eq!(opaque(0), 0xFF00_0000);
assert_eq!(opaque(0x00FF00), crate::hud::rgba(0, 255, 0, 255));
assert_eq!(
opaque(Status::Biomes.color()),
crate::hud::rgba(0x80, 0xB2, 0x52, 255)
);
}
}
+1
View File
@@ -93,6 +93,7 @@ pub mod keymapping;
pub mod lan;
pub mod landing;
pub mod lang;
pub mod level_loading;
pub mod light;
pub mod logging;
pub mod menu;
+24
View File
@@ -619,6 +619,30 @@ impl Streaming {
pub fn is_lit(&self, x: i32, z: i32) -> bool {
self.lit.contains(&(x, z))
}
/// `ChunkMap.getLatestStatus` for the loading screen's chunk map: the
/// furthest step this column has reached, `None` before it is asked for.
pub fn latest_status(
&self,
world: &World,
x: i32,
z: i32,
) -> Option<crate::level_loading::Status> {
use crate::level_loading::Status;
let key = (x, z);
Some(if self.lit.contains(&key) {
Status::Full
} else if world.chunks.contains_key(&key) {
Status::InitializeLight
} else if self.decorated.contains(&key) || self.finishing.contains(&key) {
Status::Features
} else if self.protos.contains_key(&key) {
Status::Terrain
} else if self.generating.contains(&key) {
Status::Empty
} else {
return None;
})
}
pub fn is_meshing(&self, section: (i32, i32, i32)) -> bool {
self.meshing.contains_key(&section)
}