1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-01 06:10:06 -04:00

[user texture] change uv to normalized texture coords in 0-1 range

This commit is contained in:
Emil Ernerfeldt
2020-09-09 17:14:42 +02:00
parent db2afadc76
commit 13060d495b
5 changed files with 39 additions and 54 deletions

View File

@@ -23,37 +23,40 @@ impl Texture {
let rect = ui.allocate_space(size);
let top_left = Vertex {
pos: rect.min,
uv: (0, 0),
uv: pos2(0.0, 0.0),
color: WHITE,
};
let bottom_right = Vertex {
pos: rect.max,
uv: (self.width as u16 - 1, self.height as u16 - 1),
uv: pos2(1.0, 1.0),
color: WHITE,
};
let mut triangles = Triangles::default();
triangles.add_rect(top_left, bottom_right);
ui.painter().add(PaintCmd::Triangles(triangles));
let tex_w = self.width as f32;
let tex_h = self.height as f32;
if ui.hovered(rect) {
show_tooltip(ui.ctx(), |ui| {
let pos = ui.top_left();
let pos = ui.input().mouse.pos.unwrap_or_else(|| ui.top_left());
let zoom_rect = ui.allocate_space(vec2(128.0, 128.0));
let u = remap_clamp(pos.x, rect.range_x(), 0.0..=self.width as f32 - 1.0).round();
let v = remap_clamp(pos.y, rect.range_y(), 0.0..=self.height as f32 - 1.0).round();
let u = remap_clamp(pos.x, rect.range_x(), 0.0..=tex_w);
let v = remap_clamp(pos.y, rect.range_y(), 0.0..=tex_h);
let texel_radius = 32.0;
let u = u.max(texel_radius);
let v = v.max(texel_radius);
let u = u.max(texel_radius).min(tex_w - texel_radius);
let v = v.max(texel_radius).min(tex_h - texel_radius);
let top_left = Vertex {
pos: zoom_rect.min,
uv: ((u - texel_radius) as u16, (v - texel_radius) as u16),
uv: pos2((u - texel_radius) / tex_w, (v - texel_radius) / tex_h),
color: WHITE,
};
let bottom_right = Vertex {
pos: zoom_rect.max,
uv: ((u + texel_radius) as u16, (v + texel_radius) as u16),
uv: pos2((u + texel_radius) / tex_w, (v + texel_radius) / tex_h),
color: WHITE,
};
let mut triangles = Triangles::default();

View File

@@ -14,7 +14,7 @@ pub struct Pos2 {
// implicit w = 1
}
pub fn pos2(x: f32, y: f32) -> Pos2 {
pub const fn pos2(x: f32, y: f32) -> Pos2 {
Pos2 { x, y }
}
@@ -31,7 +31,7 @@ impl From<&[f32; 2]> for Pos2 {
}
impl Pos2 {
pub fn new(x: f32, y: f32) -> Self {
pub const fn new(x: f32, y: f32) -> Self {
Self { x, y }
}

View File

@@ -18,7 +18,7 @@ use {
/// The default Egui texture has the top-left corner pixel fully white.
/// You need need use a clamping texture sampler for this to work
/// (so it doesn't do bilinear blending with bottom right corner).
pub const WHITE_UV: (u16, u16) = (0, 0);
pub const WHITE_UV: Pos2 = pos2(0.0, 0.0);
/// The vertex type.
///
@@ -30,9 +30,10 @@ pub struct Vertex {
/// (0,0) is the top left corner of the screen.
pub pos: Pos2, // 64 bit
/// Texel coordinates in the texture.
/// Normalized texture coordinates.
/// (0, 0) is the top left corner of the texture.
pub uv: (u16, u16), // 32 bit
/// (1, 1) is the bottom right corner of the texture.
pub uv: Pos2, // 64 bit
/// sRGBA with premultiplied alpha
pub color: Srgba, // 32 bit
@@ -109,12 +110,12 @@ impl Triangles {
let top_right = Vertex {
pos: pos2(bottom_right.pos.x, top_left.pos.y),
uv: (bottom_right.uv.0, top_left.uv.1),
uv: pos2(bottom_right.uv.x, top_left.uv.y),
color: top_left.color,
};
let botom_left = Vertex {
pos: pos2(top_left.pos.x, bottom_right.pos.y),
uv: (top_left.uv.0, bottom_right.uv.1),
uv: pos2(top_left.uv.x, bottom_right.uv.y),
color: top_left.color,
};
self.vertices.push(top_left);
@@ -691,6 +692,9 @@ fn tessellate_paint_command(
out.reserve_triangles(num_chars * 2);
out.reserve_vertices(num_chars * 4);
let tex_w = fonts.texture().width as f32;
let tex_h = fonts.texture().height as f32;
let text_offset = vec2(0.0, 1.0); // Eye-balled for buttons. TODO: why is this needed?
let font = &fonts[text_style];
@@ -701,14 +705,14 @@ fn tessellate_paint_command(
if let Some(glyph) = font.uv_rect(c) {
let mut top_left = Vertex {
pos: pos + glyph.offset + vec2(*x_offset, line.y_min) + text_offset,
uv: glyph.min,
uv: pos2(glyph.min.0 as f32 / tex_w, glyph.min.1 as f32 / tex_h),
color,
};
top_left.pos.x = font.round_to_pixel(top_left.pos.x); // Pixel-perfection.
top_left.pos.y = font.round_to_pixel(top_left.pos.y); // Pixel-perfection.
let bottom_right = Vertex {
pos: top_left.pos + glyph.size,
uv: glyph.max,
uv: pos2(glyph.max.0 as f32 / tex_w, glyph.max.1 as f32 / tex_h),
color,
};
out.add_rect(top_left, bottom_right);