public ITraceInformation CalculateIntersection(Ray line) { ITraceInformation bestHit = null; foreach (var triangle in GetTriangles()) { var hit = triangle.CalculateIntersection(line); if (!hit.Intersects) { continue; } if (bestHit == null) { bestHit = hit; continue; } if (hit.Distance < bestHit.Distance) { bestHit = hit; } } if (bestHit == null) { return(new NoIntersection()); } return(new Intersection(bestHit.IntersectionPosition, line, Material, this) { Normal = bestHit.Normal }); }
public ITraceInformation Trace(Ray ray) { ITraceInformation closestHit = null; foreach (var entity in this) { var hitInfo = entity.CalculateIntersection(ray); if (!hitInfo.Intersects) { continue; } if (closestHit == null) { closestHit = hitInfo; continue; } if (hitInfo.Distance < closestHit.Distance) { closestHit = hitInfo; } } return(closestHit ?? new NoIntersection()); }