Upbge 0.3 - Dynamic depth of field mechanics for a first person view camera

Simple dynamic depth of field mechanics, for a FPS camera. It uses eevee’s native depth of field, taking advantage of the object focus feature. With logic nodes, you use a ray to pinpoint where the camera is looking and place the focus object there. So the dof is changed in realtime. Its not an advanced solution, but could still be of interest to some. Full instructions in a readme text inside the file.

upbge 3 - depth of field with dynamic focus.blend (1.6 MB)

Logic nodes setup (to be applied to the raycasting object):

Readme text description:
“the depth of field feature can use an object as its focus point
we want this effect to be dynamic, and for that we need to have this focus object placed on the surface of whatever it is we are looking at, in realtime
to achieve this:
create a cube (set its visualization to wire, so that it will be invisible in game, and also no collision) centered to and in front of the camera
parent it to the camera
we will later use logic nodes to make the cube cast a ray so we can get the exact point the camera is pointing at (in the center of the screen)
add an empty, centered on the cube, and move it a long enough distance in front of it
parent the empty to the cube (this will be the ray target)
you can check slow parent and add a small offset to make the changes in direction a bit smoother if needed
create another object (an icosphere) that will be the focus of the camera, set it to wire and no collision as well
now select the camera, in the camera object data properties tab activate depth of field, select the icosphere as its focus, set fstop to whatever you want
the depth of field should be visible now, centered around the icosphere
select the cube, and open the logic nodes editor
create a new logic tree (named here as ‘depth of field - focus object raycast logic’), and in it:
add a ray node
on condition, connect an on update node (so it activates the logic whenever it’s called)
on origin, connect a get position node, set object to self, leave it as position global
on aim, connect a get position node, set object to the empty, leave it as position global
now, add a set position node
leave it as position global
on condition, connect to the ray’s has result out socket
set object to the icosphere
on value, connect to the ray’s picked point out socket
this will fire a ray from the cube to the empty, and when it hits something, it will position the icosphere on the ray’s hit point
the depth of field will be updated dynamically because the focus object will always be located on whatever we are poiting at in realtime
after applying the logic nodes tree to the cube, go into the logic bricks editor and on the always sensor that activates the logic node tree, change the pulse frequency to 10 or so, to save on calculations (optional)”

2 Likes