/*
//unity中的测试代码
void Cula(Mesh mesh1,Mesh mesh2)
{
int[] indexs = mesh1.triangles;
Vector3[] v1s = mesh1.vertices;
int length = v1s.Length;
int indexlenght = indexs.Length;
UnityEngine.Debug.Log(length);
UnityEngine.Debug.Log(indexlenght);
Vector3[] vs = mesh2.vertices;
Vector3[] normalBody = mesh2.normals;
int length2 = vs.Length;
for (int i = 0; i < length2; i++)
{
for (int j = 0; j < indexlenght;)
{
isEnter(vs[i], normalBody[i], v1s[indexs[j]], v1s[indexs[j + 1]], v1s[indexs[j + 2]]);
j += 3;
}
}
}
*/
//三角检测
bool isEnter(Vector3 o,Vector3 d,Vector3 v0, Vector3 v1, Vector3 v2)
{
Vector3 e1 = v1 - v0;
Vector3 e2 = v2 - v0;
Vector3 t = o - v0;
Vector3 p = Vector3.Cross(d, e2);
Vector3 q = Vector3.Cross(t, e1);
float pe1 = 1 / (Vector3.Dot(p, e1));
float tt = pe1 * Vector3.Dot(q, e2);
float u = pe1 * Vector3.Dot(p, t);
float v = pe1 * Vector3.Dot(q, d);
if (tt >= 0 && u >= 0 && v >= 0 && u + v <= 1)
{
return true;
}
return false;
}
基于Tomas Möller的快速检测算法的C#实现,供参考。
[1]Tomas Möller, Trumbore B . Fast, Minimum Storage Ray-Triangle Intersection[J]. 2005.