Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. using UnityEngine.SpatialTracking;
  2.  
  3. #if LIH_PRESENT
  4. using UnityEngine.Experimental.XR.Interaction;
  5. #endif
  6.  
  7. namespace UnityEngine.XR.Interaction.Toolkit
  8. {
  9.     /// <summary>
  10.     /// <see cref="XRBaseController"/> <see cref="MonoBehaviour"/> that interprets
  11.     /// feature values on an input device in the XR input subsystem into
  12.     /// XR Interaction Interactor position, rotation, and interaction states.
  13.     /// </summary>
  14.     [AddComponentMenu("XR/XR Controller (Device-based)")]
  15.     public class XRController : XRBaseController
  16.     {
  17.         [SerializeField]
  18.         [Tooltip("The XRNode for this controller.")]
  19.         XRNode m_ControllerNode = XRNode.RightHand;
  20.  
  21.         /// <summary>
  22.         /// The <see cref="XRNode"/> for this controller.
  23.         /// </summary>
  24.         public XRNode controllerNode
  25.         {
  26.             get => m_ControllerNode;
  27.             set => m_ControllerNode = value;
  28.         }
  29.  
  30.         [SerializeField]
  31.         [Tooltip("The input to use for detecting a select.")]
  32.         InputHelpers.Button m_SelectUsage = InputHelpers.Button.Grip;
  33.  
  34.         /// <summary>
  35.         /// The input to use for detecting a select.
  36.         /// </summary>
  37.         public InputHelpers.Button selectUsage
  38.         {
  39.             get => m_SelectUsage;
  40.             set => m_SelectUsage = value;
  41.         }
  42.  
  43.         [SerializeField]
  44.         [Tooltip("The input to use for detecting activation.")]
  45.         InputHelpers.Button m_ActivateUsage = InputHelpers.Button.Trigger;
  46.  
  47.         /// <summary>
  48.         /// The input to use for detecting activation.
  49.         /// </summary>
  50.         public InputHelpers.Button activateUsage
  51.         {
  52.             get => m_ActivateUsage;
  53.             set => m_ActivateUsage = value;
  54.         }
  55.  
  56.         [SerializeField]
  57.         [Tooltip("The input to use for detecting a UI press.")]
  58.         InputHelpers.Button m_UIPressUsage = InputHelpers.Button.Trigger;
  59.  
  60.         /// <summary>
  61.         /// The input to use for detecting a UI press.
  62.         /// </summary>
  63.         public InputHelpers.Button uiPressUsage
  64.         {
  65.             get => m_UIPressUsage;
  66.             set => m_UIPressUsage = value;
  67.         }
  68.  
  69.         [SerializeField]
  70.         [Tooltip("The amount an axis needs to be pressed to trigger an interaction event.")]
  71.         float m_AxisToPressThreshold = 0.1f;
  72.  
  73.         /// <summary>
  74.         /// The amount an axis needs to be pressed to trigger an interaction event.
  75.         /// </summary>
  76.         public float axisToPressThreshold
  77.         {
  78.             get => m_AxisToPressThreshold;
  79.             set => m_AxisToPressThreshold = value;
  80.         }
  81.  
  82.         [SerializeField]
  83.         [Tooltip("The input to use to rotate an anchor to the Left.")]
  84.         InputHelpers.Button m_RotateAnchorLeft = InputHelpers.Button.PrimaryAxis2DLeft;
  85.  
  86.         /// <summary>
  87.         /// The input to use to rotate an anchor to the Left.
  88.         /// </summary>
  89.         public InputHelpers.Button rotateObjectLeft
  90.         {
  91.             get => m_RotateAnchorLeft;
  92.             set => m_RotateAnchorLeft = value;
  93.         }
  94.  
  95.         [SerializeField]
  96.         [Tooltip("The input to use to rotate an anchor to the Right.")]
  97.         InputHelpers.Button m_RotateAnchorRight = InputHelpers.Button.PrimaryAxis2DRight;
  98.  
  99.         /// <summary>
  100.         /// The input to use to rotate an anchor to the Right.
  101.         /// </summary>
  102.         public InputHelpers.Button rotateObjectRight
  103.         {
  104.             get => m_RotateAnchorRight;
  105.             set => m_RotateAnchorRight = value;
  106.         }
  107.  
  108.         [SerializeField]
  109.         [Tooltip("The input that will be used to translate the anchor away from the interactor.")]
  110.         InputHelpers.Button m_MoveObjectIn = InputHelpers.Button.PrimaryAxis2DUp;
  111.  
  112.         /// <summary>
  113.         /// The input that will be used to translate the anchor away from the interactor.
  114.         /// </summary>
  115.         public InputHelpers.Button moveObjectIn
  116.         {
  117.             get => m_MoveObjectIn;
  118.             set => m_MoveObjectIn = value;
  119.         }
  120.  
  121.         [SerializeField]
  122.         [Tooltip("The input that will be used to translate the anchor towards the interactor.")]
  123.         InputHelpers.Button m_MoveObjectOut = InputHelpers.Button.PrimaryAxis2DDown;
  124.  
  125.         /// <summary>
  126.         /// The input that will be used to translate the anchor towards the interactor.
  127.         /// </summary>
  128.         public InputHelpers.Button moveObjectOut
  129.         {
  130.             get => m_MoveObjectOut;
  131.             set => m_MoveObjectOut = value;
  132.         }
  133.  
  134. #if LIH_PRESENT
  135.         [SerializeField, Tooltip("Pose provider used to provide tracking data separate from the XR Node.")]
  136.         BasePoseProvider m_PoseProvider;
  137.  
  138.         /// <summary>
  139.         /// Pose provider used to provide tracking data separate from the <see cref="XRNode"/>.
  140.         /// </summary>
  141.         public BasePoseProvider poseProvider
  142.         {
  143.             get => m_PoseProvider;
  144.             set => m_PoseProvider = value;
  145.         }
  146. #endif
  147.  
  148.         public InputDeviceWrapper inputDevice
  149.         {
  150.             get
  151.             {
  152.                 return m_InputDevice.isValid ? m_InputDevice : (m_InputDevice = new InputDeviceWrapper(controllerNode));
  153.             }
  154.         }
  155.         private InputDeviceWrapper m_InputDevice;
  156.  
  157.         /// <inheritdoc />
  158.         protected override void UpdateTrackingInput(XRControllerState controllerState)
  159.         {
  160.             controllerState.poseDataFlags = PoseDataFlags.NoData;
  161. #if LIH_PRESENT_V1API
  162.             if (m_PoseProvider != null)
  163.             {
  164.                 if (m_PoseProvider.TryGetPoseFromProvider(out var poseProviderPose))
  165.                 {
  166.                     controllerState.position = poseProviderPose.position;
  167.                     controllerState.rotation = poseProviderPose.rotation;
  168.                     controllerState.poseDataFlags = PoseDataFlags.Position | PoseDataFlags.Rotation;
  169.                 }
  170.             }
  171.             else
  172. #elif LIH_PRESENT_V2API
  173.             if (m_PoseProvider != null)
  174.             {
  175.                 var retFlags = m_PoseProvider.GetPoseFromProvider(out var poseProviderPose);
  176.                 if ((retFlags & PoseDataFlags.Position) != 0)
  177.                 {
  178.                     controllerState.position = poseProviderPose.position;
  179.                     controllerState.poseDataFlags |= PoseDataFlags.Position;
  180.                 }
  181.                 if ((retFlags & PoseDataFlags.Rotation) != 0)
  182.                 {
  183.                     controllerState.rotation = poseProviderPose.rotation;
  184.                     controllerState.poseDataFlags |= PoseDataFlags.Rotation;
  185.                 }
  186.             }
  187.             else
  188. #endif
  189.             {
  190.                 if (inputDevice.TryGetFeatureValue(CommonUsages.devicePosition, out controllerState.position))
  191.                 {
  192.                     controllerState.poseDataFlags |= PoseDataFlags.Position;
  193.                 }
  194.  
  195.                 if (inputDevice.TryGetFeatureValue(CommonUsages.deviceRotation, out controllerState.rotation))
  196.                 {
  197.                     controllerState.poseDataFlags |= PoseDataFlags.Rotation;
  198.                 }
  199.             }
  200.         }
  201.  
  202.         /// <inheritdoc />
  203.         protected override void UpdateInput(XRControllerState controllerState)
  204.         {
  205.             controllerState.ResetFrameDependentStates();
  206.  
  207.             HandleInteractionAction(m_SelectUsage, ref controllerState.selectInteractionState);
  208.             HandleInteractionAction(m_ActivateUsage, ref controllerState.activateInteractionState);
  209.             HandleInteractionAction(m_UIPressUsage, ref controllerState.uiPressInteractionState);
  210.         }
  211.  
  212.         void HandleInteractionAction(InputHelpers.Button button, ref InteractionState interactionState)
  213.         {
  214.             inputDevice.IsPressed(button, out var pressed, m_AxisToPressThreshold);
  215.  
  216.             if (pressed)
  217.             {
  218.                 if (!interactionState.active)
  219.                 {
  220.                     interactionState.activatedThisFrame = true;
  221.                     interactionState.active = true;
  222.                 }
  223.             }
  224.             else
  225.             {
  226.                 if (interactionState.active)
  227.                 {
  228.                     interactionState.deactivatedThisFrame = true;
  229.                     interactionState.active = false;
  230.                 }
  231.             }
  232.         }
  233.  
  234.         /// <inheritdoc />
  235.         public override bool SendHapticImpulse(float amplitude, float duration)
  236.         {
  237.             if (inputDevice.TryGetHapticCapabilities(out var capabilities) &&
  238.                 capabilities.supportsImpulse)
  239.             {
  240.                 return inputDevice.SendHapticImpulse(0u, amplitude, duration);
  241.             }
  242.             return false;
  243.         }
  244.     }
  245. }
  246.