I have found a lot of questions and answers on this topic, but no complete recommended way to do this. We are rendering HD frames and want to save them to disk in raw format (i.e. not JPEG or PNG) at 60FPS. right now we are getting around 8FPS with the following code:
IEnumerator RecordFrame()
{
yield return new WaitForEndOfFrame();
var renderTexture = new RenderTexture(Screen.width, Screen.height, 32, RenderTextureFormat.ARGB32);
cam.targetTexture = renderTexture;
cam.Render();
cam.targetTexture = null;
RenderTexture.active = renderTexture;
Texture2D screenshot = new Texture2D(Screen.width, Screen.height, TextureFormat.RGBA32, false);
screenshot.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
screenshot.Apply();
RenderTexture.active = null;
String path = "Screenshot_" + m_num + ".bin";
byte[] bytes = screenshot.GetRawTextureData();
File.WriteAllBytes(path, bytes);
m_num++;
}
public void LateUpdate()
{
StartCoroutine(RecordFrame());
}
From past posts it seems that ReadPixels is the culprit, and needs optimization. I am trying glReadPixels but it returns a black frame, like the one in this post: glReadPixels in Unity 5 always get black frame - Questions & Answers - Unity Discussions
Is there a definitive answer to this question? Or any asset in the store that can achieve this functionality out of the box?