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