ExporterSplitter.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using UnityEditor;
  6. using UnityEngine;
  7. using UnityEngine.SceneManagement;
  8. namespace Unity3D2Babylon
  9. {
  10. public class ExporterSpliter : EditorWindow
  11. {
  12. Cubemap splitCube;
  13. [MenuItem("BabylonJS/Cubemap Splitter", false, 101)]
  14. public static void InitSpliter()
  15. {
  16. ExporterSpliter splitter = ScriptableObject.CreateInstance<ExporterSpliter>();
  17. splitter.OnInitialize();
  18. splitter.ShowUtility();
  19. }
  20. public void OnInitialize()
  21. {
  22. maxSize = new Vector2(512, 155);
  23. minSize = this.maxSize;
  24. }
  25. void OnEnable()
  26. {
  27. titleContent = new GUIContent("Cubemap Splitter");
  28. }
  29. public void OnGUI()
  30. {
  31. GUILayout.Label("Choose the Cubemap you want to split into 6 images", EditorStyles.boldLabel);
  32. EditorGUILayout.Space();
  33. splitCube = EditorGUILayout.ObjectField("Cubemap:", splitCube, typeof(Cubemap), false) as Cubemap;
  34. EditorGUILayout.Space();
  35. EditorGUILayout.Space();
  36. if (GUILayout.Button("Split Cubemap"))
  37. {
  38. if (splitCube)
  39. {
  40. Split();
  41. }
  42. if (!splitCube)
  43. {
  44. ExporterWindow.ShowMessage("You must select a cubemap");
  45. }
  46. }
  47. }
  48. public void Split()
  49. {
  50. ExporterWindow.ReportProgress(1, "Splitting cubemap textures... This may take a while.");
  51. var filePath = AssetDatabase.GetAssetPath(splitCube);
  52. int splitSize = splitCube.width;
  53. Color[] CubeMapColors;
  54. var faceTexturePath = Path.Combine(Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath));
  55. bool png = (ExporterWindow.exportationOptions.DefaultImageFormat == (int)BabylonImageFormat.PNG);
  56. string fext = (png == true) ? ".png" : ".jpg";
  57. string filename = "";
  58. string preview = "";
  59. var importTool = new BabylonTextureImporter(filePath);
  60. bool isReadable = importTool.IsReadable();
  61. if (!isReadable)
  62. {
  63. importTool.SetReadable();
  64. }
  65. try
  66. {
  67. filename = (faceTexturePath + "_py" + fext);
  68. preview = Path.GetFileName(filename);
  69. ExporterWindow.ReportProgress(1, "Splitting texture face " + preview + " ... This may take a while.");
  70. Texture2D tex = new Texture2D(splitSize, splitSize, TextureFormat.RGB24, false);
  71. CubeMapColors = splitCube.GetPixels(CubemapFace.PositiveY);
  72. tex.SetPixels(CubeMapColors, 0);
  73. tex.Apply();
  74. WriteTextureFile(filename, tex, png);
  75. filename = (faceTexturePath + "_ny" + fext);
  76. preview = Path.GetFileName(filename);
  77. ExporterWindow.ReportProgress(1, "Splitting texture face " + preview + " ... This may take a while.");
  78. CubeMapColors = splitCube.GetPixels(CubemapFace.NegativeY);
  79. tex.SetPixels(CubeMapColors, 0);
  80. tex.Apply();
  81. WriteTextureFile(filename, tex, png);
  82. filename = (faceTexturePath + "_px" + fext);
  83. preview = Path.GetFileName(filename);
  84. ExporterWindow.ReportProgress(1, "Splitting texture face " + preview + " ... This may take a while.");
  85. CubeMapColors = splitCube.GetPixels(CubemapFace.PositiveX);
  86. tex.SetPixels(CubeMapColors, 0);
  87. tex.Apply();
  88. WriteTextureFile(filename, tex, png);
  89. filename = (faceTexturePath + "_nx" + fext);
  90. preview = Path.GetFileName(filename);
  91. ExporterWindow.ReportProgress(1, "Splitting texture face " + preview + " ... This may take a while.");
  92. CubeMapColors = splitCube.GetPixels(CubemapFace.NegativeX);
  93. tex.SetPixels(CubeMapColors, 0);
  94. tex.Apply();
  95. WriteTextureFile(filename, tex, png);
  96. filename = (faceTexturePath + "_pz" + fext);
  97. preview = Path.GetFileName(filename);
  98. ExporterWindow.ReportProgress(1, "Splitting texture face " + preview + " ... This may take a while.");
  99. CubeMapColors = splitCube.GetPixels(CubemapFace.PositiveZ);
  100. tex.SetPixels(CubeMapColors, 0);
  101. tex.Apply();
  102. WriteTextureFile(filename, tex, png);
  103. filename = (faceTexturePath + "_nz" + fext);
  104. preview = Path.GetFileName(filename);
  105. ExporterWindow.ReportProgress(1, "Splitting texture face " + preview + " ... This may take a while.");
  106. CubeMapColors = splitCube.GetPixels(CubemapFace.NegativeZ);
  107. tex.SetPixels(CubeMapColors, 0);
  108. tex.Apply();
  109. WriteTextureFile(filename, tex, png);
  110. }
  111. catch (Exception ex)
  112. {
  113. UnityEngine.Debug.LogException(ex);
  114. }
  115. finally
  116. {
  117. if (!isReadable)
  118. {
  119. ExporterWindow.ReportProgress(1, "Finalizing assets database...");
  120. importTool.ForceUpdate();
  121. }
  122. }
  123. ExporterWindow.ReportProgress(1, "Refresing assets database...");
  124. AssetDatabase.Refresh();
  125. ExporterWindow.ReportProgress(1, "Cubemap split complete.");
  126. EditorUtility.ClearProgressBar();
  127. this.Close();
  128. }
  129. private void WriteTextureFile(string filename, Texture2D texture, bool png)
  130. {
  131. if (png)
  132. {
  133. File.WriteAllBytes(filename, texture.EncodeToPNG());
  134. }
  135. else
  136. {
  137. File.WriteAllBytes(filename, texture.EncodeToJPG(ExporterWindow.exportationOptions.DefaultQualityLevel));
  138. }
  139. }
  140. public void OnInspectorUpdate()
  141. {
  142. this.Repaint();
  143. }
  144. }
  145. }