@@ -4,6 +4,9 @@ precision mediump float;
44uniform vec2 u_stageSize;
55uniform float u_lineThickness;
66uniform float u_lineLength;
7+ // The X and Y components of u_penPoints hold the first pen point. The Z and W components hold the difference between
8+ // the second pen point and the first. This is done because calculating the difference in the shader leads to floating-
9+ // point error when both points have large-ish coordinates.
710uniform vec4 u_penPoints;
811
912// Add this to divisors to prevent division by 0, which results in NaNs propagating through calculations.
@@ -41,23 +44,26 @@ void main() {
4144 position.x *= u_lineLength + (2.0 * expandedRadius);
4245 position.y *= 2.0 * expandedRadius;
4346
44- // Center around first pen point
47+ // 1. Center around first pen point
4548 position -= expandedRadius;
4649
47- // Rotate quad to line angle
48- vec2 pointDiff = u_penPoints.zw - u_penPoints.xy ;
50+ // 2. Rotate quad to line angle
51+ vec2 pointDiff = u_penPoints.zw;
4952 // Ensure line has a nonzero length so it's rendered properly
5053 // As long as either component is nonzero, the line length will be nonzero
5154 // If the line is zero-length, give it a bit of horizontal length
5255 pointDiff.x = (abs (pointDiff.x) < epsilon && abs (pointDiff.y) < epsilon) ? epsilon : pointDiff.x;
5356 // The `normalized` vector holds rotational values equivalent to sine/cosine
5457 // We're applying the standard rotation matrix formula to the position to rotate the quad to the line angle
58+ // pointDiff can hold large values so we must divide by u_lineLength instead of calling GLSL's normalize function:
59+ // https://asawicki.info/news_1596_watch_out_for_reduced_precision_normalizelength_in_opengl_es
5560 vec2 normalized = pointDiff / max (u_lineLength, epsilon);
5661 position = mat2 (normalized.x, normalized.y, - normalized.y, normalized.x) * position;
57- // Translate quad
62+
63+ // 3. Translate quad
5864 position += u_penPoints.xy;
5965
60- // Apply view transform
66+ // 4. Apply view transform
6167 position *= 2.0 / u_stageSize;
6268 gl_Position = vec4 (position, 0 , 1 );
6369 #else
0 commit comments