Need Help Optimising Ray Casting

Hey DevForum!

I wrote a very simple light Bounce simulator with ray casting, but I just can’t get it to run smoothly even when I tried implementing Parallel Luau. Am I doing something wrong or is it not possible to make this perform better.

Code:
(Parented it in under an Actor in StarterPlayer and parent a boolean value called debugEnabledValue to it)

local debrisService = game:GetService("Debris")
local runService = game:GetService("RunService")

local debounce = false
local targetBrightness 
local targetCFrame
local targetColor
local targetPosition
local targetRange

local times = 8
local intensity = 0.5
local updateTime = 5
local renderDistance = 50

local function gatherResults(lightPart, raycastParams)
	local results = {}
	for angle = 0, 360, 360/times do
		for angle2 = 0, 360, 360/times do
			--
			local x1 = math.cos(math.rad(angle))
			local x2 = math.sin(math.rad(angle2))
			local y1 = math.sin(math.rad(angle))
			local y2 = math.sin(math.rad(angle2))
			local z = math.cos(math.rad(angle2))
			local x = x1 * x2
			local y = y1 * y2
			--
			local origin = lightPart.Position
			local direction = Vector3.new(x,y,z)
			--
			local raycastResult = workspace:Raycast(origin, direction.Unit*5000)
			--
			if raycastResult then
				if script.debugEnabledValue.Value == true then
--[[
					local distance = (origin - raycastResult.Position).Magnitude
					local raypart = Instance.new("Part", workspace.ignoreFolder)
					raypart.Anchored = true
					raypart.CanCollide = false
					raypart.CastShadow = false
					raypart.CanQuery = false
					raypart.Position = raycastResult.Position
					raypart.CFrame = CFrame.lookAt(raycastResult.Position, origin) *CFrame.new(0, 0, -distance/2)
					raypart.Material = Enum.Material.Neon
					raypart.Name = tostring(angle).." : "..tostring(angle2)
					raypart.Size = Vector3.new(0.05,0.05,distance)
--]]
				end
				table.insert(results, raycastResult)
			end
		end
	end
	return results
end

runService.RenderStepped:Connect(function()
	if debounce == true then return end
	debounce = true
	for _, light in pairs(workspace:GetDescendants()) do
		if light:IsA("PointLight") then
			if not light:IsA("PointLight") or light:FindFirstAncestor(workspace:WaitForChild("ignoreFolder").Name) then continue end
			if not light:FindFirstAncestorWhichIsA("Part") or light.Enabled == false then continue end
			local lightPart = light:FindFirstAncestorWhichIsA("Part")
			--
			local distance = (workspace.CurrentCamera.CFrame.Position - lightPart.Position).Magnitude
			if distance > renderDistance then continue end
			--
			local raycastParams = RaycastParams.new()
			raycastParams.FilterDescendantsInstances = {lightPart}
			--
			task.desynchronize()
			local results = gatherResults(lightPart, raycastParams)
			if #results == 0 then continue end
			task.synchronize()
			for _, result:RaycastResult in pairs(results) do
				targetBrightness = intensity/ result.Distance*2
				targetCFrame = CFrame.lookAt(result.Position, lightPart.Position)
				if result.Instance:IsA("BasePart") then
					targetColor = result.Instance.Color
				else
					targetColor = light.Color
				end
				targetPosition = result.Position
				targetRange = result.Distance
				local bouncePart = Instance.new("Part", workspace.ignoreFolder)
				bouncePart.Anchored = true
				bouncePart.Position = targetPosition
				bouncePart.CFrame = targetCFrame
				bouncePart.CanCollide = false
				bouncePart.CanQuery = false
				bouncePart.CanTouch = false
				bouncePart.Size = Vector3.new(1,1,1)
				if script.debugEnabledValue == true then
					bouncePart.Transparency = 0
					local decal = Instance.new("Decal")
					decal.Face = Enum.NormalId.Front
					decal.Texture = "rbxassetid://106411906905300"
					decal.Parent = bouncePart
				else
					bouncePart.Transparency = 1
				end
				local bounceSpotLight = Instance.new("SpotLight", bouncePart)
				bounceSpotLight.Brightness = targetBrightness
				bounceSpotLight.Color = targetColor
				bounceSpotLight.Range = targetRange
				debrisService:AddItem(bouncePart, updateTime)
			end
		end
	end
	task.wait(updateTime)
	debounce = false
end)

Any help is very much appreciated and I hope you have a nice day or evening!

Hierarchy
image

Can you show me what your system is doing?

Parrarel luau is only good on client, as Roblox need 6+ players to assign more cores

Also even if you’re raycasting on client, your system uses only 1 actor, which means only one separate thread will work, this isn’t bad, but makes parrarel luau useless here

What you should do is reduce raycasting as much as possible, best way to do this is by calculating them on client, and after that only checking some propertiess on server like if they hitted near target or if player could even do this move

Raycasting overall is pretty much well optimized, remember you can always try to not use it, for instance far away objects may be visual only, without raycasts


It shoots a few rays around to calculate light simulation, at the hit Positions it creates a spotlight

Okay, I’ll try to optimize your system this afternoon

No way, that would awesome!
Thanks in advance

Just quickly:

  1. GetDescendants is traversing the entire workspace tree every frame. You might want to use CollectionService to tag specific lights instead. This can also replace your ignoreFolder.

  2. Parallel lua is not likely to help much here, and you should be able to get this performant on a single thread. I wouldn’t turn to parallel lua until you have optimized the single-threaded version.

  3. You can use the MicroProfiler | Documentation - Roblox Creator Hub to see exactly how long each section is taking to run, and use that to inform your optimization.

Ok i’ll try using tags, thank you!

it done


LightBounce.rbxm (4.5 KB)

Yo dude, no way! Currently at school, ill try it out at home.
Thank you so much!

Bro thank you so much, its insane how fast it renders now. Can you please explain what you did because i am so interested in it now I see how powerful it really is.