ObserverViewBlender.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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.StreammingCast
  10. {
  11. using NRKernal.Record;
  12. using UnityEngine;
  13. /// <summary> An observer view blender. </summary>
  14. public class ObserverViewBlender : IFrameConsumer
  15. {
  16. /// <summary> Target camera. </summary>
  17. protected Camera m_TargetCamera;
  18. /// <summary> The encoder. </summary>
  19. protected IEncoder m_Encoder;
  20. /// <summary> Gets or sets the width. </summary>
  21. /// <value> The width. </value>
  22. public int Width
  23. {
  24. get;
  25. private set;
  26. }
  27. /// <summary> Gets or sets the height. </summary>
  28. /// <value> The height. </value>
  29. public int Height
  30. {
  31. get;
  32. private set;
  33. }
  34. /// <summary> Gets the blend texture. </summary>
  35. /// <value> The blend texture. </value>
  36. public Texture BlendTexture
  37. {
  38. get
  39. {
  40. return m_TargetCamera?.targetTexture;
  41. }
  42. }
  43. /// <summary> Configs. </summary>
  44. /// <param name="camera"> The camera.</param>
  45. /// <param name="encoder"> The encoder.</param>
  46. /// <param name="param"> The parameter.</param>
  47. public virtual void Config(Camera camera, IEncoder encoder, CameraParameters param)
  48. {
  49. Width = param.cameraResolutionWidth;
  50. Height = param.cameraResolutionHeight;
  51. m_TargetCamera = camera;
  52. m_Encoder = encoder;
  53. m_TargetCamera.enabled = true;
  54. // As the texture will be used to blend with physical camera image, the alpha channel need to be 0.
  55. m_TargetCamera.backgroundColor = new Color(0, 0, 0, 0);
  56. m_TargetCamera.targetTexture = UnityExtendedUtility.CreateRenderTexture(Width, Height, 24, RenderTextureFormat.ARGB32);
  57. m_TargetCamera.depthTextureMode = DepthTextureMode.Depth;
  58. }
  59. /// <summary> Executes the 'frame' action. </summary>
  60. /// <param name="frame"> The frame.</param>
  61. public virtual void OnFrame(UniversalTextureFrame frame)
  62. {
  63. // Commit frame
  64. m_Encoder.Commit((RenderTexture)frame.textures[0], frame.timeStamp);
  65. }
  66. /// <summary>
  67. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged
  68. /// resources. </summary>
  69. public void Dispose()
  70. {
  71. }
  72. }
  73. }