public bool Intersect(Ray ray, out HitData hitData) { // Outsource this into the scene to enable accelerating data structures. Ray offsettedRay = new Ray(ray.Start - Position, ray.Direction); HitData closestHit = HitData.NoHit; HitData lastHit; triangles.ForEach((IIntersectable) => { if (IIntersectable.Intersect(offsettedRay, out lastHit)) { HitData offsettedHit = new HitData(lastHit.Position + Position, lastHit.Normal, lastHit.TextureCoords, lastHit.Material); if (!closestHit.exists() && lastHit.exists()) { closestHit = offsettedHit; return; } if (offsettedRay.Start.Distance(offsettedHit.Position) < offsettedRay.Start.Distance(closestHit.Position)) { closestHit = offsettedHit; } } }); hitData = closestHit; return(hitData.exists()); }
/// <summary> /// Intersect a ray with scene objects. Writes out hit data. /// </summary> /// <param name="ray">The ray that will be intersected with the scene.</param> /// <param name="hitData">The hit data of the closest hit that will be written out if there is an intersection. /// Will output HitData.NoHit otherwise and can be checked with the exists() method on the object.</param> /// <returns>Returns true, if there is an intersection with at least one scene object, false otherwise.</returns> public bool Intersect(Ray ray, out HitData hitData, bool isShadowRay = false) { // TODO: Use acceleration Data structures HitData closestHit = HitData.NoHit; HitData lastHit; ObjectList.ForEach((IIntersectable) => { if (isShadowRay && IIntersectable.CastShadows == false) { return; } if (IIntersectable.Intersect(ray, out lastHit)) { if (!closestHit.exists() && lastHit.exists()) { closestHit = lastHit; return; } if (ray.Start.Distance(lastHit.Position) < ray.Start.Distance(closestHit.Position)) { closestHit = lastHit; } } }); hitData = closestHit; return(hitData.exists()); }