Global Illumination reflection

Greetings!
I’m trying to create Global Illumination with the follow code and I need help on reflecting the rays when hit and stopping once they reach the max bounce limit!

local config = {};
config.MaxBounce = 5;


for i,v in pairs(workspace:GetDescendants()) do
	if v:IsA("Light") then
		local part = v.Parent;
		local orig = part.Position;
		local dir = part.CFrame.LookVector*5000;
		local ray = Ray.new(orig, dir);
		local hitPart, hitPosition = workspace:FindPartOnRay(ray)

		if hitPart then
			local p = Instance.new('Part')
			p.Transparency = 1
			p.CastShadow = false
			p.CanCollide = false
			p.Anchored = true
			p.Position = hitPosition
			p.CFrame = CFrame.new(hitPosition, part.Position)
			local x = Instance.new('SpotLight')
			x.Parent = p
			p.Parent = workspace.GI
		else
			print('no hit')
		end
	end
end

Thanks!

2 Likes

here’s a simple function for reflecting stuff

local function Reflect(surfaceNormal, raynormal)
	return raynormal- (2 * raynormal:Dot(surfaceNormal) * surfaceNormal)
end
2 Likes