Last active
October 30, 2018 21:22
-
-
Save dmitrykolesnikovich/adf8221abd783deedc0aef2c57a3ab7b to your computer and use it in GitHub Desktop.
Simple line drawing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://www.shadertoy.com/view/XtlXz7 | |
#define resolution vec2(500.0, 500.0) | |
#define Thickness 0.03 | |
float drawLine(vec2 p1, vec2 p2) { | |
vec2 uv = gl_FragCoord.xy / resolution.xy; | |
float a = abs(distance(p1, uv)); | |
float b = abs(distance(p2, uv)); | |
float c = abs(distance(p1, p2)); | |
float d = sqrt(c*c + Thickness*Thickness); | |
if ( a >= d || b >= d ) | |
{ | |
if (distance(p1, uv) <= Thickness || | |
distance(p2, uv) <= Thickness) | |
return 1.0; | |
else | |
return 0.0; | |
} | |
float p = (a + b + c) * 0.5; | |
// median to (p1, p2) vector | |
float h = 2.0 / c * sqrt( p * ( p - a) * ( p - b) * ( p - c)); | |
if (h <= Thickness) | |
{ | |
return 1.0; | |
} | |
else | |
{ | |
return 0.0; | |
} | |
//return mix(1.0, 0.0, smoothstep(0.5 * Thickness, 1.5 * Thickness, h)); | |
} | |
void mainImage( out vec4 fragColor, in vec2 fragCoord ) | |
{ | |
vec2 p1 = vec2(0.9 + sin(iTime)*0.15, 0.1); | |
vec2 p2 = vec2(0.9 + cos(iTime)*0.15, 0.8); | |
fragColor = vec4(drawLine(p1, p2)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment