<p>In a fragment shader, I need to draw a line at a given value of <em>u</em>. The shader is applied to a planar polygon mesh that's being drawn in the scene with a perspective camera. (It isn't a full-screen textured quad; that would be easy!)</p>

<p>The problem is, the thickness of the line is given in screen pixels, not in world units. How can I draw this line in my shader?</p>

<p>The shader is Cg, but a pseudocode answer outlining the technique is fine.</p>

CC BY-SA 3.0
2017-06-30T09:02:17.050Z
If you compute du/dx etc can you not check if the bounds of the pixel (including required line widths) include the required U value and set the colour accordingly?
Simon F
CC BY-SA 3.0
2017-07-10T20:50:04.140Z
Can't you simply convert the thickness into texture coordinates? I assume you know the dimension of your planar polygon and you know your starting point in world space? If so, simply offset from that point given your thickness, than transform it into UV coordinate. You can either multiple view-projection matrix and then divide by w (perspective divide) and then convert [-1, 1] to [0, 1] by times 0.5 and then add 0.5. This should get you a UV offset from the line start. You now have your thickness. You might want to do half the thickness and offset from center (the start).
ChaoSXDemon
<fragment-shader>
How can I draw a line whose position is given in UV space and its width in screen space?
Dan Hulme
# How can I draw a line whose position is given in UV space and its width in screen space?

(Asked by: username_0 on 2017-06-30)

In a fragment shader, I need to draw a line at a given value of *u*. The shader is applied to a planar polygon mesh that's being drawn in the scene with a perspective camera. (It isn't a full-screen textured quad; that would be easy!)

The problem is, the thickness of the line is given in screen pixels, not in world units. How can I draw this line in my shader?

The shader is Cg, but a pseudocode answer outlining the technique is fine.

**Tags:** fragment-shader

> **Comments:**
> **username_1**: If you compute du/dx etc can you not check if the bounds of the pixel (including required line widths) include the required U value and set the colour accordingly?  
> **username_2**: Can't you simply convert the thickness into texture coordinates? I assume you know the dimension of your planar polygon and you know your starting point in world space? If so, simply offset from that point given your thickness, than transform it into UV coordinate. You can either multiple view-projection matrix and then divide by w (perspective divide) and then convert [-1, 1] to [0, 1] by times 0.5 and then add 0.5. This should get you a UV offset from the line start. You now have your thickness. You might want to do half the thickness and offset from center (the start).  

---

## 0 answers

