I have a line(A, B) and a triangle(P0, P1, P2) somewhere in 3D space. In other words, I have 3 points ([x,y,z] each) for the triangle, and two points (also [x,y,z]) for the line. One point of line lie inside triangle(A or B). The main goal is find intersection on triangle edge that forms part of line (A, B) on the screen.
What I do:
1. Calculate plane Normal from line start (A), line end (B) and Camera position:
Vector3 Normal = Vector3.Cross(A - cameraPos, B - cameraPos);
2. Find two intersections by checking each edge of triangle:
bool IsPlaneIntersectLine(Vector3 Normal, Vector3 A, Vector3 B, Vector3 EdgePoint1, Vector3 EdgePoint2, out Vector3 Intersection)
{
float dotProduct = Vector3.Dot(Normal, (EdgePoint1 - EdgePoint2));
if (dotProduct == 0f)
return false;
float dot1 = Vector3.Dot(Normal, (A - EdgePoint2));
float distance = dot1 / dotProduct;
if (distance > 1f || distance < 0f)
return false;
Intersection = EdgePoint2 + distance * (EdgePoint1 - EdgePoint2);
return true;
}
Vector3 intersection1, intersection2, intersection3;
bool isEdge1Intersected = IsPlaneIntersectLine(Normal, A, B, triangle.P0, triangle.P1, out intersection1);
bool isEdge2Intersected = IsPlaneIntersectLine(Normal, A, B, triangle.P1, triangle.P2, out intersection2);
bool isEdge3Intersected = IsPlaneIntersectLine(Normal, A, B, triangle.P2, triangle.P0, out intersection3);
As a result I have two intersections, but only one is correct - the intersection, that is a part of line (A, B) on screen. Also I have limitations - I can't convert this points to screen space for checking - which of two intersection points is between A and B.
Also I tried check dot product between line vector and intersection vector:
float dot = Vector3.Dot((Intersection - A), (B - A));
if (dot < 0f)
{
return false;
}
But there is some triangles/edges that doesn't suitable for this condition.
How to find one intersection on triangle edge that forms part of line (A, B) on the screen?