Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text2d render quality #1171

Merged
merged 4 commits into from
Jan 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/bevy_sprite/src/render/sprite_sheet.vert
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void main() {
vec2(sprite_rect.end.x, sprite_rect.begin.y),
sprite_rect.end
);
v_Uv = (atlas_positions[gl_VertexIndex] + vec2(0.01, 0.01)) / AtlasSize;
v_Uv = (atlas_positions[gl_VertexIndex]) / AtlasSize;
v_Color = TextureAtlasSprite_color;
gl_Position = ViewProj * SpriteTransform * vec4(ceil(vertex_position), 1.0);
gl_Position = ViewProj * SpriteTransform * vec4(vertex_position, 1.0);
blunted2night marked this conversation as resolved.
Show resolved Hide resolved
}
1 change: 1 addition & 0 deletions crates/bevy_text/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ bevy_reflect = { path = "../bevy_reflect", version = "0.4.0", features = ["bevy"
bevy_render = { path = "../bevy_render", version = "0.4.0" }
bevy_sprite = { path = "../bevy_sprite", version = "0.4.0" }
bevy_transform = { path = "../bevy_transform", version = "0.4.0" }
bevy_window = { path = "../bevy_window", version = "0.4.0" }
bevy_utils = { path = "../bevy_utils", version = "0.4.0" }

# other
Expand Down
16 changes: 12 additions & 4 deletions crates/bevy_text/src/glyph_brush.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,16 @@ impl GlyphBrush {

let mut positioned_glyphs = Vec::new();
for sg in glyphs {
let glyph_id = sg.glyph.id;
if let Some(outlined_glyph) = font.font.outline_glyph(sg.glyph) {
let SectionGlyph {
section_index: _,
byte_index: _,
mut glyph,
font_id: _,
} = sg;
let glyph_id = glyph.id;
let base_x = glyph.position.x.floor();
glyph.position.x = 0.;
if let Some(outlined_glyph) = font.font.outline_glyph(glyph) {
let bounds = outlined_glyph.px_bounds();
let handle_font_atlas: Handle<FontAtlasSet> = handle.as_weak();
let font_atlas_set = font_atlas_set_storage
Expand All @@ -92,10 +100,10 @@ impl GlyphBrush {
let glyph_width = glyph_rect.width();
let glyph_height = glyph_rect.height();

let x = bounds.min.x + glyph_width / 2.0 - min_x;
let x = base_x + bounds.min.x + glyph_width / 2.0 - min_x;
// the 0.5 accounts for odd-numbered heights (bump up by 1 pixel)
// max_y = text block height, and up is negative (whereas for transform, up is positive)
let y = max_y - bounds.max.y + glyph_height / 2.0 + 0.5;
let y = max_y - bounds.max.y + glyph_height / 2.0;
let position = Vec2::new(x, y);

positioned_glyphs.push(PositionedGlyph {
Expand Down
29 changes: 26 additions & 3 deletions crates/bevy_text/src/text2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use bevy_render::{
};
use bevy_sprite::{TextureAtlas, QUAD_HANDLE};
use bevy_transform::prelude::{GlobalTransform, Transform};
use bevy_window::Windows;
use glyph_brush_layout::{HorizontalAlign, VerticalAlign};

use crate::{
Expand Down Expand Up @@ -56,6 +57,7 @@ pub fn draw_text2d_system(
mut context: DrawContext,
msaa: Res<Msaa>,
meshes: Res<Assets<Mesh>>,
windows: Res<Windows>,
mut render_resource_bindings: ResMut<RenderResourceBindings>,
text_pipeline: Res<DefaultTextPipeline>,
mut query: Query<
Expand All @@ -73,6 +75,12 @@ pub fn draw_text2d_system(
let font_quad = meshes.get(&QUAD_HANDLE).unwrap();
let vertex_buffer_descriptor = font_quad.get_vertex_buffer_descriptor();

let scale_factor = if let Some(window) = windows.get_primary() {
window.scale_factor() as f32
} else {
1.
};

for (entity, mut draw, visible, text, global_transform, calculated_size) in query.iter_mut() {
if !visible.is_visible {
continue;
Expand All @@ -99,7 +107,7 @@ pub fn draw_text2d_system(
msaa: &msaa,
text_glyphs: &text_glyphs.glyphs,
font_quad_vertex_descriptor: &vertex_buffer_descriptor,
scale_factor: 1.,
scale_factor,
style: &text.style,
};

Expand All @@ -114,10 +122,12 @@ pub struct QueuedText2d {
}

/// Updates the TextGlyphs with the new computed glyphs from the layout
#[allow(clippy::too_many_arguments)]
pub fn text2d_system(
mut queued_text: Local<QueuedText2d>,
mut textures: ResMut<Assets<Texture>>,
fonts: Res<Assets<Font>>,
windows: Res<Windows>,
mut texture_atlases: ResMut<Assets<TextureAtlas>>,
mut font_atlas_set_storage: ResMut<Assets<FontAtlasSet>>,
mut text_pipeline: ResMut<DefaultTextPipeline>,
Expand All @@ -135,6 +145,12 @@ pub fn text2d_system(
return;
}

let scale_factor = if let Some(window) = windows.get_primary() {
window.scale_factor()
} else {
1.
};

// Computes all text in the local queue
let mut new_queue = Vec::new();
let query = text_queries.q1_mut();
Expand All @@ -145,7 +161,7 @@ pub fn text2d_system(
text.font.clone(),
&fonts,
&text.value,
text.style.font_size,
scale_value(text.style.font_size, scale_factor),
text.style.alignment,
Size::new(f32::MAX, f32::MAX),
&mut *font_atlas_set_storage,
Expand All @@ -163,11 +179,18 @@ pub fn text2d_system(
let text_layout_info = text_pipeline.get_glyphs(&entity).expect(
"Failed to get glyphs from the pipeline that have just been computed",
);
calculated_size.size = text_layout_info.size;
calculated_size.size = Size {
width: scale_value(text_layout_info.size.width, 1. / scale_factor),
height: scale_value(text_layout_info.size.height, 1. / scale_factor),
};
}
}
}
}

queued_text.entities = new_queue;
}

fn scale_value(value: f32, factor: f64) -> f32 {
(value as f64 * factor) as f32
}