Utils.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. using UnityEngine;
  2. using System.IO;
  3. using System.Diagnostics;
  4. using System.Runtime.InteropServices;
  5. //-----------------------------------------------------------------------------
  6. // Copyright 2012-2022 RenderHeads Ltd. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. namespace RenderHeads.Media.AVProMovieCapture
  9. {
  10. public class Utils
  11. {
  12. public static string[] WindowsImageSequenceFormatNames = new string[] { "PNG (uncompressed)" };
  13. public static string[] MacOSImageSequenceFormatNames = new string[] { "PNG", "JPEG", "TIFF", "HEIF" };
  14. public static string[] IOSImageSequenceFormatNames = new string[] { "PNG", "JPEG", "TIFF", "HEIF" };
  15. public static string[] AndroidImageSequenceFormatNames = new string[] { "PNG", "JPEG" };
  16. public static string[] GetNativeImageSequenceFormatNames()
  17. {
  18. string[] result = null;
  19. #if UNITY_EDITOR
  20. #if UNITY_EDITOR_WIN
  21. result = WindowsImageSequenceFormatNames;
  22. #elif UNITY_EDITOR_OSX
  23. result = MacOSImageSequenceFormatNames;
  24. #endif
  25. #else
  26. #if UNITY_STANDALONE_WIN
  27. result = WindowsImageSequenceFormatNames;
  28. #elif UNITY_STANDALONE_OSX
  29. result = MacOSImageSequenceFormatNames;
  30. #elif UNITY_IOS
  31. result = IOSImageSequenceFormatNames;
  32. #elif UNITY_ANDROID
  33. result = AndroidImageSequenceFormatNames;
  34. #endif
  35. #endif
  36. if (result == null)
  37. {
  38. result = new string[0];
  39. }
  40. return result;
  41. }
  42. public static bool HasAlphaChannel(RenderTextureFormat format)
  43. {
  44. bool result = false;
  45. switch (format)
  46. {
  47. case RenderTextureFormat.ARGB32:
  48. case RenderTextureFormat.BGRA32:
  49. case RenderTextureFormat.ARGB4444:
  50. case RenderTextureFormat.ARGB1555:
  51. case RenderTextureFormat.ARGB2101010:
  52. case RenderTextureFormat.ARGB64:
  53. case RenderTextureFormat.RGBAUShort:
  54. case RenderTextureFormat.ARGBInt:
  55. case RenderTextureFormat.ARGBFloat:
  56. case RenderTextureFormat.ARGBHalf:
  57. #if UNITY_2017_2_OR_NEWER
  58. case RenderTextureFormat.BGRA10101010_XR:
  59. #endif
  60. result = true;
  61. break;
  62. }
  63. return result;
  64. }
  65. public static RenderTextureFormat GetBestRenderTextureFormat(bool supportHDR, bool supportTransparency, bool favourSpeedOverQuality)
  66. {
  67. RenderTextureFormat result = RenderTextureFormat.Default;
  68. if (supportTransparency)
  69. {
  70. if (supportHDR)
  71. {
  72. // HDR Transparent
  73. result = RenderTextureFormat.DefaultHDR;
  74. if (favourSpeedOverQuality)
  75. {
  76. #if UNITY_2017_2_OR_NEWER
  77. if (SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.BGRA10101010_XR)) return RenderTextureFormat.BGRA10101010_XR;
  78. #endif
  79. if (SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGBHalf)) return RenderTextureFormat.ARGBHalf;
  80. if (SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGBFloat)) return RenderTextureFormat.ARGBFloat;
  81. }
  82. else
  83. {
  84. if (SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGBFloat)) return RenderTextureFormat.ARGBFloat;
  85. if (SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGBHalf)) return RenderTextureFormat.ARGBHalf;
  86. #if UNITY_2017_2_OR_NEWER
  87. if (SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.BGRA10101010_XR)) return RenderTextureFormat.BGRA10101010_XR;
  88. #endif
  89. if (SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGB64)) return RenderTextureFormat.ARGB64;
  90. if (SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGB2101010)) return RenderTextureFormat.ARGB2101010;
  91. }
  92. }
  93. else
  94. {
  95. // SDR Transparent
  96. if (SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGB32)) return RenderTextureFormat.ARGB32;
  97. if (SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.BGRA32)) return RenderTextureFormat.BGRA32;
  98. }
  99. }
  100. else
  101. {
  102. if (supportHDR)
  103. {
  104. // HDR non-transparent
  105. result = RenderTextureFormat.DefaultHDR;
  106. /*if (favourSpeedOverQuality)
  107. {
  108. if (SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.RGB111110Float)) return RenderTextureFormat.RGB111110Float;
  109. if (SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGB2101010)) return RenderTextureFormat.ARGB2101010;
  110. if (SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGBHalf)) return RenderTextureFormat.ARGBHalf;
  111. if (SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGBFloat)) return RenderTextureFormat.ARGBFloat;
  112. }
  113. else
  114. {
  115. if (SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGBFloat)) return RenderTextureFormat.ARGBFloat;
  116. if (SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGBHalf)) return RenderTextureFormat.ARGBHalf;
  117. if (SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGB2101010)) return RenderTextureFormat.ARGB2101010;
  118. if (SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.RGB111110Float)) return RenderTextureFormat.RGB111110Float;
  119. }*/
  120. }
  121. }
  122. if (!SystemInfo.SupportsRenderTextureFormat(result))
  123. {
  124. UnityEngine.Debug.LogError("[AVProMovieCapture] Couldn't find suitable texture format " + result + " for " + supportHDR + " " + supportTransparency + " " + favourSpeedOverQuality);
  125. }
  126. return result;
  127. }
  128. /// <summary>
  129. /// The "main" camera isn't necessarily the one the gets rendered last to screen,
  130. /// so we sort all cameras by depth and find the one with no render target
  131. /// </summary>
  132. public static Camera GetUltimateRenderCamera()
  133. {
  134. Camera result = Camera.main;
  135. {
  136. // Iterate all enabled cameras
  137. float highestDepth = float.MinValue;
  138. Camera[] enabledCameras = Camera.allCameras;
  139. for (int cameraIndex = 0; cameraIndex < enabledCameras.Length; cameraIndex++)
  140. {
  141. Camera camera = enabledCameras[cameraIndex];
  142. // Ignore null cameras
  143. if (camera != null)
  144. {
  145. // Ignore cameras that are hidden or have a targetTexture
  146. bool isHidden = (camera.hideFlags & HideFlags.HideInHierarchy) == HideFlags.HideInHierarchy;
  147. if (!isHidden && camera.targetTexture == null)
  148. {
  149. // Ignore cameras that render nothing
  150. if (camera.pixelRect.width > 0f && camera.pixelHeight > 0f)
  151. {
  152. // Keep the one with highest depth
  153. // TODO: handle the case where camera depths are equal - which one is first then?
  154. if (camera.depth >= highestDepth)
  155. {
  156. highestDepth = camera.depth;
  157. result = camera;
  158. }
  159. }
  160. }
  161. }
  162. }
  163. }
  164. return result;
  165. }
  166. public static bool HasContributingCameras(Camera parentCamera)
  167. {
  168. bool result = true;
  169. // If the camera doesn't clear the target completely then it may have other contributing cameras
  170. if (parentCamera.rect == new Rect(0f, 0f, 1f, 1f))
  171. {
  172. if (parentCamera.clearFlags == CameraClearFlags.Skybox ||
  173. parentCamera.clearFlags == CameraClearFlags.Color)
  174. {
  175. result = false;
  176. }
  177. }
  178. return result;
  179. }
  180. /// <summary>
  181. /// Returns a list of cameras sorted in render order from first to last that contribute to the rendering to parentCamera
  182. /// </summary>
  183. public static Camera[] FindContributingCameras(Camera parentCamera)
  184. {
  185. System.Collections.Generic.List<Camera> validCameras = new System.Collections.Generic.List<Camera>(8);
  186. {
  187. // Iterate all enabled/disabled cameras (they may become enabled later on)
  188. Camera[] allCameras = (Camera[])Resources.FindObjectsOfTypeAll(typeof(Camera));
  189. for (int cameraIndex = 0; cameraIndex < allCameras.Length; cameraIndex++)
  190. {
  191. Camera camera = allCameras[cameraIndex];
  192. // Ignore null cameras and camera that is the parent
  193. if (camera != null && camera != parentCamera)
  194. {
  195. // Only allow cameras with depth less or equal to parent camera
  196. if (camera.depth <= parentCamera.depth)
  197. {
  198. // Ignore cameras that are hidden or have a targetTexture that doesn't match the parent
  199. bool isHidden = (camera.hideFlags & HideFlags.HideInHierarchy) == HideFlags.HideInHierarchy;
  200. if (!isHidden && camera.targetTexture == parentCamera.targetTexture)
  201. {
  202. // Ignore cameras that render nothing
  203. if (camera.pixelRect.width > 0 && camera.pixelHeight > 0)
  204. {
  205. validCameras.Add(camera);
  206. }
  207. }
  208. }
  209. }
  210. }
  211. }
  212. if (validCameras.Count > 1)
  213. {
  214. // Sort by depth (render order)
  215. // TODO: handle the case where camera depths are equal - which one is first then?
  216. validCameras.Sort(delegate (Camera a, Camera b)
  217. {
  218. if (a != b) // Pre .Net 4.6.2 Sort() can compare an elements with itself
  219. {
  220. if (a.depth < b.depth)
  221. {
  222. return -1;
  223. }
  224. else if (a.depth > b.depth)
  225. {
  226. return 1;
  227. }
  228. else if (a.depth == b.depth)
  229. {
  230. UnityEngine.Debug.LogWarning("[AVProMovieCapture] Cameras '" + a.name + "' and '" + b.name + "' have the same depth value - unable to determine render order: " + a.depth);
  231. }
  232. }
  233. return 0;
  234. });
  235. // Starting from the last camera to render, find the first one that clears the screen completely
  236. for (int i = (validCameras.Count - 1); i >= 0; i--)
  237. {
  238. if (validCameras[i].rect == new Rect(0f, 0f, 1f, 1f))
  239. {
  240. if (validCameras[i].clearFlags == CameraClearFlags.Skybox ||
  241. validCameras[i].clearFlags == CameraClearFlags.Color)
  242. {
  243. // Remove all cameras before this
  244. validCameras.RemoveRange(0, i);
  245. break;
  246. }
  247. }
  248. }
  249. }
  250. return validCameras.ToArray();
  251. }
  252. #if UNITY_EDITOR_OSX || (UNITY_STANDALONE_OSX && !UNITY_EDITOR)
  253. private static string PhotosScheme { get { return "photos://"; } }
  254. private static string FileScheme { get { return "file://"; } }
  255. #elif UNITY_IOS && !UNITY_EDITOR
  256. private static string PhotosScheme { get { return "photos-redirect://"; } }
  257. private static string FileScheme { get { return "shareddocuments://"; } }
  258. #endif
  259. private static string URLEscapePathByPercentEncoding(string path)
  260. {
  261. return path.Replace("%", "%25") // Escape percents first so escaped character's percents aren't themselves escaped
  262. .Replace(" ", "%20")
  263. .Replace("\"", "%22")
  264. .Replace("#", "%23")
  265. .Replace(";", "%3B")
  266. .Replace("<", "%3C")
  267. .Replace(">", "%3E")
  268. .Replace("?", "%3F")
  269. .Replace("[", "%5B")
  270. .Replace("\\", "%5C")
  271. .Replace("]", "%5D")
  272. .Replace("^", "%5E")
  273. .Replace("`", "%60")
  274. .Replace("{", "%7B")
  275. .Replace("|", "%7C")
  276. .Replace("}", "%7D");
  277. }
  278. public static bool ShowInExplorer(string itemPath)
  279. {
  280. bool result = false;
  281. #if UNITY_EDITOR_OSX || (!UNITY_EDITOR && (UNITY_STANDALONE_OSX || UNITY_IOS))
  282. string url;
  283. if (itemPath.StartsWith("avpmc-photolibrary://"))
  284. {
  285. // TODO: Find out how to open photos to a specific album/item
  286. url = PhotosScheme;
  287. }
  288. else
  289. {
  290. url = FileScheme + URLEscapePathByPercentEncoding(Directory.GetParent(itemPath).FullName);
  291. }
  292. UnityEngine.Debug.LogFormat("ShowInExplorer - opening URL: {0}", url);
  293. UnityEngine.Application.OpenURL(url);
  294. result = true;
  295. #else
  296. #if !UNITY_WEBPLAYER
  297. itemPath = Path.GetFullPath(itemPath.Replace(@"/", @"\")); // explorer doesn't like front slashes
  298. if (File.Exists(itemPath))
  299. {
  300. #if UNITY_EDITOR_WIN || (!UNITY_EDITOR && UNITY_STANDALONE_WIN)
  301. Process.Start("explorer.exe", "/select," + itemPath);
  302. #else
  303. #endif
  304. result = true;
  305. }
  306. else if (Directory.Exists(itemPath))
  307. {
  308. // NOTE: We use OpenURL() instead of the explorer process so that it opens explorer inside the folder
  309. UnityEngine.Application.OpenURL(itemPath);
  310. result = true;
  311. }
  312. #endif
  313. #endif // UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
  314. return result;
  315. }
  316. public static bool OpenInDefaultApp(string itemPath)
  317. {
  318. bool result = false;
  319. #if UNITY_EDITOR_OSX || (!UNITY_EDITOR && (UNITY_STANDALONE_OSX || UNITY_IOS))
  320. string url;
  321. if (itemPath.StartsWith("avpmc-photolibrary://"))
  322. {
  323. // TODO: Find out how to open photos to a specific album/item
  324. url = PhotosScheme;
  325. }
  326. else
  327. {
  328. url = FileScheme + URLEscapePathByPercentEncoding(itemPath);
  329. }
  330. UnityEngine.Debug.LogFormat("OpenInDefaultApp - opening URL: {0}", url);
  331. UnityEngine.Application.OpenURL(url);
  332. result = true;
  333. #else
  334. itemPath = Path.GetFullPath(itemPath.Replace(@"/", @"\"));
  335. if (File.Exists(itemPath))
  336. {
  337. UnityEngine.Application.OpenURL(itemPath);
  338. result = true;
  339. }
  340. else if (Directory.Exists(itemPath))
  341. {
  342. UnityEngine.Application.OpenURL(itemPath);
  343. result = true;
  344. }
  345. #endif
  346. return result;
  347. }
  348. public static long GetFileSize(string filename)
  349. {
  350. #if UNITY_WEBPLAYER
  351. return 0;
  352. #else
  353. System.IO.FileInfo fi = new System.IO.FileInfo(filename);
  354. return fi.Length;
  355. #endif
  356. }
  357. #if UNITY_EDITOR_WIN || (!UNITY_EDITOR && UNITY_STANDALONE_WIN)
  358. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  359. [return: MarshalAs(UnmanagedType.Bool)]
  360. private static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
  361. out ulong lpFreeBytesAvailable,
  362. out ulong lpTotalNumberOfBytes,
  363. out ulong lpTotalNumberOfFreeBytes);
  364. public static bool DriveFreeBytes(string folderName, out ulong freespace)
  365. {
  366. freespace = 0;
  367. if (string.IsNullOrEmpty(folderName))
  368. {
  369. throw new System.ArgumentNullException("folderName");
  370. }
  371. if (!folderName.EndsWith("\\"))
  372. {
  373. folderName += '\\';
  374. }
  375. ulong free = 0, dummy1 = 0, dummy2 = 0;
  376. if (GetDiskFreeSpaceEx(folderName, out free, out dummy1, out dummy2))
  377. {
  378. freespace = free;
  379. return true;
  380. }
  381. else
  382. {
  383. return false;
  384. }
  385. }
  386. #endif
  387. public static string GetImageFileExtension(ImageSequenceFormat format)
  388. {
  389. string result = string.Empty;
  390. switch (format)
  391. {
  392. case ImageSequenceFormat.PNG:
  393. result = "png";
  394. break;
  395. case ImageSequenceFormat.JPEG:
  396. result = "jpg";
  397. break;
  398. case ImageSequenceFormat.TIFF:
  399. result = "tiff";
  400. break;
  401. case ImageSequenceFormat.HEIF:
  402. result = "heif";
  403. break;
  404. }
  405. return result;
  406. }
  407. }
  408. }