-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
454 lines (413 loc) · 12.4 KB
/
Program.cs
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
using System;
using System.Collections.Generic;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Text;
//Copyright (c) 2017 Anna Eerkes All rights reserved;
//License (CC BY-SA 4.0)
//compile with: /unsafe
namespace RMAOECompiler
{
class Program
{
public static Settings compileSettings = new Settings();
public static int maxResolution
{
get
{
return compileSettings.resolution;
}
set
{
compileSettings.resolution = value;
}
}
public enum EPostFix
{
_R,
_M,
_AO,
_E
}
public enum ETextureType
{
Roughness,
Metallicity,
AmbientOcclusion,
Emissive
}
public static int threads = Environment.ProcessorCount;
public static List<Bitmap> tempResult = new List<Bitmap>();
//we have this set here so we can generate this image once and just re-use it.
private static Bitmap _defaultColour = null;
public static Bitmap defaultColour
{
get
{
if (_defaultColour == null)
{
Bitmap b = new Bitmap(maxResolution, maxResolution);
using (Graphics graph = Graphics.FromImage(b))
{
int AlphaColor = 255 - compileSettings.emissiveDefaultValue;
if (!compileSettings.invertAlpha)
{
AlphaColor = compileSettings.emissiveDefaultValue;
}
SolidBrush brush = new SolidBrush(Color.FromArgb(AlphaColor, compileSettings.roughnessDefaultValue,
compileSettings.metallicityDefaultValue, compileSettings.ambientOcclusionDefaultValue));
graph.FillRectangle(brush, 0, 0, maxResolution, maxResolution);
}
_defaultColour = b;
}
return _defaultColour;
}
}
static void Main(string[] args)
{
CopyRight();
initialize();
//we set a default resolution; can be overwritten with a commandprompt
//this is legacy fallback; should now just use settings.json
if (args.Length != 0)
{
int num = 0;
Int32.TryParse(args[0], out num);
compileSettings.resolution = num;
}
List <string> allFiles = new List<string>();
//We gather all textures, we can have textures where we don't have certain values; those default to black.
foreach (ETextureType textureType in Enum.GetValues(typeof(ETextureType)))
{
allFiles = Index(textureType, allFiles);
}
//once we get the name of all the textures; actually compile them.
foreach (string fileName in allFiles)
{
CreateTexture(fileName);
//we run a garbage collect after each file has been created
System.GC.Collect();
}
//Keep the console open until ENTER has been pressed.
Console.WriteLine("");
Console.WriteLine("==============================================================");
Console.WriteLine(" Press ENTER to exit... ");
Console.ReadLine();
}
//Gather the clean names of the textures
static string CleanName(string dirtyName)
{
string cleanName = dirtyName.Replace(".png", "");
cleanName = cleanName.Replace("\\", "");
//we replace all the different postfixes
foreach (EPostFix postFix in Enum.GetValues(typeof(EPostFix)))
{
cleanName = cleanName.Replace(postFix.ToString(), "");
}
return cleanName;
}
//Create a list of all the different textures
static List<string> Index(ETextureType type, List<string> allFiles)
{
string typeName = type.ToString();
string[] fileNames = Directory.GetFiles(typeName);
foreach (string file in fileNames)
{
string fn = CleanName(file).Replace(typeName,"");
if (!allFiles.Contains(fn))
{
allFiles.Add(fn);
}
}
return allFiles;
}
//Create and save the texture
static void CreateTexture(string fileName)
{
Console.WriteLine(string.Format("Compiling texture: {0}",fileName));
string filePath = null;
Bitmap[] image = new Bitmap[4];
Bitmap endResult = new Bitmap(maxResolution, maxResolution);
//this can be hardcoded; because we only have 4 channels anyways;
for (int i = 0; i < 4; i++)
{
filePath = string.Format("{0}/{1}{2}.png", Enum.GetNames(typeof(ETextureType))[i], fileName, Enum.GetNames(typeof(EPostFix))[i]);
if (File.Exists(filePath))
{
Bitmap thisBitmap = (Bitmap)Image.FromFile(filePath);
if (thisBitmap.Height != maxResolution || thisBitmap.Width != maxResolution)
{
//Here we resize the image;
thisBitmap = new Bitmap(thisBitmap, maxResolution, maxResolution);
}
image[i] = thisBitmap;
}
else
{
//We set the texture to white, so we have control over it in-engine;
image[i] = defaultColour;
}
}
//Actually compile the image
endResult = compileImage(image);
//now save:
string fullFilePath = string.Format("RMAOE/{0}_RMAOE.png", fileName);
if (File.Exists(fullFilePath))
{
File.Delete(fullFilePath);
}
endResult.Save(fullFilePath, ImageFormat.Png);
}
static Bitmap compileImage(Bitmap[] image)
{
//initialize and cache the final image
Bitmap result = new Bitmap(maxResolution, maxResolution);
BitmapData resultBmData = result.LockBits(new Rectangle(0, 0, maxResolution, maxResolution), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
int bytesPerPixel = Bitmap.GetPixelFormatSize(image[0].PixelFormat) / 8;
int widthInBytes = maxResolution * bytesPerPixel;
BitmapData[] imageData = new BitmapData[4];
for (int i = 0; i < 4; i++)
{
imageData[i] = image[i].LockBits(new Rectangle(0, 0, maxResolution, maxResolution), ImageLockMode.ReadOnly, image[i].PixelFormat);
image[i].UnlockBits(imageData[i]);
}
//since we're using pointers we have to use unsafe.
unsafe
{
//since we're using a variety of different textures, we need pointers to them all.
byte* ptrFirstPixel = (byte*)resultBmData.Scan0;
byte* ptrRedPixel = (byte*)imageData[0].Scan0; //Roughness
byte* ptrGreenPixel = (byte*)imageData[1].Scan0; //Metallicity
byte* ptrBluePixel = (byte*)imageData[2].Scan0; //AO
byte* ptrAlphaPixel = (byte*)imageData[3].Scan0; //Emissive;
Parallel.For(0, maxResolution, y =>
{
byte* currentRedLine = ptrRedPixel + (y * imageData[0].Stride);
byte* currentGreenLine = ptrGreenPixel + (y * imageData[1].Stride);
byte* currentBlueLine = ptrBluePixel + (y * imageData[2].Stride);
byte* currentAlphaLine = ptrAlphaPixel + (y * imageData[3].Stride);
byte* currentLine = ptrFirstPixel + (y * resultBmData.Stride);
for (int x= 0; x < widthInBytes; x = x + bytesPerPixel)
{
currentLine[x] = currentBlueLine[x + 2];
currentLine[x + 1] = currentGreenLine[x + 2];
currentLine[x + 2] = currentRedLine[x + 2];
currentLine[x + 3] = currentAlphaLine[x + 2];
}
});
result.UnlockBits(resultBmData);
}
return result;
}
static void initialize()
{
//Create a directory to put used files into
if (!Directory.Exists("RMAOE"))
{
Directory.CreateDirectory("RMAOE");
}
foreach (ETextureType type in Enum.GetValues(typeof(ETextureType)))
{
string typeName = type.ToString();
if (!Directory.Exists(typeName))
{
System.Console.WriteLine("Creating folder: " + typeName);
Directory.CreateDirectory(typeName);
}
}
//load settings
if (File.Exists("settings.json"))
{
StreamReader sr = new StreamReader("settings.json");
string jsonString = sr.ReadToEnd();
JavaScriptSerializer ser = new JavaScriptSerializer();
compileSettings = ser.Deserialize<Settings>(jsonString);
}
else
{
//if we don't have settings, create settings
JavaScriptSerializer ser = new JavaScriptSerializer();
string json = FormatOutput(ser.Serialize(compileSettings));
File.WriteAllText("settings.json", json);
}
}
static void CopyRight()
{
Console.WriteLine(" RMAOE Compiler (c) 2017 Anna Eerkes ");
Console.WriteLine(" License (CC BY-SA 4.0) ");
Console.WriteLine("==============================================================");
Console.WriteLine("=== This merges a variety of black/white textures into one ===");
Console.WriteLine("=== To be used with PBR engines like Unreal or Unity ===");
Console.WriteLine("=== Red Channel - Roughness (R) ===");
Console.WriteLine("=== Green Channel - Metallicity (M) ===");
Console.WriteLine("=== Blue Channel - Ambient Occlusion (AO) ===");
Console.WriteLine("=== Alpha Channel - INVERSE Emissive (E) ===");
Console.WriteLine("==============================================================");
Console.WriteLine("");
}
//Pretty up the json to make it actually useable.
//code from https://stackoverflow.com/a/23828858
public static string FormatOutput(string jsonString)
{
var stringBuilder = new StringBuilder();
bool escaping = false;
bool inQuotes = false;
int indentation = 0;
foreach (char character in jsonString)
{
if (escaping)
{
escaping = false;
stringBuilder.Append(character);
}
else
{
if (character == '\\')
{
escaping = true;
stringBuilder.Append(character);
}
else if (character == '\"')
{
inQuotes = !inQuotes;
stringBuilder.Append(character);
}
else if (!inQuotes)
{
if (character == ',')
{
stringBuilder.Append(character);
stringBuilder.Append("\r\n");
stringBuilder.Append('\t', indentation);
}
else if (character == '[' || character == '{')
{
stringBuilder.Append(character);
stringBuilder.Append("\r\n");
stringBuilder.Append('\t', ++indentation);
}
else if (character == ']' || character == '}')
{
stringBuilder.Append("\r\n");
stringBuilder.Append(' ', --indentation);
stringBuilder.Append(character);
}
else if (character == ':')
{
stringBuilder.Append(character);
stringBuilder.Append(' ');
}
else
{
stringBuilder.Append(character);
}
}
else
{
stringBuilder.Append(character);
}
}
}
return stringBuilder.ToString();
}
}
public class Settings
{
private int _resolution = 1024;
public int resolution
{
get
{
return _resolution;
}
set
{
if (value <= 0 || value > 8192 || (value & (value - 1)) != 0)
{
//just log some of the exceptions
if (value <= 0)
{
Console.WriteLine("Resolution needs to be bigger than 0.");
}
else if (value > 8192)
{
Console.WriteLine("Requested resolution is too high; this was written in 2017, 8k textures were pretty much the highest we could go!");
}
else if ((value & (value - 1)) != 0)
{
Console.WriteLine("Need to use a power of 2-resolution. This is much better for game engines.");
}
}
else
{
_resolution = value;
Console.WriteLine(string.Format("Using requested resolution: {0}", value));
}
}
}
public bool invertAlpha = true;
private int _roughnessDefaultValue = 255;
public int roughnessDefaultValue
{
get
{
return _roughnessDefaultValue;
}
set
{
if (value < 256 && value >= 0)
{
_roughnessDefaultValue = value;
}
}
}
private int _metallicityDefaultValue = 255;
public int metallicityDefaultValue
{
get
{
return _metallicityDefaultValue;
}
set
{
if (value< 256 && value >= 0)
{
_metallicityDefaultValue = value;
}
}
}
private int _ambientOcclusionDefaultValue = 255;
public int ambientOcclusionDefaultValue
{
get
{
return _ambientOcclusionDefaultValue;
}
set
{
if (value < 256 && value >= 0)
{
_ambientOcclusionDefaultValue = value;
}
}
}
private int _emissiveDefaultValue = 0;
public int emissiveDefaultValue
{
get
{
return _emissiveDefaultValue;
}
set
{
if (value < 256 && value >= 0)
{
_emissiveDefaultValue = value;
}
}
}
}
}