Im making pixelated screen on Roblox studio with a bunch of raycasting
local cam = workspace.CurrentCamera
local size = cam.ViewportSize
local X = size.X
local Y = size.Y
local RunService = game:GetService("RunService")
local RayCastParams = RaycastParams.new()
RayCastParams.FilterDescendantsInstances = game.Players.LocalPlayer.Character:GetChildren()
RayCastParams.FilterType = Enum.RaycastFilterType.Exclude
local newframe = game.Players.LocalPlayer.PlayerGui.ScreenGui.Frames[1]
local GUI = game.Players.LocalPlayer.PlayerGui.ScreenGui
local index = 2
for Ypixel = 0,(Y/4)/newframe.Size.Y.Offset,1 do
for Xpixel = 0, (X/4)/newframe.Size.X.Offset,1 do
local cloneframe = newframe:Clone()
cloneframe.Name = index
index += 1
cloneframe.Position = UDim2.fromOffset(Xpixel * newframe.Size.X.Offset,Ypixel * newframe.Size.Y.Offset)
cloneframe.Parent = game.Players.LocalPlayer.PlayerGui.ScreenGui.Frames
newframe = cloneframe
end
end
local Frames = {}
for FrameName,Frame in pairs(GUI.Frames:GetChildren()) do
Frames[FrameName] = { Xpos = Frame.AbsolutePosition.X;
Ypos = Frame.AbsolutePosition.Y;}
end
local Rays = #GUI.Frames:GetChildren()
local Division = 2056
local RaysPerFrame = math.round(#GUI.Frames:GetChildren() / Division)
local LastFrameCasted = 0
function Raycast()
local FrameCasted = 0
for i = LastFrameCasted,Rays ,1 do
local ind = i
ind = math.clamp(i,1,Rays)
if LastFrameCasted >= Rays then
LastFrameCasted = 0
end
if FrameCasted >= RaysPerFrame then
break
end
FrameCasted += 1
LastFrameCasted += 1
local Xpos = Frames[ind].Xpos
local Ypos = Frames[ind].Ypos
local CamRay = cam:ScreenPointToRay(Xpos,Ypos)
local result = workspace:Raycast(CamRay.Origin, CamRay.Direction * 500, RayCastParams)
task.synchronize()
if result then
GUI.Frames[ind].BackgroundColor3 = result.Instance.Color
else
GUI.Frames[ind].BackgroundColor3 = Color3.new(0.615686, 0.815686, 0.815686)
end
end
end
local cam = game.Workspace.CurrentCamera
-- 🔁 Start loop
RunService.PreRender:Connect(Raycast)```
Im dividing the raycasting into few parts at the cost of the raycasting speed. Does someone has a better idea of making this faster but still maintaining the FrameTime at low
** You are now Level 4! **