-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTexturePackerBatch.cs
More file actions
281 lines (243 loc) · 11.6 KB
/
TexturePackerBatch.cs
File metadata and controls
281 lines (243 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
using System;
using System.IO;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
using PBRTexturesData = TexturePackerUtilities.PBRTexturesData;
using ExportSettings = TexturePackerUtilities.ExportSettings;
using SurfaceData = TexturePackerUtilities.SurfaceData;
public class TexturePackerBatch : EditorWindow
{
public enum TextureType
{
Albedo,
Normal,
Metallic,
Smoothness,
Roughness,
AmbientOcclusion,
}
public ExportSettings exportSettings = new ExportSettings();
public string texturesFolder = "Assets/Textures";
public string suffixAlbedo = "_albedo";
public string suffixNormal = "_normal";
public string suffixMetallic = "_metallic";
public string suffixSmoothness = "_smoothness";
public string suffixRoughness = "_roughness";
public string suffixAO = "_ao";
private string[] _suffixes => new string[] { suffixAlbedo, suffixNormal, suffixMetallic, suffixSmoothness, suffixRoughness, suffixAO };
private Dictionary<TextureType, string> _suffixesDict => new Dictionary<TextureType, string>()
{
{ TextureType.Albedo, suffixAlbedo },
{ TextureType.Normal, suffixNormal },
{ TextureType.Metallic, suffixMetallic },
{ TextureType.Smoothness, suffixSmoothness },
{ TextureType.Roughness, suffixRoughness },
{ TextureType.AmbientOcclusion, suffixAO },
};
public float metallicDefault = 0f;
public float smoothnessDefault = 0.5f;
public SurfaceData redChannel = SurfaceData.Metallic;
public SurfaceData greenChannel = SurfaceData.Smoothness;
public SurfaceData blueChannel = SurfaceData.AmbientOcclusion;
private static EditorWindow _editorWindow;
private Vector2 _scrollPos;
#if UNITY_EDITOR
[MenuItem("Tools/Texture Packer/Texture Packer (Batch)")]
public static void ShowWindow()
{
_editorWindow = GetWindow(typeof(TexturePackerBatch), false);
}
[MenuItem("Assets/Texture Packer/Texture Packer (Batch)")]
public static void ShowWindowRightClick()
{
_editorWindow = GetWindow(typeof(TexturePackerBatch), false);
}
private void OnInspectorUpdate()
{
if (!_editorWindow)
_editorWindow = GetWindow(typeof(TexturePackerBatch), false);
}
private void OnGUI()
{
if (_editorWindow)
{
GUILayout.BeginArea(new Rect(0, 0, _editorWindow.position.size.x, _editorWindow.position.size.y));
GUILayout.BeginVertical();
_scrollPos = GUILayout.BeginScrollView(_scrollPos, false, true, GUILayout.ExpandHeight(true));
}
GUIStyle BigBold = new GUIStyle();
BigBold.fontSize = 16;
BigBold.fontStyle = FontStyle.Bold;
BigBold.wordWrap = true;
BigBold.alignment = TextAnchor.MiddleCenter;
GUIStyle Wrap = new GUIStyle();
Wrap.wordWrap = true;
Wrap.alignment = TextAnchor.MiddleCenter;
GUIStyle warn = new GUIStyle();
warn.richText = true;
warn.wordWrap = true;
warn.fontStyle = FontStyle.Bold;
warn.alignment = TextAnchor.MiddleCenter;
warn.normal.textColor = new Color(0.7f, 0, 0);
GUIStyle preview = new GUIStyle();
preview.alignment = TextAnchor.UpperCenter;
// Batch data
GUILayout.BeginVertical(EditorStyles.helpBox);
GUILayout.Space(10f);
texturesFolder = EditorGUILayout.TextField("Textures Folder", texturesFolder);
GUILayout.Space(5f);
suffixAlbedo = EditorGUILayout.TextField("Albedo Suffix", suffixAlbedo);
suffixNormal = EditorGUILayout.TextField("Normal Suffix", suffixNormal);
suffixMetallic = EditorGUILayout.TextField("Metallic Suffix", suffixMetallic);
suffixSmoothness = EditorGUILayout.TextField("Smoothness Suffix", suffixSmoothness);
suffixRoughness = EditorGUILayout.TextField("Roughness Suffix", suffixRoughness);
suffixAO = EditorGUILayout.TextField("AmbientOcclusion Suffix", suffixAO);
metallicDefault = EditorGUILayout.Slider("Metallic Default", metallicDefault, 0f, 1f);
smoothnessDefault = EditorGUILayout.Slider("Smoothness Default", smoothnessDefault, 0f, 1f);
GUILayout.Space(10f);
GUILayout.EndVertical();
// Channel Selection
GUILayout.BeginVertical(EditorStyles.helpBox);
GUILayout.Space(10f);
exportSettings.folderName = EditorGUILayout.TextField("Folder Name", exportSettings.folderName);
GUILayout.Space(10f);
exportSettings.generateMaterial = EditorGUILayout.Toggle("Generate Material", exportSettings.generateMaterial);
if (exportSettings.generateMaterial)
{
exportSettings.shader = (Shader)EditorGUILayout.ObjectField("Shader", exportSettings.shader, typeof(Shader), false);
exportSettings.material = (Material)EditorGUILayout.ObjectField("Material", exportSettings.material, typeof(Material), false);
exportSettings.propNameAlbedo = EditorGUILayout.TextField("Albedo property name: ", exportSettings.propNameAlbedo);
exportSettings.propNameNormal = EditorGUILayout.TextField("Normal property name: ", exportSettings.propNameNormal);
exportSettings.propNameMask = EditorGUILayout.TextField("Mask property name: ", exportSettings.propNameMask);
exportSettings.metallicIfMask = EditorGUILayout.Slider("Metallic value if there's mask", exportSettings.metallicIfMask, 0f, 1f);
exportSettings.smoothnessIfMask = EditorGUILayout.Slider("Smoothness value if there's mask", exportSettings.smoothnessIfMask, 0f, 1f);
exportSettings.propNameMetallic = EditorGUILayout.TextField("Metallic property name: ", exportSettings.propNameMetallic);
exportSettings.propNameSmoothness = EditorGUILayout.TextField("Smoothness property name: ", exportSettings.propNameSmoothness);
}
GUILayout.Space(10f);
redChannel = (TexturePackerUtilities.SurfaceData)EditorGUILayout.EnumPopup("Red:", redChannel);
greenChannel = (TexturePackerUtilities.SurfaceData)EditorGUILayout.EnumPopup("Green:", greenChannel);
blueChannel = (TexturePackerUtilities.SurfaceData)EditorGUILayout.EnumPopup("Blue:", blueChannel);
GUILayout.Space(10f);
exportSettings.format = (TexturePackerUtilities.Format)EditorGUILayout.EnumPopup("Format:", exportSettings.format);
GUILayout.Space(10f);
exportSettings.textureFormatStandalone = (TextureImporterFormat)EditorGUILayout.EnumPopup("Standalone", exportSettings.textureFormatStandalone);
exportSettings.textureFormatAndroid = (TextureImporterFormat)EditorGUILayout.EnumPopup("Android", exportSettings.textureFormatAndroid);
exportSettings.textureFormatIOS = (TextureImporterFormat)EditorGUILayout.EnumPopup("iOS", exportSettings.textureFormatIOS);
exportSettings.textureFormatWebGL = (TextureImporterFormat)EditorGUILayout.EnumPopup("WebGL", exportSettings.textureFormatWebGL);
GUILayout.Space(10f);
GUILayout.EndVertical();
// Button
GUILayout.BeginVertical(EditorStyles.helpBox);
GUILayout.Space(10f);
if (GUILayout.Button("Generate PBR Textures"))
{
EditorUtility.DisplayProgressBar("Packing Textures, please wait...", "", 1f);
try
{
List<PBRTexturesData> pbrTexturesDatas = new List<PBRTexturesData>();
// "keyword l: <label> t: <type>"
// https://docs.unity3d.com/ScriptReference/AssetDatabase.FindAssets.html
string[] guids = AssetDatabase.FindAssets("_ t: texture2D", new[] { texturesFolder });
foreach (string guid in guids)
{
Texture2D tex = (Texture2D)AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(guid), typeof(Texture2D));
if (tex == null)
{
continue;
}
// Get Texture Type
bool hasSuffixInName = false;
string suffix = "";
string textureName = "";
TextureType textureType = TextureType.Albedo;
foreach (string s in _suffixes)
{
if (tex.name.Contains(s))
{
hasSuffixInName = true;
suffix = s;
textureName = tex.name.Replace(suffix, "");
textureType = _suffixesDict.FirstOrDefault(x => x.Value == suffix).Key;
}
}
if (!hasSuffixInName)
{
continue;
}
// Duplicate texture because it's not 'Readable'
bool linear = textureType != TextureType.Albedo;
Texture2D texture = TexturePackerUtilities.DuplicateTexture(tex, linear);
// If there's existing data for this texture
int currentIndex = -1;
for (int i = 0; i < pbrTexturesDatas.Count; i++)
{
if (pbrTexturesDatas[i].name == textureName)
{
currentIndex = i;
break;
}
}
PBRTexturesData data;
if (currentIndex != -1)
{
data = pbrTexturesDatas[currentIndex];
}
else
{
data = new PBRTexturesData(textureName, texture.width, texture.height, redChannel, greenChannel, blueChannel);
pbrTexturesDatas.Add(data);
}
switch (textureType)
{
case TextureType.Albedo:
data.albedoTexture = texture;
break;
case TextureType.Normal:
data.normalTexture = texture;
break;
case TextureType.Metallic:
data.metallicTexture = texture;
break;
case TextureType.Smoothness:
data.smoothnessTexture = texture;
data.isRoughness = false;
break;
case TextureType.Roughness:
data.smoothnessTexture = texture;
data.isRoughness = true;
break;
case TextureType.AmbientOcclusion:
data.ambientOcclusionTexture = texture;
break;
}
}
if (pbrTexturesDatas != null && pbrTexturesDatas.Count > 0)
{
TexturePackerUtilities.GenerateAndSaveTextures(pbrTexturesDatas, exportSettings);
}
pbrTexturesDatas.Clear();
}
catch (Exception e)
{
Debug.Log($"!!! Exception: {e}");
}
EditorUtility.ClearProgressBar();
}
GUILayout.Space(10f);
GUILayout.EndVertical();
GUILayout.Space(100);
if (_editorWindow)
{
GUILayout.EndScrollView();
GUILayout.EndVertical();
GUILayout.EndArea();
}
}
#endif
}