API_GSXR_Slam.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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 OpenXRCamera.Instance.head;
  138. if (API_GSXR_Slam.SlamManager != null && API_GSXR_Slam.SlamManager.status.running == true)
  139. {
  140. return Camera.main.transform;
  141. }
  142. return null;
  143. }
  144. ///API-No.10
  145. /// <summary>
  146. /// 设置瞳距,Awake时调用,Start后调用无效
  147. /// </summary>
  148. /// <param name="offset">瞳距的偏移量,单位米</param>
  149. public static void GSXR_Set_PD(float offset = 0)
  150. {
  151. if (API_GSXR_Slam.SlamManager != null) {
  152. API_GSXR_Slam.plugin.GSXR_SetMeshOffset(0, offset / 2);
  153. API_GSXR_Slam.plugin.GSXR_SetMeshOffset(2, offset / 2);
  154. //Camera.mainOffsetPostion += offset / 2 * Vector3.left;
  155. //API_GSXR_Slam.SlamManager.rightCameraOffsetPostion += offset / 2 * Vector3.right;
  156. }
  157. }
  158. ///API-No.11
  159. /// <summary>
  160. /// 获取瞳距,
  161. /// </summary>
  162. /// <param name="type">右眼类型:2,左眼类型:0</param>
  163. public static float GSXR_Get_PD(int type)
  164. {
  165. if (API_GSXR_Slam.SlamManager != null)
  166. {
  167. return API_GSXR_Slam.plugin.GSXR_GetMeshOffset(type)+0.064f;
  168. }
  169. return -1;
  170. }
  171. ///API-No.12
  172. /// <summary>
  173. /// 重定位,若无效果,表示系统初始化未完成,且只有在眼镜上有效
  174. /// </summary>
  175. public static void GSXR_RecenterTracking()
  176. {
  177. if (API_GSXR_Slam.SlamManager != null)
  178. {
  179. API_GSXR_Slam.SlamManager.RecenterTracking();
  180. }
  181. }
  182. ///API-No.13
  183. /// <summary>
  184. /// StartSlam
  185. /// </summary>
  186. public static void GSXR_Start_Slam()
  187. {
  188. if (API_GSXR_Slam.SlamManager != null)
  189. {
  190. API_GSXR_Slam.SlamManager.StartSlam();
  191. }
  192. }
  193. ///API-No.14
  194. /// <summary>
  195. /// StopSlam
  196. /// When a StartSlam is running (not completed), calling StopSlam will not work
  197. /// </summary>
  198. public static void GSXR_Stop_Slam()
  199. {
  200. if (API_GSXR_Slam.SlamManager != null)
  201. {
  202. API_GSXR_Slam.SlamManager.StopSlam();
  203. }
  204. }
  205. ///API-No.15
  206. /// <summary>
  207. /// ResetSlam
  208. /// </summary>
  209. public static void GSXR_Reset_Slam()
  210. {
  211. if (API_GSXR_Slam.SlamManager != null)
  212. {
  213. API_GSXR_Slam.SlamManager.ResetSlam();
  214. }
  215. }
  216. public static void GSXR_Reset_Slam_V2()
  217. {
  218. GSXR_ResaveMap("resetslam");
  219. }
  220. ///API-No.16
  221. /// <summary>
  222. /// IS Slam 6Dof DataLost
  223. /// </summary>
  224. public static bool GSXR_Is_SlamDataLost
  225. {
  226. get
  227. {
  228. if (API_GSXR_Slam.SlamManager != null)
  229. {
  230. return API_GSXR_Slam.SlamManager.IsTrackingValid;
  231. }
  232. return true;
  233. }
  234. }
  235. ///API-No.17
  236. /// <summary>
  237. /// Get FishEye Data
  238. /// </summary>
  239. public static int GSXR_Get_LatestFishEyeBinocularData(ref bool outBUdate, ref uint outCurrFrameIndex, ref ulong outFrameExposureNano, byte[] outLeftFrameData, byte[] outRightFrameData)
  240. {
  241. if (API_GSXR_Slam.plugin != null)
  242. {
  243. PermissionRequest.getInstance.GetPerssion(UnityEngine.Android.Permission.Camera);
  244. return API_GSXR_Slam.plugin.GSXR_Get_LatestFishEyeBinocularData(ref outBUdate, ref outCurrFrameIndex, ref outFrameExposureNano, outLeftFrameData, outRightFrameData);
  245. }
  246. return 0;
  247. }
  248. static bool isOpen=false;
  249. public static bool GSXR_Is_EnablePointCloudData()
  250. {
  251. return isOpen;
  252. }
  253. public static void GSXR_Set_PointCloudData(bool _isOpen)
  254. {
  255. isOpen = _isOpen;
  256. }
  257. public static int GSXR_Get_PointCloudData(ref int dataNum, ref ulong dataTimestamp, float[] dataArray)
  258. {
  259. if (isOpen == false) {
  260. dataNum = 0;
  261. return 0;
  262. }
  263. if (API_GSXR_Slam.plugin != null)
  264. {
  265. return API_GSXR_Slam.plugin.GSXR_Get_PointCloudData(ref dataNum, ref dataTimestamp, dataArray);
  266. }
  267. return 0;
  268. }
  269. public static int GSXR_Get_OfflineMapRelocState()
  270. {
  271. if (API_GSXR_Slam.plugin != null)
  272. {
  273. return API_GSXR_Slam.plugin.GSXR_Get_OfflineMapRelocState();
  274. }
  275. return 0;
  276. }
  277. public static int GSXR_ResaveMap(string path)
  278. {
  279. if (API_GSXR_Slam.plugin != null)
  280. {
  281. return API_GSXR_Slam.plugin.GSXR_ResaveMap(path);
  282. }
  283. return 0;
  284. }
  285. public static void GSXR_SaveMap()
  286. {
  287. if (API_GSXR_Slam.plugin != null) {
  288. API_GSXR_Slam.plugin.GSXR_SaveMap();
  289. }
  290. }
  291. public static int GSXR_Get_Gnss(ref double dt, float[] gnss)
  292. {
  293. if (API_GSXR_Slam.plugin != null)
  294. {
  295. return API_GSXR_Slam.plugin.GSXR_Get_Gnss(ref dt, gnss);
  296. }
  297. return 0;
  298. }
  299. public static int GSXR_Get_PanelNum()
  300. {
  301. if (API_GSXR_Slam.plugin != null)
  302. {
  303. return API_GSXR_Slam.plugin.GSXR_Get_PanelNum();
  304. }
  305. return -1;
  306. }
  307. public static int GSXR_Get_PanelInfo(float[] info)
  308. {
  309. if (API_GSXR_Slam.plugin != null)
  310. {
  311. return API_GSXR_Slam.plugin.GSXR_Get_PanelInfo(info);
  312. }
  313. return -1;
  314. }
  315. public static int GSXR_Set_HeadOrigin(Vector3 postion) {
  316. if (API_GSXR_Slam.plugin != null) {
  317. API_GSXR_Slam.SlamManager.transform.position = (API_GSXR_Slam.SlamManager.transform.position - Camera.main.transform.position) + postion;
  318. return 1;
  319. }
  320. return -1;
  321. }
  322. public static int GSXR_Set_WorldOrigin(Vector3 postion) {
  323. if (API_GSXR_Slam.plugin != null) {
  324. API_GSXR_Slam.SlamManager.transform.position = Matrix4x4.TRS(-postion, Quaternion.identity, Vector3.one).MultiplyPoint(API_GSXR_Slam.SlamManager.transform.position);
  325. return 1;
  326. }
  327. return -1;
  328. }
  329. #region Old KS API
  330. /// <summary>
  331. /// Controller Vibrate
  332. /// </summary>
  333. /// <param name="index">0-LeftController 1-RightController</param>
  334. /// <param name="isOn"></param>
  335. /// <param name="vibrateOnType"></param>
  336. /// <param name="time"></param>
  337. public static void GSXR_Set_ControllerVibrate(int index, bool isOn, GSXRManager.VibrateOnType vibrateOnType = GSXRManager.VibrateOnType.OneShot, float time = 0.1f) {
  338. if (API_GSXR_Slam.plugin != null) {
  339. API_GSXR_Slam.SlamManager.GSXR_Set_ControllerVibrate(index, isOn, vibrateOnType, time);
  340. }
  341. }
  342. /// <summary>
  343. /// GSXR_Get_ControllerVibrateStatus
  344. /// </summary>
  345. /// <param name="index">0-LeftController 1-RightController</param>
  346. /// <returns></returns>
  347. public static bool GSXR_Get_ControllerVibrateStatus(int index)
  348. {
  349. if (API_GSXR_Slam.plugin != null)
  350. {
  351. return API_GSXR_Slam.SlamManager.GSXR_Get_ControllerVibrateStatus(index);
  352. }
  353. return false;
  354. }
  355. #endregion
  356. #region NEW KS Controller API
  357. /// <summary>
  358. /// Controller Vibrate
  359. /// </summary>
  360. /// <param name="index"></param>
  361. /// <param name="isOn"></param>
  362. /// <param name="amplitude"></param>
  363. /// <param name="frequency"></param>
  364. /// <param name="time"></param>
  365. /// <returns></returns>
  366. public static int GSXR_Set_ControllerVibrate(int index, bool isOn, float amplitude, float frequency, float time)
  367. {
  368. if (API_GSXR_Slam.plugin != null)
  369. {
  370. return API_GSXR_Slam.SlamManager.GSXR_Set_ControllerVibrate(index, isOn, amplitude, frequency, time);
  371. }
  372. return -1;
  373. }
  374. [Obsolete("Please Use NEW KS Vibrate GSXR_Set_ControllerVibrate(int index, bool isOn, float amplitude, float frequency, float time)")]
  375. public static int GSXR_Set_ControllerVibrate(int index, bool isOn, int amplitude, int frequency, int time)
  376. {
  377. if (API_GSXR_Slam.plugin != null)
  378. {
  379. return API_GSXR_Slam.SlamManager.GSXR_Set_ControllerVibrate(index, isOn, amplitude/15f, frequency/15f, time/10f);
  380. }
  381. return -1;
  382. }
  383. /// <summary>
  384. /// GSXR_Get_ControllerVibrateState,
  385. /// return:"0" is false ,"1" is true
  386. /// </summary>
  387. /// <param name="index">0-LeftController 1-RightController</param>
  388. /// <returns></returns>
  389. public static int GSXR_Get_ControllerVibrateState(int index)
  390. {
  391. if (API_GSXR_Slam.plugin != null)
  392. {
  393. return API_GSXR_Slam.SlamManager.GSXR_Get_ControllerVibrateState(index);
  394. }
  395. return -1;
  396. }
  397. #endregion
  398. #region Other API
  399. /// <summary>
  400. /// GSXR_Set_OnFloorOrOnHead
  401. /// </summary>
  402. /// <param name="type">0-Head, 1-Floor</param>
  403. public static void GSXR_Set_OnFloorOrOnHead(int type)
  404. {
  405. if (API_GSXR_Slam.plugin != null)
  406. {
  407. API_GSXR_Slam.SlamManager.GSXR_Set_OnFloorOrOnHead(type);
  408. }
  409. }
  410. public static void GSXR_Bind_Controller(int index,int type)
  411. {
  412. if (API_GSXR_Slam.plugin != null)
  413. {
  414. API_GSXR_Slam.SlamManager.GSXR_Bind_Controller(index, type);
  415. }
  416. }
  417. public static void GSXR_Unbind_Controller(int index, int type)
  418. {
  419. if (API_GSXR_Slam.plugin != null)
  420. {
  421. API_GSXR_Slam.SlamManager.GSXR_Unbind_Controller(index, type);
  422. }
  423. }
  424. public static int GSXR_Get_ControllerBondState()
  425. {
  426. if (API_GSXR_Slam.plugin != null)
  427. {
  428. return API_GSXR_Slam.SlamManager.GSXR_Get_ControllerBondState();
  429. }
  430. return -2;
  431. }
  432. /// <summary>
  433. /// Whether to Enable the Controller binding status callback interface
  434. /// </summary>
  435. public static void GSXR_Set_ControllerBondEventCallback(bool isEnable)
  436. {
  437. if (API_GSXR_Slam.plugin != null)
  438. {
  439. API_GSXR_Slam.SlamManager.GSXR_Set_ControllerBondEventCallback(isEnable);
  440. }
  441. }
  442. /// <summary>
  443. /// Add Controller binding state callback event
  444. /// </summary>
  445. /// <param name="callback"></param>
  446. public static void GSXR_Add_ControllerBondEventCallback(Action<int> callback)
  447. {
  448. if (API_GSXR_Slam.plugin != null)
  449. {
  450. API_GSXR_Slam.SlamManager.GSXR_Add_ControllerBondEventCallback(callback);
  451. }
  452. }
  453. /// <summary>
  454. /// Remove Controller binding state callback event
  455. /// </summary>
  456. /// <param name="callback"></param>
  457. public static void GSXR_Remove_ControllerBondEventCallback(Action<int> callback)
  458. {
  459. if (API_GSXR_Slam.plugin != null)
  460. {
  461. API_GSXR_Slam.SlamManager.GSXR_Remove_ControllerBondEventCallback(callback);
  462. }
  463. }
  464. #endregion
  465. #region SeeThrough API
  466. /// <summary>
  467. /// GSXR_StartSeeThrough
  468. /// </summary>
  469. public static void GSXR_StartSeeThrough()
  470. {
  471. if (API_GSXR_Slam.plugin != null)
  472. {
  473. API_GSXR_Slam.plugin.GSXR_StartSeeThrough();
  474. }
  475. }
  476. /// <summary>
  477. /// GSXR_StopSeeThrough
  478. /// </summary>
  479. public static void GSXR_StopSeeThrough()
  480. {
  481. if (API_GSXR_Slam.plugin != null)
  482. {
  483. API_GSXR_Slam.plugin.GSXR_StopSeeThrough();
  484. }
  485. }
  486. public static void GSXR_Add_SeeThrough_Callback(Action<bool> callback)
  487. {
  488. if (API_GSXR_Slam.plugin != null)
  489. {
  490. API_GSXR_Slam.plugin.GSXR_Regist_SeeThrough_Callback(callback);
  491. }
  492. }
  493. public static void GSXR_Remove_SeeThrough_Callback(Action<bool> callback)
  494. {
  495. if (API_GSXR_Slam.plugin != null)
  496. {
  497. API_GSXR_Slam.plugin.GSXR_UnRegist_SeeThrough_Callbak(callback);
  498. }
  499. }
  500. #endregion
  501. /// <summary>
  502. /// GSXR_Add_SlamPauseCallback
  503. /// </summary>
  504. /// <param name="callback"></param>
  505. public static void GSXR_Add_SlamPauseCallback(GSXRManager.OnApplicationPauseDele callback)
  506. {
  507. GSXRManager.onApplicationPauseDele += callback;
  508. }
  509. /// <summary>
  510. ///
  511. /// </summary>
  512. /// <param name="callback"></param>
  513. public static void GSXR_Remove_SlamPauseCallback(GSXRManager.OnApplicationPauseDele callback)
  514. {
  515. GSXRManager.onApplicationPauseDele -= callback;
  516. }
  517. }