Hi, I’m attempting to create lines of a fixed width. I am creating a mesh using the Lines topology, and then using a geometry shader to create a quad from each line. My goal is to create lines that are of a constant width, ignoring perspective. Here’s what I’ve got so far:
Shader "Unlit/ThickLine"
{
Properties
{
_Color("Main Color", Color) = (1,1,1,1)
}
SubShader
{
Pass
{
CGPROGRAM
#pragma target 5.0
#pragma vertex vert
#pragma fragment frag
#pragma geometry geo
#include "UnityCG.cginc"
struct v2g
{
float4 vertex : SV_POSITION;
};
struct g2f
{
float4 pos: POSITION;
};
fixed4 _Color;
v2g vert (float4 position : POSITION)
{
v2g o;
o.vertex = position;
return o;
}
[maxvertexcount(4)]
void geo(line v2g v[2], inout TriangleStream<g2f> ts)
{
float weight = 1;
float4 p1 = mul(UNITY_MATRIX_MVP, v[0].vertex);
float4 p2 = mul(UNITY_MATRIX_MVP, v[1].vertex);
float4 dir = normalize(p2 - p1);
float4 perp = float4(dir.y, -dir.x, 0, 0);
float4 v1_top = p1 + perp * weight;
float4 v1_bot = p1 - perp * weight;
float4 v2_top = p2 + perp * weight;
float4 v2_bot = p2 - perp * weight;
g2f o1;
o1.pos = v1_top;
ts.Append(o1);
g2f o2;
o2.pos = v1_bot;
ts.Append(o2);
g2f o3;
o3.pos = v2_top;
ts.Append(o3);
g2f o4;
o4.pos = v2_bot;
ts.Append(o4);
}
fixed4 frag (g2f i) : SV_Target
{
return _Color;
}
ENDCG
}
}
}
Here’s what I think I’m doing: I’m converting both the line points into view space. From there I get the direction from one point to another, then the direction perpendicular to this. Now I can build the four points of the quad to represent this line. Since I’m doing the offsets in view space, this should create a line of constant width. Here’s what I’m getting:
(With the camera right beside the starting point).
I’m not sure what I’m doing wrong, but it looks like the offsets are being applied in world space…but I’m not sure how since I clearly transform the vertices beforehand.
Thanks for any help,
Erik

