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.

119 lines
4.9 KiB

  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using Oculus.Avatar;
  5. public abstract class OvrAvatarDriver : MonoBehaviour {
  6. public enum PacketMode
  7. {
  8. SDK,
  9. Unity
  10. };
  11. public PacketMode Mode;
  12. protected PoseFrame CurrentPose;
  13. public PoseFrame GetCurrentPose() { return CurrentPose; }
  14. public abstract void UpdateTransforms(IntPtr sdkAvatar);
  15. private ovrAvatarControllerType ControllerType = ovrAvatarControllerType.Quest;
  16. public struct ControllerPose
  17. {
  18. public ovrAvatarButton buttons;
  19. public ovrAvatarTouch touches;
  20. public Vector2 joystickPosition;
  21. public float indexTrigger;
  22. public float handTrigger;
  23. public bool isActive;
  24. public static ControllerPose Interpolate(ControllerPose a, ControllerPose b, float t)
  25. {
  26. return new ControllerPose
  27. {
  28. buttons = t < 0.5f ? a.buttons : b.buttons,
  29. touches = t < 0.5f ? a.touches : b.touches,
  30. joystickPosition = Vector2.Lerp(a.joystickPosition, b.joystickPosition, t),
  31. indexTrigger = Mathf.Lerp(a.indexTrigger, b.indexTrigger, t),
  32. handTrigger = Mathf.Lerp(a.handTrigger, b.handTrigger, t),
  33. isActive = t < 0.5f ? a.isActive : b.isActive,
  34. };
  35. }
  36. }
  37. public struct PoseFrame
  38. {
  39. public Vector3 headPosition;
  40. public Quaternion headRotation;
  41. public Vector3 handLeftPosition;
  42. public Quaternion handLeftRotation;
  43. public Vector3 handRightPosition;
  44. public Quaternion handRightRotation;
  45. public float voiceAmplitude;
  46. public ControllerPose controllerLeftPose;
  47. public ControllerPose controllerRightPose;
  48. public static PoseFrame Interpolate(PoseFrame a, PoseFrame b, float t)
  49. {
  50. return new PoseFrame
  51. {
  52. headPosition = Vector3.Lerp(a.headPosition, b.headPosition, t),
  53. headRotation = Quaternion.Slerp(a.headRotation, b.headRotation, t),
  54. handLeftPosition = Vector3.Lerp(a.handLeftPosition, b.handLeftPosition, t),
  55. handLeftRotation = Quaternion.Slerp(a.handLeftRotation, b.handLeftRotation, t),
  56. handRightPosition = Vector3.Lerp(a.handRightPosition, b.handRightPosition, t),
  57. handRightRotation = Quaternion.Slerp(a.handRightRotation, b.handRightRotation, t),
  58. voiceAmplitude = Mathf.Lerp(a.voiceAmplitude, b.voiceAmplitude, t),
  59. controllerLeftPose = ControllerPose.Interpolate(a.controllerLeftPose, b.controllerLeftPose, t),
  60. controllerRightPose = ControllerPose.Interpolate(a.controllerRightPose, b.controllerRightPose, t),
  61. };
  62. }
  63. };
  64. void Start()
  65. {
  66. var headsetType = OVRPlugin.GetSystemHeadsetType();
  67. switch (headsetType)
  68. {
  69. case OVRPlugin.SystemHeadset.GearVR_R320:
  70. case OVRPlugin.SystemHeadset.GearVR_R321:
  71. case OVRPlugin.SystemHeadset.GearVR_R322:
  72. case OVRPlugin.SystemHeadset.GearVR_R323:
  73. case OVRPlugin.SystemHeadset.GearVR_R324:
  74. case OVRPlugin.SystemHeadset.GearVR_R325:
  75. ControllerType = ovrAvatarControllerType.Malibu;
  76. break;
  77. case OVRPlugin.SystemHeadset.Oculus_Go:
  78. ControllerType = ovrAvatarControllerType.Go;
  79. break;
  80. case OVRPlugin.SystemHeadset.Oculus_Quest:
  81. case OVRPlugin.SystemHeadset.Rift_S:
  82. ControllerType = ovrAvatarControllerType.Quest;
  83. break;
  84. case OVRPlugin.SystemHeadset.Rift_DK1:
  85. case OVRPlugin.SystemHeadset.Rift_DK2:
  86. case OVRPlugin.SystemHeadset.Rift_CV1:
  87. default:
  88. ControllerType = ovrAvatarControllerType.Touch;
  89. break;
  90. }
  91. }
  92. public void UpdateTransformsFromPose(IntPtr sdkAvatar)
  93. {
  94. if (sdkAvatar != IntPtr.Zero)
  95. {
  96. ovrAvatarTransform bodyTransform = OvrAvatar.CreateOvrAvatarTransform(CurrentPose.headPosition, CurrentPose.headRotation);
  97. ovrAvatarHandInputState inputStateLeft = OvrAvatar.CreateInputState(OvrAvatar.CreateOvrAvatarTransform(CurrentPose.handLeftPosition, CurrentPose.handLeftRotation), CurrentPose.controllerLeftPose);
  98. ovrAvatarHandInputState inputStateRight = OvrAvatar.CreateInputState(OvrAvatar.CreateOvrAvatarTransform(CurrentPose.handRightPosition, CurrentPose.handRightRotation), CurrentPose.controllerRightPose);
  99. CAPI.ovrAvatarPose_UpdateBody(sdkAvatar, bodyTransform);
  100. CAPI.ovrAvatarPose_UpdateHandsWithType(sdkAvatar, inputStateLeft, inputStateRight, ControllerType);
  101. }
  102. }
  103. public static bool GetIsTrackedRemote()
  104. {
  105. return OVRInput.IsControllerConnected(OVRInput.Controller.RTrackedRemote) || OVRInput.IsControllerConnected(OVRInput.Controller.LTrackedRemote);
  106. }
  107. }