Commit b665bf1f authored by Sylvain Buche's avatar Sylvain Buche
Browse files
parents 086784eb 64114a03
Loading
Loading
Loading
Loading
+41 −0
Original line number Diff line number Diff line
@@ -49,5 +49,46 @@ The name of the Trackable in the World Storage must correspond to the name of th

More information about ARFoundation Mesh Tracking can be found here: https://docs.unity3d.com/Packages/com.unity.xr.arfoundation@5.1/manual/features/object-tracking.html

### Map Trackables ###

#### iOS: ARWorldMap ####

On iOS/ARKit Map Trackables are supported with  ARWorlMap feature. Only one Map Trackable per World Graph is supported.

In ARKit, an ARWorldMap includes ARKit's awareness of the physical space in which the user moves the device that can be serialized into a file to be reloaded later on the same or on another device: 
https://developer.apple.com/documentation/arkit/arworldmap

The .map file can be placed in the Unity persistent data path of the application on the user device with the following name: "ARkitWorlMap.map". Alternatively, the variable keyvalueTags of the Trackable can also contain a parameter with the "url" key providing a link to download the map file.

By default, the origin of the Map Trackable is the point (0, 0 ,0). If the Map includes an Anchor, you can use it as the origin by setting the name of the Trackable in the World Storage with the TrackableId of the ARAnchor stored in the map. https://docs.unity3d.com/Packages/com.unity.xr.arfoundation@5.1/api/UnityEngine.XR.ARSubsystems.TrackableId.html

Samples for creating and serializing an ARWorldMap with ARFoundation can be found here:
https://github.com/Unity-Technologies/arfoundation-samples/blob/main/Assets/Scripts/Runtime/ARWorldMapController.cs 

#### Android ARCore Cloud Anchors ####

On Android/ARcore Map Trackables are supported with Cloud Anchors.

Cloud Anchors are anchors that are hosted on the ARCore API cloud endpoint. This hosting enables users to share experiences in the same app: see https://developers.google.com/ar/develop/cloud-anchors

For using ARCore cloud anchors you need to:
* Add the the arcore unity extensions package as a dependency of the Unity project  https://github.com/google-ar/arcore-unity-extensions
* Add to the runtime asmdef of this package a reference to Google.XR.ARCoreExtensions.asmdef
* Add a new Script Define Symbol in the Unity project settings: "ETSIARF_ARCORE_EXTENSIONS"
* Then, follow the instructions detailed here https://developers.google.com/ar/develop/unity-arf/cloud-anchors/developer-guide and here  https://developers.google.com/ar/develop/authorization?platform=unity-arf to configure the project and your google cloud account

To use a Map Trackable that corresponds to a Google Cloud Anchor, the name of the Trackable in the World Storage must correspond to the name of Google UUID of the anchor.

### Geo Trackables ###

#### iOS: perspectives ####

On iOS, Geo Trackables are not yet supported with this package.

A first possibility for adding this support is to adapt the Google GeoSpatial code and then run your iOS application with the ARCore AR backend.

