using System; using System.Runtime.InteropServices; using UnityEngine; using UnityEngine.XR.MagicLeap.Native; using static UnityEngine.XR.MagicLeap.InputSubsystem.Extensions.MLHeadTracking.NativeBindings; namespace UnityEngine.XR.MagicLeap { public static partial class InputSubsystem { public static partial class Extensions { [Obsolete] public static bool TryGetHeadTrackingState(InputDevice headDevice, out MLHeadTracking.State headTrackingState) => MLHeadTracking.TryGetState(headDevice, out headTrackingState); public static bool TryGetHeadTrackingStateEx(InputDevice headDevice, out MLHeadTracking.StateEx headTrackingState) => MLHeadTracking.TryGetStateEx(headDevice, out headTrackingState); public static class MLHeadTracking { /// /// A set of possible error conditions that can cause Head Tracking to be less than ideal. /// [Obsolete] public enum TrackingError { /// /// No error, tracking is nominal. /// None, /// /// There are not enough features in the environment. /// NotEnoughFeatures, /// /// Lighting in the environment is not sufficient to track accurately. /// LowLight, /// /// Head tracking failed for an unknown reason. /// Unknown } /// /// A set of possible error conditions that can cause Head Tracking to be less than ideal. /// [Flags] public enum TrackingErrorFlag { /// /// No error, tracking is nominal. /// None = 0, /// /// Head tracking failed for an unknown reason. /// Unknown = 1 << 0, /// /// There are not enough features in the environment. /// NotEnoughFeatures = 1 << 1, /// /// Lighting in the environment is not sufficient to track accurately. /// LowLight = 1 << 2, /// /// Head tracking failed due to excessive motion. /// ExcessiveMotion = 1 << 3 } /// /// A set of possible tracking modes the Head Tracking system can be in. /// [Obsolete] public enum TrackingMode { /// /// Full 6 degrees of freedom tracking (position and orientation). /// Mode6DOF, /// /// Head tracking is unavailable. /// ModeUnavailable } /// /// A set of possible tracking status for the Head Tracking system. /// public enum HeadTrackingStatus { /// /// Head tracking is unavailable. /// Invalid = 0, /// /// Head tracking is initializing. /// Initializing = 1, /// /// Head tracking is relocalizing. /// Relocalizing = 2, /// /// Valid head tracking data is available. /// Valid = 100 } /// /// A set of all types of map events that can occur that a developer /// may have to handle. /// [Flags] public enum MapEvents : ulong { /// /// Map was lost. It could possibly recover. /// Lost = (1 << 0), /// /// Previous map was recovered. /// Recovered = (1 << 1), /// /// Failed to recover previous map. /// RecoveryFailed = (1 << 2), /// /// New map session created. /// NewSession = (1 << 3) } [Obsolete] public static bool TryGetState(InputDevice headDevice, out State headTrackingState) => NativeBindings.TryGetState(headDevice, out headTrackingState); public static bool TryGetStateEx(InputDevice headDevice, out StateEx headTrackingState) { if(MLDevice.IsOpenXRLoaderActive()) { return global::MagicLeap.OpenXR.LegacySupport.MLHeadTracking.TryGetStateEx(out headTrackingState); } else { return NativeBindings.TryGetStateEx(headDevice, out headTrackingState); } } public static bool TryGetMapEvents(InputDevice headDevice, out MapEvents mapEvents) { if (MLDevice.IsOpenXRLoaderActive()) { return global::MagicLeap.OpenXR.LegacySupport.MLHeadTracking.TryGetMapEvents(out mapEvents); } else { return NativeBindings.TryGetMapEvents(headDevice, out mapEvents); } } private static bool gotData = false; private static NativeBindings.MLHeadTrackingStaticData data; public static void GetStaticData(out MagicLeapNativeBindings.MLCoordinateFrameUID outUID) { outUID = MagicLeapNativeBindings.MLCoordinateFrameUID.EmptyFrame; if (!gotData) { var headHandle = MagicLeapXrProviderNativeBindings.GetHeadTrackerHandle(); data = new(); MLResult result = MLResult.Create(MLHeadTrackingGetStaticData(headHandle, ref data)); if (!result.IsOk) { Debug.LogError($"MLHeadTracking::GetStaticData failed: {result}"); gotData = false; return; } } outUID = data.coord_frame_head; return; } /// /// A structure containing information on the current state of the /// Head Tracking system. /// [Obsolete] public readonly struct State { /// /// What tracking mode the Head Tracking system is currently in. /// public readonly TrackingMode Mode; /// /// A confidence value (from 0..1) representing the confidence in the /// current pose estimation. /// public readonly float Confidence; /// /// Represents what tracking error (if any) is present. /// public readonly TrackingError Error; internal State(NativeBindings.State nativeState) { this.Mode = nativeState.Mode; this.Confidence = nativeState.Confidence; this.Error = nativeState.Error; } public override string ToString() { return $"Mode: {Mode}, Condidence: {Confidence}, Error: {Error}"; } } /// /// A structure containing information on the current state of the /// Head Tracking system. /// public readonly struct StateEx { /// /// Current status of the Head Tracking system. /// public readonly HeadTrackingStatus Status; /// /// A confidence value (from 0..1) representing the confidence in the /// current pose estimation. /// public readonly float Confidence; /// /// Represents what tracking error (if any) is present. /// public readonly TrackingErrorFlag Error; internal StateEx(NativeBindings.MLHeadTrackingStateEx nativeState) { this.Status = nativeState.Status; this.Confidence = nativeState.Confidence; this.Error = (TrackingErrorFlag)nativeState.Error; } public override string ToString() { return $"Status: {Status}, Condidence: {Confidence}, Error: {Error}"; } } internal static class NativeBindings { [Obsolete] private static byte[] allocatedHeadTrackingStateData = new byte[Marshal.SizeOf()]; private static byte[] allocatedHeadTrackingStateExData = new byte[Marshal.SizeOf()]; private static byte[] allocatedHeadTrackingMapEventsData = new byte[sizeof(MapEvents)]; [Obsolete] public static bool TryGetState(InputDevice device, out MLHeadTracking.State state) { if (!device.TryGetFeatureValue(InputSubsystem.Extensions.DeviceFeatureUsages.Head.TrackingState, allocatedHeadTrackingStateData)) goto Failure; try { IntPtr ptr = Marshal.AllocHGlobal(allocatedHeadTrackingStateData.Length); Marshal.Copy(allocatedHeadTrackingStateData, 0, ptr, allocatedHeadTrackingStateData.Length); var nativeState = Marshal.PtrToStructure(ptr); Marshal.FreeHGlobal(ptr); state = new MLHeadTracking.State(nativeState); return true; } catch (Exception e) { Debug.LogError("TryGetState failed with the exception: " + e); goto Failure; } Failure: state = default; return false; } public static bool TryGetStateEx(InputDevice device, out MLHeadTracking.StateEx state) { if (!device.TryGetFeatureValue(InputSubsystem.Extensions.DeviceFeatureUsages.Head.TrackingStateEx, allocatedHeadTrackingStateExData)) goto Failure; try { IntPtr ptr = Marshal.AllocHGlobal(allocatedHeadTrackingStateExData.Length); Marshal.Copy(allocatedHeadTrackingStateExData, 0, ptr, allocatedHeadTrackingStateExData.Length); var nativeState = Marshal.PtrToStructure(ptr); Marshal.FreeHGlobal(ptr); state = new MLHeadTracking.StateEx(nativeState); return true; } catch (Exception e) { Debug.LogError("TryGetStateEx failed with the exception: " + e); goto Failure; } Failure: state = default; return false; } public static bool TryGetMapEvents(InputDevice device, out MLHeadTracking.MapEvents mapEvents) { if (!device.TryGetFeatureValue(InputSubsystem.Extensions.DeviceFeatureUsages.Head.TrackingMapEvents, allocatedHeadTrackingMapEventsData)) { mapEvents = default; return false; } try { mapEvents = (MapEvents)BitConverter.ToUInt64(allocatedHeadTrackingMapEventsData); return (mapEvents != 0); } catch (Exception e) { Debug.LogError("TryGetMapEvents failed with the exception: " + e); mapEvents = default; return false; } } /// /// A structure containing information on the current state of the /// Head Tracking system. /// [Obsolete] [StructLayout(LayoutKind.Sequential)] public readonly struct State { /// /// What tracking mode the Head Tracking system is currently in. /// public readonly TrackingMode Mode; /// /// A confidence value (from 0..1) representing the confidence in the /// current pose estimation. /// public readonly float Confidence; /// /// Represents what tracking error (if any) is present. /// public readonly TrackingError Error; } /// /// A structure containing information on the current state of the /// Head Tracking system. /// [StructLayout(LayoutKind.Sequential)] public readonly struct MLHeadTrackingStateEx { /// /// Version of this structure. /// public readonly uint Version; /// /// Current status of the Head Tracking system. /// public readonly HeadTrackingStatus Status; /// /// A confidence value (from 0..1) representing the confidence in the /// current pose estimation. /// public readonly float Confidence; /// /// Represents what tracking error (if any) is present. /// public readonly uint Error; } /// /// Returns static information about the Head Tracker /// /// A handle to the tracker retireved by GetHeadTrackerHandle() /// Target to populate the data about that Head Tracker /// /// MLResult.Result will be MLResult.Code.InvalidParam if failed to receive static data due to an invalid input parameter. /// MLResult.Result will be MLResult.Code.Ok if successfully received static data. /// MLResult.Result will be MLResult.Code.UnspecifiedFailure if failed to receive static data due to an unknown error. /// [DllImport(MagicLeapNativeBindings.MLPerceptionClientDll, CallingConvention = CallingConvention.Cdecl)] public static extern MLResult.Code MLHeadTrackingGetStaticData(ulong handle, ref MLHeadTrackingStaticData data); /// /// Static information about a Head Tracker. Populate this structure with MLHeadTrackingGetStaticData() /// [StructLayout(LayoutKind.Sequential)] public struct MLHeadTrackingStaticData { /// /// Coordinate frame ID of the head /// public MagicLeapNativeBindings.MLCoordinateFrameUID coord_frame_head; } } } } } }