OverlayBase.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 UnityEngine;
  12. using System;
  13. using System.Collections.Generic;
  14. public class OverlayBase : MonoBehaviour, IComparable<OverlayBase>
  15. {
  16. public class IntPtrComparer : IEqualityComparer<IntPtr>
  17. {
  18. public bool Equals(IntPtr x, IntPtr y)
  19. {
  20. return x == y;
  21. }
  22. public int GetHashCode(IntPtr obj)
  23. {
  24. return obj.GetHashCode();
  25. }
  26. }
  27. /// <summary>
  28. /// The compositionDepth defines the order of the NROverlays in composition. The overlay with smaller compositionDepth would be composited in the front of the overlay with larger compositionDepth.
  29. /// </summary>
  30. [Tooltip("The compositionDepth defines the order of the NROverlays in composition. The overlay with smaller compositionDepth would be composited in the front of the overlay with larger compositionDepth.")]
  31. public int compositionDepth = 0;
  32. [Tooltip("Whether active this overlay when script start")]
  33. public bool ActiveOnStart = true;
  34. private bool alreadyAddedToSwapChain = false;
  35. //use custom comparer to avoid boxing, default comparer will convert struct IntPtr to object.
  36. public Dictionary<IntPtr, Texture> Textures = new Dictionary<IntPtr, Texture>(new IntPtrComparer());
  37. protected BufferSpec m_BufferSpec;
  38. protected ViewPort[] m_ViewPorts;
  39. protected int m_LayerId = 0;
  40. private bool m_IsDirty = false;
  41. private bool m_IsActive = true;
  42. public ViewPort[] ViewPorts
  43. {
  44. get
  45. {
  46. return m_ViewPorts;
  47. }
  48. }
  49. public BufferSpec BufferSpec
  50. {
  51. get { return m_BufferSpec; }
  52. set
  53. {
  54. m_BufferSpec.Copy(value);
  55. }
  56. }
  57. public int LayerId
  58. {
  59. get
  60. {
  61. return m_LayerId;
  62. }
  63. set
  64. {
  65. m_LayerId = value;
  66. }
  67. }
  68. public bool IsActive
  69. {
  70. get { return m_IsActive; }
  71. private set { m_IsActive = value; }
  72. }
  73. public UInt64 NativeSpecHandler { get; set; }
  74. public int CompareTo(OverlayBase that)
  75. {
  76. return this.compositionDepth.CompareTo(that.compositionDepth);
  77. }
  78. public virtual Texture GetTexturePtr()
  79. {
  80. return null;
  81. }
  82. protected void SetDirty(bool value)
  83. {
  84. m_IsDirty = m_IsDirty == true ? true : value;
  85. }
  86. void OnEnable()
  87. {
  88. IsActive = true;
  89. }
  90. void OnDisable()
  91. {
  92. IsActive = false;
  93. }
  94. protected void Start()
  95. {
  96. if (ActiveOnStart)
  97. {
  98. InitAndActive();
  99. }
  100. }
  101. public void InitAndActive()
  102. {
  103. if (!alreadyAddedToSwapChain)
  104. {
  105. Initialize();
  106. NRSwapChainManager.Instance.Add(this);
  107. alreadyAddedToSwapChain = true;
  108. }
  109. }
  110. void Update()
  111. {
  112. if (m_IsDirty && m_ViewPorts != null)
  113. {
  114. DestroyViewPort();
  115. CreateViewport();
  116. m_IsDirty = false;
  117. }
  118. }
  119. public virtual void Destroy()
  120. {
  121. if (alreadyAddedToSwapChain)
  122. {
  123. NRSwapChainManager.Instance.Remove(this);
  124. alreadyAddedToSwapChain = false;
  125. }
  126. }
  127. protected void OnDestroy()
  128. {
  129. this.Destroy();
  130. }
  131. protected virtual void Initialize() { }
  132. public virtual void CreateOverlayTextures() { }
  133. public virtual void ReleaseOverlayTextures() { }
  134. public virtual void CreateViewport() { }
  135. public virtual void UpdateViewPort() { }
  136. public virtual void DestroyViewPort() { }
  137. /// <summary> Just for display overlay. </summary>
  138. public virtual void SwapBuffers(IntPtr bufferHandler) { }
  139. public override string ToString()
  140. {
  141. if (ViewPorts.Length == 1)
  142. {
  143. return string.Format("LayerId:{0}, go:{1}, depth:{2}, viewIndex:{3}, BufferSpec:{4}\nviewPort0:{5}", m_LayerId, gameObject.name, compositionDepth, ViewPorts[0].index, m_BufferSpec.ToString(),
  144. ViewPorts[0].ToString());
  145. }
  146. else if (ViewPorts.Length == 2)
  147. {
  148. return string.Format("LayerId:{0}, go:{1}, depth:{2}, viewIndex:{3}_{4}, BufferSpec:{5}\nviewPort0:{6}\nviewPort1:{7}", m_LayerId, gameObject.name, compositionDepth,
  149. ViewPorts[0].index, ViewPorts[1].index, m_BufferSpec.ToString(),
  150. ViewPorts[0].ToString(),
  151. ViewPorts[1].ToString());
  152. }
  153. return string.Empty;
  154. }
  155. }
  156. }