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