Skip to content

Commit

Permalink
Add support for outlined text.
Browse files Browse the repository at this point in the history
  • Loading branch information
aweinstock314 committed Feb 21, 2023
1 parent c907aeb commit d688ed7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 2 additions & 0 deletions examples/hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ fn main() -> Result<(), Box<dyn Error>> {
bounds: (size.width as f32, size.height as f32),
text: vec![Text::new("Hello wgpu_glyph!")
.with_color([0.0, 0.0, 0.0, 1.0])
.with_outline_color([1.0, 1.0, 1.0, 1.0])
.with_scale(40.0)],
..Section::default()
});
Expand All @@ -143,6 +144,7 @@ fn main() -> Result<(), Box<dyn Error>> {
bounds: (size.width as f32, size.height as f32),
text: vec![Text::new("Hello wgpu_glyph!")
.with_color([1.0, 1.0, 1.0, 1.0])
.with_outline_color([0.0, 0.0, 0.0, 1.0])
.with_scale(40.0)],
..Section::default()
});
Expand Down
3 changes: 3 additions & 0 deletions src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ fn build<D>(
2 => Float32x2,
3 => Float32x2,
4 => Float32x4,
5 => Float32x4,
],
}],
},
Expand Down Expand Up @@ -448,6 +449,7 @@ pub struct Instance {
tex_left_top: [f32; 2],
tex_right_bottom: [f32; 2],
color: [f32; 4],
outline_color: [f32; 4],
}

impl Instance {
Expand Down Expand Up @@ -503,6 +505,7 @@ impl Instance {
tex_left_top: [tex_coords.min.x, tex_coords.max.y],
tex_right_bottom: [tex_coords.max.x, tex_coords.min.y],
color: extra.color,
outline_color: extra.outline_color,
}
}
}
23 changes: 22 additions & 1 deletion src/shader/glyph.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ struct VertexInput {
@location(2) tex_left_top: vec2<f32>,
@location(3) tex_right_bottom: vec2<f32>,
@location(4) color: vec4<f32>,
@location(5) outline_color: vec4<f32>,
}

struct VertexOutput {
@builtin(position) position: vec4<f32>,
@location(0) f_tex_pos: vec2<f32>,
@location(1) f_color: vec4<f32>,
@location(2) f_outline_color: vec4<f32>,
}

@vertex
Expand Down Expand Up @@ -52,6 +54,7 @@ fn vs_main(input: VertexInput) -> VertexOutput {
}

out.f_color = input.color;
out.f_outline_color = input.outline_color;
out.position = globals.transform * vec4<f32>(pos, input.left_top.z, 1.0);

return out;
Expand All @@ -61,9 +64,27 @@ fn vs_main(input: VertexInput) -> VertexOutput {
fn fs_main(input: VertexOutput) -> @location(0) vec4<f32> {
var alpha: f32 = textureSample(font_tex, font_sampler, input.f_tex_pos).r;

var pixel_size: vec2<f32> = (1.0 / vec2<f32>(textureDimensions(font_tex)));

var border = false;
for(var i = -1; i <= 1 && !border; i += 1) {
for(var j = -1; j <= 1 && !border; j += 1) {
if i == 0 && j == 0 {
continue;
}
if textureSample(font_tex, font_sampler, input.f_tex_pos + pixel_size*vec2<f32>(f32(i), f32(j))).r <= 0.0 {
border = true;
}
}
}

if (alpha <= 0.0) {
discard;
}

return input.f_color * vec4<f32>(1.0, 1.0, 1.0, alpha);
if border {
return input.f_outline_color * vec4<f32>(1.0, 1.0, 1.0, alpha);
} else {
return input.f_color * vec4<f32>(1.0, 1.0, 1.0, alpha);
}
}

0 comments on commit d688ed7

Please sign in to comment.