Is it possible to draw line thickness in a fragment shader?

后端 未结 4 1562
感情败类
感情败类 2020-12-08 01:28

Is it possible for me to add line thickness in the fragment shader considering that I draw the line with GL_LINES? Most of the examples I saw seem to access only the texels

4条回答
  •  被撕碎了的回忆
    2020-12-08 01:44

    Here's my approach. Let p1 and p2 be the two points defining the line, and let point be the point whose distance to the line you wish to measure. Point is most likely gl_FragCoord.xy / resolution;

    Here's the function.

    float distanceToLine(vec2 p1, vec2 p2, vec2 point) {
        float a = p1.y-p2.y;
        float b = p2.x-p1.x;
        return abs(a*point.x+b*point.y+p1.x*p2.y-p2.x*p1.y) / sqrt(a*a+b*b);
    }
    

    Then use that in your mix and smoothstep functions.

    Also check out this answer: https://stackoverflow.com/a/9246451/911207

提交回复
热议问题