暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GenericXRDevice.cs 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // ENABLE_VR is not defined on Game Core but the assembly is available with limited features when the XR module is enabled.
  2. #if UNITY_INPUT_SYSTEM_ENABLE_XR && (ENABLE_VR || UNITY_GAMECORE) && !UNITY_FORCE_INPUTSYSTEM_XR_OFF || PACKAGE_DOCS_GENERATION
  3. using UnityEngine.InputSystem.Controls;
  4. using UnityEngine.InputSystem.XR.Haptics;
  5. using UnityEngine.InputSystem.Layouts;
  6. using UnityEngine.XR;
  7. namespace UnityEngine.InputSystem.XR
  8. {
  9. [InputControlLayout(isGenericTypeOfDevice = true, displayName = "XR HMD", canRunInBackground = true)]
  10. public class XRHMD : TrackedDevice
  11. {
  12. /// <summary>
  13. /// The base type of all XR head mounted displays. This can help organize shared behaviour across all HMDs.
  14. /// </summary>
  15. ///
  16. /// <remarks>
  17. ///
  18. /// To give your head tracking an extra update before rendering:
  19. /// First, enable before render updates on your Device.
  20. ///
  21. /// <sample>
  22. /// <code>
  23. /// // JSON
  24. /// {
  25. /// "name" : "MyHMD",
  26. /// "extend" : "HMD",
  27. /// "beforeRender" : "Update"
  28. /// }
  29. /// </code>
  30. /// </sample>
  31. ///
  32. /// Then, make sure you put extra `StateEvents` for your HMD on the queue right in time before rendering. Also, if your HMD is a combination of non-tracking and tracking controls, you can update just the tracking by sending a delta event instead of a full state event.
  33. ///
  34. /// </remarks>
  35. [InputControl(noisy = true)]
  36. public Vector3Control leftEyePosition { get; protected set; }
  37. [InputControl(noisy = true)]
  38. public QuaternionControl leftEyeRotation { get; protected set; }
  39. [InputControl(noisy = true)]
  40. public Vector3Control rightEyePosition { get; protected set; }
  41. [InputControl(noisy = true)]
  42. public QuaternionControl rightEyeRotation { get; protected set; }
  43. [InputControl(noisy = true)]
  44. public Vector3Control centerEyePosition { get; protected set; }
  45. [InputControl(noisy = true)]
  46. public QuaternionControl centerEyeRotation { get; protected set; }
  47. protected override void FinishSetup()
  48. {
  49. base.FinishSetup();
  50. centerEyePosition = GetChildControl<Vector3Control>("centerEyePosition");
  51. centerEyeRotation = GetChildControl<QuaternionControl>("centerEyeRotation");
  52. leftEyePosition = GetChildControl<Vector3Control>("leftEyePosition");
  53. leftEyeRotation = GetChildControl<QuaternionControl>("leftEyeRotation");
  54. rightEyePosition = GetChildControl<Vector3Control>("rightEyePosition");
  55. rightEyeRotation = GetChildControl<QuaternionControl>("rightEyeRotation");
  56. }
  57. }
  58. /// <summary>
  59. /// The base type for all XR handed controllers.
  60. /// </summary>
  61. [InputControlLayout(commonUsages = new[] { "LeftHand", "RightHand" }, isGenericTypeOfDevice = true, displayName = "XR Controller")]
  62. public class XRController : TrackedDevice
  63. {
  64. /// <summary>
  65. /// A quick accessor for the currently active left handed device.
  66. /// </summary>
  67. /// <remarks>
  68. /// If there is no left hand connected, this will be null.
  69. /// This also matches any currently tracked device that contains the 'LeftHand' device usage.
  70. /// <example>
  71. /// <code>
  72. /// // To set up an Action to specifically target
  73. /// // the left-hand XR controller:
  74. ///
  75. /// var action = new InputAction(binding: "/&lt;XRController&gt;{leftHand}/position");
  76. /// </code>
  77. /// </example>
  78. ///
  79. /// <example>
  80. /// <code>
  81. /// // To make the left-hand XR controller behave like the right-hand one
  82. /// var controller = XRController.leftHand;
  83. /// InputSystem.SetUsage(controller, CommonUsages.RightHand);
  84. /// </code>
  85. /// </example>
  86. /// </remarks>
  87. public static XRController leftHand => InputSystem.GetDevice<XRController>(CommonUsages.LeftHand);
  88. /// <summary>
  89. /// A quick accessor for the currently active right handed device. This is also tracked via usages on the device.
  90. /// </summary>
  91. /// <remarks>If there is no left hand connected, this will be null. This also matches any currently tracked device that contains the 'RightHand' device usage.</remarks>
  92. public static XRController rightHand => InputSystem.GetDevice<XRController>(CommonUsages.RightHand);
  93. protected override void FinishSetup()
  94. {
  95. base.FinishSetup();
  96. var capabilities = description.capabilities;
  97. var deviceDescriptor = XRDeviceDescriptor.FromJson(capabilities);
  98. if (deviceDescriptor != null)
  99. {
  100. if ((deviceDescriptor.characteristics & InputDeviceCharacteristics.Left) != 0)
  101. InputSystem.SetDeviceUsage(this, CommonUsages.LeftHand);
  102. else if ((deviceDescriptor.characteristics & InputDeviceCharacteristics.Right) != 0)
  103. InputSystem.SetDeviceUsage(this, CommonUsages.RightHand);
  104. }
  105. }
  106. }
  107. /// <summary>
  108. /// Identifies a controller that is capable of rumble or haptics.
  109. /// </summary>
  110. public class XRControllerWithRumble : XRController
  111. {
  112. public void SendImpulse(float amplitude, float duration)
  113. {
  114. var command = SendHapticImpulseCommand.Create(0, amplitude, duration);
  115. ExecuteCommand(ref command);
  116. }
  117. }
  118. }
  119. #endif