Dynamic Depth Of Field

I did a kind-of dynamic depth of field, basically works on mouse position.

-- Dynamic Depth Of Field.
local RunService = game:GetService("RunService")
local Lighting = game:GetService("Lighting")

local Player = script.Parent.Parent
local Mouse = Player:GetMouse()

local DOF = Lighting:WaitForChild("DepthOfField")
local LIMIT = 20 -- Radius Diameter


DOF.FarIntensity = 0.22
RunService.RenderStepped:Connect(function(dt)
	
	local RadiusVector = (Mouse.Origin.p - Mouse.Hit.p)
	local RadiusMagnitude = RadiusVector.Magnitude
	
	if RadiusMagnitude <= LIMIT then
		DOF.FocusDistance = RadiusMagnitude
	end	
	
end)
1 Like

Updated Code:

-- Dynamic Depth Of Field.
local RunService = game:GetService("RunService")
local Lighting = game:GetService("Lighting")
local Players = game:GetService("Players")

local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()

local DOF = Lighting:WaitForChild("DepthOfField")
local UPPER_LIMIT = 100 -- Radius Diameter
local LOWER_LIMIT = -20


DOF.FarIntensity = 0.22
RunService.RenderStepped:Connect(function(dt)
	
	local RadiusVector = (Mouse.Origin.p - Mouse.Hit.p)
	local RadiusMagnitude = RadiusVector.Magnitude
	
	-- if RadiusMagnitude <= LIMIT then
	-- 	DOF.FocusDistance = RadiusMagnitude
	-- end
	DOF.FocusDistance = math.clamp(RadiusMagnitude, LOWER_LIMIT, UPPER_LIMIT)
	
end)