internal bool FirstPointOfIntersection(ref Ray ray, ref float tMax, Vector3[] vertices, out Vector3 pointOfIntersection)
{
var rayCopy = ray;
var dist = float.MaxValue;
VisitNodes(rootId, ref ray, ref tMax, (node) =>
{
if (node == null) return;
if (node.TriIndices.Length == 0) return;
for (var i = 0; i < node.TriIndices.Length; i++)
{
var tri = node.TriIndices[i];
var v0 = vertices[tri.Index0];
var v1 = vertices[tri.Index1];
var v2 = vertices[tri.Index2];
float newDist;
if (!Intersection.RayTriangleIntersect(rayCopy, v0, v1, v2, out newDist)) continue;
//Collision happens behind the startPos
if (newDist < 0.0f) continue;
dist = Math.Min(dist, newDist);
}
});
if (dist < tMax)
{
pointOfIntersection = ray.Position + dist*ray.Direction;
return true;
}
pointOfIntersection = Vector3.Zero;
return false;
}