API_GSXR_Slam.cs 16 KB

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