-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCameraExport360.cs
More file actions
146 lines (123 loc) · 3.83 KB
/
CameraExport360.cs
File metadata and controls
146 lines (123 loc) · 3.83 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class CameraExport360 {
[Range(0, 100)]
const int JpegQuality = 100;
const string BlitCubemapToEquirect_ShaderName = "BlitCubemapToEquirect";
#if UNITY_EDITOR
[MenuItem("CONTEXT/Camera/Capture 360 to png...")]
public static void Capture360ToPng(MenuCommand menuCommand)
{
Capture360ToFile(menuCommand, PopX.IO.ImageFileType.png);
}
#endif
#if UNITY_EDITOR
[MenuItem("CONTEXT/Camera/Capture 360 to jpeg...")]
public static void Capture360ToJpeg(MenuCommand menuCommand)
{
Capture360ToFile(menuCommand, PopX.IO.ImageFileType.jpg);
}
#endif
#if UNITY_EDITOR
[MenuItem("CONTEXT/Camera/Capture 360 to exr...")]
public static void Capture360ToExr(MenuCommand menuCommand)
{
Capture360ToFile(menuCommand, PopX.IO.ImageFileType.exr);
}
#endif
#if UNITY_EDITOR
public static void Capture360ToFile(MenuCommand menuCommand,PopX.IO.ImageFileType FileType)
{
var cam = menuCommand.context as Camera;
var Texture = Capture360ToTexture(cam);
var DefaultName = cam.name + "_360";
System.Action<PopX.IO.ImageFileType,string> SaveTextureToFile = (Extension,Filename) =>
{
byte[] Bytes = null;
switch (Extension)
{
case PopX.IO.ImageFileType.jpg:
Bytes = Texture.EncodeToJPG(JpegQuality);
break;
case PopX.IO.ImageFileType.png:
Bytes = Texture.EncodeToPNG();
break;
case PopX.IO.ImageFileType.exr:
Bytes = Texture.EncodeToEXR();
break;
default:
throw new System.Exception("Don't know how to export texture to " + Extension);
}
System.IO.File.WriteAllBytes(Filename, Bytes);
};
PopX.IO.SaveFile(DefaultName,FileType,SaveTextureToFile);
}
#endif
public static Shader GetCubemapToEquirectShader()
{
#if UNITY_EDITOR
var AssetGuids = AssetDatabase.FindAssets(BlitCubemapToEquirect_ShaderName);
foreach (var AssetGuid in AssetGuids)
{
try
{
var AssetPath = AssetDatabase.GUIDToAssetPath(AssetGuid);
var AssetShader = AssetDatabase.LoadAssetAtPath<Shader>(AssetPath);
return AssetShader;
}
catch (System.Exception e)
{
Debug.LogError("Failed to grab shader asset " + AssetGuid + ": " + e.Message );
}
}
throw new System.Exception("Failed to find shader " + BlitCubemapToEquirect_ShaderName);
#else
throw new System.Exception("This is currently editor only. Change caller to allow shader param to allow capture in-player");
#endif
}
public static Texture2D Capture360ToTexture(Camera cam)
{
var rt = RenderTexture.GetTemporary(4096, 4096, 0, RenderTextureFormat.ARGB32);
try
{
Capture360ToRenderTexture(cam, rt);
var Output = PopX.Textures.GetTexture2D(rt, false);
RenderTexture.ReleaseTemporary(rt);
return Output;
}
catch
{
RenderTexture.ReleaseTemporary(rt);
throw;
}
}
public static void Capture360ToRenderTexture(Camera cam,RenderTexture Target)
{
Target.dimension = UnityEngine.Rendering.TextureDimension.Cube;
if ( !cam.RenderToCubemap(Target) )
throw new System.Exception("cam(" + cam.name + ").RenderToCubemap failed");
// take a temp texture, turn it into an equirect and return
var EquirectShader = GetCubemapToEquirectShader();
var EquirectTarget = RenderTexture.GetTemporary(Target.width, Target.height, 0, Target.format);
try
{
var EquirectShader_CubemapUniform = "Cubemap";
var EquirectShader_UseCubemapKeyword = "USE_CUBEMAP";
var EquirectMaterial = new Material(EquirectShader);
EquirectMaterial.SetTexture(EquirectShader_CubemapUniform, Target);
EquirectMaterial.EnableKeyword(EquirectShader_UseCubemapKeyword);
Graphics.Blit(Target, EquirectTarget, EquirectMaterial);
Graphics.Blit( EquirectTarget, Target );
RenderTexture.ReleaseTemporary(EquirectTarget);
}
catch
{
RenderTexture.ReleaseTemporary(EquirectTarget);
throw;
}
}
}