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.

96 lines
4.5 KiB

  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.Collections.Generic;
  5. using Oculus.Avatar;
  6. public class OvrAvatarTestDriver : OvrAvatarDriver {
  7. private Vector3 headPos = new Vector3(0f, 1.6f, 0f);
  8. private Quaternion headRot = Quaternion.identity;
  9. ControllerPose GetMalibuControllerPose(OVRInput.Controller controller)
  10. {
  11. ovrAvatarButton buttons = 0;
  12. if (OVRInput.Get(OVRInput.Button.PrimaryIndexTrigger, controller)) buttons |= ovrAvatarButton.One;
  13. return new ControllerPose
  14. {
  15. buttons = buttons,
  16. touches = OVRInput.Get(OVRInput.Touch.PrimaryTouchpad) ? ovrAvatarTouch.One : 0,
  17. joystickPosition = OVRInput.Get(OVRInput.Axis2D.PrimaryTouchpad, controller),
  18. indexTrigger = 0f,
  19. handTrigger = 0f,
  20. isActive = (OVRInput.GetActiveController() & controller) != 0,
  21. };
  22. }
  23. float voiceAmplitude = 0.0f;
  24. ControllerPose GetControllerPose(OVRInput.Controller controller)
  25. {
  26. ovrAvatarButton buttons = 0;
  27. if (OVRInput.Get(OVRInput.Button.One, controller)) buttons |= ovrAvatarButton.One;
  28. if (OVRInput.Get(OVRInput.Button.Two, controller)) buttons |= ovrAvatarButton.Two;
  29. if (OVRInput.Get(OVRInput.Button.Start, controller)) buttons |= ovrAvatarButton.Three;
  30. if (OVRInput.Get(OVRInput.Button.PrimaryThumbstick, controller)) buttons |= ovrAvatarButton.Joystick;
  31. ovrAvatarTouch touches = 0;
  32. if (OVRInput.Get(OVRInput.Touch.One, controller)) touches |= ovrAvatarTouch.One;
  33. if (OVRInput.Get(OVRInput.Touch.Two, controller)) touches |= ovrAvatarTouch.Two;
  34. if (OVRInput.Get(OVRInput.Touch.PrimaryThumbstick, controller)) touches |= ovrAvatarTouch.Joystick;
  35. if (OVRInput.Get(OVRInput.Touch.PrimaryThumbRest, controller)) touches |= ovrAvatarTouch.ThumbRest;
  36. if (OVRInput.Get(OVRInput.Touch.PrimaryIndexTrigger, controller)) touches |= ovrAvatarTouch.Index;
  37. if (!OVRInput.Get(OVRInput.NearTouch.PrimaryIndexTrigger, controller)) touches |= ovrAvatarTouch.Pointing;
  38. if (!OVRInput.Get(OVRInput.NearTouch.PrimaryThumbButtons, controller)) touches |= ovrAvatarTouch.ThumbUp;
  39. return new ControllerPose
  40. {
  41. buttons = buttons,
  42. touches = touches,
  43. joystickPosition = OVRInput.Get(OVRInput.Axis2D.PrimaryThumbstick, controller),
  44. indexTrigger = OVRInput.Get(OVRInput.Axis1D.PrimaryIndexTrigger, controller),
  45. handTrigger = OVRInput.Get(OVRInput.Axis1D.PrimaryHandTrigger, controller),
  46. isActive = (OVRInput.GetActiveController() & controller) != 0,
  47. };
  48. }
  49. private void CalculateCurrentPose()
  50. {
  51. if (GetIsTrackedRemote())
  52. {
  53. CurrentPose = new PoseFrame
  54. {
  55. voiceAmplitude = voiceAmplitude,
  56. headPosition = headPos,
  57. headRotation = headRot,
  58. handLeftPosition = OVRInput.GetLocalControllerPosition(OVRInput.Controller.LTrackedRemote),
  59. handLeftRotation = OVRInput.GetLocalControllerRotation(OVRInput.Controller.LTrackedRemote),
  60. handRightPosition = OVRInput.GetLocalControllerPosition(OVRInput.Controller.RTrackedRemote),
  61. handRightRotation = OVRInput.GetLocalControllerRotation(OVRInput.Controller.RTrackedRemote),
  62. controllerLeftPose = GetMalibuControllerPose(OVRInput.Controller.LTrackedRemote),
  63. controllerRightPose = GetMalibuControllerPose(OVRInput.Controller.RTrackedRemote),
  64. };
  65. }
  66. else
  67. {
  68. CurrentPose = new PoseFrame
  69. {
  70. voiceAmplitude = voiceAmplitude,
  71. headPosition = headPos,
  72. headRotation = headRot,
  73. handLeftPosition = OVRInput.GetLocalControllerPosition(OVRInput.Controller.LTouch),
  74. handLeftRotation = OVRInput.GetLocalControllerRotation(OVRInput.Controller.LTouch),
  75. handRightPosition = OVRInput.GetLocalControllerPosition(OVRInput.Controller.RTouch),
  76. handRightRotation = OVRInput.GetLocalControllerRotation(OVRInput.Controller.RTouch),
  77. controllerLeftPose = GetControllerPose(OVRInput.Controller.LTouch),
  78. controllerRightPose = GetControllerPose(OVRInput.Controller.RTouch),
  79. };
  80. }
  81. }
  82. public override void UpdateTransforms(IntPtr sdkAvatar)
  83. {
  84. CalculateCurrentPose();
  85. UpdateTransformsFromPose(sdkAvatar);
  86. }
  87. }