| 1 |
precision highp float; |
| 2 |
precision highp int; |
| 3 |
|
| 4 |
#define FAST_CIRCLE 1 |
| 5 |
#define FAST_SQUARE 2 |
| 6 |
#define FAST_ARROW 3 |
| 7 |
#define FAST_CROSS 4 |
| 8 |
#define FAST_TRIANGLE_UP 5 |
| 9 |
#define FAST_TRIANGLE_DOWN 6 |
| 10 |
|
| 11 |
// This parameter is used for reducing aliasing |
| 12 |
#define SMOOTH_PIXELS 1.0 |
| 13 |
|
| 14 |
// ANGLE_X = X * PI / 180. |
| 15 |
#define ANGLE_20 0.3490658503988659 |
| 16 |
#define ANGLE_60 1.0471975511965976 |
| 17 |
|
| 18 |
varying vec4 v_fill_color; |
| 19 |
varying vec4 v_stroke_color; |
| 20 |
varying float v_inner_size; |
| 21 |
varying float v_outer_size; |
| 22 |
varying vec2 v_pixel; |
| 23 |
|
| 24 |
uniform float stroke_width; |
| 25 |
|
| 26 |
|
| 27 |
/* |
| 28 |
* Rotate a point in the 2-D plane, given an angle in radians |
| 29 |
*/ |
| 30 |
vec2 rotate_xy(vec2 x, float angle) { |
| 31 |
float sina = sin(angle); |
| 32 |
float cosa = cos(angle); |
| 33 |
mat2 m = mat2(cosa, -sina, sina, cosa); |
| 34 |
return m * x.xy; |
| 35 |
} |
| 36 |
|
| 37 |
/* |
| 38 |
* Returns 1.0 if pixel inside of a circle (0.0 otherwise) given the circle radius and the |
| 39 |
* pixel position. |
| 40 |
*/ |
| 41 |
float smooth_circle(in float radius, in vec2 pixel_position) { |
| 42 |
// This function does not use the ellipse function for optimization purpose |
| 43 |
// Circle equation: x^2 + y^2 = radius^2 |
| 44 |
float d = dot(pixel_position, pixel_position); |
| 45 |
float r1 = pow(radius - SMOOTH_PIXELS, 2.0); |
| 46 |
float r2 = pow(radius + SMOOTH_PIXELS, 2.0); |
| 47 |
return 1.0 - smoothstep(r1, r2, d); |
| 48 |
} |
| 49 |
|
| 50 |
/* |
| 51 |
* Returns 1.0 if pixel inside of an ellipse (0.0 otherwise) given the ellipse radius and the |
| 52 |
* pixel position. |
| 53 |
*/ |
| 54 |
float smooth_ellipse(in float a, in float b, in vec2 pixel_position) { |
| 55 |
// Ellipse equation: b^2 * x^2 + a^2 * y^2 = a^2 * b^2 |
| 56 |
float r_x = pow(a, 2.0); |
| 57 |
float r_y = pow(b, 2.0); |
| 58 |
float d = r_y * pow(pixel_position.x, 2.0) + r_x * pow(pixel_position.y, 2.0); |
| 59 |
float r1 = pow(a - SMOOTH_PIXELS, 2.0) * pow(b - SMOOTH_PIXELS, 2.0); |
| 60 |
float r2 = pow(a + SMOOTH_PIXELS, 2.0) * pow(b + SMOOTH_PIXELS, 2.0); |
| 61 |
return 1.0 - smoothstep(r1, r2, d); |
| 62 |
} |
| 63 |
|
| 64 |
/* |
| 65 |
* Returns 1.0 if pixel inside of a rectangle (0.0 otherwise) given the rectangle half-size |
| 66 |
* on the x and y axes and the pixel position. |
| 67 |
*/ |
| 68 |
float smooth_rectangle(in vec2 size, in vec2 pixel_position) { |
| 69 |
vec2 rec = smoothstep(vec2(-SMOOTH_PIXELS), vec2(SMOOTH_PIXELS), size - abs(pixel_position)); |
| 70 |
return rec.x * rec.y; |
| 71 |
} |
| 72 |
|
| 73 |
/* |
| 74 |
* Returns 1.0 if pixel inside of a rectangle (0.0 otherwise) given the rectangle half-size |
| 75 |
* on the x and y axes and the pixel position. |
| 76 |
*/ |
| 77 |
float rectangle(in vec2 size, in vec2 pixel_position) { |
| 78 |
vec2 rec = step(0.0, size - abs(pixel_position)); |
| 79 |
return rec.x * rec.y; |
| 80 |
} |
| 81 |
|
| 82 |
/* |
| 83 |
* Returns 1.0 if pixel inside of a square (0.0 otherwise) given the square half-size |
| 84 |
* and the pixel position. |
| 85 |
*/ |
| 86 |
float smooth_square(in float size, in vec2 pixel_position) { |
| 87 |
return smooth_rectangle(vec2(size), pixel_position); |
| 88 |
} |
| 89 |
|
| 90 |
float smooth_isosceles_triangle(in float angle, in float height, in vec2 pixel_position) { |
| 91 |
float half_angle = angle / 2.0; |
| 92 |
|
| 93 |
// The triangle center is on vec2(0.0, -height/3.0) |
| 94 |
vec2 translated_pixel = vec2(pixel_position.x, pixel_position.y - height / 3.0); |
| 95 |
|
| 96 |
vec2 pixel_left = rotate_xy(translated_pixel, -half_angle); |
| 97 |
vec2 pixel_right = rotate_xy(translated_pixel, half_angle); |
| 98 |
|
| 99 |
float half_height = height * 0.5; |
| 100 |
|
| 101 |
float half_bottom = half_height * tan(half_angle); |
| 102 |
|
| 103 |
return smoothstep(-SMOOTH_PIXELS, SMOOTH_PIXELS, half_bottom + pixel_left.x) * |
| 104 |
smoothstep(-SMOOTH_PIXELS, SMOOTH_PIXELS, half_bottom - pixel_right.x) * |
| 105 |
smoothstep(-SMOOTH_PIXELS, SMOOTH_PIXELS, translated_pixel.y + half_height); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
/* |
| 110 |
* Returns 1.0 if pixel inside of a cross shape (0.0 otherwise) given the cross half-size |
| 111 |
* on the x and y axes and the pixel position. |
| 112 |
*/ |
| 113 |
float cross(in vec2 size, in vec2 pixel_position) { |
| 114 |
float cross_shape = rectangle(size.xy, pixel_position) + |
| 115 |
rectangle(size.yx, pixel_position); |
| 116 |
return clamp(cross_shape, 0.0, 1.0); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
void main(void) { |
| 121 |
// fill_weight and stroke_weight are color factors |
| 122 |
// e.g. if fill_weight == 1.0 then the pixel color will be v_fill_color |
| 123 |
// e.g. if stroke_weight == 1.0 then the pixel color will be v_stroke_color |
| 124 |
float fill_weight = 0.0; |
| 125 |
float stroke_weight = 0.0; |
| 126 |
|
| 127 |
// Note for the reader: In the following code, |
| 128 |
// - `1.0` -> True |
| 129 |
// - `0.0` -> False |
| 130 |
// - `1.0 - A` -> NOT A |
| 131 |
// - `A + B` -> A OR B |
| 132 |
// - `A * B` -> A AND B |
| 133 |
|
| 134 |
float inner_shape = 0.0; |
| 135 |
float outer_shape = 0.0; |
| 136 |
|
| 137 |
#if FAST_DRAW == FAST_CIRCLE |
| 138 |
|
| 139 |
inner_shape = smooth_circle(v_inner_size, v_pixel); |
| 140 |
outer_shape = smooth_circle(v_outer_size, v_pixel); |
| 141 |
|
| 142 |
#elif FAST_DRAW == FAST_SQUARE |
| 143 |
|
| 144 |
inner_shape = smooth_square(v_inner_size, v_pixel); |
| 145 |
outer_shape = 1.0; // Always in the outer_shape |
| 146 |
|
| 147 |
#elif FAST_DRAW == FAST_CROSS |
| 148 |
|
| 149 |
float r = v_outer_size / 3.0; |
| 150 |
|
| 151 |
inner_shape = cross(vec2(v_inner_size, r - 2.0 * stroke_width), v_pixel); |
| 152 |
outer_shape = cross(vec2(r, v_outer_size), v_pixel); |
| 153 |
|
| 154 |
#elif FAST_DRAW == FAST_ARROW |
| 155 |
|
| 156 |
inner_shape = smooth_isosceles_triangle(ANGLE_20, v_inner_size, v_pixel); |
| 157 |
outer_shape = smooth_isosceles_triangle(ANGLE_20, v_outer_size, v_pixel); |
| 158 |
|
| 159 |
#elif FAST_DRAW == FAST_TRIANGLE_UP |
| 160 |
|
| 161 |
inner_shape = smooth_isosceles_triangle(ANGLE_60, v_inner_size, v_pixel); |
| 162 |
outer_shape = smooth_isosceles_triangle(ANGLE_60, v_outer_size, v_pixel); |
| 163 |
|
| 164 |
#elif FAST_DRAW == FAST_TRIANGLE_DOWN |
| 165 |
|
| 166 |
vec2 reversed_pixel = vec2(v_pixel.x, -v_pixel.y); |
| 167 |
|
| 168 |
inner_shape = smooth_isosceles_triangle(ANGLE_60, v_inner_size, reversed_pixel); |
| 169 |
outer_shape = smooth_isosceles_triangle(ANGLE_60, v_outer_size, reversed_pixel); |
| 170 |
|
| 171 |
#endif |
| 172 |
|
| 173 |
// `inner_shape` is the shape without the stroke, `outer_shape` is the shape with the stroke |
| 174 |
// note that the stroke is always drawn, only that it has the `v_fill_color` if stroke is None |
| 175 |
fill_weight = inner_shape; |
| 176 |
stroke_weight = (1.0 - inner_shape) * outer_shape; |
| 177 |
|
| 178 |
#if !FILL |
| 179 |
fill_weight = 0.0; |
| 180 |
#endif |
| 181 |
|
| 182 |
gl_FragColor = v_fill_color * fill_weight + v_stroke_color * stroke_weight; |
| 183 |
} |