BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / VR / Editor / OVRScreenshotWizard.cs

OVRScreenshotWizard.cs

Project Import
timfa Tim Falken · pushed by timfa · 6 years ago
b77834a1 History
OVRScreenshotWizard.cs 6.3 KiB Download Raw
1/************************************************************************************
2
3Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
4
5Licensed under the Oculus SDK License Version 3.4.1 (the "License");
6you may not use the Oculus SDK except in compliance with the License,
7which is provided at the time of installation or download, or which
8otherwise accompanies this software in either electronic or hard copy form.
9
10You may obtain a copy of the License at
11
12https://developer.oculus.com/licenses/sdk-3.4.1
13
14Unless required by applicable law or agreed to in writing, the Oculus SDK
15distributed under the License is distributed on an "AS IS" BASIS,
16WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17See the License for the specific language governing permissions and
18limitations under the License.
19
20************************************************************************************/
21
22using UnityEngine;
23using UnityEditor;
24using System.IO;
25
26/// <summary>
27/// From the selected transform, takes a cubemap screenshot that can be submitted with the application
28/// as a screenshot (or additionally used for reflection shaders).
29/// </summary>
30class OVRScreenshotWizard : ScriptableWizard
31{
32 public enum TexFormat
33 {
34 JPEG, // 512kb at 1k x 1k resolution vs
35 PNG, // 5.3mb
36 }
37
38 public enum SaveMode {
39 SaveCubemapScreenshot,
40 SaveUnityCubemap,
41 SaveBoth,
42 }
43
44 public GameObject renderFrom = null;
45 public int size = 2048;
46 public SaveMode saveMode = SaveMode.SaveUnityCubemap;
47 public string cubeMapFolder = "Assets/Textures/Cubemaps";
48 public TexFormat textureFormat = TexFormat.PNG;
49
50 /// <summary>
51 /// Validates the user's input
52 /// </summary>
53 void OnWizardUpdate()
54 {
55 helpString = "Select a game object positioned in the place where\nyou want to render the cubemap screenshot from: ";
56 isValid = (renderFrom != null);
57 }
58
59 /// <summary>
60 /// Create the asset path if it is not available.
61 /// Assuming the newFolderPath is stated with "Assets", which is a requirement.
62 /// </summary>
63 static bool CreateAssetPath( string newFolderPath )
64 {
65 const int maxFoldersCount = 32;
66 string currentPath;
67 string[] pathFolders;
68
69 pathFolders = newFolderPath.Split (new char[]{ '/' }, maxFoldersCount);
70
71 if (!string.Equals ("Assets", pathFolders [0], System.StringComparison.OrdinalIgnoreCase))
72 {
73 Debug.LogError( "Folder path has to be started with \" Assets \" " );
74 return false;
75 }
76
77 currentPath = "Assets";
78 for (int i = 1; i < pathFolders.Length; i++)
79 {
80 if (!string.IsNullOrEmpty(pathFolders[i]))
81 {
82 string newPath = currentPath + "/" + pathFolders[i];
83 if (!AssetDatabase.IsValidFolder(newPath))
84 AssetDatabase.CreateFolder(currentPath, pathFolders[i]);
85 currentPath = newPath;
86 }
87 }
88
89 Debug.Log( "Created path: " + currentPath );
90 return true;
91 }
92
93 /// <summary>
94 /// Renders the cubemap
95 /// </summary>
96 void OnWizardCreate()
97 {
98 if ( !AssetDatabase.IsValidFolder( cubeMapFolder ) )
99 {
100 if (!CreateAssetPath(cubeMapFolder))
101 {
102 Debug.LogError( "Created path failed: " + cubeMapFolder );
103 return;
104 }
105 }
106
107 bool existingCamera = true;
108 bool existingCameraStateSave = true;
109 Camera camera = renderFrom.GetComponent<Camera>();
110 if (camera == null)
111 {
112 camera = renderFrom.AddComponent<Camera>();
113 camera.farClipPlane = 10000f;
114 existingCamera = false;
115 }
116 else
117 {
118 existingCameraStateSave = camera.enabled;
119 camera.enabled = true;
120 }
121 // find the last screenshot saved
122 if (cubeMapFolder[cubeMapFolder.Length-1] != '/')
123 {
124 cubeMapFolder += "/";
125 }
126 int idx = 0;
127 string[] fileNames = Directory.GetFiles(cubeMapFolder);
128 foreach(string fileName in fileNames)
129 {
130 if (!fileName.ToLower().EndsWith(".cubemap"))
131 {
132 continue;
133 }
134 string temp = fileName.Replace(cubeMapFolder + "vr_screenshot_", string.Empty);
135 temp = temp.Replace(".cubemap", string.Empty);
136 int tempIdx = 0;
137 if (int.TryParse( temp, out tempIdx ))
138 {
139 if (tempIdx > idx)
140 {
141 idx = tempIdx;
142 }
143 }
144 }
145 string pathName = string.Format("{0}vr_screenshot_{1}.cubemap", cubeMapFolder, (++idx).ToString("d2"));
146 Cubemap cubemap = new Cubemap(size, TextureFormat.RGB24, false);
147
148 // render into cubemap
149 if ((camera != null) && (cubemap != null))
150 {
151 // set up cubemap defaults
152 OVRCubemapCapture.RenderIntoCubemap(camera, cubemap);
153 if (existingCamera)
154 {
155 camera.enabled = existingCameraStateSave;
156 }
157 else
158 {
159 DestroyImmediate(camera);
160 }
161 // generate a regular texture as well?
162 if ( ( saveMode == SaveMode.SaveCubemapScreenshot ) || ( saveMode == SaveMode.SaveBoth ) )
163 {
164 GenerateTexture(cubemap, pathName);
165 }
166
167 if ( ( saveMode == SaveMode.SaveUnityCubemap ) || ( saveMode == SaveMode.SaveBoth ) )
168 {
169 Debug.Log( "Saving: " + pathName );
170 // by default the unity cubemap isn't saved
171 AssetDatabase.CreateAsset( cubemap, pathName );
172 // reimport as necessary
173 AssetDatabase.SaveAssets();
174 // select it in the project tree so developers can find it
175 EditorGUIUtility.PingObject( cubemap );
176 Selection.activeObject = cubemap;
177 }
178 AssetDatabase.Refresh();
179 }
180 }
181
182 /// <summary>
183 /// Generates a NPOT 6x1 cubemap in the following format PX NX PY NY PZ NZ
184 /// </summary>
185 void GenerateTexture(Cubemap cubemap, string pathName)
186 {
187 // Encode the texture and save it to disk
188 pathName = pathName.Replace(".cubemap", (textureFormat == TexFormat.PNG) ? ".png" : ".jpg" ).ToLower();
189 pathName = pathName.Replace( cubeMapFolder.ToLower(), "" );
190 string format = textureFormat.ToString();
191 string fullPath = EditorUtility.SaveFilePanel( string.Format( "Save Cubemap Screenshot as {0}", format ), "", pathName, format.ToLower() );
192 if ( !string.IsNullOrEmpty( fullPath ) )
193 {
194 Debug.Log( "Saving: " + fullPath );
195 OVRCubemapCapture.SaveCubemapCapture(cubemap, fullPath);
196 }
197 }
198
199 /// <summary>
200 /// Unity Editor menu option to take a screenshot
201 /// </summary>
202 [MenuItem("Oculus/Tools/OVR Screenshot Wizard", false, 100000)]
203 static void TakeOVRScreenshot()
204 {
205 OVRScreenshotWizard wizard = ScriptableWizard.DisplayWizard<OVRScreenshotWizard>("OVR Screenshot Wizard", "Render Cubemap");
206 if (wizard != null)
207 {
208 if (Selection.activeGameObject != null)
209 wizard.renderFrom = Selection.activeGameObject;
210 else
211 wizard.renderFrom = Camera.main.gameObject;
212
213 wizard.isValid = (wizard.renderFrom != null);
214 }
215 }
216}