-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathTextureLoader.cs
More file actions
511 lines (452 loc) · 20.3 KB
/
TextureLoader.cs
File metadata and controls
511 lines (452 loc) · 20.3 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
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using UndertaleModLib.Models;
using UndertaleModLib;
using System.Text.RegularExpressions;
using System.Collections;
using System.Drawing.Imaging;
using Serilog;
using UndertaleModLib.Util;
namespace ModShardLauncher
{
public class TextureInfo
{
public string Source;
public byte[] Data;
public int Width;
public int Height;
}
public enum SplitType
{
Horizontal,
Vertical
}
public enum BestFitHeuristic
{
Area,
MaxOneAxis,
}
public class Node
{
public Rectangle Bounds;
public TextureInfo Texture;
public SplitType SplitType;
}
public class Atlas
{
public int Width;
public int Height;
public List<Node> Nodes;
}
public class TextureLoader
{
public static List<TextureInfo> SourceTextures = new();
public static StringWriter LogWriter;
public static StringWriter Error;
public static int padding;
public static int AtlasSize;
public static BestFitHeuristic FitHeuristic;
public static List<Atlas> Atlasses;
public static Regex sprFrameRegex = new(@"^(.+?)(?:_(-*\d+))*$", RegexOptions.Compiled);
public static UndertaleData Data => DataLoader.data;
public TextureLoader()
{
SourceTextures = new List<TextureInfo>();
LogWriter = new StringWriter();
Error = new StringWriter();
}
public static void LoadTextures(ModFile mod)
{
Process(mod, 2048, 2, false);
foreach (Atlas atlas in Atlasses)
{
// read an atlas as an image
Image image = CreateAtlasImage(atlas);
// save the image in a memory stream
MemoryStream memoryStream = new();
image.Save(memoryStream, ImageFormat.Png);
memoryStream.Seek(0, SeekOrigin.Begin);
// create a new embedded texture
UndertaleEmbeddedTexture ueTexture = new()
{
Name = Data.Strings.MakeString(mod.Name)
};
// вместо TextureBlob используем GMImage
ueTexture.TextureData.Image = GMImage.FromPng(memoryStream.ToArray());
// добавляем новую текстуру в Data
Data.EmbeddedTextures.Add(ueTexture);
// convert the image as a bitmap to compute the collision mask
Bitmap atlasBitmap = new(image);
foreach (Node node in atlas.Nodes)
{
if(node.Texture != null)
{
// create a new texture page item and add it in Data
UndertaleTexturePageItem texturePageItem = Msl.CreateTexureItem(
ueTexture,
new RectTexture((ushort)node.Bounds.X, (ushort)node.Bounds.Y, (ushort)node.Bounds.Width, (ushort)node.Bounds.Height),
new RectTexture(0, 0, (ushort)node.Bounds.Width, (ushort)node.Bounds.Height),
new BoundingData<ushort>((ushort)node.Bounds.Width, (ushort)node.Bounds.Height)
);
Data.TexturePageItems.Add(texturePageItem);
// this texture page item can be injected in a sprite
// reading the name of the png
// to find the frame number and the name of the sprite associated
string stripped = Path.GetFileNameWithoutExtension(node.Texture.Source);
string spriteName;
int frame;
try
{
System.Text.RegularExpressions.Match spriteParts = sprFrameRegex.Match(stripped);
spriteName = spriteParts.Groups[1].Value;
int.TryParse(spriteParts.Groups[2].Value, out frame);
}
catch
{
continue;
}
// find the sprite
UndertaleSprite sprite = Data.Sprites.ByName(spriteName);
// create a textureEntry from the texture page item
UndertaleSprite.TextureEntry textureEntry = new()
{
Texture = texturePageItem
};
// test if the sprite exists or not
if (sprite == null)
{
// clone the image
Rectangle bmpRect = new(node.Bounds.X, node.Bounds.Y, node.Bounds.Width, node.Bounds.Height);
PixelFormat format = atlasBitmap.PixelFormat;
Bitmap clone = atlasBitmap.Clone(bmpRect, format);
// create a new sprite
UndertaleSprite newSprite = Msl.CreateSpriteNoCollisionMasks(
spriteName,
new MarginData(0, node.Bounds.Height - 1, 0, node.Bounds.Width - 1),
new OriginData(0, 0),
new BoundingData<uint>((uint)node.Bounds.Width, (uint)node.Bounds.Height)
);
// populate the list of textures with null
// indeed, we want to add the texture at the position frame
// since the sprite didnt exist, we need to make the frame - 1 first texture as null
for (int i = 0; i < frame; i++)
newSprite.Textures.Add(null);
// create a new mask for this sprite but it seems Stoneshard does not use collision mask
newSprite.CollisionMasks.Add(newSprite.NewMaskEntry());
// ?
int correctedWidth = (node.Bounds.Width + 7) / 8 * 8;
// using a bit array to transpose masks, ?
BitArray maskingBitArray = new(correctedWidth * node.Bounds.Height);
BitArray bitArray = new(correctedWidth * node.Bounds.Height);
// loop on color off the image pixel by pixel
// and make a mask for alpha > 0
for(int y = 0; y < node.Bounds.Height; y++)
{
for (int x = 0; x < node.Bounds.Width; x++)
{
Color pixelColor = clone.GetPixel(x, y);
maskingBitArray[y * correctedWidth + x] = pixelColor.A > 0;
}
}
// ?
for(int i = 0; i < maskingBitArray.Length; i += 8)
{
for(int j = 0; j < 8; j++)
{
bitArray[j + i] = maskingBitArray[-(j - 7) + i];
}
}
// convert the bitArray into an array of bytes
byte[] bytes = new byte[maskingBitArray.Length / 8];
bitArray.CopyTo(bytes, 0);
// inject the array of bytes in the collision mask
for(int i = 0; i < bytes.Length; i++)
newSprite.CollisionMasks[0].Data[i] = bytes[i];
// put the texture page item in the frame index
newSprite.Textures.Add(textureEntry);
// add the new Sprite in data
Data.Sprites.Add(newSprite);
continue;
}
// the sprite already exists
// we then need to check the list of textures
// we want to add the texture page item at the frame index
// if the index is out of bound, fill the list with the texture page item until we reach frame - 1
if (frame > sprite.Textures.Count - 1)
{
while (frame > sprite.Textures.Count - 1)
{
sprite.Textures.Add(textureEntry);
}
continue;
}
// put the texture page item at the frame index
sprite.Textures[frame] = textureEntry;
}
}
}
Atlasses.Clear();
}
public static void Process(ModFile mod, int _AtlasSize, int _Padding, bool _DebugMode)
{
padding = _Padding;
AtlasSize = _AtlasSize;
// scan all textures in mod
// and store them in SourceTextures
ScanForTextures(mod);
List<TextureInfo> textures = SourceTextures.ToList();
//2: generate as many atlasses as needed (with the latest one as small as possible)
Atlasses = new List<Atlas>();
while (textures.Count > 0)
{
Atlas atlas = new()
{
Width = _AtlasSize,
Height = _AtlasSize
};
List<TextureInfo> leftovers = LayoutAtlas(textures, atlas);
if (leftovers.Count == 0)
{
// we reached the last atlas. Check if this last atlas could have been twice smaller
while (leftovers.Count == 0)
{
atlas.Width /= 2;
atlas.Height /= 2;
leftovers = LayoutAtlas(textures, atlas);
}
// we need to go 1 step larger as we found the first size that is to small
atlas.Width *= 2;
atlas.Height *= 2;
leftovers = LayoutAtlas(textures, atlas);
}
Atlasses.Add(atlas);
textures = leftovers;
}
}
public static void SaveAtlasses(string destination)
{
int atlasCount = 0;
string prefix = destination.Replace(Path.GetExtension(destination), "");
StreamWriter tw = new(destination);
tw.WriteLine("source_tex, atlas_tex, u, v, scale_u, scale_v");
foreach (Atlas atlas in Atlasses)
{
string atlasName = string.Format(prefix + "{0:000}" + ".png", atlasCount);
//1: Save images
Image img = CreateAtlasImage(atlas);
img.Save(atlasName, ImageFormat.Png);
//2: save description in file
foreach (Node n in atlas.Nodes)
{
if (n.Texture != null)
{
tw.Write(n.Texture.Source + ", ");
tw.Write(atlasName + ", ");
tw.Write(((float)n.Bounds.X / atlas.Width).ToString() + ", ");
tw.Write(((float)n.Bounds.Y / atlas.Height).ToString() + ", ");
tw.Write(((float)n.Bounds.Width / atlas.Width).ToString() + ", ");
tw.WriteLine(((float)n.Bounds.Height / atlas.Height).ToString());
}
}
++atlasCount;
}
tw.Close();
tw = new StreamWriter(prefix + ".log");
tw.WriteLine("--- LOG -------------------------------------------");
tw.WriteLine(LogWriter.ToString());
tw.WriteLine("--- ERROR -----------------------------------------");
tw.WriteLine(Error.ToString());
tw.Close();
}
// scan all png packed in the modFile
// and add them in a List<TextureInfo>
private static void ScanForTextures(ModFile modFile)
{
SourceTextures.Clear();
// look for all elements in the modFile
// and save the png ones
foreach (FileChunk fileChunk in modFile.Files)
{
// all non png files are not images
if (!fileChunk.name.EndsWith("png")) continue;
// icon.png is tied to the mod itself
if (fileChunk.name == modFile.Name + "\\" + "icon.png") continue;
// GetFile can fail
try
{
// open the image with a memory stream
byte[] byteFile = modFile.GetFile(fileChunk.name);
using Image image = Image.FromStream(new MemoryStream(byteFile));
// check if the image loaded is correct regarding the AtlasSize
if (image != null && image.Width <= AtlasSize && image.Height <= AtlasSize)
{
// create a new texture information from the image
TextureInfo textureInfo = new()
{
Width = image.Width,
Height = image.Height,
Source = fileChunk.name.Split("\\")[^1],
Data = byteFile
};
Log.Information("Successfully load texture {0}", fileChunk.name);
SourceTextures.Add(textureInfo);
}
else
{
throw new BadImageFormatException("Cannot load the image {0}", fileChunk.name);
}
}
catch(Exception ex)
{
Log.Error(ex, "Something went wrong");
}
}
}
private static void HorizontalSplit(Node _ToSplit, int _Width, int _Height, List<Node> _List)
{
Node n1 = new();
n1.Bounds.X = _ToSplit.Bounds.X + _Width + padding;
n1.Bounds.Y = _ToSplit.Bounds.Y;
n1.Bounds.Width = _ToSplit.Bounds.Width - _Width - padding;
n1.Bounds.Height = _Height;
n1.SplitType = SplitType.Vertical;
Node n2 = new();
n2.Bounds.X = _ToSplit.Bounds.X;
n2.Bounds.Y = _ToSplit.Bounds.Y + _Height + padding;
n2.Bounds.Width = _ToSplit.Bounds.Width;
n2.Bounds.Height = _ToSplit.Bounds.Height - _Height - padding;
n2.SplitType = SplitType.Horizontal;
if (n1.Bounds.Width > 0 && n1.Bounds.Height > 0)
_List.Add(n1);
if (n2.Bounds.Width > 0 && n2.Bounds.Height > 0)
_List.Add(n2);
}
private static void VerticalSplit(Node _ToSplit, int _Width, int _Height, List<Node> _List)
{
Node n1 = new();
n1.Bounds.X = _ToSplit.Bounds.X + _Width + padding;
n1.Bounds.Y = _ToSplit.Bounds.Y;
n1.Bounds.Width = _ToSplit.Bounds.Width - _Width - padding;
n1.Bounds.Height = _ToSplit.Bounds.Height;
n1.SplitType = SplitType.Vertical;
Node n2 = new();
n2.Bounds.X = _ToSplit.Bounds.X;
n2.Bounds.Y = _ToSplit.Bounds.Y + _Height + padding;
n2.Bounds.Width = _Width;
n2.Bounds.Height = _ToSplit.Bounds.Height - _Height - padding;
n2.SplitType = SplitType.Horizontal;
if (n1.Bounds.Width > 0 && n1.Bounds.Height > 0)
_List.Add(n1);
if (n2.Bounds.Width > 0 && n2.Bounds.Height > 0)
_List.Add(n2);
}
private static bool MaxOneAxisFit(Node node, TextureInfo ti, out float ratio)
{
if (ti.Width <= node.Bounds.Width && ti.Height <= node.Bounds.Height)
{
float wRatio = ti.Width / (float)node.Bounds.Width;
float hRatio = ti.Height / (float)node.Bounds.Height;
ratio = wRatio > hRatio ? wRatio : hRatio;
return true;
}
ratio = 0.0f;
return false;
}
private static bool AreaFit(Node node, TextureInfo ti, float nodeArea, out float coverage)
{
if (ti.Width <= node.Bounds.Width && ti.Height <= node.Bounds.Height)
{
float textureArea = ti.Width * ti.Height;
coverage = textureArea / nodeArea;
return true;
}
coverage = 0.0f;
return false;
}
private static TextureInfo? FindBestFitForNode(Node _Node, List<TextureInfo> _Textures)
{
TextureInfo? bestFit = null;
float nodeArea = _Node.Bounds.Width * _Node.Bounds.Height;
float maxCriteria = 0.0f;
foreach (TextureInfo ti in _Textures)
{
switch (FitHeuristic)
{
// Max of Width and Height ratios
case BestFitHeuristic.MaxOneAxis:
if(MaxOneAxisFit(_Node, ti, out float ratio) && ratio > maxCriteria)
{
maxCriteria = ratio;
bestFit = ti;
}
break;
// Maximize Area coverage
case BestFitHeuristic.Area:
if (AreaFit(_Node, ti, nodeArea, out float coverage) && coverage > maxCriteria)
{
maxCriteria = coverage;
bestFit = ti;
}
break;
}
}
return bestFit;
}
private static List<TextureInfo> LayoutAtlas(List<TextureInfo> textures, Atlas atlas)
{
List<Node> freeList = new();
atlas.Nodes = new List<Node>();
List<TextureInfo> textures_d = textures.ToList();
Node root = new();
root.Bounds.Size = new Size(atlas.Width, atlas.Height);
root.SplitType = SplitType.Horizontal;
freeList.Add(root);
while (freeList.Count > 0 && textures_d.Count > 0)
{
Node node = freeList[0];
freeList.RemoveAt(0);
TextureInfo? bestFit = FindBestFitForNode(node, textures_d);
if (bestFit != null)
{
if (node.SplitType == SplitType.Horizontal)
{
HorizontalSplit(node, bestFit.Width, bestFit.Height, freeList);
}
else
{
VerticalSplit(node, bestFit.Width, bestFit.Height, freeList);
}
node.Texture = bestFit;
node.Bounds.Width = bestFit.Width;
node.Bounds.Height = bestFit.Height;
textures_d.Remove(bestFit);
}
atlas.Nodes.Add(node);
}
return textures_d;
}
private static Image CreateAtlasImage(Atlas atlas)
{
// create a new empty image
Image image = new Bitmap(atlas.Width, atlas.Height, PixelFormat.Format32bppArgb);
// create the tool to populate this image
Graphics graphics = Graphics.FromImage(image);
foreach (Node n in atlas.Nodes)
{
if (n.Texture != null)
{
Image sourceImage = Image.FromStream(new MemoryStream(n.Texture.Data));
graphics.DrawImage(sourceImage, n.Bounds);
}
else
{
graphics.FillRectangle(Brushes.DarkMagenta, n.Bounds);
}
}
return image;
}
}
}