public IntersectInfo GetClosestIntersection(Ray ray) { List <IntersectInfo> allPrimary = new List <IntersectInfo>(); Ray checkFrontAndBacks = new Ray(ray); checkFrontAndBacks.intersectionType = IntersectionType.Both; foreach (IntersectInfo info in primary.IntersectionIterator(checkFrontAndBacks)) { allPrimary.Add(info); } if (allPrimary.Count == 0) { // We did not hit the primary object. We are done. The subtract object does not mater. return(null); } allPrimary.Sort(new CompareIntersectInfoOnDistance()); // we hit the primary object, did we hit the subtract object before (within error) hitting the primary. List <IntersectInfo> allSubtract = new List <IntersectInfo>(); foreach (IntersectInfo info in subtract.IntersectionIterator(checkFrontAndBacks)) { allSubtract.Add(info); } if (allSubtract.Count == 0) { // we did not hit the subtract so return the first primary return(allPrimary[0]); } allSubtract.Sort(new CompareIntersectInfoOnDistance()); List <IntersectInfo> result = new List <IntersectInfo>(); IntersectInfo.Subtract(allPrimary, allSubtract, result); if (result.Count > 0) { return(result[0]); } return(null); }