using UnityEngine; using System.Collections; //----------------------------------------------------------------------------- // Copyright 2012-2017 RenderHeads Ltd. All rights reserved. //----------------------------------------------------------------------------- namespace RenderHeads.Media.AVProMovieCapture { /// /// Capture from the screen (backbuffer). Everything is captured as it appears on the screen, including IMGUI rendering. /// This component waits for the frame to be completely rendered and then captures it. /// [AddComponentMenu("AVPro Movie Capture/Capture From Screen", 0)] public class CaptureFromScreen : CaptureBase { //private const int NewFrameSleepTimeMs = 6; private YieldInstruction _waitForEndOfFrame; public override bool PrepareCapture() { if (_capturing) { return false; } if (SystemInfo.graphicsDeviceVersion.StartsWith("OpenGL") && !SystemInfo.graphicsDeviceVersion.Contains("emulated")) { Debug.LogError("[AVProMovieCapture] OpenGL not yet supported for CaptureFromScreen component, please use Direct3D11 instead. You may need to switch your build platform to Windows."); return false; } SelectRecordingResolution(Screen.width, Screen.height); _pixelFormat = NativePlugin.PixelFormat.RGBA32; if (SystemInfo.graphicsDeviceVersion.StartsWith("OpenGL") && !SystemInfo.graphicsDeviceVersion.Contains("emulated")) { // TODO: add this back in once we have fixed opengl support _pixelFormat = NativePlugin.PixelFormat.BGRA32; _isTopDown = true; } else { _isTopDown = false; if (_isDirectX11) { _isTopDown = false; } } GenerateFilename(); _waitForEndOfFrame = new WaitForEndOfFrame(); return base.PrepareCapture(); } public override void UnprepareCapture() { _waitForEndOfFrame = null; base.UnprepareCapture(); } private IEnumerator FinalRenderCapture() { yield return _waitForEndOfFrame; TickFrameTimer(); bool canGrab = true; if (IsUsingMotionBlur()) { // If the motion blur is still accumulating, don't grab this frame canGrab = _motionBlur.IsFrameAccumulated; } if (canGrab && CanOutputFrame()) { // Grab final RenderTexture into texture and encode if (IsRecordingUnityAudio()) { int audioDataLength = 0; System.IntPtr audioDataPtr = _audioCapture.ReadData(out audioDataLength); if (audioDataLength > 0) { NativePlugin.EncodeAudio(_handle, audioDataPtr, (uint)audioDataLength); } } RenderThreadEvent(NativePlugin.PluginEvent.CaptureFrameBuffer); GL.InvalidateState(); UpdateFPS(); } RenormTimer(); //yield return null; } public override void UpdateFrame() { if (_capturing && !_paused) { StartCoroutine(FinalRenderCapture()); } base.UpdateFrame(); } } }