-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cs
More file actions
70 lines (63 loc) · 2.94 KB
/
Utils.cs
File metadata and controls
70 lines (63 loc) · 2.94 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using UnityEngine;
namespace at.ph3.unity.shadow.res
{
public class Utils
{
// From here: https://discussions.unity.com/t/save-rendertexture-or-texture2d-as-image-file-utility/891718
// just for quick debugging; synchronous is fine for this, it's good to actually notice when it's done
public enum SaveTextureFileFormat
{
EXR, JPG, PNG, TGA
};
/// <summary>
/// Saves a Texture2D to disk with the specified filename and image format
/// </summary>
/// <param name="tex"></param>
/// <param name="filePath"></param>
/// <param name="fileFormat"></param>
/// <param name="jpgQuality"></param>
static public void SaveTexture2DToFile(Texture2D tex, string filePath, SaveTextureFileFormat fileFormat, int jpgQuality = 95)
{
switch (fileFormat)
{
case SaveTextureFileFormat.EXR:
System.IO.File.WriteAllBytes(filePath + ".exr", tex.EncodeToEXR());
break;
case SaveTextureFileFormat.JPG:
System.IO.File.WriteAllBytes(filePath + ".jpg", tex.EncodeToJPG(jpgQuality));
break;
case SaveTextureFileFormat.PNG:
System.IO.File.WriteAllBytes(filePath + ".png", tex.EncodeToPNG());
break;
case SaveTextureFileFormat.TGA:
System.IO.File.WriteAllBytes(filePath + ".tga", tex.EncodeToTGA());
break;
}
}
/// <summary>
/// Saves a RenderTexture to disk with the specified filename and image format
/// </summary>
/// <param name="renderTexture"></param>
/// <param name="filePath"></param>
/// <param name="fileFormat"></param>
/// <param name="jpgQuality"></param>
static public void SaveRenderTextureToFile(RenderTexture renderTexture, string filePath, SaveTextureFileFormat fileFormat = SaveTextureFileFormat.PNG, int jpgQuality = 95)
{
Texture2D tex;
if (fileFormat != SaveTextureFileFormat.EXR)
tex = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.ARGB32, false, false);
else
tex = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.RGBAFloat, false, true);
var oldRt = RenderTexture.active;
RenderTexture.active = renderTexture;
tex.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
tex.Apply();
RenderTexture.active = oldRt;
SaveTexture2DToFile(tex, filePath, fileFormat, jpgQuality);
if (Application.isPlaying)
UnityEngine.Object.Destroy(tex);
else
UnityEngine.Object.DestroyImmediate(tex);
}
}
}