Search Unity

AR Foundation - Pausing & Resuming tracking?

Discussion in 'AR' started by ARXRKix, Oct 1, 2018.

  1. ARXRKix

    ARXRKix

    Joined:
    Apr 17, 2018
    Posts:
    5
    Unity 2018.1.3f1

    AR Foundation 1.0.0-preview.17
    ARCore XR 1.0.0-preview.19
    ARKit XR 1.0.0-preview.14

    Hi, I have created an app that utilises AR Foundation, for it to work on both iOS and Android. It seems that in Android I can easily get access to the ARPlaneManager script and run:


    Code (CSharp):
    1. public ARPlaneManager arplanemanager;
    2.  
    3. arplanemanager.OnDisable();
    to disable the tracking.

    However on iOS this does not work ,as when i try to get access to the ARPlaneManager it comes up with a message saying I cant change/edit the scripts as Access is Denied (read only)?

    Is there a way to easily Pause & Resume Tracking on both iOS and Android without using the code above?

    UPDATE:

    I found this:

    Is it as simple as

    Code (CSharp):
    1. arsession.enabled = true
    2.  
    3. //or
    4.  
    5. arsession.enabled = false
     
    Last edited: Oct 1, 2018
  2. tdmowrer

    tdmowrer

    Joined:
    Apr 21, 2017
    Posts:
    605
    Looks like you've found the answer. If you wanted to disable only the plane detection, you can apply the same logic to the plane manager, e.g.:
    Code (CSharp):
    1. arplanemanager.enabled = false
    The OnDisable method is a callback that is invoked in response to a component becoming disabled; you should not normally invoke it directly if your intent is to disable the component.
     
  3. ARXRKix

    ARXRKix

    Joined:
    Apr 17, 2018
    Posts:
    5

    What would be the best way to pause tracking - as arsession.enabled = false; disables the camera and currently tracked object.

    I'm placing a model on a plane once the user taps. But i want to anchor the position of the model and stop all tracking (still allowing the user to move around and view the model) and have the camera video still showing?
     
    wjsgodls98 and henryqng like this.
  4. tdmowrer

    tdmowrer

    Joined:
    Apr 21, 2017
    Posts:
    605
    What do you mean by "stop all tracking"? Tracking generally refers to tracking the device location in space, but disabling this would not let you "move around and view the model".
     
  5. ARXRKix

    ARXRKix

    Joined:
    Apr 17, 2018
    Posts:
    5
    I was referring to stopping the app detecting planes all the time.

    I implemented the arplanemanager.enabled = false; it works

    This thread can be marker as SOLVED :D
     
  6. necipcik

    necipcik

    Joined:
    Feb 22, 2013
    Posts:
    5
    Where are we suppose to implement this?

    arplanemanager.enabled = false;
     
  7. tdmowrer

    tdmowrer

    Joined:
    Apr 21, 2017
    Posts:
    605
    Whenever you would like to disable plane detection. Can you be more specific in your question? Please post some code if you can.
     
  8. necipcik

    necipcik

    Joined:
    Feb 22, 2013
    Posts:
    5
    i was trying to achieve exact same thing as ARXRKix did. i managed to disable plane detection with this:

    GetComponent<ARPlaneManager>().enabled = false;
    GetComponent<ARPointCloudManager>().enabled = false;

    now the new question is how am i suppose to get rid of the last detected points and plane off the screen as soon as i tap to place the 3d object?
     
    Futurristic likes this.
  9. tdmowrer

    tdmowrer

    Joined:
    Apr 21, 2017
    Posts:
    605
    You can disable the
    GameObject
    s of those things:
    Code (CSharp):
    1. foreach (var trackable in planeManager.trackables)
    2. {
    3.     trackable.gameObject.SetActive(false);
    4. }
    You can do the same for point clouds.
     
  10. skywalker887

    skywalker887

    Joined:
    May 24, 2017
    Posts:
    17
    A question here .If the we stop the ARPlaneManager by setting .enable=false ,will the tracking stop also for a plan .For example ,lets say we detect a plane and use AttachReferencePoint to that plane and then disable the APPlaneManager ,will this effect the Plane and ARReferencePoint position and orientation as the user moves around ,will that cause a drift or tracking becomes disconnected ?
     
  11. tdmowrer

    tdmowrer

    Joined:
    Apr 21, 2017
    Posts:
    605
    No plane will receive any update as long as plane detection is disabled (which is what disabling the ARPlaneManager does). Reference points may still update, and if they are attached to a plane, then they can only move in the, well, plane of the ARPlane. That is, they try to maintain a constant relative distance to the plane. Since the plane is no longer updating, the reference point may become inaccurate with respect to its distance from the (real) surface it is "attached" to.
     
  12. skywalker887

    skywalker887

    Joined:
    May 24, 2017
    Posts:
    17
    Thanks @tdmowrer for the response .
    I read many blogs which recommend disabling the ARplane manager as soon the need for discovering more planes is no longer required ,which will improve performance ,how should we balance between this and having the planes positions being drifted due to absent updates ?
     
  13. tdmowrer

    tdmowrer

    Joined:
    Apr 21, 2017
    Posts:
    605
    World tracking is still running, so planes shouldn't really drift away too much, but the detected surface won't continue to be refined. How you balance performance vs fidelity isn't something I can answer -- it depends on your app's needs.
     
    Fenikkel likes this.
  14. skywalker887

    skywalker887

    Joined:
    May 24, 2017
    Posts:
    17
    So for already detected surfaces where reference points are attached will still be updated ,but when new surfaces are detected and ARPlaneManager is disabled ,it might gets inaccurate update ,is that correct ?
     
  15. tdmowrer

    tdmowrer

    Joined:
    Apr 21, 2017
    Posts:
    605
    Surfaces (i.e., planes) will not be detected if plane detection is disabled. Disabling the ARPlaneManager disables plane detection.
     
  16. skywalker887

    skywalker887

    Joined:
    May 24, 2017
    Posts:
    17
    Ok ,but since surfaces will stop getting refined ,I guess it is better to keep ARPlaneManager enabled if we really want to have the best accuracy ,Thanks.
     
  17. paulksi

    paulksi

    Joined:
    Nov 9, 2015
    Posts:
    27
    Hello, i would like to ask what happens when you cover up camera, because it seems that gyro is disabled when you do it?

    i loged cube`s values, but it`s the same all the time?
    can anybody tell me what`s going on here? it seems that the whole coordination system changes when you cover up camera? Thank you.
     
    Last edited: Mar 19, 2020
    sabanmete likes this.
  18. RiccardoAxed

    RiccardoAxed

    Joined:
    Aug 29, 2017
    Posts:
    119
    Hi, I would ask what is the right and clean procedure to enable/disable the whole AR system, so to switch from an AR session to an "ordinary" 3D environment (no tracking, no device passthrough,...), always staying in the same scene (no scene switching)?

    My idea would be to alternatively switch on/off those GameObject groups:

    1. ARSession, ARSessionOrigin and ARCamera (that is, the camera with Tracked Pose Driver component).
    2. An ordinary camera tagged "MainCamera", without AR features.

    So, could I simply enable/disable objects 1 and 2 to switch on/off the AR features in a "clean" way? That is, no further background tracking processes when I switch off group 1, and ability to resume AR tracking and real camera passthrough when I switch it on?
     
    AndreasUnityas and DiamondGear like this.
  19. ZhenyaTse

    ZhenyaTse

    Joined:
    Nov 12, 2020
    Posts:
    1
    Have you ever found out the answer to your question? It seems crazy to keep the ARSession active in vain if most of the time its not used...
     
  20. RiccardoAxed

    RiccardoAxed

    Joined:
    Aug 29, 2017
    Posts:
    119
    Having had no answers to my question, I simply applied what I was suggesting in my post, that is to put all the AR stuff under a GameObject and enabling/disabling it when needed.

    Moreover, every time I enable the AR stuff, I also call the Reset() method on the ARSession instance cause it seems to destroy all the trackables objects created in the previous session (planes, points,...). The Reset() method is rather radical cause it restarts the whole AR process from availability check and initialization, so you have to continuously check the ARSession.state if you need to perform specific actions during the various ARSessionState (SessionInitializing, SessionTracking, and so on).

    I used this routine in every AR project I worked on, without any problem.
     
  21. Sayaka_Akemi

    Sayaka_Akemi

    Joined:
    Jan 21, 2019
    Posts:
    5
    Having the exact same issue. Want to switch between "normal mode" to "ar mode".
    The normal UI is responsive and works normally. Then I disable the main camera and switch to the AR mode (enable ARSession, ARSessionOrigin). Camera works normally, UI is less responsive, but that's to be expected. The issue arises when I switch BACK to the normal mode. UI still less responsive. Tried disabling and even destroying ARSession and ARSessionOrigin objects. Something clearly goes on in the background that doesn't get cleaned-up upon disable/destroy.

    I would really love to know the proper way to stop and cleanup all the AR stuff after you initialize it for the first time. For now, the only solution seems to be to unload the scene.
     
  22. KyryloKuzyk

    KyryloKuzyk

    Joined:
    Nov 4, 2013
    Posts:
    1,143
    You can Initialize/Deinialize the loader. The scene change is not required.
    https://forum.unity.com/threads/reset-ar-session.537261/#post-6581095
     
  23. Sayaka_Akemi

    Sayaka_Akemi

    Joined:
    Jan 21, 2019
    Posts:
    5
    Thank you so much for your feedback. What you suggested does seem to reinizialize the camera after deinitializing (looking at device monitor LogCat), but my problem persists.
    I did notice, that when I enable AR objects for the second time (after disabling/deinitializing them), I see a warning: "An input device ARCore with the TrackedDevice characteristic was registered but the ARPoseDriver is already consuming data from ARCore". Which suggests that not everything was stopped/cleaned after all.

    Here's how I deinitialize my AR functionality:

    Code (CSharp):
    1. ModelViewer.SetActive(true);
    2. ARViewer.SetActive(false);
    3. ARSession.SetActive(false);
    4. ARSessionOrigin.SetActive(false);
    5. ARSession.GetComponent<ARSession>().Reset();
    6. xrManagerSettings.DeinitializeLoader();
    7. Debug.Log("Deinitialization done");
    Note: ModelViewer is an object holding normal camera and the rest for my app, ARViewer is an object holding ARSession, ARSessionOrigin and all the rest for my AR app.

    Edit: I realize disabling ARViewer container, ARSession and ARSessionOrigin before DeinitializeLoader is redundant. I'm trying to show the workflow I had before.
     
    Last edited: Mar 29, 2021
  24. Sayaka_Akemi

    Sayaka_Akemi

    Joined:
    Jan 21, 2019
    Posts:
    5
    Wow. Upon further investigation, the performance problem actually persists even after changing scenes! I'm sorry I said earlier that " the only solution seems to be to unload the scene.". I didn't actually test it because I assumed it should work. But no. I unloaded a scene after enabling AR mode portion of the app and the main app still suffers from unresponsive UI (low framerate when animating UI, etc). Everything is smooth before enabling AR objects for the first time.

    Very curious. Could this be a bug, or could it be a device issue? I'm using Samsung Galaxy Tab S3 tablet.
    Will try to run the app on other devices and report the behaviour.
     
  25. KyryloKuzyk

    KyryloKuzyk

    Joined:
    Nov 4, 2013
    Posts:
    1,143
    This is a minor bug in ARPoseDriver, but it's harmless and caused by a static 's_InputTrackingDevice' field.
    This may be the cause:
    https://forum.unity.com/threads/why...ne-make-frame-rate-drop.1027915/#post-6657781
     
    makaka-org and Sayaka_Akemi like this.
  26. Sayaka_Akemi

    Sayaka_Akemi

    Joined:
    Jan 21, 2019
    Posts:
    5
    Oh god, that was it! That was the secret global side-effect I couldn't find! Thank you so so much. Been trying to find and fix this "mysterious performance memory leak" for 2 days now.
    Thanks again for all the feedback :)

    For anyone having the same issue, don't forget to manually revert Application.targetFrameRate when you stop using ARSession with "MatchFrameRate" bool option.
     
    JoRangers and KyryloKuzyk like this.
  27. TheVirtualMunk

    TheVirtualMunk

    Joined:
    Sep 6, 2019
    Posts:
    150
    Is there any way to store the tracking data (and anchors) across scenes? I guess I can just not destroy the ARSession when changing scenes and then pause it, but would be nice to be able to store the tracking data so that we can easily change scenes but keep all the found planes and feature points (our app has a lot of going in and out of scenes with and without AR).
     
    shailadevi likes this.