Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

I'm trying to pixelate the player's screen (SOLVED) ?

Asked by 3 years ago
Edited 3 years ago
dist = 3
thic = 0.1
globalpart = Instance.new("Part")
globalpart.Size = Vector3.new(1, 1, 1)
print(globalpart.Size)
globalpart.Anchored = true
globalpart.CanCollide = false
globalpart.Transparency = 1
globalpart.Parent = game.Workspace
script.Parent.Adornee = globalpart

function refreshView()
    local vfov = game.Workspace.CurrentCamera.FieldOfView
    local aspectRatio = game.Players.LocalPlayer.PlayerGui.ScreenGui.AbsoluteSize.X/game.Players.LocalPlayer.PlayerGui.ScreenGui.AbsoluteSize.Y
    local hfov = vfov * (aspectRatio - 1)
    print(aspectRatio)
    globalpart.Size = Vector3.new(2*math.sqrt(math.cos(hfov/2)^2 - (dist + thic/2)^2), 2*math.sqrt(math.cos(vfov/2)^2 - (dist + thic/2)^2), 0.5)
    print(Vector3.new(2*math.sqrt(math.cos(hfov/2)^2 - (dist + thic/2)^2), 2*math.sqrt(math.cos(vfov/2)^2 - (dist + thic/2)^2), 0.5))
    print(globalpart.Size)
    local newcam = Instance.new("Camera")
    script.Parent.ViewportFrame.CurrentCamera = newcam
    newcam.FieldOfView = game.Workspace.CurrentCamera.FieldOfView
    newcam.Parent = script.Parent.ViewportFrame
    newcam.CFrame = game.Workspace.CurrentCamera.CFrame * CFrame.new(0, 0, dist + thic/2)
    newcam.HeadLocked = false
    globalpart.CFrame = game.Workspace.CurrentCamera.CFrame * CFrame.new(0, 0, -(dist + thic/2))
    for i, v in pairs(script.Parent.ViewportFrame:GetChildren()) do
        v:Destroy()
    end
    for i, v in pairs(game.Workspace:GetChildren()) do
        if v.ClassName ~= "Terrain" and v.ClassName ~= "Camera" then
            local eee = v:Clone()
            if eee then
                eee.Parent = script.Parent.ViewportFrame
            end
        end
    end
    newcam:Destroy()
end

game:GetService("RunService"):BindToRenderStep("runit", Enum.RenderPriority.Camera.Value - 1, refreshView)

Here's what my StarterGui looks like: image

So I'm trying to get the vertical and horizontal FOV then have a part fill the screen at a set distance away so I can get a pixelated screen effect. The size of the part remains 1, 1, 1 and the print prints -nan(ind), -nan(ind), 0.5. I don't know why.

LAST UPDATE I HAVE FIGURED IT OUT! My math was bad. I redid my math and switched from BindToRenderStepped because it was laggy. Now it's just PERFECT. Here is the code if you want to try it:

thic = 0.1
dist = 1.5 + thic/2
globalpart = Instance.new("Part")
globalpart.Size = Vector3.new(1, 1, 1)
print(globalpart.Size)
globalpart.Anchored = true
globalpart.CastShadow = false
globalpart.CanCollide = false
globalpart.Transparency = 1
globalpart.Parent = game.Workspace
script.Parent.Adornee = globalpart

for i, v in pairs(script.Parent.ViewportFrame:GetChildren()) do
    if v.ClassName ~= "Sky" then
        v:Destroy()
    end
end
for i, v in pairs(game.Workspace:GetChildren()) do
    if v.ClassName ~= "Terrain" and v.ClassName ~= "Camera" and v ~= globalpart then
        local eee = v:Clone()
        if eee then
            eee.Parent = script.Parent.ViewportFrame
        end
    end
end

game.Workspace.Changed:Connect(function()
    for i, v in pairs(script.Parent.ViewportFrame:GetChildren()) do
        if v.ClassName ~= "Sky" then
            v:Destroy()
        end
    end
    for i, v in pairs(game.Workspace:GetChildren()) do
        if v.ClassName ~= "Terrain" and v.ClassName ~= "Camera" and v ~= globalpart then
            local eee = v:Clone()
            if eee then
                eee.Parent = script.Parent.ViewportFrame
            end
        end
    end
end)

