Tracking3DManager.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using EZXR.Glass;
  2. using EZXR.Glass.Core;
  3. using EZXR.Glass.Device;
  4. using EZXR.Glass.SixDof;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.Runtime.InteropServices;
  9. using UnityEngine;
  10. namespace EZXR.Glass.Tracking3D
  11. {
  12. public class Tracking3DManager : MonoBehaviour
  13. {
  14. public bool IsLocationSuccess { get { return isLocationSuccess; } }
  15. private bool isLocationSuccess;
  16. public Pose AnchorPose
  17. {
  18. get
  19. {
  20. return _anchorPose;
  21. }
  22. }
  23. private Pose _anchorPose;
  24. private EZXRTrack3dSession Track3dsession;
  25. private NormalRGBCameraDevice rgbCameraDevice;
  26. private EZVIOInputImage locCamImageBuffer;
  27. private float[] locCamIntriarray = new float[8];
  28. private OSTMatrices ost_m = new OSTMatrices();
  29. private bool hasIntricsSet = false;
  30. private bool hasPoseOffsetSet = false;
  31. private string algAssetPath = "";
  32. // Start is called before the first frame update
  33. void Start()
  34. {
  35. }
  36. private void OnEnable()
  37. {
  38. StartCoroutine(startRGBCamera());
  39. }
  40. private void OnDisable()
  41. {
  42. stopRGBCamera();
  43. stopTrackSession();
  44. }
  45. // Update is called once per frame
  46. void Update()
  47. {
  48. }
  49. public void enableTracking(string path)
  50. {
  51. algAssetPath = path;
  52. stopTrackSession();
  53. StartCoroutine(startTrackSession());
  54. }
  55. private IEnumerator startRGBCamera()
  56. {
  57. yield return new WaitUntil(() => SessionManager.Instance != null && SessionManager.Instance.IsInited);
  58. hasIntricsSet = false;
  59. hasPoseOffsetSet = false;
  60. Debug.Log("-10001-startRGBCamera new rgbCameraDevice Open");
  61. locCamImageBuffer = new EZVIOInputImage();
  62. rgbCameraDevice = new NormalRGBCameraDevice();
  63. rgbCameraDevice.Open();
  64. }
  65. private void stopRGBCamera()
  66. {
  67. if (rgbCameraDevice != null)
  68. rgbCameraDevice.Close();
  69. if (locCamImageBuffer.fullImg != IntPtr.Zero)
  70. {
  71. Marshal.FreeHGlobal(locCamImageBuffer.fullImg);
  72. locCamImageBuffer.fullImg = IntPtr.Zero;
  73. }
  74. }
  75. private IEnumerator startTrackSession()
  76. {
  77. yield return new WaitUntil(() => rgbCameraDevice.IsStarted());
  78. Track3dsession = new EZXRTrack3dSession();
  79. Debug.Log("-10001-startTrackSession Track3dsession.Create");
  80. if (Track3dsession != null)
  81. {
  82. Track3dsession.Create(Application.persistentDataPath + "/" + algAssetPath);
  83. }
  84. InvokeRepeating("UpdateCameraImage", 0.5f, 3.0f);
  85. Debug.Log("-10001-startTrackSession end");
  86. }
  87. private void stopTrackSession()
  88. {
  89. Debug.Log("-10001-stopTrackSession");
  90. hasPoseOffsetSet = false;
  91. hasIntricsSet = false;
  92. if (Track3dsession != null)
  93. {
  94. Track3dsession.Destroy();
  95. Track3dsession = null;
  96. }
  97. CancelInvoke("UpdateCameraImage");
  98. }
  99. private void UpdateCameraImage()
  100. {
  101. if (ARFrame.SessionStatus != EZVIOState.EZVIOCameraState_Tracking)
  102. return;
  103. if (rgbCameraDevice == null)
  104. {
  105. Debug.Log("rgbCameraDevice is null");
  106. return;
  107. }
  108. if (Track3dsession == null)
  109. {
  110. Debug.Log("Track3dsession is null");
  111. return;
  112. }
  113. if (!hasPoseOffsetSet)
  114. {
  115. NativeTracking.GetOSTParams(ref ost_m);
  116. Track3dsession.SetPoseFromHeadToLocCam(ost_m.T_RGB_Head);
  117. hasPoseOffsetSet = true;
  118. }
  119. bool res = false;
  120. //Debug.Log("call getCurrentRGBImage");
  121. res = rgbCameraDevice.getCurrentImage(ref locCamImageBuffer, locCamIntriarray);
  122. //Debug.Log("call getCurrentRGBImage " + res);
  123. if (res)
  124. {
  125. if (!hasIntricsSet)
  126. {
  127. //Debug.Log("-10001-UpdateCameraImage Track3dsession.UpdateRGBCameraIntrics");
  128. Track3dsession.SetCameraIntrics(locCamIntriarray);
  129. hasIntricsSet = true;
  130. }
  131. double timestamp_sec = locCamImageBuffer.timestamp;
  132. Pose imageTsHeadPose = ARFrame.GetHistoricalHeadPose(timestamp_sec);
  133. Track3dsession.UpdateHeadPose(imageTsHeadPose, timestamp_sec);
  134. int format = (int)locCamImageBuffer.imgFormat;
  135. //Debug.Log("-10001-UpdateRgbFrame Track3dsession.UpdateRGBImage:"+ rgbImageBuffer.fullImg+","+timestamp_sec.ToString("f3"));
  136. //Debug.Log("-10001-UpdateRgbFrame Track3dsession.UpdateRGBImage:" + locCamImageBuffer.imgRes.width + "," + locCamImageBuffer.imgRes.height + ",fotmat=" + locCamImageBuffer.imgFormat);
  137. Track3dsession.UpdateImage(locCamImageBuffer.fullImg, timestamp_sec, (int)locCamImageBuffer.imgRes.width, (int)locCamImageBuffer.imgRes.height, format);
  138. }
  139. Pose pose = Pose.identity;
  140. bool isRelocSucc = Track3dsession.GettTrackedAnchorPose(ref pose);
  141. if (isRelocSucc)
  142. {
  143. _anchorPose = pose;
  144. isLocationSuccess = true;
  145. }
  146. }
  147. }
  148. }