From 608fce800a7a28a2351908d1c55fb00551eb6390 Mon Sep 17 00:00:00 2001
From: jlacoche <jeremy.lacoche@orange.com>
Date: Mon, 8 Jul 2024 11:29:25 +0200
Subject: [PATCH] remove some log, wait for arsession to be ready for loading
 arkit world map

---
 ...orldAnalysisARFoundationCoroutineHelper.cs |  8 +--
 ...AnalysisARFoundationModuleARKitWorldMap.cs | 55 ++++++++++++-------
 2 files changed, 37 insertions(+), 26 deletions(-)

diff --git a/Runtime/Scripts/WorldAnalysisARFoundationCoroutineHelper.cs b/Runtime/Scripts/WorldAnalysisARFoundationCoroutineHelper.cs
index 2aebd07..b240bde 100644
--- a/Runtime/Scripts/WorldAnalysisARFoundationCoroutineHelper.cs
+++ b/Runtime/Scripts/WorldAnalysisARFoundationCoroutineHelper.cs
@@ -8,11 +8,6 @@ public class WorldAnalysisARFoundationCoroutineHelper : MonoBehaviour
 
     private void Awake()
     {
-        // If there is an instance, and it's not me, delete myself.
-
-
-        Debug.Log("HERE SINGLETON AWAKE");
-
         if (Instance != null && Instance != this)
         {
             Destroy(this);
@@ -25,7 +20,6 @@ public class WorldAnalysisARFoundationCoroutineHelper : MonoBehaviour
 
     public void StartACoroutine(IEnumerator coroutine)
     {
-        Debug.Log("HERE SINGLETON");
         StartCoroutine(coroutine);
     }
-}
+}
\ No newline at end of file
diff --git a/Runtime/Scripts/WorldAnalysisARFoundationModuleARKitWorldMap.cs b/Runtime/Scripts/WorldAnalysisARFoundationModuleARKitWorldMap.cs
index 5bb1ea4..5b1ef20 100644
--- a/Runtime/Scripts/WorldAnalysisARFoundationModuleARKitWorldMap.cs
+++ b/Runtime/Scripts/WorldAnalysisARFoundationModuleARKitWorldMap.cs
@@ -119,6 +119,8 @@ public class WorldAnalysisARFoundationModuleARKitWorldMap : WorldAnalysisARFound
     {
         foreach (var trackedAnchor in eventArgs.updated)
         {
+            Debug.Log("Anchor found  " +trackedAnchor.trackableId.ToString());
+
             if (trackedAnchor.trackableId.ToString() == m_arfoundationAnchorTrackableId)
             {
                 /// look for an anchor with the trackable Id correspond to the ETSI ARF trackable name
@@ -137,12 +139,14 @@ public class WorldAnalysisARFoundationModuleARKitWorldMap : WorldAnalysisARFound
     {
         if (url.Length > 0)
         {
+             Debug.Log("Load AR Map from URL");
             LoadWorldMapFromURL(url);
             // don't check if url is valid
             return true ;
         }
         else 
         {
+             Debug.Log("Load AR Map locally");
             string localMap = Application.persistentDataPath + "/ARkitWorlMap.map";
             if (File.Exists(localMap))
             {
@@ -191,31 +195,44 @@ public class WorldAnalysisARFoundationModuleARKitWorldMap : WorldAnalysisARFound
     /// <returns>coroutine</returns>
     public IEnumerator CoroutineLoadWorldMap(string mapPath)
     {
+        while (ARSession.state == ARSessionState.CheckingAvailability || ARSession.state == ARSessionState.None ||ARSession.state == ARSessionState.SessionInitializing)
+        {
+            // wait for ar session to be ready
+            yield return null ;
+        }
+
         ARSession session = Component.FindAnyObjectByType<ARSession>() ;
         ARKitSessionSubsystem sessionSubsystem = (ARKitSessionSubsystem)session.subsystem;
-        FileStream file;
-        file = File.Open(mapPath, FileMode.Open);
-        const int bytesPerFrame = 1024 * 10;
-        var bytesRemaining = file.Length;
-        var binaryReader = new BinaryReader(file);
-        var allBytes = new List<byte>();
-        while (bytesRemaining > 0)
+        if (sessionSubsystem == null)
         {
-            var bytes = binaryReader.ReadBytes(bytesPerFrame);
-            allBytes.AddRange(bytes);
-            bytesRemaining -= bytesPerFrame;
-            yield return null;
+            Debug.Log("Cannot load map: no ARKitSessionSubsystem");
         }
-
-        var data = new NativeArray<byte>(allBytes.Count, Allocator.Temp);
-        data.CopyFrom(allBytes.ToArray());
-        ARWorldMap worldMap;
-        if (ARWorldMap.TryDeserialize(data, out worldMap))
+        else 
         {
-            data.Dispose();
+            FileStream file;
+            file = File.Open(mapPath, FileMode.Open);
+            const int bytesPerFrame = 1024 * 10;
+            var bytesRemaining = file.Length;
+            var binaryReader = new BinaryReader(file);
+            var allBytes = new List<byte>();
+            while (bytesRemaining > 0)
+            {
+                var bytes = binaryReader.ReadBytes(bytesPerFrame);
+                allBytes.AddRange(bytes);
+                bytesRemaining -= bytesPerFrame;
+                yield return null;
+            }
+
+            var data = new NativeArray<byte>(allBytes.Count, Allocator.Temp);
+            data.CopyFrom(allBytes.ToArray());
+            ARWorldMap worldMap;
+            if (ARWorldMap.TryDeserialize(data, out worldMap))
+            {
+                data.Dispose();
+            }
+            sessionSubsystem.ApplyWorldMap(worldMap);
+            UpdateTrackableInfoWithPose(Vector3.zero, Quaternion.identity); // before trying to find an anchor: default pause is origin of the map
         }
-        sessionSubsystem.ApplyWorldMap(worldMap);
-        UpdateTrackableInfoWithPose(Vector3.zero, Quaternion.identity); // before trying to find an anchor: default pause is origin of the map
     }
 }
 #endif
\ No newline at end of file
-- 
GitLab