// Copyright HTC Corporation All Rights Reserved. using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; using UnityEngine.InputSystem.Layouts; using UnityEngine.InputSystem.Controls; using UnityEngine.InputSystem.XR; using UnityEngine.Scripting; using UnityEngine.XR; using UnityEngine.XR.OpenXR.Features; using UnityEngine.XR.OpenXR.Input; #if UNITY_EDITOR using UnityEditor; using UnityEditor.XR.OpenXR.Features; #endif #if USE_INPUT_SYSTEM_POSE_CONTROL // Scripting Define Symbol added by using OpenXR Plugin 1.6.0. using PoseControl = UnityEngine.InputSystem.XR.PoseControl; #else using PoseControl = UnityEngine.XR.OpenXR.Input.PoseControl; #endif namespace VIVE.OpenXR { /// /// This enables the use of HTC VIVE Focus 3 interaction profiles in OpenXR. /// #if UNITY_EDITOR [OpenXRFeature(UiName = "VIVE Focus 3 Controller Interaction", BuildTargetGroups = new[] { BuildTargetGroup.Android, BuildTargetGroup.Standalone }, Company = "HTC", Desc = "Allows for mapping input to the VIVE Focus 3 interaction profile.", DocumentationLink = "https://www.khronos.org/registry/OpenXR/specs/1.0/html/xrspec.html#XR_HTC_vive_focus3_controller_interaction", OpenxrExtensionStrings = kOpenxrExtensionString, Version = "1.0.0", Category = FeatureCategory.Interaction, FeatureId = featureId)] #endif public class VIVEFocus3Profile : OpenXRInteractionFeature { public const string kOpenxrExtensionString = "XR_HTC_vive_focus3_controller_interaction"; /// /// The feature id string. This is used to give the feature a well known id for reference. /// public const string featureId = "vive.openxr.feature.focus3controller"; /// /// An Input System device based on the hand interaction profile in the Interaction Profile. /// [Preserve, InputControlLayout(displayName = "VIVE Focus 3 Controller (OpenXR)", commonUsages = new[] { "LeftHand", "RightHand" })] public class VIVEFocus3Controller : XRControllerWithRumble { /// /// A [Vector2Control](xref:UnityEngine.InputSystem.Controls.Vector2Control) that represents the OpenXR binding. /// [Preserve, InputControl(aliases = new[] { "Primary2DAxis", "joystick", "joystickAxis", "thumbstickAxis" }, usage = "Primary2DAxis")] public Vector2Control thumbstick { get; private set; } /// /// A [AxisControl](xref:UnityEngine.InputSystem.Controls.AxisControl) that represents the OpenXR binding. /// [Preserve, InputControl(aliases = new[] { "GripAxis", "squeeze" }, usage = "Grip")] public AxisControl grip { get; private set; } /// /// A [ButtonControl](xref:UnityEngine.InputSystem.Controls.ButtonControl) that represents the OpenXR binding. /// [Preserve, InputControl(aliases = new[] { "GripButton", "squeezeClick" }, usage = "GripButton")] public ButtonControl gripPressed { get; private set; } /// /// A [ButtonControl](xref:UnityEngine.InputSystem.Controls.ButtonControl) that represents the OpenXR binding. /// [Preserve, InputControl(aliases = new[] { "GripTouch", "squeezeTouch" }, usage = "GripTouch")] public ButtonControl gripTouched { get; private set; } /// /// A [ButtonControl](xref:UnityEngine.InputSystem.Controls.ButtonControl) that represents the OpenXR bindings, depending on handedness. /// [Preserve, InputControl(aliases = new[] { "menuButton" }, usage = "MenuButton")] public ButtonControl menu { get; private set; } /// /// A [ButtonControl](xref:UnityEngine.InputSystem.Controls.ButtonControl) that represents the OpenXR bindings, depending on handedness. /// [Preserve, InputControl(aliases = new[] { "A", "X", "buttonA", "buttonX" }, usage = "PrimaryButton")] public ButtonControl primaryButton { get; private set; } /// /// A [ButtonControl](xref:UnityEngine.InputSystem.Controls.ButtonControl) that represents the OpenXR bindings, depending on handedness. /// [Preserve, InputControl(aliases = new[] { "B", "Y", "buttonB", "buttonY" }, usage = "SecondaryButton")] public ButtonControl secondaryButton { get; private set; } /// /// A [AxisControl](xref:UnityEngine.InputSystem.Controls.AxisControl) that represents the OpenXR binding. /// [Preserve, InputControl(aliases = new[] { "triggerAxis" }, usage = "Trigger")] public AxisControl trigger { get; private set; } /// /// A [ButtonControl](xref:UnityEngine.InputSystem.Controls.ButtonControl) that represents the OpenXR binding. /// [Preserve, InputControl(aliases = new[] { "triggerButton", "triggerClick" }, usage = "TriggerButton")] public ButtonControl triggerPressed { get; private set; } /// /// A [ButtonControl](xref:UnityEngine.InputSystem.Controls.ButtonControl) that represents the OpenXR binding. /// [Preserve, InputControl(aliases = new[] { "triggerTouch", "indexTouch", "indexNearTouched" }, usage = "TriggerTouch")] public ButtonControl triggerTouched { get; private set; } /// /// A [ButtonControl](xref:UnityEngine.InputSystem.Controls.ButtonControl) that represents the OpenXR binding. /// [Preserve, InputControl(aliases = new[] { "Primary2DAxisClick", "joystickPress", "joystickClick" }, usage = "Primary2DAxisClick")] public ButtonControl thumbstickClicked { get; private set; } /// /// A [ButtonControl](xref:UnityEngine.InputSystem.Controls.ButtonControl) that represents the OpenXR binding. /// [Preserve, InputControl(aliases = new[] { "Primary2DAxisTouch", "joystickTouch" }, usage = "Primary2DAxisTouch")] public ButtonControl thumbstickTouched { get; private set; } /// /// A [ButtonControl](xref:UnityEngine.InputSystem.Controls.ButtonControl) that represents the OpenXR binding. /// [Preserve, InputControl(aliases = new[] { "ParkingTouched" })] public ButtonControl parkingTouched { get; private set; } /// /// A that represents the OpenXR binding. The grip pose represents the location of the user's palm or holding a motion controller. /// [Preserve, InputControl(offset = 0, aliases = new[] { "device", "gripPose" }, usage = "Device")] public PoseControl devicePose { get; private set; } /// /// A that represents the OpenXR binding. The pointer pose represents the tip of the controller pointing forward. /// [Preserve, InputControl(offset = 0, alias = "aimPose", usage = "Pointer")] public PoseControl pointerPose { 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 = 24, usage = "IsTracked")] new public 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 to indicate what data is valid. This value is equivalent to mapping devicePose/trackingState. /// [Preserve, InputControl(offset = 28, usage = "TrackingState")] new public 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. For the VIVE Focus 3 device, this is both the device and the pointer position. This value is equivalent to mapping devicePose/position. /// [Preserve, InputControl(offset = 32, alias = "gripPosition")] new public 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. For the VIVE Focus 3 device, this is both the device and the pointer rotation. This value is equivalent to mapping devicePose/rotation. /// [Preserve, InputControl(offset = 44, alias = "gripOrientation")] new public QuaternionControl deviceRotation { get; private set; } /// /// A [Vector3Control](xref:UnityEngine.InputSystem.Controls.Vector3Control) required for back compatibility with the XRSDK layouts. This is the pointer position. This value is equivalent to mapping pointerPose/position. /// [Preserve, InputControl(offset = 92)] 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 = 104, alias = "pointerOrientation")] public QuaternionControl pointerRotation { get; private set; } /// /// A that represents the binding. /// [Preserve, InputControl(usage = "Haptic")] public HapticControl haptic { get; private set; } /// /// Internal call used to assign controls to the the correct element. /// protected override void FinishSetup() { base.FinishSetup(); thumbstick = GetChildControl("thumbstick"); trigger = GetChildControl("trigger"); triggerPressed = GetChildControl("triggerPressed"); triggerTouched = GetChildControl("triggerTouched"); grip = GetChildControl("grip"); gripPressed = GetChildControl("gripPressed"); gripTouched = GetChildControl("gripTouched"); menu = GetChildControl("menu"); primaryButton = GetChildControl("primaryButton"); secondaryButton = GetChildControl("secondaryButton"); thumbstickClicked = GetChildControl("thumbstickClicked"); thumbstickTouched = GetChildControl("thumbstickTouched"); parkingTouched = GetChildControl("parkingTouched"); devicePose = GetChildControl("devicePose"); pointerPose = GetChildControl("pointerPose"); isTracked = GetChildControl("isTracked"); trackingState = GetChildControl("trackingState"); devicePosition = GetChildControl("devicePosition"); deviceRotation = GetChildControl("deviceRotation"); pointerPosition = GetChildControl("pointerPosition"); pointerRotation = GetChildControl("pointerRotation"); haptic = GetChildControl("haptic"); } } /// /// The interaction profile string used to reference the Interaction Profile. /// public const string profile = "/interaction_profiles/htc/vive_focus3_controller"; // Available Bindings // Left Hand Only /// /// Constant for a boolean interaction binding '.../input/x/click' OpenXR Input Binding. Used by input subsystem to bind actions to physical inputs. This binding is only available for the user path. /// public const string buttonX = "/input/x/click"; /// /// Constant for a boolean interaction binding '.../input/y/click' OpenXR Input Binding. Used by input subsystem to bind actions to physical inputs. This binding is only available for the user path. /// public const string buttonY = "/input/y/click"; /// /// Constant for a boolean interaction binding '.../input/menu/click' OpenXR Input Binding. Used by input subsystem to bind actions to physical inputs. This binding is only available for the user path. /// public const string menu = "/input/menu/click"; // Right Hand Only /// /// Constant for a boolean interaction binding '.../input/a/click' OpenXR Input Binding. Used by input subsystem to bind actions to physical inputs. This binding is only available for the user path. /// public const string buttonA = "/input/a/click"; /// /// Constant for a boolean interaction binding '..."/input/b/click' OpenXR Input Binding. Used by input subsystem to bind actions to physical inputs. This binding is only available for the user path. /// public const string buttonB = "/input/b/click"; /// /// Constant for a boolean interaction binding '.../input/system/click' OpenXR Input Binding. Used by input subsystem to bind actions to physical inputs. This binding is only available for the user path. /// public const string system = "/input/system/click"; // Both Hands /// /// Constant for a float interaction binding '.../input/squeeze/value' OpenXR Input Binding. Used by input subsystem to bind actions to physical inputs. /// public const string grip = "/input/squeeze/value"; /// /// Constant for a boolean interaction binding '.../input/squeeze/click' OpenXR Input Binding. Used by input subsystem to bind actions to physical inputs. /// public const string gripPress = "/input/squeeze/click"; /// /// Constant for a boolean interaction binding '.../input/squeeze/touch' OpenXR Input Binding. Used by input subsystem to bind actions to physical inputs. /// public const string gripTouch = "/input/squeeze/touch"; /// /// Constant for a float 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 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/touch' OpenXR Input Binding. Used by input subsystem to bind actions to physical inputs. /// public const string triggerTouch = "/input/trigger/touch"; /// /// Constant for a Vector2 interaction binding '.../input/thumbstick' OpenXR Input Binding. Used by input subsystem to bind actions to physical inputs. /// public const string thumbstick = "/input/thumbstick"; /// /// Constant for a boolean interaction binding '.../input/thumbstick/click' OpenXR Input Binding. Used by input subsystem to bind actions to physical inputs. /// public const string thumbstickClick = "/input/thumbstick/click"; /// /// Constant for a boolean interaction binding '.../input/thumbstick/touch' OpenXR Input Binding. Used by input subsystem to bind actions to physical inputs. /// public const string thumbstickTouch = "/input/thumbstick/touch"; /// /// Constant for a boolean interaction binding '.../input/thumbrest/touch' OpenXR Input Binding. Used by input subsystem to bind actions to physical inputs. /// public const string thumbrest = "/input/thumbrest/touch"; /// /// Constant for a hand grip pose interaction binding '.../input/grip/pose' OpenXR Input Binding. Used by input subsystem to bind actions to physical inputs. /// public const string gripPose = "/input/grip/pose"; /// /// Constant for a hand point pose interaction binding '.../input/aim/pose' OpenXR Input Binding. Used by input subsystem to bind actions to physical inputs. /// public const string pointerPose = "/input/aim/pose"; /// /// Constant for a haptic interaction binding '.../output/haptic' OpenXR Input Binding. Used by input subsystem to bind actions to physical outputs. /// public const string haptic = "/output/haptic"; private const string kDeviceLocalizedName = "VIVE Focus 3 Controller OpenXR"; /// /// Registers the layout with the Input System. /// protected override void RegisterDeviceLayout() { InputSystem.RegisterLayout(typeof(VIVEFocus3Controller), matches: new InputDeviceMatcher() .WithInterface(XRUtilities.InterfaceMatchAnyVersion) .WithProduct(kDeviceLocalizedName)); } /// /// Removes the layout from the Input System. /// protected override void UnregisterDeviceLayout() { InputSystem.RemoveLayout(typeof(VIVEFocus3Controller).Name); } /// protected override void RegisterActionMapsWithRuntime() { ActionMapConfig actionMap = new ActionMapConfig() { name = "vivefocus3controller", localizedName = kDeviceLocalizedName, desiredInteractionProfile = profile, manufacturer = "HTC", serialNumber = "", deviceInfos = new List() { new DeviceConfig() { characteristics = InputDeviceCharacteristics.HeldInHand | InputDeviceCharacteristics.TrackedDevice | InputDeviceCharacteristics.Controller | InputDeviceCharacteristics.Left, userPath = UserPaths.leftHand // "/user/hand/left" }, new DeviceConfig() { characteristics = InputDeviceCharacteristics.HeldInHand | InputDeviceCharacteristics.TrackedDevice | InputDeviceCharacteristics.Controller | InputDeviceCharacteristics.Right, userPath = UserPaths.rightHand // "/user/hand/right" } }, actions = new List() { // Thumbstick Axis new ActionConfig() { name = "thumbstick", localizedName = "Thumbstick Axis", type = ActionType.Axis2D, usages = new List() { "Primary2DAxis" }, bindings = new List() { new ActionBinding() { interactionPath = thumbstick, interactionProfileName = profile, } } }, // Grip Axis new ActionConfig() { name = "grip", localizedName = "Grip Axis", type = ActionType.Axis1D, usages = new List() { "Grip" }, bindings = new List() { new ActionBinding() { interactionPath = grip, interactionProfileName = profile, } } }, // Grip Press new ActionConfig() { name = "gripPressed", localizedName = "Grip Pressed", type = ActionType.Binary, usages = new List() { "GripButton" }, bindings = new List() { new ActionBinding() { interactionPath = gripPress, interactionProfileName = profile, } } }, // Grip Touch // Known issue: Registering gripTouched will cause Controller Interaction Profile not work. /*new ActionConfig() { name = "gripTouched", localizedName = "Grip Touched", type = ActionType.Binary, usages = new List() { "GripTouch" }, bindings = new List() { new ActionBinding() { interactionPath = gripTouch, interactionProfileName = profile, } } },*/ // Menu new ActionConfig() { name = "menu", localizedName = "Menu", type = ActionType.Binary, usages = new List() { "MenuButton" }, bindings = new List() { new ActionBinding() { interactionPath = menu, interactionProfileName = profile, userPaths = new List() { UserPaths.leftHand } }, new ActionBinding() { interactionPath = system, interactionProfileName = profile, userPaths = new List() { UserPaths.rightHand } }, } }, // X / A Press new ActionConfig() { name = "primaryButton", localizedName = "Primary Pressed", type = ActionType.Binary, usages = new List() { "PrimaryButton" }, bindings = new List() { new ActionBinding() { interactionPath = buttonX, interactionProfileName = profile, userPaths = new List() { UserPaths.leftHand } }, new ActionBinding() { interactionPath = buttonA, interactionProfileName = profile, userPaths = new List() { UserPaths.rightHand } }, } }, // Y / B Press new ActionConfig() { name = "secondaryButton", localizedName = "Secondary Pressed", type = ActionType.Binary, usages = new List() { "SecondaryButton" }, bindings = new List() { new ActionBinding() { interactionPath = buttonY, interactionProfileName = profile, userPaths = new List() { UserPaths.leftHand } }, new ActionBinding() { interactionPath = buttonB, interactionProfileName = profile, userPaths = new List() { UserPaths.rightHand } }, } }, // Trigger Axis new ActionConfig() { name = "trigger", localizedName = "Trigger Axis", type = ActionType.Axis1D, usages = new List() { "Trigger" }, bindings = new List() { new ActionBinding() { interactionPath = trigger, interactionProfileName = profile, } } }, // Trigger Press new ActionConfig() { name = "triggerPressed", localizedName = "Trigger Pressed", type = ActionType.Binary, usages = new List() { "TriggerButton" }, bindings = new List() { new ActionBinding() { interactionPath = triggerClick, interactionProfileName = profile, } } }, // Trigger Touch new ActionConfig() { name = "triggerTouched", localizedName = "Trigger Touched", type = ActionType.Binary, usages = new List() { "TriggerTouch" }, bindings = new List() { new ActionBinding() { interactionPath = triggerTouch, interactionProfileName = profile, } } }, // Thumbstick Click new ActionConfig() { name = "thumbstickClicked", localizedName = "Thumbstick Pressed", type = ActionType.Binary, usages = new List() { "Primary2DAxisClick" }, bindings = new List() { new ActionBinding() { interactionPath = thumbstickClick, interactionProfileName = profile, } } }, // Thumbstick Touch new ActionConfig() { name = "thumbstickTouched", localizedName = "Thumbstick Touched", type = ActionType.Binary, usages = new List() { "Primary2DAxisTouch" }, bindings = new List() { new ActionBinding() { interactionPath = thumbstickTouch, interactionProfileName = profile, } } }, // Thumbrest Touch new ActionConfig() { name = "parkingTouched", localizedName = "Parking Touch", type = ActionType.Binary, bindings = new List() { new ActionBinding() { interactionPath = thumbrest, interactionProfileName = profile, } } }, // Device Pose new ActionConfig() { name = "devicePose", localizedName = "Grip Pose", type = ActionType.Pose, usages = new List() { "Device" }, bindings = new List() { new ActionBinding() { interactionPath = gripPose, interactionProfileName = profile, } } }, // Pointer Pose new ActionConfig() { name = "pointerPose", localizedName = "Pointer Pose", type = ActionType.Pose, usages = new List() { "Pointer" }, bindings = new List() { new ActionBinding() { interactionPath = pointerPose, interactionProfileName = profile, } } }, // Haptics new ActionConfig() { name = "haptic", localizedName = "Haptic Output", type = ActionType.Vibrate, usages = new List() { "Haptic" }, bindings = new List() { new ActionBinding() { interactionPath = haptic, interactionProfileName = profile, } } } } }; AddActionMap(actionMap); } } }