1
2 // 绘制点
3 float point(vec2 p, vec2 center, float radius)
4 {
5 vec2 c2p = p-center;
6 float d = dot(c2p,c2p); // c2p的长度平方
7 if(d < radius * radius)
8 return 1.0;
9
10 return 0.0;
11 }
12
13
14 // 绘制直线
15 float line(vec2 p, vec2 a, vec2 b, float w)
16 {
17 vec2 a2b = b - a;
18 vec2 a2p = p - a;
19
20
21 // 因为投影 proj(a2p,a2b) 等于 dot(a2p,a2b) / length(a2b)
22 // 在把投影的值和a2b向量做比例关系。
23 // 所以proj(a2p,a2b)/length(a2b) 等于 dot(a2p,a2b)/dot(a2b,a2b)
24
25 float h = clamp( dot(a2p,a2b)/dot(a2b,a2b), 0.0, 1.0 );
26 vec2 p1 = mix( a, b, h );
27 if( length( p1 - p ) <= w )
28 return 1.0;
29
30 return 0.0;
31 }
32
33 void main(void)
34 {
35 vec2 uv = gl_FragCoord.xy / iResolution.y;
36 uv.x = uv.x + (1.0 - iResolution.x/iResolution.y)*0.5;
37
38 vec3 col = vec3(1.0,0,0) * point(uv, vec2(0.5,0.5), 0.05)
39 + vec3(0,0,1.0) * line(uv, vec2(0.0,0.25), vec2(1.0,0.55), 0.05);
40
41 gl_FragColor = vec4(col,1.0);
42 }