OVRCommon.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /************************************************************************************
  2. Copyright : Copyright 2017 Oculus VR, LLC. All Rights reserved.
  3. Licensed under the Oculus VR Rift SDK License Version 3.4.1 (the "License");
  4. you may not use the Oculus VR Rift SDK except in compliance with the License,
  5. which is provided at the time of installation or download, or which
  6. otherwise accompanies this software in either electronic or hard copy form.
  7. You may obtain a copy of the License at
  8. https://developer.oculus.com/licenses/sdk-3.4.1
  9. Unless required by applicable law or agreed to in writing, the Oculus VR SDK
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ************************************************************************************/
  15. using UnityEngine;
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Runtime.InteropServices;
  19. /// <summary>
  20. /// Miscellaneous extension methods that any script can use.
  21. /// </summary>
  22. public static class OVRExtensions
  23. {
  24. /// <summary>
  25. /// Converts the given world-space transform to an OVRPose in tracking space.
  26. /// </summary>
  27. public static OVRPose ToTrackingSpacePose(this Transform transform, Camera camera)
  28. {
  29. OVRPose headPose;
  30. #if UNITY_2017_2_OR_NEWER
  31. headPose.position = UnityEngine.XR.InputTracking.GetLocalPosition(UnityEngine.XR.XRNode.Head);
  32. headPose.orientation = UnityEngine.XR.InputTracking.GetLocalRotation(UnityEngine.XR.XRNode.Head);
  33. #else
  34. headPose.position = UnityEngine.VR.InputTracking.GetLocalPosition(UnityEngine.VR.VRNode.Head);
  35. headPose.orientation = UnityEngine.VR.InputTracking.GetLocalRotation(UnityEngine.VR.VRNode.Head);
  36. #endif
  37. var ret = headPose * transform.ToHeadSpacePose(camera);
  38. return ret;
  39. }
  40. /// <summary>
  41. /// Converts the given pose from tracking-space to world-space.
  42. /// </summary>
  43. public static OVRPose ToWorldSpacePose(OVRPose trackingSpacePose)
  44. {
  45. OVRPose headPose;
  46. #if UNITY_2017_2_OR_NEWER
  47. headPose.position = UnityEngine.XR.InputTracking.GetLocalPosition(UnityEngine.XR.XRNode.Head);
  48. headPose.orientation = UnityEngine.XR.InputTracking.GetLocalRotation(UnityEngine.XR.XRNode.Head);
  49. #else
  50. headPose.position = UnityEngine.VR.InputTracking.GetLocalPosition(UnityEngine.VR.VRNode.Head);
  51. headPose.orientation = UnityEngine.VR.InputTracking.GetLocalRotation(UnityEngine.VR.VRNode.Head);
  52. #endif
  53. // Transform from tracking-Space to head-Space
  54. OVRPose poseInHeadSpace = headPose.Inverse() * trackingSpacePose;
  55. // Transform from head space to world space
  56. OVRPose ret = Camera.main.transform.ToOVRPose() * poseInHeadSpace;
  57. return ret;
  58. }
  59. /// <summary>
  60. /// Converts the given world-space transform to an OVRPose in head space.
  61. /// </summary>
  62. public static OVRPose ToHeadSpacePose(this Transform transform, Camera camera)
  63. {
  64. return camera.transform.ToOVRPose().Inverse() * transform.ToOVRPose();
  65. }
  66. internal static OVRPose ToOVRPose(this Transform t, bool isLocal = false)
  67. {
  68. OVRPose pose;
  69. pose.orientation = (isLocal) ? t.localRotation : t.rotation;
  70. pose.position = (isLocal) ? t.localPosition : t.position;
  71. return pose;
  72. }
  73. internal static void FromOVRPose(this Transform t, OVRPose pose, bool isLocal = false)
  74. {
  75. if (isLocal)
  76. {
  77. t.localRotation = pose.orientation;
  78. t.localPosition = pose.position;
  79. }
  80. else
  81. {
  82. t.rotation = pose.orientation;
  83. t.position = pose.position;
  84. }
  85. }
  86. internal static OVRPose ToOVRPose(this OVRPlugin.Posef p)
  87. {
  88. return new OVRPose()
  89. {
  90. position = new Vector3(p.Position.x, p.Position.y, -p.Position.z),
  91. orientation = new Quaternion(-p.Orientation.x, -p.Orientation.y, p.Orientation.z, p.Orientation.w)
  92. };
  93. }
  94. internal static OVRTracker.Frustum ToFrustum(this OVRPlugin.Frustumf f)
  95. {
  96. return new OVRTracker.Frustum()
  97. {
  98. nearZ = f.zNear,
  99. farZ = f.zFar,
  100. fov = new Vector2()
  101. {
  102. x = Mathf.Rad2Deg * f.fovX,
  103. y = Mathf.Rad2Deg * f.fovY
  104. }
  105. };
  106. }
  107. internal static Color FromColorf(this OVRPlugin.Colorf c)
  108. {
  109. return new Color() { r = c.r, g = c.g, b = c.b, a = c.a };
  110. }
  111. internal static OVRPlugin.Colorf ToColorf(this Color c)
  112. {
  113. return new OVRPlugin.Colorf() { r = c.r, g = c.g, b = c.b, a = c.a };
  114. }
  115. internal static Vector3 FromVector3f(this OVRPlugin.Vector3f v)
  116. {
  117. return new Vector3() { x = v.x, y = v.y, z = v.z };
  118. }
  119. internal static Vector3 FromFlippedZVector3f(this OVRPlugin.Vector3f v)
  120. {
  121. return new Vector3() { x = v.x, y = v.y, z = -v.z };
  122. }
  123. internal static OVRPlugin.Vector3f ToVector3f(this Vector3 v)
  124. {
  125. return new OVRPlugin.Vector3f() { x = v.x, y = v.y, z = v.z };
  126. }
  127. internal static OVRPlugin.Vector3f ToFlippedZVector3f(this Vector3 v)
  128. {
  129. return new OVRPlugin.Vector3f() { x = v.x, y = v.y, z = -v.z };
  130. }
  131. internal static Quaternion FromQuatf(this OVRPlugin.Quatf q)
  132. {
  133. return new Quaternion() { x = q.x, y = q.y, z = q.z, w = q.w };
  134. }
  135. internal static Quaternion FromFlippedZQuatf(this OVRPlugin.Quatf q)
  136. {
  137. return new Quaternion() { x = -q.x, y = -q.y, z = q.z, w = q.w };
  138. }
  139. internal static OVRPlugin.Quatf ToQuatf(this Quaternion q)
  140. {
  141. return new OVRPlugin.Quatf() { x = q.x, y = q.y, z = q.z, w = q.w };
  142. }
  143. internal static OVRPlugin.Quatf ToFlippedZQuatf(this Quaternion q)
  144. {
  145. return new OVRPlugin.Quatf() { x = -q.x, y = -q.y, z = q.z, w = q.w };
  146. }
  147. }
  148. /// <summary>
  149. /// An affine transformation built from a Unity position and orientation.
  150. /// </summary>
  151. [System.Serializable]
  152. public struct OVRPose
  153. {
  154. /// <summary>
  155. /// A pose with no translation or rotation.
  156. /// </summary>
  157. public static OVRPose identity
  158. {
  159. get {
  160. return new OVRPose()
  161. {
  162. position = Vector3.zero,
  163. orientation = Quaternion.identity
  164. };
  165. }
  166. }
  167. public override bool Equals(System.Object obj)
  168. {
  169. return obj is OVRPose && this == (OVRPose)obj;
  170. }
  171. public override int GetHashCode()
  172. {
  173. return position.GetHashCode() ^ orientation.GetHashCode();
  174. }
  175. public static bool operator ==(OVRPose x, OVRPose y)
  176. {
  177. return x.position == y.position && x.orientation == y.orientation;
  178. }
  179. public static bool operator !=(OVRPose x, OVRPose y)
  180. {
  181. return !(x == y);
  182. }
  183. /// <summary>
  184. /// The position.
  185. /// </summary>
  186. public Vector3 position;
  187. /// <summary>
  188. /// The orientation.
  189. /// </summary>
  190. public Quaternion orientation;
  191. /// <summary>
  192. /// Multiplies two poses.
  193. /// </summary>
  194. public static OVRPose operator*(OVRPose lhs, OVRPose rhs)
  195. {
  196. var ret = new OVRPose();
  197. ret.position = lhs.position + lhs.orientation * rhs.position;
  198. ret.orientation = lhs.orientation * rhs.orientation;
  199. return ret;
  200. }
  201. /// <summary>
  202. /// Computes the inverse of the given pose.
  203. /// </summary>
  204. public OVRPose Inverse()
  205. {
  206. OVRPose ret;
  207. ret.orientation = Quaternion.Inverse(orientation);
  208. ret.position = ret.orientation * -position;
  209. return ret;
  210. }
  211. /// <summary>
  212. /// Converts the pose from left- to right-handed or vice-versa.
  213. /// </summary>
  214. internal OVRPose flipZ()
  215. {
  216. var ret = this;
  217. ret.position.z = -ret.position.z;
  218. ret.orientation.z = -ret.orientation.z;
  219. ret.orientation.w = -ret.orientation.w;
  220. return ret;
  221. }
  222. internal OVRPlugin.Posef ToPosef()
  223. {
  224. return new OVRPlugin.Posef()
  225. {
  226. Position = position.ToVector3f(),
  227. Orientation = orientation.ToQuatf()
  228. };
  229. }
  230. }
  231. /// <summary>
  232. /// Encapsulates an 8-byte-aligned of unmanaged memory.
  233. /// </summary>
  234. public class OVRNativeBuffer : IDisposable
  235. {
  236. private bool disposed = false;
  237. private int m_numBytes = 0;
  238. private IntPtr m_ptr = IntPtr.Zero;
  239. /// <summary>
  240. /// Creates a buffer of the specified size.
  241. /// </summary>
  242. public OVRNativeBuffer(int numBytes)
  243. {
  244. Reallocate(numBytes);
  245. }
  246. /// <summary>
  247. /// Releases unmanaged resources and performs other cleanup operations before the <see cref="OVRNativeBuffer"/> is
  248. /// reclaimed by garbage collection.
  249. /// </summary>
  250. ~OVRNativeBuffer()
  251. {
  252. Dispose(false);
  253. }
  254. /// <summary>
  255. /// Reallocates the buffer with the specified new size.
  256. /// </summary>
  257. public void Reset(int numBytes)
  258. {
  259. Reallocate(numBytes);
  260. }
  261. /// <summary>
  262. /// The current number of bytes in the buffer.
  263. /// </summary>
  264. public int GetCapacity()
  265. {
  266. return m_numBytes;
  267. }
  268. /// <summary>
  269. /// A pointer to the unmanaged memory in the buffer, starting at the given offset in bytes.
  270. /// </summary>
  271. public IntPtr GetPointer(int byteOffset = 0)
  272. {
  273. if (byteOffset < 0 || byteOffset >= m_numBytes)
  274. return IntPtr.Zero;
  275. return (byteOffset == 0) ? m_ptr : new IntPtr(m_ptr.ToInt64() + byteOffset);
  276. }
  277. /// <summary>
  278. /// Releases all resource used by the <see cref="OVRNativeBuffer"/> object.
  279. /// </summary>
  280. /// <remarks>Call <see cref="Dispose"/> when you are finished using the <see cref="OVRNativeBuffer"/>. The <see cref="Dispose"/>
  281. /// method leaves the <see cref="OVRNativeBuffer"/> in an unusable state. After calling <see cref="Dispose"/>, you must
  282. /// release all references to the <see cref="OVRNativeBuffer"/> so the garbage collector can reclaim the memory that
  283. /// the <see cref="OVRNativeBuffer"/> was occupying.</remarks>
  284. public void Dispose()
  285. {
  286. Dispose(true);
  287. GC.SuppressFinalize(this);
  288. }
  289. private void Dispose(bool disposing)
  290. {
  291. if (disposed)
  292. return;
  293. if (disposing)
  294. {
  295. // dispose managed resources
  296. }
  297. // dispose unmanaged resources
  298. Release();
  299. disposed = true;
  300. }
  301. private void Reallocate(int numBytes)
  302. {
  303. Release();
  304. if (numBytes > 0)
  305. {
  306. m_ptr = Marshal.AllocHGlobal(numBytes);
  307. m_numBytes = numBytes;
  308. }
  309. else
  310. {
  311. m_ptr = IntPtr.Zero;
  312. m_numBytes = 0;
  313. }
  314. }
  315. private void Release()
  316. {
  317. if (m_ptr != IntPtr.Zero)
  318. {
  319. Marshal.FreeHGlobal(m_ptr);
  320. m_ptr = IntPtr.Zero;
  321. m_numBytes = 0;
  322. }
  323. }
  324. }