// ENABLE_VR is not defined on Game Core but the assembly is available with limited features when the XR module is enabled.
#if UNITY_INPUT_SYSTEM_ENABLE_XR && (ENABLE_VR || UNITY_GAMECORE) && !UNITY_FORCE_INPUTSYSTEM_XR_OFF || PACKAGE_DOCS_GENERATION
using UnityEngine.InputSystem.Controls;
using UnityEngine.InputSystem.XR.Haptics;
using UnityEngine.InputSystem.Layouts;
using UnityEngine.XR;
namespace UnityEngine.InputSystem.XR
{
[InputControlLayout(isGenericTypeOfDevice = true, displayName = "XR HMD", canRunInBackground = true)]
public class XRHMD : TrackedDevice
{
///
/// The base type of all XR head mounted displays. This can help organize shared behaviour across all HMDs.
///
///
///
///
/// To give your head tracking an extra update before rendering:
/// First, enable before render updates on your Device.
///
///
///
/// // JSON
/// {
/// "name" : "MyHMD",
/// "extend" : "HMD",
/// "beforeRender" : "Update"
/// }
///
///
///
/// 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.
///
///
[InputControl(noisy = true)]
public Vector3Control leftEyePosition { get; protected set; }
[InputControl(noisy = true)]
public QuaternionControl leftEyeRotation { get; protected set; }
[InputControl(noisy = true)]
public Vector3Control rightEyePosition { get; protected set; }
[InputControl(noisy = true)]
public QuaternionControl rightEyeRotation { get; protected set; }
[InputControl(noisy = true)]
public Vector3Control centerEyePosition { get; protected set; }
[InputControl(noisy = true)]
public QuaternionControl centerEyeRotation { get; protected set; }
protected override void FinishSetup()
{
base.FinishSetup();
centerEyePosition = GetChildControl("centerEyePosition");
centerEyeRotation = GetChildControl("centerEyeRotation");
leftEyePosition = GetChildControl("leftEyePosition");
leftEyeRotation = GetChildControl("leftEyeRotation");
rightEyePosition = GetChildControl("rightEyePosition");
rightEyeRotation = GetChildControl("rightEyeRotation");
}
}
///
/// The base type for all XR handed controllers.
///
[InputControlLayout(commonUsages = new[] { "LeftHand", "RightHand" }, isGenericTypeOfDevice = true, displayName = "XR Controller")]
public class XRController : TrackedDevice
{
///
/// A quick accessor for the currently active left handed device.
///
///
/// If there is no left hand connected, this will be null.
/// This also matches any currently tracked device that contains the 'LeftHand' device usage.
///
///
/// // To set up an Action to specifically target
/// // the left-hand XR controller:
///
/// var action = new InputAction(binding: "/<XRController>{leftHand}/position");
///
///
///
///
///
/// // To make the left-hand XR controller behave like the right-hand one
/// var controller = XRController.leftHand;
/// InputSystem.SetUsage(controller, CommonUsages.RightHand);
///
///
///
public static XRController leftHand => InputSystem.GetDevice(CommonUsages.LeftHand);
///
/// A quick accessor for the currently active right handed device. This is also tracked via usages on the device.
///
/// If there is no left hand connected, this will be null. This also matches any currently tracked device that contains the 'RightHand' device usage.
public static XRController rightHand => InputSystem.GetDevice(CommonUsages.RightHand);
protected override void FinishSetup()
{
base.FinishSetup();
var capabilities = description.capabilities;
var deviceDescriptor = XRDeviceDescriptor.FromJson(capabilities);
if (deviceDescriptor != null)
{
if ((deviceDescriptor.characteristics & InputDeviceCharacteristics.Left) != 0)
InputSystem.SetDeviceUsage(this, CommonUsages.LeftHand);
else if ((deviceDescriptor.characteristics & InputDeviceCharacteristics.Right) != 0)
InputSystem.SetDeviceUsage(this, CommonUsages.RightHand);
}
}
}
///
/// Identifies a controller that is capable of rumble or haptics.
///
public class XRControllerWithRumble : XRController
{
public void SendImpulse(float amplitude, float duration)
{
var command = SendHapticImpulseCommand.Create(0, amplitude, duration);
ExecuteCommand(ref command);
}
}
}
#endif