Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gpu_shader_2D_nodelink_vert.glsl « shaders « gpu « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b83ea59a692a586a7ce535449e14c2758e00e56c (plain)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
 * 2D Quadratic Bezier thick line drawing
 */

#define MID_VERTEX 65

#ifndef USE_GPU_SHADER_CREATE_INFO

/* u is position along the curve, defining the tangent space.
 * v is "signed" distance (compressed to [0..1] range) from the pos in expand direction */
in vec2 uv;
in vec2 pos; /* verts position in the curve tangent space */
in vec2 expand;

#  ifdef USE_INSTANCE
/* Instance attrs. */
in vec2 P0;
in vec2 P1;
in vec2 P2;
in vec2 P3;
in ivec4 colid_doarrow;
in vec4 start_color;
in vec4 end_color;
in ivec2 domuted;
in float dim_factor;
in float thickness;
in float dash_factor;
in float dash_alpha;

uniform vec4 colors[6];

#  else
/* Single curve drawcall, use uniform. */
uniform vec2 bezierPts[4];

uniform vec4 colors[3];
uniform bool doArrow;
uniform bool doMuted;
uniform float dim_factor;
uniform float thickness;
uniform float dash_factor;
uniform float dash_alpha;

#  endif

uniform float expandSize;
uniform float arrowSize;
uniform mat4 ModelViewProjectionMatrix;

out float colorGradient;
out vec4 finalColor;
out float lineU;
flat out float lineLength;
flat out float dashFactor;
flat out float dashAlpha;
flat out int isMainLine;
#endif

#ifdef USE_INSTANCE
#  define colStart (colid_doarrow[0] < 3 ? start_color : node_link_data.colors[colid_doarrow[0]])
#  define colEnd (colid_doarrow[1] < 3 ? end_color : node_link_data.colors[colid_doarrow[1]])
#  define colShadow node_link_data.colors[colid_doarrow[2]]
#  define doArrow (colid_doarrow[3] != 0)
#  define doMuted (domuted[0] != 0)

#else
#  define P0 node_link_data.bezierPts[0].xy
#  define P1 node_link_data.bezierPts[1].xy
#  define P2 node_link_data.bezierPts[2].xy
#  define P3 node_link_data.bezierPts[3].xy
#  define cols node_link_data.colors
#  define doArrow node_link_data.doArrow
#  define doMuted node_link_data.doMuted
#  define dim_factor node_link_data.dim_factor
#  define thickness node_link_data.thickness
#  define dash_factor node_link_data.dash_factor
#  define dash_alpha node_link_data.dash_alpha

#  define colShadow node_link_data.colors[0]
#  define colStart node_link_data.colors[1]
#  define colEnd node_link_data.colors[2]

#endif

/* Define where along the noodle the gradient will starts and ends.
 * Use 0.25 instead of 0.35-0.65, because of a visual shift issue. */
const float start_gradient_threshold = 0.25;
const float end_gradient_threshold = 0.55;

void main(void)
{
  /* Parameters for the dashed line. */
  isMainLine = expand.y != 1.0 ? 0 : 1;
  dashFactor = dash_factor;
  dashAlpha = dash_alpha;
  /* Approximate line length, no need for real bezier length calculation. */
  lineLength = distance(P0, P3);
  /* TODO: Incorrect U, this leads to non-uniform dash distribution. */
  lineU = uv.x;

  float t = uv.x;
  float t2 = t * t;
  float t2_3 = 3.0 * t2;
  float one_minus_t = 1.0 - t;
  float one_minus_t2 = one_minus_t * one_minus_t;
  float one_minus_t2_3 = 3.0 * one_minus_t2;

  vec2 point = (P0 * one_minus_t2 * one_minus_t + P1 * one_minus_t2_3 * t +
                P2 * t2_3 * one_minus_t + P3 * t2 * t);

  vec2 tangent = ((P1 - P0) * one_minus_t2_3 + (P2 - P1) * 6.0 * (t - t2) + (P3 - P2) * t2_3);

  /* tangent space at t */
  tangent = normalize(tangent);
  vec2 normal = tangent.yx * vec2(-1.0, 1.0);

  /* Position vertex on the curve tangent space */
  point += (pos.x * tangent + pos.y * normal) * node_link_data.arrowSize;

  gl_Position = ModelViewProjectionMatrix * vec4(point, 0.0, 1.0);

  vec2 exp_axis = expand.x * tangent + expand.y * normal;

  /* rotate & scale the expand axis */
  exp_axis = ModelViewProjectionMatrix[0].xy * exp_axis.xx +
             ModelViewProjectionMatrix[1].xy * exp_axis.yy;

  float expand_dist = (uv.y * 2.0 - 1.0);
  colorGradient = expand_dist;

  if (gl_VertexID < MID_VERTEX) {
    /* Shadow pass */
    finalColor = colShadow;
  }
  else {
    /* Second pass */
    if (uv.x < start_gradient_threshold) {
      finalColor = colStart;
    }
    else if (uv.x > end_gradient_threshold) {
      finalColor = colEnd;
    }
    else {
      /* Add 0.1 to avoid a visual shift issue. */
      finalColor = mix(colStart, colEnd, uv.x + 0.1);
    }
    expand_dist *= 0.5;
    if (doMuted) {
      finalColor[3] = 0.65;
    }
  }

  finalColor[3] *= dim_factor;

  /* Expand into a line */
  gl_Position.xy += exp_axis * node_link_data.expandSize * expand_dist * thickness;

  /* If the link is not muted or is not a reroute arrow the points are squashed to the center of
   * the line. Magic numbers are defined in drawnode.c */
  if ((expand.x == 1.0 && !doMuted) ||
      (expand.y != 1.0 && (pos.x < 0.70 || pos.x > 0.71) && !doArrow)) {
    gl_Position.xy *= 0.0;
  }
}