0

I have a virtual camera in a game engine. The camera has an DOF setting where you can set the :

  1. Distance to object
  2. Aperture
  3. Focal length

I would like to set a depth of field (lets say about 5m) around the tracked object. When the object moves, the DOF needs to stay constant (at 5m). The distance to the object from the camera is trivial to obtain.

This means I need to calculate the aperture and focal length combination that will result in the DOF at said distance.

I am not even sure that this can be done with the information available. Any suggestions will be welcome.

5
  • What photographic problem are you trying to solve? That is, what photo will an answer to this question allow you to take that you can't take now?
    – Michael C
    May 12 at 11:29
  • Does this answer your question? What exactly determines depth of field?
    – Michael C
    May 12 at 11:30
  • Depth of field is not some magic zone within which everything is equally sharp. There is only one focus distance, and everything else is blurry to one degree or another depending on how far in front of or behind that distance an object is. What we call DoF is what is "sharp enough" to look in focus to our eyes. Everything on a billboard looks sharp when viewed from 60-70 feet away. But if you climb to the platform you can see that it is not all equally sharp. Thus display size and viewing distance also play a role in the limits of DoF. How does your virtual camera handle that?
    – Michael C
    May 12 at 11:34
  • Do you tell it how large the screen you're playing on is and how far away the player's eyes are from the screen? If not, none of your calculations will be accurate except for one specific screen size at any one specific viewing distance.
    – Michael C
    May 12 at 11:35
  • If you want to check out a codebase that can do this, Second Life is open source [idk if you could re-use it, but you could look at it]. - wiki.secondlife.com/wiki/Get_source_and_compile There's a user guide to its use at wiki.secondlife.com/wiki/Depth_of_field [It definitely depends on viewport size/scale as to how this works. It doesn't travel well, as mentioned by @MichaelC]
    – Tetsujin
    May 13 at 7:54

3 Answers 3

2

The Depth of Field formula can be obtained from Wikipedia.

DOF = 2 u2 N C / f2

N = aperture F-number
C = circle of confusion
u = distance to subject
f = focal length

Distance to the subject is set by camera position.

Circle of confusion is arbitrary, but usually set based on sensor size and expected viewing conditions. You'll need to get this from the game engine or figure it out experimentally.

Focal length is determined by your desired angle of view. You can set focal length experimentally, based on what looks good. Otherwise, you will need to experimentally determine the sensor size the camera is using. The formulas relating focal length, sensor size, and angle of view are on Wikipedia.

α = arctan (d / 2f)
tan (α/2) = d / 2f ⇒ f = d / (2 tan (α/2)), d = f / (2 tan (α/2))

α = angle of view
d = width or height of sensor
f = focal length

Then you can calculate the aperture you need for the desired DOF.

N = DOF · f2 / 2 u2 C

1

The image of the outside world formed by the lens is composed of countless circles of light we call circles of confusion. These are the teeniest bits of an image that can convey intelligence. We declare an image to be tack sharp when these are undiscoverable. The criterion is ½ mm in diameter or smaller seen from reading distance.

All charts, tables and calculators dealing with depth-of-field compute permissible circle size. The 0.5mm size is for the displayed image. The image inside the camera must be smaller, so that it can tolerate enlargement to make the displayed image size.

Several approaches, all dealing with geometry, can be applied. All are flawed because several of the variables cannot be accurately predicted. The following is a simple geometric approach that I think will work OK for your application. This approach is based on looking back at the camera from a distance and mathematically choosing an iris (aperture) diameter suitable for your needs.

The basis – a circle viewed from 3000 times its diameter -- appears as a point of light with no discernable diameter. The 3000 X is too stringent; 2000 is better for your needs. I suggest you experiment, likely 1000 or 1500 will serve your needs. Assuming 2000 X is used.

Find the diameter of the iris for your focal length and f-number setting. Suppose a 30mm lens is set to f/11. The diameter of the iris is 30/11 = 2.7mm. Multiply by 2000 = 5500mm = 5.5 meters. This simple math tells me that for a camera with a 30mm lens mounted, focused at 5.5 meters, set to f/11 will yield an image with sufficient depth-of-field for your needs.

Using simple algebra, you can work this with other variables. Say f/4 then the focus distance changes to 15 meters. Thus 30/4 = 7.5 X 2000 = 15,000mm = 15 meters. Try it using 1000 basis or 1500 basis. You will like it; it’s easy math.

0

I use a rough formula for guesstimating hyperfocal distance (HFD) and depth of field (DoF)... I think it could be used here to adequately approximate the camera view.

But first you have to consider the type of camera you are simulating. I.e. if it is an SLR with autofocus the aperture typically remains set at the maximum; and you cannot see the DoF of the set aperture unless you activate a DoF preview button/function. If it is a mirrorless camera, a DSLR in live view with exposure preview active, or an old school manual focus/manual aperture camera, then the selected/set aperture will change the viewed image by default. I.e. in the first case you only need to approximate the DoF for the len's max aperture (unless also programing a preview function).

The formula I use to SWAG hyperfocal distance and DoF (in feet) is: HFD=.1xFL(.1xFL). For example, a 50mm lens would be .1x50(.1x50) = 5x5 = 25ft HFD (a little easier; take the first number of the FL and square it; if the FL is 35mm use 3x4 instead of 3x3).

The HFD is the subject distance (focus setting) where the DoF exists from 50% of the HFD out to infinity. In this example, if the 50mm lens is set to focus at the 25ft HFD the DoF would then extend from 12ft to infinity.

The SWAG formula is based upon a FF camera with a lens at f/11, a 1.5x APS camera set to f/16, or an M4/3 camera set to f/22. If you halve the aperture setting (f#) the HFD ~ doubles... I.e. 50mm on FF @ f/11= 25ft HFD; at f/5.6 = 50ft HFD (as I said, this is a SWAG... the actual HFD numbers are 24.3/48.5ft).

Then, if the lens is instead focused at 50% of the HFD, the DoF is equal to approximately half of the HFD number. I.e. 50mm on FF @ f/11 focused at 12.5ft the DoF = approx 12.5ft (**15.5ft actual). In this case the DoF exists 25% in front, and 75% behind the point of focus.

And then at very short distances the DoF exists as 50% in front of, and behind, the point of focus.

DoF Calculator


** at shorter FL's the error of the SWAG is greater.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.