Comments and answers for "Constant thickness line shader"
http://answers.unity.com/questions/1321780/constant-thickness-line-shader.html
The latest comments and answers for the question "Constant thickness line shader"Comment by Glurth on Glurth's answer
http://answers.unity.com/comments/1322844/view.html
cool! Especially like your intersection computation, efficient!Tue, 07 Mar 2017 15:24:31 GMTGlurthAnswer by le_duke
http://answers.unity.com/answers/1322842/view.html
The answer of this question in the comments by **Glurth**, Thanks !
I was wrong trying to compute the width of the line based on the distance with the vertex when the vertex is behind the camera.
Has **Glurth** said the correct way to compute the camera width in this case is to compute the line intersection with the near clip plane,we can then use this intersection coordinates as new coordinates for the vertex, resulting in having the good width.
The final result for the vertex shader is :
v2f vert (appdata v)
{
v2f o;
//extrusion direction
float extrusionDir = v.nextPoint.w;
//clip space pos
float4 currentProjected = UnityObjectToClipPos(float4(v.vertex.xyz, 1.0));
float4 nextProjected = UnityObjectToClipPos(float4(v.nextPoint.xyz, 1.0));
//get 2D screen space with W divide
float2 currentScreen = currentProjected.xy / currentProjected.w;
float2 nextScreen = nextProjected.xy / nextProjected.w;
//compute extrusion dir and normal
float2 dir = normalize(nextScreen.xy - currentScreen.xy);
float2 normal = float2(-dir.y /*_ProjectionParams.x*/, dir.x);
float perspectiveCancel = currentProjected.w /100;
//special case where vertex is behind the camera clip plane
if (currentProjected.w <0)
{
float3 normalizedClipSpaceDir = normalize(currentProjected.xyw - nextProjected.xyw);
float coef = -currentProjected.w / normalizedClipSpaceDir.z;
float3 intersectionPos = currentProjected.xyw + (normalizedClipSpaceDir * coef);
float4 clippedPos = float4(intersectionPos.x, intersectionPos.y, 0, intersectionPos.z);
currentProjected = clippedPos;
}
else
{
//apply extrusion
currentProjected += float4((normal * extrusionDir * _LineWidth), 0, 0) * perspectiveCancel;
}
o.vertex = currentProjected;
return o;
}Tue, 07 Mar 2017 15:18:47 GMTle_dukeComment by Glurth on Glurth's comment
http://answers.unity.com/comments/1322171/view.html
No, not zero. It's thickness will depend on the distance from the camera, at the intersection point. Compute it just like you would any point in front of the camera, after clipping it to the frustum.Mon, 06 Mar 2017 04:40:57 GMTGlurthComment by le_duke on le_duke's comment
http://answers.unity.com/comments/1322051/view.html
Thanks for the answer.
If I undestand what you said well, when the line intersect with the camera frustrum the line width at the intersection should be 0.
I'm going to work on that tomorrow and I'll let you know if I managed to correct the issue.Sun, 05 Mar 2017 22:14:34 GMTle_dukeComment by Glurth
http://answers.unity.com/comments/1321862/view.html
Alas even if you get that edge behind the camera to the correct length, consider what happens to the shape as the line passes by the camera: Your formula assigns a line width based on the distance from the camera. So a line that start/ends X units in front/behind the camera, would have the same width on each end. However, since the center of the line is closer to the camera, due to perspective, it will appear wider than both endpoints.
I suggest computing the intersection of your line and the camera frustum, then use those intersection points, to compute the line width, rather than trying to account for "behind camera widths". (I think you can use clipspace to compute the frustum intersections, it might prove a bit easier/faster.)Sun, 05 Mar 2017 15:07:21 GMTGlurth