Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

ARKit 2 ARWorldMap scanning issue

Discussion in 'AR' started by hayk_hakobyan_frismos, Jan 30, 2019.

  1. hayk_hakobyan_frismos

    hayk_hakobyan_frismos

    Joined:
    Jul 11, 2018
    Posts:
    4
    Hi. I have an issue on my IPhone XS (IOS 12.1.2) when scanning ARWorldMap.
    When i start scanning the ARWorldMap at first time everything works fine. But when I reloading the scene and restarting the session nothing happens. Feature points and surfaces detection is stopping.
    Everything is working perfectly on IPad Pro (A1671), IPad (6th generation(A1893)) and IPhone X. Here is my code:
    Code (CSharp):
    1.  
    2. public bool getPointCloud = true;
    3. public bool enableLightEstimation = true;
    4. public bool enableAutoFocus = false;
    5. private bool sessionStarted = false;
    6. private bool areaIsMaped = false;
    7.  
    8. private Camera m_camera;
    9. public ARReferenceImagesSet detectionImages = null;
    10. private UnityARSessionNativeInterface m_session;
    11. public UnityARAlignment startAlignment = UnityARAlignment.UnityARAlignmentGravity;
    12. public UnityARPlaneDetection planeDetection = UnityARPlaneDetection.Horizontal;
    13. private UnityARSessionRunOption runOptions = UnityARSessionRunOption.ARSessionRunOptionRemoveExistingAnchors | UnityARSessionRunOption.ARSessionRunOptionResetTracking;
    14.  
    15. private void StartSession(ARWorldMap arWorldMap = null)
    16. {
    17.     m_session = UnityARSessionNativeInterface.GetARSessionNativeInterface();
    18.  
    19.     ARKitWorldTrackingSessionConfiguration config = new ARKitWorldTrackingSessionConfiguration();
    20.     config.planeDetection = planeDetection;
    21.     config.alignment = startAlignment;
    22.     config.getPointCloudData = getPointCloud;
    23.     config.enableLightEstimation = enableLightEstimation;
    24.     config.enableAutoFocus = enableAutoFocus;
    25.     config.worldMap = arWorldMap;
    26.  
    27.     if (detectionImages != null)
    28.     {
    29.         config.referenceImagesGroupName = detectionImages.resourceGroupName;
    30.     }
    31.  
    32.     if (config.IsSupported)
    33.     {
    34.         m_session.RunWithConfigAndOptions(config,runOptions);
    35.         UnityARSessionNativeInterface.ARFrameUpdatedEvent += FirstFrameUpdate;
    36.     }
    37. }
    38.  
    39. private void FirstFrameUpdate(UnityARCamera cam)
    40. {
    41.     sessionStarted = true;
    42.     UnityARSessionNativeInterface.ARFrameUpdatedEvent -= FirstFrameUpdate;
    43. }
    44.  
    45. private void Update()
    46. {
    47.     if (m_camera != null && sessionStarted)
    48.     {
    49.         // JUST WORKS!
    50.         Matrix4x4 matrix = m_session.GetCameraPose();
    51.         m_camera.transform.localPosition = UnityARMatrixOps.GetPosition(matrix);
    52.         m_camera.transform.localRotation = UnityARMatrixOps.GetRotation(matrix);
    53.  
    54.         m_camera.projectionMatrix = m_session.GetCameraProjection();
    55.     }
    56. }
    I have tried everything, but nothing helps! ))) Please HELP !!!
     
  2. Alex_F

    Alex_F

    Joined:
    Mar 4, 2014
    Posts:
    10
    Hey! Have you solved your issue?
    I've experienced something similar lately: there was something wrong with tracking after changing scenes (e.g. back to menu and then to the AR scene). It seems to work now. I'm not sure what exactly helped. Among other I've added some clean up code in the menu scene before going back to AR.
    Code (CSharp):
    1. Resources.UnloadUnusedAssets();
    2. System.GC.Collect();
    Also unsubscribed of all events before exiting AR (onDestroy).
     
  3. hayk_hakobyan_frismos

    hayk_hakobyan_frismos

    Joined:
    Jul 11, 2018
    Posts:
    4
    In my case Vuforia is causing this issue. I have 2 scenes: 1. Vuforia 2. ARKit2

    I can switch between the scenes using SceneManager.LoadScene("Vuforia or ARKit2").

    When I switch from "Vuforia" to "ARKit2" scene everything is fine. But if after that I switch back to "Vuforia" and once again to "ARKit2" feature points start floating and horizontal surface will never be detected. But if I use other scene instead of "Vuforia" everything works perfectly!
     
  4. hayk_hakobyan_frismos

    hayk_hakobyan_frismos

    Joined:
    Jul 11, 2018
    Posts:
    4
    I don't have any solution yet.
     
  5. hayk_hakobyan_frismos

    hayk_hakobyan_frismos

    Joined:
    Jul 11, 2018
    Posts:
    4
    After a long time of researching I found the solution!!! Each time I switch from "Vuforia" to "ARKit 2" scene the first thing I need to do is create a new NativeARSession to override changes made by Vuforia plugin. It is easy to do. I just need to call the constructor of "UnityARSessionNativeInterface" class. Here is my code:
    Code (CSharp):
    1.  public UnityARSessionNativeInterface()
    2.         {
    3. #if !UNITY_EDITOR && UNITY_IOS
    4.             m_NativeARSession = unity_CreateNativeARSession();
    5.             session_SetSessionCallbacks(m_NativeARSession, _frame_update, _ar_session_failed, _ar_session_interrupted,
    6.             _ar_session_interruption_ended, _ar_session_should_relocalize, _ar_tracking_changed, _ar_session_get_world_map_completion_handler,
    7.             _ar_session_ref_obj_extract_completion_handler);
    8.             session_SetPlaneAnchorCallbacks(m_NativeARSession, _anchor_added, _anchor_updated, _anchor_removed);
    9.             session_SetUserAnchorCallbacks(m_NativeARSession, _user_anchor_added, _user_anchor_updated, _user_anchor_removed);
    10.             session_SetFaceAnchorCallbacks(m_NativeARSession, _face_anchor_added, _face_anchor_updated, _face_anchor_removed);
    11.             session_SetImageAnchorCallbacks(m_NativeARSession, _image_anchor_added, _image_anchor_updated, _image_anchor_removed);
    12.             session_SetObjectAnchorCallbacks(m_NativeARSession, _object_anchor_added, _object_anchor_updated, _object_anchor_removed);
    13.             session_SetEnvironmentProbeAnchorCallbacks(m_NativeARSession, _envprobe_anchor_added, _envprobe_anchor_updated, _envprobe_anchor_removed);
    14. #endif
    15.         }
    16.  
    17. public static void CreateARSession()
    18.         {
    19.             s_UnityARSessionNativeInterface = new UnityARSessionNativeInterface();
    20.         }