Background (Before learning algorithms)
Detection of intersection between ray and triangle is a common problem in game programming, the most typical application is Picking, this paper introduces a common method, this method is also used in DirectX, the method is fast, and less storage space. The theory is explained first, and the corresponding code implementation and display in Unity are shown at the end of the article. The simple and intuitive method is to determine whether the rays intersect the plane in which the triangle is located, and if so, whether the intersection is within the triangle. But this method is not very efficient because it computes the plane of the triangle. Moller-trumbore ray triangle intersection algorithm is a fast method to calculate the intersection point between ray and triangle in space. By vector and matrix calculation, the intersection point and barycenter coordinate can be obtained quickly without the need to predict the plane equation containing triangle. It is used in computer graphics to realize ray tracking calculation involving triangular meshes. The algorithm is named after the inventors, TomasMoller and Ben Trumbore.
Parameters that
Suppose the equation of Ray Ray=O+td(O is the starting point, D is the direction of Ray (direction vector), t is the weight), a point starts from the starting point O, moves any length along the direction D, and obtains the end point R. According to the different value of T, the obtained R value is also different, all these different R values constitute the whole Ray. The three vertices of a triangle, P0, P1, P2. U is the weight of P1, v is the weight of P2, and 1- U – V is the weight of P0, and you can kind of think of it as moving some distance along the edge AC, and then moving some distance along the edge AB, and then finding their sum. How far to move is controlled by the parameters U and V, and the mathematical meaning expressed is the equation of the triangle and all the points inside it.
Derivation process
Two important theorems
Clem’s rule for solving linear equations
D, E1, E2 are determinants containing three parameters, which can be expressed as
Vector mixed product
Let’s put these equations together
reduction
Cram’s rule
The mixed product in the denominator, using the mixed product theorem, the minus sign in front of D, cancels out
make
Molecular part:
make
Is equal to the original type
so
Similarly, the other two parameters u and v can be deduced
conclusion
In the end, we can calculate the three parameters t, U and V through the known data, and judge whether the intersection is through the range limit of the three parameters. If the intersection is not satisfied, it is not intersected, if it is satisfied, it is intersected
Code implementation
// Vector3 a b c triangle vertexs
// orig is ray original point, dir is direction vector
bool rayTriangleIntersect(Vector3 orig, Vector3 dir,
Vector3 a, Vector3 b, Vector3 c, float t, float b1, float b2)
{
bool isIn = false;
Vector3 E1 = b - a;
Vector3 E2 = c - a;
Vector3 S = orig - a;
Vector3 S1 = Vector3.Cross(dir, E2);
Vector3 S2 = Vector3.Cross(S, E1);
// Common coefficient
float coeff = 1.0 f / Vector3.Dot(S1, E1);
t = coeff * Vector3.Dot(S2, E2);
b1 = coeff * Vector3.Dot(S1, S);
b2 = coeff * Vector3.Dot(S2, dir);
Debug.Log($"t = {t}, b1 = {b1}, b2 = {b2}");
if (t >= 0 && b1 >= 0 && b2 >= 0 && (1 - b1 - b2) >= 0)
{
isIn = true;
}
return isIn;
}
Copy the code
Demo effects in Unity
When a ray intersects a triangle, the triangle material turns red. The ray is in the form of LineRenderer. The triangle uses a script to edit the vertex of the material
Project Address:Github.com/shadow-lr/R…
digression
Although the detection of intersection between ray and triangle can be used to realize Picking, most programs do not adopt this method, because this method is very inefficient. We can imagine that in a large 3D game, the number of triangles in a certain model is likely to be millions. In this case, Finding the intersection of every triangle on the model is extremely time consuming. Therefore, the generally feasible method is to use the bounding sphere and bounding box (AABB, OBB, FDH) instead, to calculate the smallest sphere or equation that can accommodate the model, as long as the ray and the bounding sphere or bounding box intersection can be determined, there is a certain error in knowledge accuracy, but enough to meet the needs of most programs.