FrameBlender.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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.Record
  10. {
  11. using UnityEngine;
  12. /// <summary> A frame blender. </summary>
  13. public class FrameBlender : BlenderBase
  14. {
  15. /// <summary> Target camera. </summary>
  16. protected Camera m_TargetCamera;
  17. /// <summary> The encoder. </summary>
  18. protected IEncoder m_Encoder;
  19. /// <summary> The blend material. </summary>
  20. private Material m_BlendMaterial;
  21. /// <summary> The blend mode. </summary>
  22. protected BlendMode m_BlendMode;
  23. /// <summary> The RGB source. </summary>
  24. protected RenderTexture m_RGBSource;
  25. /// <summary> The temporary combine tex. </summary>
  26. protected Texture2D m_TempCombineTex;
  27. /// <summary> The blend texture. </summary>
  28. private RenderTexture m_BlendTexture;
  29. /// <summary> Gets or sets the blend texture. </summary>
  30. /// <value> The blend texture. </value>
  31. public override RenderTexture BlendTexture
  32. {
  33. get
  34. {
  35. return m_BlendTexture;
  36. }
  37. protected set
  38. {
  39. m_BlendTexture = value;
  40. }
  41. }
  42. /// <summary> Gets the RGB texture. </summary>
  43. /// <value> The RGB texture. </value>
  44. public RenderTexture RGBTexture
  45. {
  46. get
  47. {
  48. return m_RGBSource;
  49. }
  50. }
  51. /// <summary> Gets the virtual texture. </summary>
  52. /// <value> The virtual texture. </value>
  53. public RenderTexture VirtualTexture
  54. {
  55. get
  56. {
  57. return m_TargetCamera.targetTexture;
  58. }
  59. }
  60. /// <summary> Initializes this object. </summary>
  61. /// <param name="camera"> The camera.</param>
  62. /// <param name="encoder"> The encoder.</param>
  63. /// <param name="param"> The parameter.</param>
  64. public override void Init(Camera camera, IEncoder encoder, CameraParameters param)
  65. {
  66. Width = param.cameraResolutionWidth;
  67. Height = param.cameraResolutionHeight;
  68. m_BlendMode = param.blendMode;
  69. m_TargetCamera = camera;
  70. m_Encoder = encoder;
  71. // An extra rendering is required when BlendMode is RGBOnly or WidescreenBlend.
  72. switch (m_BlendMode)
  73. {
  74. case BlendMode.RGBOnly:
  75. BlendTexture = UnityExtendedUtility.CreateRenderTexture(Width, Height, 24, RenderTextureFormat.ARGB32, false);
  76. break;
  77. case BlendMode.VirtualOnly:
  78. BlendTexture = UnityExtendedUtility.CreateRenderTexture(Width, Height, 24, RenderTextureFormat.ARGB32, false);
  79. break;
  80. case BlendMode.Blend:
  81. BlendTexture = UnityExtendedUtility.CreateRenderTexture(Width, Height, 24, RenderTextureFormat.ARGB32, false);
  82. break;
  83. case BlendMode.WidescreenBlend:
  84. BlendTexture = UnityExtendedUtility.CreateRenderTexture(2 * Width, Height, 24, RenderTextureFormat.ARGB32, false);
  85. m_RGBSource = UnityExtendedUtility.CreateRenderTexture(Width, Height, 24, RenderTextureFormat.ARGB32, false);
  86. m_TempCombineTex = new Texture2D(2 * Width, Height, TextureFormat.ARGB32, false);
  87. break;
  88. default:
  89. break;
  90. }
  91. m_TargetCamera.enabled = false;
  92. m_TargetCamera.targetTexture = UnityExtendedUtility.CreateRenderTexture(Width, Height, 24, RenderTextureFormat.ARGB32);
  93. }
  94. /// <summary> Executes the 'frame' action. </summary>
  95. /// <param name="frame"> The frame.</param>
  96. public override void OnFrame(UniversalTextureFrame frame)
  97. {
  98. Texture2D frametex = frame.textures[0] as Texture2D;
  99. if (m_BlendMode != BlendMode.RGBOnly)
  100. {
  101. m_TargetCamera.Render();
  102. }
  103. if (m_BlendMaterial == null)
  104. {
  105. CreatBlendMaterial(m_BlendMode, frame.textureType);
  106. }
  107. bool isyuv = frame.textureType == TextureType.YUV;
  108. switch (m_BlendMode)
  109. {
  110. case BlendMode.RGBOnly:
  111. const string MainTextureStr = "_MainTex";
  112. if (isyuv)
  113. {
  114. const string UTextureStr = "_UTex";
  115. const string VTextureStr = "_VTex";
  116. m_BlendMaterial.SetTexture(MainTextureStr, frame.textures[0]);
  117. m_BlendMaterial.SetTexture(UTextureStr, frame.textures[1]);
  118. m_BlendMaterial.SetTexture(VTextureStr, frame.textures[2]);
  119. }
  120. else
  121. {
  122. m_BlendMaterial.SetTexture(MainTextureStr, frame.textures[0]);
  123. }
  124. Graphics.Blit(frame.textures[0], BlendTexture, m_BlendMaterial);
  125. break;
  126. case BlendMode.VirtualOnly:
  127. m_BlendMaterial.SetTexture("_MainTex", m_TargetCamera.targetTexture);
  128. Graphics.Blit(m_TargetCamera.targetTexture, BlendTexture, m_BlendMaterial);
  129. break;
  130. case BlendMode.Blend:
  131. m_BlendMaterial.SetTexture("_MainTex", m_TargetCamera.targetTexture);
  132. if (isyuv)
  133. {
  134. m_BlendMaterial.SetTexture("_YTex", frame.textures[0]);
  135. m_BlendMaterial.SetTexture("_UTex", frame.textures[1]);
  136. m_BlendMaterial.SetTexture("_VTex", frame.textures[2]);
  137. }
  138. else
  139. {
  140. m_BlendMaterial.SetTexture("_BcakGroundTex", frame.textures[0]);
  141. }
  142. Graphics.Blit(m_TargetCamera.targetTexture, BlendTexture, m_BlendMaterial);
  143. break;
  144. case BlendMode.WidescreenBlend:
  145. if (isyuv)
  146. {
  147. throw new System.Exception("Not support yuv texture for this mode...");
  148. }
  149. CombineTexture(frametex, m_TargetCamera.targetTexture, m_TempCombineTex, BlendTexture);
  150. break;
  151. default:
  152. break;
  153. }
  154. // Commit frame
  155. m_Encoder.Commit(BlendTexture, frame.timeStamp);
  156. FrameCount++;
  157. }
  158. private void CreatBlendMaterial(BlendMode mode, TextureType texturetype)
  159. {
  160. string shader_name;
  161. if (mode == BlendMode.Blend)
  162. {
  163. shader_name = "Record/Shaders/AlphaBlend{0}";
  164. shader_name = string.Format(shader_name, texturetype == TextureType.RGB ? "" : "YUV");
  165. }
  166. else if (mode == BlendMode.VirtualOnly)
  167. {
  168. shader_name = "Record/Shaders/NormalTexture";
  169. }
  170. else
  171. {
  172. shader_name = "Record/Shaders/NormalBlend{0}";
  173. shader_name = string.Format(shader_name, texturetype == TextureType.RGB ? "" : "YUV");
  174. }
  175. m_BlendMaterial = new Material(Resources.Load<Shader>(shader_name));
  176. }
  177. /// <summary> Combine texture. </summary>
  178. /// <param name="bgsource"> The bgsource.</param>
  179. /// <param name="foresource"> The foresource.</param>
  180. /// <param name="tempdest"> The tempdest.</param>
  181. /// <param name="dest"> Destination for the.</param>
  182. private void CombineTexture(Texture2D bgsource, RenderTexture foresource, Texture2D tempdest, RenderTexture dest)
  183. {
  184. const string MainTextureStr = "_MainTex";
  185. m_BlendMaterial.SetTexture(MainTextureStr, m_RGBSource);
  186. Graphics.Blit(bgsource, m_RGBSource, m_BlendMaterial);
  187. RenderTexture prev = RenderTexture.active;
  188. RenderTexture.active = m_RGBSource;
  189. tempdest.ReadPixels(new Rect(0, 0, m_RGBSource.width, m_RGBSource.height), 0, 0);
  190. RenderTexture.active = foresource;
  191. tempdest.ReadPixels(new Rect(0, 0, foresource.width, foresource.height), foresource.width, 0);
  192. tempdest.Apply();
  193. RenderTexture.active = prev;
  194. Graphics.Blit(tempdest, dest);
  195. }
  196. /// <summary>
  197. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged
  198. /// resources. </summary>
  199. public override void Dispose()
  200. {
  201. m_BlendTexture?.Release();
  202. m_RGBSource?.Release();
  203. GameObject.Destroy(m_TempCombineTex);
  204. m_BlendTexture = null;
  205. m_RGBSource = null;
  206. m_TempCombineTex = null;
  207. }
  208. }
  209. }