SHARE
TWEET

Untitled

a guest Feb 17th, 2020 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using Microsoft.MixedReality.Toolkit;
  2. using Microsoft.MixedReality.Toolkit.Input;
  3. using Microsoft.MixedReality.Toolkit.Utilities;
  4. using System;
  5. using UnityEngine;
  6. using UnityEngine.XR;
  7.  
  8. public class ControllerSourceManager : MonoBehaviour, IMixedRealitySourceStateHandler, IMixedRealityInputHandler
  9. {
  10.     // declare delegate
  11.     public delegate void OnControllerStatusChange(Handedness handedness);
  12.  
  13.     private IMixedRealityController detectedController;
  14.     private IMixedRealityHand detectedHand;
  15.     private MixedRealityPose handPose;
  16.  
  17.     private IMixedRealityController leftController, rightController;
  18.     private IMixedRealityHand leftHand, rightHand;
  19.  
  20.     public event OnControllerStatusChange OnControllerFound;
  21.     public event OnControllerStatusChange OnControllerLost;
  22.  
  23.     //private IMixedRealityInputSystem inputSystem = null;
  24.     //protected IMixedRealityInputSystem InputSystem
  25.     //{
  26.     //    get
  27.     //    {
  28.     //        if (inputSystem == null)
  29.     //        {
  30.     //            MixedRealityServiceRegistry.TryGetService(out inputSystem);
  31.     //        }
  32.     //        return inputSystem;
  33.     //    }
  34.     //}
  35.  
  36.     public static ControllerSourceManager Instance { get; private set; }
  37.  
  38.     private void Awake()
  39.     {
  40.         Instance = this;
  41.     }
  42.  
  43.     private void OnEnable()
  44.     {
  45.         CoreServices.InputSystem.RegisterHandler<IMixedRealityInputHandler>(this);
  46.         //InputSystem?.RegisterHandler<ControllerSourceManager>(this);
  47.     }
  48.  
  49.     private void OnDisable()
  50.     {
  51.         CoreServices.InputSystem.UnregisterHandler<IMixedRealityInputHandler>(this);
  52.         //InputSystem?.UnregisterHandler<ControllerSourceManager>(this);
  53.     }
  54.  
  55.     public bool TryGetControllerPose(TrackedHandJoint handJointToTrack, out Vector3 position, out Quaternion rotation, bool getPointerOnly = false)
  56.     {
  57.         bool retrieved = false;
  58.  
  59.         position = Vector3.zero;
  60.         rotation = Quaternion.identity;
  61.  
  62.         if (!XRSettings.enabled)
  63.         {
  64.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  65.             position = ray.GetPoint(0.5f);
  66.             rotation = Camera.main.transform.rotation;//Camera.main.transform.rotation + Quaternion.AngleAxis(90, Vector3.up) + Quaternion.AngleAxis(45, Vector3.left);
  67.  
  68.             retrieved = true;
  69.         }
  70.         else
  71.         {
  72.             if (getPointerOnly)
  73.             {
  74.                 try
  75.                 {
  76.                     position = (detectedHand ?? detectedController).InputSource.Pointers[0].Position;
  77.                     rotation = (detectedHand ?? detectedController).InputSource.Pointers[0].Rotation;
  78.                     retrieved = true;
  79.                 }
  80.                 catch (Exception)
  81.                 {
  82.                     retrieved = false;
  83.                 }
  84.             }
  85.             else
  86.             {
  87.                 retrieved = TryGetSpecificControllerPose(Handedness.None, handJointToTrack, out position, out rotation);
  88.             }
  89.         }
  90.  
  91.         return retrieved;
  92.     }
  93.  
  94.     public bool TryGetSpecificControllerPose(Handedness handedness, TrackedHandJoint handJointToTrack, out Vector3 position, out Quaternion rotation)
  95.     {
  96.         bool retrieved = false;
  97.  
  98.         position = Vector3.zero;
  99.         rotation = Quaternion.identity;
  100.  
  101.         IMixedRealityHand currentHand = detectedHand;
  102.         IMixedRealityController currentController = detectedController;
  103.  
  104.         if (handedness == Handedness.Left)
  105.         {
  106.             currentHand = leftHand;
  107.             currentController = leftController;
  108.         }
  109.         else if (handedness == Handedness.Right)
  110.         {
  111.             currentHand = rightHand;
  112.             currentController = rightController;
  113.         }
  114.  
  115.         if (currentHand != null && currentHand.TryGetJoint(handJointToTrack, out handPose))
  116.         {
  117.             position = handPose.Position;
  118.             rotation = handPose.Rotation;
  119.             retrieved = true;
  120.         }
  121.         else if (currentController != null)
  122.         {
  123.             try
  124.             {
  125.                 position = currentController.InputSource.Pointers[0].Position;
  126.                 rotation = currentController.InputSource.Pointers[0].Rotation;
  127.                 retrieved = true;
  128.             }
  129.             catch (Exception)
  130.             {
  131.                 retrieved = false;
  132.             }
  133.         }
  134.  
  135.         return retrieved;
  136.     }
  137.  
  138.     public bool TryGetControllerPose(out Vector3 position)
  139.     {
  140.         return TryGetControllerPose(TrackedHandJoint.Palm, out position, out _);
  141.     }
  142.  
  143.     public bool TryGetControllerPose(out Quaternion rotation)
  144.     {
  145.         return TryGetControllerPose(TrackedHandJoint.Palm, out _, out rotation);
  146.     }
  147.  
  148.     public bool TryGetControllerPose(out Vector3 position, out Quaternion rotation)
  149.     {
  150.         return TryGetControllerPose(TrackedHandJoint.Palm, out position, out rotation);
  151.     }
  152.  
  153.     public bool TryGetPointer(out Vector3 position, out Quaternion rotation)
  154.     {
  155.         bool retrieved = false;
  156.  
  157.         position = Vector3.zero;
  158.         rotation = Quaternion.identity;
  159.  
  160.         try
  161.         {
  162.             position = (detectedHand ?? detectedController).InputSource.Pointers[0].Position;
  163.             rotation = (detectedHand ?? detectedController).InputSource.Pointers[0].Rotation;
  164.             retrieved = true;
  165.         }
  166.         catch (Exception)
  167.         {
  168.             retrieved = false;
  169.         }
  170.  
  171.         return retrieved;
  172.     }
  173.  
  174.     public bool TryGetPointer(out Vector3 position)
  175.     {
  176.         return TryGetPointer(out position, out _);
  177.     }
  178.  
  179.     public bool TryGetPointer(out Quaternion rotation)
  180.     {
  181.         return TryGetPointer(out _, out rotation);
  182.     }
  183.  
  184.     public bool TryGetCursor(out Vector3 position, out Quaternion rotation)
  185.     {
  186.         bool retrieved = false;
  187.  
  188.         position = Vector3.zero;
  189.         rotation = Quaternion.identity;
  190.  
  191.         try
  192.         {
  193.             position = (detectedHand ?? detectedController).InputSource.Pointers[0].BaseCursor.Position;
  194.             rotation = (detectedHand ?? detectedController).InputSource.Pointers[0].BaseCursor.Rotation;
  195.             retrieved = true;
  196.         }
  197.         catch (Exception)
  198.         {
  199.             retrieved = false;
  200.         }
  201.  
  202.         return retrieved;
  203.     }
  204.  
  205.     public bool TryGetCursor(out Vector3 position)
  206.     {
  207.         return TryGetCursor(out position, out _);
  208.     }
  209.  
  210.     public bool TryGetCursor(out Quaternion rotation)
  211.     {
  212.         return TryGetCursor(out _, out rotation);
  213.     }
  214.  
  215.     public void ShowController(bool showController)
  216.     {
  217.         if (detectedController != null)
  218.         {
  219.             try
  220.             {
  221.                 detectedController.Visualizer.GameObjectProxy.SetActive(showController);
  222.             }
  223.             catch (Exception)
  224.             {
  225.  
  226.             }
  227.         }
  228.     }
  229.  
  230.     private void SetController(IMixedRealityController controller)
  231.     {
  232.         IMixedRealityHand hand = controller as IMixedRealityHand;
  233.         IMixedRealityController mrcontroller = controller as IMixedRealityController;
  234.  
  235.         if (hand != null)
  236.         {
  237.             try
  238.             {
  239.                 detectedHand = hand;
  240.  
  241.                 if (controller.ControllerHandedness == Handedness.Left)
  242.                 {
  243.                     leftHand = hand;
  244.                 }
  245.                 else if (controller.ControllerHandedness == Handedness.Right)
  246.                 {
  247.                     rightHand = hand;
  248.                 }
  249.             }
  250.             catch (Exception)
  251.             {
  252.                 //Debug.Log("Missed a hand, retrying");
  253.             }
  254.         }
  255.         else if (mrcontroller != null)
  256.         {
  257.             try
  258.             {
  259.                 detectedController = mrcontroller;
  260.  
  261.                 if (controller.ControllerHandedness == Handedness.Left)
  262.                 {
  263.                     leftController = mrcontroller;
  264.                 }
  265.                 else if (controller.ControllerHandedness == Handedness.Right)
  266.                 {
  267.                     rightController = mrcontroller;
  268.                 }
  269.             }
  270.             catch (Exception)
  271.             {
  272.                 //Debug.Log("Missed a controller, retrying");
  273.             }
  274.         }
  275.     }
  276.  
  277.     public void OnSourceDetected(SourceStateEventData eventData)
  278.     {
  279.         SetController(eventData.Controller);
  280.  
  281.         if (eventData.Controller != null)
  282.         {
  283.             OnControllerFound.Invoke(eventData.Controller.ControllerHandedness);
  284.         }
  285.     }
  286.  
  287.     public void OnSourceLost(SourceStateEventData eventData)
  288.     {
  289.         if (eventData.Controller != null)
  290.         {
  291.             OnControllerLost.Invoke(eventData.Controller.ControllerHandedness);
  292.         }
  293.     }
  294.  
  295.     public void OnInputUp(InputEventData eventData)
  296.     {
  297.     }
  298.  
  299.     public void OnInputDown(InputEventData eventData)
  300.     {
  301.         try
  302.         {
  303.             foreach (var p in eventData.InputSource.Pointers)
  304.             {
  305.                 if (p.Result != null)
  306.                 {
  307.                     SetController(p.Controller);
  308.                 }
  309.             }
  310.         }
  311.         catch (Exception)
  312.         {
  313.         }
  314.     }
  315. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top