public static Hit RayTracePrimitives(Ray ray, Hit skipHit, IEnumerable <Primitive> primitives, BVH <Primitive> Accelerator) { Hit hit = default; // If we have an accelerator, filter down to only the needed primitives. #if BVH if (Accelerator != null) { IEnumerable <BoundingIntersection <Primitive> > intersections = Accelerator.IntersectLeaves(ray); BoundingIntersection <Primitive> previous = null; foreach (BoundingIntersection <Primitive> current in intersections) { if (previous != null && current.Near > previous.Far) { break; } Hit currentHit = current.Object.RayTrace(ray, skipHit); if (currentHit != default && (hit == default || currentHit.Distance < hit.Distance)) { hit = currentHit; previous = current; } } } else #endif { foreach (Primitive prim in primitives) { Hit currentHit = prim.RayTrace(ray, skipHit); #if DEBUG prim.RayTrace(ray, skipHit); #endif if (currentHit != default && (hit == default || currentHit.Distance < hit.Distance)) { hit = currentHit; } } } return(hit); }