-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbasic.vert
More file actions
174 lines (151 loc) · 4.42 KB
/
basic.vert
File metadata and controls
174 lines (151 loc) · 4.42 KB
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
165
166
167
168
169
170
171
172
173
174
const float PI = 3.14159265358979323846264;
const float THETA = 15.0 * 3.14159265358979323846264/180.0;
// Cross product of v1 and v2
float cross(in vec2 v1, in vec2 v2)
{
return v1.x*v2.y - v1.y*v2.x;
}
// ----------------------------------------------------------------------------
// Returns distance of v3 to line v1-v2
float signed_distance(in vec2 v1, in vec2 v2, in vec2 v3)
{
return cross(v2-v1,v1-v3) / length(v2-v1);
}
// Uniforms
// ------------------------------------
uniform mat4 u_mvpMatrix;
uniform vec4 u_color;
uniform float u_lineWidth;
uniform float u_antialias;
uniform vec2 u_lineCaps;
uniform float u_linejoin;
uniform float u_miter_limit;
uniform float u_length;
uniform float u_closed;
// Attributes
// ------------------------------------
attribute vec2 a_position;
attribute vec2 a_segment;
attribute vec2 a_angles;
attribute vec4 a_tangents;
attribute vec2 a_texcoord;
//attribute float a_index;
// Varying
// ------------------------------------
varying vec4 v_color;
varying vec2 v_segment;
varying vec2 v_angles;
varying vec2 v_linecaps;
varying vec2 v_texcoord;
varying vec2 v_miter;
varying float v_miter_limit;
varying float v_length;
varying float v_linejoin;
varying float v_linewidth;
varying float v_antialias;
varying float v_closed;
void main()
{
v_color = u_color;
v_linewidth = u_lineWidth;
v_antialias = u_antialias;
v_linecaps = u_lineCaps;
v_linejoin = u_linejoin;
v_miter_limit = u_miter_limit;
v_length = u_length;
v_closed = u_closed;
bool closed = (v_closed > 0.0);
v_angles = a_angles;
v_segment = a_segment;
// Thickness below 1 pixel are represented using a 1 pixel thickness
// and a modified alpha
v_color.a = min(v_linewidth, v_color.a);
v_linewidth = max(v_linewidth, 1.0);
// If color is fully transparent we just will discard the fragment anyway
if( v_color.a <= 0.0 )
{
gl_Position = vec4(0.0,0.0,0.0,1.0);
return;
}
// This is the actual half width of the line
float w = ceil(1.25*v_antialias+v_linewidth)/2.0;
vec2 position = a_position;
vec2 t1 = normalize(a_tangents.xy);
vec2 t2 = normalize(a_tangents.zw);
float u = a_texcoord.x;
float v = a_texcoord.y;
vec2 o1 = vec2( +t1.y, -t1.x);
// This is a join
// ----------------------------------------------------------------
if( t1 != t2 )
{
float angle = atan (t1.x*t2.y-t1.y*t2.x, t1.x*t2.x+t1.y*t2.y);
vec2 t = normalize(t1+t2);
vec2 o = vec2( + t.y, - t.x);
position.xy += v * w * o / cos(angle/2.0);
if( angle < 0.0 ) {
if( u == +1.0 ) {
// u = v_segment.y + v * w * tan(angle/2.0);
u = v_segment.y;
} else {
// u = v_segment.x - v * w * tan(angle/2.0);
u = v_segment.x;
}
}
else
{
if( u == +1.0 )
{
// u = v_segment.y + u * w * tan(angle/2.0);
u = v_segment.y;
}
else
{
//u = v_segment.x - u * w * tan(angle/2.0);
u = v_segment.x;
}
}
// This is a line start or end (t1 == t2)
// ------------------------------------------------------------------------
}
else
{
position += v * w * o1;
if( u == -1.0 )
{
u = v_segment.x - w;
position -= w * t1;
}
else
{
u = v_segment.y + w;
position += w * t2;
}
}
// Miter distance
// ------------------------------------------------------------------------
vec2 t;
vec2 curr = a_position;
if( a_texcoord.x < 0.0 )
{
vec2 next = curr + t2*(v_segment.y-v_segment.x);
v_miter.x = signed_distance(curr, curr+t, position);
v_miter.y = signed_distance(next, next+t, position);
}
else
{
vec2 prev = curr - t1*(v_segment.y-v_segment.x);
v_miter.x = signed_distance(prev, prev+t, position);
v_miter.y = signed_distance(curr, curr+t, position);
}
if (!closed && v_segment.x <= 0.0)
{
v_miter.x = 1e10;
}
if (!closed && v_segment.y >= v_length)
{
v_miter.y = 1e10;
}
v_texcoord = vec2( u, v*w );
gl_Position = u_mvpMatrix*vec4(position,0.0,1.0);
}