TerrainDisplay.Collision.BSPTree.FirstPointOfIntersection C# (CSharp) Method

FirstPointOfIntersection() private method

private FirstPointOfIntersection ( Ray &ray, float &tMax, System.Vector3 vertices, System.Vector3 &pointOfIntersection ) : bool
ray Ray
tMax float
vertices System.Vector3
pointOfIntersection System.Vector3
return bool
        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;
        }