OVRScreenshotWizard.cs
6.31 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/************************************************************************************
Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
Licensed under the Oculus SDK License Version 3.4.1 (the "License");
you may not use the Oculus SDK except in compliance with the License,
which is provided at the time of installation or download, or which
otherwise accompanies this software in either electronic or hard copy form.
You may obtain a copy of the License at
https://developer.oculus.com/licenses/sdk-3.4.1
Unless required by applicable law or agreed to in writing, the Oculus SDK
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
************************************************************************************/
using UnityEngine;
using UnityEditor;
using System.IO;
/// <summary>
/// From the selected transform, takes a cubemap screenshot that can be submitted with the application
/// as a screenshot (or additionally used for reflection shaders).
/// </summary>
class OVRScreenshotWizard : ScriptableWizard
{
public enum TexFormat
{
JPEG, // 512kb at 1k x 1k resolution vs
PNG, // 5.3mb
}
public enum SaveMode {
SaveCubemapScreenshot,
SaveUnityCubemap,
SaveBoth,
}
public GameObject renderFrom = null;
public int size = 2048;
public SaveMode saveMode = SaveMode.SaveUnityCubemap;
public string cubeMapFolder = "Assets/Textures/Cubemaps";
public TexFormat textureFormat = TexFormat.PNG;
/// <summary>
/// Validates the user's input
/// </summary>
void OnWizardUpdate()
{
helpString = "Select a game object positioned in the place where\nyou want to render the cubemap screenshot from: ";
isValid = (renderFrom != null);
}
/// <summary>
/// Create the asset path if it is not available.
/// Assuming the newFolderPath is stated with "Assets", which is a requirement.
/// </summary>
static bool CreateAssetPath( string newFolderPath )
{
const int maxFoldersCount = 32;
string currentPath;
string[] pathFolders;
pathFolders = newFolderPath.Split (new char[]{ '/' }, maxFoldersCount);
if (!string.Equals ("Assets", pathFolders [0], System.StringComparison.OrdinalIgnoreCase))
{
Debug.LogError( "Folder path has to be started with \" Assets \" " );
return false;
}
currentPath = "Assets";
for (int i = 1; i < pathFolders.Length; i++)
{
if (!string.IsNullOrEmpty(pathFolders[i]))
{
string newPath = currentPath + "/" + pathFolders[i];
if (!AssetDatabase.IsValidFolder(newPath))
AssetDatabase.CreateFolder(currentPath, pathFolders[i]);
currentPath = newPath;
}
}
Debug.Log( "Created path: " + currentPath );
return true;
}
/// <summary>
/// Renders the cubemap
/// </summary>
void OnWizardCreate()
{
if ( !AssetDatabase.IsValidFolder( cubeMapFolder ) )
{
if (!CreateAssetPath(cubeMapFolder))
{
Debug.LogError( "Created path failed: " + cubeMapFolder );
return;
}
}
bool existingCamera = true;
bool existingCameraStateSave = true;
Camera camera = renderFrom.GetComponent<Camera>();
if (camera == null)
{
camera = renderFrom.AddComponent<Camera>();
camera.farClipPlane = 10000f;
existingCamera = false;
}
else
{
existingCameraStateSave = camera.enabled;
camera.enabled = true;
}
// find the last screenshot saved
if (cubeMapFolder[cubeMapFolder.Length-1] != '/')
{
cubeMapFolder += "/";
}
int idx = 0;
string[] fileNames = Directory.GetFiles(cubeMapFolder);
foreach(string fileName in fileNames)
{
if (!fileName.ToLower().EndsWith(".cubemap"))
{
continue;
}
string temp = fileName.Replace(cubeMapFolder + "vr_screenshot_", string.Empty);
temp = temp.Replace(".cubemap", string.Empty);
int tempIdx = 0;
if (int.TryParse( temp, out tempIdx ))
{
if (tempIdx > idx)
{
idx = tempIdx;
}
}
}
string pathName = string.Format("{0}vr_screenshot_{1}.cubemap", cubeMapFolder, (++idx).ToString("d2"));
Cubemap cubemap = new Cubemap(size, TextureFormat.RGB24, false);
// render into cubemap
if ((camera != null) && (cubemap != null))
{
// set up cubemap defaults
OVRCubemapCapture.RenderIntoCubemap(camera, cubemap);
if (existingCamera)
{
camera.enabled = existingCameraStateSave;
}
else
{
DestroyImmediate(camera);
}
// generate a regular texture as well?
if ( ( saveMode == SaveMode.SaveCubemapScreenshot ) || ( saveMode == SaveMode.SaveBoth ) )
{
GenerateTexture(cubemap, pathName);
}
if ( ( saveMode == SaveMode.SaveUnityCubemap ) || ( saveMode == SaveMode.SaveBoth ) )
{
Debug.Log( "Saving: " + pathName );
// by default the unity cubemap isn't saved
AssetDatabase.CreateAsset( cubemap, pathName );
// reimport as necessary
AssetDatabase.SaveAssets();
// select it in the project tree so developers can find it
EditorGUIUtility.PingObject( cubemap );
Selection.activeObject = cubemap;
}
AssetDatabase.Refresh();
}
}
/// <summary>
/// Generates a NPOT 6x1 cubemap in the following format PX NX PY NY PZ NZ
/// </summary>
void GenerateTexture(Cubemap cubemap, string pathName)
{
// Encode the texture and save it to disk
pathName = pathName.Replace(".cubemap", (textureFormat == TexFormat.PNG) ? ".png" : ".jpg" ).ToLower();
pathName = pathName.Replace( cubeMapFolder.ToLower(), "" );
string format = textureFormat.ToString();
string fullPath = EditorUtility.SaveFilePanel( string.Format( "Save Cubemap Screenshot as {0}", format ), "", pathName, format.ToLower() );
if ( !string.IsNullOrEmpty( fullPath ) )
{
Debug.Log( "Saving: " + fullPath );
OVRCubemapCapture.SaveCubemapCapture(cubemap, fullPath);
}
}
/// <summary>
/// Unity Editor menu option to take a screenshot
/// </summary>
[MenuItem("Oculus/Tools/OVR Screenshot Wizard", false, 100000)]
static void TakeOVRScreenshot()
{
OVRScreenshotWizard wizard = ScriptableWizard.DisplayWizard<OVRScreenshotWizard>("OVR Screenshot Wizard", "Render Cubemap");
if (wizard != null)
{
if (Selection.activeGameObject != null)
wizard.renderFrom = Selection.activeGameObject;
else
wizard.renderFrom = Camera.main.gameObject;
wizard.isValid = (wizard.renderFrom != null);
}
}
}