Skip to content
Snippets Groups Projects
Commit fedbf024 authored by ickk's avatar ickk
Browse files

fix sdf AA for sub-pixel text rendering

parent f866db42
No related branches found
No related tags found
No related merge requests found
......@@ -70,16 +70,6 @@ fn sample_sdf(coords: vec2<f32>, arr: i32, scale: f32) -> f32 {
return clamp((median3(sample.rgb) - 0.5) * scale + 0.5, 0., 1.);
}
fn sample_subpixel(coords: vec2<f32>, dim: vec2<f32>, arr: i32, scale: f32) -> f32 {
return 0.2 * (
sample_sdf(coords.xy, arr, scale)
+ sample_sdf(coords.xy + vec2(-dim.x, dim.y) / 3., arr, scale)
+ sample_sdf(coords.xy + vec2(-dim.x, -dim.y) / 3., arr, scale)
+ sample_sdf(coords.xy + vec2(dim.x, dim.y) / 3., arr, scale)
+ sample_sdf(coords.xy + vec2(dim.x, -dim.y) / 3., arr, scale)
);
}
@fragment
fn fragment(in: VertexOutput) -> @location(0) vec4<f32> {
if quad_type.t == 0 {
......@@ -96,28 +86,28 @@ fn fragment(in: VertexOutput) -> @location(0) vec4<f32> {
return vec4<f32>(in.color.rgb, rect_dist * in.color.a);
}
if quad_type.t == 1 {
var px_range = 5.;
var px_range = 2.5;
var tex_dimensions = textureDimensions(font_texture);
var msdf_unit = vec2(px_range, px_range) / vec2(f32(tex_dimensions.x), f32(tex_dimensions.y));
let subpixel_width = fwidth(in.uv.x) / 3.;
let scale = dot(msdf_unit, 0.5 / fwidth(in.uv.xy));
let subpixel_dimensions = vec2(fwidth(in.uv.x) / 3., fwidth(in.uv.y));
let red = sample_subpixel(vec2(in.uv.x - subpixel_dimensions.x, 1. - in.uv.y), subpixel_dimensions, i32(in.uv.z), scale);
let green = sample_subpixel(vec2(in.uv.x, 1. - in.uv.y), subpixel_dimensions, i32(in.uv.z), scale);
let blue = sample_subpixel(vec2(in.uv.x + subpixel_dimensions.x, 1. - in.uv.y), subpixel_dimensions, i32(in.uv.z), scale);
let alpha = (red + green + blue) / 3.;
// RGB stripe sub-pixel arrangement
let red = sample_sdf(vec2(in.uv.x - subpixel_width, 1. - in.uv.y), i32(in.uv.z), scale);
let green = sample_sdf(vec2(in.uv.x, 1. - in.uv.y), i32(in.uv.z), scale);
let blue = sample_sdf(vec2(in.uv.x + subpixel_width, 1. - in.uv.y), i32(in.uv.z), scale);
// fudge: this really should be somehow blended per-channel, using alpha here is a nasty hack
let alpha = clamp(0.4 * (red + green + blue), 0., 1.);
return vec4(red * in.color.r, green * in.color.g, blue * in.color.b, alpha);
}
if quad_type.t == 2 {
var px_range = 2.5;
var tex_dimensions = textureDimensions(font_texture);
var msdf_unit = vec2<f32>(px_range, px_range) / vec2<f32>(f32(tex_dimensions.x), f32(tex_dimensions.y));
var x = textureSample(font_texture, font_sampler, vec2<f32>(in.uv.x, 1.0 - in.uv.y), i32(in.uv.z));
var v = max(min(x.r, x.g), min(max(x.r, x.g), x.b));
var sig_dist = (v - 0.5) * dot(msdf_unit, 0.5 / fwidth(vec2<f32>(in.uv.x, 1.0 - in.uv.y)));
var a = clamp(sig_dist + 0.5, 0.0, 1.0);
return vec4<f32>(in.color.rgb, a);
var msdf_unit = vec2(px_range, px_range) / vec2(f32(tex_dimensions.x), f32(tex_dimensions.y));
let scale = dot(msdf_unit, 0.5 / fwidth(in.uv.xy));
let alpha = sample_sdf(vec2(in.uv.x, 1. - in.uv.y), i32(in.uv.z), scale);
return vec4(in.color.rgb, alpha);
}
if quad_type.t == 3 {
var bs = min(in.border_radius, min(in.size.x, in.size.y));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment