Api.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. //-----------------------------------------------------------------------
  2. //-----------------------------------------------------------------------
  3. namespace Rokid.UXR.Native
  4. {
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.Runtime.InteropServices;
  9. using UnityEngine;
  10. using Rokid.XR.Core;
  11. /// <summary>
  12. /// UXR Plugin.
  13. /// </summary>
  14. public static class Api
  15. {
  16. private static Rect _cachedSafeArea;
  17. private static ScreenOrientation _cachedScreenOrientation;
  18. /// <summary>
  19. /// Updates screen parameters. This method must be called at framerate to ensure the current
  20. /// screen orientation is properly taken into account by the head tracker.
  21. /// </summary>
  22. public static void UpdateScreenParams()
  23. {
  24. if (!XRLoader._isInitialized)
  25. {
  26. Debug.LogError(
  27. "Please initialize UXR loader before calling this function.");
  28. return;
  29. }
  30. // Only set viewport orientation if it has changed since the last check.
  31. if (_cachedScreenOrientation != Screen.orientation)
  32. {
  33. _cachedScreenOrientation = Screen.orientation;
  34. XRLoader.SetViewportOrientation(_cachedScreenOrientation);
  35. XRLoader.RecalculateRectangles(Screen.safeArea);
  36. //ReloadDeviceParams();
  37. }
  38. }
  39. public static void setTrackingType(int type)
  40. {
  41. if (!XRLoader._isInitialized)
  42. {
  43. Debug.LogError(
  44. "Please initialize UXR loader before calling this function.");
  45. return;
  46. }
  47. setHeadTrackingType(type);
  48. }
  49. /// <summary>
  50. /// Recenters the head tracker(yaw only).
  51. /// </summary>
  52. public static void Recenter()
  53. {
  54. if (!XRLoader._isInitialized)
  55. {
  56. Debug.LogError(
  57. "Please initialize UXR loader before calling this function.");
  58. return;
  59. }
  60. recenterHeadPose();
  61. }
  62. /// <summary>
  63. /// Recenters the head tracker(pitch,yaw and roll).
  64. /// </summary>
  65. public static void Recenter2()
  66. {
  67. if (!XRLoader._isInitialized)
  68. {
  69. Debug.LogError(
  70. "Please initialize UXR loader before calling this function.");
  71. return;
  72. }
  73. recenterHeadPoseYPR();
  74. }
  75. public static string getDebugInfoU()
  76. {
  77. if (!XRLoader._isInitialized)
  78. {
  79. Debug.LogError(
  80. "Please initialize UXR loader before calling this function.");
  81. return "Not Initialized";
  82. }
  83. IntPtr keyPtr = getDebugInfo();
  84. string result = Marshal.PtrToStringAnsi(keyPtr);
  85. //Debug.Log("Debug Info: " + result);
  86. return result;
  87. }
  88. public static int getHeadTrackerState()
  89. {
  90. if (!XRLoader._isInitialized)
  91. {
  92. Debug.LogError(
  93. "Please initialize UXR loader before calling this function.");
  94. return 1;
  95. }
  96. int state = getSlamState();
  97. //Debug.Log("head tracker State: " + state);
  98. return state;
  99. }
  100. /// <summary>
  101. /// 获取预测的未来某时刻Head位姿
  102. /// </summary>
  103. /// <param name="position"></param>
  104. /// <param name="orientation"></param>
  105. /// <returns></returns>
  106. public static long getHeadPose(float[] position, float[] orientation)
  107. {
  108. if (!XRLoader._isInitialized)
  109. {
  110. Debug.LogError(
  111. "Please initialize UXR loader before calling this function.");
  112. return 1;
  113. }
  114. long ts = getHeadPoseRHS(position, orientation);
  115. //R2L
  116. position[2] = -position[2];
  117. orientation[0] = -orientation[0]; orientation[1] = -orientation[1];
  118. return ts;
  119. }
  120. /// <summary>
  121. /// 获取最新相机时刻(physical camera)的Head位姿
  122. /// </summary>
  123. /// <param name="position"></param>
  124. /// <param name="orientation"></param>
  125. /// <returns></returns>
  126. public static long GetHeadPoseCameraBase(float[] position, float[] orientation)
  127. {
  128. if (!XRLoader._isInitialized)
  129. {
  130. Debug.LogError(
  131. "Please initialize UXR loader before calling this function.");
  132. return 1;
  133. }
  134. long ts = getHeadPoseCameraBaseRHS(position, orientation);
  135. //R2L
  136. position[2] = -position[2];
  137. orientation[0] = -orientation[0]; orientation[1] = -orientation[1];
  138. return ts;
  139. }
  140. /// <summary>
  141. /// 获取左右眼投影参数
  142. /// </summary>
  143. /// <param name="frustum_left">float[6] {left,right,bottom,top,near,far}</param>
  144. /// <param name="frustum_right">{left,right,bottom,top,near,far}</param>
  145. /// <returns></returns>
  146. public static bool getFrustum(float[] frustum_left, float[] frustum_right)
  147. {
  148. if (!XRLoader._isInitialized)
  149. {
  150. Debug.LogError(
  151. "Please initialize UXR loader before calling this function.");
  152. return false;
  153. }
  154. bool result = get_frustum(frustum_left, frustum_right);
  155. return result;
  156. }
  157. public static String GetDeviceModel()
  158. {
  159. IntPtr charPointer = CardboardUnity_getDeviceModel();
  160. if (charPointer == null)
  161. {
  162. return "";
  163. }
  164. return Marshal.PtrToStringAnsi(CardboardUnity_getDeviceModel());
  165. }
  166. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  167. private static extern void setHeadTrackingType(int type);
  168. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  169. private static extern void recenterHeadPose();
  170. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  171. private static extern void recenterHeadPoseYPR();
  172. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  173. private static extern IntPtr getDebugInfo();
  174. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  175. private static extern int getSlamState();
  176. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  177. private static extern long getHeadPoseRHS(float[] position, float[] orientation);
  178. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  179. private static extern long getHeadPoseCameraBaseRHS(float[] position, float[] orientation);
  180. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  181. private static extern bool get_frustum(float[] frustum_left, float[] frustum_right);
  182. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  183. public static extern int getGlassProductId();
  184. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  185. public static extern bool isUsbConnect();
  186. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  187. public static extern IntPtr getGlassName();
  188. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  189. public static extern IntPtr getGlassTypeId();
  190. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  191. public static extern IntPtr getGlassSn();
  192. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  193. public static extern IntPtr getGlassSeed();
  194. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  195. public static extern void setGlassBrightness(int value);
  196. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  197. public static extern int getGlassBrightness();
  198. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  199. public static extern IntPtr getGlassCalFile();
  200. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  201. public static extern IntPtr getGlassFirmwareVersion();
  202. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  203. public static extern bool isPreviewing();
  204. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  205. public static extern void getPreviewDimen(int[] data);
  206. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  207. public static extern void openPhoneTracker();
  208. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  209. public static extern void closePhoneTracker();
  210. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  211. public static extern void getPhonePose(float[] oritation);
  212. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  213. public static extern void recenterPhonePose();
  214. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  215. public static extern void recenterPhonePoseYPR();
  216. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  217. private static extern void CardboardUnity_setDeviceURL(String url);
  218. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  219. private static extern IntPtr CardboardUnity_getDeviceModel();
  220. /**
  221. * 获取fx,fy
  222. */
  223. [Obsolete("Use NativeInterface.NativeAPI.GetFocalLength instead"), DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  224. public static extern void getFocalLength(float[] data);
  225. /**
  226. * 获取cx,cy
  227. */
  228. [Obsolete("Use NativeInterface.NativeAPI.GetPrincipalPoint instead"), DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  229. public static extern void getPrincipalPoint(float[] data);
  230. /**
  231. * 获取width,height
  232. */
  233. [Obsolete("Use NativeInterface.NativeAPI.GetImageDimensions instead"), DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  234. public static extern void getImageDimensions(int[] data);
  235. /**
  236. * pinhole:k1,k2,k3,p1,p2
  237. * fisheye:alpha,k1,k2,k3,k4;
  238. */
  239. [Obsolete("Use NativeInterface.NativeAPI.GetDistortion instead"), DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  240. public static extern void getDistortion(float[] data);
  241. }
  242. }