Blame view

subgraph_shaders.py 8.1 KB
9f9f1788   Pavel Govyadinov   clead up version ...
1
2
3
4
5
6
7
8
9
10
  #!/usr/bin/env python3
  # -*- coding: utf-8 -*-
  """
  Created on Mon Aug  5 15:35:04 2019
  
  @author: pavel
  """
  
  #shaders for drawing supernodes
  vert_s = """
9f9f1788   Pavel Govyadinov   clead up version ...
11
12
13
14
15
  // Uniforms
  // ------------------------------------
  uniform mat4 u_model;
  uniform mat4 u_view;
  uniform mat4 u_projection;
25fa0bfe   Pavel Govyadinov   Stable, pre-vispy...
16
  //uniform vec3 u_graph_size;
9f9f1788   Pavel Govyadinov   clead up version ...
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
  uniform bool u_picking;
  // Attributes
  // ------------------------------------
  attribute vec3  a_position;
  attribute vec4  a_bg_color;
  attribute vec4  a_cluster_color;
  attribute vec2  a_value;
  attribute float a_arc_length;
  attribute vec4 a_outer_arc_length;
  attribute vec4 a_unique_id;
  
  // Varyings
  // ------------------------------------
  varying vec4 v_bg_color;
  varying vec4 v_cluster_color;
  varying vec2 v_value;
  varying float v_zoom_level;
  varying float v_arc_length;
  varying vec4 v_outer_arc_length;
  varying vec4 v_unique_id;
  
  void main (void) {
      v_value = a_value;
      v_bg_color = a_bg_color;
      v_cluster_color = a_cluster_color;
      gl_Position = u_projection * u_view * u_model *
          vec4(a_position, 1.0);
      v_zoom_level = u_view[0][0];
      v_arc_length = a_arc_length;
      v_outer_arc_length = a_outer_arc_length;
      v_unique_id = a_unique_id;
  }
  """
  
  frag_s = """
9f9f1788   Pavel Govyadinov   clead up version ...
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
  const float pi = 3.1415926535897932384626433832795;
  // Varyings
  // ------------------------------------
  varying vec4 v_bg_color;
  varying vec2 v_value;
  varying float v_zoom_level;
  varying vec4 v_cluster_color;
  varying float v_arc_length;
  varying vec4 v_outer_arc_length;
  varying vec4 v_unique_id;
  
  uniform bool u_picking;
  
  
  // Attributes
  // ------------------------------------
  
  // Functions
  // ------------------------------------
  float new_alpha(float zoom_level);
  float atan2(float y, float x);
  // Main
  // ------------------------------------
  void main()
  {
      if(!u_picking)
      {
      //This is the color of the outer arc lengths
           float R = 0.55;
           float R2 = 0.3;
           float dist = sqrt(dot(v_value, v_value));
17e4b25a   Pavel Govyadinov   Bug fixes, Dual v...
83
84
           float sm = smoothstep(R, R-0.0024, dist);
           float sm2 = smoothstep(R2, R2+0.0024, dist);
9f9f1788   Pavel Govyadinov   clead up version ...
85
           float alpha = sm*sm2;
17e4b25a   Pavel Govyadinov   Bug fixes, Dual v...
86
           float epsilon = 0.005;
9f9f1788   Pavel Govyadinov   clead up version ...
87
88
  
           float n_alpha = 1.0;
17e4b25a   Pavel Govyadinov   Bug fixes, Dual v...
89
           if(v_zoom_level < 0.0024)
9f9f1788   Pavel Govyadinov   clead up version ...
90
91
92
93
94
95
96
97
98
99
100
           {
                n_alpha = new_alpha(v_zoom_level);
           }
           else
           {
                discard;
           }
           float angle = atan(v_value[1], v_value[0]);
           if(dist < 0.25)
           {
                //gl_FragColor = v_cluster_color;
17e4b25a   Pavel Govyadinov   Bug fixes, Dual v...
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
                vec2 p = (v_value.xy);
                vec3 normal = normalize(vec3(p.xy, 0.25));
                vec3 direction = normalize(vec3(0.0, 0.0, 5.0));
                float diffuse = max(0.0, dot(direction, normal));
                float specular = pow(diffuse, 24.0);
                gl_FragColor = vec4(max(diffuse*v_cluster_color.rgb, specular*vec3(0.6)), n_alpha);
  //              gl_FragColor = vec4(v_cluster_color.rgb, n_alpha);
           }
           if(dist < 0.25+epsilon && dist > 0.25)
           {
                vec2 p = (v_value.xy);
                vec3 normal = normalize(vec3(p.xy, 0.25));
                vec3 direction = normalize(vec3(0.0, 0.0, 5.0));
                float diffuse = max(0.0, dot(direction, normal));
                float specular = pow(diffuse, 24.0);
                   gl_FragColor = vec4(max(diffuse*v_cluster_color.rgb, specular*vec3(0.6)), exp(-n_alpha*n_alpha));
9f9f1788   Pavel Govyadinov   clead up version ...
117
118
119
120
121
122
123
124
125
126
           }
           if(dist > 0.3 && 0.55 > dist)
           {
                float angle2 = angle+pi;
                if(angle2 > v_arc_length)
                {
                        discard;
                }
                else
                {
17e4b25a   Pavel Govyadinov   Bug fixes, Dual v...
127
128
129
130
131
132
                      vec3 normal = normalize(vec3(v_value.xy, 1.0));
                      vec3 direction = normalize(vec3(0.0, 0.0, 5.0));
                      float diffuse = max(0.0, dot(direction, normal));
                      float specular = pow(diffuse, 24.0);
                      //gl_FragColor = vec4(v_bg_color.rgb, n_alpha);
                      gl_FragColor = vec4(max(diffuse*v_bg_color.rgb, specular*vec3(1.0)), n_alpha);
9f9f1788   Pavel Govyadinov   clead up version ...
133
134
135
136
137
138
139
140
                }
           }
           angle = atan(v_value[1]/dist, v_value[0]/dist);
      //Need to add antialiasing to all other circles in the form of smoothstep.
  
      //THIS NEED TO BE FIXED
           if(dist > 0.61 && 0.9 > dist)
           {
17e4b25a   Pavel Govyadinov   Bug fixes, Dual v...
141
142
143
144
                vec3 normal = normalize(vec3(v_value.xy, 1.0));
                vec3 direction = normalize(vec3(0.0, 0.0, 5.0));
                float diffuse = max(0.0, dot(direction, normal));
                float specular = pow(diffuse, 24.0);
9f9f1788   Pavel Govyadinov   clead up version ...
145
146
                if(angle < v_outer_arc_length[1])
                {
17e4b25a   Pavel Govyadinov   Bug fixes, Dual v...
147
148
149
                        vec3 color = vec3(0.32, 0.61, 0.93);
                       // gl_FragColor = vec4(0.32, 0.61, 0.93, n_alpha);
                        gl_FragColor = vec4(max(diffuse*color, specular*vec3(1.0)), n_alpha);
9f9f1788   Pavel Govyadinov   clead up version ...
150
151
152
153
                        //gl_FragColor = vec4(1.0, 0.0, 0.0, n_alpha);
                }
                else if(angle > v_outer_arc_length[1] && angle < v_outer_arc_length[2])
                {
17e4b25a   Pavel Govyadinov   Bug fixes, Dual v...
154
155
156
                        vec3 color = vec3(0.337, 0.866, 0.415);
                        gl_FragColor = vec4(max(diffuse*color, specular*vec3(1.0)), n_alpha);
                        //gl_FragColor = vec4(0.337, 0.866, 0.415, n_alpha);
9f9f1788   Pavel Govyadinov   clead up version ...
157
158
159
160
                        //gl_FragColor = vec4(0.0, 1.0, 0.0, n_alpha);
                }
                else if(angle > v_outer_arc_length[2] && angle < v_outer_arc_length[3])
                {
17e4b25a   Pavel Govyadinov   Bug fixes, Dual v...
161
162
163
                        vec3 color = vec3(0.988, 0.631, 0.058);
                        gl_FragColor = vec4(max(diffuse*color, specular*vec3(1.0)), n_alpha);
                        //gl_FragColor = vec4(0.988, 0.631, 0.058, n_alpha);
9f9f1788   Pavel Govyadinov   clead up version ...
164
165
166
167
                        //gl_FragColor = vec4(0.0, 0.0, 1.0, n_alpha);
                }
                else //if(angle > v_outer_arc_length[3] && angle < pi)
                {
17e4b25a   Pavel Govyadinov   Bug fixes, Dual v...
168
169
170
                        vec3 color = vec3(0.93, 0.949, 0.329);
                        gl_FragColor = vec4(max(diffuse*color, specular*vec3(1.0)), n_alpha);
                        //gl_FragColor = vec4(0.93, 0.949, 0.329, n_alpha);
9f9f1788   Pavel Govyadinov   clead up version ...
171
172
                        //gl_FragColor = vec4(1.0, 1.0, 0.0, n_alpha);
                }
9f9f1788   Pavel Govyadinov   clead up version ...
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
           }
      }
      else
      {
           float dist = sqrt(dot(v_value, v_value));
           if (dist > 0.9)
               discard;
           else
               gl_FragColor = v_unique_id;
      }
  }
  
  float atan2(float y, float x)
  {
       float s;
       if(abs(x) > abs(y))
           s = 1.0;
       else
          s = 0.0;
       return (mix(pi/2.0 - atan(x,y), atan(y,x), s));
  }
  
  //float atan2(in float y, in float x)
  //{
  //    return x == 0.0 ? sign(y)*pi/2 : atan(y, x);
  //}
  
  float new_alpha(float zoom_level)
  {
17e4b25a   Pavel Govyadinov   Bug fixes, Dual v...
202
      float val = 1 - (zoom_level - 0.0015)/(0.0024-0.0015);
9f9f1788   Pavel Govyadinov   clead up version ...
203
204
205
206
207
208
209
210
211
      if(val > 1.0)
      {
          val = 1.0;
      }
      return val;
  }
  """
  
  vs_s = """
9f9f1788   Pavel Govyadinov   clead up version ...
212
213
214
215
216
217
218
219
220
221
222
223
224
  
  // Uniforms
  // ------------------------------------
  uniform mat4 u_model;
  uniform mat4 u_view;
  uniform mat4 u_projection;
  
  // Attributes
  // ------------------------------------
  attribute vec3 a_position;
  attribute vec2 a_normal;
  attribute vec4 a_fg_color;
  attribute float a_linewidth;
9f9f1788   Pavel Govyadinov   clead up version ...
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
  
  // Varyings
  // ------------------------------------
  varying vec4 v_fg_color;
  varying float v_zoom_level;
  varying vec2 v_normal;
  varying float v_linewidth;
  
  void main() {
      vec3 delta = vec3(a_normal*a_linewidth/((1-u_view[0][0])*0.1), 0);
      //vec3 delta = vec3(a_normal*a_linewidth/(1-u_view[0][0]), 0);
      gl_Position = u_model * u_view * u_projection * vec4(a_position+delta, 1.0);
      //gl_Position = u_projection * u_view * u_model *
      //    vec4(a_position, 1.0);
      v_zoom_level = u_view[0][0];
      v_fg_color = a_fg_color;
      v_normal = a_normal;
      v_linewidth = a_linewidth;
  
  }
  """
  
  fs_s = """
4407a915   Pavel Govyadinov   working with new ...
248
  
9f9f1788   Pavel Govyadinov   clead up version ...
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
  // Varying
  // ------------------------------------
  varying vec4 v_fg_color;
  varying float v_zoom_level;
  varying vec2 v_normal;
  varying float v_linewidth;
  
  float new_alpha(float zoom_level);
  
  void main()
  {
        float l = length(v_normal);
        float feather = 0.5;
        float alpha = 1.0;
        if(l > v_linewidth/2.0+feather)
            discard;
        else
            alpha = 0.0;
  
17e4b25a   Pavel Govyadinov   Bug fixes, Dual v...
268
        if(v_zoom_level < 0.0024)
9f9f1788   Pavel Govyadinov   clead up version ...
269
270
271
272
273
274
              alpha = new_alpha(v_zoom_level);
        gl_FragColor = vec4(v_fg_color.rgb, alpha);
  }
  
  float new_alpha(float zoom_level)
  {
17e4b25a   Pavel Govyadinov   Bug fixes, Dual v...
275
      float val = 1 - (zoom_level - 0.0015)/(0.0024-0.0015);
9f9f1788   Pavel Govyadinov   clead up version ...
276
277
278
279
280
281
      if(val > 1.0)
      {
          val = 1.0;
      }
      return val;
  }
25fa0bfe   Pavel Govyadinov   Stable, pre-vispy...
282
  """