-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
Copy pathline.vert
216 lines (189 loc) · 7.82 KB
/
line.vert
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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
Part of the Processing project - http://processing.org
Copyright (c) 2012-15 The Processing Foundation
Copyright (c) 2004-12 Ben Fry and Casey Reas
Copyright (c) 2001-04 Massachusetts Institute of Technology
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation, version 2.1.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/
#define PROCESSING_LINE_SHADER
precision mediump int;
uniform mat4 uModelViewMatrix;
uniform mat4 uProjectionMatrix;
uniform float uStrokeWeight;
uniform bool uUseLineColor;
uniform vec4 uMaterialColor;
uniform vec4 uViewport;
uniform int uPerspective;
uniform int uStrokeJoin;
IN vec4 aPosition;
IN vec3 aTangentIn;
IN vec3 aTangentOut;
IN float aSide;
IN vec4 aVertexColor;
OUT vec4 vColor;
OUT vec2 vTangent;
OUT vec2 vCenter;
OUT vec2 vPosition;
OUT float vMaxDist;
OUT float vCap;
OUT float vJoin;
vec2 lineIntersection(vec2 aPoint, vec2 aDir, vec2 bPoint, vec2 bDir) {
// Rotate and translate so a starts at the origin and goes out to the right
bPoint -= aPoint;
vec2 rotatedBFrom = vec2(
bPoint.x*aDir.x + bPoint.y*aDir.y,
bPoint.y*aDir.x - bPoint.x*aDir.y
);
vec2 bTo = bPoint + bDir;
vec2 rotatedBTo = vec2(
bTo.x*aDir.x + bTo.y*aDir.y,
bTo.y*aDir.x - bTo.x*aDir.y
);
float intersectionDistance =
rotatedBTo.x + (rotatedBFrom.x - rotatedBTo.x) * rotatedBTo.y /
(rotatedBTo.y - rotatedBFrom.y);
return aPoint + aDir * intersectionDistance;
}
void main() {
// Caps have one of either the in or out tangent set to 0
vCap = (aTangentIn == vec3(0.)) != (aTangentOut == (vec3(0.)))
? 1. : 0.;
// Joins have two unique, defined tangents
vJoin = (
aTangentIn != vec3(0.) &&
aTangentOut != vec3(0.) &&
aTangentIn != aTangentOut
) ? 1. : 0.;
vec4 posp = uModelViewMatrix * aPosition;
vec4 posqIn = uModelViewMatrix * (aPosition + vec4(aTangentIn, 0));
vec4 posqOut = uModelViewMatrix * (aPosition + vec4(aTangentOut, 0));
float facingCamera = pow(
// The word space tangent's z value is 0 if it's facing the camera
abs(normalize(posqIn-posp).z),
// Using pow() here to ramp `facingCamera` up from 0 to 1 really quickly
// so most lines get scaled and don't get clipped
0.25
);
// Moving vertices slightly toward the camera
// to avoid depth-fighting with the fill triangles.
// This prevents popping effects due to half of
// the line disappearing behind the geometry faces.
float zOffset = mix(-0.00045, -1., facingCamera);
posp.z -= zOffset;
posqIn.z -= zOffset;
posqOut.z -= zOffset;
vec4 p = uProjectionMatrix * posp;
vec4 qIn = uProjectionMatrix * posqIn;
vec4 qOut = uProjectionMatrix * posqOut;
vCenter = p.xy;
// formula to convert from clip space (range -1..1) to screen space (range 0..[width or height])
// screen_p = (p.xy/p.w + <1,1>) * 0.5 * uViewport.zw
// prevent division by W by transforming the tangent formula (div by 0 causes
// the line to disappear, see https://github.com/processing/processing/issues/5183)
// t = screen_q - screen_p
//
// tangent is normalized and we don't care which aDirection it points to (+-)
// t = +- normalize( screen_q - screen_p )
// t = +- normalize( (q.xy/q.w+<1,1>)*0.5*uViewport.zw - (p.xy/p.w+<1,1>)*0.5*uViewport.zw )
//
// extract common factor, <1,1> - <1,1> cancels out
// t = +- normalize( (q.xy/q.w - p.xy/p.w) * 0.5 * uViewport.zw )
//
// convert to common divisor
// t = +- normalize( ((q.xy*p.w - p.xy*q.w) / (p.w*q.w)) * 0.5 * uViewport.zw )
//
// remove the common scalar divisor/factor, not needed due to normalize and +-
// (keep uViewport - can't remove because it has different components for x and y
// and corrects for aspect ratio, see https://github.com/processing/processing/issues/5181)
// t = +- normalize( (q.xy*p.w - p.xy*q.w) * uViewport.zw )
vec2 tangentIn = normalize((qIn.xy*p.w - p.xy*qIn.w) * uViewport.zw);
vec2 tangentOut = normalize((qOut.xy*p.w - p.xy*qOut.w) * uViewport.zw);
vec2 curPerspScale;
if(uPerspective == 1) {
// Perspective ---
// convert from world to clip by multiplying with projection scaling factor
// to get the right thickness (see https://github.com/processing/processing/issues/5182)
// The y value of the projection matrix may be flipped if rendering to a Framebuffer.
// Multiplying again by its sign here negates the flip to get just the scale.
curPerspScale = (uProjectionMatrix * vec4(1, sign(uProjectionMatrix[1][1]), 0, 0)).xy;
} else {
// No Perspective ---
// multiply by W (to cancel out division by W later in the pipeline) and
// convert from screen to clip (derived from clip to screen above)
curPerspScale = p.w / (0.5 * uViewport.zw);
}
vec2 offset;
if (vJoin == 1.) {
vTangent = normalize(tangentIn + tangentOut);
vec2 normalIn = vec2(-tangentIn.y, tangentIn.x);
vec2 normalOut = vec2(-tangentOut.y, tangentOut.x);
float side = sign(aSide);
float sideEnum = abs(aSide);
// We generate vertices for joins on either side of the centerline, but
// the "elbow" side is the only one needing a join. By not setting the
// offset for the other side, all its vertices will end up in the same
// spot and not render, effectively discarding it.
if (sign(dot(tangentOut, vec2(-tangentIn.y, tangentIn.x))) != side) {
// Side enums:
// 1: the side going into the join
// 2: the middle of the join
// 3: the side going out of the join
if (sideEnum == 2.) {
// Calculate the position + tangent on either side of the join, and
// find where the lines intersect to find the elbow of the join
vec2 c = (posp.xy/posp.w + vec2(1.,1.)) * 0.5 * uViewport.zw;
vec2 intersection = lineIntersection(
c + (side * normalIn * uStrokeWeight / 2.),
tangentIn,
c + (side * normalOut * uStrokeWeight / 2.),
tangentOut
);
offset = (intersection - c);
// When lines are thick and the angle of the join approaches 180, the
// elbow might be really far from the center. We'll apply a limit to
// the magnitude to avoid lines going across the whole screen when this
// happens.
float mag = length(offset);
float maxMag = 3. * uStrokeWeight;
if (mag > maxMag) {
offset *= maxMag / mag;
}
} else if (sideEnum == 1.) {
offset = side * normalIn * uStrokeWeight / 2.;
} else if (sideEnum == 3.) {
offset = side * normalOut * uStrokeWeight / 2.;
}
}
if (uStrokeJoin == STROKE_JOIN_BEVEL) {
vec2 avgNormal = vec2(-vTangent.y, vTangent.x);
vMaxDist = abs(dot(avgNormal, normalIn * uStrokeWeight / 2.));
} else {
vMaxDist = uStrokeWeight / 2.;
}
} else {
vec2 tangent = aTangentIn == vec3(0.) ? tangentOut : tangentIn;
vTangent = tangent;
vec2 normal = vec2(-tangent.y, tangent.x);
float normalOffset = sign(aSide);
// Caps will have side values of -2 or 2 on the edge of the cap that
// extends out from the line
float tangentOffset = abs(aSide) - 1.;
offset = (normal * normalOffset + tangent * tangentOffset) *
uStrokeWeight * 0.5;
vMaxDist = uStrokeWeight / 2.;
}
vPosition = vCenter + offset;
gl_Position.xy = p.xy + offset.xy * curPerspScale;
gl_Position.zw = p.zw;
vColor = (uUseLineColor ? aVertexColor : uMaterialColor);
}