SlamApi.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Assets.NXR.Scripts.Slam;
  4. using UnityEngine;
  5. using Nxr.Internal;
  6. using NibiruTask;
  7. using System;
  8. using System.Runtime.InteropServices;
  9. using System.Threading;
  10. public class SlamApi : MonoBehaviour
  11. {
  12. enum TestMode
  13. {
  14. TM_Map,
  15. TM_Camera,
  16. TM_PlaneDetection
  17. }
  18. public struct MapStatus
  19. {
  20. public bool refresh;
  21. public int status;
  22. public int quality; // 0~100
  23. public float percent; // map match
  24. }
  25. public TextMesh CameraTextTip;
  26. public TextMesh MapTextTip;
  27. public TextMesh PlaneDetectionTip;
  28. public MeshRenderer ThermalCameraPreview;
  29. public MeshRenderer RgbCameraPreview;
  30. public MeshRenderer TofCameraPreview;
  31. public MeshRenderer FishEyeLeftCameraPreview;
  32. public MeshRenderer FishEyeRightCameraPreview;
  33. Texture2D RgbCamTexture2D_RGB;
  34. IntPtr[] RgbCameraDataPtrArray = new IntPtr[2] {IntPtr.Zero, IntPtr.Zero};
  35. public GameObject MapTestObj;
  36. public GameObject CameraTestObj;
  37. public GameObject PlaneDetectionTestObj;
  38. Texture2D ThermalCamTexture2D_Y;
  39. Texture2D ThermalCamTexture2D_U;
  40. Texture2D ThermalCamTexture2D_V;
  41. Texture2D TofCamTexture2D;
  42. Texture2D FishEyeLeftCamTexture2D;
  43. Texture2D FishEyeRightCamTexture2D;
  44. Color[] FishEyeLeftColors;
  45. Color[] FishEyeRightColors;
  46. TestMode CurTestMode = TestMode.TM_Map;
  47. Dictionary<NxrSlam.RawDataType, Queue<NxrSlam.NXRStreamData>> StreamDataDict =
  48. new Dictionary<NxrSlam.RawDataType, Queue<NxrSlam.NXRStreamData>>();
  49. Queue<NxrSlam.NXRPlaneData> PlaneDataQueue = new Queue<NxrSlam.NXRPlaneData>();
  50. Dictionary<int, NxrSlamPlane> PlaneDict = new Dictionary<int, NxrSlamPlane>();
  51. MapStatus mapStatus;
  52. public GameObject MapObjects;
  53. public void ShowMapTest()
  54. {
  55. CurTestMode = TestMode.TM_Map;
  56. MapTestObj.SetActive(true);
  57. CameraTestObj.SetActive(false);
  58. PlaneDetectionTestObj.SetActive(false);
  59. }
  60. public void ShowCameraTest()
  61. {
  62. CurTestMode = TestMode.TM_Camera;
  63. MapTestObj.SetActive(false);
  64. CameraTestObj.SetActive(true);
  65. PlaneDetectionTestObj.SetActive(false);
  66. }
  67. public void ShowPlaneDetectionTest()
  68. {
  69. CurTestMode = TestMode.TM_PlaneDetection;
  70. MapTestObj.SetActive(false);
  71. CameraTestObj.SetActive(false);
  72. PlaneDetectionTestObj.SetActive(true);
  73. }
  74. public void StartPlaneDetection()
  75. {
  76. PlaneDetectionTip.text = "等待检测结果...";
  77. Debug.Log("StartPlaneDetection");
  78. NxrSlam.Instance.StartPlaneDetection();
  79. }
  80. public void StopPlaneDetection()
  81. {
  82. PlaneDetectionTip.text = "请开始平面检测...";
  83. Debug.Log("StopPlaneDetection");
  84. NxrSlam.Instance.StopPlaneDetection();
  85. }
  86. string MapBinPath;
  87. public void BuildMap()
  88. {
  89. MapBinPath = Nxr.Internal.NxrViewer.Instance.GetStoragePath() + "/unity_map.bin";
  90. Debug.Log("BuildMap." + MapBinPath);
  91. NxrSlam.Instance.StartBuildMap();
  92. }
  93. public void FinishMap()
  94. {
  95. Debug.Log("FinishMap");
  96. // 记录地图原点
  97. Vector3 orginalVec = Camera.main.transform.position;
  98. PlayerPrefs.SetFloat("map_original_x", orginalVec.x);
  99. PlayerPrefs.SetFloat("map_original_y", orginalVec.y);
  100. PlayerPrefs.SetFloat("map_original_z", orginalVec.z);
  101. Debug.Log("Save Map Original Position : " + orginalVec.x + "," + orginalVec.y + "," + orginalVec.z);
  102. PlayerPrefs.Save();
  103. NxrSlam.Instance.StopBuildMap(MapBinPath);
  104. }
  105. public void ReadMap()
  106. {
  107. MapBinPath = Nxr.Internal.NxrViewer.Instance.GetStoragePath() + "/unity_map.bin";
  108. Debug.Log("ReadMap." + MapBinPath);
  109. // NxrSlam.Instance.StopSlam();
  110. NxrSlam.Instance.StartSlamWithMap(MapBinPath);
  111. }
  112. bool MapObjectsShowing = false;
  113. public void PutObjectInMap()
  114. {
  115. MapObjectsShowing = !MapObjectsShowing;
  116. Debug.Log("MapObjectsShowing." + MapObjectsShowing);
  117. MapObjects.SetActive(MapObjectsShowing);
  118. Vector3 mapOriginalVec;
  119. mapOriginalVec.x = PlayerPrefs.GetFloat("map_original_x");
  120. mapOriginalVec.y = PlayerPrefs.GetFloat("map_original_y");
  121. mapOriginalVec.z = PlayerPrefs.GetFloat("map_original_z");
  122. Debug.Log("Load Map Original Position : " + mapOriginalVec.x + "," + mapOriginalVec.y + "," + mapOriginalVec.z);
  123. MapObjects.transform.position = mapOriginalVec;
  124. }
  125. public void THERMALCamera()
  126. {
  127. Debug.Log("THERMALCamera");
  128. NxrSlam.Instance.StartStreaming(NxrSlam.RawDataType.THERMAL);
  129. }
  130. public void RGBCamera()
  131. {
  132. Debug.Log("RGBCamera");
  133. NxrSlam.Instance.StartStreaming(NxrSlam.RawDataType.RGB);
  134. }
  135. public void TOFCamera()
  136. {
  137. Debug.Log("TOFCamera");
  138. NxrSlam.Instance.SetTofStreamMode(NxrSlam.TOFStreamMode.ONLY_DEPTH);
  139. NxrSlam.Instance.StartStreaming(NxrSlam.RawDataType.TOF);
  140. }
  141. public void FISHEYECamera()
  142. {
  143. Debug.Log("FISHEYECamera");
  144. NxrSlam.Instance.StartStreaming(NxrSlam.RawDataType.FISHEYE);
  145. }
  146. public void StopCamera()
  147. {
  148. Debug.Log("StopCamera");
  149. NxrSlam.Instance.StopStreaming();
  150. }
  151. // Start is called before the first frame update
  152. void Start()
  153. {
  154. NxrSlam.Instance.Init();
  155. NxrSlam.Instance.EnableNativeLog(false);
  156. NxrSlam.Instance.MapStatusAndQualityEvent = OnMapStatusAndQualityEvent;
  157. NxrSlam.Instance.MapPercentEvent = OnMapPercent;
  158. NxrSlam.Instance.StreamDataEvent = OnStreamDataEvent;
  159. NxrSlam.Instance.PlaneDetectionEvent = OnPlaneDetectionEvent;
  160. mapStatus.refresh = false;
  161. StreamDataDict[NxrSlam.RawDataType.EYETRACKING] = new Queue<NxrSlam.NXRStreamData>();
  162. StreamDataDict[NxrSlam.RawDataType.RGB] = new Queue<NxrSlam.NXRStreamData>();
  163. StreamDataDict[NxrSlam.RawDataType.TOF] = new Queue<NxrSlam.NXRStreamData>();
  164. StreamDataDict[NxrSlam.RawDataType.FISHEYE] = new Queue<NxrSlam.NXRStreamData>();
  165. StreamDataDict[NxrSlam.RawDataType.THERMAL] = new Queue<NxrSlam.NXRStreamData>();
  166. Thread thread = new Thread(ShowFishEyeImage);
  167. thread.Start();
  168. }
  169. void OnPlaneDetectionEvent(NxrSlam.NXRPlaneData data)
  170. {
  171. //Debug.Log("plane data id=" + data.id + ",normal : x=" + data.normal.x + ",y=" + data.normal.y + ",z=" + data.normal.z
  172. // + ",pointes size=" + data.size);
  173. //for (int i = 0; i < data.size; i++)
  174. //{
  175. // Debug.Log(data.id +"," + i + ".Point=" + data.points[i].x + "," + data.points[i].y + "," + data.points[i].z);
  176. //}
  177. while (PlaneDataQueue.Count > 10)
  178. {
  179. PlaneDataQueue.Dequeue();
  180. }
  181. PlaneDataQueue.Enqueue(data);
  182. }
  183. void OnStreamDataEvent(NxrSlam.NXRStreamData data)
  184. {
  185. // data=RGB,type=0,1280X720,size=1382400,format=YUV420p
  186. // data=TOF,type=3,224X172,size=462336,format=YUYV
  187. // data=FISHEYE,type=0,640X400,size = 0,format = YUYV,left = 256000,right = 256000
  188. //Debug.Log("stream data=" + data.rawDataType.ToString() + ",type=" + data.type + ",size=" + data.width + "X" + data.height
  189. // + ",size=" + data.size + ",format=" + data.rgbCodecFormat + ",left=" + data.leftSize + ",right=" + data.rightSize);
  190. if (data.size > 0 || data.leftSize > 0 || data.rightSize > 0)
  191. {
  192. if (StreamDataDict[data.rawDataType].Count > 30)
  193. {
  194. StreamDataDict[data.rawDataType].Clear();
  195. }
  196. if (data.rawDataType == NxrSlam.RawDataType.RGB)
  197. {
  198. lock (rgbCameraDataLock)
  199. {
  200. StreamDataDict[data.rawDataType].Enqueue(data);
  201. }
  202. RgbFpsCheck();
  203. }
  204. else
  205. {
  206. StreamDataDict[data.rawDataType].Enqueue(data);
  207. }
  208. }
  209. }
  210. void OnMapStatusAndQualityEvent(int status, int quality)
  211. {
  212. mapStatus.refresh = true;
  213. mapStatus.status = status;
  214. mapStatus.quality = quality;
  215. }
  216. void OnMapPercent(float percentage)
  217. {
  218. mapStatus.refresh = true;
  219. mapStatus.percent = percentage;
  220. }
  221. public bool TestPoints = false;
  222. DateTime RgbFpsPrintTime = DateTime.Now;
  223. private int RgbFpsCount = 0;
  224. private void RgbFpsCheck()
  225. {
  226. TimeSpan diffSpan = new TimeSpan(DateTime.Now.Ticks - RgbFpsPrintTime.Ticks);
  227. if (diffSpan.TotalMilliseconds > 900)
  228. {
  229. RgbFpsPrintTime = DateTime.Now;
  230. Debug.LogError("Rgb Camera Frame Rate : " + RgbFpsCount);
  231. RgbFpsCount = 0;
  232. }
  233. RgbFpsCount++;
  234. }
  235. private readonly object rgbCameraDataLock = new object();
  236. void OnEnable()
  237. {
  238. StartCoroutine("EndOfFrame");
  239. }
  240. void OnDisable()
  241. {
  242. StopCoroutine("EndOfFrame");
  243. }
  244. IEnumerator EndOfFrame()
  245. {
  246. while (true)
  247. {
  248. yield return new WaitForEndOfFrame();
  249. //
  250. // ProcessRGBCameraData();
  251. }
  252. }
  253. private void ProcessRGBCameraData()
  254. {
  255. Queue<NxrSlam.NXRStreamData> rgbQueue = null;
  256. lock (rgbCameraDataLock)
  257. {
  258. rgbQueue = StreamDataDict[NxrSlam.RawDataType.RGB];
  259. }
  260. if (rgbQueue != null && rgbQueue.Count > 0)
  261. {
  262. // tail
  263. NxrSlam.NXRStreamData data = rgbQueue.ToArray()[rgbQueue.Count - 1];
  264. if (data.size > 0)
  265. {
  266. if (RgbCamTexture2D_RGB == null)
  267. {
  268. Debug.Log("Create Texture2D : data=" + data.size + "," + data.width + "," + data.height + "," +
  269. data.data.Length);
  270. RgbCamTexture2D_RGB = new Texture2D(data.width, data.height, TextureFormat.RGB24, false);
  271. RgbCameraPreview.material.SetTexture("_MainTex", RgbCamTexture2D_RGB);
  272. }
  273. // Debug.Log("data=" + data.size + "," + data.width + "," + data.height + "," + data.data.Length);
  274. RgbCamTexture2D_RGB.LoadRawTextureData(data.data);
  275. RgbCamTexture2D_RGB.Apply();
  276. }
  277. }
  278. if (NxrSlam.Instance.IsRGBCameraSteaming)
  279. {
  280. int size = NxrSlam.getNxrStreamSize((int) NxrSlam.RawDataType.RGB);
  281. if (size > 0 && RgbCameraDataPtrArray[0] == IntPtr.Zero)
  282. {
  283. RgbCameraDataPtrArray[0] = Marshal.AllocHGlobal(size);
  284. RgbCameraDataPtrArray[1] = Marshal.AllocHGlobal(size);
  285. bool success = NxrSlam.initPtr((int) NxrSlam.RawDataType.RGB, RgbCameraDataPtrArray[0],
  286. RgbCameraDataPtrArray[1]);
  287. if (!success)
  288. {
  289. Debug.LogError("NxrSlam.initPtr Failed !!!");
  290. }
  291. }
  292. if (size > 0)
  293. {
  294. int width = 0;
  295. int height = 0;
  296. //DateTime startTime = DateTime.Now;
  297. int validPtrIndex = NxrSlam.getNxrStreamData((int) NxrSlam.RawDataType.RGB, ref width, ref height);
  298. //TimeSpan diffSpan = DateTime.Now.Subtract(startTime);
  299. //Debug.LogError("getNxrStreamData:" + width + "X" + height + "," + size + "," + validPtrIndex + "," + diffSpan.TotalMilliseconds);
  300. if (width > 0 && height > 0 && RgbCamTexture2D_RGB == null)
  301. {
  302. RgbCamTexture2D_RGB = new Texture2D(width, height, TextureFormat.RGB24, false);
  303. RgbCameraPreview.material.SetTexture("_MainTex", RgbCamTexture2D_RGB);
  304. }
  305. if (validPtrIndex >= 0 && RgbCamTexture2D_RGB != null)
  306. {
  307. RgbCamTexture2D_RGB.LoadRawTextureData(RgbCameraDataPtrArray[validPtrIndex], size);
  308. RgbCamTexture2D_RGB.Apply();
  309. NxrSlam.unlockStream(validPtrIndex);
  310. }
  311. }
  312. }
  313. }
  314. // Update is called once per frame
  315. void Update()
  316. {
  317. if (TestPoints)
  318. {
  319. TestPoints = false;
  320. DebugPoints();
  321. }
  322. if (mapStatus.refresh)
  323. {
  324. mapStatus.refresh = false;
  325. MapTextTip.text = "地图质量:" + mapStatus.quality + ",地图匹配度(0~100):" + (int) (mapStatus.percent * 100);
  326. }
  327. if (NxrInput.GetKeyUp(CKeyEvent.KEYCODE_BACK) ||
  328. NxrInput.GetControllerKeyUp(CKeyEvent.KEYCODE_CONTROLLER_MENU) ||
  329. NxrInput.GetControllerKeyUp(CKeyEvent.KEYCODE_CONTROLLER_MENU,
  330. InteractionManager.NACTION_HAND_TYPE.HAND_LEFT) ||
  331. NxrInput.GetControllerKeyUp(CKeyEvent.KEYCODE_CONTROLLER_MENU,
  332. InteractionManager.NACTION_HAND_TYPE.HAND_RIGHT))
  333. {
  334. // 返回键
  335. if (Application.isMobilePlatform) Application.Quit();
  336. }
  337. // THERMAL
  338. Queue<NxrSlam.NXRStreamData> thermalQueue = StreamDataDict[NxrSlam.RawDataType.THERMAL];
  339. if (thermalQueue.Count > 0)
  340. {
  341. NxrSlam.NXRStreamData data = thermalQueue.Dequeue();
  342. if (data.size > 0)
  343. {
  344. if (ThermalCamTexture2D_Y == null)
  345. {
  346. //yuv422
  347. ThermalCamTexture2D_Y = new Texture2D(data.width, data.height, TextureFormat.Alpha8, false);
  348. ThermalCamTexture2D_U = new Texture2D(data.width >> 1, data.height, TextureFormat.Alpha8, false);
  349. ThermalCamTexture2D_V = new Texture2D(data.width >> 1, data.height, TextureFormat.Alpha8, false);
  350. ThermalCameraPreview.material.SetTexture("_MainTex", ThermalCamTexture2D_Y);
  351. ThermalCameraPreview.material.SetTexture("_UTex", ThermalCamTexture2D_U);
  352. ThermalCameraPreview.material.SetTexture("_VTex", ThermalCamTexture2D_V);
  353. }
  354. byte[] dataY = null;
  355. byte[] dataU = null;
  356. byte[] dataV = null;
  357. NxrSlam.Instance.LoadYUV(data.data, data.width, data.height, ref dataY, ref dataU, ref dataV,
  358. NxrSlam.Codec.YUYV);
  359. ThermalCamTexture2D_Y.LoadRawTextureData(dataY);
  360. ThermalCamTexture2D_U.LoadRawTextureData(dataU);
  361. ThermalCamTexture2D_V.LoadRawTextureData(dataV);
  362. ThermalCamTexture2D_Y.Apply();
  363. ThermalCamTexture2D_U.Apply();
  364. ThermalCamTexture2D_V.Apply();
  365. }
  366. }
  367. // RGB
  368. ProcessRGBCameraData();
  369. // TOF
  370. Queue<NxrSlam.NXRStreamData> tofQueue = StreamDataDict[NxrSlam.RawDataType.TOF];
  371. if (tofQueue.Count > 0)
  372. {
  373. NxrSlam.NXRStreamData data = tofQueue.Dequeue();
  374. // Debug.Log("tofQueue=" + tofQueue.Count + ",size=" + data.size + ",w=" + data.width + ",h=" + data.height);
  375. if (data.size > 0)
  376. {
  377. if (TofCamTexture2D == null)
  378. {
  379. TofCamTexture2D = new Texture2D(data.width, data.height, TextureFormat.RGB565, false);
  380. TofCameraPreview.material.SetTexture("_MainTex", TofCamTexture2D);
  381. }
  382. // 224*172*3*4
  383. float dmax = 7.5f;
  384. //Color[] colors = new Color[data.width* data.height];
  385. int dIndex = 0;
  386. for (int i = 0; i < data.size; i++)
  387. {
  388. if (i % 4 == 0)
  389. {
  390. float d = System.BitConverter.ToSingle(data.data, i);
  391. int u = (int) Mathf.Max(0, Mathf.Min(255.0f, d * 255.0f / dmax));
  392. // RGB = R + G * 256 + B * 256 * 256
  393. Color pixelsColor = NxrSlam.Instance.Int_To_RGB(u);
  394. //colors[dIndex] = pixelsColor;
  395. int x = dIndex % data.width;
  396. int y = dIndex / data.width;
  397. // 上下翻转
  398. TofCamTexture2D.SetPixel(x, data.height - y, pixelsColor);
  399. dIndex++;
  400. }
  401. }
  402. //TofCamTexture2D.SetPixels(0, 0, data.width, data.height, colors);
  403. TofCamTexture2D.Apply();
  404. }
  405. }
  406. // FishEye
  407. Queue<NxrSlam.NXRStreamData> fishEyeQueue = StreamDataDict[NxrSlam.RawDataType.FISHEYE];
  408. if (fishEyeQueue.Count > 0)
  409. {
  410. if (!isStartConvert)
  411. {
  412. NxrSlam.NXRStreamData data = fishEyeQueue.Dequeue();
  413. _nxrStreamData = data;
  414. if (data.leftSize > 0 && data.rightSize > 0)
  415. {
  416. if (FishEyeLeftCamTexture2D == null)
  417. {
  418. FishEyeLeftCamTexture2D = new Texture2D(data.width, data.height, TextureFormat.RGB24, false);
  419. FishEyeRightCamTexture2D = new Texture2D(data.width, data.height, TextureFormat.RGB24, false);
  420. FishEyeLeftCameraPreview.material.SetTexture("_MainTex", FishEyeLeftCamTexture2D);
  421. FishEyeRightCameraPreview.material.SetTexture("_MainTex", FishEyeRightCamTexture2D);
  422. FishEyeLeftColors = new Color[data.leftSize];
  423. FishEyeRightColors = new Color[data.rightSize];
  424. }
  425. }
  426. isStartConvert = true;
  427. }
  428. }
  429. if (isConverted)
  430. {
  431. FishEyeLeftCamTexture2D.SetPixels(FishEyeLeftColors);
  432. FishEyeLeftCamTexture2D.Apply();
  433. FishEyeRightCamTexture2D.SetPixels(FishEyeRightColors);
  434. FishEyeRightCamTexture2D.Apply();
  435. isConverted = false;
  436. }
  437. // Plane Detection
  438. if (PlaneDataQueue.Count > 0)
  439. {
  440. NxrSlam.NXRPlaneData data = PlaneDataQueue.Dequeue();
  441. if (PlaneDict.ContainsKey(data.id))
  442. {
  443. PlaneDict[data.id].MakePlane(data);
  444. }
  445. else
  446. {
  447. PlaneDict[data.id] = NxrSlam.Instance.GeneratePlaneObject(data);
  448. }
  449. string planeTip = "平面检测成功: \n";
  450. foreach (NxrSlamPlane plane in PlaneDict.Values)
  451. {
  452. planeTip += "[Id." + plane.PlaneId + "] 共 " + plane.PlanePointsCount + "个点\n";
  453. }
  454. PlaneDetectionTip.text = planeTip;
  455. }
  456. }
  457. private bool isRunning = true;
  458. private bool isStartConvert = false;
  459. private bool isConverted = false;
  460. private NxrSlam.NXRStreamData _nxrStreamData;
  461. void ShowFishEyeImage()
  462. {
  463. while (isRunning)
  464. {
  465. if (isStartConvert && !isConverted)
  466. {
  467. if (_nxrStreamData != null && _nxrStreamData.leftSize > 0 && _nxrStreamData.rightSize > 0)
  468. {
  469. var data = _nxrStreamData;
  470. for (int i = 0; i < data.leftSize; i++)
  471. {
  472. var dataLeft = data.left[i] / 255.0f;
  473. var dataRight = data.right[i] / 255.0f;
  474. FishEyeLeftColors[i].r = dataLeft;
  475. FishEyeLeftColors[i].g = dataLeft;
  476. FishEyeLeftColors[i].b = dataLeft;
  477. FishEyeRightColors[i].r = dataRight;
  478. FishEyeRightColors[i].g = dataRight;
  479. FishEyeRightColors[i].b = dataRight;
  480. }
  481. isStartConvert = false;
  482. isConverted = true;
  483. }
  484. }
  485. Thread.Sleep(10);
  486. }
  487. }
  488. private void OnDestroy()
  489. {
  490. if (RgbCameraDataPtrArray[0] != IntPtr.Zero)
  491. {
  492. Marshal.FreeHGlobal(RgbCameraDataPtrArray[0]);
  493. Marshal.FreeHGlobal(RgbCameraDataPtrArray[1]);
  494. }
  495. NxrSlam.Instance.UnInit();
  496. Debug.Log("OnDestroy");
  497. isRunning = false;
  498. isStartConvert = false;
  499. isConverted = false;
  500. }
  501. private void OnApplicationPause(bool pause)
  502. {
  503. Debug.Log("OnApplicationPause=" + pause);
  504. if (pause) NxrSlam.Instance.Pause();
  505. }
  506. private void OnApplicationQuit()
  507. {
  508. NxrSlam.Instance.UnInit();
  509. Debug.Log("OnApplicationQuit");
  510. }
  511. void DebugPoints()
  512. {
  513. NxrSlam.NXRPlaneData testdata = new NxrSlam.NXRPlaneData(null);
  514. testdata.id = 1;
  515. testdata.points = new NxrSlam.NXRPlaneData.Point3D[]
  516. {
  517. /*
  518. new NxrSlam.NXRPlaneData.Point3D(0.597963809967041,-0.483312696218491,0.43642520904541),
  519. new NxrSlam.NXRPlaneData.Point3D(0.448058247566223,-0.483312696218491,0.369752883911133),
  520. new NxrSlam.NXRPlaneData.Point3D(0.32343664765358,-0.483312696218491,0.323436379432678),
  521. new NxrSlam.NXRPlaneData.Point3D(0.3141910135746,-0.483312696218491,0.323615431785584),
  522. new NxrSlam.NXRPlaneData.Point3D(0.0990831553936005,-0.483312696218491,0.33366933465004),
  523. new NxrSlam.NXRPlaneData.Point3D(0.0974671021103859,-0.483312696218491,0.333752363920212),
  524. new NxrSlam.NXRPlaneData.Point3D(0.0561475902795792,-0.483312696218491,0.337889224290848),
  525. new NxrSlam.NXRPlaneData.Point3D(-0.465384006500244,-0.483312696218491,0.51269006729126),
  526. new NxrSlam.NXRPlaneData.Point3D(-0.232375964522362,-0.483312696218491,0.975189507007599),
  527. new NxrSlam.NXRPlaneData.Point3D(0.267775535583496,-0.483312696218491,0.737699329853058),
  528. new NxrSlam.NXRPlaneData.Point3D(0.360924899578094,-0.483312696218491,0.65373432636261),
  529. new NxrSlam.NXRPlaneData.Point3D(0.597963809967041,-0.483312696218491,0.43642520904541)
  530. */
  531. // II
  532. new NxrSlam.NXRPlaneData.Point3D(1.0432962179184, -0.302729845046997, 0.245670855045319),
  533. new NxrSlam.NXRPlaneData.Point3D(0.599149584770203, -0.302729845046997, 0.168545603752136),
  534. new NxrSlam.NXRPlaneData.Point3D(-0.248820841312408, -0.302729845046997, 0.573924124240875),
  535. new NxrSlam.NXRPlaneData.Point3D(-0.401118278503418, -0.302729845046997, 0.789052367210388),
  536. new NxrSlam.NXRPlaneData.Point3D(-0.450264513492584, -0.302729845046997, 0.886043250560761),
  537. new NxrSlam.NXRPlaneData.Point3D(-0.450264513492584, -0.302729845046997, 0.886043250560761),
  538. new NxrSlam.NXRPlaneData.Point3D(-0.0576245971024036, -0.302729845046997, 1.1546128988266),
  539. new NxrSlam.NXRPlaneData.Point3D(0.103346958756447, -0.302729845046997, 1.081174492836),
  540. new NxrSlam.NXRPlaneData.Point3D(0.836297452449799, -0.302729845046997, 0.65020763874054),
  541. new NxrSlam.NXRPlaneData.Point3D(1.04192793369293, -0.302729845046997, 0.37995707988739),
  542. new NxrSlam.NXRPlaneData.Point3D(1.0432962179184, -0.302729845046997, 0.245670855045319)
  543. //III
  544. //new NxrSlam.NXRPlaneData.Point3D(1.3766975402832,-0.298782527446747,-0.534084439277649),
  545. //new NxrSlam.NXRPlaneData.Point3D(-0.277698934078217,-0.298782527446747,-0.0697378441691399),
  546. //new NxrSlam.NXRPlaneData.Point3D(-0.531768798828125,-0.298782527446747,0.118963070213795),
  547. //new NxrSlam.NXRPlaneData.Point3D(-0.630833983421326,-0.298782527446747,0.548060476779938),
  548. //new NxrSlam.NXRPlaneData.Point3D(-0.165737017989159,-0.298782527446747,0.634526371955872),
  549. //new NxrSlam.NXRPlaneData.Point3D(0.930005371570587,-0.298782527446747,0.364291131496429),
  550. //new NxrSlam.NXRPlaneData.Point3D(0.97565633058548,-0.298782527446747,0.314914882183075),
  551. //new NxrSlam.NXRPlaneData.Point3D(1.14742374420166,-0.298782527446747,0.0324256010353565),
  552. //new NxrSlam.NXRPlaneData.Point3D(1.34535908699036,-0.298782527446747,-0.367374181747437)
  553. };
  554. testdata.size = testdata.points.Length;
  555. testdata.Refresh();
  556. PlaneDataQueue.Enqueue(testdata);
  557. }
  558. }