CaptureFromTexture.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #if UNITY_5_4_OR_NEWER
  2. #define AVPRO_MOVIECAPTURE_RENDERTEXTUREBGRA32_54
  3. #endif
  4. using UnityEngine;
  5. //-----------------------------------------------------------------------------
  6. // Copyright 2012-2017 RenderHeads Ltd. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. namespace RenderHeads.Media.AVProMovieCapture
  9. {
  10. /// <summary>
  11. /// Capture from a Texture object (including RenderTexture, WebcamTexture)
  12. /// </summary>
  13. [AddComponentMenu("AVPro Movie Capture/Capture From Texture", 3)]
  14. public class CaptureFromTexture : CaptureBase
  15. {
  16. private Texture _sourceTexture;
  17. private RenderTexture _renderTexture;
  18. private System.IntPtr _targetNativePointer = System.IntPtr.Zero;
  19. public void SetSourceTexture(Texture texture)
  20. {
  21. _sourceTexture = texture;
  22. if (texture is Texture2D)
  23. {
  24. if ((((Texture2D)texture).format != TextureFormat.ARGB32) &&
  25. (((Texture2D)texture).format != TextureFormat.RGBA32) &&
  26. (((Texture2D)texture).format != TextureFormat.BGRA32))
  27. {
  28. Debug.LogWarning("[AVProMovieCapture] texture format may not be supported: " + ((Texture2D)texture).format);
  29. }
  30. }
  31. else if (texture is RenderTexture)
  32. {
  33. if ((((RenderTexture)texture).format != RenderTextureFormat.ARGB32) &&
  34. (((RenderTexture)texture).format != RenderTextureFormat.Default)
  35. #if AVPRO_MOVIECAPTURE_RENDERTEXTUREBGRA32_54
  36. && (((RenderTexture)texture).format != RenderTextureFormat.BGRA32)
  37. #endif
  38. )
  39. {
  40. Debug.LogWarning("[AVProMovieCapture] texture format may not be supported: " + ((RenderTexture)texture).format);
  41. }
  42. }
  43. }
  44. public override void UpdateFrame()
  45. {
  46. TickFrameTimer();
  47. if (_capturing && !_paused && _sourceTexture)
  48. {
  49. bool canGrab = true;
  50. // If motion blur is enabled, wait until all frames are accumulated
  51. if (IsUsingMotionBlur())
  52. {
  53. // If the motion blur is still accumulating, don't grab this frame
  54. canGrab = _motionBlur.IsFrameAccumulated;
  55. }
  56. if (canGrab)
  57. {
  58. if (CanOutputFrame())
  59. {
  60. // If motion blur is enabled, use the motion blur result
  61. Texture sourceTexture = _sourceTexture;
  62. if (IsUsingMotionBlur())
  63. {
  64. sourceTexture = _motionBlur.FinalTexture;
  65. }
  66. // If the texture isn't a RenderTexture then blit it to the Rendertexture so the native plugin can grab it
  67. if (!(sourceTexture is RenderTexture))
  68. {
  69. _renderTexture.DiscardContents();
  70. Graphics.Blit(sourceTexture, _renderTexture);
  71. sourceTexture = _renderTexture;
  72. }
  73. if (_targetNativePointer == System.IntPtr.Zero || _supportTextureRecreate)
  74. {
  75. // NOTE: If support for captures to survive through alt-tab events, or window resizes where the GPU resources are recreated
  76. // is required, then this line is needed. It is very expensive though as it does a sync with the rendering thread.
  77. _targetNativePointer = sourceTexture.GetNativeTexturePtr();
  78. }
  79. NativePlugin.SetTexturePointer(_handle, _targetNativePointer);
  80. RenderThreadEvent(NativePlugin.PluginEvent.CaptureFrameBuffer);
  81. // Handle audio from Unity
  82. if (IsRecordingUnityAudio())
  83. {
  84. int audioDataLength = 0;
  85. System.IntPtr audioDataPtr = _audioCapture.ReadData(out audioDataLength);
  86. if (audioDataLength > 0)
  87. {
  88. NativePlugin.EncodeAudio(_handle, audioDataPtr, (uint)audioDataLength);
  89. }
  90. }
  91. UpdateFPS();
  92. }
  93. }
  94. }
  95. base.UpdateFrame();
  96. RenormTimer();
  97. }
  98. private void LateUpdate()
  99. {
  100. if (_motionBlur != null)
  101. {
  102. if (_capturing && !_paused && _handle >= 0)
  103. {
  104. _motionBlur.Accumulate(_sourceTexture);
  105. }
  106. }
  107. }
  108. public override Texture GetPreviewTexture()
  109. {
  110. if (IsUsingMotionBlur())
  111. {
  112. return _motionBlur.FinalTexture;
  113. }
  114. if (_sourceTexture is RenderTexture)
  115. {
  116. return _sourceTexture;
  117. }
  118. return _renderTexture;
  119. }
  120. public override bool PrepareCapture()
  121. {
  122. if (_capturing)
  123. {
  124. return false;
  125. }
  126. if (_sourceTexture == null)
  127. {
  128. Debug.LogError("[AVProMovieCapture] No texture set to capture");
  129. return false;
  130. }
  131. if (SystemInfo.graphicsDeviceVersion.StartsWith("OpenGL") && !SystemInfo.graphicsDeviceVersion.Contains("emulated"))
  132. {
  133. Debug.LogError("[AVProMovieCapture] OpenGL not yet supported for CaptureFromTexture component, please use Direct3D11 instead. You may need to switch your build platform to Windows.");
  134. return false;
  135. }
  136. _pixelFormat = NativePlugin.PixelFormat.RGBA32;
  137. SelectRecordingResolution(_sourceTexture.width, _sourceTexture.height);
  138. _renderTexture = RenderTexture.GetTemporary(_targetWidth, _targetHeight, 0, RenderTextureFormat.ARGB32);
  139. _renderTexture.Create();
  140. GenerateFilename();
  141. return base.PrepareCapture();
  142. }
  143. public override void UnprepareCapture()
  144. {
  145. _targetNativePointer = System.IntPtr.Zero;
  146. NativePlugin.SetTexturePointer(_handle, System.IntPtr.Zero);
  147. if (_renderTexture != null)
  148. {
  149. RenderTexture.ReleaseTemporary(_renderTexture);
  150. _renderTexture = null;
  151. }
  152. base.UnprepareCapture();
  153. }
  154. }
  155. }