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)