// %BANNER_BEGIN% // --------------------------------------------------------------------- // %COPYRIGHT_BEGIN% // Copyright (c) (2024) Magic Leap, Inc. All Rights Reserved. // Use of this file is governed by the Software License Agreement, located here: https://www.magicleap.com/software-license-agreement-ml2 // Terms and conditions applicable to third-party materials accompanying this distribution may also be found in the top-level NOTICE file appearing herein. // %COPYRIGHT_END% // --------------------------------------------------------------------- // %BANNER_END% using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem.Controls; using UnityEngine.InputSystem.Layouts; using UnityEngine.InputSystem.XR; using UnityEngine.Scripting; using UnityEngine.XR; using UnityEngine.XR.ARSubsystems; using UnityEngine.XR.OpenXR.Features; using UnityEngine.XR.OpenXR.Input; #if USE_INPUT_SYSTEM_POSE_CONTROL using PoseControl = UnityEngine.InputSystem.XR.PoseControl; #else using PoseControl = UnityEngine.XR.OpenXR.Input.PoseControl; #endif #if UNITY_EDITOR using UnityEditor; using UnityEditor.XR.OpenXR.Features; #endif namespace MagicLeap.OpenXR.InteractionProfiles { /// /// This enables the use of interaction profiles in OpenXR. /// #if UNITY_EDITOR [UnityEditor.XR.OpenXR.Features.OpenXRFeature(UiName = "Magic Leap 2 Controller Interaction Profile", BuildTargetGroups = new[] {BuildTargetGroup.Android, BuildTargetGroup.Standalone}, Company = "Magic Leap", Desc = "Allows for mapping input to the Magic Leap 2 Controller.", OpenxrExtensionStrings = ExtensionString, Version = "1.0.0", Category = UnityEditor.XR.OpenXR.Features.FeatureCategory.Interaction, FeatureId = FeatureId)] #endif public class MagicLeapControllerProfile : OpenXRInteractionFeature { /// /// The feature id string. This is used to give the feature a well known id for reference. /// public const string FeatureId = "com.magicleap.openxr.feature.input.ml2controllerprofile"; /// /// Extension required for enable the Magic Leap 2 Controller Interaction Profile. /// public const string ExtensionString = "XR_ML_ml2_controller_interaction"; /// /// An Input System device based off the Magic Leap Controller interaction profile. This device contains one haptic output motor. /// [Preserve, InputControlLayout(displayName = "Magic Leap Controller (OpenXR)", commonUsages = new[] { "LeftHand", "RightHand" })] public class MagicLeapController : XRControllerWithRumble { /// /// A [ButtonControl](xref:UnityEngine.InputSystem.Controls.ButtonControl) that represents information from the OpenXR binding. /// [Preserve, InputControl(aliases = new[] { "GripButton", "shoulderClicked"}, usage = "GripButton")] public ButtonControl gripPressed { get; private set; } /// /// A [ButtonControl](xref:UnityEngine.InputSystem.Controls.ButtonControl) that represents the OpenXR binding. /// [Preserve, InputControl(aliases = new[] { "Primary", "menubutton" }, usage ="MenuButton")] public ButtonControl menu { get; private set; } /// /// A that represents information from the OpenXR binding. /// [Preserve, InputControl(offset = 0, aliases = new[] { "device", "gripPose" }, usage = "Device")] public PoseControl devicePose { get; private set; } /// /// A that represents information from the OpenXR binding. /// [Preserve, InputControl(offset = 0, alias = "aimPose", usage = "Pointer")] public PoseControl pointer { get; private set; } /// /// A [ButtonControl](xref:UnityEngine.InputSystem.Controls.ButtonControl) required for backwards compatibility with the XRSDK layouts. This represents the overall tracking state of the device. This value is equivalent to mapping devicePose/isTracked. /// [Preserve, InputControl(offset = 2)] public new ButtonControl isTracked { get; private set; } /// /// A [IntegerControl](xref:UnityEngine.InputSystem.Controls.IntegerControl) required for backwards compatibility with the XRSDK layouts. This represents the bit flag set indicating what data is valid. This value is equivalent to mapping devicePose/trackingState. /// [Preserve, InputControl(offset = 4)] public new IntegerControl trackingState { get; private set; } /// /// A [Vector3Control](xref:UnityEngine.InputSystem.Controls.Vector3Control) required for backwards compatibility with the XRSDK layouts. This is the device position, or grip position. This value is equivalent to mapping devicePose/position. /// [Preserve, InputControl(offset = 8, alias = "gripPosition")] public new Vector3Control devicePosition { get; private set; } /// /// A [QuaternionControl](xref:UnityEngine.InputSystem.Controls.QuaternionControl) required for backwards compatibility with the XRSDK layouts. This is the device orientation, or grip orientation. This value is equivalent to mapping devicePose/rotation. /// [Preserve, InputControl(offset = 20, alias = "gripOrientation")] public new QuaternionControl deviceRotation { get; private set; } /// /// A [Vector3Control](xref:UnityEngine.InputSystem.Controls.Vector3Control) required for backwards compatibility with the XRSDK layouts. This is the pointer position. This value is equivalent to mapping pointerPose/position. /// [Preserve, InputControl(offset = 68)] public Vector3Control pointerPosition { get; private set; } /// /// A [QuaternionControl](xref:UnityEngine.InputSystem.Controls.QuaternionControl) required for backwards compatibility with the XRSDK layouts. This is the pointer rotation. This value is equivalent to mapping pointerPose/rotation. /// [Preserve, InputControl(offset = 80, alias = "pointerOrientation")] public QuaternionControl pointerRotation { get; private set; } /// /// A [AxisControl](xref:UnityEngine.InputSystem.Controls.AxisControl) that represents information from the OpenXR binding. /// [Preserve, InputControl(alias = "triggeraxis", usage = "Trigger")] public AxisControl trigger { get; private set; } /// /// A [ButtonControl](xref:UnityEngine.InputSystem.Controls.ButtonControl) that represents information from the OpenXR binding. /// [Preserve, InputControl(alias = "triggerbutton", usage = "TriggerButton")] public ButtonControl triggerPressed { get; private set; } /// /// A [Vector2Control](xref:UnityEngine.InputSystem.Controls.Vector2Control) that represents information from the OpenXR binding. /// [Preserve, InputControl(aliases = new[] { "Primary2DAxis", "touchpadaxes", "touchpad" }, usage = "Primary2DAxis")] public Vector2Control trackpad { get; private set; } /// /// A [ButtonControl](xref:UnityEngine.InputSystem.Controls.ButtonControl) that represents information from the OpenXR binding. /// [Preserve, InputControl(aliases = new[] { "joystickorpadpressed", "touchpadpressed" }, usage = "Primary2DAxisClick")] public ButtonControl trackpadClicked { get; private set; } /// /// A [ButtonControl](xref:UnityEngine.InputSystem.Controls.ButtonControl) that represents information from the OpenXR binding. /// [Preserve, InputControl(aliases = new[] { "joystickorpadtouched", "touchpadtouched" }, usage = "Primary2DAxisTouch")] public ButtonControl trackpadTouched { get; private set; } /// /// A [AxisControl](xref:UnityEngine.InputSystem.Controls.AxisControl) that represents the OpenXR binding. /// [Preserve, InputControl(alias = "touchpadForce", usage = "Secondary2DAxisForce")] public AxisControl trackpadForce { get; private set; } /// /// A that represents the binding. /// [Preserve, InputControl(usage = "Haptic")] public HapticControl haptic { get; private set; } /// protected override void FinishSetup() { base.FinishSetup(); gripPressed = GetChildControl("gripPressed"); menu = GetChildControl("menu"); devicePose = GetChildControl("devicePose"); pointer = GetChildControl("pointer"); isTracked = GetChildControl("isTracked"); trackingState = GetChildControl("trackingState"); devicePosition = GetChildControl("devicePosition"); deviceRotation = GetChildControl("deviceRotation"); pointerPosition = GetChildControl("pointerPosition"); pointerRotation = GetChildControl("pointerRotation"); trigger = GetChildControl("trigger"); triggerPressed = GetChildControl("triggerPressed"); trackpad = GetChildControl("trackpad"); trackpadClicked = GetChildControl("trackpadClicked"); trackpadTouched = GetChildControl("trackpadTouched"); trackpadForce = GetChildControl("trackpadForce"); haptic = GetChildControl("haptic"); } } /// /// OpenXR string that represents the Interaction Profile. /// public const string profile = "/interaction_profiles/ml/ml2_controller"; /// /// Constant for a boolean interaction binding '.../input/menu/click' OpenXR Input Binding. Used by input subsystem to bind actions to physical inputs. /// public const string menu = "/input/menu/click"; /// /// Constant for a boolean interaction binding '.../input/trigger/click' OpenXR Input Binding. Used by input subsystem to bind actions to physical inputs. /// public const string triggerClick = "/input/trigger/click"; /// /// Constant for a boolean interaction binding '.../input/trigger/value' OpenXR Input Binding. Used by input subsystem to bind actions to physical inputs. /// public const string trigger = "/input/trigger/value"; /// /// Constant for a Vector2 interaction binding '.../input/trackpad' OpenXR Input Binding. Used by input subsystem to bind actions to physical inputs. /// public const string trackpad = "/input/trackpad"; /// /// Constant for a boolean interaction binding '.../input/trackpad/click' OpenXR Input Binding. Used by input subsystem to bind actions to physical inputs. /// public const string trackpadClick = "/input/trackpad/click"; /// /// Constant for a boolean interaction binding '.../input/trackpad/force' OpenXR Input Binding. Used by input subsystem to bind actions to physical inputs. /// public const string trackpadForce = "/input/trackpad/force"; /// /// Constant for a boolean interaction binding '.../input/trackpad/touch' OpenXR Input Binding. Used by input subsystem to bind actions to physical inputs. /// public const string trackpadTouch = "/input/trackpad/touch"; /// /// Constant for a pose interaction binding '.../input/grip/pose' OpenXR Input Binding. Used by input subsystem to bind actions to physical inputs. /// public const string grip = "/input/grip/pose"; /// /// Constant for a pose interaction binding '.../input/aim/pose' OpenXR Input Binding. Used by input subsystem to bind actions to physical inputs. /// public const string aim = "/input/aim/pose"; /// /// Constant for a boolean interaction binding '.../input/shoulder/click' OpenXR Input Binding. Used by input subsystem to bind actions to physical inputs. /// public const string shoulderClick = "/input/shoulder/click"; /// /// Constant for a haptic interaction binding '.../input/output/haptic' OpenXR Input Binding. Used by input subsystem to bind actions to physical inputs. /// public const string haptic = "/output/haptic"; private const string kDeviceLocalizedName = "Magic Leap Controller OpenXR"; /// /// Registers the layout with the Input System. /// protected override void RegisterDeviceLayout() { UnityEngine.InputSystem.InputSystem.RegisterLayout(typeof(MagicLeapController), matches: new InputDeviceMatcher() .WithInterface(XRUtilities.InterfaceMatchAnyVersion) .WithProduct(kDeviceLocalizedName)); } /// /// Unregisters the layout from the Input System. /// protected override void UnregisterDeviceLayout() { UnityEngine.InputSystem.InputSystem.RemoveLayout(nameof(MagicLeapController)); } protected override string GetDeviceLayoutName() { return nameof(MagicLeapController); } protected override InteractionProfileType GetInteractionProfileType() { return base.GetInteractionProfileType(); } /// protected override void RegisterActionMapsWithRuntime() { ActionMapConfig actionMap = new ActionMapConfig() { name = nameof(MagicLeapController), localizedName = kDeviceLocalizedName, desiredInteractionProfile = profile, manufacturer = "Magic Leap", deviceInfos = new List() { new() { characteristics = InputDeviceCharacteristics.HeldInHand | InputDeviceCharacteristics.TrackedDevice | InputDeviceCharacteristics.Controller | InputDeviceCharacteristics.Left, userPath = UserPaths.leftHand }, new() { characteristics = InputDeviceCharacteristics.HeldInHand | InputDeviceCharacteristics.TrackedDevice | InputDeviceCharacteristics.Controller | InputDeviceCharacteristics.Right, userPath = UserPaths.rightHand } }, actions = new List() { // Bumper new() { name = nameof(MagicLeapController.gripPressed), localizedName = "Grip Pressed", type = ActionType.Binary, usages = new List(){ "GripButton" }, bindings = new List() { new() { interactionPath = shoulderClick, interactionProfileName = profile, } } }, // Menu new() { name =nameof(MagicLeapController.menu), localizedName = "Menu", type = ActionType.Binary, usages = new List() { "MenuButton" }, bindings = new List() { new() { interactionPath = menu, interactionProfileName = profile, } } }, // Device Pose new() { name = nameof(MagicLeapController.devicePose), localizedName = "Device Pose", type = ActionType.Pose, usages = new List() { "Device" }, bindings = new List() { new() { interactionPath = grip, interactionProfileName = profile, } } }, // Pointer Pose new() { name = nameof(MagicLeapController.pointer), localizedName = "Pointer Pose", type = ActionType.Pose, usages = new List() { "Pointer" }, bindings = new List() { new() { interactionPath = aim, interactionProfileName = profile, } } }, // Trigger new() { name = nameof(MagicLeapController.trigger), localizedName = "Trigger", type = ActionType.Axis1D, usages = new List() { "Trigger" }, bindings = new List() { new() { interactionPath = trigger, interactionProfileName = profile, } } }, // Trigger Pressed new() { name = nameof(MagicLeapController.triggerPressed), localizedName = "Trigger Pressed", type = ActionType.Binary, usages = new List() { "TriggerButton" }, bindings = new List() { new() { interactionPath = triggerClick, interactionProfileName = profile, } } }, new() { name = nameof(MagicLeapController.trackpad), localizedName = "Trackpad", type = ActionType.Axis2D, usages = new List() { "Primary2DAxis" }, bindings = new List() { new() { interactionPath = trackpad, interactionProfileName = profile, } } }, new() { name = nameof(MagicLeapController.trackpadTouched), localizedName = "Trackpad Touched", type = ActionType.Binary, usages = new List() { "Primary2DAxisTouch" }, bindings = new List() { new() { interactionPath = trackpadTouch, interactionProfileName = profile, } } }, new() { name = nameof(MagicLeapController.trackpadClicked), localizedName = "Trackpad Clicked", type = ActionType.Binary, usages = new List() { "Primary2DAxisClick" }, bindings = new List() { new() { interactionPath = trackpadClick, interactionProfileName = profile, } } }, new() { name = nameof(MagicLeapController.trackpadForce), localizedName = "Trackpad Force", type = ActionType.Axis1D, usages = new List() { "Secondary2DAxisForce" }, bindings = new List() { new() { interactionPath = trackpadForce, interactionProfileName = profile, } } }, // Haptics new() { name = nameof(MagicLeapController.haptic), localizedName = "Haptic Output", type = ActionType.Vibrate, usages = new List() { "Haptic" }, bindings = new List() { new() { interactionPath = haptic, interactionProfileName = profile, } } } } }; AddActionMap(actionMap); } #region Raycast subsystem private static List subsystemDescriptors = new List(); protected override void OnSubsystemCreate() { CreateSubsystem(subsystemDescriptors, RaycastSubsystem.Id); } protected override void OnSubsystemDestroy() { DestroySubsystem(); } // We don't actually need a Raycast Subsystem with OpenXR; however, because we use AR Foundation, the XRRayInteractor component // on our XR Origin rig's Controller object will instantiate an ARRaycastManager, which in turn will log an annoying warning if it can't find a corresponding subsytem. // By creating an instance of this subsystem, we can avoid those warnings. Note that this implementation doesn't actually do anything! private class RaycastSubsystem : XRRaycastSubsystem { public const string Id = "MagicLeap-Raycast"; public override TrackableChanges GetChanges(Unity.Collections.Allocator allocator) => default; private class MagicLeapProvider : Provider { } [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)] private static void RegisterDescriptor() { XRRaycastSubsystemDescriptor.RegisterDescriptor(new XRRaycastSubsystemDescriptor.Cinfo { id = RaycastSubsystem.Id, providerType = typeof(MagicLeapProvider), subsystemTypeOverride = typeof(RaycastSubsystem), supportsTrackedRaycasts = false, supportsViewportBasedRaycast = false, supportsWorldBasedRaycast = false, supportedTrackableTypes = TrackableType.None }); } } #endregion } }