Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.6 KiB

  1. #if VRTK_DEFINE_SDK_DAYDREAM
  2. namespace VRTK
  3. {
  4. using UnityEngine;
  5. /// <summary>
  6. /// DaydreamReach component uses touchpad to extend controller position out in front of user
  7. /// along the current controller orientation plane
  8. /// </summary>
  9. public class DaydreamReach : MonoBehaviour
  10. {
  11. [Tooltip("Controller to track, defaults to ./Controller but probably want a Pointer Joint like GvrControllerPointer/Laser")]
  12. public Transform controller;
  13. [Tooltip("Maximum reach distance from controller origin.")]
  14. public float reachDistance = 0.5f;
  15. private Vector3 positionOrigin;
  16. protected virtual void Start()
  17. {
  18. if (controller == null)
  19. {
  20. controller = transform.Find("Controller");
  21. }
  22. positionOrigin = transform.position;
  23. }
  24. protected virtual void Update()
  25. {
  26. if (!GvrController.IsTouching)
  27. {
  28. // snap back to origin
  29. transform.position = positionOrigin;
  30. }
  31. else
  32. {
  33. transform.position = ReachForwardByDistance();
  34. }
  35. }
  36. private Vector3 ReachForwardByDistance()
  37. {
  38. Vector3 pos = positionOrigin;
  39. Vector3 offset = Vector3.zero;
  40. Vector2 touch = GetTouch(5);
  41. offset.z = touch.y * reachDistance;
  42. offset = controller.transform.rotation * offset;
  43. pos += offset;
  44. return pos;
  45. }
  46. /// <summary>
  47. /// GetTouch gets touch position adjusted coordinates. Abs(x) and abs(y) always 0 to 1
  48. /// </summary>
  49. /// <param name="origin">like a numeric keypad, 1= lower left, 2=lower-center, 5=center etc</param>
  50. /// <returns>A Vector2 of the touch position.</returns>
  51. private Vector2 GetTouch(int origin = 5)
  52. {
  53. if (!GvrController.IsTouching)
  54. {
  55. return Vector2.zero;
  56. }
  57. Vector2 touch = GvrController.TouchPos;
  58. // raw: 0,0 is top left, x is left-right, y is front-back
  59. // invert y axis
  60. touch.y = 1.0f - touch.y;
  61. switch (origin)
  62. {
  63. case 1:
  64. break;
  65. case 2:
  66. touch.x = touch.x * 2f - 1f;
  67. break;
  68. case 5:
  69. touch.y = touch.y * 2f - 1f;
  70. touch.x = touch.x * 2f - 1f;
  71. break;
  72. }
  73. return touch;
  74. }
  75. }
  76. }
  77. #endif