API_GSXR_Slam.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. using SC.XR.Unity;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. public class API_GSXR_Slam
  7. {
  8. public static GSXRManager SlamManager
  9. {
  10. get
  11. {
  12. return GSXRManager.Instance;
  13. }
  14. }
  15. public static GSXRPlugin plugin
  16. {
  17. get
  18. {
  19. return GSXRPlugin.Instance;
  20. }
  21. }
  22. ///API-No.1
  23. /// <summary>
  24. /// 设置眼镜进入模式,运行过程中可修改
  25. /// </summary>
  26. public static void GSXR_Set_TrackMode(TrackMode mode)
  27. {
  28. if (API_GSXR_Slam.SlamManager != null)
  29. {
  30. int trackingMode = API_GSXR_Slam.plugin.GetTrackingMode();
  31. if (mode == TrackMode.Mode_6Dof)
  32. {
  33. trackingMode |= (int)GSXRPlugin.TrackingMode.kTrackingPosition;
  34. }
  35. else
  36. {
  37. trackingMode &= (int)(~(1 << 1));
  38. }
  39. API_GSXR_Slam.plugin.SetTrackingMode(trackingMode);
  40. }
  41. }
  42. ///API-No.2
  43. /// <summary>
  44. /// Slam系统是否在运行
  45. /// </summary>
  46. /// <returns>true表示在运行,false表示未运行(Pause时为false)</returns>
  47. public static bool GSXR_Is_SlamRunning() {
  48. if (API_GSXR_Slam.SlamManager != null) {
  49. return API_GSXR_Slam.SlamManager.status.running;
  50. }
  51. return false;
  52. }
  53. ///API-No.3
  54. /// <summary>
  55. /// Slam系统是否初始化完成
  56. /// </summary>
  57. /// <returns></returns>
  58. public static bool GSXR_Is_SlamInitialized() {
  59. if (API_GSXR_Slam.SlamManager != null) {
  60. return API_GSXR_Slam.SlamManager.status.initialized;
  61. }
  62. return false;
  63. }
  64. ///API-No.4
  65. /// <summary>
  66. /// 设置Slam初始化完成时的回调
  67. /// </summary>
  68. /// <param name="action"></param>
  69. public static void GSXR_Add_InitializedCallBack(Action action)
  70. {
  71. GSXRManager.SlamInitializedCallBack += action;
  72. }
  73. ///API-No.5
  74. public static void GSXR_Remove_InitializedCallBack(Action action)
  75. {
  76. GSXRManager.SlamInitializedCallBack -= action;
  77. }
  78. ///API-No.6
  79. /// <summary>
  80. /// 设置渲染帧率,只能在Start中调用
  81. /// </summary>
  82. /// <param name="frameRate">默认-1表示系统默认帧率,设置范围0-200</param>
  83. public static void GSXR_Set_RenderFrame(int frameRate = -1)
  84. {
  85. if (API_GSXR_Slam.SlamManager != null)
  86. {
  87. if (frameRate == -1)
  88. {
  89. API_GSXR_Slam.plugin.SetVSyncCount((int)(API_GSXR_Slam.SlamManager.settings.vSyncCount = GSXRManager.SlamSettings.eVSyncCount.k1));
  90. QualitySettings.vSyncCount = (int)(API_GSXR_Slam.SlamManager.settings.vSyncCount = GSXRManager.SlamSettings.eVSyncCount.k1);//Vsync
  91. }
  92. else
  93. {
  94. API_GSXR_Slam.plugin.SetVSyncCount((int)(API_GSXR_Slam.SlamManager.settings.vSyncCount = GSXRManager.SlamSettings.eVSyncCount.k0));
  95. QualitySettings.vSyncCount = (int)(API_GSXR_Slam.SlamManager.settings.vSyncCount = GSXRManager.SlamSettings.eVSyncCount.k0);//Don't sync
  96. Application.targetFrameRate = (frameRate >= 0 && frameRate < 200) ? frameRate : 75;
  97. }
  98. }
  99. }
  100. ///API-No.7
  101. /// <summary>
  102. /// 获取左右眼摄像头
  103. /// </summary>
  104. /// <returns>List[0]左眼 List[1]右眼,空表示系统未启动完成</returns>
  105. public static List<Camera> GSXR_Get_EyeCameras()
  106. {
  107. List<Camera> cameraList = new List<Camera>(2);
  108. if (API_GSXR_Slam.SlamManager != null && API_GSXR_Slam.SlamManager.status.running == true)
  109. {
  110. cameraList.Add(Camera.main);
  111. cameraList.Add(API_GSXR_Slam.SlamManager.rightCamera);
  112. }
  113. return cameraList;
  114. }
  115. ///API-No.8
  116. /// <summary>
  117. /// 获取左右眼渲染的画面,为获取当前帧的渲染结果,当前帧结束时调用
  118. /// </summary>
  119. /// <returns>List[0]左眼 List[1]右眼,空表示系统未启动完成</returns>
  120. public static List<RenderTexture> GSXR_Get_RenderTexure()
  121. {
  122. List<Camera> cameraList = GSXR_Get_EyeCameras();
  123. List<RenderTexture> RTList = new List<RenderTexture>(2);
  124. foreach (var item in cameraList)
  125. {
  126. RTList.Add(item.targetTexture);
  127. }
  128. return RTList;
  129. }
  130. ///API-No.9
  131. /// <summary>
  132. /// 获取头部物体,如果想获取头部的旋转移动等数据,在LateUpdate方法里调用
  133. /// </summary>
  134. /// <returns>空表示系统未启动完成</returns>
  135. public static Transform GSXR_Get_Head()
  136. {
  137. return Camera.main.transform;
  138. }
  139. ///API-No.10
  140. /// <summary>
  141. /// 设置瞳距,Awake时调用,Start后调用无效
  142. /// </summary>
  143. /// <param name="offset">瞳距的偏移量,单位米</param>
  144. public static void GSXR_Set_PD(float offset = 0)
  145. {
  146. if (API_GSXR_Slam.SlamManager != null) {
  147. API_GSXR_Slam.plugin.GSXR_SetMeshOffset(0, offset / 2);
  148. API_GSXR_Slam.plugin.GSXR_SetMeshOffset(2, offset / 2);
  149. //Camera.mainOffsetPostion += offset / 2 * Vector3.left;
  150. //API_GSXR_Slam.SlamManager.rightCameraOffsetPostion += offset / 2 * Vector3.right;
  151. }
  152. }
  153. ///API-No.11
  154. /// <summary>
  155. /// 获取瞳距,
  156. /// </summary>
  157. /// <param name="type">右眼类型:2,左眼类型:0</param>
  158. public static float GSXR_Get_PD(int type)
  159. {
  160. if (API_GSXR_Slam.SlamManager != null)
  161. {
  162. return API_GSXR_Slam.plugin.GSXR_GetMeshOffset(type)+0.064f;
  163. }
  164. return -1;
  165. }
  166. ///API-No.12
  167. /// <summary>
  168. /// 重定位,若无效果,表示系统初始化未完成,且只有在眼镜上有效
  169. /// </summary>
  170. public static void GSXR_RecenterTracking()
  171. {
  172. if (API_GSXR_Slam.SlamManager != null)
  173. {
  174. API_GSXR_Slam.SlamManager.RecenterTracking();
  175. }
  176. }
  177. ///API-No.13
  178. /// <summary>
  179. /// StartSlam
  180. /// </summary>
  181. public static void GSXR_Start_Slam()
  182. {
  183. if (API_GSXR_Slam.SlamManager != null)
  184. {
  185. API_GSXR_Slam.SlamManager.StartSlam();
  186. }
  187. }
  188. ///API-No.14
  189. /// <summary>
  190. /// StopSlam
  191. /// When a StartSlam is running (not completed), calling StopSlam will not work
  192. /// </summary>
  193. public static void GSXR_Stop_Slam()
  194. {
  195. if (API_GSXR_Slam.SlamManager != null)
  196. {
  197. API_GSXR_Slam.SlamManager.StopSlam();
  198. }
  199. }
  200. ///API-No.15
  201. /// <summary>
  202. /// ResetSlam
  203. /// </summary>
  204. public static void GSXR_Reset_Slam()
  205. {
  206. if (API_GSXR_Slam.SlamManager != null)
  207. {
  208. API_GSXR_Slam.SlamManager.ResetSlam();
  209. }
  210. }
  211. public static void GSXR_Reset_Slam_V2()
  212. {
  213. GSXR_ResaveMap("resetslam");
  214. }
  215. ///API-No.16
  216. /// <summary>
  217. /// IS Slam 6Dof DataLost
  218. /// </summary>
  219. public static bool GSXR_Is_SlamDataLost
  220. {
  221. get
  222. {
  223. if (API_GSXR_Slam.SlamManager != null)
  224. {
  225. return API_GSXR_Slam.SlamManager.IsTrackingValid;
  226. }
  227. return true;
  228. }
  229. }
  230. ///API-No.17
  231. /// <summary>
  232. /// Get FishEye Data
  233. /// </summary>
  234. public static int GSXR_Get_LatestFishEyeBinocularData(ref bool outBUdate, ref uint outCurrFrameIndex, ref ulong outFrameExposureNano, byte[] outLeftFrameData, byte[] outRightFrameData)
  235. {
  236. if (API_GSXR_Slam.plugin != null)
  237. {
  238. PermissionRequest.getInstance.GetPerssion(UnityEngine.Android.Permission.Camera);
  239. return API_GSXR_Slam.plugin.GSXR_Get_LatestFishEyeBinocularData(ref outBUdate, ref outCurrFrameIndex, ref outFrameExposureNano, outLeftFrameData, outRightFrameData);
  240. }
  241. return 0;
  242. }
  243. static bool isOpen=false;
  244. public static bool GSXR_Is_EnablePointCloudData()
  245. {
  246. return isOpen;
  247. }
  248. public static void GSXR_Set_PointCloudData(bool _isOpen)
  249. {
  250. isOpen = _isOpen;
  251. }
  252. public static int GSXR_Get_PointCloudData(ref int dataNum, ref ulong dataTimestamp, float[] dataArray)
  253. {
  254. if (isOpen == false) {
  255. dataNum = 0;
  256. return 0;
  257. }
  258. if (API_GSXR_Slam.plugin != null)
  259. {
  260. return API_GSXR_Slam.plugin.GSXR_Get_PointCloudData(ref dataNum, ref dataTimestamp, dataArray);
  261. }
  262. return 0;
  263. }
  264. public static int GSXR_Get_OfflineMapRelocState()
  265. {
  266. if (API_GSXR_Slam.plugin != null)
  267. {
  268. return API_GSXR_Slam.plugin.GSXR_Get_OfflineMapRelocState();
  269. }
  270. return 0;
  271. }
  272. public static int GSXR_ResaveMap(string path)
  273. {
  274. if (API_GSXR_Slam.plugin != null)
  275. {
  276. return API_GSXR_Slam.plugin.GSXR_ResaveMap(path);
  277. }
  278. return 0;
  279. }
  280. public static void GSXR_SaveMap()
  281. {
  282. if (API_GSXR_Slam.plugin != null) {
  283. API_GSXR_Slam.plugin.GSXR_SaveMap();
  284. }
  285. }
  286. public static int GSXR_Get_Gnss(ref double dt, float[] gnss)
  287. {
  288. if (API_GSXR_Slam.plugin != null)
  289. {
  290. return API_GSXR_Slam.plugin.GSXR_Get_Gnss(ref dt, gnss);
  291. }
  292. return 0;
  293. }
  294. public static int GSXR_Get_PanelNum()
  295. {
  296. if (API_GSXR_Slam.plugin != null)
  297. {
  298. return API_GSXR_Slam.plugin.GSXR_Get_PanelNum();
  299. }
  300. return -1;
  301. }
  302. public static int GSXR_Get_PanelInfo(float[] info)
  303. {
  304. if (API_GSXR_Slam.plugin != null)
  305. {
  306. return API_GSXR_Slam.plugin.GSXR_Get_PanelInfo(info);
  307. }
  308. return -1;
  309. }
  310. public static int GSXR_Set_HeadOrigin(Vector3 postion) {
  311. if (API_GSXR_Slam.plugin != null) {
  312. API_GSXR_Slam.SlamManager.transform.position = (API_GSXR_Slam.SlamManager.transform.position - Camera.main.transform.position) + postion;
  313. return 1;
  314. }
  315. return -1;
  316. }
  317. public static int GSXR_Set_WorldOrigin(Vector3 postion) {
  318. if (API_GSXR_Slam.plugin != null) {
  319. API_GSXR_Slam.SlamManager.transform.position = Matrix4x4.TRS(-postion, Quaternion.identity, Vector3.one).MultiplyPoint(API_GSXR_Slam.SlamManager.transform.position);
  320. return 1;
  321. }
  322. return -1;
  323. }
  324. #region Old KS API
  325. /// <summary>
  326. /// Controller Vibrate
  327. /// </summary>
  328. /// <param name="index">0-LeftController 1-RightController</param>
  329. /// <param name="isOn"></param>
  330. /// <param name="vibrateOnType"></param>
  331. /// <param name="time"></param>
  332. public static void GSXR_Set_ControllerVibrate(int index, bool isOn, GSXRManager.VibrateOnType vibrateOnType = GSXRManager.VibrateOnType.OneShot, float time = 0.1f) {
  333. if (API_GSXR_Slam.plugin != null) {
  334. API_GSXR_Slam.SlamManager.GSXR_Set_ControllerVibrate(index, isOn, vibrateOnType, time);
  335. }
  336. }
  337. /// <summary>
  338. /// GSXR_Get_ControllerVibrateStatus
  339. /// </summary>
  340. /// <param name="index">0-LeftController 1-RightController</param>
  341. /// <returns></returns>
  342. public static bool GSXR_Get_ControllerVibrateStatus(int index)
  343. {
  344. if (API_GSXR_Slam.plugin != null)
  345. {
  346. return API_GSXR_Slam.SlamManager.GSXR_Get_ControllerVibrateStatus(index);
  347. }
  348. return false;
  349. }
  350. #endregion
  351. #region NEW KS Controller API
  352. /// <summary>
  353. /// Controller Vibrate
  354. /// </summary>
  355. /// <param name="index"></param>
  356. /// <param name="isOn"></param>
  357. /// <param name="amplitude"></param>
  358. /// <param name="frequency"></param>
  359. /// <param name="time"></param>
  360. /// <returns></returns>
  361. public static int GSXR_Set_ControllerVibrate(int index, bool isOn, float amplitude, float frequency, float time)
  362. {
  363. if (API_GSXR_Slam.plugin != null)
  364. {
  365. return API_GSXR_Slam.SlamManager.GSXR_Set_ControllerVibrate(index, isOn, amplitude, frequency, time);
  366. }
  367. return -1;
  368. }
  369. [Obsolete("Please Use NEW KS Vibrate GSXR_Set_ControllerVibrate(int index, bool isOn, float amplitude, float frequency, float time)")]
  370. public static int GSXR_Set_ControllerVibrate(int index, bool isOn, int amplitude, int frequency, int time)
  371. {
  372. if (API_GSXR_Slam.plugin != null)
  373. {
  374. return API_GSXR_Slam.SlamManager.GSXR_Set_ControllerVibrate(index, isOn, amplitude/15f, frequency/15f, time/10f);
  375. }
  376. return -1;
  377. }
  378. /// <summary>
  379. /// GSXR_Get_ControllerVibrateState,
  380. /// return:"0" is false ,"1" is true
  381. /// </summary>
  382. /// <param name="index">0-LeftController 1-RightController</param>
  383. /// <returns></returns>
  384. public static int GSXR_Get_ControllerVibrateState(int index)
  385. {
  386. if (API_GSXR_Slam.plugin != null)
  387. {
  388. return API_GSXR_Slam.SlamManager.GSXR_Get_ControllerVibrateState(index);
  389. }
  390. return -1;
  391. }
  392. #endregion
  393. #region Other API
  394. /// <summary>
  395. /// GSXR_Set_OnFloorOrOnHead
  396. /// </summary>
  397. /// <param name="type">0-Head, 1-Floor</param>
  398. public static void GSXR_Set_OnFloorOrOnHead(int type)
  399. {
  400. if (API_GSXR_Slam.plugin != null)
  401. {
  402. API_GSXR_Slam.SlamManager.GSXR_Set_OnFloorOrOnHead(type);
  403. }
  404. }
  405. public static void GSXR_Bind_Controller(int index,int type)
  406. {
  407. if (API_GSXR_Slam.plugin != null)
  408. {
  409. API_GSXR_Slam.SlamManager.GSXR_Bind_Controller(index, type);
  410. }
  411. }
  412. public static void GSXR_Unbind_Controller(int index, int type)
  413. {
  414. if (API_GSXR_Slam.plugin != null)
  415. {
  416. API_GSXR_Slam.SlamManager.GSXR_Unbind_Controller(index, type);
  417. }
  418. }
  419. public static int GSXR_Get_ControllerBondState()
  420. {
  421. if (API_GSXR_Slam.plugin != null)
  422. {
  423. return API_GSXR_Slam.SlamManager.GSXR_Get_ControllerBondState();
  424. }
  425. return -2;
  426. }
  427. /// <summary>
  428. /// Whether to Enable the Controller binding status callback interface
  429. /// </summary>
  430. public static void GSXR_Set_ControllerBondEventCallback(bool isEnable)
  431. {
  432. if (API_GSXR_Slam.plugin != null)
  433. {
  434. API_GSXR_Slam.SlamManager.GSXR_Set_ControllerBondEventCallback(isEnable);
  435. }
  436. }
  437. /// <summary>
  438. /// Add Controller binding state callback event
  439. /// </summary>
  440. /// <param name="callback"></param>
  441. public static void GSXR_Add_ControllerBondEventCallback(Action<int> callback)
  442. {
  443. if (API_GSXR_Slam.plugin != null)
  444. {
  445. API_GSXR_Slam.SlamManager.GSXR_Add_ControllerBondEventCallback(callback);
  446. }
  447. }
  448. /// <summary>
  449. /// Remove Controller binding state callback event
  450. /// </summary>
  451. /// <param name="callback"></param>
  452. public static void GSXR_Remove_ControllerBondEventCallback(Action<int> callback)
  453. {
  454. if (API_GSXR_Slam.plugin != null)
  455. {
  456. API_GSXR_Slam.SlamManager.GSXR_Remove_ControllerBondEventCallback(callback);
  457. }
  458. }
  459. #endregion
  460. #region SeeThrough API
  461. /// <summary>
  462. /// GSXR_StartSeeThrough
  463. /// </summary>
  464. public static void GSXR_StartSeeThrough()
  465. {
  466. if (API_GSXR_Slam.plugin != null)
  467. {
  468. API_GSXR_Slam.plugin.GSXR_StartSeeThrough();
  469. }
  470. }
  471. /// <summary>
  472. /// GSXR_StopSeeThrough
  473. /// </summary>
  474. public static void GSXR_StopSeeThrough()
  475. {
  476. if (API_GSXR_Slam.plugin != null)
  477. {
  478. API_GSXR_Slam.plugin.GSXR_StopSeeThrough();
  479. }
  480. }
  481. public static void GSXR_Add_SeeThrough_Callback(Action<bool> callback)
  482. {
  483. if (API_GSXR_Slam.plugin != null)
  484. {
  485. API_GSXR_Slam.plugin.GSXR_Regist_SeeThrough_Callback(callback);
  486. }
  487. }
  488. public static void GSXR_Remove_SeeThrough_Callback(Action<bool> callback)
  489. {
  490. if (API_GSXR_Slam.plugin != null)
  491. {
  492. API_GSXR_Slam.plugin.GSXR_UnRegist_SeeThrough_Callbak(callback);
  493. }
  494. }
  495. #endregion
  496. /// <summary>
  497. /// GSXR_Add_SlamPauseCallback
  498. /// </summary>
  499. /// <param name="callback"></param>
  500. public static void GSXR_Add_SlamPauseCallback(GSXRManager.OnApplicationPauseDele callback)
  501. {
  502. GSXRManager.onApplicationPauseDele += callback;
  503. }
  504. /// <summary>
  505. ///
  506. /// </summary>
  507. /// <param name="callback"></param>
  508. public static void GSXR_Remove_SlamPauseCallback(GSXRManager.OnApplicationPauseDele callback)
  509. {
  510. GSXRManager.onApplicationPauseDele -= callback;
  511. }
  512. }