#faster raycasting

1 messages · Page 1 of 1 (latest)

keen lynx
#

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
soft rivet
keen lynx
crisp trout
#

sorry i didn't bother reading ur code much at all

#

give me a second to look it over

keen lynx
soft rivet
#

U can do as many as u want but more does not equal better

crisp trout
#

wait why are u using task.synchronize?

keen lynx
#

ik the parrarel luau is the solutions

crisp trout
#

ah

keen lynx
#

is there a faster way to make it with just 1 script

#

like the raycasting is faster

#

it updates faster

soft rivet
#

ur raycasting once per frame?

keen lynx
soft rivet
#

raycast the amount of pixels per frame

keen lynx
#

if i want it 25% of total frame per frame then it raycast 25% per frame

crisp trout
#

idk, the code looks like it re cloning the pixel grid every time u make a tweak but u said otherwise

keen lynx
#

it finishes the raycast then it updates again

soft rivet
#

the code is weird asf ngl

crisp trout
#

imma leave it to messy

#

he is smarter than me

keen lynx
crisp trout
#

but u r most def rebuilding for no reason

keen lynx
#

not that good at naming

soft rivet
#

comment out task.sync

crisp trout
soft rivet
#

yeah once he implements it but its not implemented rn

crisp trout
#

oh ok

keen lynx
#

how can i make it updates faster with still maintaining a good frametime beside parrarel luau

#

so once i do parrarel luau

#

it becomes even faster

soft rivet
#

It should be way faster than this

#

something is messed up with ur code

keen lynx
soft rivet
crisp trout
#

i would fix it

#

but i dont have access to the gui

keen lynx
#

im applying the color

soft rivet
#

if he is then thats the issue but from what i see he is just coloring it

#

not rebuilding the frame

crisp trout
#

its hard to explain

boreal coyoteBOT
#

studio** You are now Level 4! **studio

keen lynx
keen lynx
#

tell me im wrong

crisp trout
#

sorry i cant really help because of the gui, see what parallel does for u

keen lynx
#

ok

crisp trout
# keen lynx ok

oh nvm i was hallucinating you actually do it once, prolly ur table lookups

crisp trout
#

u index through the gui .frames

#

u constantly check the folder, prolly does slow it down

keen lynx
#

so what should i do

#

i need to change the color

crisp trout
#

make an array once at teh start

#

containg the frame and its pos

keen lynx
#

i did

#
local Frames = {}

for FrameName,Frame in pairs(GUI.Frames:GetChildren()) do
    Frames[FrameName] = { Xpos = Frame.AbsolutePosition.X;
        Ypos = Frame.AbsolutePosition.Y;}
end
crisp trout
#

im genuinely blind ic

#

alr time to go to bed

soft rivet
#

What is the resolution

#

@keen lynx

keen lynx
#

my resolution?

soft rivet
#

How many pixels

keen lynx
soft rivet
#

so many frames?

keen lynx
# soft rivet so many frames?

customizeable local Division = 2056 local RaysPerFrame = math.round(#GUI.Frames:GetChildren() / Division) for Ypixel = 0,(Y/1)/newframe.Size.Y.Offset,1 do for Xpixel = 0, (X/1)/newframe.Size.X.Offset,1 do

#

(Y/1) is full frame

soft rivet
keen lynx
soft rivet
#

yeah

keen lynx
#

yea it will lag

soft rivet
#

You need to use parallel lua to make it less laggy

#

but my code isn't as janky as yours but

keen lynx
#

hv u ever done this?

#

what did u do to make it

#

less laggy?

#

still parrarel luau?

#

ig parrarel luau is the only solution..

soft rivet
#

discord thinks my code is virus

soft rivet
#

let me just check something

keen lynx
#

ok

soft rivet
#

Okay so my code runs poorly not because of the raycasts

#

it runs poorly because of how we access the frame

#

that is what is slowing it down and it might be the same issue with you

keen lynx
#

in this ? ```lua
if result then

        GUI.Frames[ind].BackgroundColor3 =  result.Instance.Color
    else
        GUI.Frames[ind].BackgroundColor3 = Color3.new(0.615686, 0.815686, 0.815686)
    end    ```
soft rivet
#

its a table right?

#

{}?

keen lynx
soft rivet
#

yeah

#

or is it a frame with children

keen lynx
#

nope

#

it's a folder named frame

#

with cloned frames inside it

soft rivet
#

yeah thats what is causing the lag and its the same issue for me even when i use a table

#

And I am not sure what a better way is

#

its not raycasting that is slowing you down, its looping through all the pixels

#

and i suppose the best way is to use parallel lua to split the looping and raycasting

#

I did some benchmarking and here is the numbers:

Goal for 60 fps: 0.0166666667 or lower

soft rivet
#
game:GetService("RunService").RenderStepped:Connect(function(dt)
    local startT = os.clock()
    
    
    for i, pixel in ipairs(canvas) do

        local dir = camera:ScreenPointToRay(pixel.AbsolutePosition.X, pixel.AbsolutePosition.Y).Direction


        local rayCastParams = RaycastParams.new()
        rayCastParams.FilterType = Enum.RaycastFilterType.Exclude
        rayCastParams.FilterDescendantsInstances = {script.Parent}



        local result = workspace:Raycast(camera.CFrame.Position, dir * 500, rayCastParams)
        if result then
            pixel.BackgroundColor3 = result.Instance.Color
        else
            pixel.BackgroundColor3  = Color3.new()
        end
    end

    warn(os.clock() - startT)

    fpsLabel.Text = math.round(1/dt)
end)
keen lynx
soft rivet
#

Yeah only solution is to do parallel

#

I also noticed my fps going down to 30 if the only thing i do in the loop is to create a raycastParams so make sure its outside the loop

keen lynx
#

yea I did that

#

ty

#

parrarel ig

soft rivet
#

yeah lol

soft rivet
#

I did implement parallel into my version, it took the fps from 15 to 25 with 4 actors

#

and idk if the way i do it is the best either