OVRExtensions.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.VR;
  4. public static class OVRExtensions
  5. {
  6. public static OVRPose ToTrackingSpacePose(this Transform transform)
  7. {
  8. OVRPose lhs;
  9. lhs.position = InputTracking.GetLocalPosition(VRNode.Head);
  10. lhs.orientation = InputTracking.GetLocalRotation(VRNode.Head);
  11. return lhs * transform.ToHeadSpacePose();
  12. }
  13. public static OVRPose ToHeadSpacePose(this Transform transform)
  14. {
  15. return Camera.current.transform.ToOVRPose(false).Inverse() * transform.ToOVRPose(false);
  16. }
  17. internal static OVRPose ToOVRPose(this Transform t, bool isLocal = false)
  18. {
  19. OVRPose result;
  20. result.orientation = ((!isLocal) ? t.rotation : t.localRotation);
  21. result.position = ((!isLocal) ? t.position : t.localPosition);
  22. return result;
  23. }
  24. internal static void FromOVRPose(this Transform t, OVRPose pose, bool isLocal = false)
  25. {
  26. if (isLocal)
  27. {
  28. t.localRotation = pose.orientation;
  29. t.localPosition = pose.position;
  30. }
  31. else
  32. {
  33. t.rotation = pose.orientation;
  34. t.position = pose.position;
  35. }
  36. }
  37. internal static OVRPose ToOVRPose(this OVRPlugin.Posef p)
  38. {
  39. return new OVRPose
  40. {
  41. position = new Vector3(p.Position.x, p.Position.y, -p.Position.z),
  42. orientation = new Quaternion(-p.Orientation.x, -p.Orientation.y, p.Orientation.z, p.Orientation.w)
  43. };
  44. }
  45. internal static OVRTracker.Frustum ToFrustum(this OVRPlugin.Frustumf f)
  46. {
  47. return new OVRTracker.Frustum
  48. {
  49. nearZ = f.zNear,
  50. farZ = f.zFar,
  51. fov = new Vector2
  52. {
  53. x = 57.29578f * f.fovX,
  54. y = 57.29578f * f.fovY
  55. }
  56. };
  57. }
  58. internal static Color FromColorf(this OVRPlugin.Colorf c)
  59. {
  60. return new Color
  61. {
  62. r = c.r,
  63. g = c.g,
  64. b = c.b,
  65. a = c.a
  66. };
  67. }
  68. internal static OVRPlugin.Colorf ToColorf(this Color c)
  69. {
  70. return new OVRPlugin.Colorf
  71. {
  72. r = c.r,
  73. g = c.g,
  74. b = c.b,
  75. a = c.a
  76. };
  77. }
  78. internal static Vector3 FromVector3f(this OVRPlugin.Vector3f v)
  79. {
  80. return new Vector3
  81. {
  82. x = v.x,
  83. y = v.y,
  84. z = v.z
  85. };
  86. }
  87. internal static Vector3 FromFlippedZVector3f(this OVRPlugin.Vector3f v)
  88. {
  89. return new Vector3
  90. {
  91. x = v.x,
  92. y = v.y,
  93. z = -v.z
  94. };
  95. }
  96. internal static OVRPlugin.Vector3f ToVector3f(this Vector3 v)
  97. {
  98. return new OVRPlugin.Vector3f
  99. {
  100. x = v.x,
  101. y = v.y,
  102. z = v.z
  103. };
  104. }
  105. internal static OVRPlugin.Vector3f ToFlippedZVector3f(this Vector3 v)
  106. {
  107. return new OVRPlugin.Vector3f
  108. {
  109. x = v.x,
  110. y = v.y,
  111. z = -v.z
  112. };
  113. }
  114. internal static Quaternion FromQuatf(this OVRPlugin.Quatf q)
  115. {
  116. return new Quaternion
  117. {
  118. x = q.x,
  119. y = q.y,
  120. z = q.z,
  121. w = q.w
  122. };
  123. }
  124. internal static OVRPlugin.Quatf ToQuatf(this Quaternion q)
  125. {
  126. return new OVRPlugin.Quatf
  127. {
  128. x = q.x,
  129. y = q.y,
  130. z = q.z,
  131. w = q.w
  132. };
  133. }
  134. }