intersectTriangle method
Implementation
Vector3? intersectTriangle(Vector3 a, Vector3 b, Vector3 c, bool backfaceCulling, Vector3 target) {
// Compute the offset origin, edges, and normal.
// from http://www.geometrictools.com/GTEngine/Include/Mathematics/GteIntrRay3Triangle3.h
_edge1.subVectors(b, a);
_edge2.subVectors(c, a);
_normal.crossVectors(_edge1, _edge2);
// Solve Q + t*D = b1*E1 + b2*E2 (Q = kDiff, D = ray direction,
// E1 = kEdge1, E2 = kEdge2, N = Cross(E1,E2)) by
// |Dot(D,N)|*b1 = sign(Dot(D,N))*Dot(D,Cross(Q,E2))
// |Dot(D,N)|*b2 = sign(Dot(D,N))*Dot(D,Cross(E1,Q))
// |Dot(D,N)|*t = -sign(Dot(D,N))*Dot(Q,N)
var ddN = direction.dot(_normal);
int sign;
if (ddN > 0) {
if (backfaceCulling) return null;
sign = 1;
} else if (ddN < 0) {
sign = -1;
ddN = -ddN;
} else {
return null;
}
_diff.subVectors(origin, a);
var ddQxE2 = sign * direction.dot(_edge2.crossVectors(_diff, _edge2));
// b1 < 0, no intersection
if (ddQxE2 < 0) {
return null;
}
var ddE1xQ = sign * direction.dot(_edge1.cross(_diff));
// b2 < 0, no intersection
if (ddE1xQ < 0) {
return null;
}
// b1+b2 > 1, no intersection
if (ddQxE2 + ddE1xQ > ddN) {
return null;
}
// Line intersects triangle, check if ray does.
var qdN = -sign * _diff.dot(_normal);
// t < 0, no intersection
if (qdN < 0) {
return null;
}
// Ray intersects triangle.
return at(qdN / ddN, target);
}