Blame view

graph_shaders.py 6.47 KB
9f9f1788   Pavel Govyadinov   clead up version ...
1
2
3
4
5
6
7
8
9
10
11
12
13
  #!/usr/bin/env python3
  # -*- coding: utf-8 -*-
  """
  Created on Mon Aug  5 14:48:00 2019
  Collection of shaders necessary to draw all elements of the graph.
  @author: pavel
  """
  
  from vispy import gloo, app, scene
  
  #shaders for drawing nodes
  
  vert = """
4407a915   Pavel Govyadinov   working with new ...
14
  
9f9f1788   Pavel Govyadinov   clead up version ...
15
16
  // Uniforms
  // ------------------------------------
25fa0bfe   Pavel Govyadinov   Stable, pre-vispy...
17
18
  uniform float u_size;
  uniform float u_antialias;
9f9f1788   Pavel Govyadinov   clead up version ...
19
20
21
  uniform mat4 u_model;
  uniform mat4 u_view;
  uniform mat4 u_projection;
25fa0bfe   Pavel Govyadinov   Stable, pre-vispy...
22
  //uniform vec3 u_graph_size;
9f9f1788   Pavel Govyadinov   clead up version ...
23
24
  uniform bool u_picking;
  
9f9f1788   Pavel Govyadinov   clead up version ...
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
  // Attributes
  // ------------------------------------
  attribute vec3  a_position;
  attribute vec4  a_fg_color;
  attribute vec4  a_bg_color;
  attribute float a_linewidth;
  attribute float a_size;
  attribute vec4  a_unique_id;
  attribute float  a_selection;
  // Varyings
  // ------------------------------------
  varying vec4 v_fg_color;
  varying vec4 v_bg_color;
  varying float v_size;
  varying float v_linewidth;
  varying float v_antialias;
  varying vec4  v_unique_id;
  varying float  v_selection;
  
  varying float v_zoom_level;
  void main (void) {
      v_size = a_size * u_size;
      v_linewidth = a_linewidth;
      v_antialias = u_antialias;
      v_fg_color  = a_fg_color;
      v_bg_color = a_bg_color;
      gl_Position = u_projection * u_view * u_model *
          vec4(a_position*u_size,1.0);
      gl_PointSize = v_size + 2*(v_linewidth + 1.5*v_antialias);
      v_zoom_level = u_view[0][0];
      v_unique_id = a_unique_id;
      v_selection = a_selection;
  }
  """
  
  frag = """
4407a915   Pavel Govyadinov   working with new ...
61
  
9f9f1788   Pavel Govyadinov   clead up version ...
62
63
64
65
66
67
  // Constants
  // ------------------------------------
  uniform mat4 u_model;
  uniform mat4 u_view;
  uniform mat4 u_projection;
  uniform float u_antialias;
9f9f1788   Pavel Govyadinov   clead up version ...
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
  uniform bool u_picking;
  
  // Varyings
  // ------------------------------------
  varying vec4 v_fg_color;
  varying vec4 v_bg_color;
  varying float v_size;
  varying float v_linewidth;
  varying float v_antialias;
  varying vec4 v_unique_id;
  
  varying float v_zoom_level;
  varying float v_selection;
  // Functions
  // ------------------------------------
  float marker(vec2 P, float size);
  float new_alpha(float zoom_level);
  // Main
  // ------------------------------------
  void main()
  {
   if(!u_picking)
   {
      float size = v_size + 2*(v_linewidth + 1.5*v_antialias);
      float t = v_linewidth/2.0-v_antialias;
4407a915   Pavel Govyadinov   working with new ...
93
94
95
      vec2 p = (gl_PointCoord.xy - vec2(0.5,0.5))*size;
      float r = length(p);
      r -= v_size/2;
9f9f1788   Pavel Govyadinov   clead up version ...
96
      float d = abs(r) - t;
193cb4c6   Pavel Govyadinov   need this to test
97
      if( r > (v_linewidth/3.0+v_antialias))
9f9f1788   Pavel Govyadinov   clead up version ...
98
99
100
101
102
      {
          discard;
      }
      else if( d < 0.0 )
      {
17e4b25a   Pavel Govyadinov   Bug fixes, Dual v...
103
         if(v_zoom_level < 0.0024)
9f9f1788   Pavel Govyadinov   clead up version ...
104
105
106
         {
             float alpha = d/v_antialias;
             alpha = new_alpha(v_zoom_level);
4407a915   Pavel Govyadinov   working with new ...
107
             gl_FragColor = mix(vec4(1,1,1,0.9), v_bg_color, max(1.0-alpha, 0.3));
9f9f1788   Pavel Govyadinov   clead up version ...
108
109
110
111
112
113
114
115
         }
         else
         {
             if(v_selection == 1.0)
                 gl_FragColor = vec4(1.0, 0.0, 0.0, v_fg_color.a);
             else if(v_selection == 2.0)
                 gl_FragColor = vec4(0.0, 1.0, 0.0, v_fg_color.a);
             else
193cb4c6   Pavel Govyadinov   need this to test
116
                 gl_FragColor = vec4(v_fg_color.rgb, v_bg_color.a);
9f9f1788   Pavel Govyadinov   clead up version ...
117
118
119
120
121
         }
      }
      else
      {
          float alpha = d/v_antialias;
17e4b25a   Pavel Govyadinov   Bug fixes, Dual v...
122
          if(v_zoom_level < 0.0024)
9f9f1788   Pavel Govyadinov   clead up version ...
123
124
125
126
              alpha = new_alpha(v_zoom_level);
          else
              alpha = exp(-alpha*alpha);
  
17e4b25a   Pavel Govyadinov   Bug fixes, Dual v...
127
          if (r > 0 && v_zoom_level >= 0.0024)
9f9f1788   Pavel Govyadinov   clead up version ...
128
129
              gl_FragColor = vec4(v_fg_color.rgb, alpha*v_fg_color.a);
          else
17e4b25a   Pavel Govyadinov   Bug fixes, Dual v...
130
              if(v_zoom_level < 0.0024)
9f9f1788   Pavel Govyadinov   clead up version ...
131
132
133
134
135
                  if(vec3(gl_Color.rgb) != vec3(v_bg_color.rgb))
                      gl_FragColor = mix(vec4(1,1,1,0.9), v_bg_color, max(1.0-alpha, 0.3));
                  else
                      gl_FragColor = vec4(gl_Color.rgb, max(1.0-alpha, 0.1));
              else
4407a915   Pavel Govyadinov   working with new ...
136
                  {
17e4b25a   Pavel Govyadinov   Bug fixes, Dual v...
137
138
139
140
141
142
143
                          gl_FragColor = mix(v_bg_color, v_fg_color, alpha);
                          //vec3 color = vec3(1.0, 0.0, 0.0);
                          //vec3 normal = normalize(vec3(p.xy, d));
                          //vec3 direction = normalize(vec3(1.0, 1.0, 1.0));
                          //float diffuse = max(0.0, dot(direction, normal));
                          //float specular = pow(diffuse, 24.0);
                          //gl_FragColor = mix(vec4(max(diffuse*v_bg_color.rgb, specular*vec3(1.0)), 1.0), v_fg_color, alpha);
4407a915   Pavel Govyadinov   working with new ...
144
                  }
9f9f1788   Pavel Govyadinov   clead up version ...
145
146
147
148
149
150
151
152
153
154
      }
  
  
   } else {
         float size = v_size +2*(v_linewidth + 1.5*v_antialias);
         float t = v_linewidth/2.0-v_antialias;
         // The marker function needs to be linked with this shader
         float r = marker(gl_PointCoord, size);
         float d = abs(r) - t;
         float alpha = d/v_antialias;
17e4b25a   Pavel Govyadinov   Bug fixes, Dual v...
155
         if(v_zoom_level < 0.0024)
9f9f1788   Pavel Govyadinov   clead up version ...
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
             alpha = new_alpha(v_zoom_level);
         else
             alpha = exp(-alpha*alpha);
         if(r > 0)
             gl_FragColor = v_unique_id;
         else
             gl_FragColor = v_unique_id;
  
      }
  }
  
  float marker(vec2 P, float size)
  {
      float r = length((P.xy - vec2(0.5,0.5))*size);
      r -= v_size/2;
      return r;
  }
  
  float new_alpha(float zoom_level)
  {
17e4b25a   Pavel Govyadinov   Bug fixes, Dual v...
176
      float val = (zoom_level - 0.0024)/(0.00075-0.0024);
9f9f1788   Pavel Govyadinov   clead up version ...
177
178
179
180
181
182
183
184
185
186
187
188
      if(val < 0)
      {
          val = 0;
      }
      return val;
  }
  """
  
  #Shaders for the lines between the nodes
  
  
  vs = """
4407a915   Pavel Govyadinov   working with new ...
189
  
9f9f1788   Pavel Govyadinov   clead up version ...
190
191
192
193
194
195
196
197
198
199
200
201
202
  
  // 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 ...
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
  
  // 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);
      //vec4 pos = u_model * u_view * vec4(a_position, 1.0);
      //gl_Position = u_projection * (pos + vec4(delta, 1.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 = """
4407a915   Pavel Govyadinov   working with new ...
227
  
9f9f1788   Pavel Govyadinov   clead up version ...
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
  // 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.5;
  
17e4b25a   Pavel Govyadinov   Bug fixes, Dual v...
247
        if(v_zoom_level < 0.0024)
9f9f1788   Pavel Govyadinov   clead up version ...
248
249
250
251
252
253
              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...
254
      float val = (zoom_level-0.00075)/(0.0024-0.00075);
9f9f1788   Pavel Govyadinov   clead up version ...
255
256
257
258
259
260
261
      if(val < 0.)
      {
          val = 0.;
      }
      return val;
  
  }
25fa0bfe   Pavel Govyadinov   Stable, pre-vispy...
262
  """