1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
# Copyright (c) 2014, Nicolas P. Rougier. All Rights Reserved.
# Distributed under the (new) BSD License.
"""Antialiasing GLSL functions."""
# -----------------------------------------------------------------------------
# Stroke
# -----------------------------------------------------------------------------
"""Compute antialiased fragment color for a stroke line.
Inputs:
- distance (float): Signed distance to border (in pixels).
Template variables:
- linewidth (float): Stroke line width (in pixels).
- antialias (float): Stroke antialiased area (in pixels).
- stroke (vec4): Stroke color.
Outputs:
- color (vec4): The final color.
"""
ANTIALIAS_STROKE = """
vec4 stroke(float distance)
{
vec4 frag_color;
float t = $linewidth/2.0 - $antialias;
float signed_distance = distance;
float border_distance = abs(signed_distance) - t;
float alpha = border_distance/$antialias;
alpha = exp(-alpha*alpha);
if( border_distance < 0.0 )
frag_color = $stroke;
else
frag_color = vec4($stroke.rgb, $stroke.a * alpha);
return frag_color;
}
"""
# -----------------------------------------------------------------------------
# Stroke
# -----------------------------------------------------------------------------
"""Compute antialiased fragment color for an outlined shape.
Inputs:
- distance (float): Signed distance to border (in pixels).
Template variables:
- linewidth (float): Stroke line width (in pixels).
- antialias (float): Stroke antialiased area (in pixels).
- stroke (vec4): Stroke color.
- fill (vec4): Fill color.
Outputs:
- color (vec4): The final color.
"""
ANTIALIAS_OUTLINE = """
vec4 outline(float distance)
{
vec4 frag_color;
float t = $linewidth/2.0 - $antialias;
float signed_distance = distance;
float border_distance = abs(signed_distance) - t;
float alpha = border_distance/$antialias;
alpha = exp(-alpha*alpha);
if( border_distance < 0.0 )
frag_color = $stroke;
else if( signed_distance < 0.0 )
frag_color = mix($fill, $stroke, sqrt(alpha));
else
frag_color = vec4($stroke.rgb, $stroke.a * alpha);
return frag_color;
}
"""
# -----------------------------------------------------------------------------
# Filled
# -----------------------------------------------------------------------------
"""Compute antialiased fragment color for a filled shape.
Inputs:
- distance (float): Signed distance to border (in pixels).
Template variables:
- linewidth (float): Stroke line width (in pixels).
- antialias (float): Stroke antialiased area (in pixels).
- fill (vec4): Fill color.
Outputs:
- color (vec4): The final color.
"""
ANTIALIAS_FILLED = """
vec4 filled(float distance)
{
vec4 frag_color;
float t = $linewidth/2.0 - $antialias;
float signed_distance = distance;
float border_distance = abs(signed_distance) - t;
float alpha = border_distance/$antialias;
alpha = exp(-alpha*alpha);
if( border_distance < 0.0 )
frag_color = $fill;
else if( signed_distance < 0.0 )
frag_color = $fill;
else
frag_color = vec4($fill.rgb, alpha * $fill.a);
return frag_color;
}
"""
|