How can I fix my pixelation effect?

I have been trying to create a makeshift filter that makes the game appear pixelated, using GUI and ray casting, with the idea being that multiple rays are sent out in a sort of “FOV cone” formation, and then returns the color of the objects the rays hit, then setting the colors of the frames in the GUI to said object colors.

However, the rays clearly seem to be going places that they are not intended to, leading to a buggy visual that does not reflect the workspace whatsoever. I am inclined to believe I have an issue with the aim of my rays, however I have spent around an hour trying to think about which way they are meant to go, and trying to modify the variables accordingly.

Here is footage of what happens:
Black tiles mean that the ray returned nil.

Code:

local run = game["Run Service"]
local SCREENSIZE = 30
local SCREEN = script.Parent.Screen
local char = game.Players.LocalPlayer.Character
char:WaitForChild("Head").CanQuery = false
local camera = workspace.CurrentCamera

run.Heartbeat:Connect(function()
	for i, v in pairs(SCREEN:GetChildren()) do
		if v:IsA("Frame") then
			v:Destroy()
		end
	end
	for y = 0, SCREENSIZE do
		for x = 0, SCREENSIZE do
			local newframe = Instance.new("Frame")
			newframe.BackgroundTransparency = 0.5
			newframe.Parent = SCREEN
			newframe.Size = UDim2.new(1 / SCREENSIZE,0,1 / SCREENSIZE,0)
			newframe.Position = UDim2.new(0 + (x / SCREENSIZE),0,0 + (y / SCREENSIZE),0)
			local rayres = workspace:Raycast(camera.CFrame.Position,camera.CFrame.LookVector + Vector3.new(35 * ((y - y * 2) + (SCREENSIZE / 2)),35 * ((x - x * 2) + (SCREENSIZE / 2)),0))
			if rayres == nil then
				newframe.BackgroundColor3 = Color3.new(0,0,0)
			elseif rayres.Instance:IsA("Part") then
				newframe.BackgroundColor3 = rayres.Instance.Color
			end
		end
	end
end)

Help would be much appreciated!

1 Like

Although I have no answer for why your code is failing, I do have some pointers that should make your life a lot easier.

The Camera instance has a method ScreenPointToRay which sounds like it would be the exact thing that would simplify your calculation. Do note that the rays returned by this method are only a single stud long, so you will want to do your own raycast afterwards using the information returned by the method. Using this method you can probably just substitute the line where you define local rayres with two lines of code; one where you call ScreenPointToRay and then a second one where you use its returned data to do a different Raycast.

Im not really sure but I think its because your look vector is still a direction not being multiplied by anything so your ray is raycast Vector3.new(35 * ((y - y * 2) + (SCREENSIZE / 2)),35 * ((x - x * 2) + (SCREENSIZE / 2)) + a vector3 with numbers between -1 and 1

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.