123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using UnityEditor;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- namespace Unity3D2Babylon
- {
- public class ExporterSpliter : EditorWindow
- {
- Cubemap splitCube;
- [MenuItem("BabylonJS/Cubemap Splitter", false, 101)]
- public static void InitSpliter()
- {
- ExporterSpliter splitter = ScriptableObject.CreateInstance<ExporterSpliter>();
- splitter.OnInitialize();
- splitter.ShowUtility();
- }
- public void OnInitialize()
- {
- maxSize = new Vector2(512, 155);
- minSize = this.maxSize;
- }
- void OnEnable()
- {
- titleContent = new GUIContent("Cubemap Splitter");
- }
- public void OnGUI()
- {
- GUILayout.Label("Choose the Cubemap you want to split into 6 images", EditorStyles.boldLabel);
- EditorGUILayout.Space();
- splitCube = EditorGUILayout.ObjectField("Cubemap:", splitCube, typeof(Cubemap), false) as Cubemap;
- EditorGUILayout.Space();
- EditorGUILayout.Space();
- if (GUILayout.Button("Split Cubemap"))
- {
- if (splitCube)
- {
- Split();
- }
- if (!splitCube)
- {
- ExporterWindow.ShowMessage("You must select a cubemap");
- }
- }
- }
- public void Split()
- {
- ExporterWindow.ReportProgress(1, "Splitting cubemap textures... This may take a while.");
- var filePath = AssetDatabase.GetAssetPath(splitCube);
- int splitSize = splitCube.width;
- Color[] CubeMapColors;
- var faceTexturePath = Path.Combine(Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath));
- bool png = (ExporterWindow.exportationOptions.DefaultImageFormat == (int)BabylonImageFormat.PNG);
- string fext = (png == true) ? ".png" : ".jpg";
- string filename = "";
- string preview = "";
- var importTool = new BabylonTextureImporter(filePath);
- bool isReadable = importTool.IsReadable();
- if (!isReadable)
- {
- importTool.SetReadable();
- }
- try
- {
- filename = (faceTexturePath + "_py" + fext);
- preview = Path.GetFileName(filename);
- ExporterWindow.ReportProgress(1, "Splitting texture face " + preview + " ... This may take a while.");
- Texture2D tex = new Texture2D(splitSize, splitSize, TextureFormat.RGB24, false);
- CubeMapColors = splitCube.GetPixels(CubemapFace.PositiveY);
- tex.SetPixels(CubeMapColors, 0);
- tex.Apply();
- WriteTextureFile(filename, tex, png);
- filename = (faceTexturePath + "_ny" + fext);
- preview = Path.GetFileName(filename);
- ExporterWindow.ReportProgress(1, "Splitting texture face " + preview + " ... This may take a while.");
- CubeMapColors = splitCube.GetPixels(CubemapFace.NegativeY);
- tex.SetPixels(CubeMapColors, 0);
- tex.Apply();
- WriteTextureFile(filename, tex, png);
- filename = (faceTexturePath + "_px" + fext);
- preview = Path.GetFileName(filename);
- ExporterWindow.ReportProgress(1, "Splitting texture face " + preview + " ... This may take a while.");
- CubeMapColors = splitCube.GetPixels(CubemapFace.PositiveX);
- tex.SetPixels(CubeMapColors, 0);
- tex.Apply();
- WriteTextureFile(filename, tex, png);
- filename = (faceTexturePath + "_nx" + fext);
- preview = Path.GetFileName(filename);
- ExporterWindow.ReportProgress(1, "Splitting texture face " + preview + " ... This may take a while.");
- CubeMapColors = splitCube.GetPixels(CubemapFace.NegativeX);
- tex.SetPixels(CubeMapColors, 0);
- tex.Apply();
- WriteTextureFile(filename, tex, png);
- filename = (faceTexturePath + "_pz" + fext);
- preview = Path.GetFileName(filename);
- ExporterWindow.ReportProgress(1, "Splitting texture face " + preview + " ... This may take a while.");
- CubeMapColors = splitCube.GetPixels(CubemapFace.PositiveZ);
- tex.SetPixels(CubeMapColors, 0);
- tex.Apply();
- WriteTextureFile(filename, tex, png);
- filename = (faceTexturePath + "_nz" + fext);
- preview = Path.GetFileName(filename);
- ExporterWindow.ReportProgress(1, "Splitting texture face " + preview + " ... This may take a while.");
- CubeMapColors = splitCube.GetPixels(CubemapFace.NegativeZ);
- tex.SetPixels(CubeMapColors, 0);
- tex.Apply();
- WriteTextureFile(filename, tex, png);
- }
- catch (Exception ex)
- {
- UnityEngine.Debug.LogException(ex);
- }
- finally
- {
- if (!isReadable)
- {
- ExporterWindow.ReportProgress(1, "Finalizing assets database...");
- importTool.ForceUpdate();
- }
- }
- ExporterWindow.ReportProgress(1, "Refresing assets database...");
- AssetDatabase.Refresh();
- ExporterWindow.ReportProgress(1, "Cubemap split complete.");
- EditorUtility.ClearProgressBar();
- this.Close();
- }
- private void WriteTextureFile(string filename, Texture2D texture, bool png)
- {
- if (png)
- {
- File.WriteAllBytes(filename, texture.EncodeToPNG());
- }
- else
- {
- File.WriteAllBytes(filename, texture.EncodeToJPG(ExporterWindow.exportationOptions.DefaultQualityLevel));
- }
- }
- public void OnInspectorUpdate()
- {
- this.Repaint();
- }
- }
- }
|