当前位置:   article > 正文

Unity中使用XR Input获取XR设备中手柄的姿态信息_untiy中xrcontroller对应inputdevice.trygetfeatureusage

untiy中xrcontroller对应inputdevice.trygetfeatureusages怎么用

在上一篇博客Unity在XR设备中获取手柄的按键信息_XR风云-CSDN博客我们知道怎样获取手柄的按键信息,那怎样获取得到手柄的姿态信息呢?

也是很简单的了,Unity的XR Input已经帮我们对接上Oculus平台,能够获取手柄姿态信息。

1、根据输入设备的类型获取设备,设备类型包括如下,我们今天是获取手柄信息,主要用到Left或者Right。

  1. //
  2. // 摘要:
  3. // A set of bit flags describing XR.InputDevice characteristics.
  4. [Flags]
  5. public enum InputDeviceCharacteristics : uint
  6. {
  7. //
  8. // 摘要:
  9. // A default value specifying no flags.
  10. None = 0,
  11. //
  12. // 摘要:
  13. // The InputDevice is attached to the head.
  14. HeadMounted = 1,
  15. //
  16. // 摘要:
  17. // The InputDevice has a camera and associated camera tracking information.
  18. Camera = 2,
  19. //
  20. // 摘要:
  21. // The InputDevice is held in the user's hand. Typically, a tracked controller.
  22. HeldInHand = 4,
  23. //
  24. // 摘要:
  25. // The InputDevice provides hand tracking information via a Hand input feature.
  26. HandTracking = 8,
  27. //
  28. // 摘要:
  29. // The InputDevice provides eye tracking information via an Eyes input feature.
  30. EyeTracking = 16,
  31. //
  32. // 摘要:
  33. // The InputDevice provides 3DOF or 6DOF tracking data.
  34. TrackedDevice = 32,
  35. //
  36. // 摘要:
  37. // The InputDevice is a game controller.
  38. Controller = 64,
  39. //
  40. // 摘要:
  41. // The InputDevice is an unmoving reference object used to locate and track other
  42. // objects in the world.
  43. TrackingReference = 128,
  44. //
  45. // 摘要:
  46. // The InputDevice is associated with the left side of the user.
  47. Left = 256,
  48. //
  49. // 摘要:
  50. // The InputDevice is associated with the right side of the user.
  51. Right = 512,
  52. //
  53. // 摘要:
  54. // The InputDevice reports software approximated, positional data.
  55. Simulated6DOF = 1024
  56. }

2、 姿态信息主要使用2个值,CommonUsages.devicePosition, CommonUsages.deviceRotation,来获取得到位置信息以及旋转信息。我们封装了一些借口,获取得到某个手柄的姿态信息。

  1. public static bool Get(InputDeviceCharacteristics desiredCharacteristics, InputFeatureUsage<Vector3> inputFeatureUsage, out Vector3 value)
  2. {
  3. List<InputDevice> allDevices = new List<InputDevice>();
  4. InputDevices.GetDevicesWithCharacteristics(desiredCharacteristics, allDevices);
  5. if (allDevices.Count <= 0)
  6. {
  7. Debug.LogError($"not has device by {desiredCharacteristics}");
  8. value = default;
  9. return false;
  10. }
  11. if (allDevices.Count > 1)
  12. {
  13. Debug.LogError($"If the number of devices exceeds 1, the first device is selected by default. ");
  14. value = default;
  15. return false;
  16. }
  17. InputDevice device = allDevices[0];
  18. if (!device.TryGetFeatureValue(inputFeatureUsage, out value))
  19. {
  20. Debug.LogError($"TryGetFeatureValue {inputFeatureUsage} failed!");
  21. value = default;
  22. return false;
  23. }
  24. return true;
  25. }
  26. public static bool Get(InputDeviceCharacteristics desiredCharacteristics, InputFeatureUsage<Quaternion> inputFeatureUsage, out Quaternion value)
  27. {
  28. List<InputDevice> allDevices = new List<InputDevice>();
  29. InputDevices.GetDevicesWithCharacteristics(desiredCharacteristics, allDevices);
  30. if (allDevices.Count <= 0)
  31. {
  32. Debug.LogError($"not has device by {desiredCharacteristics}");
  33. value = default;
  34. return false;
  35. }
  36. if (allDevices.Count > 1)
  37. {
  38. Debug.LogError($"If the number of devices exceeds 1, the first device is selected by default. ");
  39. value = default;
  40. return false;
  41. }
  42. InputDevice device = allDevices[0];
  43. if (!device.TryGetFeatureValue(inputFeatureUsage, out value))
  44. {
  45. Debug.LogError($"TryGetFeatureValue {inputFeatureUsage} failed!");
  46. value = default;
  47. return false;
  48. }
  49. return true;
  50. }

3、获取右手柄的姿态信息调用如下,获取其他信息也是一样的方法。

  1. XRMeetingInput.Get(InputDeviceCharacteristics.Right, CommonUsages.devicePosition, out Vector3 postion);
  2. XRMeetingInput.Get(InputDeviceCharacteristics.Right, CommonUsages.deviceRotation, out Quaternion rotation);

4、获取得到的信息,我开始以为获取得到是手柄的世界坐标,但是我怎么调试都不正确。经过查询资料才得知,这个函数返回的姿态信息都是本地姿态localPosition,localRotation,不是世界坐标。

所以需要把他们转换成世界坐标,比如在Oculus中需要知道playspace的Transform,然后进行转换成世界坐标,如下转换:

  1. Vector3 position = MixedRealityPlayspace.Transform.TransformPoint(localPosition);
  2. Quaternion rotation = MixedRealityPlayspace.Rotation * localRotation;

这样就可以获取得到手柄的世界坐标。

参考文献:

Unity - Manual: Unity XR Input

Unity - Scripting API: CommonUsages

Unity - Scripting API: XR.InputDevices.GetDevicesWithCharacteristics

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/1010721
推荐阅读
相关标签
  

闽ICP备14008679号