-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertUnrealtoEnfusion.cs
More file actions
254 lines (234 loc) · 11.9 KB
/
ConvertUnrealtoEnfusion.cs
File metadata and controls
254 lines (234 loc) · 11.9 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
using ImageMagick;
using System;
namespace TextureConverter
{
public partial class ConvertUnrealtoEnfusion : Form
{
public ConvertUnrealtoEnfusion()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
button2.Select();
}
private void button2_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Text = folderBrowserDialog1.SelectedPath;
textBox2.Text = folderBrowserDialog1.SelectedPath; // auto-output by default
}
}
private void button3_Click(object sender, EventArgs e)
{
if (folderBrowserDialog2.ShowDialog() == DialogResult.OK)
textBox2.Text = folderBrowserDialog2.SelectedPath;
}
private async void button1_Click(object sender, EventArgs e)
{
string inputFolder = textBox1.Text;
string outputFolder = textBox2.Text;
if (!Directory.Exists(inputFolder))
{
MessageBox.Show("You need to specify a source directory to get started.", "Failed to find source folder", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (!Directory.Exists(outputFolder))
Directory.CreateDirectory(outputFolder); // Create output folder if it doesn't exist
string[] fileSuffixes = { textBox3.Text + ".PNG", textBox6.Text + ".PNG", textBox5.Text + ".PNG" };
// Check the state of the LZW checkbox
bool useLZWCompression = checkBox1.Checked;
List<string> completeTextures = new List<string>();
foreach (string folder in Directory.GetDirectories(inputFolder))
{
foreach (string file in Directory.GetFiles(folder))
{
if (Array.Exists(fileSuffixes, suffix => file.EndsWith(suffix, StringComparison.OrdinalIgnoreCase)))
{
if (comboBox1.SelectedItem.ToString() == "MR/MetalnessRoughness")
{
if (file.EndsWith(textBox3.Text + ".PNG", StringComparison.OrdinalIgnoreCase))
{
await MR_ProcessBCRTexture(file, useLZWCompression, folder, folder);
}
else if (file.EndsWith(textBox5.Text + ".PNG", StringComparison.OrdinalIgnoreCase))
{
await MR_ProcessNMOTexture(file, useLZWCompression, folder, folder);
}
}
else if (comboBox1.SelectedItem.ToString() == "AORM/ARM")
{
if (file.EndsWith(textBox3.Text + ".PNG", StringComparison.OrdinalIgnoreCase))
{
await AORM_ProcessBCRTexture(file, useLZWCompression, folder, folder);
}
else if (file.EndsWith(textBox5.Text + ".PNG", StringComparison.OrdinalIgnoreCase))
{
await AORM_ProcessNMOTexture(file, useLZWCompression, folder, folder);
}
}
completeTextures.Add(file);
}
}
}
MessageBox.Show("All the texture files within the source folder have been converted, check the destination folder for results.", "Finished converting textures", MessageBoxButtons.OK, MessageBoxIcon.Information);
progressBar1.Value = 0; // Reset progress bar...
foreach(var file in completeTextures)
{
File.Delete(file);
}
}
private async Task AORM_ProcessBCRTexture(string file, bool useLZWCompression, string inputFolder, string outputFolder)
{
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);
progressBar1.Value = 25;
using (MagickImage albedoImage = new MagickImage(file))
{
MagickImage masksImage = new MagickImage(Path.Combine(inputFolder, file.Replace(textBox3.Text + ".PNG", textBox6.Text + ".PNG")));
progressBar1.Value = 50;
MagickImage masksGreenChannel = (MagickImage)masksImage.Separate(Channels.Green).First();
progressBar1.Value = 75;
// Composite the new alpha image into the Albedo image
albedoImage.Alpha(AlphaOption.Transparent);
albedoImage.Composite(masksGreenChannel, CompositeOperator.CopyAlpha);
progressBar1.Value = 100;
// Check compression and save image
if (useLZWCompression)
{
albedoImage.Format = MagickFormat.Tiff;
albedoImage.Settings.Compression = CompressionMethod.LZW;
albedoImage.Write(Path.Combine(outputFolder, Path.GetFileNameWithoutExtension(file).Replace(textBox3.Text, "_BCR.TIF")));
}
else
{
albedoImage.Write(Path.Combine(outputFolder, Path.GetFileNameWithoutExtension(file).Replace(textBox3.Text, "_BCR.TIF")));
}
albedoImage.Dispose();
masksImage.Dispose();
masksGreenChannel.Dispose();
}
}
private async Task MR_ProcessBCRTexture(string file, bool useLZWCompression, string inputFolder, string outputFolder)
{
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);
progressBar1.Value = 25;
using (MagickImage albedoImage = new MagickImage(file))
{
MagickImage masksImage = new MagickImage(Path.Combine(inputFolder, file.Replace(textBox3.Text + ".PNG", textBox6.Text + ".PNG")));
masksImage.Negate(); // Invert
progressBar1.Value = 50;
MagickImage masksGreenChannel = (MagickImage)masksImage.Separate(Channels.Green).First();
progressBar1.Value = 75;
// Composite the new alpha image into the Albedo image
albedoImage.Alpha(AlphaOption.Transparent);
albedoImage.Composite(masksGreenChannel, CompositeOperator.CopyAlpha);
progressBar1.Value = 100;
// Check compression and save image
if (useLZWCompression)
{
albedoImage.Format = MagickFormat.Tiff;
albedoImage.Settings.Compression = CompressionMethod.LZW;
albedoImage.Write(Path.Combine(outputFolder, Path.GetFileNameWithoutExtension(file).Replace(textBox3.Text, "_BCR.TIF")));
}
else
{
albedoImage.Write(Path.Combine(outputFolder, Path.GetFileNameWithoutExtension(file).Replace(textBox3.Text, "_BCR.TIF")));
}
albedoImage.Dispose();
masksImage.Dispose();
masksGreenChannel.Dispose();
}
}
private async Task AORM_ProcessNMOTexture(string file, bool useLZWCompression, string inputFolder, string outputFolder)
{
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);
progressBar1.Value = 20;
using (MagickImage normalImage = new MagickImage(file))
{
MagickImage masksImage = new MagickImage(Path.Combine(inputFolder, file.Replace(textBox5.Text + ".PNG", textBox6.Text + ".PNG")));
progressBar1.Value = 40;
MagickImage masksRedChannel = (MagickImage)masksImage.Separate(Channels.Red).First();
progressBar1.Value = 60;
MagickImage masksBlueChannel = (MagickImage)masksImage.Separate(Channels.Blue).First();
progressBar1.Value = 80;
// Composite the new alpha image into the Normal image
normalImage.Alpha(AlphaOption.Transparent);
normalImage.Composite(masksBlueChannel, CompositeOperator.CopyBlue);
normalImage.Composite(masksRedChannel, CompositeOperator.CopyAlpha);
progressBar1.Value = 100;
// Check compression and save image
if (useLZWCompression)
{
normalImage.Format = MagickFormat.Tiff;
normalImage.Settings.Compression = CompressionMethod.LZW;
normalImage.Write(Path.Combine(outputFolder, Path.GetFileNameWithoutExtension(file).Replace(textBox5.Text, "_NMO.TIF")));
}
else
{
normalImage.Write(Path.Combine(outputFolder, Path.GetFileNameWithoutExtension(file).Replace(textBox5.Text, "_NMO.TIF")));
}
normalImage.Dispose();
masksImage.Dispose();
masksRedChannel.Dispose();
masksBlueChannel.Dispose();
}
}
private async Task MR_ProcessNMOTexture(string file, bool useLZWCompression, string inputFolder, string outputFolder)
{
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);
progressBar1.Value = 20;
using (MagickImage normalImage = new MagickImage(file))
{
MagickImage masksImage = new MagickImage(Path.Combine(inputFolder, file.Replace(textBox5.Text + ".PNG", textBox6.Text + ".PNG")));
progressBar1.Value = 50;
MagickImage masksBlueChannel = (MagickImage)masksImage.Separate(Channels.Blue).First();
MagickImage occlusionImage = new MagickImage(Path.Combine(inputFolder, file.Replace(textBox5.Text + ".PNG", "_Occlusion.PNG")));
progressBar1.Value = 60;
MagickImage masksRedChannel = (MagickImage)occlusionImage.Separate(Channels.Red).First();
progressBar1.Value = 75;
// Composite the new alpha image into the Normal image
normalImage.Alpha(AlphaOption.Transparent);
normalImage.Composite(masksBlueChannel, CompositeOperator.CopyBlue);
normalImage.Composite(masksRedChannel, CompositeOperator.CopyAlpha);
progressBar1.Value = 100;
// Check compression and save image
if (useLZWCompression)
{
normalImage.Format = MagickFormat.Tiff;
normalImage.Settings.Compression = CompressionMethod.LZW;
normalImage.Write(Path.Combine(outputFolder, Path.GetFileNameWithoutExtension(file).Replace(textBox5.Text, "_NMO.TIF")));
}
else
{
normalImage.Write(Path.Combine(outputFolder, Path.GetFileNameWithoutExtension(file).Replace(textBox5.Text, "_NMO.TIF")));
}
normalImage.Dispose();
masksImage.Dispose();
masksBlueChannel.Dispose();
masksRedChannel.Dispose();
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedItem.ToString() == "MR/MetalnessRoughness")
{
textBox3.Text = "_BaseColor";
groupBox7.Text = "MetallicRoughness";
textBox6.Text = "_MetallicRoughness";
}
else if (comboBox1.SelectedItem.ToString() == "AORM/ARM")
{
textBox3.Text = "_Albedo";
groupBox7.Text = "Masks";
textBox6.Text = "_Masks";
}
}
private void groupBox7_Enter(object sender, EventArgs e)
{
}
private void groupBox4_Enter(object sender, EventArgs e)
{
}
}
}