ResolveToRenderTexture.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using UnityEngine;
  2. //-----------------------------------------------------------------------------
  3. // Copyright 2019-2022 RenderHeads Ltd. All rights reserved.
  4. //-----------------------------------------------------------------------------
  5. namespace RenderHeads.Media.AVProVideo
  6. {
  7. /// Renders the video texture to a RenderTexture - either one provided by the user (external) or to an internal one.
  8. /// The video frames can optionally be "resolved" to unpack packed alpha, display a single stereo eye, generate mip maps, and apply colorspace conversions
  9. [AddComponentMenu("AVPro Video/Resolve To RenderTexture", 330)]
  10. [HelpURL("https://www.renderheads.com/products/avpro-video/")]
  11. public class ResolveToRenderTexture : MonoBehaviour
  12. {
  13. [SerializeField] MediaPlayer _mediaPlayer = null;
  14. [SerializeField] VideoResolveOptions _options = VideoResolveOptions.Create();
  15. [SerializeField] VideoRender.ResolveFlags _resolveFlags = (VideoRender.ResolveFlags.ColorspaceSRGB | VideoRender.ResolveFlags.Mipmaps | VideoRender.ResolveFlags.PackedAlpha | VideoRender.ResolveFlags.StereoLeft);
  16. [SerializeField] RenderTexture _externalTexture = null;
  17. private Material _materialResolve;
  18. private bool _isMaterialSetup;
  19. private bool _isMaterialDirty;
  20. private bool _isMaterialOES;
  21. private RenderTexture _internalTexture;
  22. private int _textureFrameCount = -1;
  23. public MediaPlayer MediaPlayer
  24. {
  25. get
  26. {
  27. return _mediaPlayer;
  28. }
  29. set
  30. {
  31. ChangeMediaPlayer(value);
  32. }
  33. }
  34. public RenderTexture ExternalTexture
  35. {
  36. get
  37. {
  38. return _externalTexture;
  39. }
  40. set
  41. {
  42. _externalTexture = value;
  43. }
  44. }
  45. public RenderTexture TargetTexture
  46. {
  47. get
  48. {
  49. if (_externalTexture == null) return _internalTexture;
  50. return _externalTexture;
  51. }
  52. }
  53. public void SetMaterialDirty()
  54. {
  55. _isMaterialDirty = true;
  56. }
  57. private void ChangeMediaPlayer(MediaPlayer mediaPlayer)
  58. {
  59. if (_mediaPlayer != mediaPlayer)
  60. {
  61. _mediaPlayer = mediaPlayer;
  62. _textureFrameCount = -1;
  63. _isMaterialSetup = false;
  64. _isMaterialDirty = true;
  65. Resolve();
  66. }
  67. }
  68. void Start()
  69. {
  70. _isMaterialOES = ( _mediaPlayer != null ) ? _mediaPlayer.IsUsingAndroidOESPath() : false;
  71. _materialResolve = VideoRender.CreateResolveMaterial( _isMaterialOES );
  72. VideoRender.SetupMaterialForMedia(_materialResolve, _mediaPlayer, -1);
  73. }
  74. void LateUpdate()
  75. {
  76. Debug.Assert(_mediaPlayer != null);
  77. Resolve();
  78. }
  79. public void Resolve()
  80. {
  81. ITextureProducer textureProducer = _mediaPlayer != null ? _mediaPlayer.TextureProducer : null;
  82. if (textureProducer != null && textureProducer.GetTexture())
  83. {
  84. // Check for a swap between OES and none-OES
  85. bool playerIsOES = _mediaPlayer.IsUsingAndroidOESPath();
  86. if ( _isMaterialOES != playerIsOES )
  87. {
  88. _isMaterialOES = playerIsOES;
  89. _materialResolve = VideoRender.CreateResolveMaterial( playerIsOES );
  90. }
  91. if (!_isMaterialSetup)
  92. {
  93. VideoRender.SetupMaterialForMedia(_materialResolve, _mediaPlayer, -1);
  94. _isMaterialSetup = true;
  95. _isMaterialDirty = true;
  96. }
  97. if (_isMaterialDirty)
  98. {
  99. VideoRender.SetupResolveMaterial(_materialResolve, _options);
  100. _isMaterialDirty = false;
  101. }
  102. int textureFrameCount = textureProducer.GetTextureFrameCount();
  103. if (textureFrameCount != _textureFrameCount)
  104. {
  105. _internalTexture = VideoRender.ResolveVideoToRenderTexture(_materialResolve, _internalTexture, textureProducer, _resolveFlags);
  106. _textureFrameCount = textureFrameCount;
  107. if (_internalTexture && _externalTexture)
  108. {
  109. // NOTE: This blit can be removed once we can ResolveVideoToRenderTexture is made not to recreate textures
  110. // NOTE: This blit probably doesn't do correct linear/srgb conversion if the colorspace settings differ, may have to use GL.sRGBWrite
  111. Graphics.Blit(_internalTexture, _externalTexture);
  112. }
  113. }
  114. }
  115. }
  116. void OnDisable()
  117. {
  118. if (_internalTexture)
  119. {
  120. RenderTexture.ReleaseTemporary(_internalTexture); _internalTexture = null;
  121. }
  122. }
  123. void OnDestroy()
  124. {
  125. if (_materialResolve)
  126. {
  127. Destroy(_materialResolve); _materialResolve = null;
  128. }
  129. }
  130. #if false
  131. void OnGUI()
  132. {
  133. if (TargetTexture)
  134. {
  135. GUI.DrawTexture(new Rect(0f, 0f, Screen.width * 0.8f, Screen.height * 0.8f), TargetTexture, ScaleMode.ScaleToFit, true);
  136. }
  137. }
  138. #endif
  139. }
  140. }