-
Notifications
You must be signed in to change notification settings - Fork 2
/
BigImageRecorder.cs
47 lines (43 loc) · 1.83 KB
/
BigImageRecorder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System.IO;
using UnityEditor.Recorder;
using UnityEngine;
namespace UnityEditor.BigImageRecorder
{
/// <summary>
/// Recorder to capture image sequences that exceed Unity's maximum texture size.
/// </summary>
class BigImageRecorder : GenericRecorder<BigImageRecorderSettings>
{
protected override void RecordFrame(RecordingSession session)
{
var path = Settings.FileNameGenerator.BuildAbsolutePath(session);
var input = m_Inputs[0] as BigCameraInput;
for (var row = 0; row < input.InputSettings.RowCount; row++)
{
for (var column = 0; column < input.InputSettings.ColumnCount; column++)
{
var sectionPath = AddPathSuffix(path, $"_{row}-{column}");
var renderTexture = input.OutputRenderTextures[row, column];
var texture = RenderTextureToTexture2D(renderTexture);
var bytes = texture.EncodeToPNG();
File.WriteAllBytes(sectionPath, bytes);
}
}
}
static string AddPathSuffix(string path, string suffix)
{
var directoryName = Path.GetDirectoryName(path);
var fileName = Path.GetFileNameWithoutExtension(path);
var extension = Path.GetExtension(path);
return Path.Combine(directoryName, fileName + suffix + extension);
}
static Texture2D RenderTextureToTexture2D(RenderTexture renderTexture)
{
var texture = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.RGBA32, false);
RenderTexture.active = renderTexture;
texture.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
texture.Apply();
return texture;
}
}
}