Author Topic: Custom touch input for VR  (Read 146 times)

batman2142

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Custom touch input for VR
« on: November 24, 2017, 03:31:34 AM »
I am trying to add support for VR controllers (using VRTK) by making a script similar to the TouchScript bridge script that you provided in this thread here: http://www.tasharen.com/forum/index.php?topic=4984.msg56678

My code:
  1.  
  2.     void OnPointerActivated(object sender, ControllerInteractionEventArgs e)
  3.     {
  4.         // Start tracking touches
  5.         UICamera.GetInputTouchCount = OnGetTouchCount;
  6.         UICamera.GetInputTouch = OnGetTouch;
  7.  
  8.         mTouches.Add(new UICamera.Touch()
  9.         {
  10.             phase = TouchPhase.Began,
  11.             fingerId = 0,
  12.             position = HoverPosition,
  13.             tapCount = 1
  14.         });
  15.     }
  16.  
  17.     void OnPointerHover(object sender, DestinationMarkerEventArgs e)
  18.     {
  19.         for (int index = 0; index < mTouches.size; ++index)
  20.         {
  21.             UICamera.Touch t = mTouches[index];
  22.  
  23.             if (t.fingerId == 0)
  24.             {
  25.                 var pointInScreenSpace = VRTK_DeviceFinder.HeadsetCamera().GetComponent<Camera>().WorldToScreenPoint(e.destinationPosition);
  26.                 t.position = new Vector2(pointInScreenSpace.x, pointInScreenSpace.y);
  27.                 t.phase = TouchPhase.Moved;
  28.                 HoverPosition = t.position;
  29.                 break;
  30.             }
  31.         }
  32.     }
  33.  
  34.     void OnPointerClicked()
  35.     {
  36.          for (int index = 0; index < mTouches.size; ++index)
  37.         {
  38.             UICamera.Touch t = mTouches[index];
  39.  
  40.             if (t.fingerId == 0)
  41.             {
  42.                 t.phase = TouchPhase.Ended;
  43.                 t.position = HoverPosition;
  44.                 break;
  45.             }
  46.         }
  47.     }
  48.  
  49.     void OnPointerDeactivated(object sender, ControllerInteractionEventArgs e)
  50.     {
  51.         // Stop tracking touches
  52.         mTouches.Clear();
  53.         UICamera.GetInputTouchCount = null;
  54.         UICamera.GetInputTouch = null;
  55.     }
  56.  
  57.     int OnGetTouchCount()
  58.     {
  59.         return mTouches.size;
  60.     }
  61.     UICamera.Touch OnGetTouch(int index)
  62.     {
  63.         return mTouches[index];
  64.     }
  65.  
  66.     void LateUpdate()
  67.     {
  68.         int index = 0;
  69.  
  70.         while (index < mTouches.size)
  71.         {
  72.             UICamera.Touch touch = mTouches[index];
  73.  
  74.             if (touch.phase == TouchPhase.Ended)
  75.             {
  76.                 mTouches.RemoveAt(index);
  77.             }
  78.             else
  79.             {
  80.                 touch.phase = TouchPhase.Moved;
  81.                 ++index;
  82.             }
  83.         }
  84.     }
  85.  

1. I have the touchPosition, that is a Camera.WorldToScreenPoint, and use it as touch.position when adding touches using UICamera.GetInputTouch and UICamera.GetInputTouchCount callbacks.
Could you tell me if there is something wrong in how I am adding the Touch events to mTouches and the reason why UICamera is not processing a touch once my TouchEnded touch has been added? Notice that I have changed fingerID of the touches to 0, since there is only 1 pointer I am using for now, would this be an issue?

2. I also tried making my own UICamera and replaced Input.mousePosition with HoverPosition and that did not work either.

3. There is OnCustomInput in UICamera but I could not find any resources on how to use it, could you please tell me how to go about it?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,137
  • Toronto, Canada
    • View Profile
Re: Custom touch input for VR
« Reply #1 on: November 29, 2017, 08:39:23 AM »
UICamera.onCustomInput is just a delegate that will be called by UICamera when processing events. What you do inside the function is up to you, but you will ultimately need to call UICamera's Notify function to trigger actual events. You can use UICamera.ProcessOthers as an example.

Looking over your code I'm not sure what "HoverPosition" is or where it's coming from that you assign your original touch position as. I also think that the tap count should be 0, not 1. You haven't actually tapped anything yet at that point, merely pressed on something.

You don't seem to assign UICamera.GetTouch anywhere, and it's needed to actually retrieve your touch info. You have the function, you just don't assign it as a UICamera.GetTouch delegate anywhere.