Merge branch 'nametag_fix' into 'master'

Nametag FPS Fix

See merge request veloren/veloren!178

Former-commit-id: c5846595d3c059f374862861fe40c1eb005c4c0f
This commit is contained in:
Forest Anderson
2019-05-26 16:07:07 +00:00
4 changed files with 51 additions and 30 deletions

View File

@ -46,7 +46,7 @@ impl<'a> System<'a> for Sys {
} }
// Move towards the target. // Move towards the target.
let dist = tgt_pos.distance(pos.0); let dist: f32 = Vec2::from(tgt_pos - pos.0).magnitude();
control.move_dir = if dist > 5.0 { control.move_dir = if dist > 5.0 {
Vec2::from(tgt_pos - pos.0).normalized() Vec2::from(tgt_pos - pos.0).normalized()
} else if dist < 1.5 && dist > 0.0 { } else if dist < 1.5 && dist > 0.0 {

View File

@ -348,7 +348,7 @@ impl Hud {
&mut self.ids.health_bars, &mut self.ids.health_bars,
&mut ui_widgets.widget_id_generator(), &mut ui_widgets.widget_id_generator(),
); );
// Healh Bar // Health Bar
Rectangle::fill_with([120.0, 8.0], Color::Rgba(0.3, 0.3, 0.3, 0.5)) Rectangle::fill_with([120.0, 8.0], Color::Rgba(0.3, 0.3, 0.3, 0.5))
.x_y(0.0, -25.0) .x_y(0.0, -25.0)
.position_ingame(pos.0 + Vec3::new(0.0, 0.0, 3.0)) .position_ingame(pos.0 + Vec3::new(0.0, 0.0, 3.0))

View File

@ -46,7 +46,7 @@ impl Pipeline for UiPipeline {
impl From<Vec3<f32>> for Locals { impl From<Vec3<f32>> for Locals {
fn from(pos: Vec3<f32>) -> Self { fn from(pos: Vec3<f32>) -> Self {
Self { Self {
pos: [pos[0], pos[1], pos[2], 1.0], pos: [pos.x, pos.y, pos.z, 1.0],
} }
} }
} }

View File

@ -60,7 +60,7 @@ enum DrawKind {
enum DrawCommand { enum DrawCommand {
Draw { kind: DrawKind, verts: Range<usize> }, Draw { kind: DrawKind, verts: Range<usize> },
Scissor(Aabr<u16>), Scissor(Aabr<u16>),
WorldPos(Option<Consts<UiLocals>>), WorldPos(Option<usize>),
} }
impl DrawCommand { impl DrawCommand {
fn image(verts: Range<usize>) -> DrawCommand { fn image(verts: Range<usize>) -> DrawCommand {
@ -97,6 +97,8 @@ pub struct Ui {
// Consts for default ui drawing position (ie the interface) // Consts for default ui drawing position (ie the interface)
interface_locals: Consts<UiLocals>, interface_locals: Consts<UiLocals>,
default_globals: Consts<Globals>, default_globals: Consts<Globals>,
// Consts to specify positions of ingame elements (e.g. Nametags)
ingame_locals: Vec<Consts<UiLocals>>,
// Window size for updating scaling // Window size for updating scaling
window_resized: Option<Vec2<f64>>, window_resized: Option<Vec2<f64>>,
// Scaling of the ui // Scaling of the ui
@ -118,6 +120,7 @@ impl Ui {
model: renderer.create_dynamic_model(100)?, model: renderer.create_dynamic_model(100)?,
interface_locals: renderer.create_consts(&[UiLocals::default()])?, interface_locals: renderer.create_consts(&[UiLocals::default()])?,
default_globals: renderer.create_consts(&[Globals::default()])?, default_globals: renderer.create_consts(&[Globals::default()])?,
ingame_locals: Vec::new(),
window_resized: None, window_resized: None,
scale, scale,
}) })
@ -229,9 +232,11 @@ impl Ui {
let window_scissor = default_scissor(renderer); let window_scissor = default_scissor(renderer);
let mut current_scissor = window_scissor; let mut current_scissor = window_scissor;
let mut ingame_local_index = 0;
enum Placement { enum Placement {
Interface, Interface,
// Number and resolution // Number of primitives left to render ingame and relative scaling/resolution
InWorld(usize, Option<f32>), InWorld(usize, Option<f32>),
}; };
@ -295,6 +300,7 @@ impl Ui {
} }
match placement { match placement {
// No primitives left to place in the world at the current position, go back to drawing the interface
Placement::InWorld(0, _) => { Placement::InWorld(0, _) => {
placement = Placement::Interface; placement = Placement::Interface;
p_scale_factor = self.scale.scale_factor_physical(); p_scale_factor = self.scale.scale_factor_physical();
@ -307,24 +313,22 @@ impl Ui {
// Push new position command // Push new position command
self.draw_commands.push(DrawCommand::WorldPos(None)); self.draw_commands.push(DrawCommand::WorldPos(None));
} }
Placement::InWorld(n, res) => match kind { // Primitives still left to draw ingame
// Other types don't need to be drawn in the game Placement::InWorld(num_prims, res) => match kind {
// Other types don't aren't drawn & shoudn't decrement the number of primitives left to draw ingame
PrimitiveKind::Other(_) => {} PrimitiveKind::Other(_) => {}
_ => { // Decrement the number of primitives left
placement = Placement::InWorld(n - 1, res); _ => placement = Placement::InWorld(num_prims - 1, res),
if res.is_none() {
continue;
}
}
}, },
Placement::Interface => {} Placement::Interface => {}
} }
// Functions for converting for conrod scalar coords to GL vertex coords (-1.0 to 1.0). // Functions for converting for conrod scalar coords to GL vertex coords (-1.0 to 1.0).
let (ui_win_w, ui_win_h) = if let Placement::InWorld(_, Some(res)) = placement { let (ui_win_w, ui_win_h) = match placement {
(res as f64, res as f64) Placement::InWorld(_, Some(res)) => (res as f64, res as f64),
} else { // Behind the camera or far away
(self.ui.win_w, self.ui.win_h) Placement::InWorld(_, None) => continue,
Placement::Interface => (self.ui.win_w, self.ui.win_h),
}; };
let vx = |x: f64| (x / ui_win_w * 2.0) as f32; let vx = |x: f64| (x / ui_win_w * 2.0) as f32;
let vy = |y: f64| (y / ui_win_h * 2.0) as f32; let vy = |y: f64| (y / ui_win_h * 2.0) as f32;
@ -529,16 +533,6 @@ impl Ui {
.unwrap() .unwrap()
.state .state
.parameters; .parameters;
// Finish current state
self.draw_commands.push(match current_state {
State::Plain => DrawCommand::plain(start..mesh.vertices().len()),
State::Image => DrawCommand::image(start..mesh.vertices().len()),
});
start = mesh.vertices().len();
// Push new position command
self.draw_commands.push(DrawCommand::WorldPos(Some(
renderer.create_consts(&[parameters.pos.into()]).unwrap(),
)));
let pos_in_view = view_mat * Vec4::from_point(parameters.pos); let pos_in_view = view_mat * Vec4::from_point(parameters.pos);
let scale_factor = self.ui.win_w as f64 let scale_factor = self.ui.win_w as f64
@ -547,7 +541,34 @@ impl Ui {
* (0.5 * fov as f64).tan() * (0.5 * fov as f64).tan()
* parameters.res as f64); * parameters.res as f64);
// Don't process ingame elements behind the camera or very far away // Don't process ingame elements behind the camera or very far away
placement = if scale_factor > 0.1 { placement = if scale_factor > 0.2 {
// Finish current state
self.draw_commands.push(match current_state {
State::Plain => {
DrawCommand::plain(start..mesh.vertices().len())
}
State::Image => {
DrawCommand::image(start..mesh.vertices().len())
}
});
start = mesh.vertices().len();
// Push new position command
if self.ingame_locals.len() > ingame_local_index {
renderer
.update_consts(
&mut self.ingame_locals[ingame_local_index],
&[parameters.pos.into()],
)
.unwrap();
} else {
self.ingame_locals.push(
renderer.create_consts(&[parameters.pos.into()]).unwrap(),
);
}
self.draw_commands
.push(DrawCommand::WorldPos(Some(ingame_local_index)));
ingame_local_index += 1;
p_scale_factor = ((scale_factor * 10.0).log2().round().powi(2) p_scale_factor = ((scale_factor * 10.0).log2().round().powi(2)
/ 10.0) / 10.0)
.min(1.6) .min(1.6)
@ -622,8 +643,8 @@ impl Ui {
DrawCommand::Scissor(new_scissor) => { DrawCommand::Scissor(new_scissor) => {
scissor = *new_scissor; scissor = *new_scissor;
} }
DrawCommand::WorldPos(ref pos) => { DrawCommand::WorldPos(index) => {
locals = pos.as_ref().unwrap_or(&self.interface_locals); locals = index.map_or(&self.interface_locals, |i| &self.ingame_locals[i]);
} }
DrawCommand::Draw { kind, verts } => { DrawCommand::Draw { kind, verts } => {
let tex = match kind { let tex = match kind {