A second possibility is to add the support for ARKit GeoTracking: https://developer.apple.com/documentation/arkit/arkit_in_ios/content_anchors/tracking_geographic_locations_in_ar
This feature is not implemented yet due to the small number of places supported (see https://developer.apple.com/documentation/arkit/argeotrackingconfiguration)

#### Android: Google GeoSpatial ####
To come
 No newline at end of file
+2 −0
Original line number Diff line number Diff line
@@ -89,6 +89,8 @@ public class WorldAnalysisARFoundation : MonoBehaviour, WorldAnalysisInterface
#if UNITY_IOS
        WorldAnalysisARFoundationModuleMesh meshModule = new WorldAnalysisARFoundationModuleMesh();
        m_trackableModules.Add(meshModule);
        WorldAnalysisARFoundationModuleARKitWorldMap worldMapModule = new WorldAnalysisARFoundationModuleARKitWorldMap();
        m_trackableModules.Add(worldMapModule);
#endif
#if ETSIARF_ARCORE_EXTENSIONS
    
+1 −7
Original line number Diff line number Diff line
@@ -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
+6 −6
Original line number Diff line number Diff line
@@ -41,10 +41,10 @@ public class WorldAnalysisARFoundationModuleARCoreAnchor : WorldAnalysisARFounda
    }

    /// <summary>
    /// 
    /// Get the pose of the trackable from its uuid
    /// </summary>
    /// <param name="uuid"></param>
    /// <returns></returns>
    /// <param name="uuid">id of the trackable</param>
    /// <returns>null or trackableInfo with last updated values</returns>
    public TrackableInfo GetPoseTrackable(Guid uuid)
    {
        if (m_trackedCloudAnchors.ContainsKey(uuid.ToString()))
@@ -58,10 +58,10 @@ public class WorldAnalysisARFoundationModuleARCoreAnchor : WorldAnalysisARFounda
    }

    /// <summary>
    /// 
    /// Need to be a map
    /// </summary>
    /// <param name="trackable"></param>
    /// <returns></returns>
    /// <returns>map or not (does not check is solved)</returns>
    public bool AddTrackable(ETSI.ARF.OpenAPI.WorldStorage.Trackable trackable)
    {
        /// Here : we don't check if the trackable is allready added, AddImageToLibrary does it
@@ -134,7 +134,7 @@ public class WorldAnalysisARFoundationModuleARCoreAnchor : WorldAnalysisARFounda
            info.timeStamp = unchecked((int)DateTimeOffset.UtcNow.ToUnixTimeMilliseconds());
            info.position = position;
            info.rotation = rotation;
            info.trackableType = ETSI.ARF.OpenAPI.WorldAnalysis.TrackableType.IMAGE_MARKER;
            info.trackableType = ETSI.ARF.OpenAPI.WorldAnalysis.TrackableType.MAP;
            m_trackedCloudAnchors[info.name] = info;
        }
    }
+238 −0
Original line number Diff line number Diff line

#if UNITY_IOS
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
using UnityEngine.XR.ARKit;
using System.IO;
using Unity.XR.CoreUtils;
using static WorldAnalysisARFoundationModule;
using ETSI.ARF.OpenAPI.WorldStorage;
using Unity.Collections;


public class WorldAnalysisARFoundationModuleARKitWorldMap : WorldAnalysisARFoundationModule
{
    /// <summary>
    ///  Anchor manager
    /// </summary>
    private ARAnchorManager m_anchorManager;
    ///Has loaded a worl map
    private bool m_hasAddedMap ;
    /// Possible trackable id (arfoundation) of an anchor contained in the world map. Take the pose of this anchor if it exists or zero
    private string m_arfoundationAnchorTrackableId ;
    /// uuId of the map trackabled 
    private  Guid   m_trackedUUID ;
    /// computed and updated pose 
    private TrackableInfo m_trackedPose ;

    /// <summary>
    /// Initialize image tracking module
    /// </summary>
    public void Initialize()
    {
        XROrigin origin = UnityEngine.Object.FindAnyObjectByType<XROrigin>();
        m_anchorManager = origin.gameObject.AddComponent<ARAnchorManager>();
        GameObject anchorPrefab = (GameObject)Resources.Load("ARFAnchorTrackingPrefab");
        m_anchorManager.anchorPrefab = anchorPrefab;
        m_anchorManager.anchorsChanged += OnTrackedAnchorChanged;
        m_trackedPose = null ;
        m_hasAddedMap= false ;
    }

    /// <summary>
    ///  found  
    /// </summary>
    /// <param name="uuid">name of the mesh trackable</param>
    public TrackableInfo GetPoseTrackable(Guid uuid)
    {
        if (m_trackedPose != null)
        {
            if (m_trackedUUID == uuid)
            {
                return m_trackedPose ;
            }
        }
        return null; 
    }

    /// <summary>
    /// Add a trackable : need to be a map
    /// </summary>
    /// <param name="trackable">Image trackable</param>
    /// <returns>Supported or not</returns>
    public bool AddTrackable(Trackable trackable)
    {
        if (trackable.TrackableType != ETSI.ARF.OpenAPI.WorldStorage.TrackableType.MAP)
        {
            return false;
        }
        if (m_hasAddedMap) 
        {
            Debug.Log("Only one ARKit map can be loaded");
            return false ;
        }

        // Check if a map url is provided
        string url = "" ;
        if (trackable.KeyvalueTags.ContainsKey("url"))
        {
            foreach(string s in trackable.KeyvalueTags["url"])
            {
                // first one
                url = s ;
                break ;
            }
        }
        bool resul = AddWorldMapToARKit(url); 
        m_trackedUUID = trackable.UUID ;
        m_arfoundationAnchorTrackableId = trackable.Name ;
        m_hasAddedMap = resul ;
        return resul;
    }

