From 8d4b151ec9f687325510301e29d80abcf6e3d299 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 9 Oct 2026 00:40:30 +0200 Subject: [PATCH] Add field-of-view effects and the spyglass Camera.tickFov / calculateFov: movement speed (sprint, speed effects), bow draw and spyglass zoom ease in at half a step per tick, with the death and underwater/lava narrowing. The spyglass zooms to 0.1, hides the hand, drops the 8x mouse factor and draws the scope overlay; it stops after 1200 ticks or on release with vanilla's sounds. Inventory icons of display_context item models now use their gui case. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 55 ++++++++- crates/horizon-client/src/game/fov.rs | 107 ++++++++++++++++++ crates/horizon-client/src/game/render_tick.rs | 2 +- 3 files changed, 160 insertions(+), 4 deletions(-) create mode 100644 crates/horizon-client/src/game/fov.rs diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 2cfa8e233..acb0f182d 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -8,6 +8,7 @@ mod creatures; mod enchant_effects; mod end; mod explosion; +mod fov; mod growth; mod machines; mod music; @@ -462,6 +463,12 @@ struct Game<'a> { chest_open: HashMap<(i32, i32, i32), (f32, f32)>, /// Item being used (eaten): hotbar slot and ticks used. using: Option<(usize, u32)>, + /// Camera.oldFovModifier / fovModifier. + fov_modifier: [f32; 2], + /// Hud.scopeScale (spyglass overlay size). + scope_scale: f32, + /// Duration of the last frame in ticks (GUI easing). + frame_ticks: f32, title: Option, /// Armor slots: head, chest, legs, feet. armor: [Option<ItemStack>; 4], @@ -1421,6 +1428,10 @@ impl Game<'_> { if self.use_potion_item(&stack)? || self.throw_item(&stack)? { return Ok(()); } + if &*stack.item == "minecraft:spyglass" { + self.use_spyglass(); + return Ok(()); + } if &*stack.item == "minecraft:bow" { if self.using.is_none() { self.using = Some((self.selected, 0)); @@ -1527,6 +1538,9 @@ impl Game<'_> { if &*stack.item == "minecraft:bow" && slot == self.selected { self.release_bow(ticks); } + if &*stack.item == "minecraft:spyglass" { + self.stop_spyglass(); + } self.using = None; return; } @@ -1541,6 +1555,16 @@ impl Game<'_> { self.using = Some((slot, ticks + 1)); return; } + if &*stack.item == "minecraft:spyglass" { + // SpyglassItem.USE_DURATION, then finishUsingItem. + if ticks + 1 >= 1200 { + self.stop_spyglass(); + self.using = None; + } else { + self.using = Some((slot, ticks + 1)); + } + return; + } let duration = 32; let ticks = ticks + 1; let remaining = duration - ticks.min(duration); @@ -2058,6 +2082,20 @@ impl Game<'_> { if o.get("type").and_then(|t| t.as_str()) == Some("minecraft:model") { return o.get("model").and_then(|m| m.as_str()).map(str::to_owned); } + // A display_context select: the inventory shows the `gui` case. + if o.get("property").and_then(|t| t.as_str()) + == Some("minecraft:display_context") + && let Some(case) = + o.get("cases").and_then(|c| c.as_array()).and_then(|cases| { + cases.iter().find(|c| match &c["when"] { + serde_json::Value::String(w) => w == "gui", + serde_json::Value::Array(ws) => ws.iter().any(|w| w == "gui"), + _ => false, + }) + }) + { + return first_model(&case["model"]); + } for key in [ "model", "fallback", "on_false", "on_true", "cases", "entries", "tints", ] { @@ -2282,6 +2320,7 @@ impl Game<'_> { batch.text(atlas, text, (w - tw) / 2.0, h / 2.0 - 4.0, u32::MAX, true); return batch; } + self.draw_spyglass(&mut batch); { let atlas = &self.res.atlas; // Crosshair. @@ -5985,7 +6024,7 @@ impl Game<'_> { } else { Vec::new() }; - let hand = if title || self.screen == Screen::Inventory { + let hand = if title || self.screen == Screen::Inventory || self.scoping() { Vec::new() } else { self.hand_geometry(alpha) @@ -6021,7 +6060,11 @@ impl Game<'_> { camera, yaw, pitch, - fov_degrees: if title { 85.0 } else { self.settings.fov }, + fov_degrees: if title { + 85.0 + } else { + self.fov_degrees(alpha as f32, camera.to_array()) + }, bob: (self.settings.view_bobbing && !title && self.player.is_some()).then(|| { let a = alpha as f32; ( @@ -6517,6 +6560,7 @@ impl ApplicationHandler for Game<'_> { WindowEvent::RedrawRequested => { let now = Instant::now(); let elapsed = now.duration_since(self.previous); + self.frame_ticks = elapsed.as_secs_f32() * 20.0; self.previous = now; // Arrow keys turn the camera (accessibility / scripted testing). let turn = |k| f32::from(u8::from(self.keys.contains(&k))); @@ -6535,6 +6579,7 @@ impl ApplicationHandler for Game<'_> { if self.title.is_none() { self.tick()?; self.tick_ambience(); + self.tick_fov(); } self.tick_music(); } @@ -6592,7 +6637,8 @@ impl ApplicationHandler for Game<'_> { { // MouseHandler.turnPlayer at default sensitivity 0.5: 0.15 degrees per count. let s = f64::from(self.settings.sensitivity) * 0.6 + 0.2; - let f = s * s * s * 8.0; + // Scoping drops MouseHandler's 8x factor. + let f = s * s * s * if self.scoping() { 1.0 } else { 8.0 }; self.yaw += (x * f * 0.15) as f32; self.pitch = (self.pitch + (y * f * 0.15) as f32).clamp(-90.0, 90.0); } @@ -6819,6 +6865,9 @@ pub fn run() -> Result<()> { last_hurt_by_player: HashSet::new(), chest_open: HashMap::new(), using: None, + fov_modifier: [1.0, 1.0], + scope_scale: 0.5, + frame_ticks: 0.0, title: None, armor: [None, None, None, None], applied_modifiers: Vec::new(), diff --git a/crates/horizon-client/src/game/fov.rs b/crates/horizon-client/src/game/fov.rs new file mode 100644 index 000000000..080ad41ff --- /dev/null +++ b/crates/horizon-client/src/game/fov.rs @@ -0,0 +1,107 @@ +//! Field of view (`Camera.tickFov`, `calculateFov`, +//! `AbstractClientPlayer.getFieldOfViewModifier`) and the spyglass +//! (`SpyglassItem`, `Hud.extractSpyglassOverlay`). +use super::Game; +use horizon_client::{ + fog::{FogType, fog_type}, + hud::{HudBatch, rgba}, +}; + +/// `Abilities.walkingSpeed`. +const WALKING_SPEED: f32 = 0.1; +/// `options.fovEffectScale` (vanilla default 1). +const FOV_EFFECT_SCALE: f32 = 1.0; + +impl Game<'_> { + /// `Player.isScoping`: using a spyglass. + pub(super) fn scoping(&self) -> bool { + self.using + .and_then(|(slot, _)| self.inventory[slot].as_ref()) + .is_some_and(|s| &*s.item == "minecraft:spyglass") + } + /// `AbstractClientPlayer.getFieldOfViewModifier` (first person). + fn fov_target(&self) -> f32 { + let Some(p) = &self.player else { return 1.0 }; + let mut modifier = 1.0f32; + // Movement speed (sprinting, speed and slowness) relative to walking. + modifier *= (p.speed / WALKING_SPEED + 1.0) / 2.0; + if let Some((slot, ticks)) = self.using + && let Some(stack) = &self.inventory[slot] + { + match &*stack.item { + "minecraft:bow" => { + let scale = (ticks as f32 / 20.0).min(1.0); + modifier *= 1.0 - scale * scale * 0.15; + } + "minecraft:spyglass" => return 0.1, + _ => {} + } + } + 1.0 + (modifier - 1.0) * FOV_EFFECT_SCALE + } + /// `Camera.tickFov`. + pub(super) fn tick_fov(&mut self) { + let target = self.fov_target(); + let [_, current] = self.fov_modifier; + let next = (current + (target - current) * 0.5).clamp(0.1, 1.5); + self.fov_modifier = [current, next]; + } + /// `Camera.calculateFov` with `modifyFovBasedOnDeathOrFluid`. + pub(super) fn fov_degrees(&self, partial: f32, camera: [f64; 3]) -> f32 { + let [old, new] = self.fov_modifier; + let mut fov = self.settings.fov * (old + (new - old) * partial); + if let Some(p) = &self.player + && p.health.dead_or_dying() + { + let ticks = self + .ui + .died_at + .map_or(0.0, |t| t.elapsed().as_secs_f32() * 20.0); + let duration = (ticks + partial).min(20.0); + fov /= (1.0 - 500.0 / (duration + 500.0)) * 2.0 + 1.0; + } + if matches!( + fog_type(&self.world, &self.res.facts, camera, self.forward()), + FogType::Water | FogType::Lava + ) { + fov *= 1.0 + (0.857_142_87 - 1.0) * FOV_EFFECT_SCALE; + } + fov + } + /// `SpyglassItem.use`: start scoping. + pub(super) fn use_spyglass(&mut self) { + if self.using.is_none() { + self.using = Some((self.selected, 0)); + let at = self.eye_position(); + self.audio.play("minecraft:item.spyglass.use", at, 1.0, 1.0); + } + } + /// `SpyglassItem.releaseUsing` / `finishUsingItem` (after 1200 ticks). + pub(super) fn stop_spyglass(&mut self) { + let at = self.eye_position(); + self.audio + .play("minecraft:item.spyglass.stop_using", at, 1.0, 1.0); + } + /// `Hud.extractCameraOverlays`: the scope, its size easing from 0.5 to + /// 1.125 at half a step per tick of frame time. + pub(super) fn draw_spyglass(&mut self, batch: &mut HudBatch) { + if !self.scoping() { + self.scope_scale = 0.5; + return; + } + self.scope_scale += (1.125 - self.scope_scale) * (0.5 * self.frame_ticks).min(1.0); + let atlas = &self.res.atlas; + let (w, h) = (batch.width, batch.height); + let src = w.min(h); + let ratio = (w / src).min(h / src) * self.scope_scale; + let (sw, sh) = ((src * ratio).floor(), (src * ratio).floor()); + let (left, top) = (((w - sw) / 2.0).floor(), ((h - sh) / 2.0).floor()); + let (right, bottom) = (left + sw, top + sh); + batch.sprite_scaled(atlas, "misc/spyglass_scope", left, top, sw, sh, u32::MAX); + let black = rgba(0, 0, 0, 255); + batch.fill(atlas, 0.0, bottom, w, h - bottom, black); + batch.fill(atlas, 0.0, 0.0, w, top, black); + batch.fill(atlas, 0.0, top, left, bottom - top, black); + batch.fill(atlas, right, top, w - right, bottom - top, black); + } +} diff --git a/crates/horizon-client/src/game/render_tick.rs b/crates/horizon-client/src/game/render_tick.rs index f3d2d26f2..26da78dfa 100644 --- a/crates/horizon-client/src/game/render_tick.rs +++ b/crates/horizon-client/src/game/render_tick.rs @@ -43,7 +43,7 @@ fn rains_at(world: &World, biomes: &Biomes, x: i32, y: i32, z: i32) -> bool { } impl Game<'_> { - fn forward(&self) -> [f64; 3] { + pub(super) fn forward(&self) -> [f64; 3] { let (yaw, pitch) = ( f64::from(self.yaw).to_radians(), f64::from(self.pitch).to_radians(),