diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 659e9c93d..aa35a6429 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -36,6 +36,7 @@ mod options; mod packs; mod paintings; mod persist; +mod perspective; mod piglins; mod postprocess; mod potions; @@ -437,6 +438,7 @@ pub struct Resources { context: Arc, threads: usize, player_arm: Option, + player_type: Option>, /// firstperson_righthand display transforms per item: (rotation, translation, scale). displays: HashMap>, /// Enchantments, workstation recipes and brewing (game/stations.rs). @@ -625,6 +627,9 @@ struct Game<'a> { adv: advancements::AdvState, stats_ui: stats_screen::StatsUi, adv_ui: advancements_screen::AdvUi, + camera_type: perspective::CameraType, + /// The local player as a model (third person, the inventory). + player_model: Option, } #[derive(Clone, Copy, Debug, PartialEq, Eq)] enum Dim { @@ -701,8 +706,10 @@ impl Game<'_> { /// each block's outline shape (`BlockGetter.clip` with `ClipContext.Block.OUTLINE`). fn pick(&self) -> Option { let origin = self.eye_position()?; - let dir = view_vector(self.yaw, self.pitch); - let reach = self.reach(); + self.clip_ray(origin, view_vector(self.yaw, self.pitch), self.reach()) + } + /// The first block outline hit within `reach` along the unit `dir`. + fn clip_ray(&self, origin: [f64; 3], dir: [f64; 3], reach: f64) -> Option { let mut pos = origin.map(|v| v.floor() as i32); let step = dir.map(|d| if d > 0.0 { 1 } else { -1 }); let delta = dir.map(|d| { @@ -2755,13 +2762,15 @@ impl Game<'_> { self.draw_spyglass(&mut batch); { let atlas = &self.res.atlas; - // Crosshair. - batch.sprite( - atlas, - "gui/sprites/hud/crosshair", - (w - 15.0) / 2.0, - (h - 15.0) / 2.0, - ); + // Crosshair (first person only). + if self.camera_type.first_person() { + batch.sprite( + atlas, + "gui/sprites/hud/crosshair", + (w - 15.0) / 2.0, + (h - 15.0) / 2.0, + ); + } } // Gui: spectators get the (closed) spectator menu instead of the // hotbar and no player health. @@ -3274,6 +3283,9 @@ impl Game<'_> { } } } + if self.container.is_none() && self.craft_size != 3 { + self.draw_inventory_player(batch, ox, oy); + } for i in self.slots() { let (sx, sy) = self.slot_position(i); if i == 504 && self.offhand.is_none() { @@ -3695,6 +3707,7 @@ impl Game<'_> { } // Mobs and their projectiles. self.mob_geometry(camera, alpha, &mut out); + self.player_model_geometry(camera, alpha, &mut out); self.name_tag_geometry(camera, alpha, &mut out); // Arrows: two crossed quads along the flight direction. if let Some(r) = self @@ -4309,6 +4322,7 @@ impl Game<'_> { self.tick_stats(); self.spawn_phantoms(); self.tick_advancements(); + self.tick_player_model(); Ok(()) } /// SkyRenderer.renderSunMoonAndStars: bodies on a frame rotated Y -90 then X by angle. @@ -5734,6 +5748,7 @@ impl Game<'_> { fn hand_geometry(&mut self, alpha: f64) -> Vec { let mut out = Vec::new(); if self.spectator() + || !self.camera_type.first_person() || self .player .as_ref() @@ -6530,7 +6545,7 @@ impl Game<'_> { return Ok(false); }; let alpha = self.clock.fraction(); - let (camera, underwater) = match &self.player { + let (camera, underwater, view_yaw, view_pitch) = match &self.player { Some(p) => { let (a, b) = (self.previous_position, p.position); let eye = self.previous_eye + alpha as f32 * (self.eye - self.previous_eye); @@ -6539,15 +6554,21 @@ impl Game<'_> { a.y + (b.y - a.y) * alpha + f64::from(eye), a.z + (b.z - a.z) * alpha, ); + let (c, yaw, pitch) = self.camera_view(c); let s = self.world .block(c.x.floor() as i32, c.y.floor() as i32, c.z.floor() as i32); - (c, self.res.facts.get(s).fluid == Fluid::Water) + (c, self.res.facts.get(s).fluid == Fluid::Water, yaw, pitch) } - None => (glam::DVec3::new(0.5, 100.0, 0.5), false), + None => ( + glam::DVec3::new(0.5, 100.0, 0.5), + false, + self.yaw, + self.pitch, + ), }; self.audio.listener = [camera.x, camera.y, camera.z]; - self.audio.listener_yaw = self.yaw; + self.audio.listener_yaw = view_yaw; let block = [ camera.x.floor() as i32, camera.y.floor() as i32, @@ -6648,7 +6669,7 @@ impl Game<'_> { let (yaw, pitch) = if title { (-(self.started.elapsed().as_secs_f32() * 2.0) % 360.0, 10.0) } else { - (self.yaw, self.pitch) + (view_yaw, view_pitch) }; let water_fog = { let c = info.water_fog; @@ -6737,6 +6758,7 @@ impl Game<'_> { KeyCode::F1 => self.toggle_hide_gui(), KeyCode::F11 => self.toggle_fullscreen(), KeyCode::F2 => self.take_screenshot(), + KeyCode::F5 if self.screen == Screen::Playing => self.cycle_camera(), KeyCode::KeyE if self.screen == Screen::Creative && self.creative_screen.tab().search => {} KeyCode::KeyE => match self.screen { @@ -7449,6 +7471,7 @@ pub fn run() -> Result<()> { mob_data, context, threads, + player_type: perspective::player_type(&root), player_arm: horizon_client::mobs::load_model(&root, "minecraft:player#main") .and_then(|m| m.children.into_iter().find(|c| c.name == "right_arm")), displays: HashMap::new(), @@ -7617,6 +7640,8 @@ pub fn run() -> Result<()> { adv: Default::default(), stats_ui: Default::default(), adv_ui: Default::default(), + camera_type: Default::default(), + player_model: None, }; game.ui.vanilla_root = vanilla_root; game.res.atlas.font.uniform = game.settings.force_unicode; diff --git a/crates/horizon-client/src/game/perspective.rs b/crates/horizon-client/src/game/perspective.rs new file mode 100644 index 000000000..d56c46584 --- /dev/null +++ b/crates/horizon-client/src/game/perspective.rs @@ -0,0 +1,349 @@ +//! The camera type (`CameraType`: first person, third person behind and +//! in front, cycled by F5), the detached camera (`Camera.setup` with +//! `getMaxZoom`), and the local player drawn as a model: in third person +//! and in the inventory (`InventoryScreen.extractEntityInInventoryFollowsMouse`). +use super::{Game, Screen}; +use horizon_client::{ + hud::HudBatch, + mobs::{Anim, Brain, Mob, MobType}, + pose::Pose, +}; +use horizon_core::geometry::Vec3; +use std::{path::Path, sync::Arc}; + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub(super) enum CameraType { + #[default] + FirstPerson, + ThirdPersonBack, + ThirdPersonFront, +} +impl CameraType { + pub(super) fn first_person(self) -> bool { + self == CameraType::FirstPerson + } + fn cycle(self) -> Self { + match self { + CameraType::FirstPerson => CameraType::ThirdPersonBack, + CameraType::ThirdPersonBack => CameraType::ThirdPersonFront, + CameraType::ThirdPersonFront => CameraType::FirstPerson, + } + } +} +/// The player's model type (`PlayerModel` with the default skin). +pub(super) fn player_type(root: &Path) -> Option> { + horizon_client::mobs::load_model(root, "minecraft:player#main").map(|model| { + Arc::new(MobType { + id: "minecraft:player".into(), + // Players render as a humanoid; the species only drives local AI. + species: horizon_client::mobs::Species::Zombie, + width: 0.6, + height: 1.8, + eye_height: 1.62, + category: "misc".into(), + max_health: 20.0, + speed: 0.1, + flying_speed: 0.02, + attack_damage: 1.0, + attack_knockback: 0.0, + follow_range: 0.0, + step_height: 0.6, + armor: 0.0, + armor_toughness: 0.0, + knockback_resistance: 0.0, + jump_strength: 0.42, + gravity: 0.08, + safe_fall_distance: 3.0, + fall_damage_multiplier: 1.0, + water_efficiency: 0.0, + water_slowdown: 0.8, + air_speed: 0.02, + baby_scale: 0.5, + head_rot_speed: 10.0, + max_head_x_rot: 40.0, + max_head_y_rot: 75.0, + ambient_interval: 80, + flags: Default::default(), + model: Arc::new(model), + layers: Default::default(), + texture: "entity/player/wide/steve".into(), + brain: Brain::Passive, + anim: Anim::Humanoid { zombie_arms: false }, + sounds: [ + "entity.player.hurt", + "entity.player.hurt", + "entity.player.death", + ], + step_sound: None, + food: Arc::from([]), + tempt: Arc::from([]), + }) + }) +} +fn wrap(d: f32) -> f32 { + let d = d % 360.0; + if d >= 180.0 { + d - 360.0 + } else if d < -180.0 { + d + 360.0 + } else { + d + } +} +impl Game<'_> { + /// `key.togglePerspective`. + pub(super) fn cycle_camera(&mut self) { + self.camera_type = self.camera_type.cycle(); + } + /// `Camera.setup`: the eye, or a point up to four blocks behind (or in + /// front of) it, pulled in where blocks are in the way. Returns the + /// position, yaw and pitch to render from. + pub(super) fn camera_view(&self, eye: glam::DVec3) -> (glam::DVec3, f32, f32) { + let (mut yaw, mut pitch) = (self.yaw, self.pitch); + if self.camera_type.first_person() || self.player.is_none() || self.sleeping.is_some() { + return (eye, yaw, pitch); + } + if self.camera_type == CameraType::ThirdPersonFront { + yaw += 180.0; + pitch = -pitch; + } + let f = super::view_vector(yaw, pitch); + let mut dist = 4.0f64; + for i in 0..8 { + let o = [ + f64::from((i & 1) * 2 - 1) * 0.1, + f64::from((i >> 1 & 1) * 2 - 1) * 0.1, + f64::from((i >> 2 & 1) * 2 - 1) * 0.1, + ]; + let from = [eye.x + o[0], eye.y + o[1], eye.z + o[2]]; + let back = [-f[0], -f[1], -f[2]]; + if let Some(hit) = self.clip_ray(from, back, dist) { + let d = glam::DVec3::from_array(hit.point) - eye; + dist = dist.min(d.length()); + } + } + (eye - glam::DVec3::from_array(f) * dist, yaw, pitch) + } + /// Keep the player's model in step with the player (`LivingEntity.tick` + /// body turning, `WalkAnimationState`, the swing and the pose). + pub(super) fn tick_player_model(&mut self) { + let Some(p) = self.player.as_ref() else { + self.player_model = None; + return; + }; + if self.player_model.is_none() { + let Some(kind) = self.res.player_type.clone() else { + return; + }; + self.player_model = Some(Mob::new(0, kind, p.position, self.yaw)); + } + let crouching = p.pose == Pose::Crouching; + let (position, flying) = (p.position, p.flying || p.fall_flying); + let attack = self.swing_anim; + let swinging = self.swing.is_some(); + let (yaw, pitch) = (self.yaw, self.pitch); + let main = self.inventory[self.selected] + .as_ref() + .map(|s| s.item.clone()); + let off = self.offhand.as_ref().map(|s| s.item.clone()); + let riding = self.riding.is_some(); + let hurt = p.health.hurt.hurt_time; + let Some(m) = self.player_model.as_mut() else { + return; + }; + let at = Vec3::new( + position.x, + position.y - if crouching { 0.125 } else { 0.0 }, + position.z, + ); + let (dx, dy, dz) = ( + at.x - m.position.x, + at.y - m.position.y, + at.z - m.position.z, + ); + m.previous = m.position; + m.position = at; + if dx * dx + dy * dy + dz * dz > 100.0 { + m.previous = at; + } + m.previous_yaw = m.yaw; + m.previous_body_yaw = m.body_yaw; + m.previous_head_yaw = m.head_yaw; + m.previous_head_pitch = m.head_pitch; + m.yaw = yaw; + m.head_yaw = yaw; + m.head_pitch = pitch; + let mut target = m.body_yaw; + if dx * dx + dz * dz > 0.002_500_000_2 { + target = (dz.atan2(dx) as f32).to_degrees() - 90.0; + } + if swinging { + target = yaw; + } + // tickHeadTurn: ease toward the target, within 50 degrees of the head. + m.body_yaw += wrap(target - m.body_yaw) * 0.3; + let diff = wrap(yaw - m.body_yaw); + if diff.abs() > 50.0 { + m.body_yaw += diff - diff.signum() * 50.0; + } + // WalkAnimationState.update(min(distance * 4, 1), 0.4). + let dist = (dx * dx + dz * dz + if flying { dy * dy } else { 0.0 }).sqrt() as f32; + m.previous_limb_speed = m.limb_speed; + m.limb_speed += ((dist * 4.0).min(1.0) - m.limb_speed) * 0.4; + m.limb_position += m.limb_speed; + m.age = m.age.wrapping_add(1); + m.anim.attack_anim_o = attack.0; + m.anim.attack_anim = attack.1; + m.extra.main_hand = main; + m.extra.off_hand = off; + m.extra.crouching = crouching; + m.extra.passenger = riding; + m.hurt_time = hurt; + } + /// Whether the player's own model shows in the world view. + fn player_model_shown(&self) -> bool { + !self.camera_type.first_person() + && self.sleeping.is_none() + && !self.spectator() + && self.potions.effects.get("minecraft:invisibility").is_none() + && self + .player + .as_ref() + .is_some_and(|p| !p.health.dead_or_dying()) + } + /// The player's model for the entity pass in third person. + pub(super) fn player_model_geometry( + &self, + camera: glam::DVec3, + alpha: f64, + out: &mut Vec, + ) { + if !self.player_model_shown() { + return; + } + let Some(m) = self.player_model.as_ref() else { + return; + }; + let light = self.world.light( + m.position.x.floor() as i32, + (m.position.y + 1.62).floor() as i32, + m.position.z.floor() as i32, + ); + let packed = (u32::from(light >> 4) * 16) | ((u32::from(light & 15) * 16) << 8); + let atlas = &self.res.atlas; + let lookup = |name: &str| atlas.sprites.get(name).map(|r| [r.u0, r.v0, r.u1, r.v1]); + horizon_client::mobs::render::render( + m, + alpha as f32, + camera, + &lookup, + packed, + &self.res.mob_data, + out, + ); + } + /// `extractEntityInInventoryFollowsMouse`: the player in the box + /// `[x0, x1) x [y0, y1)`, `size` pixels per block, turned toward the + /// cursor; drawn as depth-sorted, back-face-culled quads. + #[allow(clippy::too_many_arguments)] + pub(super) fn draw_player_preview( + &self, + batch: &mut HudBatch, + x0: f32, + y0: f32, + x1: f32, + y1: f32, + size: f32, + offset_y: f32, + ) { + let Some(m) = self.player_model.as_ref() else { + return; + }; + if self + .player + .as_ref() + .is_some_and(|p| p.health.dead_or_dying()) + { + return; + } + let (cx, cy) = ((x0 + x1) / 2.0, (y0 + y1) / 2.0); + let x_angle = ((cx - self.cursor.0) / 40.0).atan(); + let y_angle = ((cy - self.cursor.1) / 40.0).atan(); + let mut m = m.clone(); + // The render state: still, facing out, the head toward the cursor. + m.previous = m.position; + m.body_yaw = 180.0 + x_angle * 20.0; + m.previous_body_yaw = m.body_yaw; + m.head_yaw = m.body_yaw + x_angle * 20.0; + m.previous_head_yaw = m.head_yaw; + m.head_pitch = -y_angle * 20.0; + m.previous_head_pitch = m.head_pitch; + m.position.y += 0.125 * f64::from(u8::from(m.extra.crouching)); + m.previous = m.position; + let camera = glam::DVec3::new(m.position.x, m.position.y, m.position.z); + let atlas = &self.res.atlas; + let lookup = |name: &str| atlas.sprites.get(name).map(|r| [r.u0, r.v0, r.u1, r.v1]); + let mut verts = Vec::new(); + horizon_client::mobs::render::render( + &m, + 1.0, + camera, + &lookup, + 240 | 240 << 8, + &self.res.mob_data, + &mut verts, + ); + let height = if m.extra.crouching { 1.5 } else { 1.8 }; + // PictureInPictureRenderer: the box centre, (size, size, -size); + // then the translation and Rz(pi) * Rx(y_angle * 20 degrees). + let gui = glam::Mat4::from_translation(glam::Vec3::new(cx, cy, 0.0)) + * glam::Mat4::from_scale(glam::Vec3::new(size, size, -size)) + * glam::Mat4::from_translation(glam::Vec3::new(0.0, height / 2.0 + offset_y, 0.0)) + * glam::Mat4::from_rotation_z(std::f32::consts::PI) + * glam::Mat4::from_rotation_x((y_angle * 20.0).to_radians()); + let mut quads: Vec<(f32, [[f32; 2]; 4], [[f32; 2]; 4], u32)> = Vec::new(); + for q in verts.as_chunks::<4>().0 { + let p: [glam::Vec3; 4] = + std::array::from_fn(|i| gui.transform_point3(glam::Vec3::from_array(q[i].pos))); + let n = (p[1] - p[0]).cross(p[2] - p[0]); + // The winding gives inward normals: faces with n.z >= 0 point away. + if n.z >= 0.0 { + continue; + } + let n = n.normalize_or_zero(); + // Lighting.Entry.ENTITY_IN_UI, roughly: lit from the front and above. + let shade = (0.55 + 0.45 * -n.z - 0.15 * n.y).clamp(0.4, 1.0); + let c = q[0].color; + let ch = |s: u32| ((((c >> s) & 255) as f32 * shade) as u32) << s; + let color = (c & 0xFF00_0000) | ch(0) | ch(8) | ch(16); + let depth = p.iter().map(|v| v.z).sum::() / 4.0; + quads.push(( + depth, + p.map(|v| [v.x, v.y]), + std::array::from_fn(|i| q[i].uv), + color, + )); + } + // Far (low z) first: the viewer is on the +z side. + quads.sort_by(|a, b| a.0.total_cmp(&b.0)); + let start = batch.vertices.len(); + for (_, p, uv, color) in quads { + batch.quad(p, uv, color); + } + batch.clip_since(start, x0, y0, x1, y1); + } + /// The survival inventory's player box (`InventoryScreen`). + pub(super) fn draw_inventory_player(&self, batch: &mut HudBatch, ox: f32, oy: f32) { + if self.screen == Screen::Inventory { + self.draw_player_preview( + batch, + ox + 26.0, + oy + 8.0, + ox + 75.0, + oy + 78.0, + 30.0, + 0.0625, + ); + } + } +} diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 3cc52f459..d0d821410 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -14,7 +14,7 @@ use horizon_client::{ entities::{Experience, ItemEntity}, hud::{HudBatch, rgba}, items::ItemStack, - mobs::{Anim, Brain, Mob, MobType}, + mobs::{Mob, MobType}, motion::{MotionWorld, PlayerMotion}, net::{Event, Session}, save::SaveDir, @@ -136,54 +136,7 @@ impl Game<'_> { let lang = Arc::new(horizon_client::net::load_language(&root)); let view = self.settings.render_distance.clamp(2, 32) as i8; let session = Session::connect(address, name, view, lang.clone()); - let player_type = - horizon_client::mobs::load_model(&root, "minecraft:player#main").map(|model| { - Arc::new(MobType { - id: "minecraft:player".into(), - // Players render as a humanoid; the species only drives local AI. - species: horizon_client::mobs::Species::Zombie, - width: 0.6, - height: 1.8, - eye_height: 1.62, - category: "misc".into(), - max_health: 20.0, - speed: 0.1, - flying_speed: 0.02, - attack_damage: 1.0, - attack_knockback: 0.0, - follow_range: 0.0, - step_height: 0.6, - armor: 0.0, - armor_toughness: 0.0, - knockback_resistance: 0.0, - jump_strength: 0.42, - gravity: 0.08, - safe_fall_distance: 3.0, - fall_damage_multiplier: 1.0, - water_efficiency: 0.0, - water_slowdown: 0.8, - air_speed: 0.02, - baby_scale: 0.5, - head_rot_speed: 10.0, - max_head_x_rot: 40.0, - max_head_y_rot: 75.0, - ambient_interval: 80, - flags: Default::default(), - model: Arc::new(model), - layers: Default::default(), - texture: "entity/player/wide/steve".into(), - brain: Brain::Passive, - anim: Anim::Humanoid { zombie_arms: false }, - sounds: [ - "entity.player.hurt", - "entity.player.hurt", - "entity.player.death", - ], - step_sound: None, - food: Arc::from([]), - tempt: Arc::from([]), - }) - }); + let player_type = super::perspective::player_type(&root); let mut streaming = Streaming::new( Arc::new(NoTerrain), self.res.context.clone(), diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index a75f4cd6f..2f79a0a67 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -399,6 +399,9 @@ pub struct Attrs { /// Species-specific persistent state (variants, colours, taming, ageing...). #[derive(Clone, Debug, Default)] pub struct Extra { + /// Players: crouching and riding (HumanoidModel's poses). + pub crouching: bool, + pub passenger: bool, /// AgeableMob.age: negative while a baby (a tadpole counts up to 24000). pub age: i32, /// `AgeLocked` (tadpoles fed a slime ball... and ageable mobs). diff --git a/crates/horizon-client/src/mobs/anim.rs b/crates/horizon-client/src/mobs/anim.rs index fbc655441..77cecee00 100644 --- a/crates/horizon-client/src/mobs/anim.rs +++ b/crates/horizon-client/src/mobs/anim.rs @@ -411,6 +411,27 @@ pub fn setup( None, m.extra.main_hand.is_some(), ); + if m.extra.passenger { + // HumanoidModel.setupAnim: riding. + p.get("right_arm")[3] -= PI / 5.0; + p.get("left_arm")[3] -= PI / 5.0; + for (leg, side) in [("right_leg", 1.0), ("left_leg", -1.0)] { + p.xrot(leg, -1.413_716_7); + p.yrot(leg, side * PI / 10.0); + p.zrot(leg, side * 0.078_539_82); + } + } + if m.extra.crouching { + p.xrot("body", 0.5); + p.get("right_arm")[3] += 0.4; + p.get("left_arm")[3] += 0.4; + p.get("right_leg")[2] += 4.0; + p.get("left_leg")[2] += 4.0; + p.get("head")[1] += 4.2; + p.get("body")[1] += 3.2; + p.get("left_arm")[1] += 3.2; + p.get("right_arm")[1] += 3.2; + } if m.ai.aggressive && !bow { // SkeletonModel melee pose. let sw = s.swing; diff --git a/crates/horizon-client/src/mobs/render.rs b/crates/horizon-client/src/mobs/render.rs index ed74e79f5..9e85b3f1d 100644 --- a/crates/horizon-client/src/mobs/render.rs +++ b/crates/horizon-client/src/mobs/render.rs @@ -102,6 +102,9 @@ pub fn body(m: &Mob) -> (String, String) { s.to_owned() } }; + if &*m.kind.id == "minecraft:player" { + return ("main".into(), m.kind.texture.to_string()); + } match m.species() { s if super::equine::is_equine(s) => { let name = super::equine::model_name(s); @@ -268,6 +271,9 @@ pub fn body_color(m: &Mob, alpha: f32) -> u32 { pub fn layers(m: &Mob) -> Vec { use Species as S; let mut v = Vec::new(); + if &*m.kind.id == "minecraft:player" { + return v; + } let white = 0xFFFF_FFFF; let push = |v: &mut Vec, model: &str, texture: &str, color: u32, full_bright: bool| { v.push(Layer {