Skip to content

Instantly share code, notes, and snippets.

@Foxxive
Last active April 19, 2022 15:48
Show Gist options
  • Select an option

  • Save Foxxive/1119d7d5a029752c8f9c8a3a7c695bd0 to your computer and use it in GitHub Desktop.

Select an option

Save Foxxive/1119d7d5a029752c8f9c8a3a7c695bd0 to your computer and use it in GitHub Desktop.
Dynamic Depth of Field script for Roblox games to use, free of charge
--// Made by Foxxive \\--
--[[ ______ ______ ____ _________ ________
____ | ____/ __ \ \ / /\ \ / /_ _\ \ / / ____|
/ __ \| |__ | | | \ V / \ V / | | \ \ / /| |__
/ / _` | __|| | | |> < > < | | \ \/ / | __|
| | (_| | | | |__| / . \ / . \ _| |_ \ / | |____
\ \__,_|_| \____/_/ \_\/_/ \_\_____| \/ |______|
\____/
]]
-- creates new DOF effect, make sure you don't have another one already because it'll do weird stuff
local DOF = Instance.new("DepthOfFieldEffect", game.Lighting)
DOF.InFocusRadius = 0
DOF.NearIntensity = 0
local camera = workspace.CurrentCamera
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local head = char:WaitForChild("Head")
local HumanoidRootPart = char:WaitForChild("HumanoidRootPart")
-- how far the object can be before it focuses
local focusRadius = 6
-- TweenService is messy, you can change this if you like lol
function Lerp(a, b, t)
return a + (b - a) * t
end
-- checks if the camera is fully zoomed in, because you can't check if the player is in first person for some reason
function isFirstPerson()
local cameraDistance = (camera.CFrame.Position - camera.Focus.Position).Magnitude
return (cameraDistance < 1)
end
game:GetService("RunService").RenderStepped:Connect(function(dt)
local ignoreList = isFirstPerson() and {character} or {}
local camRay = Ray.new(cam.CFrame.Position, cam.CFrame.LookVector * focusRadius)
local hit, hitPosition = workspace:FindPartOnRayWithIgnoreList(ray, ignoreList)
-- this is so glass and stuff doesn't break, change the value if you want
if hit and hit.Transparency < 0.3 then
local distanceFromCamera = (hitPosition - camera.CFrame.Position).Magnitude
local intensity = focusRadius / distanceFromCamera * 0.25
DOF.FocusDistance = distanceFromCamera
DOF.FarIntensity = Lerp(DOF.FarIntensity, intensity, dt * 8)
else
DOF.FarIntensity = Lerp(DOF.FarIntensity, 0, dt * 8)
end
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment