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.

102 lines
5.0 KiB

  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.Collections.Generic;
  5. using Oculus.Avatar;
  6. public class OvrAvatarLocalDriver : OvrAvatarDriver {
  7. Vector3 centerEyePosition = Vector3.zero;
  8. Quaternion centerEyeRotation = 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. // Platform and device agnostic calls to return center eye pose, used to pass in head pose to sdk
  52. OVRNodeStateProperties.GetNodeStatePropertyVector3(UnityEngine.XR.XRNode.CenterEye, NodeStatePropertyType.Position,
  53. OVRPlugin.Node.EyeCenter, OVRPlugin.Step.Render, out centerEyePosition);
  54. OVRNodeStateProperties.GetNodeStatePropertyQuaternion(UnityEngine.XR.XRNode.CenterEye, NodeStatePropertyType.Orientation,
  55. OVRPlugin.Node.EyeCenter, OVRPlugin.Step.Render, out centerEyeRotation);
  56. if (GetIsTrackedRemote())
  57. {
  58. CurrentPose = new PoseFrame
  59. {
  60. voiceAmplitude = voiceAmplitude,
  61. headPosition = centerEyePosition,
  62. headRotation = centerEyeRotation,
  63. handLeftPosition = OVRInput.GetLocalControllerPosition(OVRInput.Controller.LTrackedRemote),
  64. handLeftRotation = OVRInput.GetLocalControllerRotation(OVRInput.Controller.LTrackedRemote),
  65. handRightPosition = OVRInput.GetLocalControllerPosition(OVRInput.Controller.RTrackedRemote),
  66. handRightRotation = OVRInput.GetLocalControllerRotation(OVRInput.Controller.RTrackedRemote),
  67. controllerLeftPose = GetMalibuControllerPose(OVRInput.Controller.LTrackedRemote),
  68. controllerRightPose = GetMalibuControllerPose(OVRInput.Controller.RTrackedRemote),
  69. };
  70. }
  71. else
  72. {
  73. CurrentPose = new PoseFrame
  74. {
  75. voiceAmplitude = voiceAmplitude,
  76. headPosition = centerEyePosition,
  77. headRotation = centerEyeRotation,
  78. handLeftPosition = OVRInput.GetLocalControllerPosition(OVRInput.Controller.LTouch),
  79. handLeftRotation = OVRInput.GetLocalControllerRotation(OVRInput.Controller.LTouch),
  80. handRightPosition = OVRInput.GetLocalControllerPosition(OVRInput.Controller.RTouch),
  81. handRightRotation = OVRInput.GetLocalControllerRotation(OVRInput.Controller.RTouch),
  82. controllerLeftPose = GetControllerPose(OVRInput.Controller.LTouch),
  83. controllerRightPose = GetControllerPose(OVRInput.Controller.RTouch),
  84. };
  85. }
  86. }
  87. public override void UpdateTransforms(IntPtr sdkAvatar)
  88. {
  89. CalculateCurrentPose();
  90. UpdateTransformsFromPose(sdkAvatar);
  91. }
  92. }