Implementing antialiased lines material

I’m trying to make a material for drawing AAed lines. The idea is to:

  1. Interpolate the screen coordinate of the vertices along the length of the line
  2. use the distance between the screen coordinate and the frag coordinate to find a suitable alpha value

I modified the unshaded.vert (I assumed a resolution of 1920x1080):

varying vec2 screenPos;
screenPos = vec2( ((gl_Position.x/gl_Position.w)+1.)*1920./2. , ((gl_Position.y/gl_Position.w)+1.)*1080./2.);

And unshaded.frag:

gl_FragColor = vec4(,1-distance(screenPos,gl_FragCoord.xy)/4.5);

The results look good to me, except that lines which have a component normal to the viewing plane are weirdly tapered.

This lead me to think that transformation to screen coordinates was somehow wrong. But I haven’t been able to figure out how. Or maybe there is something more fundamental that I don’t understand?

Thanks in advance for you time.

screen pos is from 0 to 1, gl_FragCoord is from -1 to 1.
you need to transpose them in the same space like

vec2 fragCoord = gl_FragCoord.xy * 0.5 + 0.5;

Also you can have the real screen resolution injected to you shader by declaring

 uniform vec2 g_Resolution;

x is width, y is height.

1 Like

Ugh. I’m sorry it took so long to get back to you.

I think that I have my coordinates in the same domain (when calculating screenPos I add 1 and then divide by 2 from gl_position). If something that serious were off I don’t think I could get it to work weel enough for the screencap.

If it’s helpful, I can make a test class.

yep always helps