#version 300 es
precision highp float;
uniform vec2 uResolution;
uniform vec2 uMouse;
out vec4 outColor;
const float PI = 3.1415926;
float segment(vec2 xy, vec2 from, vec2 to) {
vec2 v1 = xy - from;
vec2 v2 = to - from;
float orthlen = dot(v1, v2) / dot(v2, v2);
orthlen = clamp(orthlen, 0.0, 1.0);
vec2 orth = orthlen * v2;
return length(v1 - orth);
}
vec3 contour(float v, float interval) {
return abs(v) < 0.01
? vec3(0.0)
: mod(v, interval) < 0.01
? vec3(1.0)
: mix(vec3(1.0, 0.0, 1.0), vec3(0.0, 1.0, 1.0), atan(v) / PI + 0.5);
}
void main() {
vec2 pos = (2.0 * gl_FragCoord.xy - uResolution.xy) / min(uResolution.x, uResolution.y);
vec2 mouse = (2.0 * uMouse.xy - uResolution.xy) / min(uResolution.x, uResolution.y);
mouse.y *= -1.0;
float interval = 0.1;
float sdf = segment(pos, vec2(0.0), mouse);
outColor.rgb = vec3(contour(sdf, interval));
outColor.a = 1.0;
}