Search Unity

Disabling Auto Focus

Discussion in 'AR' started by danjspoor, Nov 8, 2018.

  1. danjspoor

    danjspoor

    Joined:
    Mar 22, 2018
    Posts:
    2
    @tdmowrer Is there way to disable the camera auto focus with AR foundation? When testing on an iPhone 8, anytime I move the camera slightly, the AR objects appear to move in and out slightly, which gets annoying after a while... especially when trying to do a smooth video recording.

    I tested the example scene in ARKit Plugin to conclude that it also happens when auto focus is turned ON, but does not happen when it's turned OFF. I don't see a similar option to turn this off in AR Foundation. Is there any plan to add this option? Is this possibly unique to my device or is there another solution that would allow auto focus to stay enabled without the unwanted effect?

    If you want a sample video to see this, please let me know.

    Thanks!
     
  2. tdmowrer

    tdmowrer

    Joined:
    Apr 21, 2017
    Posts:
    605
    No, there is no way to set the focus mode, and ARKit enables it by default. We'll be adding this soon, though.
     
    danjspoor likes this.
  3. danjspoor

    danjspoor

    Joined:
    Mar 22, 2018
    Posts:
    2
    Ah ok. Thanks. Glad to hear it'll be added soon.
     
  4. ElinH

    ElinH

    Joined:
    Aug 29, 2017
    Posts:
    18
    Any update on this?
     
  5. tdmowrer

    tdmowrer

    Joined:
    Apr 21, 2017
    Posts:
    605
    Yep, support for this was added in preview 21 in December. See the changelog for details.
     
  6. ElinH

    ElinH

    Joined:
    Aug 29, 2017
    Posts:
    18
    Oh that's awesome.
    But I can't find any documentation how to disable the auto focus.
    Can you give me a example code snippet?
     
  7. tdmowrer

    tdmowrer

    Joined:
    Apr 21, 2017
    Posts:
    605
    There is a public property on the ARCameraOptions that you can set via script code. This example disables auto focus:

    Code (csharp):
    1. GetComponent<UnityEngine.XR.ARFoundation.ARCameraOptions>().focusMode = UnityEngine.XR.ARExtensions.CameraFocusMode.Fixed;
     
    unity_bfawXRcvR563OA and Blarp like this.
  8. Blarp

    Blarp

    Joined:
    May 13, 2014
    Posts:
    270
    arfoundation and LWR seems to have a very grainy camera feed. I was thinking it was because i didnt have the cameraoptions script attached to anything.

    When I add, the camera feed goes black. Most likely due to the LWR support using a special ar camera background texture to make this possible and autofocus not playing nice with it.
     
  9. ElinH

    ElinH

    Joined:
    Aug 29, 2017
    Posts:
    18
    Awesome thank you.
     
  10. JoMaHo

    JoMaHo

    Joined:
    Apr 2, 2017
    Posts:
    94
    Hi!
    I´m having issues with autfocus/fixed focus on my Ar Foundation AR app. I have attached the AR Camera options script to my AR Camera component, and have tried to make it work, but autofocus seems to be enabled all the time, even if I buld with the Fixed settings in the AR Camera optins script.

    I have also tried to add a button with a fuction that swaps Auto/fixed focus at runtime, using the script snippet mentioned in the above referenced post, but still the focus is always trying to get the camera in focus, making that small focus jagging.

    Is there something I´m doing wrong here?

    Using Unity 2018.3.2, Mac, ARKit XR Preview 17, Ar Foundation preview 22, building for iPhoneXR.

    EDIT: I had the solution right there; updated to ARkit XR Preview 20 - all good!
     
    Last edited: Apr 4, 2019
  11. jae_yoo

    jae_yoo

    Joined:
    Apr 22, 2019
    Posts:
    2
    I don't have ARFoundation module within UnityEngine.XR.
    What am I missing?
     
  12. mkrfft

    mkrfft

    Joined:
    Sep 8, 2017
    Posts:
    22
    @tdmowrer Autofocus doesn't seem to be affected by the flag on ARKit 2.1.0 Preview 5. Am I doing something wrong? Autofocus is always on.
     
  13. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    833
    @tdmowrer FYI this doesn't appear to work, when building with focus mode set to fixed, the iPhone will happily continue to perform auto-focus. Is there another setting?
     
    Last edited: Jul 8, 2019
  14. davidmo_unity

    davidmo_unity

    Unity Technologies

    Joined:
    Jun 18, 2019
    Posts:
    99
    Hello, you are in fact correct that there is a bug in ARFoundation all the way back to 1.5 particular to iOS and it's focus mode settings. iOS11.3 changed the default setting to Auto Focus on so the only way to access the appropriate focus mode is to do one of the following:

    1. Set the focus mode to 'Auto' in the editor then change from Auto to Fixed During Runtime.

    2. Set the focus mode to 'Fixed' in the editor and then change from Fixed to Auto back to Fixed during runtime.

    We do have a fix for this across effected packages that is currently in review but for now the best way to achieve the desired functionality is by setting it during runtime.
     
    enhawk and Blarp like this.
  15. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    833
    Thanks for clarifying this. The above code sample is out of date, here is the updated version:

    Code (CSharp):
    1. GetComponent<UnityEngine.XR.ARFoundation.ARCameraManager>().focusMode = UnityEngine.XR.ARSubsystems.CameraFocusMode.Fixed;
     
    Blarp likes this.
  16. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    833
    @davidmo_unity I just tried this out and the iPhone is still happily auto-focusing constantly.

    Unity version: 2019.1.5f1
    ARKit: preview.5 - 2.1.0
    AR Foundation: preview.3 - 2.1.0
    Xcode: 10.1
    iOS base target: 11
    iOS installed: 12
     
  17. davidmo_unity

    davidmo_unity

    Unity Technologies

    Joined:
    Jun 18, 2019
    Posts:
    99
    Okay can you also include the code segment you used to change the autofocus during runtime. It should work unless there is a deeper issue in ARKit that hasn't been looked into. Also thank you for posting the updated code sample.
     
  18. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    833
    I'm using it in this way on a script that is on a game object in the main scene, thanks.

    Code (CSharp):
    1. public void Start()
    2.     {
    3.         GetComponent<UnityEngine.XR.ARFoundation.ARCameraManager>().focusMode = UnityEngine.XR.ARSubsystems.CameraFocusMode.Fixed;
    4.     }
     
  19. davidmo_unity

    davidmo_unity

    Unity Technologies

    Joined:
    Jun 18, 2019
    Posts:
    99
    So there is a strong possibility that this code is executing before the ARCameraManager has even had a chance to be enabled and set that value. I would suggest attempting something like:

    Code (CSharp):
    1. void Update()
    2. {
    3.     var cameraManager = GetComponent<UnityEngine.XR.ARFoundation.ARCameraManager>();
    4.     if (cameraManager != null && cameraManager.enabled)
    5.     {
    6.         cameraManager.focusMode = CameraFocusMode.Fixed;
    7.    
    8.         // Whatever else you need to do with this script if anything.
    9.     }
    10. }
    Which should only work if the camera mode was set to Auto in the editor. If it was set to Fixed then you would need to use the following in the if-statement


    cameraManager.focusMode = CameraFocusMode.Auto;
    cameraManager.focusMode = CameraFocusMode.Fixed;
     
  20. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    833
    thanks I'll try this
     
  21. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    833
    @davidmo_unity Sorry to say this but the iPhone is still autofocusing. Did you guys have much luck on your side with this technique?
     
    ajbobba and unnanego like this.
  22. FOKSlab

    FOKSlab

    Joined:
    Jun 27, 2017
    Posts:
    30
    I have got the same issue, and whatever I try to do to force fixed focus, my iPad camera is returning to autofocus mode. This is really annoying since I am using tracked images to calibrate some 3D objects' poses regarding my iPad: when autofocus is enabled, tracked images are sometimes not in focus and so not tracked :(. Is there any way to fix that ?
     
    ajbobba likes this.
  23. Kmil81

    Kmil81

    Joined:
    May 5, 2019
    Posts:
    9
    Did anyone ever find a fix to this on iPhone?