NRDisplayOverlay.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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.Experimental
  10. {
  11. using System;
  12. using UnityEngine;
  13. [ScriptOrder(NativeConstants.NRRENDER_ORDER)]
  14. [RequireComponent(typeof(Camera))]
  15. public class NRDisplayOverlay : OverlayBase
  16. {
  17. /// <summary>
  18. /// Which display this overlay should render to.
  19. /// </summary>
  20. [Tooltip("Which display this overlay should render to.")]
  21. public NativeDevice targetEye;
  22. private Camera m_RenderCamera;
  23. private int m_EyeWidth;
  24. private int m_EyeHeight;
  25. private bool m_IsTrackingModeChanging = false;
  26. private RenderTexture m_HijackRenderTextureLeft = null;
  27. private RenderTexture m_HijackRenderTextureRight = null;
  28. protected override void Initialize()
  29. {
  30. base.Initialize();
  31. m_RenderCamera = gameObject.GetComponent<Camera>();
  32. #if !UNITY_EDITOR
  33. NRDevice.Instance.Init();
  34. var resolution = NRFrame.GetDeviceResolution(NativeDevice.LEFT_DISPLAY);
  35. m_EyeWidth = resolution.width;
  36. m_EyeHeight = resolution.height;
  37. #else
  38. m_EyeWidth = 1920;
  39. m_EyeHeight = 1080;
  40. #endif
  41. this.compositionDepth = int.MaxValue;
  42. m_BufferSpec.size = new NativeResolution((int)m_EyeWidth, (int)m_EyeHeight);
  43. m_BufferSpec.colorFormat = NRColorFormat.NR_COLOR_FORMAT_RGBA_8888;
  44. m_BufferSpec.depthFormat = NRDepthStencilFormat.NR_DEPTH_STENCIL_FORMAT_DEPTH_24;
  45. m_BufferSpec.samples = 1;
  46. m_BufferSpec.useExternalSurface = false;
  47. NRSessionManager.Instance.TrackingLostListener.OnTrackStateChanged -= OnTrackStateChanged;
  48. NRSessionManager.Instance.TrackingLostListener.OnTrackStateChanged += OnTrackStateChanged;
  49. }
  50. private void OnTrackStateChanged(bool trackChanging, RenderTexture leftRT, RenderTexture rightRT)
  51. {
  52. if (trackChanging)
  53. {
  54. m_IsTrackingModeChanging = true;
  55. m_HijackRenderTextureLeft = leftRT;
  56. m_HijackRenderTextureRight = rightRT;
  57. m_RenderCamera.targetTexture = null;
  58. SetDirty(true);
  59. }
  60. else
  61. {
  62. m_IsTrackingModeChanging = false;
  63. m_HijackRenderTextureLeft = null;
  64. m_HijackRenderTextureRight = null;
  65. SetDirty(true);
  66. }
  67. }
  68. public override void SwapBuffers(IntPtr bufferHandler)
  69. {
  70. if (!Textures.ContainsKey(bufferHandler))
  71. {
  72. NRDebugger.Error("[NRDisplayOverlay] SwapBuffers: Can not find the texture-[{0}]", bufferHandler);
  73. return;
  74. }
  75. Texture targetTexture;
  76. Textures.TryGetValue(bufferHandler, out targetTexture);
  77. RenderTexture renderTexture = targetTexture as RenderTexture;
  78. if (renderTexture == null)
  79. {
  80. NRDebugger.Error("[NRDisplayOverlay] The texture is null...");
  81. return;
  82. }
  83. if (m_IsTrackingModeChanging)
  84. {
  85. var srcTexture = (targetEye == NativeDevice.LEFT_DISPLAY) ? m_HijackRenderTextureLeft : m_HijackRenderTextureRight;
  86. Graphics.CopyTexture(srcTexture, renderTexture);
  87. }
  88. else
  89. {
  90. m_RenderCamera.targetTexture = renderTexture;
  91. }
  92. }
  93. public override void CreateViewport()
  94. {
  95. m_ViewPorts = new ViewPort[1];
  96. m_ViewPorts[0].reprojection = m_IsTrackingModeChanging ? NRReprojectionType.NR_REPROJECTION_TYPE_NONE : NRReprojectionType.NR_REPROJECTION_TYPE_FULL;
  97. m_ViewPorts[0].sourceUV = new Rect(0, 0, 1, 1);
  98. m_ViewPorts[0].targetDisplay = targetEye == NativeDevice.LEFT_DISPLAY ? LayerSide.Left : LayerSide.Right;
  99. m_ViewPorts[0].layerID = m_LayerId;
  100. NRFrame.GetEyeFov(targetEye, ref m_ViewPorts[0].fov);
  101. m_ViewPorts[0].is3DLayer = true;
  102. NRSwapChainManager.Instance.CreateBufferViewport(ref m_ViewPorts[0]);
  103. }
  104. public override void DestroyViewPort()
  105. {
  106. base.DestroyViewPort();
  107. if (m_ViewPorts != null)
  108. {
  109. var viewport = m_ViewPorts[0];
  110. NRSwapChainManager.Instance.DestroyBufferViewPort(viewport.nativeHandler);
  111. m_ViewPorts = null;
  112. }
  113. }
  114. public override void UpdateViewPort()
  115. {
  116. if (m_ViewPorts == null)
  117. {
  118. NRDebugger.Warning("Can not update view port for this layer:{0}", gameObject.name);
  119. return;
  120. }
  121. NRSwapChainManager.Instance.UpdateBufferViewport(ref m_ViewPorts[0]);
  122. }
  123. public override void CreateOverlayTextures()
  124. {
  125. NRDebugger.Info("[NRDisplayOverlay] CreateOverlayTextures. ");
  126. ReleaseOverlayTextures();
  127. for (int i = 0; i < m_BufferSpec.bufferCount; i++)
  128. {
  129. RenderTexture texture = UnityExtendedUtility.CreateRenderTexture((int)m_EyeWidth, (int)m_EyeHeight, 24, RenderTextureFormat.ARGB32);
  130. IntPtr texturePtr = texture.GetNativeTexturePtr();
  131. Textures.Add(texturePtr, texture);
  132. // NRDebugger.Info("[NRDisplayOverlay] CreateOverlayTextures: {0} => {1}", i, texturePtr);
  133. }
  134. }
  135. public override void ReleaseOverlayTextures()
  136. {
  137. if (Textures.Count == 0)
  138. {
  139. return;
  140. }
  141. NRDebugger.Info("[NRDisplayOverlay] ReleaseOverlayTextures. ");
  142. foreach (var item in Textures)
  143. {
  144. RenderTexture rt = item.Value as RenderTexture;
  145. if (rt != null) rt.Release();
  146. }
  147. Textures.Clear();
  148. }
  149. new void OnDestroy()
  150. {
  151. base.OnDestroy();
  152. if (NRSessionManager.Instance.TrackingLostListener != null)
  153. NRSessionManager.Instance.TrackingLostListener.OnTrackStateChanged -= OnTrackStateChanged;
  154. }
  155. }
  156. }