CaptureFromScreen.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using UnityEngine;
  2. using System.Collections;
  3. //-----------------------------------------------------------------------------
  4. // Copyright 2012-2017 RenderHeads Ltd. All rights reserved.
  5. //-----------------------------------------------------------------------------
  6. namespace RenderHeads.Media.AVProMovieCapture
  7. {
  8. /// <summary>
  9. /// Capture from the screen (backbuffer). Everything is captured as it appears on the screen, including IMGUI rendering.
  10. /// This component waits for the frame to be completely rendered and then captures it.
  11. /// </summary>
  12. [AddComponentMenu("AVPro Movie Capture/Capture From Screen", 0)]
  13. public class CaptureFromScreen : CaptureBase
  14. {
  15. //private const int NewFrameSleepTimeMs = 6;
  16. private YieldInstruction _waitForEndOfFrame;
  17. public override bool PrepareCapture()
  18. {
  19. if (_capturing)
  20. {
  21. return false;
  22. }
  23. if (SystemInfo.graphicsDeviceVersion.StartsWith("OpenGL") && !SystemInfo.graphicsDeviceVersion.Contains("emulated"))
  24. {
  25. Debug.LogError("[AVProMovieCapture] OpenGL not yet supported for CaptureFromScreen component, please use Direct3D11 instead. You may need to switch your build platform to Windows.");
  26. return false;
  27. }
  28. SelectRecordingResolution(Screen.width, Screen.height);
  29. _pixelFormat = NativePlugin.PixelFormat.RGBA32;
  30. if (SystemInfo.graphicsDeviceVersion.StartsWith("OpenGL") && !SystemInfo.graphicsDeviceVersion.Contains("emulated"))
  31. {
  32. // TODO: add this back in once we have fixed opengl support
  33. _pixelFormat = NativePlugin.PixelFormat.BGRA32;
  34. _isTopDown = true;
  35. }
  36. else
  37. {
  38. _isTopDown = false;
  39. if (_isDirectX11)
  40. {
  41. _isTopDown = false;
  42. }
  43. }
  44. GenerateFilename();
  45. _waitForEndOfFrame = new WaitForEndOfFrame();
  46. return base.PrepareCapture();
  47. }
  48. public override void UnprepareCapture()
  49. {
  50. _waitForEndOfFrame = null;
  51. base.UnprepareCapture();
  52. }
  53. private IEnumerator FinalRenderCapture()
  54. {
  55. yield return _waitForEndOfFrame;
  56. TickFrameTimer();
  57. bool canGrab = true;
  58. if (IsUsingMotionBlur())
  59. {
  60. // If the motion blur is still accumulating, don't grab this frame
  61. canGrab = _motionBlur.IsFrameAccumulated;
  62. }
  63. if (canGrab && CanOutputFrame())
  64. {
  65. // Grab final RenderTexture into texture and encode
  66. if (IsRecordingUnityAudio())
  67. {
  68. int audioDataLength = 0;
  69. System.IntPtr audioDataPtr = _audioCapture.ReadData(out audioDataLength);
  70. if (audioDataLength > 0)
  71. {
  72. NativePlugin.EncodeAudio(_handle, audioDataPtr, (uint)audioDataLength);
  73. }
  74. }
  75. RenderThreadEvent(NativePlugin.PluginEvent.CaptureFrameBuffer);
  76. GL.InvalidateState();
  77. UpdateFPS();
  78. }
  79. RenormTimer();
  80. //yield return null;
  81. }
  82. public override void UpdateFrame()
  83. {
  84. if (_capturing && !_paused)
  85. {
  86. StartCoroutine(FinalRenderCapture());
  87. }
  88. base.UpdateFrame();
  89. }
  90. }
  91. }