Search code examples
swiftcameraarkit

Adjust camera focus in ARKit


I want to adjust the device's physical camera focus while in augmented reality. (I'm not talking about the SCNCamera object.)
In an Apple Dev forum post, I've read that autofocus would interfere with ARKit's object detection, which makes sense to me.

Now, I'm working on an app where the users will be close to the object they're looking at. The focus the camera has by default makes everything look very blurry when closer to an object than around 10cm.

Can I adjust the camera's focus before initializing the scene, or preferably while in the scene?


20.01.2018
Apparently, there's still no solution to this problem. You can read more about this at this reddit post and this developer forum post for private API workarounds and other (non-helping) info.


25.01.2018
@AlexanderVasenin provided a useful update pointing to Apple's documentation. It shows that ARKit will be able to support not just focusing, but also autofocusing as of iOS 11.3.
See my usage sample below.


Solution

  • As stated by Alexander, iOS 11.3 brings autofocus to ARKit.
    The corresponding documentation site shows how it is declared:

    var isAutoFocusEnabled: Bool { get set }
    

    You can access it this way:

    var configuration = ARWorldTrackingConfiguration()
    configuration.isAutoFocusEnabled = true // or false
    

    However, as it is true by default, you should not even have to set it manually, unless you chose to opt out.