using System.Collections.Generic; using UnityEngine.InputSystem.XR; using UnityEngine.InputSystem.Layouts; using UnityEngine.InputSystem.Controls; using UnityEngine.Scripting; using UnityEngine.XR.OpenXR.Features.Interactions; using UnityEngine.XR.OpenXR.Input; #if UNITY_EDITOR using UnityEditor; #endif #if USE_INPUT_SYSTEM_POSE_CONTROL using PoseControl = UnityEngine.InputSystem.XR.PoseControl; #else using PoseControl = UnityEngine.XR.OpenXR.Input.PoseControl; #endif namespace UnityEngine.XR.OpenXR.Features { /// /// This enables the use of HTC Vive Focus3 Controllers interaction profiles in OpenXR. /// #if UNITY_EDITOR [UnityEditor.XR.OpenXR.Features.OpenXRFeature(UiName = "HTC Vive Focus3 Controller Support", BuildTargetGroups = new[] { BuildTargetGroup.Standalone, BuildTargetGroup.WSA }, Company = "HTC", Desc = "Allows for mapping input to the HTC Vive Focus3 Controller interaction profile.", //DocumentationLink = "https://developer.vive.com/resources/openxr/openxr-pcvr/tutorials/unity/cosmos-controller-openxr-feature-unity/", OpenxrExtensionStrings = "XR_HTC_vive_focus3_controller_interaction XR_EXT_palm_pose", Version = "0.0.2", Category = UnityEditor.XR.OpenXR.Features.FeatureCategory.Interaction, FeatureId = featureId)] #endif public class HtcViveFocus3InputFeature : OpenXRInteractionFeature { /// /// The feature id string. This is used to give the feature a well known id for reference. /// public const string featureId = "com.htc.openxr.feature.input.htcvivefocus3"; /// /// An Input System device based off the HTC Vive Controller. /// /// [Preserve, InputControlLayout(displayName = "HTC Vive Focus3 Controller (OpenXR)", commonUsages = new[] { "LeftHand", "RightHand" })] public class ViveFocus3Controller : XRControllerWithRumble { /// /// A representing information from the OpenXR binding. /// [Preserve, InputControl(aliases = new[] { "thumbresttouch" })] public ButtonControl thumbrestTouched { get; private set; } /// /// A representing information from the OpenXR binding. /// [Preserve, InputControl(aliases = new[] { "griptouch" })] public ButtonControl gripTouched { get; private set; } /// /// A representing information from the OpenXR binding. /// [Preserve, InputControl(aliases = new[] { "GripAxis" })] public AxisControl grip { get; private set; } /// /// A representing information from the OpenXR binding. /// [Preserve, InputControl(aliases = new[] { "GripButton" })] public ButtonControl gripPressed { get; private set; } /// /// A representing the OpenXR bindings, depending on handedness. /// [Preserve, InputControl(aliases = new[] { "B", "Y" })] public ButtonControl secondaryButton { get; private set; } /// /// A representing the OpenXR bindings, depending on handedness. /// [Preserve, InputControl(aliases = new[] { "A", "X" })] public ButtonControl primaryButton { get; private set; } /// /// A representing information from the OpenXR binding. /// [Preserve, InputControl(aliases = new[] { "Primary", "menubutton" })] public ButtonControl menu { get; private set; } /// /// A representing information from the OpenXR binding. /// [Preserve, InputControl(aliases = new[] { "triggertouch" })] public ButtonControl triggerTouched { get; private set; } /// /// A representing information from the OpenXR binding. /// [Preserve, InputControl(aliases = new[] { "triggeraxis" })] public AxisControl trigger { get; private set; } /// /// A representing information from the OpenXR binding. /// [Preserve, InputControl(aliases = new[] { "triggerbutton" })] public ButtonControl triggerPressed { get; private set; } /// /// A representing information from the OpenXR binding. /// [Preserve, InputControl(aliases = new[] { "Primary2DAxis", "joystickaxes" })] public Vector2Control joystick { get; private set; } /// /// A representing information from the OpenXR binding. /// [Preserve, InputControl(aliases = new[] { "joystickorpadpressed", "joystickpressed" })] public ButtonControl joystickClicked { get; private set; } /// /// A representing information from the OpenXR binding. /// [Preserve, InputControl(aliases = new[] { "joystickorpadtouched", "joysticktouched" })] public ButtonControl joystickTouched { get; private set; } /// /// A representing information from the OpenXR binding. /// [Preserve, InputControl(offset = 0, alias = "device")] public PoseControl devicePose { get; private set; } /// /// A representing information from the OpenXR binding. /// [Preserve, InputControl(offset = 0)] public PoseControl pointer { get; private set; } /// /// A required for back 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 = 28)] new public ButtonControl isTracked { get; private set; } /// /// A required for back 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 = 32)] new public IntegerControl trackingState { get; private set; } /// /// A required for back compatibility with the XRSDK layouts. this is the device position, or grip position. this value is equivalent to mapping devicePose/position /// [Preserve, InputControl(offset = 36, aliases = new[] { "gripPosition" })] new public Vector3Control devicePosition { get; private set; } /// /// A required for back compatibility with the XRSDK layouts. this is the device orientation, or grip orientation. this value is equivalent to mapping devicePose/rotation /// [Preserve, InputControl(offset = 48, aliases = new[] { "gripOrientation", "gripRotation" })] new public QuaternionControl deviceRotation { get; private set; } /// A required for back compatibility with the XRSDK layouts. this is the pointer position. this value is equivalent to mapping pointerPose/position /// [Preserve, InputControl(offset = 96)] public Vector3Control pointerPosition { get; private set; } /// /// A required for back compatibility with the XRSDK layouts. this is the pointer rotation. this value is equivalent to mapping pointerPose/rotation /// [Preserve, InputControl(offset = 108, aliases = new[] { "pointerOrientation" })] public QuaternionControl pointerRotation { get; private set; } /// /// A that represents the binding. /// [Preserve, InputControl(usage = "Haptic")] public HapticControl haptic { get; private set; } /// /// A representing information from the OpenXR binding. /// [Preserve, InputControl()] public PoseControl palm { get; private set; } protected override void FinishSetup() { base.FinishSetup(); thumbrestTouched = GetChildControl("thumbrestTouched"); gripTouched = GetChildControl("gripTouched"); grip = GetChildControl("grip"); gripPressed = GetChildControl("gripPressed"); primaryButton = GetChildControl("primaryButton"); secondaryButton = GetChildControl("secondaryButton"); menu = GetChildControl("menu"); trigger = GetChildControl("trigger"); triggerTouched = GetChildControl("triggerTouched"); triggerPressed = GetChildControl("triggerPressed"); joystick = GetChildControl("joystick"); joystickClicked = GetChildControl("joystickClicked"); joystickTouched = GetChildControl("joystickTouched"); devicePose = GetChildControl("devicePose"); pointer = GetChildControl("pointer"); isTracked = GetChildControl("isTracked"); trackingState = GetChildControl("trackingState"); devicePosition = GetChildControl("devicePosition"); deviceRotation = GetChildControl("deviceRotation"); pointerPosition = GetChildControl("pointerPosition"); pointerRotation = GetChildControl("pointerRotation"); haptic = GetChildControl("haptic"); palm = GetChildControl("palm"); } } /// The interaction profile string used to reference the HTC Vive Focus3 Controller. public const string profile = "/interaction_profiles/htc/vive_focus3_controller"; /// /// Constant for a interaction binding '.../input/trigger/click' OpenXR Input Binding. Used by to bind actions to physical inputs. /// public const string select = "/input/trigger/click"; /// /// Constant for a interaction binding '.../input/a/click' OpenXR Input Binding. Used by to bind actions to physical inputs. /// public const string a = "/input/a/click"; /// /// Constant for a interaction binding '.../input/b/click' OpenXR Input Binding. Used by to bind actions to physical inputs. /// public const string b = "/input/b/click"; /// /// Constant for a interaction binding '.../input/x/click' OpenXR Input Binding. Used by to bind actions to physical inputs. /// public const string x = "/input/x/click"; /// /// Constant for a interaction binding '.../input/y/click' OpenXR Input Binding. Used by to bind actions to physical inputs. /// public const string y = "/input/y/click"; /// /// Constant for a interaction binding '.../input/squeeze/click' OpenXR Input Binding. Used by to bind actions to physical inputs. /// public const string squeeze = "/input/squeeze/value"; /// /// Constant for a interaction binding '.../input/squeeze/touch' OpenXR Input Binding. Used by to bind actions to physical inputs. /// public const string squeezeTouch = "/input/squeeze/touch"; /// /// Constant for a interaction binding '.../input/squeeze/click' OpenXR Input Binding. Used by to bind actions to physical inputs. /// public const string squeezeClick = "/input/squeeze/click"; /// /// Constant for a interaction binding '.../input/menu/click' OpenXR Input Binding. Used by to bind actions to physical inputs. /// public const string menu = "/input/menu/click"; /// /// Constant for a interaction binding '.../input/trigger/touch' OpenXR Input Binding. Used by to bind actions to physical inputs. /// public const string triggerTouch = "/input/trigger/touch"; /// /// Constant for a interaction binding '.../input/trigger/value' OpenXR Input Binding. Used by to bind actions to physical inputs. /// public const string trigger = "/input/trigger/value"; /// /// Constant for a interaction binding '.../input/trigger/click' OpenXR Input Binding. Used by to bind actions to physical inputs. /// public const string triggerClick = "/input/trigger/click"; /// /// Constant for a interaction binding '.../input/trackpad' OpenXR Input Binding. Used by to bind actions to physical inputs. /// public const string thumbstick = "/input/thumbstick"; /// /// Constant for a interaction binding '.../input/trackpad/click' OpenXR Input Binding. Used by to bind actions to physical inputs. /// public const string thumbstickClick = "/input/thumbstick/click"; /// /// Constant for a interaction binding '.../input/trackpad/touch' OpenXR Input Binding. Used by to bind actions to physical inputs. /// public const string thumbstickTouch = "/input/thumbstick/touch"; /// /// Constant for a interaction binding '.../input/grip/pose' OpenXR Input Binding. Used by to bind actions to physical inputs. /// public const string grip = "/input/grip/pose"; /// /// Constant for a interaction binding '.../input/aim/pose' OpenXR Input Binding. Used by to bind actions to physical inputs. /// public const string aim = "/input/aim/pose"; /// /// Constant for a interaction binding '.../output/haptic' OpenXR Input Binding. Used by to bind actions to physical inputs. /// public const string haptic = "/output/haptic"; /// /// Constant for a interaction binding '.../input/palm_ext/pose' OpenXR Input Binding. Used by to bind actions to physical inputs. /// public const string palm = "/input/palm_ext/pose"; /// /// Constant for a interaction binding '.../input/thumbrest/touch' OpenXR Input Binding. Used by to bind actions to physical inputs. /// public const string thumbrest = "/input/thumbrest/touch"; private const string kDeviceLocalizedName = "HTC Vive Focus3 Controller OpenXR"; /// /// Registers the layout with the Input System. Matches the that is registered with . /// protected override void RegisterDeviceLayout() { InputSystem.InputSystem.RegisterLayout(typeof(ViveFocus3Controller), matches: new InputDeviceMatcher() .WithInterface(XRUtilities.InterfaceMatchAnyVersion) .WithProduct(kDeviceLocalizedName)); } /// /// Removes the layout from the Input System. Matches the that is registered with . /// protected override void UnregisterDeviceLayout() { InputSystem.InputSystem.RemoveLayout(typeof(ViveFocus3Controller).Name); } /// /// Registers an with OpenXR that matches the HTC Vive Focus3 Controller. Also calls when the Input System package is available. /// protected override void RegisterActionMapsWithRuntime() { ActionMapConfig actionMap = new ActionMapConfig() { name = "vivefocus3controller", localizedName = kDeviceLocalizedName, desiredInteractionProfile = profile, manufacturer = "HTC", serialNumber = "", deviceInfos = new List() { new DeviceConfig() { characteristics = (InputDeviceCharacteristics)(InputDeviceCharacteristics.HeldInHand | InputDeviceCharacteristics.TrackedDevice | InputDeviceCharacteristics.Controller | InputDeviceCharacteristics.Left), userPath = UserPaths.leftHand }, new DeviceConfig() { characteristics = (InputDeviceCharacteristics)(InputDeviceCharacteristics.HeldInHand | InputDeviceCharacteristics.TrackedDevice | InputDeviceCharacteristics.Controller | InputDeviceCharacteristics.Right), userPath = UserPaths.rightHand } }, actions = new List() { new ActionConfig() { name = "primarybutton", localizedName = "Primary Button", type = ActionType.Binary, usages = new List() { "PrimaryButton" }, bindings = new List() { new ActionBinding() { interactionPath = x, interactionProfileName = profile, userPaths = new List() { UserPaths.leftHand } }, new ActionBinding() { interactionPath = a, interactionProfileName = profile, userPaths = new List() { UserPaths.rightHand } }, } }, new ActionConfig() { name = "secondarybutton", localizedName = "Secondary Button", type = ActionType.Binary, usages = new List() { "SecondaryButton" }, bindings = new List() { new ActionBinding() { interactionPath = y, interactionProfileName = profile, userPaths = new List() { UserPaths.leftHand } }, new ActionBinding() { interactionPath = b, interactionProfileName = profile, userPaths = new List() { UserPaths.rightHand } }, } }, new ActionConfig() { name = "thumbrestTouched", localizedName = "Thumbrest Touched", type = ActionType.Binary, usages = new List() { "ThumbrestTouch", }, bindings = new List() { new ActionBinding() { interactionPath = thumbrest, interactionProfileName = profile, } } }, new ActionConfig() { name = "grip", localizedName = "Grip", type = ActionType.Axis1D, usages = new List() { "Grip" }, bindings = new List() { new ActionBinding() { interactionPath = squeeze, interactionProfileName = profile, } } }, new ActionConfig() { name = "gripTouched", localizedName = "Grip Touched", type = ActionType.Binary, usages = new List() { "GripTouch", }, bindings = new List() { new ActionBinding() { interactionPath = squeezeTouch, interactionProfileName = profile, } } }, new ActionConfig() { name = "grippressed", localizedName = "Grip Pressed", type = ActionType.Binary, usages = new List() { "GripPressed", "GripButton" }, bindings = new List() { new ActionBinding() { interactionPath = squeezeClick, interactionProfileName = profile, } } }, 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 ActionConfig() { name = "trigger", localizedName = "Trigger", type = ActionType.Axis1D, usages = new List() { "Trigger" }, bindings = new List() { new ActionBinding() { interactionPath = trigger, interactionProfileName = profile, } } }, new ActionConfig() { name = "triggerTouched", localizedName = "TriggerTouched", type = ActionType.Binary, usages = new List() { "TriggerTouch" }, bindings = new List() { new ActionBinding() { interactionPath = triggerTouch, interactionProfileName = profile, } } }, new ActionConfig() { name = "triggerpressed", localizedName = "Trigger Pressed", type = ActionType.Binary, usages = new List() { "TriggerPressed", "TriggerButton" }, bindings = new List() { new ActionBinding() { interactionPath = triggerClick, interactionProfileName = profile, } } }, new ActionConfig() { name = "thumbstick", localizedName = "Thumbstick", type = ActionType.Axis2D, usages = new List() { "Primary2DAxis" }, bindings = new List() { new ActionBinding() { interactionPath = thumbstick, interactionProfileName = profile, } } }, new ActionConfig() { name = "thumbsticktouched", localizedName = "Thumbstick Touched", type = ActionType.Binary, usages = new List() { "Primary2DAxisTouch" }, bindings = new List() { new ActionBinding() { interactionPath = thumbstickTouch, interactionProfileName = profile, } } }, new ActionConfig() { name = "thumbstickclicked", localizedName = "Thumbstick Clicked", type = ActionType.Binary, usages = new List() { "Primary2DAxisClick" }, bindings = new List() { new ActionBinding() { interactionPath = thumbstickClick, interactionProfileName = profile, } } }, new ActionConfig() { name = "devicepose", localizedName = "Device Pose", type = ActionType.Pose, usages = new List() { "Device" }, bindings = new List() { new ActionBinding() { interactionPath = grip, interactionProfileName = profile, } } }, new ActionConfig() { name = "pointer", localizedName = "Pointer Pose", type = ActionType.Pose, usages = new List() { "Pointer" }, bindings = new List() { new ActionBinding() { interactionPath = aim, interactionProfileName = profile, } } }, new ActionConfig() { name = "haptic", localizedName = "Haptic Output", type = ActionType.Vibrate, usages = new List() { "Haptic" }, bindings = new List() { new ActionBinding() { interactionPath = haptic, interactionProfileName = profile, } } }, new ActionConfig() { name = "palm", localizedName = "Palm Pose", type = ActionType.Pose, usages = new List() { "Palm" }, bindings = new List() { new ActionBinding() { interactionPath = palm, interactionProfileName = profile, } } } } }; AddActionMap(actionMap); } } }