NRTrackingModeChangedListener.cs 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal
  10. {
  11. using System;
  12. using System.Collections;
  13. using UnityEngine;
  14. public class NRTrackingModeChangedListener
  15. {
  16. public delegate void OnTrackStateChangedDel(bool trackChanging, RenderTexture leftRT, RenderTexture rightRT);
  17. public event OnTrackStateChangedDel OnTrackStateChanged;
  18. private NRTrackingModeChangedTip m_LostTrackingTip;
  19. private Coroutine m_EnableRenderCamera;
  20. private Coroutine m_DisableRenderCamera;
  21. private const float MinTimeLastLimited = 0;
  22. private const float MaxTimeLastLimited = 6f;
  23. public NRTrackingModeChangedListener()
  24. {
  25. NRHMDPoseTracker.OnChangeTrackingMode += OnChangeTrackingMode;
  26. }
  27. private void OnChangeTrackingMode(NRHMDPoseTracker.TrackingType origin, NRHMDPoseTracker.TrackingType target)
  28. {
  29. NRDebugger.Info("[NRTrackingModeChangedListener] OnChangeTrackingMode origin:{0} target:{1}", origin, target);
  30. if (target == NRHMDPoseTracker.TrackingType.Tracking0Dof || target == NRHMDPoseTracker.TrackingType.Tracking0DofStable)
  31. {
  32. return;
  33. }
  34. if (m_EnableRenderCamera != null)
  35. {
  36. NRKernalUpdater.Instance.StopCoroutine(m_EnableRenderCamera);
  37. m_EnableRenderCamera = null;
  38. }
  39. m_EnableRenderCamera = NRKernalUpdater.Instance.StartCoroutine(EnableTrackingInitializingRenderCamera());
  40. }
  41. public IEnumerator EnableTrackingInitializingRenderCamera()
  42. {
  43. if (m_LostTrackingTip == null)
  44. {
  45. m_LostTrackingTip = NRTrackingModeChangedTip.Create();
  46. }
  47. m_LostTrackingTip.gameObject.SetActive(true);
  48. var reason = NRFrame.LostTrackingReason;
  49. m_LostTrackingTip.SetMessage(NativeConstants.TRACKING_MODE_SWITCH_TIP);
  50. float begin_time = Time.realtimeSinceStartup;
  51. var endofFrame = new WaitForEndOfFrame();
  52. yield return endofFrame;
  53. yield return endofFrame;
  54. yield return endofFrame;
  55. NRDebugger.Info("[NRTrackingModeChangedListener] Enter tracking initialize mode...");
  56. OnTrackStateChanged?.Invoke(true, m_LostTrackingTip.LeftRT, m_LostTrackingTip.RightRT);
  57. NRHMDPoseTracker postTracker = NRSessionManager.Instance.NRHMDPoseTracker;
  58. while ((NRFrame.LostTrackingReason != LostTrackingReason.NONE || postTracker.IsTrackModeChanging || (Time.realtimeSinceStartup - begin_time) < MinTimeLastLimited)
  59. && (Time.realtimeSinceStartup - begin_time) < MaxTimeLastLimited)
  60. {
  61. NRDebugger.Info("[NRTrackingModeChangedListener] Wait for tracking: modeChanging={0}, lostTrackReason={1}",
  62. postTracker.IsTrackModeChanging, NRFrame.LostTrackingReason);
  63. yield return endofFrame;
  64. }
  65. if (m_DisableRenderCamera == null)
  66. {
  67. m_DisableRenderCamera = NRKernalUpdater.Instance.StartCoroutine(DisableTrackingInitializingRenderCamera());
  68. }
  69. yield return m_DisableRenderCamera;
  70. m_DisableRenderCamera = null;
  71. }
  72. public IEnumerator DisableTrackingInitializingRenderCamera()
  73. {
  74. if (m_LostTrackingTip != null)
  75. {
  76. m_LostTrackingTip.gameObject.SetActive(false);
  77. }
  78. yield return new WaitForEndOfFrame();
  79. yield return new WaitForEndOfFrame();
  80. yield return new WaitForEndOfFrame();
  81. OnTrackStateChanged?.Invoke(false, m_LostTrackingTip.LeftRT, m_LostTrackingTip.RightRT);
  82. NRDebugger.Info("[NRTrackingModeChangedListener] Exit tracking initialize mode...");
  83. }
  84. }
  85. }