NRMeshingManager.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 System.Collections.Generic;
  14. using UnityEngine;
  15. using static NRKernal.NativeMeshing;
  16. public interface IMeshInfoProcessor
  17. {
  18. void UpdateMeshInfo(ulong identifier, NRMeshingBlockState meshingBlockState, Mesh mesh);
  19. void ClearMeshInfo();
  20. }
  21. public class NRMeshingManager : SingletonBehaviour<NRMeshingManager>
  22. {
  23. /// The size of the bounding box
  24. [SerializeField]
  25. private Vector3 m_BoundingBoxSize;
  26. [SerializeField]
  27. private float m_MeshRequestInterval;
  28. IMeshInfoProcessor[] m_MeshInfoProcessors;
  29. private NativeMeshing m_NativeMeshing;
  30. Coroutine m_MeshingCoroutine;
  31. float m_MeshUpdateTime = 0;
  32. Func<KeyValuePair<ulong, BlockInfo>, bool> m_Predicate = new Func<KeyValuePair<ulong, BlockInfo>, bool>(
  33. p => p.Value.blockState != NRMeshingBlockState.NR_MESHING_BLOCK_STATE_UNCHANGED);
  34. /// <summary> Starts this object. </summary>
  35. private void Start()
  36. {
  37. if (isDirty)
  38. return;
  39. NRDebugger.Info("[NRMeshingManager] Start");
  40. m_NativeMeshing = new NativeMeshing();
  41. m_NativeMeshing.Create();
  42. m_NativeMeshing.Start();
  43. m_NativeMeshing.SetMeshingFlags(NRMeshingFlags.NR_MESHING_FLAGS_COMPUTE_NORMAL);
  44. m_MeshInfoProcessors = GetComponents<IMeshInfoProcessor>();
  45. }
  46. void Update()
  47. {
  48. if (m_MeshingCoroutine == null)
  49. {
  50. m_MeshUpdateTime += Time.deltaTime;
  51. if (m_MeshUpdateTime >= m_MeshRequestInterval)
  52. {
  53. RequestMeshing();
  54. m_MeshUpdateTime = 0;
  55. }
  56. }
  57. }
  58. void RequestMeshing()
  59. {
  60. if (m_MeshingCoroutine != null)
  61. StopCoroutine(m_MeshingCoroutine);
  62. m_MeshingCoroutine = StartCoroutine(RequestMeshingCoroutine());
  63. }
  64. IEnumerator RequestMeshingCoroutine()
  65. {
  66. yield return RequestMeshInfoCoroutine();
  67. yield return RequestMeshDetailCoroutine();
  68. m_MeshingCoroutine = null;
  69. }
  70. IEnumerator RequestMeshInfoCoroutine()
  71. {
  72. NRDebugger.Info("[NRMeshingManager] Start RequestMeshInfoCoroutine");
  73. if (m_NativeMeshing.RequestMeshInfo(m_BoundingBoxSize, NRFrame.HeadPose))
  74. {
  75. while (!m_NativeMeshing.GetMeshInfoResult())
  76. {
  77. NRDebugger.Debug("[NRMeshingManager] Wait GetMeshInfoResult");
  78. yield return null;
  79. }
  80. var timestamp = m_NativeMeshing.GetMeshInfoTimestamp();
  81. NRDebugger.Info("[NRMeshingManager] GetMeshInfoTimestamp: {0}", timestamp);
  82. m_NativeMeshing.GetBlockInfoData();
  83. m_NativeMeshing.DestroyMeshInfo();
  84. m_NativeMeshing.DestroyMeshInfoRequest();
  85. }
  86. }
  87. IEnumerator RequestMeshDetailCoroutine()
  88. {
  89. NRDebugger.Info("[NRMeshingManager] Start RequestMeshDetailCoroutine");
  90. if (m_NativeMeshing.RequestMeshDetail(m_Predicate))
  91. {
  92. while (!m_NativeMeshing.GetMeshDetailResult())
  93. {
  94. NRDebugger.Debug("[NRMeshingManager] Wait GetMeshDetailResult");
  95. yield return null;
  96. }
  97. var timestamp = m_NativeMeshing.GetMeshDetailTimestamp();
  98. NRDebugger.Info("[NRMeshingManager] GetMeshDetailTimestamp: {0}", timestamp);
  99. yield return m_NativeMeshing.GetMeshDetailData(ProcessMeshDetail);
  100. m_NativeMeshing.DestroyMeshDetail();
  101. m_NativeMeshing.DestroyMeshDetailRequest();
  102. }
  103. }
  104. void ProcessMeshDetail(ulong identifier, NRMeshingBlockState meshingBlockState, Mesh mesh)
  105. {
  106. foreach (var processor in m_MeshInfoProcessors)
  107. {
  108. processor.UpdateMeshInfo(identifier, meshingBlockState, mesh);
  109. }
  110. }
  111. /// <summary> Executes the 'application pause' action. </summary>
  112. /// <param name="pause"> True to pause.</param>
  113. private void OnApplicationPause(bool pause)
  114. {
  115. NRDebugger.Info("[NRMeshingManager] OnApplicationPause: {0}", pause);
  116. if (m_NativeMeshing != null)
  117. {
  118. if (pause)
  119. {
  120. m_NativeMeshing.Pause();
  121. }
  122. else
  123. {
  124. m_NativeMeshing.Resume();
  125. }
  126. }
  127. }
  128. protected override void OnDestroy()
  129. {
  130. NRDebugger.Info("[NRMeshingManager] OnDestroy");
  131. base.OnDestroy();
  132. if (m_NativeMeshing != null)
  133. {
  134. m_NativeMeshing.Stop();
  135. m_NativeMeshing.Destroy();
  136. }
  137. }
  138. }
  139. }