How can i make this 3D raycasting renderer gui less laggy?

recently i have found this antique old 3D raycasting renderer gui made in around 2012, and somehow it still works! but its pretty laggy. If you have a good pc, the most that this thing can run at is like 15fps, but for me, it runs at like 3fps. So how would i make this less laggy and run more efficiently?

repeat wait() until player.Character
Char = player.Character
HRP = Char:WaitForChild("HumanoidRootPart")

wait(0)
Ambient = script.Parent.Ambient
Light = game.Workspace.Light --known light source
pixel = 60 --the pixels to be generated and rendered
size = 10 --pixel size
sky = 1
length = 100 --view range
debug = false --if true the script will handle with the old pixels rather than creating new ones
function tint(col,rat) 
	r = 1-col.r
	g = 1-col.g
	b = 1-col.b
	return Color3.new(r*rat,g*rat,b*rat)
end
function shade(col,rat)
	r = col.r
	g = col.g
	b = col.b
	return Color3.new(r*rat,g*rat,b*rat)
end
function color3add(col,col2)
	return Color3.new(col.r+col2.r,col.g+col2.g,col.b+col2.b)
end
function interpolate(col,col2,rat)
return Color3.new(col.r*(1-rat) + col2.r*rat,col.g*(1-rat) + col2.g*rat,col.b*(1-rat) + col2.b*rat)
end
function interpolateT(col,col2,rat,val)
return col*(1-rat) + col2*rat
end
function reflect(vector3,norm)
	norm = norm or Vector3.new(0,1,0)
	return vector3 - ( 2*norm*vector3:Dot(norm))
end



function GeneratePixels()
	for x=0,pixel-1 do
		wait()
		for y=0,pixel-1 do
			fr = Instance.new("Frame")
			fr.Position = UDim2.new(0,x*size,0,y*size)
			fr.Size = UDim2.new(0,size,0,size)
			fr.BackgroundColor3 = Color3.new(x,y,0)
			fr.BorderSizePixel = 0
			fr.Name = x .."/".. y
			fr.Parent = script.Parent.p
			fr.ZIndex = 3
		end
	end
end
if not debug then
	GeneratePixels()
end

while true do --start the camera loop
	wait(0.1)
	c = CFrame.new(game.Workspace.CurrentCamera.CoordinateFrame.p,game.Workspace.CurrentCamera.Focus.p)
		for x=pixel-1,0,-1 do
			for y=pixel-1,0,-1 do
				dir = c*CFrame.new((pixel/2-x)*(20/pixel*.1),(pixel/2-y)*(20/pixel*.1),-1) --with default 20 pixels .1 step
				ray = Ray.new(c.p,(dir.p - c.p).unit * length) --view ray
				part,point = game.Workspace:FindPartOnRayWithIgnoreList(ray, {Light, Char})
				r,g,b = 0,0,0 --brickcolor
				rr,rg,rb = 0,0,0 --reflection color
				r = 0 --reflection ratio
				shade = 0
				if(part)then
					color = part.BrickColor.Color
					if(part.Name == "Light")then
						r,g,b = r+color.r,g+color.g,b+color.b
					end
					ray2 = Ray.new(point,(Light.Position-point).unit * Light.Range.Value) --light ray to view point
					part2 = game.Workspace:FindPartOnRayWithIgnoreList(ray2, {Light, Char})
					ratio = (Light.Position - point).magnitude/Light.Range.Value
					if(part2~=nil)then
						shade = 0
					end
					if(ratio>1)then
						ratio = 1
					elseif(ratio<0)then
						ratio = 0
					end
					if(part.Name == "Water")then --add reflection values
						ref = reflect(dir.lookVector)
						ray3 = Ray.new(point,ref*length)
						part3,point3 = game.Workspace:FindPartOnRayWithIgnoreList(ray3,{part})
						if(part3)then
							color2 = part3.BrickColor.Color
							rr,rg,rb = rr+color2.r,rg+color2.g,rb+color2.b
							r = part.Reflection.Value
						end
					end
					if(shade==0)then --light wasn't blocked
						if(rr~=0 and rg~=0 and rb~=0)then --add the reflection to the final color
							r = interpolateT(color.r*(1-ratio),rr,r,"r")
							g = interpolateT(color.g*(1-ratio),rg,r,"g")
							b = interpolateT(color.b*(1-ratio),rb,r,"b")
						else --no reflection
							r = color.r*(1-ratio)
							g = color.g*(1-ratio)
							b = color.b*(1-ratio)
						end
					end
				else 
					if(sky == 1)then
						r = 0 --default sky
						g = 153/255
						b = 204/255
					end
				end
				r = r * Ambient.Value.r
				g = g * Ambient.Value.g
				b = b * Ambient.Value.b
				script.Parent.p[(pixel-1)-x .."/".. y].BackgroundColor3 = Color3.new(r,g,b)
			end
		end
		
		Light.Position = HRP.Position
		
	end


--Originally written by su8
--July 2012
4 Likes

Higher numbers of roblox frames used as pixels cause horrible lag. You can’t do much about it. As for the rendering algorithm, pretty much everything is deprecated. add local variables, clean the code. my guess is it will NOT run above 30 fps, no matter what PC you’re using.

1 Like

i guess your right, and i’m surprised my PC even runs at 3 fps with 3.6k frames as pixels. Also nothing seems to be showing as deprecated in the code.

Roblox intellisense decided to commit die, then. There are limits to how much it can take before having to end itself. There is a crippling lack of local variables, .p, and various other garbage that slows the code down.

This may sound silly, but why do i need so much local variables if they aren’t in a function?

1 Like

global variables slow the code down by quite a bit

1 Like

really? huh, thats interesting. Honestly, i thought local was only suppose to be used in functions and enclosed stuff like that.

even a high level language like Lua has a lot of things you need to take care of. this isn’t a topic i know much about but there’s a reason newer code is written the way it’s written.

1 Like