//! Custom names: `NameTagItem.interactLivingEntity` and the name tag //! (`EntityRenderer.submitNameTag`): above the head, facing the camera, //! on a translucent black background, shown when the name is visible or the //! mob is under the crosshair within 64 blocks, as its team's name tag //! visibility allows, in the team's colours, with its score in the //! objective displayed below names. Other players' names (on a LAN host //! or a server) show whenever `LivingEntityRenderer.shouldShowName` allows. use super::{Game, view_vector}; use horizon_client::{ block_entity_render::vertex_light, mesher::Vertex, mobs::Mob, save::uuid_text, scoreboard as sb, text::Text, }; impl Game<'_> { /// A named name tag names the mob under the crosshair. pub(super) fn name_mob(&mut self, index: usize) -> bool { let Some(name) = self .held() .filter(|h| &*h.item == "minecraft:name_tag") .and_then(|h| h.custom_name().map(str::to_owned)) else { return false; }; let m = &mut self.mobs[index]; if !m.alive() { return false; } m.custom_name = Some(name); m.persistent = true; // itemStack.shrink(1), in creative too. self.consume_held(); self.swing_arm(); true } /// `Entity.belowNameDisplay`: the score holder's score in the objective /// shown below names, then a space and the objective's name. fn below_name_text(&self, holder: &str) -> Option { let board = &self.score.board; let objective = board.display_objective(sb::SLOT_BELOW_NAME)?; let score = board.score(holder, &objective.name)?; // numberFormatOrDefault(StyledFormat.NO_STYLE). let format = score .number_format .clone() .or_else(|| objective.number_format.clone()) .unwrap_or(sb::NumberFormat::Styled(serde_json::json!({}))); let mut t = Text::empty(); t.push(format.format(score.value)); t.push(Text::literal(" ")); t.push(objective.display_text()); Some(t) } /// The other players' models and names: the guests on a LAN host, the /// players a server sends on a client. fn other_players(&self) -> Vec<(&Mob, String)> { let mut out = Vec::new(); if let Some(host) = &self.lan { for (conn, m) in &host.models { if let Some(g) = host.hub.guest(*conn) { out.push((m, g.name.clone())); } } } for (id, name) in self.remote_player_names() { if let Some(m) = self.mobs.iter().find(|m| m.id == id) { out.push((m, name)); } } out } /// The names the name tags show this frame (the control socket's status). pub(super) fn shown_name_tags(&self, camera: glam::DVec3) -> Vec { self.name_tags(camera, 1.0) .into_iter() .filter_map(|t| t.lines.last().map(|(t, _)| t.plain())) .collect() } /// Each name tag this frame. fn name_tags(&self, camera: glam::DVec3, alpha: f64) -> Vec { let target = self.mob_hit().map(|(i, _)| self.mobs[i].id); let viewer = self.player_team(); let mut tags = Vec::new(); let players = self.other_players(); let named = self .mobs .iter() .filter_map(|m| Some((m, m.custom_name.clone()?, false))); let players = players.into_iter().map(|(m, name)| (m, name, true)); for (m, name, player) in named.chain(players) { if !m.alive() { continue; } // EntityAttachment.NAME_TAG: 0.5 above the (crouching) height. let height = if player && m.extra.crouching { 1.5 } else { f64::from(m.height()) }; let p = glam::DVec3::new( m.previous.x + (m.position.x - m.previous.x) * alpha, m.previous.y + (m.position.y - m.previous.y) * alpha + height + 0.5, m.previous.z + (m.position.z - m.previous.z) * alpha, ); // The name tag distance (64) is measured to the entity. let distance_sqr = glam::DVec3::new(m.position.x, m.position.y, m.position.z).distance_squared(camera); if distance_sqr >= 64.0 * 64.0 { continue; } // A player's scores and team go by name, a mob's by UUID. let holder = if player { Some(name.clone()) } else { self.persist.uuids.get(&m.id).map(|u| uuid_text(*u)) }; let team = if player { self.score.board.players_team(&name) } else { self.mob_team(m.id) }; // LivingEntityRenderer.shouldShowName (team rules included), then // MobRenderer: a visible name, or the mob under the crosshair. let invisible = sb::invisible_to( team, viewer, self.spectator(), m.invisible || m.has_effect("minecraft:invisibility"), ); // Entity.isDiscrete: a crouching player. let discrete = player && m.extra.crouching; let view = sb::NameView { discrete, distance_sqr, invisible_to_viewer: invisible, hud_hidden: self.ui.hide_gui, is_camera: false, is_vehicle: self.mobs.iter().any(|o| o.extra.mount == Some(m.id)), }; // MobRenderer.shouldShowName: a visible name, or the mob under the // crosshair; players (LivingEntityRenderer) need neither. if !sb::name_shown(team, viewer, view) || !(player || m.name_visible || Some(m.id) == target) { continue; } // getDisplayName: the name in the team's prefix, suffix and colour. let member = if player { Text::literal(&name) } else { m.custom_name_text .clone() .unwrap_or_else(|| Text::literal(&name)) }; let shown = match team { Some(t) => t.formatted_name(member), None => member, }; let mut lines = Vec::new(); // EntityRenderer.extractNameTags: the score shows within the // below-name distance (10), under the name. let mut lift = 0.0; if distance_sqr < 10.0 * 10.0 && let Some(score) = holder.and_then(|h| self.below_name_text(&h)) { lines.push((score, 0.0)); lift = f64::from(9.0f32 * 1.15 * 0.025); } lines.push((shown, lift)); let l = self.world.light( m.position.x.floor() as i32, (m.position.y + height).floor() as i32, m.position.z.floor() as i32, ); tags.push(NameTag { at: p, lines, light: l, discrete, }); } // Armor stands name themselves: an invisible one with a visible name // is a hologram (its model is skipped, its name tag is not). for a in &self.armor_stands { let Some(name) = &a.custom_name else { continue; }; if !a.name_visible { continue; } let height = match (a.marker, a.small) { (true, _) => 0.0, (false, true) => 0.9875, (false, false) => 1.975, }; let distance_sqr = glam::DVec3::new(a.position.x, a.position.y, a.position.z).distance_squared(camera); if distance_sqr >= 64.0 * 64.0 { continue; } let team = self.mob_team(a.id); let invisible = sb::invisible_to(team, viewer, self.spectator(), false); let view = sb::NameView { discrete: false, distance_sqr, invisible_to_viewer: invisible, hud_hidden: self.ui.hide_gui, is_camera: false, is_vehicle: false, }; if !sb::name_shown(team, viewer, view) { continue; } let shown = match team { Some(t) => t.formatted_name(name.clone()), None => name.clone(), }; let mut lines = Vec::new(); let mut lift = 0.0; let holder = self.persist.uuids.get(&a.id).map(|u| uuid_text(*u)); if distance_sqr < 10.0 * 10.0 && let Some(score) = holder.and_then(|h| self.below_name_text(&h)) { lines.push((score, 0.0)); lift = f64::from(9.0f32 * 1.15 * 0.025); } lines.push((shown, lift)); let l = self.world.light( a.position.x.floor() as i32, (a.position.y + height).floor() as i32, a.position.z.floor() as i32, ); tags.push(NameTag { at: glam::DVec3::new(a.position.x, a.position.y + height + 0.5, a.position.z), lines, light: l, discrete: false, }); } tags } /// `SubmitNodeCollection.submitNameTag`: a discrete name is drawn once, /// faded, on its background; any other in full over the world, its faded /// copy and background in `see_through`, over everything. pub(super) fn name_tag_geometry( &self, camera: glam::DVec3, alpha: f64, out: &mut Vec, see_through: &mut Vec, ) { // The text alpha that goes with getBackgroundOpacity(0.25): // max((0.25 + 0.75) / 2, 0.5), as ARGB.color (the floor of 255 times it). const FADED: u32 = 0x7F; for tag in self.name_tags(camera, alpha) { let o = tag.at - camera; if tag.discrete { self.name_tag_quads(o, &tag.lines, FADED, true, 0, vertex_light(tag.light), out); continue; } // LightCoordsUtil.lightCoordsWithEmission(lightCoords, 2). let lit = (tag.light >> 4).max(2) << 4 | (tag.light & 15).max(2); self.name_tag_quads(o, &tag.lines, 0xFF, false, 0, vertex_light(lit), out); // RenderPipelines.TEXT_SEE_THROUGH: no depth test, no lightmap. self.name_tag_quads(o, &tag.lines, FADED, true, 2, 0, see_through); } } /// The lines of one name tag, `o` from the camera: the text at /// `text_alpha` in its layer, on the translucent black background /// (unlit, a hair behind) when `background`. #[allow(clippy::too_many_arguments)] fn name_tag_quads( &self, o: glam::DVec3, lines: &[(Text, f64)], text_alpha: u32, background: bool, text_layer: u32, light: u32, out: &mut Vec, ) { // Options.getBackgroundOpacity(0.25) black, as ARGB.color. const BACKGROUND: u32 = 0x3F00_0000; let forward = glam::DVec3::from_array(view_vector(self.yaw, self.pitch)); let right = forward.cross(glam::DVec3::Y).normalize_or_zero(); let up = right.cross(forward).normalize_or_zero(); let white = self.gui.atlas.font.white(); let away = forward.as_vec3() * 0.002; for (text, lift) in lines { let o = (o + glam::DVec3::new(0.0, *lift, 0.0)).as_vec3(); let (r, u) = (right.as_vec3() * 0.025, up.as_vec3() * 0.025); // Text space: x right, y down (scale 0.025, -0.025). let at = |x: f32, y: f32| o + r * x - u * y; let chars: Vec<(char, u32)> = text .spans .iter() .flat_map(|s| { let rgb = s.style.color.unwrap_or(0xFF_FF_FF); // RGBA8, alpha in the top byte. let color = text_alpha << 24 | ((rgb & 0xFF) << 16) | (rgb & 0xFF00) | ((rgb >> 16) & 0xFF); s.text.chars().map(move |c| (c, color)) }) .collect(); let width: f32 = chars .iter() .map(|(c, _)| self.gui.atlas.glyph(*c).map_or(6.0, |g| g.1)) .sum(); let x0 = -(width / 2.0).trunc(); let y0 = if text.plain() == "deadmau5" { -10.0 } else { 0.0 }; let mut quad = |a: [f32; 4], uv: [f32; 4], color: u32, behind: bool, layer: u32| { let corners = [(a[0], a[3]), (a[2], a[3]), (a[2], a[1]), (a[0], a[1])]; let uvs = [ [uv[0], uv[3]], [uv[2], uv[3]], [uv[2], uv[1]], [uv[0], uv[1]], ]; for ((x, y), uv) in corners.into_iter().zip(uvs) { out.push(Vertex { normal: [0; 4], pos: (at(x, y) + if behind { away } else { glam::Vec3::ZERO }).to_array(), uv, layer, color, light, }); } }; if background { quad( [x0 - 1.0, y0 - 1.0, x0 + width, y0 + 8.0], [white.u0, white.v0, white.u1, white.v1], BACKGROUND, true, 2, ); } let mut x = x0; for (ch, color) in chars { let Some((g, advance)) = self.gui.atlas.glyph(ch) else { x += 6.0; continue; }; if ch != ' ' { quad( [x, y0, x + 8.0, y0 + 8.0], [g.u0, g.v0, g.u1, g.v1], color, false, text_layer, ); } x += advance; } } } } /// A name tag: where it hangs, its lines (text, lift), the packed light at /// the entity and whether it is discrete. struct NameTag { at: glam::DVec3, lines: Vec<(Text, f64)>, light: u8, discrete: bool, }