game:GetService("RunService").RenderStepped:Connect(function()
--function refreshView()
    local fov = game.Workspace.CurrentCamera.FieldOfView
    local aspectRatio = game.Players.LocalPlayer.PlayerGui.ScreenGui.AbsoluteSize.X/game.Players.LocalPlayer.PlayerGui.ScreenGui.AbsoluteSize.Y
    local vfov = fov
    local hfov = fov + fov * (aspectRatio - 1)
    print(aspectRatio)
    globalpart.Size = Vector3.new(2*math.sqrt(math.abs((dist/math.cos(math.rad(hfov/2)))^2 - dist^2)), 2*math.sqrt(math.abs((dist/math.cos(math.rad(vfov/2)))^2 - dist^2)), thic)
    print(hfov)
    print(vfov)
    print(Vector3.new(2*math.sqrt(math.abs((dist/math.cos(math.rad(hfov/2)))^2 - dist^2)), 2*math.sqrt(math.abs((dist/math.cos(math.rad(vfov/2)))^2 - dist^2)), thic))
    print(globalpart.Size)
    print(math.cos(math.rad(35)))
    local newcam = Instance.new("Camera")
    script.Parent.ViewportFrame.CurrentCamera = newcam
    newcam.FieldOfView = game.Workspace.CurrentCamera.FieldOfView
    newcam.Parent = script.Parent.ViewportFrame
    newcam.CFrame = game.Workspace.CurrentCamera.CFrame --* CFrame.new(0, 0, dist)
    newcam.HeadLocked = false
    globalpart.CFrame = game.Workspace.CurrentCamera.CFrame * CFrame.new(0, 0, -dist)
    newcam:Destroy()
--end
end)

--game:GetService("RunService"):BindToRenderStep("runit", 0, refreshView)

1 answer

Log in to vote
0
Answered by 3 years ago

Ok I have an update. I figured out why it was returning -nan(ind). I forgot that math.cos uses radians. The part still scales incorrectly. I still need help.

dist = 3
thic = 0.1
globalpart = Instance.new("Part")
globalpart.Size = Vector3.new(1, 1, 1)
print(globalpart.Size)
globalpart.Anchored = true
globalpart.CanCollide = false
globalpart.Transparency = 1
globalpart.Parent = game.Workspace
script.Parent.Adornee = globalpart

function refreshView()
    local fov = game.Workspace.CurrentCamera.FieldOfView
    local aspectRatio = game.Players.LocalPlayer.PlayerGui.ScreenGui.AbsoluteSize.X/game.Players.LocalPlayer.PlayerGui.ScreenGui.AbsoluteSize.Y
    local vfov = fov
    local hfov = fov * (aspectRatio - 1)
    print(aspectRatio)
    globalpart.Size = Vector3.new(2*math.sqrt(math.abs(math.cos(math.rad(hfov/2))^2 - (dist + thic/2)^2)), 2*math.sqrt(math.abs(math.cos(math.rad(vfov/2))^2 - (dist + thic/2)^2)), 0.5)
    print(hfov)
    print(vfov)
    print(Vector3.new(2*math.sqrt(math.abs(math.cos(math.rad(hfov/2))^2 - (dist + thic/2)^2)), 2*math.sqrt(math.abs(math.cos(math.rad(vfov/2))^2 - (dist + thic/2)^2)), 0.5))
    print(globalpart.Size)
    local newcam = Instance.new("Camera")
    script.Parent.ViewportFrame.CurrentCamera = newcam
    newcam.FieldOfView = game.Workspace.CurrentCamera.FieldOfView
    newcam.Parent = script.Parent.ViewportFrame
    newcam.CFrame = game.Workspace.CurrentCamera.CFrame * CFrame.new(0, 0, dist + thic/2)
    newcam.HeadLocked = false
    globalpart.CFrame = game.Workspace.CurrentCamera.CFrame * CFrame.new(0, 0, -(dist + thic/2))
    for i, v in pairs(script.Parent.ViewportFrame:GetChildren()) do
        v:Destroy()
    end
    for i, v in pairs(game.Workspace:GetChildren()) do
        if v.ClassName ~= "Terrain" and v.ClassName ~= "Camera" and v ~= globalpart then
            local eee = v:Clone()
            if eee then
                eee.Parent = script.Parent.ViewportFrame
            end
        end
    end
    newcam:Destroy()
end

game:GetService("RunService"):BindToRenderStep("runit", Enum.RenderPriority.Camera.Value - 1, refreshView)
Ad

Answer this question