| 1 |
#include <scales> |
| 2 |
|
| 3 |
uniform mat4 projectionMatrix; |
| 4 |
uniform mat4 modelViewMatrix; |
| 5 |
|
| 6 |
// for animation, all between 0 and 1 |
| 7 |
uniform float animation_time_x; |
| 8 |
uniform float animation_time_y; |
| 9 |
uniform float animation_time_z; |
| 10 |
uniform float animation_time_size; |
| 11 |
uniform float animation_time_rotation; |
| 12 |
uniform float animation_time_opacity; |
| 13 |
|
| 14 |
#ifdef HAS_DEFAULT_STROKE_COLOR |
| 15 |
uniform vec4 default_stroke_color; |
| 16 |
#endif |
| 17 |
|
| 18 |
uniform bool has_selection; |
| 19 |
uniform bool has_hover; |
| 20 |
|
| 21 |
// maybe faster to use #IFDEFS |
| 22 |
uniform bool has_selected_fill; |
| 23 |
uniform vec4 selected_fill; |
| 24 |
uniform bool has_selected_stroke; |
| 25 |
uniform vec4 selected_stroke; |
| 26 |
uniform bool has_selected_opacity; |
| 27 |
uniform float selected_opacity; |
| 28 |
|
| 29 |
uniform bool has_unselected_fill; |
| 30 |
uniform vec4 unselected_fill; |
| 31 |
uniform bool has_unselected_stroke; |
| 32 |
uniform vec4 unselected_stroke; |
| 33 |
uniform bool has_unselected_opacity; |
| 34 |
uniform float unselected_opacity; |
| 35 |
|
| 36 |
|
| 37 |
uniform bool has_hovered_fill; |
| 38 |
uniform vec4 hovered_fill; |
| 39 |
uniform bool has_hovered_stroke; |
| 40 |
uniform vec4 hovered_stroke; |
| 41 |
uniform bool has_hovered_opacity; |
| 42 |
uniform float hovered_opacity; |
| 43 |
|
| 44 |
uniform bool has_unhovered_fill; |
| 45 |
uniform vec4 unhovered_fill; |
| 46 |
uniform bool has_unhovered_stroke; |
| 47 |
uniform vec4 unhovered_stroke; |
| 48 |
uniform bool has_unhovered_opacity; |
| 49 |
uniform float unhovered_opacity; |
| 50 |
|
| 51 |
uniform vec2 domain_x; |
| 52 |
uniform vec2 domain_y; |
| 53 |
uniform vec2 domain_z; |
| 54 |
uniform vec2 domain_size; |
| 55 |
uniform vec2 domain_rotation; |
| 56 |
uniform vec2 domain_opacity; |
| 57 |
|
| 58 |
uniform vec2 range_x; |
| 59 |
uniform vec2 range_y; |
| 60 |
uniform vec2 range_z; |
| 61 |
uniform vec2 range_size; |
| 62 |
uniform vec2 range_rotation; |
| 63 |
uniform vec2 range_opacity; |
| 64 |
|
| 65 |
uniform bool fill; |
| 66 |
uniform float stroke_width; |
| 67 |
uniform float marker_scale; |
| 68 |
|
| 69 |
varying vec4 v_fill_color; |
| 70 |
varying vec4 v_stroke_color; |
| 71 |
varying float v_inner_size; |
| 72 |
varying float v_outer_size; |
| 73 |
varying vec2 v_pixel; |
| 74 |
|
| 75 |
attribute vec3 position; |
| 76 |
attribute vec2 uv; |
| 77 |
|
| 78 |
attribute float x; |
| 79 |
attribute float x_previous; |
| 80 |
attribute float y; |
| 81 |
attribute float y_previous; |
| 82 |
|
| 83 |
attribute float size; |
| 84 |
attribute float size_previous; |
| 85 |
|
| 86 |
attribute float rotation; |
| 87 |
attribute float rotation_previous; |
| 88 |
|
| 89 |
attribute float opacity; |
| 90 |
attribute float opacity_previous; |
| 91 |
|
| 92 |
// Boolean or int not supported for attributes, using a float instead |
| 93 |
attribute float selected; |
| 94 |
|
| 95 |
uniform sampler2D colormap; |
| 96 |
uniform vec2 domain_color; |
| 97 |
|
| 98 |
#ifdef USE_COLORMAP |
| 99 |
attribute float color; |
| 100 |
#else |
| 101 |
attribute vec3 color; |
| 102 |
#endif |
| 103 |
|
| 104 |
#if SCALE_TYPE_x == SCALE_TYPE_LINEAR |
| 105 |
#define SCALE_X(x) scale_transform_linear(x, range_x, domain_x) |
| 106 |
#elif SCALE_TYPE_x == SCALE_TYPE_LOG |
| 107 |
#define SCALE_X(x) scale_transform_log(x, range_x, domain_x) |
| 108 |
#endif |
| 109 |
|
| 110 |
#if SCALE_TYPE_y == SCALE_TYPE_LINEAR |
| 111 |
#define SCALE_Y(x) scale_transform_linear(x, range_y, domain_y) |
| 112 |
#elif SCALE_TYPE_y == SCALE_TYPE_LOG |
| 113 |
#define SCALE_Y(x) scale_transform_log(x, range_y, domain_y) |
| 114 |
#endif |
| 115 |
|
| 116 |
#define SCALE_SIZE(x) scale_transform_linear(x, range_size, domain_size) |
| 117 |
#define SCALE_ROTATION(x) scale_transform_linear(x, range_rotation, domain_rotation) |
| 118 |
#define SCALE_OPACITY(x) scale_transform_linear(x, range_opacity, domain_opacity) |
| 119 |
|
| 120 |
vec3 rotate_xy(vec3 x, float angle) { |
| 121 |
float sina = sin(angle); |
| 122 |
float cosa = cos(angle); |
| 123 |
mat2 m = mat2(cosa, -sina, sina, cosa); |
| 124 |
return vec3(m * x.xy, x.z); |
| 125 |
} |
| 126 |
|
| 127 |
void main(void) { |
| 128 |
|
| 129 |
vec3 animation_time = vec3(animation_time_x, animation_time_y, animation_time_z); |
| 130 |
|
| 131 |
vec3 center = mix(vec3(x_previous, y_previous, 0), vec3(x, y, 0), animation_time); |
| 132 |
|
| 133 |
// times 2 because the normalized coordinates go from [-1, 1] |
| 134 |
vec3 center_pixels = vec3(SCALE_X(center.x), SCALE_Y(center.y), 0) * 2.; |
| 135 |
|
| 136 |
// times 4 because of the normalized coordinates, and radius vs diameter use |
| 137 |
float marker_size = sqrt(mix(SCALE_SIZE(size_previous), SCALE_SIZE(size), animation_time_size)) * marker_scale * 4.; |
| 138 |
// we draw larger than the size for the stroke_width (on both side) |
| 139 |
|
| 140 |
// `v_inner_size` is the marker "radius" without the stroke, `v_outer_size` is the marker "radius" with the stroke |
| 141 |
v_inner_size = marker_size * 0.5 - stroke_width; |
| 142 |
v_outer_size = marker_size * 0.5 + stroke_width; |
| 143 |
|
| 144 |
// `full_size` is the marker "diameter" with the stroke |
| 145 |
float full_size = marker_size + 2.0 * stroke_width; |
| 146 |
|
| 147 |
// `v_pixel` is the pixel position relatively to the marker, |
| 148 |
// e.g. vec2(0.) would be the center of the square marker |
| 149 |
// e.g. vec2(0.5 * full_size) would be the top-right pixel of the square marker |
| 150 |
v_pixel = (uv - 0.5) * (full_size); |
| 151 |
|
| 152 |
float angle = SCALE_ROTATION(mix(rotation_previous, rotation, animation_time_rotation)); |
| 153 |
vec3 model_pos = rotate_xy(position, 1.) * full_size + center_pixels; |
| 154 |
|
| 155 |
#ifdef USE_COLORMAP |
| 156 |
float color_index = (color - domain_color.x) / (domain_color.y - domain_color.x); |
| 157 |
vec4 color_rgba = texture2D(colormap, vec2(color_index, 0)); |
| 158 |
#else |
| 159 |
vec4 color_rgba = vec4(color, 1.0); |
| 160 |
#endif |
| 161 |
|
| 162 |
v_fill_color = color_rgba; |
| 163 |
v_stroke_color = color_rgba; |
| 164 |
|
| 165 |
#ifdef HAS_DEFAULT_STROKE_COLOR |
| 166 |
v_stroke_color = default_stroke_color; |
| 167 |
#endif |
| 168 |
|
| 169 |
float opacity_value = SCALE_OPACITY(mix(opacity_previous, opacity, animation_time_opacity)); |
| 170 |
v_fill_color.a *= opacity_value; |
| 171 |
v_stroke_color.a *= opacity_value; |
| 172 |
|
| 173 |
if(has_selection) { |
| 174 |
if(has_selected_fill && selected > 0.5 ) |
| 175 |
v_fill_color = selected_fill; |
| 176 |
if(has_unselected_fill && selected < 0.5 ) |
| 177 |
v_fill_color = unselected_fill; |
| 178 |
if(has_selected_stroke && selected > 0.5 ) |
| 179 |
v_stroke_color = selected_stroke; |
| 180 |
if(has_unselected_stroke && selected < 0.5 ) |
| 181 |
v_stroke_color = unselected_stroke; |
| 182 |
if(has_selected_opacity && selected > 0.5 ) { |
| 183 |
v_stroke_color.a *= selected_opacity; |
| 184 |
v_fill_color.a *= selected_opacity; |
| 185 |
} |
| 186 |
if(has_unselected_opacity && selected < 0.5 ) { |
| 187 |
v_stroke_color.a *= unselected_opacity; |
| 188 |
v_fill_color.a *= unselected_opacity; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
v_fill_color.rgb *= v_fill_color.a; |
| 193 |
v_stroke_color.rgb *= v_stroke_color.a; |
| 194 |
|
| 195 |
gl_Position = projectionMatrix * vec4(rotate_xy(position, angle) * full_size, 1.0) + |
| 196 |
projectionMatrix * modelViewMatrix * vec4(center_pixels, 1.0); |
| 197 |
} |