From a728366035ca5890d5255293a5dab49ff8179c09 Mon Sep 17 00:00:00 2001 From: max Date: Wed, 6 Nov 2019 18:08:01 +0100 Subject: [PATCH] Export presets, wrap uv's, bugfixes and formatting. [MA_TextureAtlas] Updated export window to use scriptable asset export presets. The option to wrap uv's (default = true). Bigfixes and code formatting. --- .../Data/MA_TextureAtlasserProAtlas.cs | 2 +- .../MA_TextureAtlasserProExportSettings.cs | 63 ++++ .../Utils/MA_TextureAtlasserProUtils.cs | 305 +++++++++--------- ...MA_TextureAtlasserProCreateExportWindow.cs | 88 +++++ .../MA_TextureAtlasserProExportWindow.cs | 150 ++++----- .../ExportSettings/ExportSettings.txt | 1 + .../MA_DefaultExportSettings.asset | 25 ++ .../MA_DefaultSpriteExportSettings.asset | 25 ++ .../MA_TextureAtlasserProSettings.asset | 24 ++ .../MA_Utilities/MeshUtils/MA_MeshUtils.cs | 81 +++-- .../TextureUtils/MA_TextureUtils.cs | 2 +- README.md | 2 +- 12 files changed, 507 insertions(+), 261 deletions(-) create mode 100644 MA_ToolBox/MA_TextureAtlasserPro/Scripts/Data/MA_TextureAtlasserProExportSettings.cs create mode 100644 MA_ToolBox/MA_TextureAtlasserPro/Scripts/Editor/Windows/MA_TextureAtlasserProCreateExportWindow.cs create mode 100644 MA_ToolBox/MA_TextureAtlasserPro/Settings/ExportSettings/ExportSettings.txt create mode 100644 MA_ToolBox/MA_TextureAtlasserPro/Settings/ExportSettings/MA_DefaultExportSettings.asset create mode 100644 MA_ToolBox/MA_TextureAtlasserPro/Settings/ExportSettings/MA_DefaultSpriteExportSettings.asset create mode 100644 MA_ToolBox/MA_TextureAtlasserPro/Settings/MA_TextureAtlasserProSettings.asset diff --git a/MA_ToolBox/MA_TextureAtlasserPro/Scripts/Data/MA_TextureAtlasserProAtlas.cs b/MA_ToolBox/MA_TextureAtlasserPro/Scripts/Data/MA_TextureAtlasserProAtlas.cs index c9d9d1c..657b215 100644 --- a/MA_ToolBox/MA_TextureAtlasserPro/Scripts/Data/MA_TextureAtlasserProAtlas.cs +++ b/MA_ToolBox/MA_TextureAtlasserPro/Scripts/Data/MA_TextureAtlasserProAtlas.cs @@ -16,7 +16,7 @@ namespace MA_TextureAtlasserPro public MA_TextureAtlasserProQuad selectedTextureQuad; private Rect editorWorkRect; public bool showTextures = false; - + public MA_TextureAtlasserProExportSettings exportSettings; //Data public Vector2 textureAtlasSize; diff --git a/MA_ToolBox/MA_TextureAtlasserPro/Scripts/Data/MA_TextureAtlasserProExportSettings.cs b/MA_ToolBox/MA_TextureAtlasserPro/Scripts/Data/MA_TextureAtlasserProExportSettings.cs new file mode 100644 index 0000000..fabdd81 --- /dev/null +++ b/MA_ToolBox/MA_TextureAtlasserPro/Scripts/Data/MA_TextureAtlasserProExportSettings.cs @@ -0,0 +1,63 @@ +using UnityEngine; +using System.Collections; +using MA_Texture; + +namespace MA_TextureAtlasserPro +{ + [System.Serializable] + public class MA_TextureAtlasserProExportSettings : ScriptableObject + { + [HideInInspector] + public bool canModify = true; + + public ModelExportSettings modelExportSettings = new ModelExportSettings(); + public TextureExportSettings textureExportSettings = new TextureExportSettings(); + } + + [System.Serializable] + public class ModelExportSettings + { + [Header("Model settings:")] + public ModelFormat modelFormat = ModelFormat.Obj; + public bool replaceModel = false; + public bool uvFlipY = true; + public int uvChannel = 0; + public bool uvWrap = true; + } + + [System.Serializable] + public class TextureExportSettings + { + [Header("Texture settings:")] + public TextureFormat textureFormat = TextureFormat.Png; + public TextureType textureType = TextureType.Default; + public MA_TextureUtils.TextureScaleMode textureScaleMode = MA_TextureUtils.TextureScaleMode.Bilinear; + } + + public enum ExportPreset + { + Custom, + Default, + Sprites, + ReplaceObjMeshes + } + + public enum ModelFormat + { + None, + Obj + } + + public enum TextureFormat + { + None, + Png + } + + public enum TextureType + { + Default, + Sprite, + SpriteSliced + } +} \ No newline at end of file diff --git a/MA_ToolBox/MA_TextureAtlasserPro/Scripts/Editor/Utils/MA_TextureAtlasserProUtils.cs b/MA_ToolBox/MA_TextureAtlasserPro/Scripts/Editor/Utils/MA_TextureAtlasserProUtils.cs index e8ee9c3..d9b5b70 100644 --- a/MA_ToolBox/MA_TextureAtlasserPro/Scripts/Editor/Utils/MA_TextureAtlasserProUtils.cs +++ b/MA_ToolBox/MA_TextureAtlasserPro/Scripts/Editor/Utils/MA_TextureAtlasserProUtils.cs @@ -8,38 +8,13 @@ using MA_Texture; namespace MA_TextureAtlasserPro { - public enum ExportPreset - { - Custom, - Default, - Sprites, - ReplaceObjMeshes - } - public enum ModelFormat - { - None, - Obj, - ReplaceObj - } - - public enum TextureFormat - { - None, - Png - } - - public enum TextureType - { - Default, - Sprite, - SpriteSliced - } public static class MA_TextureAtlasserProUtils { public const string SETTINGSASSETPATH = "Assets/MA_ToolBox/MA_TextureAtlasserPro/Settings/"; - public const string SAVEASSETPATH = "Assets/MA_ToolBox/MA_TextureAtlasserPro/Atlasses/"; + public const string EXPORTSETTINGSASSETPATH = "Assets/MA_ToolBox/MA_TextureAtlasserPro/Settings/ExportSettings/"; + public const string SAVEASSETPATH = "Assets/MA_ToolBox/MA_TextureAtlasserPro/Atlasses/"; public const string LOADASSETPATH = "Assets/MA_ToolBox/MA_TextureAtlasserPro/Atlasses/"; public const string EXPORTASSETPATH = "Assets/MA_ToolBox/MA_TextureAtlasserPro/Exports/"; public const float VIEWOFFSET = 20; @@ -75,7 +50,39 @@ namespace MA_TextureAtlasserPro return _settings; } - public static MA_TextureAtlasserProAtlas CreateTextureAtlas(string name, Vector2 size) + public static MA_TextureAtlasserProExportSettings CreateExportSettings(string name, bool canModify = true) + { + MA_TextureAtlasserProExportSettings _settings = ScriptableObject.CreateInstance(); + _settings.canModify = canModify; + + if (_settings != null) + { + AssetDatabase.CreateAsset(_settings, EXPORTSETTINGSASSETPATH + name + ".asset"); + AssetDatabase.SaveAssets(); + AssetDatabase.Refresh(); + + return _settings; + } + else + { + return null; + } + } + + public static MA_TextureAtlasserProExportSettings LoadExportSettings() + { + string name = "MA_DefaultExportSettings"; + MA_TextureAtlasserProExportSettings _settings = AssetDatabase.LoadAssetAtPath(EXPORTSETTINGSASSETPATH + name + ".asset", typeof(MA_TextureAtlasserProExportSettings)) as MA_TextureAtlasserProExportSettings; + + if (_settings == null) + { + _settings = CreateExportSettings(name, false); + } + + return _settings; + } + + public static MA_TextureAtlasserProAtlas CreateTextureAtlas(string name, Vector2 size) { MA_TextureAtlasserProAtlas _atlas = ScriptableObject.CreateInstance(); @@ -132,8 +139,10 @@ namespace MA_TextureAtlasserPro { atlas.textureGroupRegistration = new List(); - MA_TextureGroupRegistration groupRegistration = new MA_TextureGroupRegistration(); - groupRegistration.name = DEFAULTTEXTUREGROUPNAME; + MA_TextureGroupRegistration groupRegistration = new MA_TextureGroupRegistration + { + name = DEFAULTTEXTUREGROUPNAME + }; atlas.textureGroupRegistration.Add(groupRegistration); } @@ -171,6 +180,11 @@ namespace MA_TextureAtlasserPro } } } + + if(atlas.exportSettings == null) + { + atlas.exportSettings = LoadExportSettings(); + } } public static MA_TextureAtlasserProQuad CreateTextureQuad(MA_TextureAtlasserProAtlas atlas, string name, Rect rect, bool focus = true) @@ -248,7 +262,11 @@ namespace MA_TextureAtlasserPro if (copyData) { - q.meshes = atlas.selectedTextureQuad.meshes; + q.meshes = new List(); + for (int i = 0; i < atlas.selectedTextureQuad.meshes.Count; i++) + { + q.meshes.Add(atlas.selectedTextureQuad.meshes[i]); + } for (int i = 0; i < atlas.selectedTextureQuad.textureGroups.Count; i++) { @@ -276,22 +294,28 @@ namespace MA_TextureAtlasserPro //Add texture groups foreach (MA_TextureGroupRegistration tgr in atlas.textureGroupRegistration) { - MA_TextureGroup textureGroup = new MA_TextureGroup(); - textureGroup.name = tgr.name; + MA_TextureGroup textureGroup = new MA_TextureGroup + { + name = tgr.name + }; quad.textureGroups.Add(textureGroup); } } public static void CreateTextureGroup(MA_TextureAtlasserProAtlas atlas, string name) { - MA_TextureGroupRegistration _textureGroupRegistration = new MA_TextureGroupRegistration(); - _textureGroupRegistration.name = name; + MA_TextureGroupRegistration _textureGroupRegistration = new MA_TextureGroupRegistration + { + name = name + }; atlas.textureGroupRegistration.Add(_textureGroupRegistration); foreach (MA_TextureAtlasserProQuad q in atlas.textureQuads) { - MA_TextureGroup _textureGroup = new MA_TextureGroup(); - _textureGroup.name = name; + MA_TextureGroup _textureGroup = new MA_TextureGroup + { + name = name + }; q.textureGroups.Add(_textureGroup); } } @@ -326,99 +350,95 @@ namespace MA_TextureAtlasserPro return (value == 1); } - #region Export - public static void ExportAtlasModels(MA_TextureAtlasserProAtlas atlas, ModelFormat modelFormat, string savePath = EXPORTASSETPATH) + #region Export + public static void ExportAtlasModels(MA_TextureAtlasserProAtlas atlas, ModelExportSettings modelExportSettings, string savePath = EXPORTASSETPATH) + { + switch(modelExportSettings.modelFormat) + { + case ModelFormat.None: + break; + case ModelFormat.Obj: + ExportAtlasObj(atlas, modelExportSettings, savePath); + break; + default: + break; + } + } + + private static void ExportAtlasObj(MA_TextureAtlasserProAtlas atlas, ModelExportSettings modelExportSettings, string savePath = EXPORTASSETPATH) { - switch (modelFormat) - { - case ModelFormat.None: - break; - case ModelFormat.Obj: - ExportAtlasObj(atlas, savePath); - break; - case ModelFormat.ReplaceObj: - ModifyAtlasObj(atlas); - break; - default: - break; - } + if (atlas == null || atlas.textureQuads == null) + return; + + if(modelExportSettings.replaceModel) + { + var quads = atlas.textureQuads; + + for (var index = 0; index < quads.Count; index++) + { + var quad = quads[index]; + if (quad.meshes == null) + continue; + + var meshes = quad.meshes; + for (var meshIndex = 0; meshIndex < quad.meshes.Count; meshIndex++) + { + if (meshes[meshIndex] == null) + continue; + + MA_MeshUtils.MA_UVReMap(meshes[meshIndex], atlas.textureAtlasSize, quad.guiRect, modelExportSettings.uvChannel, modelExportSettings.uvFlipY, modelExportSettings.uvWrap); + EditorUtility.SetDirty(meshes[meshIndex]); + } + } + + AssetDatabase.SaveAssets(); + } + else + { + foreach (MA_TextureAtlasserProQuad quad in atlas.textureQuads) + { + //Export Mesh + if (quad.meshes != null) + { + for (int m = 0; m < quad.meshes.Count; m++) + { + if (quad.meshes[m] != null) + { + //Create new mesh + Mesh newMesh = new Mesh(); + //Duplicate it from the current one + newMesh = MA_MeshUtils.MA_DuplicateMesh(quad.meshes[m]); + //Remap UV's + newMesh = MA_MeshUtils.MA_UVReMap(newMesh, atlas.textureAtlasSize, quad.guiRect, modelExportSettings.uvChannel, modelExportSettings.uvFlipY, modelExportSettings.uvWrap); + //Save it + string modelName = string.IsNullOrEmpty(quad.name) ? "" : quad.name + "-"; + modelName += quad.meshes[m].name; + int n = m + 1; + modelName += "_" + n.ToString("#000"); + + MA_MeshUtils.MeshToFile(newMesh, modelName, savePath); + } + } + } + } + } } - public static void ExportAtlasObj(MA_TextureAtlasserProAtlas atlas, string savePath = EXPORTASSETPATH) + public static void ExportAtlasTextures(MA_TextureAtlasserProAtlas atlas, TextureExportSettings textureExportSettings, string savePath = EXPORTASSETPATH) { - if (atlas == null || atlas.textureQuads == null) - return; - - foreach (MA_TextureAtlasserProQuad ta in atlas.textureQuads) - { - //Export Mesh - if(ta.meshes != null) - { - for (int m = 0; m < ta.meshes.Count; m++) - { - if(ta.meshes[m] != null) - { - //Create new mesh - Mesh newMesh = new Mesh(); - //Duplicate it from the current one - newMesh = MA_MeshUtils.MA_DuplicateMesh(ta.meshes[m]); - //Remap UV's - newMesh = MA_MeshUtils.MA_UVReMap(newMesh, atlas.textureAtlasSize, ta.guiRect); - //Save it - string modelName = string.IsNullOrEmpty(ta.name) ? "": ta.name + "-"; - modelName += ta.meshes[m].name; - int n = m + 1; - modelName += "_" + n.ToString("#000"); - - MA_MeshUtils.MeshToFile(newMesh, modelName, savePath); - } - } - } - } + switch (textureExportSettings.textureFormat) + { + case TextureFormat.None: + break; + case TextureFormat.Png: + ExportAtlasPNG(atlas, textureExportSettings, savePath); + break; + default: + break; + } } - public static void ModifyAtlasObj(MA_TextureAtlasserProAtlas atlas) - { - if (atlas == null || atlas.textureQuads == null) - return; - - var quads = atlas.textureQuads; - - for (var index = 0; index < quads.Count; index++) - { - var quad = quads[index]; - if (quad.meshes == null) - continue; - - var meshes = quad.meshes; - for (var meshIndex = 0; meshIndex < quad.meshes.Count; meshIndex++) - { - if (meshes[meshIndex] == null) - continue; - - MA_MeshUtils.MA_UVReMap(meshes[meshIndex], atlas.textureAtlasSize, quad.guiRect); - EditorUtility.SetDirty(meshes[meshIndex]); - } - } - - AssetDatabase.SaveAssets(); - } - - public static void ExportAtlasTextures(MA_TextureAtlasserProAtlas atlas, TextureFormat textureFormat, TextureType textureType, MA_TextureUtils.TextureScaleMode scaleMode, string savePath = EXPORTASSETPATH) - { - switch (textureFormat) - { - case TextureFormat.None: - break; - case TextureFormat.Png: - ExportAtlasPNG(atlas, textureType, scaleMode, savePath); - break; - default: - break; - } - } - - private static void ExportAtlasPNG(MA_TextureAtlasserProAtlas atlas, TextureType textureType, MA_TextureUtils.TextureScaleMode scaleMode, string savePath = EXPORTASSETPATH) + private static void ExportAtlasPNG(MA_TextureAtlasserProAtlas atlas, TextureExportSettings textureExportSettings, string savePath = EXPORTASSETPATH) { if (atlas == null || atlas.textureQuads == null || atlas.textureGroupRegistration == null) return; @@ -427,17 +447,19 @@ namespace MA_TextureAtlasserPro for (int i = 0; i < atlas.textureGroupRegistration.Count; i++) { //Create new Texture Atlas - Texture2D newTexture = new Texture2D((int)atlas.textureAtlasSize.x, (int)atlas.textureAtlasSize.y); - newTexture.name = atlas.name + "_" + atlas.textureGroupRegistration[i].name; + Texture2D newTexture = new Texture2D((int)atlas.textureAtlasSize.x, (int)atlas.textureAtlasSize.y) + { + name = atlas.name + "_" + atlas.textureGroupRegistration[i].name + }; foreach (MA_TextureAtlasserProQuad q in atlas.textureQuads) { if (q.textureGroups != null && q.textureGroups[i].texture != null) { //Create new texture part - Texture2D newTexturePart = (Texture2D)MA_Texture.MA_TextureUtils.ConvertToReadableTexture(q.textureGroups[i].texture); + Texture2D newTexturePart = (Texture2D)MA_TextureUtils.ConvertToReadableTexture(q.textureGroups[i].texture); //Scale it - newTexturePart = newTexturePart.MA_Scale2D((int)q.guiRect.width, (int)q.guiRect.height, scaleMode); + newTexturePart = newTexturePart.MA_Scale2D((int)q.guiRect.width, (int)q.guiRect.height, textureExportSettings.textureScaleMode); //Add it newTexture = newTexture.MA_Combine2D(newTexturePart, (int)q.guiRect.x, (int)q.guiRect.y); } @@ -451,25 +473,13 @@ namespace MA_TextureAtlasserPro textureImporter.SaveAndReimport(); } - switch (textureType) - { - case TextureType.Default: - break; - case TextureType.Sprite: - SetAtlasPNGSpriteSettings(atlas, textureType, savePath); - break; - case TextureType.SpriteSliced: - SetAtlasPNGSpriteSettings(atlas, textureType, savePath); - break; - default: - break; - } + SetAtlasPNGSpriteSettings(atlas, textureExportSettings, savePath); //Refresh AssetDatabase.Refresh(); } - private static void SetAtlasPNGSpriteSettings(MA_TextureAtlasserProAtlas atlas, TextureType textureType, string savePath = EXPORTASSETPATH) + private static void SetAtlasPNGSpriteSettings(MA_TextureAtlasserProAtlas atlas, TextureExportSettings textureExportSettings, string savePath = EXPORTASSETPATH) { //Foreach texture group for (int i = 0; i < atlas.textureGroupRegistration.Count; i++) @@ -481,7 +491,7 @@ namespace MA_TextureAtlasserPro textureImporter.alphaIsTransparency = true; //Slice sprites. - if (textureType == TextureType.SpriteSliced) + if (textureExportSettings.textureType == TextureType.SpriteSliced) { textureImporter.spriteImportMode = SpriteImportMode.None; //Reset it to update? textureImporter.spriteImportMode = SpriteImportMode.Multiple; @@ -492,10 +502,11 @@ namespace MA_TextureAtlasserPro if (q.textureGroups != null && q.textureGroups[i].texture != null) { //Create new SpriteMetaData. - SpriteMetaData smd = new SpriteMetaData(); - - smd.name = q.name; - smd.rect = new Rect(q.guiRect.x, atlas.textureAtlasSize.y - q.guiRect.y - q.guiRect.height, q.guiRect.width, q.guiRect.height); + SpriteMetaData smd = new SpriteMetaData + { + name = q.name, + rect = new Rect(q.guiRect.x, atlas.textureAtlasSize.y - q.guiRect.y - q.guiRect.height, q.guiRect.width, q.guiRect.height) + }; spriteMetaData.Add(smd); } diff --git a/MA_ToolBox/MA_TextureAtlasserPro/Scripts/Editor/Windows/MA_TextureAtlasserProCreateExportWindow.cs b/MA_ToolBox/MA_TextureAtlasserPro/Scripts/Editor/Windows/MA_TextureAtlasserProCreateExportWindow.cs new file mode 100644 index 0000000..684a1ce --- /dev/null +++ b/MA_ToolBox/MA_TextureAtlasserPro/Scripts/Editor/Windows/MA_TextureAtlasserProCreateExportWindow.cs @@ -0,0 +1,88 @@ +#if UNITY_EDITOR +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEditor; +using MA_Editor; +using MA_Texture; + +namespace MA_TextureAtlasserPro +{ + public class MA_TextureAtlasserProCreateExportWindow : EditorWindow + { + private const int windowHeight = 97; + private const int windowWidth = 320; + + //Editor + private static MA_TextureAtlasserProCreateExportWindow thisWindow; + public static MA_TextureAtlasserProWindow curWindow; + + //Data + string settingsName = "Settings name"; + bool nameError = true; + + [MenuItem("MA_ToolKit/MA_TextureAtlasserPro/New Export Settings")] + public static void Init() + { + InitWindow(null); + } + + public static void InitWindow(MA_TextureAtlasserProWindow currentEditorWindow) + { + curWindow = currentEditorWindow; + + GetCurrentWindow(); + + thisWindow.minSize = new Vector2(windowWidth, windowHeight); + thisWindow.maxSize = new Vector2(windowWidth, windowHeight); + thisWindow.titleContent.text = "MA_CreateExportSettings"; + + thisWindow.Show(); + } + + private static void GetCurrentWindow() + { + thisWindow = (MA_TextureAtlasserProCreateExportWindow)EditorWindow.GetWindow(); + } + + void OnGUI() + { + GUILayout.BeginArea(new Rect(MA_TextureAtlasserProUtils.VIEWOFFSET, MA_TextureAtlasserProUtils.VIEWOFFSET, position.width - (MA_TextureAtlasserProUtils.VIEWOFFSET * 2), position.height - (MA_TextureAtlasserProUtils.VIEWOFFSET * 2))); + GUILayout.BeginVertical(); + + //Input options + settingsName = EditorGUILayout.TextField("Settings name", settingsName, GUILayout.ExpandWidth(true)); + if (settingsName == "Settings name" || string.IsNullOrEmpty(settingsName)) + { + nameError = true; + GUI.backgroundColor = Color.red; + GUILayout.Box("Error: Enter a valid settings name!", EditorStyles.helpBox); + GUI.backgroundColor = Color.white; + } + else + { + nameError = false; + } + + //Create + if (!nameError) + { + if (GUILayout.Button("Create!", GUILayout.ExpandWidth(true), GUILayout.Height(37))) + { + MA_TextureAtlasserProExportSettings exportSettings = MA_TextureAtlasserProUtils.CreateExportSettings(settingsName, true); + + if (curWindow != null && curWindow.textureAtlas != null) + { + curWindow.textureAtlas.exportSettings = exportSettings; + } + + this.Close(); + } + } + + GUILayout.EndVertical(); + GUILayout.EndArea(); + } + } +} +#endif \ No newline at end of file diff --git a/MA_ToolBox/MA_TextureAtlasserPro/Scripts/Editor/Windows/MA_TextureAtlasserProExportWindow.cs b/MA_ToolBox/MA_TextureAtlasserPro/Scripts/Editor/Windows/MA_TextureAtlasserProExportWindow.cs index 4f1f765..5abd614 100644 --- a/MA_ToolBox/MA_TextureAtlasserPro/Scripts/Editor/Windows/MA_TextureAtlasserProExportWindow.cs +++ b/MA_ToolBox/MA_TextureAtlasserPro/Scripts/Editor/Windows/MA_TextureAtlasserProExportWindow.cs @@ -8,36 +8,28 @@ using MA_Texture; namespace MA_TextureAtlasserPro { - public class MA_TextureAtlasserProExportWindow : EditorWindow - { - private const int WindowHeight = 235; + public class MA_TextureAtlasserProExportWindow : EditorWindow + { + private const int windowHeight = 300; + private const int windowWidth = 320; - //Editor - private static MA_TextureAtlasserProExportWindow thisWindow; - public static MA_TextureAtlasserProWindow curWindow; + //Editor + private static MA_TextureAtlasserProExportWindow thisWindow; + public static MA_TextureAtlasserProWindow curWindow; - //Data - private static bool isLoaded = false; //Make sure we wait a frame at the start to setup and don't draw. + //Data + private static bool isLoaded = false; //Make sure we wait a frame at the start to setup and don't draw. - //Export settings. - private ExportPreset exportPreset = ExportPreset.Default; - private ModelFormat modelFormat = ModelFormat.Obj; - private TextureFormat textureFormat = TextureFormat.Png; - private TextureType textureType = TextureType.Default; - private MA_TextureUtils.TextureScaleMode textureScaleMode = MA_TextureUtils.TextureScaleMode.Bilinear; - - [MenuItem("MA_ToolKit/MA_TextureAtlasserPro/Export Atlas")] + [MenuItem("MA_ToolKit/MA_TextureAtlasserPro/Export Atlas")] private static void Init() { - GetCurrentWindow(); + GetCurrentWindow(); - thisWindow.minSize = new Vector2(420, WindowHeight); - thisWindow.maxSize = new Vector2(420, WindowHeight); + thisWindow.minSize = new Vector2(windowWidth, windowHeight); + thisWindow.titleContent.text = "MA_ExportTextureAtlas"; - thisWindow.titleContent.text = "MA_ExportTextureAtlas"; - - thisWindow.Show(); - } + thisWindow.Show(); + } public static void InitEditorWindow(MA_TextureAtlasserProWindow currentEditorWindow) { @@ -45,9 +37,7 @@ namespace MA_TextureAtlasserPro GetCurrentWindow(); - thisWindow.minSize = new Vector2(420, WindowHeight); - thisWindow.maxSize = new Vector2(420, WindowHeight); - + thisWindow.minSize = new Vector2(windowWidth, windowHeight); thisWindow.titleContent.text = "MA_ExportTextureAtlas"; thisWindow.Show(); @@ -106,45 +96,15 @@ namespace MA_TextureAtlasserPro { //Export GUILayout.BeginVertical(); + DrawExportMenu(); - DrawExportPresetMenu(); - DrawExportAdvancedOptions(); + curWindow.textureAtlas.exportSettings = (MA_TextureAtlasserProExportSettings)EditorGUILayout.ObjectField(curWindow.textureAtlas.exportSettings, typeof(MA_TextureAtlasserProExportSettings), false); - GUILayout.BeginHorizontal(EditorStyles.helpBox); + if(curWindow.textureAtlas.exportSettings != null) + { + DrawExportAdvancedOptions(); + } - switch (exportPreset) - { - case ExportPreset.Custom: - break; - case ExportPreset.Default: - modelFormat = ModelFormat.Obj; - textureFormat = TextureFormat.Png; - textureType = TextureType.Default; - textureScaleMode = MA_TextureUtils.TextureScaleMode.Bilinear; - break; - case ExportPreset.Sprites: - modelFormat = ModelFormat.None; - textureFormat = TextureFormat.Png; - textureType = TextureType.SpriteSliced; - textureScaleMode = MA_TextureUtils.TextureScaleMode.Bilinear; - break; - case ExportPreset.ReplaceObjMeshes: - modelFormat = ModelFormat.ReplaceObj; - textureFormat = TextureFormat.Png; - textureType = TextureType.Default; - textureScaleMode = MA_TextureUtils.TextureScaleMode.Bilinear; - break; - default: - break; - } - - if (GUILayout.Button("Export", GUILayout.ExpandWidth(true), GUILayout.Height(37))) - { - MA_TextureAtlasserProUtils.ExportAtlasModels(curWindow.textureAtlas, modelFormat); - MA_TextureAtlasserProUtils.ExportAtlasTextures(curWindow.textureAtlas, textureFormat, textureType, textureScaleMode); - } - - GUILayout.EndHorizontal(); GUILayout.EndVertical(); } else if(curWindow == null) @@ -172,39 +132,65 @@ namespace MA_TextureAtlasserPro isLoaded = true; } - private void DrawExportPresetMenu() + private void DrawExportMenu() { - GUILayout.BeginHorizontal(EditorStyles.helpBox); + GUILayout.BeginHorizontal(EditorStyles.helpBox, GUILayout.Height(44)); - exportPreset = (ExportPreset)EditorGUILayout.EnumPopup("ExportPreset:", exportPreset, GUILayout.ExpandWidth(true)); + if (GUILayout.Button(MA_TextureAtlasserProIcons.createAtlasIcon, GUILayout.ExpandWidth(false), GUILayout.ExpandHeight(true))) + { + MA_TextureAtlasserProCreateExportWindow.InitWindow(curWindow); + } - GUILayout.EndHorizontal(); + bool wasEnabled = GUI.enabled; + + if (curWindow.textureAtlas.exportSettings != null) + { + GUI.enabled = true; + } + else + { + GUI.enabled = false; + } + + if (GUILayout.Button("Export", GUILayout.ExpandWidth(true), GUILayout.Height(37))) + { + MA_TextureAtlasserProUtils.ExportAtlasModels(curWindow.textureAtlas, curWindow.textureAtlas.exportSettings.modelExportSettings); + MA_TextureAtlasserProUtils.ExportAtlasTextures(curWindow.textureAtlas, curWindow.textureAtlas.exportSettings.textureExportSettings); + } + + GUI.enabled = wasEnabled; + + GUILayout.EndHorizontal(); } private void DrawExportAdvancedOptions() { bool wasEnabled = GUI.enabled; - if(exportPreset == ExportPreset.Custom) - { - GUI.enabled = true; - } - else - { - GUI.enabled = false; - } + if (curWindow.textureAtlas.exportSettings.canModify) + { + GUI.enabled = true; + } + else + { + GUI.enabled = false; + } - EditorGUILayout.BeginVertical(EditorStyles.helpBox); + EditorGUILayout.BeginVertical(EditorStyles.helpBox); - GUILayout.Label("Models:", EditorStyles.miniBoldLabel); - modelFormat = (ModelFormat)EditorGUILayout.EnumPopup("ModelFormat:", modelFormat); + GUILayout.Label("Models:", EditorStyles.miniBoldLabel); + curWindow.textureAtlas.exportSettings.modelExportSettings.modelFormat = (ModelFormat)EditorGUILayout.EnumPopup("ModelFormat:", curWindow.textureAtlas.exportSettings.modelExportSettings.modelFormat); + curWindow.textureAtlas.exportSettings.modelExportSettings.replaceModel = EditorGUILayout.Toggle("ReplaceModels:", curWindow.textureAtlas.exportSettings.modelExportSettings.replaceModel); + curWindow.textureAtlas.exportSettings.modelExportSettings.uvFlipY = EditorGUILayout.Toggle("UV FlipY:", curWindow.textureAtlas.exportSettings.modelExportSettings.uvFlipY); + curWindow.textureAtlas.exportSettings.modelExportSettings.uvChannel = EditorGUILayout.IntField("UV Channel:", curWindow.textureAtlas.exportSettings.modelExportSettings.uvChannel); + curWindow.textureAtlas.exportSettings.modelExportSettings.uvWrap = EditorGUILayout.Toggle("UV Wrap:", curWindow.textureAtlas.exportSettings.modelExportSettings.uvWrap); - GUILayout.Label("Textures:", EditorStyles.miniBoldLabel); - textureFormat = (TextureFormat)EditorGUILayout.EnumPopup("TextureFormat:", textureFormat); - textureType = (TextureType)EditorGUILayout.EnumPopup("TextureType:", textureType); - textureScaleMode = (MA_TextureUtils.TextureScaleMode)EditorGUILayout.EnumPopup("TextureScaleMode:", textureScaleMode); + GUILayout.Label("Textures:", EditorStyles.miniBoldLabel); + curWindow.textureAtlas.exportSettings.textureExportSettings.textureFormat = (TextureFormat)EditorGUILayout.EnumPopup("TextureFormat:", curWindow.textureAtlas.exportSettings.textureExportSettings.textureFormat); + curWindow.textureAtlas.exportSettings.textureExportSettings.textureType = (TextureType)EditorGUILayout.EnumPopup("TextureType:", curWindow.textureAtlas.exportSettings.textureExportSettings.textureType); + curWindow.textureAtlas.exportSettings.textureExportSettings.textureScaleMode = (MA_TextureUtils.TextureScaleMode)EditorGUILayout.EnumPopup("TextureScaleMode:", curWindow.textureAtlas.exportSettings.textureExportSettings.textureScaleMode); - EditorGUILayout.EndVertical(); + EditorGUILayout.EndVertical(); GUI.enabled = wasEnabled; } diff --git a/MA_ToolBox/MA_TextureAtlasserPro/Settings/ExportSettings/ExportSettings.txt b/MA_ToolBox/MA_TextureAtlasserPro/Settings/ExportSettings/ExportSettings.txt new file mode 100644 index 0000000..5506929 --- /dev/null +++ b/MA_ToolBox/MA_TextureAtlasserPro/Settings/ExportSettings/ExportSettings.txt @@ -0,0 +1 @@ +Export settigns are supposed to be here. \ No newline at end of file diff --git a/MA_ToolBox/MA_TextureAtlasserPro/Settings/ExportSettings/MA_DefaultExportSettings.asset b/MA_ToolBox/MA_TextureAtlasserPro/Settings/ExportSettings/MA_DefaultExportSettings.asset new file mode 100644 index 0000000..82aa982 --- /dev/null +++ b/MA_ToolBox/MA_TextureAtlasserPro/Settings/ExportSettings/MA_DefaultExportSettings.asset @@ -0,0 +1,25 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &11400000 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 81466035f9fafc64db33b9e6114d774b, type: 3} + m_Name: MA_DefaultExportSettings + m_EditorClassIdentifier: + canModify: 0 + modelExportSettings: + modelFormat: 1 + replaceModel: 0 + uvFlipY: 1 + uvChannel: 0 + uvWrap: 1 + textureExportSettings: + textureFormat: 1 + textureType: 0 + textureScaleMode: 0 diff --git a/MA_ToolBox/MA_TextureAtlasserPro/Settings/ExportSettings/MA_DefaultSpriteExportSettings.asset b/MA_ToolBox/MA_TextureAtlasserPro/Settings/ExportSettings/MA_DefaultSpriteExportSettings.asset new file mode 100644 index 0000000..da71557 --- /dev/null +++ b/MA_ToolBox/MA_TextureAtlasserPro/Settings/ExportSettings/MA_DefaultSpriteExportSettings.asset @@ -0,0 +1,25 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &11400000 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 81466035f9fafc64db33b9e6114d774b, type: 3} + m_Name: MA_DefaultSpriteExportSettings + m_EditorClassIdentifier: + canModify: 0 + modelExportSettings: + modelFormat: 1 + replaceModel: 0 + uvFlipY: 1 + uvChannel: 0 + uvWrap: 1 + textureExportSettings: + textureFormat: 1 + textureType: 0 + textureScaleMode: 0 diff --git a/MA_ToolBox/MA_TextureAtlasserPro/Settings/MA_TextureAtlasserProSettings.asset b/MA_ToolBox/MA_TextureAtlasserPro/Settings/MA_TextureAtlasserProSettings.asset new file mode 100644 index 0000000..2945323 --- /dev/null +++ b/MA_ToolBox/MA_TextureAtlasserPro/Settings/MA_TextureAtlasserProSettings.asset @@ -0,0 +1,24 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &11400000 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 73f5e66553c13034e9b894ef2cc31b66, type: 3} + m_Name: MA_TextureAtlasserProSettings + m_EditorClassIdentifier: + useHotkeys: 1 + modifierKey: 1 + addQuadHotKey: 113 + removeQuadHotKey: 114 + duplicateHotKey: 100 + zoomInHotKey: 61 + zoomOutHotKey: 45 + copySelectedQuadData: 1 + duplicatedQuadNamePrefix: 'new ' + autoFocus: 1 diff --git a/MA_ToolBox/MA_Utilities/MeshUtils/MA_MeshUtils.cs b/MA_ToolBox/MA_Utilities/MeshUtils/MA_MeshUtils.cs index 58fb8db..5ece2cd 100644 --- a/MA_ToolBox/MA_Utilities/MeshUtils/MA_MeshUtils.cs +++ b/MA_ToolBox/MA_Utilities/MeshUtils/MA_MeshUtils.cs @@ -39,17 +39,19 @@ namespace MA_Mesh public static Mesh MA_DuplicateMesh(Mesh mesh) { - Mesh newMesh = new Mesh(); - newMesh.name = mesh.name; + Mesh newMesh = new Mesh + { + name = mesh.name, + bounds = mesh.bounds, + colors = mesh.colors, + subMeshCount = mesh.subMeshCount + }; + newMesh.SetVertices(new List(mesh.vertices)); - newMesh.bounds = mesh.bounds; - newMesh.colors = mesh.colors.ToArray(); - newMesh.subMeshCount = mesh.subMeshCount; for (int i = 0; i < mesh.subMeshCount; i++) { newMesh.SetTriangles(mesh.GetTriangles(i), i); } - newMesh.subMeshCount = mesh.subMeshCount; newMesh.SetNormals(new List(mesh.normals)); newMesh.SetUVs(0, new List(mesh.uv)); newMesh.SetTangents(new List(mesh.tangents)); @@ -86,27 +88,41 @@ namespace MA_Mesh return mesh; } - public static Mesh MA_UVReMap(this Mesh mesh, Vector2 atlasSize, Rect textureRect, int uvChannel = 0, bool flipY = true) - { - List uvs = new List(); - + public static Mesh MA_UVReMap(this Mesh mesh, Vector2 atlasSize, Rect textureRect, int uvChannel = 0, bool flipY = true, bool wrap = true) + { //Get UV's + List uvs = new List(); mesh.GetUVs(uvChannel, uvs); + //Min and max bounds in 0-1 space. + float xMin, xMax, yMin, yMax; + xMin = (1f / atlasSize.x * textureRect.width); + xMax = (1f / atlasSize.x * textureRect.x); + yMin = (1f / atlasSize.y * textureRect.height); + + //Flip uv's if needed. + if (flipY) + { + yMax = (1f / atlasSize.y * (atlasSize.y - textureRect.height - textureRect.y)); + } + else + { + yMax = (1f / atlasSize.y * textureRect.y); + } + for (int i = 0; i < uvs.Count; i++) { - if(flipY) + float newX = uvs[i].x * xMin + xMax; + float newY = uvs[i].y * yMin + yMax; + + //Wrap the verts outside of the uv space around back into the uv space. + if (wrap) { - uvs[i] = new Vector2((uvs[i].x / atlasSize.x * textureRect.width) + (1 / atlasSize.x * textureRect.x), - (uvs[i].y / atlasSize.y * textureRect.height) + (1 / atlasSize.y * (atlasSize.y - textureRect.height - textureRect.y))); - } - else - { - //Debug.Log("01" + uvs[i].x); - uvs[i] = new Vector2((uvs[i].x / atlasSize.x * textureRect.width) + (1 / atlasSize.x * textureRect.x), - (uvs[i].y / atlasSize.y * textureRect.height) + (1 / atlasSize.y * textureRect.y)); - //Debug.Log("02" + uvs[i].x); + newX = Wrap(newX, xMax, xMin + xMax); + newY = Wrap(newY, yMax, yMin + yMax); } + + uvs[i] = new Vector2(newX, newY); } mesh.SetUVs(uvChannel, uvs); @@ -114,6 +130,14 @@ namespace MA_Mesh return mesh; } + public static float Wrap(float val, float min, float max) + { + val -= (float)Math.Round((val - min) / (max - min)) * (max - min); + if (val < min) + val = val + max - min; + return val; + } + //Start http://wiki.unity3d.com/index.php?title=ObjExporter public static string MeshToString(Mesh mesh) { @@ -121,7 +145,6 @@ namespace MA_Mesh int normalOffset = 0; int uvOffset = 0; - Material material = new Material(Shader.Find("Standard")); StringBuilder sb = new StringBuilder(); @@ -169,9 +192,9 @@ namespace MA_Mesh } } - vertexOffset += mesh.vertices.Length; - normalOffset += mesh.normals.Length; - uvOffset += mesh.uv.Length; + //vertexOffset += mesh.vertices.Length; + //normalOffset += mesh.normals.Length; + //uvOffset += mesh.uv.Length; return sb.ToString(); } @@ -186,10 +209,10 @@ namespace MA_Mesh //End } - struct ObjMaterial - { - public string name; - public string textureName; - } + //struct ObjMaterial + //{ + // public string name; + // public string textureName; + //} } #endif \ No newline at end of file diff --git a/MA_ToolBox/MA_Utilities/TextureUtils/MA_TextureUtils.cs b/MA_ToolBox/MA_Utilities/TextureUtils/MA_TextureUtils.cs index 729277c..a66b370 100644 --- a/MA_ToolBox/MA_Utilities/TextureUtils/MA_TextureUtils.cs +++ b/MA_ToolBox/MA_Utilities/TextureUtils/MA_TextureUtils.cs @@ -218,7 +218,7 @@ namespace MA_Texture public static Texture MA_Combine(this Texture texture, Texture combineTexture, int offsetX, int offsetY) { Texture2D texture2D = (Texture2D)MA_TextureUtils.ConvertToReadableTexture(texture); - Texture2D combineTexture2D = (Texture2D)MA_TextureUtils.ConvertToReadableTexture(texture); + Texture2D combineTexture2D = (Texture2D)MA_TextureUtils.ConvertToReadableTexture(combineTexture); texture = texture2D.MA_Combine2D(combineTexture2D, offsetX, offsetY); diff --git a/README.md b/README.md index 29cb7f5..bc714d4 100644 --- a/README.md +++ b/README.md @@ -18,4 +18,4 @@ Download the UnityPackage here: https://github.com/maxartz15/MA_TextureAtlasser/ [![Github All Releases](https://img.shields.io/github/downloads/maxartz15/MA_TextureAtlasser/total.svg)]() -For more information: https://maxartz15.com/ma_textureatlas/ \ No newline at end of file +For more information: https://maxartz15.com/ma-textureatlasser/ \ No newline at end of file