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.

694 lines
18 KiB

  1. /************************************************************************************
  2. Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
  3. Licensed under the Oculus Utilities SDK License Version 1.31 (the "License"); you may not use
  4. the Utilities SDK except in compliance with the License, which is provided at the time of installation
  5. or download, or which otherwise accompanies this software in either electronic or hard copy form.
  6. You may obtain a copy of the License at
  7. https://developer.oculus.com/licenses/utilities-1.31
  8. Unless required by applicable law or agreed to in writing, the Utilities SDK distributed
  9. under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  10. ANY KIND, either express or implied. See the License for the specific language governing
  11. permissions and limitations under the License.
  12. ************************************************************************************/
  13. #if USING_XR_MANAGEMENT && USING_XR_SDK_OCULUS
  14. #define USING_XR_SDK
  15. #endif
  16. using UnityEngine;
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Runtime.InteropServices;
  20. #if USING_XR_SDK
  21. using UnityEngine.XR;
  22. using UnityEngine.Experimental.XR;
  23. #endif
  24. #if UNITY_2017_2_OR_NEWER
  25. using InputTracking = UnityEngine.XR.InputTracking;
  26. using Node = UnityEngine.XR.XRNode;
  27. using NodeState = UnityEngine.XR.XRNodeState;
  28. using Device = UnityEngine.XR.XRDevice;
  29. #elif UNITY_2017_1_OR_NEWER
  30. using InputTracking = UnityEngine.VR.InputTracking;
  31. using Node = UnityEngine.VR.VRNode;
  32. using NodeState = UnityEngine.VR.VRNodeState;
  33. using Device = UnityEngine.VR.VRDevice;
  34. #else
  35. using InputTracking = UnityEngine.VR.InputTracking;
  36. using Node = UnityEngine.VR.VRNode;
  37. using Device = UnityEngine.VR.VRDevice;
  38. #endif
  39. /// <summary>
  40. /// Miscellaneous extension methods that any script can use.
  41. /// </summary>
  42. public static class OVRExtensions
  43. {
  44. /// <summary>
  45. /// Converts the given world-space transform to an OVRPose in tracking space.
  46. /// </summary>
  47. public static OVRPose ToTrackingSpacePose(this Transform transform, Camera camera)
  48. {
  49. //Initializing to identity, but for all Oculus headsets, down below the pose will be initialized to the runtime's pose value, so identity will never be returned.
  50. OVRPose headPose = OVRPose.identity;
  51. Vector3 pos;
  52. Quaternion rot;
  53. if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.Head, NodeStatePropertyType.Position, OVRPlugin.Node.Head, OVRPlugin.Step.Render, out pos))
  54. headPose.position = pos;
  55. if (OVRNodeStateProperties.GetNodeStatePropertyQuaternion(Node.Head, NodeStatePropertyType.Orientation, OVRPlugin.Node.Head, OVRPlugin.Step.Render, out rot))
  56. headPose.orientation = rot;
  57. var ret = headPose * transform.ToHeadSpacePose(camera);
  58. return ret;
  59. }
  60. /// <summary>
  61. /// Converts the given pose from tracking-space to world-space.
  62. /// </summary>
  63. public static OVRPose ToWorldSpacePose(OVRPose trackingSpacePose)
  64. {
  65. OVRPose headPose = OVRPose.identity;
  66. Vector3 pos;
  67. Quaternion rot;
  68. if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.Head, NodeStatePropertyType.Position, OVRPlugin.Node.Head, OVRPlugin.Step.Render, out pos))
  69. headPose.position = pos;
  70. if (OVRNodeStateProperties.GetNodeStatePropertyQuaternion(Node.Head, NodeStatePropertyType.Orientation, OVRPlugin.Node.Head, OVRPlugin.Step.Render, out rot))
  71. headPose.orientation = rot;
  72. // Transform from tracking-Space to head-Space
  73. OVRPose poseInHeadSpace = headPose.Inverse() * trackingSpacePose;
  74. // Transform from head space to world space
  75. OVRPose ret = Camera.main.transform.ToOVRPose() * poseInHeadSpace;
  76. return ret;
  77. }
  78. /// <summary>
  79. /// Converts the given world-space transform to an OVRPose in head space.
  80. /// </summary>
  81. public static OVRPose ToHeadSpacePose(this Transform transform, Camera camera)
  82. {
  83. return camera.transform.ToOVRPose().Inverse() * transform.ToOVRPose();
  84. }
  85. public static OVRPose ToOVRPose(this Transform t, bool isLocal = false)
  86. {
  87. OVRPose pose;
  88. pose.orientation = (isLocal) ? t.localRotation : t.rotation;
  89. pose.position = (isLocal) ? t.localPosition : t.position;
  90. return pose;
  91. }
  92. public static void FromOVRPose(this Transform t, OVRPose pose, bool isLocal = false)
  93. {
  94. if (isLocal)
  95. {
  96. t.localRotation = pose.orientation;
  97. t.localPosition = pose.position;
  98. }
  99. else
  100. {
  101. t.rotation = pose.orientation;
  102. t.position = pose.position;
  103. }
  104. }
  105. public static OVRPose ToOVRPose(this OVRPlugin.Posef p)
  106. {
  107. return new OVRPose()
  108. {
  109. position = new Vector3(p.Position.x, p.Position.y, -p.Position.z),
  110. orientation = new Quaternion(-p.Orientation.x, -p.Orientation.y, p.Orientation.z, p.Orientation.w)
  111. };
  112. }
  113. public static OVRTracker.Frustum ToFrustum(this OVRPlugin.Frustumf f)
  114. {
  115. return new OVRTracker.Frustum()
  116. {
  117. nearZ = f.zNear,
  118. farZ = f.zFar,
  119. fov = new Vector2()
  120. {
  121. x = Mathf.Rad2Deg * f.fovX,
  122. y = Mathf.Rad2Deg * f.fovY
  123. }
  124. };
  125. }
  126. public static Color FromColorf(this OVRPlugin.Colorf c)
  127. {
  128. return new Color() { r = c.r, g = c.g, b = c.b, a = c.a };
  129. }
  130. public static OVRPlugin.Colorf ToColorf(this Color c)
  131. {
  132. return new OVRPlugin.Colorf() { r = c.r, g = c.g, b = c.b, a = c.a };
  133. }
  134. public static Vector3 FromVector3f(this OVRPlugin.Vector3f v)
  135. {
  136. return new Vector3() { x = v.x, y = v.y, z = v.z };
  137. }
  138. public static Vector3 FromFlippedXVector3f(this OVRPlugin.Vector3f v)
  139. {
  140. return new Vector3() { x = -v.x, y = v.y, z = v.z };
  141. }
  142. public static Vector3 FromFlippedZVector3f(this OVRPlugin.Vector3f v)
  143. {
  144. return new Vector3() { x = v.x, y = v.y, z = -v.z };
  145. }
  146. public static OVRPlugin.Vector3f ToVector3f(this Vector3 v)
  147. {
  148. return new OVRPlugin.Vector3f() { x = v.x, y = v.y, z = v.z };
  149. }
  150. public static OVRPlugin.Vector3f ToFlippedXVector3f(this Vector3 v)
  151. {
  152. return new OVRPlugin.Vector3f() { x = -v.x, y = v.y, z = v.z };
  153. }
  154. public static OVRPlugin.Vector3f ToFlippedZVector3f(this Vector3 v)
  155. {
  156. return new OVRPlugin.Vector3f() { x = v.x, y = v.y, z = -v.z };
  157. }
  158. public static Quaternion FromQuatf(this OVRPlugin.Quatf q)
  159. {
  160. return new Quaternion() { x = q.x, y = q.y, z = q.z, w = q.w };
  161. }
  162. public static Quaternion FromFlippedXQuatf(this OVRPlugin.Quatf q)
  163. {
  164. return new Quaternion() { x = q.x, y = -q.y, z = -q.z, w = q.w };
  165. }
  166. public static Quaternion FromFlippedZQuatf(this OVRPlugin.Quatf q)
  167. {
  168. return new Quaternion() { x = -q.x, y = -q.y, z = q.z, w = q.w };
  169. }
  170. public static OVRPlugin.Quatf ToQuatf(this Quaternion q)
  171. {
  172. return new OVRPlugin.Quatf() { x = q.x, y = q.y, z = q.z, w = q.w };
  173. }
  174. public static OVRPlugin.Quatf ToFlippedXQuatf(this Quaternion q)
  175. {
  176. return new OVRPlugin.Quatf() { x = q.x, y = -q.y, z = -q.z, w = q.w };
  177. }
  178. public static OVRPlugin.Quatf ToFlippedZQuatf(this Quaternion q)
  179. {
  180. return new OVRPlugin.Quatf() { x = -q.x, y = -q.y, z = q.z, w = q.w };
  181. }
  182. public static OVR.OpenVR.HmdMatrix34_t ConvertToHMDMatrix34(this Matrix4x4 m)
  183. {
  184. OVR.OpenVR.HmdMatrix34_t pose = new OVR.OpenVR.HmdMatrix34_t();
  185. pose.m0 = m[0, 0];
  186. pose.m1 = m[0, 1];
  187. pose.m2 = -m[0, 2];
  188. pose.m3 = m[0, 3];
  189. pose.m4 = m[1, 0];
  190. pose.m5 = m[1, 1];
  191. pose.m6 = -m[1, 2];
  192. pose.m7 = m[1, 3];
  193. pose.m8 = -m[2, 0];
  194. pose.m9 = -m[2, 1];
  195. pose.m10 = m[2, 2];
  196. pose.m11 = -m[2, 3];
  197. return pose;
  198. }
  199. public static Transform FindChildRecursive(this Transform parent, string name)
  200. {
  201. foreach (Transform child in parent)
  202. {
  203. if (child.name.Contains(name))
  204. return child;
  205. var result = child.FindChildRecursive(name);
  206. if (result != null)
  207. return result;
  208. }
  209. return null;
  210. }
  211. }
  212. //4 types of node state properties that can be queried with UnityEngine.XR
  213. public enum NodeStatePropertyType
  214. {
  215. Acceleration,
  216. AngularAcceleration,
  217. Velocity,
  218. AngularVelocity,
  219. Position,
  220. Orientation
  221. }
  222. public static class OVRNodeStateProperties
  223. {
  224. #if UNITY_2017_1_OR_NEWER
  225. private static List<NodeState> nodeStateList = new List<NodeState>();
  226. #endif
  227. public static bool IsHmdPresent()
  228. {
  229. if (OVRManager.OVRManagerinitialized && OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus)
  230. return OVRPlugin.hmdPresent;
  231. #if USING_XR_SDK
  232. XRDisplaySubsystem currentDisplaySubsystem = OVRManager.GetCurrentDisplaySubsystem();
  233. if (currentDisplaySubsystem != null)
  234. return currentDisplaySubsystem.running; //In 2019.3, this should be changed to currentDisplaySubsystem.isConnected, but this is a fine placeholder for now.
  235. return false;
  236. #else
  237. return Device.isPresent;
  238. #endif
  239. }
  240. public static bool GetNodeStatePropertyVector3(Node nodeType, NodeStatePropertyType propertyType, OVRPlugin.Node ovrpNodeType, OVRPlugin.Step stepType, out Vector3 retVec)
  241. {
  242. retVec = Vector3.zero;
  243. switch (propertyType)
  244. {
  245. case NodeStatePropertyType.Acceleration:
  246. if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus)
  247. {
  248. retVec = OVRPlugin.GetNodeAcceleration(ovrpNodeType, stepType).FromFlippedZVector3f();
  249. return true;
  250. }
  251. #if UNITY_2017_1_OR_NEWER
  252. if (GetUnityXRNodeStateVector3(nodeType, NodeStatePropertyType.Acceleration, out retVec))
  253. return true;
  254. #endif
  255. break;
  256. case NodeStatePropertyType.AngularAcceleration:
  257. if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus)
  258. {
  259. retVec = OVRPlugin.GetNodeAngularAcceleration(ovrpNodeType, stepType).FromFlippedZVector3f();
  260. return true;
  261. }
  262. #if UNITY_2017_2_OR_NEWER
  263. if (GetUnityXRNodeStateVector3(nodeType, NodeStatePropertyType.AngularAcceleration, out retVec))
  264. return true;
  265. #endif
  266. break;
  267. case NodeStatePropertyType.Velocity:
  268. if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus)
  269. {
  270. retVec = OVRPlugin.GetNodeVelocity(ovrpNodeType, stepType).FromFlippedZVector3f();
  271. return true;
  272. }
  273. #if UNITY_2017_1_OR_NEWER
  274. if (GetUnityXRNodeStateVector3(nodeType, NodeStatePropertyType.Velocity, out retVec))
  275. return true;
  276. #endif
  277. break;
  278. case NodeStatePropertyType.AngularVelocity:
  279. if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus)
  280. {
  281. retVec = OVRPlugin.GetNodeAngularVelocity(ovrpNodeType, stepType).FromFlippedZVector3f();
  282. return true;
  283. }
  284. #if UNITY_2017_2_OR_NEWER
  285. if (GetUnityXRNodeStateVector3(nodeType, NodeStatePropertyType.AngularVelocity, out retVec))
  286. return true;
  287. #endif
  288. break;
  289. case NodeStatePropertyType.Position:
  290. if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus)
  291. {
  292. retVec = OVRPlugin.GetNodePose(ovrpNodeType, stepType).ToOVRPose().position;
  293. return true;
  294. }
  295. #if UNITY_2017_1_OR_NEWER
  296. if (GetUnityXRNodeStateVector3(nodeType, NodeStatePropertyType.Position, out retVec))
  297. return true;
  298. #endif
  299. break;
  300. }
  301. return false;
  302. }
  303. public static bool GetNodeStatePropertyQuaternion(Node nodeType, NodeStatePropertyType propertyType, OVRPlugin.Node ovrpNodeType, OVRPlugin.Step stepType, out Quaternion retQuat)
  304. {
  305. retQuat = Quaternion.identity;
  306. switch (propertyType)
  307. {
  308. case NodeStatePropertyType.Orientation:
  309. if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus)
  310. {
  311. retQuat = OVRPlugin.GetNodePose(ovrpNodeType, stepType).ToOVRPose().orientation;
  312. return true;
  313. }
  314. #if UNITY_2017_1_OR_NEWER
  315. if (GetUnityXRNodeStateQuaternion(nodeType, NodeStatePropertyType.Orientation, out retQuat))
  316. return true;
  317. #endif
  318. break;
  319. }
  320. return false;
  321. }
  322. #if UNITY_2017_1_OR_NEWER
  323. private static bool ValidateProperty(Node nodeType, ref NodeState requestedNodeState)
  324. {
  325. InputTracking.GetNodeStates(nodeStateList);
  326. if (nodeStateList.Count == 0)
  327. return false;
  328. bool nodeStateFound = false;
  329. requestedNodeState = nodeStateList[0];
  330. for (int i = 0; i < nodeStateList.Count; i++)
  331. {
  332. if (nodeStateList[i].nodeType == nodeType)
  333. {
  334. requestedNodeState = nodeStateList[i];
  335. nodeStateFound = true;
  336. break;
  337. }
  338. }
  339. return nodeStateFound;
  340. }
  341. #endif
  342. #if UNITY_2017_1_OR_NEWER
  343. private static bool GetUnityXRNodeStateVector3(Node nodeType, NodeStatePropertyType propertyType, out Vector3 retVec)
  344. {
  345. retVec = Vector3.zero;
  346. NodeState requestedNodeState = default(NodeState);
  347. if (!ValidateProperty(nodeType, ref requestedNodeState))
  348. return false;
  349. if (propertyType == NodeStatePropertyType.Acceleration)
  350. {
  351. if (requestedNodeState.TryGetAcceleration(out retVec))
  352. {
  353. return true;
  354. }
  355. }
  356. else if (propertyType == NodeStatePropertyType.AngularAcceleration)
  357. {
  358. #if UNITY_2017_2_OR_NEWER
  359. if (requestedNodeState.TryGetAngularAcceleration(out retVec))
  360. {
  361. return true;
  362. }
  363. #endif
  364. }
  365. else if (propertyType == NodeStatePropertyType.Velocity)
  366. {
  367. if (requestedNodeState.TryGetVelocity(out retVec))
  368. {
  369. return true;
  370. }
  371. }
  372. else if (propertyType == NodeStatePropertyType.AngularVelocity)
  373. {
  374. #if UNITY_2017_2_OR_NEWER
  375. if (requestedNodeState.TryGetAngularVelocity(out retVec))
  376. {
  377. return true;
  378. }
  379. #endif
  380. }
  381. else if (propertyType == NodeStatePropertyType.Position)
  382. {
  383. if (requestedNodeState.TryGetPosition(out retVec))
  384. {
  385. return true;
  386. }
  387. }
  388. return false;
  389. }
  390. #endif
  391. #if UNITY_2017_1_OR_NEWER
  392. private static bool GetUnityXRNodeStateQuaternion(Node nodeType, NodeStatePropertyType propertyType, out Quaternion retQuat)
  393. {
  394. retQuat = Quaternion.identity;
  395. NodeState requestedNodeState = default(NodeState);
  396. if (!ValidateProperty(nodeType, ref requestedNodeState))
  397. return false;
  398. if (propertyType == NodeStatePropertyType.Orientation)
  399. {
  400. if (requestedNodeState.TryGetRotation(out retQuat))
  401. {
  402. return true;
  403. }
  404. }
  405. return false;
  406. }
  407. #endif
  408. }
  409. /// <summary>
  410. /// An affine transformation built from a Unity position and orientation.
  411. /// </summary>
  412. [System.Serializable]
  413. public struct OVRPose
  414. {
  415. /// <summary>
  416. /// A pose with no translation or rotation.
  417. /// </summary>
  418. public static OVRPose identity
  419. {
  420. get {
  421. return new OVRPose()
  422. {
  423. position = Vector3.zero,
  424. orientation = Quaternion.identity
  425. };
  426. }
  427. }
  428. public override bool Equals(System.Object obj)
  429. {
  430. return obj is OVRPose && this == (OVRPose)obj;
  431. }
  432. public override int GetHashCode()
  433. {
  434. return position.GetHashCode() ^ orientation.GetHashCode();
  435. }
  436. public static bool operator ==(OVRPose x, OVRPose y)
  437. {
  438. return x.position == y.position && x.orientation == y.orientation;
  439. }
  440. public static bool operator !=(OVRPose x, OVRPose y)
  441. {
  442. return !(x == y);
  443. }
  444. /// <summary>
  445. /// The position.
  446. /// </summary>
  447. public Vector3 position;
  448. /// <summary>
  449. /// The orientation.
  450. /// </summary>
  451. public Quaternion orientation;
  452. /// <summary>
  453. /// Multiplies two poses.
  454. /// </summary>
  455. public static OVRPose operator*(OVRPose lhs, OVRPose rhs)
  456. {
  457. var ret = new OVRPose();
  458. ret.position = lhs.position + lhs.orientation * rhs.position;
  459. ret.orientation = lhs.orientation * rhs.orientation;
  460. return ret;
  461. }
  462. /// <summary>
  463. /// Computes the inverse of the given pose.
  464. /// </summary>
  465. public OVRPose Inverse()
  466. {
  467. OVRPose ret;
  468. ret.orientation = Quaternion.Inverse(orientation);
  469. ret.position = ret.orientation * -position;
  470. return ret;
  471. }
  472. /// <summary>
  473. /// Converts the pose from left- to right-handed or vice-versa.
  474. /// </summary>
  475. public OVRPose flipZ()
  476. {
  477. var ret = this;
  478. ret.position.z = -ret.position.z;
  479. ret.orientation.z = -ret.orientation.z;
  480. ret.orientation.w = -ret.orientation.w;
  481. return ret;
  482. }
  483. // Warning: this function is not a strict reverse of OVRPlugin.Posef.ToOVRPose(), even after flipZ()
  484. public OVRPlugin.Posef ToPosef_Legacy()
  485. {
  486. return new OVRPlugin.Posef()
  487. {
  488. Position = position.ToVector3f(),
  489. Orientation = orientation.ToQuatf()
  490. };
  491. }
  492. public OVRPlugin.Posef ToPosef()
  493. {
  494. OVRPlugin.Posef result = new OVRPlugin.Posef();
  495. result.Position.x = position.x;
  496. result.Position.y = position.y;
  497. result.Position.z = -position.z;
  498. result.Orientation.x = -orientation.x;
  499. result.Orientation.y = -orientation.y;
  500. result.Orientation.z = orientation.z;
  501. result.Orientation.w = orientation.w;
  502. return result;
  503. }
  504. }
  505. /// <summary>
  506. /// Encapsulates an 8-byte-aligned of unmanaged memory.
  507. /// </summary>
  508. public class OVRNativeBuffer : IDisposable
  509. {
  510. private bool disposed = false;
  511. private int m_numBytes = 0;
  512. private IntPtr m_ptr = IntPtr.Zero;
  513. /// <summary>
  514. /// Creates a buffer of the specified size.
  515. /// </summary>
  516. public OVRNativeBuffer(int numBytes)
  517. {
  518. Reallocate(numBytes);
  519. }
  520. /// <summary>
  521. /// Releases unmanaged resources and performs other cleanup operations before the <see cref="OVRNativeBuffer"/> is
  522. /// reclaimed by garbage collection.
  523. /// </summary>
  524. ~OVRNativeBuffer()
  525. {
  526. Dispose(false);
  527. }
  528. /// <summary>
  529. /// Reallocates the buffer with the specified new size.
  530. /// </summary>
  531. public void Reset(int numBytes)
  532. {
  533. Reallocate(numBytes);
  534. }
  535. /// <summary>
  536. /// The current number of bytes in the buffer.
  537. /// </summary>
  538. public int GetCapacity()
  539. {
  540. return m_numBytes;
  541. }
  542. /// <summary>
  543. /// A pointer to the unmanaged memory in the buffer, starting at the given offset in bytes.
  544. /// </summary>
  545. public IntPtr GetPointer(int byteOffset = 0)
  546. {
  547. if (byteOffset < 0 || byteOffset >= m_numBytes)
  548. return IntPtr.Zero;
  549. return (byteOffset == 0) ? m_ptr : new IntPtr(m_ptr.ToInt64() + byteOffset);
  550. }
  551. /// <summary>
  552. /// Releases all resource used by the <see cref="OVRNativeBuffer"/> object.
  553. /// </summary>
  554. /// <remarks>Call <see cref="Dispose"/> when you are finished using the <see cref="OVRNativeBuffer"/>. The <see cref="Dispose"/>
  555. /// method leaves the <see cref="OVRNativeBuffer"/> in an unusable state. After calling <see cref="Dispose"/>, you must
  556. /// release all references to the <see cref="OVRNativeBuffer"/> so the garbage collector can reclaim the memory that
  557. /// the <see cref="OVRNativeBuffer"/> was occupying.</remarks>
  558. public void Dispose()
  559. {
  560. Dispose(true);
  561. GC.SuppressFinalize(this);
  562. }
  563. private void Dispose(bool disposing)
  564. {
  565. if (disposed)
  566. return;
  567. if (disposing)
  568. {
  569. // dispose managed resources
  570. }
  571. // dispose unmanaged resources
  572. Release();
  573. disposed = true;
  574. }
  575. private void Reallocate(int numBytes)
  576. {
  577. Release();
  578. if (numBytes > 0)
  579. {
  580. m_ptr = Marshal.AllocHGlobal(numBytes);
  581. m_numBytes = numBytes;
  582. }
  583. else
  584. {
  585. m_ptr = IntPtr.Zero;
  586. m_numBytes = 0;
  587. }
  588. }
  589. private void Release()
  590. {
  591. if (m_ptr != IntPtr.Zero)
  592. {
  593. Marshal.FreeHGlobal(m_ptr);
  594. m_ptr = IntPtr.Zero;
  595. m_numBytes = 0;
  596. }
  597. }
  598. }