OVRScreenshotWizard.cs 6.3 KB

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