Hi! I’m currently working on a custom line renderer for my 3d editor. The idea is to get constant width lines in pixel units. I’m using a geometry shader to create a triangle strip from a line by offseting the vertices positions a little in screen space according to the desired width.

The line gets rendered correctly when I’m far from it and everything looks good but when I get too close to it and the line intersects (I think) with the near plane, I start getting errors. I assume it’s due to the division by the w component. I’ve been trying to fix it for two whole days now and I don’t know how to do make it work. Any ideas?

I recorded a video but I’m not allowed to embed it. This is the YouTube link: https://youtu.be/y-JnEvyeIG0
Sorry for that.

It’s not the near plane, it’s the W=0 plane (i.e. the Z=0 plane in eye space). If you project a point that’s behind the viewpoint, it’s reflected in the viewpoint. E.g. (1,1,-1) has the same screen-space position as (-1,-1,1).

In short, you can’t (meaningfully) convert clip-space coordinates to screen space until after you’ve clipped the line.

You can obtain the screen-space direction vector directly as [w1.x2-w2.x1, w1.y2-w2.y1], then the perpendicular from that. Try adding/subtracting the perpendicular scaled by W to each point (in clip space). I.e.

Now it intersects with the camera correctly but when I’m too close to the line (I assume it’s still when it intersects the W=0 plane), the constant width stops working and it gets bigger and bigger on one side only.

Here’s a video showing what I mean: https://youtu.be/8VgtWpe8p4s

Thank you very much! I’m not sure I fully understand the solution but it worked wonderfully!
I’ll leave the final code here in case someone is trying to achieve something similar.