NRFrame.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal
  10. {
  11. using System;
  12. using System.Collections.Generic;
  13. using UnityEngine;
  14. /// <summary>
  15. /// Holds information about NR Device's pose in the world coordinate, trackables, etc.. Through
  16. /// this class, application can get the information of current frame. It contains session status,
  17. /// lost tracking reason, device pose, trackables, etc. </summary>
  18. public class NRFrame
  19. {
  20. /// <summary> Get the tracking state of HMD. </summary>
  21. /// <value> The session status. </value>
  22. public static SessionState SessionStatus
  23. {
  24. get
  25. {
  26. return NRSessionManager.Instance.SessionState;
  27. }
  28. }
  29. /// <summary> Get the lost tracking reason of HMD. </summary>
  30. /// <value> The lost tracking reason. </value>
  31. public static LostTrackingReason LostTrackingReason
  32. {
  33. get
  34. {
  35. return NRSessionManager.Instance.LostTrackingReason;
  36. }
  37. }
  38. /// <summary> Gets the nr renderer. </summary>
  39. /// <value> The nr renderer. </value>
  40. public static NRRenderer NRRenderer
  41. {
  42. get
  43. {
  44. return NRSessionManager.Instance.NRRenderer;
  45. }
  46. }
  47. /// <summary> The head pose. </summary>
  48. private static Pose m_HeadPose;
  49. /// <summary> Get the pose of device in unity world coordinate. </summary>
  50. /// <value> Pose of device. </value>
  51. public static Pose HeadPose
  52. {
  53. get
  54. {
  55. return m_HeadPose;
  56. }
  57. }
  58. public static bool isHeadPoseReady { get; private set; } = false;
  59. /// <summary> Gets head pose by recommond timestamp. </summary>
  60. /// <param name="pose"> [in,out] The pose.</param>
  61. /// <returns> True if it succeeds, false if it fails. </returns>
  62. [Obsolete("Use 'GetFramePresentHeadPose' instead.")]
  63. public static bool GetHeadPoseRecommend(ref Pose pose)
  64. {
  65. if (SessionStatus == SessionState.Running)
  66. {
  67. return NRSessionManager.Instance.NativeAPI.NativeHeadTracking.GetHeadPoseRecommend(ref pose);
  68. }
  69. return false;
  70. }
  71. /// <summary> Gets head pose by timestamp. </summary>
  72. /// <param name="pose"> [in,out] The pose.</param>
  73. /// <param name="timestamp"> The timestamp.</param>
  74. /// <returns> True if it succeeds, false if it fails. </returns>
  75. public static bool GetHeadPoseByTime(ref Pose pose, UInt64 timestamp)
  76. {
  77. if (SessionStatus == SessionState.Running)
  78. {
  79. return NRSessionManager.Instance.TrackingSubSystem.GetHeadPose(ref pose, timestamp);
  80. }
  81. return false;
  82. }
  83. /// <summary>
  84. /// Get the pose information when the current frame display on the screen.
  85. /// </summary>
  86. /// <param name="pose"></param>
  87. /// <param name="timestamp">current timestamp to the pose.</param>
  88. /// <returns></returns>
  89. public static bool GetFramePresentHeadPose(ref Pose pose, ref LostTrackingReason lostReason, ref UInt64 timestamp)
  90. {
  91. if (SessionStatus == SessionState.Running &&
  92. (NRRenderer == null || NRRenderer.CurrentState == NRRenderer.RendererState.Running))
  93. {
  94. isHeadPoseReady = NRSessionManager.Instance.TrackingSubSystem.GetFramePresentHeadPose(ref pose, ref lostReason, ref timestamp);
  95. return isHeadPoseReady;
  96. }
  97. return false;
  98. }
  99. /// <summary> Get the pose of center camera between left eye and right eye. </summary>
  100. /// <value> The center eye pose. </value>
  101. public static Pose CenterEyePose
  102. {
  103. get
  104. {
  105. Transform leftCamera = NRSessionManager.Instance.NRHMDPoseTracker.leftCamera.transform;
  106. Transform rightCamera = NRSessionManager.Instance.NRHMDPoseTracker.rightCamera.transform;
  107. Vector3 centerEye_pos = (leftCamera.position + rightCamera.position) * 0.5f;
  108. Quaternion centerEye_rot = Quaternion.Lerp(leftCamera.rotation, rightCamera.rotation, 0.5f);
  109. return new Pose(centerEye_pos, centerEye_rot);
  110. }
  111. }
  112. /// <summary> The eye position from head. </summary>
  113. private static EyePoseData m_EyePosFromHead;
  114. /// <summary> Get the offset position between eye and head. </summary>
  115. /// <value> The eye pose from head. </value>
  116. public static EyePoseData EyePoseFromHead
  117. {
  118. get
  119. {
  120. if (SessionStatus == SessionState.Running)
  121. {
  122. m_EyePosFromHead.LEyePose = NRDevice.Subsystem.GetDevicePoseFromHead(NativeDevice.LEFT_DISPLAY);
  123. m_EyePosFromHead.REyePose = NRDevice.Subsystem.GetDevicePoseFromHead(NativeDevice.RIGHT_DISPLAY);
  124. m_EyePosFromHead.RGBEyePose = NRDevice.Subsystem.GetDevicePoseFromHead(NativeDevice.RGB_CAMERA);
  125. }
  126. return m_EyePosFromHead;
  127. }
  128. }
  129. /// <summary> Get the offset position between device and head. </summary>
  130. /// <value> The device pose from head. </value>
  131. public static Pose GetDevicePoseFromHead(NativeDevice device)
  132. {
  133. return NRDevice.Subsystem.GetDevicePoseFromHead(device);
  134. }
  135. /// <summary> Get the project matrix of camera in unity. </summary>
  136. /// <param name="result"> [out] True to result.</param>
  137. /// <param name="znear"> The znear.</param>
  138. /// <param name="zfar"> The zfar.</param>
  139. /// <returns> project matrix of camera. </returns>
  140. public static EyeProjectMatrixData GetEyeProjectMatrix(out bool result, float znear, float zfar)
  141. {
  142. return NRDevice.Subsystem.GetEyeProjectMatrix(out result, znear, zfar);
  143. }
  144. /// <summary> Get the intrinsic matrix of device. </summary>
  145. /// <returns> The device intrinsic matrix. </returns>
  146. public static NativeMat3f GetDeviceIntrinsicMatrix(NativeDevice device)
  147. {
  148. return NRDevice.Subsystem.GetDeviceIntrinsicMatrix(device);
  149. }
  150. /// <summary> Get the distortion param of device. </summary>
  151. /// <returns> The device intrinsic matrix. </returns>
  152. public static NRDistortionParams GetDeviceDistortion(NativeDevice device)
  153. {
  154. return NRDevice.Subsystem.GetDeviceDistortion(device);
  155. }
  156. /// <summary> Get the intrinsic matrix of rgb camera. </summary>
  157. /// <returns> The RGB camera intrinsic matrix. </returns>
  158. public static NativeMat3f GetRGBCameraIntrinsicMatrix()
  159. {
  160. return GetDeviceIntrinsicMatrix(NativeDevice.RGB_CAMERA);
  161. }
  162. /// <summary> Get the Distortion of rgbcamera. </summary>
  163. /// <returns> The RGB camera distortion. </returns>
  164. public static NRDistortionParams GetRGBCameraDistortion()
  165. {
  166. return GetDeviceDistortion(NativeDevice.RGB_CAMERA);
  167. }
  168. /// <summary> Gets the resolution of device. </summary>
  169. /// <param name="eye"> device index.</param>
  170. /// <returns> The device resolution. </returns>
  171. public static NativeResolution GetDeviceResolution(NativeDevice device)
  172. {
  173. return NRDevice.Subsystem.GetDeviceResolution(device);
  174. }
  175. /// <summary> Gets device fov. </summary>
  176. /// <param name="eye"> The display index.</param>
  177. /// <param name="fov"> [in,out] The out device fov.</param>
  178. /// <returns> A NativeResult. </returns>
  179. public static void GetEyeFov(NativeDevice eye, ref NativeFov4f fov)
  180. {
  181. NRDevice.Subsystem.GetEyeFov(eye, ref fov);
  182. }
  183. private static UInt64 m_CurrentPoseTimeStamp = 0;
  184. public static UInt64 CurrentPoseTimeStamp
  185. {
  186. get
  187. {
  188. #if USING_XR_SDK
  189. return NRSessionManager.Instance.TrackingSubSystem.GetHMDTimeNanos();
  190. #else
  191. return m_CurrentPoseTimeStamp;
  192. #endif
  193. }
  194. }
  195. internal static void ResetHeadPose()
  196. {
  197. m_CurrentPoseTimeStamp = 0;
  198. m_HeadPose = Pose.identity;
  199. }
  200. internal static void OnPreUpdate(ref LostTrackingReason lostTrackReason)
  201. {
  202. Pose pose = Pose.identity;
  203. LostTrackingReason lostReason = LostTrackingReason.NONE;
  204. ulong timeStamp = 0;
  205. bool result = GetFramePresentHeadPose(ref pose, ref lostReason, ref timeStamp);
  206. if (result)
  207. {
  208. lostTrackReason = lostReason;
  209. if (lostReason != LostTrackingReason.PRE_INITIALIZING && lostReason != LostTrackingReason.INITIALIZING)
  210. {
  211. m_HeadPose = pose;
  212. m_CurrentPoseTimeStamp = timeStamp;
  213. }
  214. else
  215. {
  216. NRDebugger.Info("[NRFrame] OnPreUpdate: LostTrackReason={0}, pose={1}, timeStamp={2}, lastHeadPose={3}, lastTimeStamp={4}",
  217. lostReason, pose.ToString("F4"), timeStamp, m_HeadPose.ToString("F4"), m_CurrentPoseTimeStamp);
  218. }
  219. }
  220. // NRDebugger.Info("[NRFrame] OnPreUpdate: result={2}, LostTrackReason={3}, pos={0}, headPos={1}, ", pose.ToString("F4"), m_HeadPose.ToString("F4"), result, LostTrackingReason);
  221. }
  222. /// <summary> Get the list of trackables with specified filter. </summary>
  223. /// <typeparam name="T"> Generic type parameter.</typeparam>
  224. /// <param name="trackables"> A list where the returned trackable is stored. The previous values
  225. /// will be cleared.</param>
  226. /// <param name="filter"> Query filter.</param>
  227. public static void GetTrackables<T>(List<T> trackables, NRTrackableQueryFilter filter) where T : NRTrackable
  228. {
  229. if (SessionStatus != SessionState.Running)
  230. {
  231. return;
  232. }
  233. trackables.Clear();
  234. NRSessionManager.Instance.TrackableFactory.GetTrackables<T>(trackables, filter);
  235. }
  236. public static Matrix4x4 GetWorldMatrixFromUnityToNative()
  237. {
  238. var hmdPoseTracker = NRSessionManager.Instance.NRHMDPoseTracker;
  239. if (hmdPoseTracker == null)
  240. {
  241. return Matrix4x4.identity;
  242. }
  243. else
  244. {
  245. return hmdPoseTracker.GetWorldOffsetMatrixFromNative();
  246. }
  247. }
  248. }
  249. }