Api.cs 8.5 KB

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