ViveSoftware / ViveInputUtility-Unity

A toolkit that helps developing/prototyping VR apps.
http://u3d.as/uF7
Other
352 stars 82 forks source link

Error messages with new input system (UnityEngine.InputSystem) #232

Open nitz opened 2 years ago

nitz commented 2 years ago

Hello!

My project is using the new Input System, and I need access to trackers, which Unity doesn't yet support through Open XR. As a result, my setup looks like this:

The first issue I ran into was errors from the use of the legacy input system in OpenVRSettings.cs, (and by extension ExternalCameraSettings.cs and ExternalCameraHook.cs). They were the typical errors when the project is set to use exclusively the new input system.

I was able to work around those. I've added my simple current patch for that at the bottom of this post.

The larger issue I've run into that hasn't blocked the functionality I need (which is just position & rotation so far via VivePoseTracker), but seems like it could become a problem as I add things deals with the posing. Here's a full call stack of one of the errors:

ArgumentException: Expected control 'pose' to be of type 'PoseControl' but is of type 'PoseControl' instead!
Parameter name: path
UnityEngine.InputSystem.InputControl.GetChildControl[TControl] (System.String path) (at Library/PackageCache/com.unity.inputsystem@1.2.0/InputSystem/Controls/InputControl.cs:661)
HTC.UnityPlugin.Vive.VIUSyntheticDevice.FinishSetup () (at Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/VIUSyntheticDevice.cs:494)
UnityEngine.InputSystem.InputControl.CallFinishSetupRecursive () (at Library/PackageCache/com.unity.inputsystem@1.2.0/InputSystem/Controls/InputControl.cs:934)
UnityEngine.InputSystem.Layouts.InputDeviceBuilder.Setup (UnityEngine.InputSystem.Utilities.InternedString layout, UnityEngine.InputSystem.Utilities.InternedString variants, UnityEngine.InputSystem.Layouts.InputDeviceDescription deviceDescription) (at Library/PackageCache/com.unity.inputsystem@1.2.0/InputSystem/Devices/InputDeviceBuilder.cs:60)
UnityEngine.InputSystem.InputDevice.Build[TDevice] (System.String layoutName, System.String layoutVariants, UnityEngine.InputSystem.Layouts.InputDeviceDescription deviceDescription, System.Boolean noPrecompiledLayouts) (at Library/PackageCache/com.unity.inputsystem@1.2.0/InputSystem/Devices/InputDevice.cs:901)
UnityEngine.InputSystem.InputManager.AddDevice (UnityEngine.InputSystem.Utilities.InternedString layout, System.Int32 deviceId, System.String deviceName, UnityEngine.InputSystem.Layouts.InputDeviceDescription deviceDescription, UnityEngine.InputSystem.InputDevice+DeviceFlags deviceFlags, UnityEngine.InputSystem.Utilities.InternedString variants) (at Library/PackageCache/com.unity.inputsystem@1.2.0/InputSystem/InputManager.cs:1051)
UnityEngine.InputSystem.InputManager.AddDevice (UnityEngine.InputSystem.Layouts.InputDeviceDescription description, System.Boolean throwIfNoLayoutFound, System.String deviceName, System.Int32 deviceId, UnityEngine.InputSystem.InputDevice+DeviceFlags deviceFlags) (at Library/PackageCache/com.unity.inputsystem@1.2.0/InputSystem/InputManager.cs:1209)
UnityEngine.InputSystem.InputManager.AddDevice (UnityEngine.InputSystem.Layouts.InputDeviceDescription description) (at Library/PackageCache/com.unity.inputsystem@1.2.0/InputSystem/InputManager.cs:1182)
UnityEngine.InputSystem.InputSystem.AddDevice (UnityEngine.InputSystem.Layouts.InputDeviceDescription description) (at Library/PackageCache/com.unity.inputsystem@1.2.0/InputSystem/InputSystem.cs:1492)
HTC.UnityPlugin.Vive.VIUSyntheticDevice+RoleDeviceCreator.AddDevice (HTC.UnityPlugin.Vive.ViveRole+IMap map, System.Int32 roleValue) (at Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/VIUSyntheticDevice.cs:187)
UnityEngine.Debug:LogException(Exception)
HTC.UnityPlugin.Vive.RoleDeviceCreator:AddDevice(IMap, Int32) (at Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/VIUSyntheticDevice.cs:205)
HTC.UnityPlugin.Vive.RoleDeviceCreator:OnRoleValueMappingChanged(IMap, MappingChangedEventArg) (at Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/VIUSyntheticDevice.cs:233)
HTC.UnityPlugin.Vive.Map:InternalMapping(Int32, UInt32) (at Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/ViveRole/ViveRoleMap.cs:290)
HTC.UnityPlugin.Vive.Map:MappingRoleValue(Int32, UInt32) (at Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/ViveRole/ViveRoleMap.cs:441)
HTC.UnityPlugin.Vive.GenericMap`1:MappingRole(BodyRole, UInt32) (at Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/ViveRole/ViveRoleMap.cs:651)
HTC.UnityPlugin.Vive.MapHandler`1:MappingRole(BodyRole, UInt32) (at Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/ViveRole/ViveRoleMapHandler.cs:48)
HTC.UnityPlugin.Vive.MapHandler`1:MappingRoleIfUnbound(BodyRole, UInt32) (at Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/ViveRole/ViveRoleMapHandler.cs:55)
HTC.UnityPlugin.Vive.BodyRoleHandler:Refresh() (at Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/ViveRole/RoleMaps/BodyRole.cs:65)
HTC.UnityPlugin.Vive.BodyRoleHandler:OnConnectedDeviceChanged(UInt32, VRModuleDeviceClass, String, Boolean) (at Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/ViveRole/RoleMaps/BodyRole.cs:46)
HTC.UnityPlugin.Vive.Map:OnConnectedDeviceChanged(UInt32, VRModuleDeviceClass, String, Boolean) (at Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/ViveRole/ViveRoleMap.cs:161)
HTC.UnityPlugin.Vive.ViveRole:OnDeviceConnected(UInt32, Boolean) (at Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/ViveRole/ViveRole.cs:79)
HTC.UnityPlugin.VRModuleManagement.VRModule:InvokeDeviceConnectedEvent(UInt32, Boolean) (at Assets/HTC.UnityPlugin/VRModule/VRModuleEvent.cs:71)
HTC.UnityPlugin.VRModuleManagement.VRModule:SendAllDeviceConnectedEvent() (at Assets/HTC.UnityPlugin/VRModule/VRModuleManager.cs:507)
HTC.UnityPlugin.VRModuleManagement.VRModule:ModuleConnectedDeviceChanged() (at Assets/HTC.UnityPlugin/VRModule/VRModuleManager.cs:487)
HTC.UnityPlugin.VRModuleManagement.ModuleBase:ProcessConnectedDeviceChanged() (at Assets/HTC.UnityPlugin/VRModule/VRModuleBase.cs:155)
HTC.UnityPlugin.VRModuleManagement.SteamVRModule:UpdateDeviceConnectionAndPose(Boolean) (at Assets/HTC.UnityPlugin/VRModule/Modules/SteamVRv2Module.cs:478)
Valve.VR.SteamVR_Input:UpdatePoseActions(Boolean) (at Assets/SteamVR/Input/SteamVR_Input.cs:338)
Valve.VR.SteamVR_Input:UpdateVisualActions(Boolean) (at Assets/SteamVR/Input/SteamVR_Input.cs:317)
Valve.VR.SteamVR_Input:OnPreCull() (at Assets/SteamVR/Input/SteamVR_Input.cs:302)
Valve.VR.SteamVR_Behaviour:PreCull() (at Assets/SteamVR/Scripts/SteamVR_Behaviour.cs:221)
Valve.VR.SteamVR_Behaviour:OnBeforeRender() (at Assets/SteamVR/Scripts/SteamVR_Behaviour.cs:189)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&)

Of note: I'm not using any SteamVR_Behaviors in my scene directly.

The error message, "ArgumentException: Expected control 'pose' to be of type 'PoseControl' but is of type 'PoseControl' instead!" seems to be coming from a type mismatch that was not obvious at all to me at first. Once I was looking in the debugger, I saw the PoseControl it mentions are in fact different classes:

image

I'm unsure how to tell the whole system to use the correct one (or even really what the "correct one" even should be!)

Let me know if I can provide any more information.

Cheers!

Here's my patch for explicit new input system usage:

From 2ce3fa596942cdf41405a52c5f3e6411b195993f Mon Sep 17 00:00:00 2001
From: Chris Dailey <nitz@users.noreply.github.com>
Date: Wed, 8 Dec 2021 10:57:49 -0500
Subject: [PATCH] HTC.UnityPlugin new input system workarounds.

---
 .../VRPlatformSettings/OpenVRSettings.cs      | 16 +++++++++-----
 .../Scripts/Misc/ExternalCameraHook.cs        | 21 ++++++++++++++++---
 .../ExternalCameraSettings.cs                 | 18 ++++++++++------
 3 files changed, 41 insertions(+), 14 deletions(-)

diff --git Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/Editor/VRPlatformSettings/OpenVRSettings.cs Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/Editor/VRPlatformSettings/OpenVRSettings.cs
index 0b1a0da..33b510b 100644
--- Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/Editor/VRPlatformSettings/OpenVRSettings.cs
+++ Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/Editor/VRPlatformSettings/OpenVRSettings.cs
@@ -23,6 +23,12 @@ using Valve.VR;
 using HTC.UnityPlugin.Vive.SteamVRExtension;
 #endif

+#if ENABLE_INPUT_SYSTEM
+using InputKeyType = UnityEngine.InputSystem.Key;
+#else
+using InputKeyType = UnityEngine.KeyCode;
+#endif // ENABLE_INPUT_SYSTEM
+
 namespace HTC.UnityPlugin.Vive
 {
     public class OpenVRRecommendedSettings : VIUVersionCheck.RecommendedSettingCollection
@@ -591,8 +597,8 @@ namespace HTC.UnityPlugin.Vive
                     {
                         EditorGUI.indentLevel++;

-                        VIUSettings.externalCameraSwitchKey = (KeyCode)EditorGUILayout.EnumPopup("Switch Key", VIUSettings.externalCameraSwitchKey);
-                        VIUSettings.externalCameraSwitchKeyModifier = (KeyCode)EditorGUILayout.EnumPopup("Switch Key Modifier", VIUSettings.externalCameraSwitchKeyModifier);
+                        VIUSettings.externalCameraSwitchKey = (InputKeyType)EditorGUILayout.EnumPopup("Switch Key", VIUSettings.externalCameraSwitchKey);
+                        VIUSettings.externalCameraSwitchKeyModifier = (InputKeyType)EditorGUILayout.EnumPopup("Switch Key Modifier", VIUSettings.externalCameraSwitchKeyModifier);

                         EditorGUI.indentLevel--;
                     }
@@ -612,11 +618,11 @@ namespace HTC.UnityPlugin.Vive
 #if VIU_XR_GENERAL_SETTINGS
                         "Input" +
 #elif UNITY_2017_1_OR_NEWER
-                        "External-Camera(Mix-Reality), animated controller model" + 
+                        "External-Camera(Mix-Reality), animated controller model" +
                         ", VIVE Controller haptics(vibration)" +
                         ", VIVE Tracker USB/Pogo-pin input" +
 #else
-                        "External-Camera(Mix-Reality), animated controller model" + 
+                        "External-Camera(Mix-Reality), animated controller model" +
                         ", VIVE Controller haptics(vibration)" +
                         ", VIVE Tracker device" +
 #endif
@@ -656,4 +662,4 @@ namespace HTC.UnityPlugin.Vive
             }
         }
     }
-}
\ No newline at end of file
+}
diff --git Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/Misc/ExternalCameraHook.cs Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/Misc/ExternalCameraHook.cs
index 26386fb..d8852ad 100644
--- Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/Misc/ExternalCameraHook.cs
+++ Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/Misc/ExternalCameraHook.cs
@@ -4,7 +4,16 @@ using HTC.UnityPlugin.Utility;
 using HTC.UnityPlugin.VRModuleManagement;
 using System;
 using System.IO;
+
 using UnityEngine;
+
+#if ENABLE_INPUT_SYSTEM
+using UnityEngine.InputSystem;
+using InputKeyType = UnityEngine.InputSystem.Key;
+#else
+using InputKeyType = UnityEngine.KeyCode;
+#endif // ENABLE_INPUT_SYSTEM
+
 #if VIU_STEAMVR_2_0_0_OR_NEWER && UNITY_STANDALONE
 using Valve.VR;
 #endif
@@ -271,8 +280,14 @@ namespace HTC.UnityPlugin.Vive

         private void Update()
         {
-            if (VIUSettings.enableExternalCameraSwitch && Input.GetKeyDown(VIUSettings.externalCameraSwitchKey) && (VIUSettings.externalCameraSwitchKeyModifier != KeyCode.None && Input.GetKey(VIUSettings.externalCameraSwitchKeyModifier)))
-            {
+#if ENABLE_INPUT_SYSTEM
+           bool KeyDown(Key k) => Keyboard.current[k].wasPressedThisFrame;
+           bool KeyIsPressed(Key k) => Keyboard.current[k].isPressed;
+           if (VIUSettings.enableExternalCameraSwitch && KeyDown(VIUSettings.externalCameraSwitchKey) && (VIUSettings.externalCameraSwitchKeyModifier != Key.None && KeyIsPressed(VIUSettings.externalCameraSwitchKeyModifier)))
+#else
+           if (VIUSettings.enableExternalCameraSwitch && Input.GetKeyDown(VIUSettings.externalCameraSwitchKey) && (VIUSettings.externalCameraSwitchKeyModifier != KeyCode.None && Input.GetKey(VIUSettings.externalCameraSwitchKeyModifier)))
+#endif
+           {
                 if (!isQuadViewActive)
                 {
                     m_quadViewSwitch = true;
@@ -412,4 +427,4 @@ namespace HTC.UnityPlugin.Vive
         public void Recenter() { }
 #endif
     }
-}
\ No newline at end of file
+}
diff --git Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/VIUSettingsPartials/ExternalCameraSettings.cs Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/VIUSettingsPartials/ExternalCameraSettings.cs
index 72910a1..d8cb77b 100644
--- Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/VIUSettingsPartials/ExternalCameraSettings.cs
+++ Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/VIUSettingsPartials/ExternalCameraSettings.cs
@@ -2,6 +2,12 @@

 using UnityEngine;

+#if ENABLE_INPUT_SYSTEM
+using InputKeyType = UnityEngine.InputSystem.Key;
+#else
+using InputKeyType = UnityEngine.KeyCode;
+#endif // ENABLE_INPUT_SYSTEM
+
 namespace HTC.UnityPlugin.Vive
 {
     public partial class VIUSettings : ScriptableObject
@@ -11,8 +17,8 @@ namespace HTC.UnityPlugin.Vive
         public const bool AUTO_LOAD_EXTERNAL_CAMERA_CONFIG_ON_START_DEFAULT_VALUE = true;
         public const string EXTERNAL_CAMERA_CONFIG_FILE_PATH_DEFAULT_VALUE = "externalcamera.cfg";
         public const bool ENABLE_EXTERNAL_CAMERA_SWITCH_DEFAULT_VALUE = true;
-        public const KeyCode EXTERNAL_CAMERA_SWITCH_KEY_DEFAULT_VALUE = KeyCode.M;
-        public const KeyCode EXTERNAL_CAMERA_SWITCH_KEY_MODIFIER_DEFAULT_VALUE = KeyCode.RightShift;
+       public const InputKeyType EXTERNAL_CAMERA_SWITCH_KEY_DEFAULT_VALUE = InputKeyType.M;
+       public const InputKeyType EXTERNAL_CAMERA_SWITCH_KEY_MODIFIER_DEFAULT_VALUE = InputKeyType.RightShift;

         [SerializeField]
         private bool m_autoLoadExternalCameraConfigOnStart = AUTO_LOAD_EXTERNAL_CAMERA_CONFIG_ON_START_DEFAULT_VALUE;
@@ -21,14 +27,14 @@ namespace HTC.UnityPlugin.Vive
         [SerializeField, Tooltip(EX_CAM_UI_SWITCH_TOOLTIP)]
         private bool m_enableExternalCameraSwitch = ENABLE_EXTERNAL_CAMERA_SWITCH_DEFAULT_VALUE;
         [SerializeField]
-        private KeyCode m_externalCameraSwitchKey = EXTERNAL_CAMERA_SWITCH_KEY_DEFAULT_VALUE;
+        private InputKeyType m_externalCameraSwitchKey = EXTERNAL_CAMERA_SWITCH_KEY_DEFAULT_VALUE;
         [SerializeField]
-        private KeyCode m_externalCameraSwitchKeyModifier = EXTERNAL_CAMERA_SWITCH_KEY_MODIFIER_DEFAULT_VALUE;
+        private InputKeyType m_externalCameraSwitchKeyModifier = EXTERNAL_CAMERA_SWITCH_KEY_MODIFIER_DEFAULT_VALUE;

         public static bool autoLoadExternalCameraConfigOnStart { get { return Instance == null ? AUTO_LOAD_EXTERNAL_CAMERA_CONFIG_ON_START_DEFAULT_VALUE : s_instance.m_autoLoadExternalCameraConfigOnStart; } set { if (Instance != null) { Instance.m_autoLoadExternalCameraConfigOnStart = value; } } }
         public static string externalCameraConfigFilePath { get { return Instance == null ? EXTERNAL_CAMERA_CONFIG_FILE_PATH_DEFAULT_VALUE : s_instance.m_externalCameraConfigFilePath; } set { if (Instance != null) { Instance.m_externalCameraConfigFilePath = value; } } }
         public static bool enableExternalCameraSwitch { get { return Instance == null ? ENABLE_EXTERNAL_CAMERA_SWITCH_DEFAULT_VALUE : s_instance.m_enableExternalCameraSwitch; } set { if (Instance != null) { Instance.m_enableExternalCameraSwitch = value; } } }
-        public static KeyCode externalCameraSwitchKey { get { return Instance == null ? EXTERNAL_CAMERA_SWITCH_KEY_DEFAULT_VALUE : s_instance.m_externalCameraSwitchKey; } set { if (Instance != null) { Instance.m_externalCameraSwitchKey = value; } } }
-        public static KeyCode externalCameraSwitchKeyModifier { get { return Instance == null ? EXTERNAL_CAMERA_SWITCH_KEY_MODIFIER_DEFAULT_VALUE : s_instance.m_externalCameraSwitchKeyModifier; } set { if (Instance != null) { Instance.m_externalCameraSwitchKeyModifier = value; } } }
+        public static InputKeyType externalCameraSwitchKey { get { return Instance == null ? EXTERNAL_CAMERA_SWITCH_KEY_DEFAULT_VALUE : s_instance.m_externalCameraSwitchKey; } set { if (Instance != null) { Instance.m_externalCameraSwitchKey = value; } } }
+        public static InputKeyType externalCameraSwitchKeyModifier { get { return Instance == null ? EXTERNAL_CAMERA_SWITCH_KEY_MODIFIER_DEFAULT_VALUE : s_instance.m_externalCameraSwitchKeyModifier; } set { if (Instance != null) { Instance.m_externalCameraSwitchKeyModifier = value; } } }
     }
 }
-- 
2.34.0.windows.1
lawwong commented 2 years ago

Hi,

Thanks for the feedback, we just released v1.14.2 and fix "PoseControl" issue, it is caused by the two types that share same InputControlLayout

About LegacyInput migrate InputSystem, thanks for your patch advice but there are other place also need to be fixed/replaced and we haven't decide how do it.

For now I suggest you enable both input handle in player settings if you are using feature that involves legacy input.

viu_playersettings_input