From 24a14b083f2542bb2f8ac67f3558a3f5ef0328a3 Mon Sep 17 00:00:00 2001 From: Imbris Date: Mon, 20 May 2019 02:09:20 -0400 Subject: [PATCH] Scaling of ingame text rasterization to match final size Former-commit-id: 9f9e900afef2400af35d579e66ba95097e169b87 --- voxygen/src/hud/mod.rs | 15 ++++-------- voxygen/src/menu/char_selection/ui.rs | 2 +- voxygen/src/menu/main/ui.rs | 2 +- voxygen/src/scene/camera.rs | 5 ++++ voxygen/src/session.rs | 2 ++ voxygen/src/ui/cache.rs | 4 ++-- voxygen/src/ui/mod.rs | 34 +++++++++++++++++++++++---- 7 files changed, 46 insertions(+), 18 deletions(-) diff --git a/voxygen/src/hud/mod.rs b/voxygen/src/hud/mod.rs index 704ee508fa..fcd05f73aa 100644 --- a/voxygen/src/hud/mod.rs +++ b/voxygen/src/hud/mod.rs @@ -24,6 +24,7 @@ use small_window::{SmallWindow, SmallWindowType}; use crate::{ render::{Consts, Globals, Renderer}, + scene::camera::Camera, settings::{ControlSettings, Settings}, ui::{Ingame, Ingameable, ScaleMode, Ui}, window::{Event as WinEvent, Key, Window}, @@ -342,15 +343,6 @@ impl Hud { .set(bar_id, ui_widgets); } } - // test - Text::new("Squarefection") - .font_size(20) - .color(TEXT_COLOR) - .font_id(self.fonts.opensans) - .x_y(0.0, 0.0) - .position_ingame([0.0, 25.0, 25.0].into()) - .resolution(20.0) - .set(self.ids.temp, ui_widgets); // Display debug window. if self.show.debug { @@ -682,12 +674,15 @@ impl Hud { client: &Client, global_state: &mut GlobalState, debug_info: DebugInfo, + camera: &Camera, ) -> Vec { if let Some(maybe_id) = self.to_focus.take() { self.ui.focus_widget(maybe_id); } let events = self.update_layout(client, global_state, debug_info); - self.ui.maintain(&mut global_state.window.renderer_mut()); + let (view_mat, _, _) = camera.compute_dependents(client); + let fov = camera.get_fov(); + self.ui.maintain(&mut global_state.window.renderer_mut(), Some((view_mat, fov))); events } diff --git a/voxygen/src/menu/char_selection/ui.rs b/voxygen/src/menu/char_selection/ui.rs index 2246f01159..007a669f56 100644 --- a/voxygen/src/menu/char_selection/ui.rs +++ b/voxygen/src/menu/char_selection/ui.rs @@ -1081,7 +1081,7 @@ impl CharSelectionUi { pub fn maintain(&mut self, renderer: &mut Renderer) -> Vec { let events = self.update_layout(); - self.ui.maintain(renderer); + self.ui.maintain(renderer, None); events } diff --git a/voxygen/src/menu/main/ui.rs b/voxygen/src/menu/main/ui.rs index 9b62246b8b..2bd084f4b8 100644 --- a/voxygen/src/menu/main/ui.rs +++ b/voxygen/src/menu/main/ui.rs @@ -508,7 +508,7 @@ impl MainMenuUi { pub fn maintain(&mut self, global_state: &mut GlobalState) -> Vec { let events = self.update_layout(global_state); - self.ui.maintain(global_state.window.renderer_mut()); + self.ui.maintain(global_state.window.renderer_mut(), None); events } diff --git a/voxygen/src/scene/camera.rs b/voxygen/src/scene/camera.rs index d23638fdff..ef747ab3df 100644 --- a/voxygen/src/scene/camera.rs +++ b/voxygen/src/scene/camera.rs @@ -150,4 +150,9 @@ impl Camera { pub fn get_orientation(&self) -> Vec3 { self.ori } + + /// Get the field of view of the camera in radians. + pub fn get_fov(&self) -> f32 { + self.fov + } } diff --git a/voxygen/src/session.rs b/voxygen/src/session.rs index 8be80e3703..a74c01fd0f 100644 --- a/voxygen/src/session.rs +++ b/voxygen/src/session.rs @@ -182,6 +182,7 @@ impl PlayState for SessionState { tps: clock.get_tps(), ping_ms: self.client.borrow().get_ping_ms(), }, + &self.scene.camera(), ); // Maintain the UI. for event in hud_events { @@ -214,6 +215,7 @@ impl PlayState for SessionState { } } } + {} // Render the session. self.render(global_state.window.renderer_mut()); diff --git a/voxygen/src/ui/cache.rs b/voxygen/src/ui/cache.rs index b111222768..1ddda971fe 100644 --- a/voxygen/src/ui/cache.rs +++ b/voxygen/src/ui/cache.rs @@ -17,8 +17,8 @@ pub struct Cache { impl Cache { pub fn new(renderer: &mut Renderer) -> Result { let (w, h) = renderer.get_resolution().into_tuple(); - const SCALE_TOLERANCE: f32 = 0.1; - const POSITION_TOLERANCE: f32 = 0.1; + const SCALE_TOLERANCE: f32 = 0.3; + const POSITION_TOLERANCE: f32 = 0.5; let graphic_cache_dims = Vec2::new(w * 4, h * 4); Ok(Self { diff --git a/voxygen/src/ui/mod.rs b/voxygen/src/ui/mod.rs index c3d6a42f10..751cf36048 100644 --- a/voxygen/src/ui/mod.rs +++ b/voxygen/src/ui/mod.rs @@ -23,6 +23,7 @@ use crate::{ create_ui_quad, create_ui_tri, Consts, DynamicModel, Globals, Mesh, RenderError, Renderer, UiLocals, UiMode, UiPipeline, }, + scene::camera::Camera, window::Window, Error, }; @@ -205,7 +206,7 @@ impl Ui { self.ui.widget_input(id) } - pub fn maintain(&mut self, renderer: &mut Renderer) { + pub fn maintain(&mut self, renderer: &mut Renderer, mats: Option<(Mat4, f32)>) { // Regenerate draw commands and associated models only if the ui changed let mut primitives = match self.ui.draw_if_changed() { Some(primitives) => primitives, @@ -229,6 +230,7 @@ impl Ui { let mut current_scissor = window_scissor; let mut in_world = None; + // TODO: maybe mutate an ingame scale factor instead of this, depends on if we want them to scale with other ui scaling or not let mut p_scale_factor = self.scale.scale_factor_physical(); // Switches to the `Plain` state and completes the previous `Command` if not already in the @@ -301,8 +303,14 @@ impl Ui { } Some((n, res)) => match kind { // Other types don't need to be drawn in the game - PrimitiveKind::Other(_) => (), - _ => in_world = Some((n - 1, res)), + PrimitiveKind::Other(_) => {} + _ => { + in_world = Some((n - 1, res)); + // Skip + if p_scale_factor < 0.5 { + continue; + } + } }, None => {} } @@ -526,7 +534,25 @@ impl Ui { ))); in_world = Some((parameters.num, parameters.res)); - p_scale_factor = 1.0; + // Calculate the scale factor to pixels at this 3d point using the camera. + p_scale_factor = match mats { + Some((view_mat, fov)) => { + let pos_in_view = view_mat * Vec4::from_point(parameters.pos); + let scale_factor = self.ui.win_w as f64 + / (-2.0 + * pos_in_view.z as f64 + * (0.5 * fov as f64).tan() + * parameters.res as f64); + // Don't draw really small ingame elements or those behind the camera + if scale_factor > 0.1 { + scale_factor.min(2.0).max(0.5) + } else { + // TODO: use a flag or option instead of this + -1.0 + } + } + None => 1.0, + } } } _ => {} // TODO: Add this.