ExtraFrameBlender.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 ExtraFrameBlender : 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_BackGroundMat;
  21. private NRBackGroundRender m_NRBackGroundRender;
  22. private NRCameraInitializer m_DeviceParamInitializer;
  23. private RenderTexture m_BlendTexture;
  24. /// <summary> Gets or sets the blend texture. </summary>
  25. /// <value> The blend texture. </value>
  26. public override RenderTexture BlendTexture
  27. {
  28. get
  29. {
  30. return m_BlendTexture;
  31. }
  32. }
  33. /// <summary> Initializes this object. </summary>
  34. /// <param name="camera"> The camera.</param>
  35. /// <param name="encoder"> The encoder.</param>
  36. /// <param name="param"> The parameter.</param>
  37. public override void Init(Camera camera, IEncoder encoder, CameraParameters param)
  38. {
  39. base.Init(camera, encoder, param);
  40. Width = param.cameraResolutionWidth;
  41. Height = param.cameraResolutionHeight;
  42. m_TargetCamera = camera;
  43. m_Encoder = encoder;
  44. BlendMode = param.blendMode;
  45. m_NRBackGroundRender = m_TargetCamera.gameObject.GetComponent<NRBackGroundRender>();
  46. if (m_NRBackGroundRender == null)
  47. {
  48. m_NRBackGroundRender = m_TargetCamera.gameObject.AddComponent<NRBackGroundRender>();
  49. }
  50. m_NRBackGroundRender.enabled = false;
  51. m_DeviceParamInitializer = camera.gameObject.GetComponent<NRCameraInitializer>();
  52. m_TargetCamera.enabled = false;
  53. m_BlendTexture = UnityExtendedUtility.CreateRenderTexture(Width, Height, 24, RenderTextureFormat.ARGB32);
  54. m_TargetCamera.targetTexture = m_BlendTexture;
  55. }
  56. /// <summary> Executes the 'frame' action. </summary>
  57. /// <param name="frame"> The frame.</param>
  58. public override void OnFrame(UniversalTextureFrame frame)
  59. {
  60. base.OnFrame(frame);
  61. if (!m_DeviceParamInitializer.IsInitialized)
  62. {
  63. return;
  64. }
  65. if (m_BackGroundMat == null)
  66. {
  67. m_BackGroundMat = CreatBlendMaterial(BlendMode, frame.textureType);
  68. m_NRBackGroundRender.SetMaterial(m_BackGroundMat);
  69. }
  70. bool isyuv = frame.textureType == TextureType.YUV;
  71. const string MainTextureStr = "_MainTex";
  72. const string UTextureStr = "_UTex";
  73. const string VTextureStr = "_VTex";
  74. switch (BlendMode)
  75. {
  76. case BlendMode.VirtualOnly:
  77. m_NRBackGroundRender.enabled = false;
  78. m_TargetCamera.Render();
  79. break;
  80. case BlendMode.RGBOnly:
  81. case BlendMode.Blend:
  82. case BlendMode.WidescreenBlend:
  83. if (isyuv)
  84. {
  85. m_BackGroundMat.SetTexture(MainTextureStr, frame.textures[0]);
  86. m_BackGroundMat.SetTexture(UTextureStr, frame.textures[1]);
  87. m_BackGroundMat.SetTexture(VTextureStr, frame.textures[2]);
  88. }
  89. else
  90. {
  91. m_BackGroundMat.SetTexture(MainTextureStr, frame.textures[0]);
  92. }
  93. m_NRBackGroundRender.enabled = true;
  94. m_TargetCamera.Render();
  95. break;
  96. default:
  97. m_NRBackGroundRender.enabled = false;
  98. break;
  99. }
  100. // Commit frame
  101. m_Encoder.Commit(BlendTexture, frame.timeStamp);
  102. FrameCount++;
  103. }
  104. private Material CreatBlendMaterial(BlendMode mode, TextureType texturetype)
  105. {
  106. string shader_name;
  107. shader_name = "Record/Shaders/NRBackground{0}";
  108. shader_name = string.Format(shader_name, texturetype == TextureType.RGB ? "" : "YUV");
  109. return new Material(Resources.Load<Shader>(shader_name));
  110. }
  111. /// <summary>
  112. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged
  113. /// resources. </summary>
  114. public override void Dispose()
  115. {
  116. base.Dispose();
  117. m_BlendTexture?.Release();
  118. m_BlendTexture = null;
  119. }
  120. }
  121. }