NativeHMD.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 UnityEngine;
  13. using System.Runtime.InteropServices;
  14. /// <summary> HMD Eye offset Native API . </summary>
  15. public partial class NativeHMD
  16. {
  17. /// <summary> Handle of the hmd. </summary>
  18. private UInt64 m_HmdHandle;
  19. /// <summary> Gets the handle of the hmd. </summary>
  20. /// <value> The hmd handle. </value>
  21. public UInt64 HmdHandle
  22. {
  23. get
  24. {
  25. return m_HmdHandle;
  26. }
  27. }
  28. /// <summary> Creates a new bool. </summary>
  29. /// <returns> True if it succeeds, false if it fails. </returns>
  30. public bool Create()
  31. {
  32. NativeResult result = NativeApi.NRHMDCreate(ref m_HmdHandle);
  33. NativeErrorListener.Check(result, this, "Create");
  34. return result == NativeResult.Success;
  35. }
  36. /// <summary> Pauses this object. </summary>
  37. /// <returns> True if it succeeds, false if it fails. </returns>
  38. public bool Pause()
  39. {
  40. NativeResult result = NativeApi.NRHMDPause(m_HmdHandle);
  41. NativeErrorListener.Check(result, this, "Pause");
  42. return result == NativeResult.Success;
  43. }
  44. /// <summary> Resumes this object. </summary>
  45. /// <returns> True if it succeeds, false if it fails. </returns>
  46. public bool Resume()
  47. {
  48. NativeResult result = NativeApi.NRHMDResume(m_HmdHandle);
  49. NativeErrorListener.Check(result, this, "Resume");
  50. return result == NativeResult.Success;
  51. }
  52. /// <summary> Gets device pose from head. </summary>
  53. /// <param name="device"> The device type.</param>
  54. /// <returns> The device pose from head. </returns>
  55. public Pose GetDevicePoseFromHead(NativeDevice device)
  56. {
  57. return GetDevicePoseFromHead((int)device);
  58. }
  59. /// <summary> Gets device pose from head. </summary>
  60. /// <param name="device"> The device type.</param>
  61. /// <returns> The device pose from head. </returns>
  62. public Pose GetDevicePoseFromHead(int device)
  63. {
  64. Pose outDevicePoseFromHead = Pose.identity;
  65. NativeMat4f mat4f = new NativeMat4f(Matrix4x4.identity);
  66. NativeResult result = NativeApi.NRHMDGetEyePoseFromHead(m_HmdHandle, device, ref mat4f);
  67. if (result == NativeResult.Success)
  68. {
  69. ConversionUtility.ApiPoseToUnityPose(mat4f, out outDevicePoseFromHead);
  70. }
  71. return outDevicePoseFromHead;
  72. }
  73. /// <summary> Gets projection matrix. </summary>
  74. /// <param name="outEyesProjectionMatrix"> [in,out] The out eyes projection matrix.</param>
  75. /// <param name="znear"> The znear.</param>
  76. /// <param name="zfar"> The zfar.</param>
  77. /// <returns> True if it succeeds, false if it fails. </returns>
  78. public bool GetProjectionMatrix(ref EyeProjectMatrixData outEyesProjectionMatrix, float znear, float zfar)
  79. {
  80. NativeFov4f fov = new NativeFov4f();
  81. NativeResult result_left = NativeApi.NRHMDGetEyeFovInCoord(m_HmdHandle, (int)NativeDevice.LEFT_DISPLAY, ref fov);
  82. NativeErrorListener.Check(result_left, this, "GetProjectionMatrix-L");
  83. NRDebugger.Info("[GetProjectionMatrix] LEFT_DISPLAY: {0}", fov.ToString());
  84. outEyesProjectionMatrix.LEyeMatrix = ConversionUtility.GetProjectionMatrixFromFov(fov, znear, zfar).ToUnityMat4f();
  85. NativeResult result_right = NativeApi.NRHMDGetEyeFovInCoord(m_HmdHandle, (int)NativeDevice.RIGHT_DISPLAY, ref fov);
  86. NativeErrorListener.Check(result_right, this, "GetProjectionMatrix-R");
  87. NRDebugger.Info("[GetProjectionMatrix] RIGHT_DISPLAY: {0}", fov.ToString());
  88. outEyesProjectionMatrix.REyeMatrix = ConversionUtility.GetProjectionMatrixFromFov(fov, znear, zfar).ToUnityMat4f();
  89. NativeResult result_RGB = NativeApi.NRHMDGetEyeFovInCoord(m_HmdHandle, (int)NativeDevice.RGB_CAMERA, ref fov);
  90. NativeErrorListener.Check(result_RGB, this, "GetProjectionMatrix-RGB");
  91. outEyesProjectionMatrix.RGBEyeMatrix = ConversionUtility.GetProjectionMatrixFromFov(fov, znear, zfar).ToUnityMat4f();
  92. return (result_left == NativeResult.Success && result_right == NativeResult.Success && result_RGB == NativeResult.Success);
  93. }
  94. [Obsolete("Use 'GetEyeFovInCoord' to replace.")]
  95. public NativeFov4f GetEyeFov(NativeEye eye)
  96. {
  97. NativeFov4f fov = new NativeFov4f();
  98. NativeApi.NRHMDGetEyeFov(m_HmdHandle, (int)eye, ref fov);
  99. return fov;
  100. }
  101. public NativeFov4f GetEyeFovInCoord(NativeDevice eye)
  102. {
  103. NativeFov4f fov = new NativeFov4f();
  104. NativeApi.NRHMDGetEyeFovInCoord(m_HmdHandle, (int)eye, ref fov);
  105. return fov;
  106. }
  107. /// <summary> Gets camera intrinsic matrix. </summary>
  108. /// <param name="eye"> The eye.</param>
  109. /// <param name="CameraIntrinsicMatix"> [in,out] The camera intrinsic matix.</param>
  110. /// <returns> True if it succeeds, false if it fails. </returns>
  111. public bool GetCameraIntrinsicMatrix(int eye, ref NativeMat3f CameraIntrinsicMatix)
  112. {
  113. var result = NativeApi.NRHMDGetCameraIntrinsicMatrix(m_HmdHandle, (int)eye, ref CameraIntrinsicMatix);
  114. return result == NativeResult.Success;
  115. }
  116. /// <summary> Gets camera distortion. </summary>
  117. /// <param name="eye"> The eye.</param>
  118. /// <param name="distortion"> A variable-length parameters list containing distortion.</param>
  119. /// <returns> True if it succeeds, false if it fails. </returns>
  120. public bool GetCameraDistortion(int eye, ref NRDistortionParams distortion)
  121. {
  122. var result = NativeApi.NRHMDGetCameraDistortionParams(m_HmdHandle, eye, ref distortion);
  123. return result == NativeResult.Success;
  124. }
  125. /// <summary> Gets eye resolution. </summary>
  126. /// <param name="eye"> The eye.</param>
  127. /// <returns> The eye resolution. </returns>
  128. public NativeResolution GetEyeResolution(int eye)
  129. {
  130. NativeResolution resolution = new NativeResolution(1920, 1080);
  131. #if UNITY_EDITOR
  132. return resolution;
  133. #else
  134. var result = NativeApi.NRHMDGetEyeResolution(m_HmdHandle, eye, ref resolution);
  135. NativeErrorListener.Check(result, this, "GetEyeResolution");
  136. return resolution;
  137. #endif
  138. }
  139. /// <summary> Gets device type of running device. </summary>
  140. /// <returns> The device type. </returns>
  141. public NRDeviceType GetDeviceType()
  142. {
  143. NRDeviceType deviceType = NRDeviceType.NrealLight;
  144. NativeApi.NRHMDGetDeviceType(m_HmdHandle, ref deviceType);
  145. return deviceType;
  146. }
  147. /// <summary> Gets device type of running device. </summary>
  148. /// <param name="feature"> The request feature.</param>
  149. /// <returns> Is the feature supported. </returns>
  150. public bool IsFeatureSupported(NRSupportedFeature feature)
  151. {
  152. bool result = false;
  153. NativeApi.NRHMDIsFeatureSupported(m_HmdHandle, feature, ref result);
  154. return result;
  155. }
  156. /// <summary> Destroys this object. </summary>
  157. /// <returns> True if it succeeds, false if it fails. </returns>
  158. public bool Destroy()
  159. {
  160. NativeResult result = NativeApi.NRHMDDestroy(m_HmdHandle);
  161. NativeErrorListener.Check(result, this, "Destroy");
  162. return result == NativeResult.Success;
  163. }
  164. /// <summary> A native api. </summary>
  165. private struct NativeApi
  166. {
  167. /// <summary> Nrhmd create. </summary>
  168. /// <param name="out_hmd_handle"> [in,out] Handle of the out hmd.</param>
  169. /// <returns> A NativeResult. </returns>
  170. [DllImport(NativeConstants.NRNativeLibrary)]
  171. public static extern NativeResult NRHMDCreate(ref UInt64 out_hmd_handle);
  172. /// <summary> Nrhmd get device type. </summary>
  173. /// <param name="hmd_handle"> Handle of the hmd.</param>
  174. /// <param name="out_device_type"> [in,out] The out device type.</param>
  175. /// <returns> A NativeResult. </returns>
  176. [DllImport(NativeConstants.NRNativeLibrary)]
  177. public static extern NativeResult NRHMDGetDeviceType(UInt64 hmd_handle, ref NRDeviceType out_device_type);
  178. /// <summary>
  179. /// Check whether the current feature is supported.
  180. /// </summary>
  181. /// <param name="hmd_handle"> Handle of the out hmd.</param>
  182. /// <param name="feature"> Current feature. </param>
  183. /// <param name="out_is_supported"> Result of whether the current feature is supported. </param>
  184. /// <returns></returns>
  185. [DllImport(NativeConstants.NRNativeLibrary)]
  186. public static extern NativeResult NRHMDIsFeatureSupported(UInt64 hmd_handle, NRSupportedFeature feature, ref bool out_is_supported);
  187. /// <summary> Nrhmd pause. </summary>
  188. /// <param name="hmd_handle"> Handle of the hmd.</param>
  189. /// <returns> A NativeResult. </returns>
  190. [DllImport(NativeConstants.NRNativeLibrary)]
  191. public static extern NativeResult NRHMDPause(UInt64 hmd_handle);
  192. /// <summary> Nrhmd resume. </summary>
  193. /// <param name="hmd_handle"> Handle of the hmd.</param>
  194. /// <returns> A NativeResult. </returns>
  195. [DllImport(NativeConstants.NRNativeLibrary)]
  196. public static extern NativeResult NRHMDResume(UInt64 hmd_handle);
  197. /// <summary> Nrhmd get eye pose from head. </summary>
  198. /// <param name="hmd_handle"> Handle of the hmd.</param>
  199. /// <param name="eye"> The eye.</param>
  200. /// <param name="outEyePoseFromHead"> [in,out] The out eye pose from head.</param>
  201. /// <returns> A NativeResult. </returns>
  202. [DllImport(NativeConstants.NRNativeLibrary)]
  203. public static extern NativeResult NRHMDGetEyePoseFromHead(UInt64 hmd_handle, int eye, ref NativeMat4f outEyePoseFromHead);
  204. /// <summary> Nrhmd get eye fov. </summary>
  205. /// <param name="hmd_handle"> Handle of the hmd.</param>
  206. /// <param name="eye"> The eye.</param>
  207. /// <param name="out_eye_fov"> [in,out] The out eye fov.</param>
  208. /// <returns> A NativeResult. </returns>
  209. [DllImport(NativeConstants.NRNativeLibrary)]
  210. [Obsolete]
  211. public static extern NativeResult NRHMDGetEyeFov(UInt64 hmd_handle, int eye, ref NativeFov4f out_eye_fov);
  212. /// <summary> Nrhmd get eye fov. </summary>
  213. /// <param name="hmd_handle"> Handle of the hmd.</param>
  214. /// <param name="eye"> The eye.</param>
  215. /// <param name="out_eye_fov"> [in,out] The out eye fov.</param>
  216. /// <returns> A NativeResult. </returns>
  217. [DllImport(NativeConstants.NRNativeLibrary)]
  218. public static extern NativeResult NRHMDGetEyeFovInCoord(UInt64 hmd_handle, int eye, ref NativeFov4f out_eye_fov);
  219. /// <summary> Nrhmd get camera intrinsic matrix. </summary>
  220. /// <param name="hmd_handle"> Handle of the hmd.</param>
  221. /// <param name="eye"> The eye.</param>
  222. /// <param name="out_intrinsic_matrix"> [in,out] The out intrinsic matrix.</param>
  223. /// <returns> A NativeResult. </returns>
  224. [DllImport(NativeConstants.NRNativeLibrary)]
  225. public static extern NativeResult NRHMDGetCameraIntrinsicMatrix(
  226. UInt64 hmd_handle, int eye, ref NativeMat3f out_intrinsic_matrix);
  227. /// <summary> Nrhmd get camera distortion parameters. </summary>
  228. /// <param name="hmd_handle"> Handle of the hmd.</param>
  229. /// <param name="eye"> The eye.</param>
  230. /// <param name="out_params"> A variable-length parameters list containing out parameters.</param>
  231. /// <returns> A NativeResult. </returns>
  232. [DllImport(NativeConstants.NRNativeLibrary)]
  233. public static extern NativeResult NRHMDGetCameraDistortionParams(
  234. UInt64 hmd_handle, int eye, ref NRDistortionParams out_params);
  235. /// <summary> Nrhmd get eye resolution. </summary>
  236. /// <param name="hmd_handle"> Handle of the hmd.</param>
  237. /// <param name="eye"> The eye.</param>
  238. /// <param name="out_eye_resolution"> [in,out] The out eye resolution.</param>
  239. /// <returns> A NativeResult. </returns>
  240. [DllImport(NativeConstants.NRNativeLibrary)]
  241. public static extern NativeResult NRHMDGetEyeResolution(UInt64 hmd_handle, int eye, ref NativeResolution out_eye_resolution);
  242. /// <summary> Nrhmd destroy. </summary>
  243. /// <param name="hmd_handle"> Handle of the hmd.</param>
  244. /// <returns> A NativeResult. </returns>
  245. [DllImport(NativeConstants.NRNativeLibrary)]
  246. public static extern NativeResult NRHMDDestroy(UInt64 hmd_handle);
  247. };
  248. }
  249. }