OVRCubemapCapture.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using System;
  2. using System.IO;
  3. using System.Runtime.CompilerServices;
  4. using UnityEngine;
  5. using UnityEngine.Rendering;
  6. public class OVRCubemapCapture : MonoBehaviour
  7. {
  8. private void Update()
  9. {
  10. if (this.autoTriggerAfterLaunch)
  11. {
  12. this.autoTriggerElapse += Time.deltaTime;
  13. if (this.autoTriggerElapse >= this.autoTriggerDelay)
  14. {
  15. this.autoTriggerAfterLaunch = false;
  16. OVRCubemapCapture.TriggerCubemapCapture(base.transform.position, this.cubemapSize, this.pathName);
  17. }
  18. }
  19. if (Input.GetKeyDown(this.triggeredByKey))
  20. {
  21. OVRCubemapCapture.TriggerCubemapCapture(base.transform.position, this.cubemapSize, this.pathName);
  22. }
  23. }
  24. public static void TriggerCubemapCapture(Vector3 capturePos, int cubemapSize = 2048, string pathName = null)
  25. {
  26. GameObject gameObject = new GameObject("CubemapCamera", new Type[]
  27. {
  28. typeof(Camera)
  29. });
  30. gameObject.hideFlags = HideFlags.HideAndDontSave;
  31. gameObject.transform.position = capturePos;
  32. gameObject.transform.rotation = Quaternion.identity;
  33. Camera component = gameObject.GetComponent<Camera>();
  34. component.farClipPlane = 10000f;
  35. component.enabled = false;
  36. Cubemap cubemap = new Cubemap(cubemapSize, TextureFormat.RGB24, false);
  37. OVRCubemapCapture.RenderIntoCubemap(component, cubemap);
  38. OVRCubemapCapture.SaveCubemapCapture(cubemap, pathName);
  39. UnityEngine.Object.DestroyImmediate(cubemap);
  40. UnityEngine.Object.DestroyImmediate(gameObject);
  41. }
  42. public static void RenderIntoCubemap(Camera ownerCamera, Cubemap outCubemap)
  43. {
  44. int width = outCubemap.width;
  45. int height = outCubemap.height;
  46. CubemapFace[] array = new CubemapFace[6];
  47. RuntimeHelpers.InitializeArray(array, fieldof(<PrivateImplementationDetails>.$field-DB17E883A647963A26D973378923EF4649801319).FieldHandle);
  48. CubemapFace[] array2 = array;
  49. Vector3[] array3 = new Vector3[]
  50. {
  51. new Vector3(0f, 90f, 0f),
  52. new Vector3(0f, -90f, 0f),
  53. new Vector3(-90f, 0f, 0f),
  54. new Vector3(90f, 0f, 0f),
  55. new Vector3(0f, 0f, 0f),
  56. new Vector3(0f, 180f, 0f)
  57. };
  58. RenderTexture active = RenderTexture.active;
  59. float fieldOfView = ownerCamera.fieldOfView;
  60. float aspect = ownerCamera.aspect;
  61. Quaternion rotation = ownerCamera.transform.rotation;
  62. RenderTexture renderTexture = new RenderTexture(width, height, 24);
  63. renderTexture.antiAliasing = 8;
  64. renderTexture.dimension = TextureDimension.Tex2D;
  65. renderTexture.hideFlags = HideFlags.HideAndDontSave;
  66. Texture2D texture2D = new Texture2D(width, height, TextureFormat.RGB24, false);
  67. texture2D.hideFlags = HideFlags.HideAndDontSave;
  68. ownerCamera.targetTexture = renderTexture;
  69. ownerCamera.fieldOfView = 90f;
  70. ownerCamera.aspect = 1f;
  71. Color[] array4 = new Color[texture2D.height * texture2D.width];
  72. for (int i = 0; i < array2.Length; i++)
  73. {
  74. ownerCamera.transform.eulerAngles = array3[i];
  75. ownerCamera.Render();
  76. RenderTexture.active = renderTexture;
  77. texture2D.ReadPixels(new Rect(0f, 0f, (float)width, (float)height), 0, 0);
  78. Color[] pixels = texture2D.GetPixels();
  79. for (int j = 0; j < height; j++)
  80. {
  81. for (int k = 0; k < width; k++)
  82. {
  83. array4[j * width + k] = pixels[(height - 1 - j) * width + k];
  84. }
  85. }
  86. outCubemap.SetPixels(array4, array2[i]);
  87. }
  88. outCubemap.SmoothEdges();
  89. RenderTexture.active = active;
  90. ownerCamera.fieldOfView = fieldOfView;
  91. ownerCamera.aspect = aspect;
  92. ownerCamera.transform.rotation = rotation;
  93. ownerCamera.targetTexture = active;
  94. UnityEngine.Object.DestroyImmediate(texture2D);
  95. UnityEngine.Object.DestroyImmediate(renderTexture);
  96. }
  97. public static bool SaveCubemapCapture(Cubemap cubemap, string pathName = null)
  98. {
  99. int width = cubemap.width;
  100. int height = cubemap.height;
  101. int num = 0;
  102. int y = 0;
  103. bool flag = true;
  104. string text;
  105. string text2;
  106. if (string.IsNullOrEmpty(pathName))
  107. {
  108. text = Application.persistentDataPath + "/OVR_ScreenShot360/";
  109. text2 = null;
  110. }
  111. else
  112. {
  113. text = Path.GetDirectoryName(pathName);
  114. text2 = Path.GetFileName(pathName);
  115. if (text[text.Length - 1] != '/' || text[text.Length - 1] != '\\')
  116. {
  117. text += "/";
  118. }
  119. }
  120. if (string.IsNullOrEmpty(text2))
  121. {
  122. text2 = "OVR_" + DateTime.Now.ToString("hh_mm_ss") + ".png";
  123. }
  124. string extension = Path.GetExtension(text2);
  125. if (extension == ".png")
  126. {
  127. flag = true;
  128. }
  129. else
  130. {
  131. if (!(extension == ".jpg"))
  132. {
  133. Debug.LogError("Unsupported file format" + extension);
  134. return false;
  135. }
  136. flag = false;
  137. }
  138. try
  139. {
  140. Directory.CreateDirectory(text);
  141. }
  142. catch (Exception ex)
  143. {
  144. Debug.LogError("Failed to create path " + text + " since " + ex.ToString());
  145. return false;
  146. }
  147. Texture2D texture2D = new Texture2D(width * 6, height, TextureFormat.RGB24, false);
  148. if (texture2D == null)
  149. {
  150. Debug.LogError("[OVRScreenshotWizard] Failed creating the texture!");
  151. return false;
  152. }
  153. CubemapFace[] array = new CubemapFace[6];
  154. RuntimeHelpers.InitializeArray(array, fieldof(<PrivateImplementationDetails>.$field-DB17E883A647963A26D973378923EF4649801319).FieldHandle);
  155. CubemapFace[] array2 = array;
  156. for (int i = 0; i < array2.Length; i++)
  157. {
  158. Color[] pixels = cubemap.GetPixels(array2[i]);
  159. Color[] array3 = new Color[pixels.Length];
  160. for (int j = 0; j < height; j++)
  161. {
  162. for (int k = 0; k < width; k++)
  163. {
  164. array3[j * width + k] = pixels[(height - 1 - j) * width + k];
  165. }
  166. }
  167. texture2D.SetPixels(num, y, width, height, array3);
  168. num += width;
  169. }
  170. try
  171. {
  172. byte[] bytes = (!flag) ? texture2D.EncodeToJPG() : texture2D.EncodeToPNG();
  173. File.WriteAllBytes(text + text2, bytes);
  174. Debug.Log("Cubemap file created " + text + text2);
  175. }
  176. catch (Exception ex2)
  177. {
  178. Debug.LogError("Failed to save cubemap file since " + ex2.ToString());
  179. return false;
  180. }
  181. UnityEngine.Object.DestroyImmediate(texture2D);
  182. return true;
  183. }
  184. public bool autoTriggerAfterLaunch = true;
  185. public float autoTriggerDelay = 1f;
  186. private float autoTriggerElapse;
  187. public KeyCode triggeredByKey = KeyCode.F8;
  188. public string pathName;
  189. public int cubemapSize = 2048;
  190. }