PolyverseSkiesGenerator.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #if UNITY_EDITOR
  2. using UnityEditor;
  3. using UnityEngine;
  4. using Boxophobic.StyledGUI;
  5. using System.IO;
  6. namespace PolyverseSkies
  7. {
  8. public enum CubemapType
  9. {
  10. SkyboxCubemap = 10,
  11. CloudsCubemap = 20,
  12. }
  13. public enum CubemapSizes
  14. {
  15. _32 = 32,
  16. _64 = 64,
  17. _128 = 128,
  18. _256 = 256,
  19. _512 = 512,
  20. _1024 = 1024,
  21. _2048 = 2048,
  22. }
  23. [ExecuteInEditMode]
  24. public class PolyverseSkiesGenerator : StyledMonoBehaviour
  25. {
  26. [StyledBanner(0.968f, 0.572f, 0.890f, "Cubemap Generator", "", "https://docs.google.com/document/d/1z7A_xKNa2mXhvTRJqyu-ZQsAtbV32tEZQbO1OmPS_-s/edit#heading=h.8h46nz99pbdp")]
  27. public bool styledBanner;
  28. [StyledMessage("Info", "The cubemap generator will render the scene skybox to a static cubemap that can be used with a simple cubemap skybox shader! The generated cubemap will be saved to the Assets folder.")]
  29. public bool messageSkybox = false;
  30. [StyledMessage("Info", "The cubemap generator will render the clouds to a cubemap. Change the child particles to generate new patterns. The generated cubemap will be saved to the Assets folder. Once generated, you will get a realtime preview in the scene.")]
  31. public bool messageClouds = false;
  32. [Space(10)]
  33. public CubemapType cubemapType = CubemapType.CloudsCubemap;
  34. public CubemapSizes cubemapSize = CubemapSizes._2048;
  35. [Space(10)]
  36. [StyledButton("Generate Cubemap")]
  37. public bool generateCubemap = false;
  38. [StyledSpace(10)]
  39. public bool styledSpace0;
  40. void Start()
  41. {
  42. generateCubemap = false;
  43. }
  44. void Update()
  45. {
  46. if (cubemapType == CubemapType.SkyboxCubemap)
  47. {
  48. messageSkybox = true;
  49. messageClouds = false;
  50. }
  51. else
  52. {
  53. messageSkybox = false;
  54. messageClouds = true;
  55. }
  56. if (generateCubemap)
  57. {
  58. var cam = GetComponent<Camera>();
  59. var path = "Assets/Generated Skybox Cubemap.png";
  60. if (cubemapType == CubemapType.CloudsCubemap)
  61. {
  62. path = "Assets/Generated Clouds Cubemap.png";
  63. }
  64. Cubemap cubemap = new Cubemap((int)cubemapSize, UnityEngine.Experimental.Rendering.GraphicsFormat.R32G32B32A32_SFloat, 0);
  65. cam.RenderToCubemap(cubemap);
  66. cubemap.Apply();
  67. Texture2D img = new Texture2D((int)cubemapSize * 6, (int)cubemapSize, TextureFormat.RGBAFloat, false);
  68. for (int i = 0; i < (int)cubemapSize; i++)
  69. {
  70. for (int j = 0; j < (int)cubemapSize; j++)
  71. {
  72. var pixelColor = cubemap.GetPixel(CubemapFace.PositiveX, i, (int)cubemapSize - j);
  73. var pixelValue = 1.0f;
  74. if (cubemapType == CubemapType.CloudsCubemap)
  75. pixelValue = pixelColor.r + pixelColor.g + pixelColor.b;
  76. img.SetPixel(i, j, new Color(pixelColor.r, pixelColor.g, pixelColor.b, pixelValue));
  77. }
  78. }
  79. for (int i = 0; i < (int)cubemapSize; i++)
  80. {
  81. for (int j = 0; j < (int)cubemapSize; j++)
  82. {
  83. var pixelColor = cubemap.GetPixel(CubemapFace.NegativeX, i, (int)cubemapSize - j);
  84. var pixelValue = 1.0f;
  85. if (cubemapType == CubemapType.CloudsCubemap)
  86. pixelValue = pixelColor.r + pixelColor.g + pixelColor.b;
  87. img.SetPixel(i + (int)cubemapSize, j, new Color(pixelColor.r, pixelColor.g, pixelColor.b, pixelValue));
  88. }
  89. }
  90. for (int i = 0; i < (int)cubemapSize; i++)
  91. {
  92. for (int j = 0; j < (int)cubemapSize; j++)
  93. {
  94. var pixelColor = cubemap.GetPixel(CubemapFace.PositiveY, i, (int)cubemapSize - j);
  95. var pixelValue = 1.0f;
  96. if (cubemapType == CubemapType.CloudsCubemap)
  97. pixelValue = pixelColor.r + pixelColor.g + pixelColor.b;
  98. img.SetPixel(i + (int)cubemapSize * 2, j, new Color(pixelColor.r, pixelColor.g, pixelColor.b, pixelValue));
  99. }
  100. }
  101. for (int i = 0; i < (int)cubemapSize; i++)
  102. {
  103. for (int j = 0; j < (int)cubemapSize; j++)
  104. {
  105. var pixelColor = cubemap.GetPixel(CubemapFace.NegativeY, i, (int)cubemapSize - j);
  106. var pixelValue = 1.0f;
  107. if (cubemapType == CubemapType.CloudsCubemap)
  108. pixelValue = pixelColor.r + pixelColor.g + pixelColor.b;
  109. img.SetPixel(i + (int)cubemapSize * 3, j, new Color(pixelColor.r, pixelColor.g, pixelColor.b, pixelValue));
  110. }
  111. }
  112. for (int i = 0; i < (int)cubemapSize; i++)
  113. {
  114. for (int j = 0; j < (int)cubemapSize; j++)
  115. {
  116. var pixelColor = cubemap.GetPixel(CubemapFace.PositiveZ, i, (int)cubemapSize - j);
  117. var pixelValue = 1.0f;
  118. if (cubemapType == CubemapType.CloudsCubemap)
  119. pixelValue = pixelColor.r + pixelColor.g + pixelColor.b;
  120. img.SetPixel(i + (int)cubemapSize * 4, j, new Color(pixelColor.r, pixelColor.g, pixelColor.b, pixelValue));
  121. }
  122. }
  123. for (int i = 0; i < (int)cubemapSize; i++)
  124. {
  125. for (int j = 0; j < (int)cubemapSize; j++)
  126. {
  127. var pixelColor = cubemap.GetPixel(CubemapFace.NegativeZ, i, (int)cubemapSize - j);
  128. var pixelValue = 1.0f;
  129. if (cubemapType == CubemapType.CloudsCubemap)
  130. pixelValue = pixelColor.r + pixelColor.g + pixelColor.b;
  131. img.SetPixel(i + (int)cubemapSize * 5, j, new Color(pixelColor.r, pixelColor.g, pixelColor.b, pixelValue));
  132. }
  133. }
  134. img.Apply();
  135. byte[] imgBytes = img.EncodeToPNG();
  136. File.WriteAllBytes(path, imgBytes);
  137. AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
  138. TextureImporter texImporter = AssetImporter.GetAtPath(path) as TextureImporter;
  139. texImporter.textureShape = TextureImporterShape.TextureCube;
  140. texImporter.textureCompression = TextureImporterCompression.CompressedHQ;
  141. texImporter.mipmapEnabled = false;
  142. texImporter.sRGBTexture = false;
  143. texImporter.SaveAndReimport();
  144. AssetDatabase.SaveAssets();
  145. AssetDatabase.Refresh();
  146. if (cubemapType == CubemapType.CloudsCubemap)
  147. {
  148. RenderSettings.skybox.SetTexture("_CloudsCubemap", AssetDatabase.LoadAssetAtPath<Cubemap>(path));
  149. }
  150. Debug.Log("[Polyverse Skies] The Generated Cubemap is saved to the Assets folder!");
  151. generateCubemap = false;
  152. }
  153. }
  154. }
  155. }
  156. #endif