So i am trying to make a raytracer with reflections, but there is an issue with combing colours when there are reflections in reflections. I’ve spent approximately 3 days trying to fix this on my own, i just don’t know what to do with the reflection colours in other reflections.
here’s a picture of the issue with the current code.
The problem is most obvious in the floor and on the almost no reflectance red ball.
How do I go about fixing this issue?
Help will be greatly appreciated as always.
function ProcessReflection(Raycast, IntersectPos, Direction, OriginalColor) -- Per pixel function
if Raycast.Instance.Reflectance > 0 then -- No need to create an extra ray if the part isn't reflective
local ReflectedDirection = Direction - (2 * Direction:Dot(Raycast.Normal) * Raycast.Normal) -- Relfect the direction.
ReflectedDirection = ReflectedDirection * RenderDistance -- New reflected direction
local ReflectionRay = workspace:Raycast(IntersectPos, ReflectedDirection, Params)
if ReflectionRay then
local MirroredColour = ReflectionRay.Instance.Color:Lerp(OriginalColor, (1 - Raycast.Instance.Reflectance))
MirroredColour = ProcessReflection(ReflectionRay, ReflectionRay.Position, ReflectedDirection, MirroredColour)
return MirroredColour
else
return SkyColour:Lerp(OriginalColor, 1 - Raycast.Instance.Reflectance) -- Sky
end
else
return OriginalColor -- Object isn't reflective, so don't reflect and return original colour
end
end