    /// <summary>
    ///  Initialize capability object with Mesh tracking
    /// </summary>
    public ETSI.ARF.OpenAPI.WorldAnalysis.Capability  GetSupportedCapability()
    {
        ETSI.ARF.OpenAPI.WorldAnalysis.Capability capabilityMesh = new ETSI.ARF.OpenAPI.WorldAnalysis.Capability();
        capabilityMesh.TrackableType = ETSI.ARF.OpenAPI.WorldAnalysis.TrackableType.MAP;
        ETSI.ARF.OpenAPI.WorldAnalysis.EncodingInformationStructure encodingInformation = new ETSI.ARF.OpenAPI.WorldAnalysis.EncodingInformationStructure();
        encodingInformation.DataFormat = ETSI.ARF.OpenAPI.WorldAnalysis.EncodingInformationStructureDataFormat.ARKIT;
        encodingInformation.Version = "1.01";
        capabilityMesh.EncodingInformation = encodingInformation;
        capabilityMesh.Framerate = 30; // Not particularly specified on ARKit 
        capabilityMesh.Latency = 0; // Not particularly specified on ARKit
        capabilityMesh.Accuracy = 1; // Not particularly specified on ARKit
        return capabilityMesh ;
    }

    /// <summary>
    /// Callback when Unity anchors are updated
    /// </summary>
    /// <param name="eventArgs">update values</param>
    private void OnTrackedAnchorChanged(ARAnchorsChangedEventArgs eventArgs)
    {
        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
                UpdateTrackableInfoWithPose(trackedAnchor.transform.position,trackedAnchor.transform.rotation);
                break ;
            }
        }
    }

    /// <summary>
    /// Add a map to arkitsubsystem : check local file exist or download from given url
    /// </summary>
    /// <param name="url">url of a map to download</param>
    /// <returns>found or not</returns>
    protected bool AddWorldMapToARKit(string url)
    {
        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))
            {
                var coroutine = CoroutineLoadWorldMap(localMap);
                WorldAnalysisARFoundationCoroutineHelper.Instance.StartACoroutine(coroutine);
                return true ;
            }
        }
        // no url and no local map
        return false ;
    }

    /// <summary>
    /// Update pose of the map trackable
    /// </summary>
    /// </param name="position"> evaluated position of trackable/param>
    /// </param name="rotation"> evaluated rotation of the trackable</param>
    private void UpdateTrackableInfoWithPose(Vector3 position, Quaternion rotation)
    {
        m_trackedPose = new TrackableInfo();
        m_trackedPose.name = m_arfoundationAnchorTrackableId;
        m_trackedPose.state = ETSI.ARF.OpenAPI.WorldAnalysis.PoseEstimationState.OK; // here : could check the state of the ARKit slam
        m_trackedPose.confidence = 100;
        m_trackedPose.timeStamp = unchecked((int)DateTimeOffset.UtcNow.ToUnixTimeMilliseconds());
        m_trackedPose.position = position;
        m_trackedPose.rotation = rotation;
        m_trackedPose.trackableType = ETSI.ARF.OpenAPI.WorldAnalysis.TrackableType.MAP;
    }

    /// <summary>
    /// Load a world map from a url
    /// </summary>
    /// </param name="url"> url to the worlmap to download</param>
    private async void LoadWorldMapFromURL(string url)
    {
        Debug.Log("Download WorlMap from url "+ url);
        KeyValuePair<string , string> downloaded = await WorldAnalysisARFoundationHelper.DownloadFileHTTP(url);
        var coroutine = CoroutineLoadWorldMap(downloaded.Key);
        WorldAnalysisARFoundationCoroutineHelper.Instance.StartACoroutine(coroutine);
    }

    /// <summary>
    /// Load current map from path
    /// </summary>
    /// <param name="mapPath">path of the map</param>
    /// <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;
        if (sessionSubsystem == null)
        {
            Debug.Log("Cannot load map: no ARKitSessionSubsystem");
        }
        else 
        {
            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
        }
    }
}
#endif
 No newline at end of file
Loading