Dynamic Depth of Field

Introducing
Dynamic Depth of Field

Using the simple API included, you can easily set a Depth of Field effect’s blur distance to blur anything behind a given object. Optionally, you can have the effect automatically update when the camera changes to keep the effect between zooms.

Background:
With the release of the amazing Depth of Field effect came a lot of cool creations. I had a thought to add a “dynamic” depth of field effect to my game. When an object is selected, everything behind that object is blurred. It works the best when the distance between the selected part and the background are far apart (~15 studs). I increased the intensity for this example.

Download
https://www.roblox.com/library/5043661567/DynamicDepthOfField

GitHub repo if you’re into that

Setup
Simply require the module from another module or LocalScript and you’re good to go! There is a optional config starting on line 17.

local DynamicModule = require(path.to.DynamicDepthOfField)

--example
DynamicModule:SetDepthToPart(myModel, true)

wait(5)

DynamicModule:Reset()

API Documentation

function Module:SetDepthToPart(targetPart, dynamicallyUpdate)

Arguments

  • Model or Part targetPart → The part or model that will be in focus
  • Optional Boolean → If true, will dynamically update the focus when the camera position changes, if false, focus will not be changed when the camera position changes

Set’s the FocusDepth property to the distance between the part and camera.

function Module:StopUpdating(keepEffectEnabled)

Arguments

  • Optional Boolean → If true, the effect will persist but will not be updated when the camera moves, if false or no argument passed, the effect will be disabled

Module will disconnect the bind to RunService, thus stopping the dynamic updating of the focus effect.

function Module:Reset(optionalConfig)

Arguments

  • Optional table → Property table. Properties will be loaded into depthEffect.

Makes a call to Module:StopUpdating(), disabling the effect and resetting the properties. If a property table is passed, the properties will be overridden.

function Module:SetEffect(DepthOfFieldEffect effect)

Arguments

  • DepthOfFieldEffect → Instance that the module will favor

If config option CREATE_ON_INIT is disabled, you will want to set this to your own instance

Mutators

function Module:Enable()
function Module:Disable()
function Module:SetFocusRadius(int depth)
66 Likes

I’ve removed the tag from your title–putting something in Community Resources implies that it’s open source already.

2 Likes

This is awesome! I’ll be using this so thanks for your contribution my good sir.

1 Like

Dose this dynamically change fov depending what the player is looking at when moving camera around?

Doesn’t change the field of view, it changes the Depth of Field. But yes, by default it updates the effect range based on the distance between the camera and the target part. It updates until the StopUpdating of Reset method is called.

It gives me no errors but the script isn’t working.
image
May I know what I can do to fix this?

I assume you are new to scripting. This article has a list of service containers that you can put your localscripts in to make it function as expected.

1 Like

Yeah, I am fairly new to this. I’ll check this out.
Thank you!

LocalScripts cannot be ran server side (hence why it’s not working in ServerScriptService).

I would recommend storing your Module in ReplicatedStorage, for example, and requiring that from your LocalScript in either StarterGui, StarterPlayerScripts, or StarterCharacterScripts.

1 Like