gl_fshader.glsl (2207B)
1 uniform float u_contrast; 2 uniform float u_gamma_adjust; 3 uniform float u_outline_thickness; 4 uniform bool u_outline; 5 uniform float u_boldness; 6 uniform bool u_debug; 7 8 varying vec4 v_glyph; 9 10 11 #define SQRT2_2 0.70710678118654757 /* 1 / sqrt(2.) */ 12 #define SQRT2 1.4142135623730951 13 14 struct glyph_info_t { 15 ivec2 nominal_size; 16 ivec2 atlas_pos; 17 }; 18 19 glyph_info_t 20 glyph_info_decode (vec4 v) 21 { 22 glyph_info_t gi; 23 gi.nominal_size = (ivec2 (mod (v.zw, 256.)) + 2) / 4; 24 gi.atlas_pos = ivec2 (v_glyph.zw) / 256; 25 return gi; 26 } 27 28 29 float 30 antialias (float d) 31 { 32 return smoothstep (-.75, +.75, d); 33 } 34 35 void 36 main() 37 { 38 vec2 p = v_glyph.xy; 39 glyph_info_t gi = glyph_info_decode (v_glyph); 40 41 /* isotropic antialiasing */ 42 vec2 dpdx = dFdx (p); 43 vec2 dpdy = dFdy (p); 44 float m = length (vec2 (length (dpdx), length (dpdy))) * SQRT2_2; 45 46 vec4 color = vec4 (0,0,0,1); 47 48 float gsdist = glyphy_sdf (p, gi.nominal_size GLYPHY_DEMO_EXTRA_ARGS); 49 float sdist = gsdist / m * u_contrast; 50 51 if (!u_debug) { 52 sdist -= u_boldness * 10.; 53 if (u_outline) 54 sdist = abs (sdist) - u_outline_thickness * .5; 55 if (sdist > 1.) 56 discard; 57 float alpha = antialias (-sdist); 58 if (u_gamma_adjust != 1.) 59 alpha = pow (alpha, 1./u_gamma_adjust); 60 color = vec4 (color.rgb,color.a * alpha); 61 } else { 62 color = vec4 (0,0,0,0); 63 64 // Color the inside of the glyph a light red 65 color += vec4 (.5,0,0,.5) * smoothstep (1., -1., sdist); 66 67 float udist = abs (sdist); 68 float gudist = abs (gsdist); 69 // Color the outline red 70 color += vec4 (1,0,0,1) * smoothstep (2., 1., udist); 71 // Color the distance field in green 72 if (!glyphy_isinf (udist)) 73 color += vec4(0,.4,0,.4 - (abs(gsdist) / max(float(gi.nominal_size.x), float(gi.nominal_size.y))) * 4.); 74 75 float pdist = glyphy_point_dist (p, gi.nominal_size GLYPHY_DEMO_EXTRA_ARGS); 76 // Color points green 77 color = mix (vec4 (0,1,0,.5), color, smoothstep (.05, .06, pdist)); 78 79 glyphy_arc_list_t arc_list = glyphy_arc_list (p, gi.nominal_size GLYPHY_DEMO_EXTRA_ARGS); 80 // Color the number of endpoints per cell blue 81 color += vec4 (0,0,1,.1) * float(arc_list.num_endpoints) * 32./255.; 82 } 83 84 gl_FragColor = color; 85 }