#version 300 es
precision highp float;
uniform vec2 uResolution;
out vec4 outColor;
float line(vec2 xy, vec2 p1, vec2 p2, float thickness, float antispread) {
vec2 seg = p1 - p2;
vec2 per = vec2(seg.y, -seg.x);
float dist = dot(normalize(per), p1 - xy);
float thick = 1.0 / thickness;
float td = abs(dist * thick);
float l = 1.0 / td;
return dot(xy - p2, p1 - p2) > 0.0 && dot(xy - p1, p2 - p1) > 0.0
? pow(l, antispread)
: 0.0;
}
void main() {
vec2 pos = (2.0 * gl_FragCoord.xy - uResolution.xy) / min(uResolution.x, uResolution.y);
outColor.rgb = vec3(line(pos, vec2(0.0), vec2(0.5), 0.005, 3.0));
outColor.a = 1.0;
}
線分の判定
dist の指し示す点が線分上にある
- dot(xy - p1, p2 - p1) が正(なす角が 90 度より小)
- dot(xy - p2, p1 - p2) が正(なす角が 90 度より小)
dist の指し示す点は線分上にない
- dot(xy - p1, p2 - p1) が正(なす角が 90 度より小)
- dot(xy - p2, p1 - p2) が負(なす角が 90 度より大)
- dot(xy - p1, p2 - p1) が負(なす角が 90 度より大)
- dot(xy - p2, p1 - p2) が正(なす角が 90 度より小)
参考