-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextureTools.cs
571 lines (518 loc) · 19.9 KB
/
TextureTools.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
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
using System.Collections.Generic;
using System.IO;
using System.Collections;
using UnityEditor;
using UnityEngine;
using System;
using System.Threading;
public class Utilities : EditorWindow
{
public string ChooseePath(string path)
{
return EditorUtility.OpenFolderPanel("Chosee Path", path, "*.*");
}
}
public enum Options
{
Crop = 0,
Resize = 1
}
public enum RectOptions
{
Center = 0,
BottomRight = 1,
TopRight = 2,
BottomLeft = 3,
TopLeft = 4,
//Top = 5,
//Left = 6,
//Right = 7,
//Bottom = 8,
Custom = 9
}
public class ResizeTexture2D : Utilities
{
Vector2 scrollPositionTexture = Vector2.zero;
private Options optionsChoose;
private RectOptions rectOptions;
static EditorWindow window = null;
List<string> listString = new List<string>();
List<Texture2D> listTexture2D = new List<Texture2D>();
private string stringFileInfo = "";
private int widthCrop;
private int heightCrop;
private int widthResize = 1024, heightResize = 1024;
private Rect RectInput;
private int percent = 100;
private Texture2D texture = null;
private List<Texture2D> listTexturesOrigin = new List<Texture2D>();
private int xMod, yMod, defaultView;
[MenuItem("Utilities/Texture2D _F1")]
static void Init()
{
window = GetWindow<ResizeTexture2D>();
}
void OnGUI()
{
EditorGUILayout.LabelField("Click 1 ( or mutil Texture or choose the folder have them if them same size) to Edit"); EditorGUILayout.LabelField("Change value Width, Height to crop");
EditorGUILayout.LabelField("Resize return near to POT value of Texture");
EditorGUILayout.LabelField("Edit Texture");
optionsChoose = (Options)EditorGUILayout.EnumPopup("Choose type: ", optionsChoose);
ChooseOptions(optionsChoose);
if (optionsChoose == Options.Crop)
defaultView = EditorGUILayout.IntSlider("Size View: ", defaultView, 200, 1000);
scrollPositionTexture = EditorGUILayout.BeginScrollView(scrollPositionTexture, GUILayout.Width(position.width), GUILayout.Height(600));
GUILayout.TextArea(stringFileInfo);
if (optionsChoose == Options.Crop)
{
for (int i = 0; i < listTexture2D.Count; i++)
{
texture = listTexture2D[i];
if (texture != null)
{
DrawTexture(listTexturesOrigin[i], i, 25, ": Origin size: ");
DrawTexture(texture, i, defaultView + 55, "Preview size: ");
}
}
}
EditorGUILayout.EndScrollView();
}
void Review()
{
listTexture2D.Clear();
listString.Clear();
var selectedObjects = Selection.GetFiltered(typeof(UnityEngine.Texture2D), SelectionMode.DeepAssets);
if (selectedObjects != null)
{
if (optionsChoose == Options.Crop)
{
if ((widthCrop == 0 || heightCrop == 0))
{
return;
}
}
else
{
if ((widthResize == 0 || heightResize == 0))
{
return;
}
}
for (int index = 0; index < selectedObjects.Length; index++)
{
Texture2D newTexture2D = selectedObjects[index] as Texture2D;
listString.Add(AssetDatabase.GetAssetPath(selectedObjects[index]));
if (newTexture2D != null)
{
if (optionsChoose == Options.Crop)
{
Texture2D newTexture = null;
newTexture = new Texture2D(newTexture2D.width, newTexture2D.height, newTexture2D.format, false);
newTexture.LoadRawTextureData(newTexture2D.GetRawTextureData());
newTexture.name = newTexture2D.name;
newTexture.Apply();
listTexture2D.Add(newTexture);
}
else
{
Texture2D newTexture = null;
byte[] bytes = File.ReadAllBytes(AssetDatabase.GetAssetPath(selectedObjects[index]));
newTexture = new Texture2D(newTexture2D.width, newTexture2D.height, TextureFormat.RGBA32, false);
newTexture.LoadImage(bytes);
newTexture.name = newTexture2D.name;
newTexture.Apply();
listTexture2D.Add(newTexture);
}
}
}
LoadInfoFile();
}
}
void DrawTexture(Texture2D texture, int index, int posX, string LabelTitle)
{
float ratio = (float)texture.width / texture.height;
float widthView = texture.width < defaultView ? texture.width : defaultView;
float heigthView = widthView / ratio > defaultView ? defaultView : widthView / ratio;
widthView = heigthView >= defaultView ? heigthView * ratio : widthView;
float posLabelY = 35 + index * (defaultView + 25);
float posTextureDrawY = posLabelY + 20;
Rect rect = GUILayoutUtility.GetRect(position.width, position.width);
rect = new Rect(posX, posTextureDrawY, widthView, heigthView);
EditorGUI.LabelField(new Rect(posX, posLabelY, position.width / 2, 25), texture.name + LabelTitle + texture.width + "x" + texture.height);
EditorGUI.DrawTextureTransparent(rect, texture);
}
void ChooseOptions(Options options)
{
switch (options)
{
case Options.Resize:
widthResize = EditorGUILayout.IntField("Width:", widthResize);
heightResize = EditorGUILayout.IntField("Height:", heightResize);
EditorGUILayout.Space();
if (listTexturesOrigin.Count > 0)
{
widthResize = widthResize <= 0 ? 0 : widthResize;
heightResize = heightResize <= 0 ? 0 : heightResize;
}
int oldwidthResize = 0;
int oldheightResize = 0;
if (oldwidthResize != widthResize || oldheightResize != heightResize)
{
Review();
}
oldheightResize = heightResize;
oldwidthResize = widthResize;
if (GUILayout.Button("Resize Texture2D"))
{
ResizeTexture();
}
break;
case Options.Crop:
EditorGUILayout.BeginHorizontal();
EditorGUILayout.BeginVertical();
EditorGUI.BeginDisabledGroup(true);
EditorGUI.EndDisabledGroup();
rectOptions = (RectOptions)EditorGUILayout.EnumPopup("Position", rectOptions);
widthCrop = EditorGUILayout.IntField(" Width: ", widthCrop);
heightCrop = EditorGUILayout.IntField(" Height: ", heightCrop);
if (listTexturesOrigin.Count > 0)
{
widthCrop = listTexturesOrigin[0].width < widthCrop ? listTexturesOrigin[0].width : widthCrop <= 0 ? 0 : widthCrop;
heightCrop = listTexturesOrigin[0].height < heightCrop ? listTexturesOrigin[0].height : heightCrop <= 0 ? 0 : heightCrop;
}
int oldwidthCrop = 0;
int oldheightCrop = 0;
if (oldwidthCrop != widthCrop || oldheightCrop != heightCrop)
{
Review();
}
oldheightCrop = heightCrop;
oldwidthCrop = widthCrop;
if (rectOptions == RectOptions.Custom)
{
xMod = EditorGUILayout.IntField(" xMod: ", xMod);
yMod = EditorGUILayout.IntField(" yMod: ", yMod);
if (listTexturesOrigin.Count > 0)
{
int tempxMod = listTexturesOrigin[0].width - widthCrop;
int tempyMod = listTexturesOrigin[0].height - heightCrop;
xMod = xMod <= 0 ? 0 : xMod >= tempxMod ? tempxMod : xMod;
yMod = yMod <= 0 ? 0 : yMod >= tempyMod ? tempyMod : yMod;
}
int oldxMod = 0;
int oldyMod = 0;
if (oldxMod != xMod || oldyMod != yMod)
{
Review();
}
oldxMod = xMod;
oldyMod = yMod;
}
EditorGUILayout.EndVertical();
EditorGUILayout.EndHorizontal();
if (GUILayout.Button("Crop Texture"))
{
CropTexture(rectOptions);
}
break;
}
}
private void ResizeTexture()
{
Debug.Log("resize");
for (int index = 0; index < listTexture2D.Count; index++)
{
Debug.Log(listString[index].Replace(Application.dataPath, "Assets"));
TextureScale.Bilinear(listTexture2D[index], widthResize, heightResize);
byte[] bytes = listTexture2D[index].EncodeToPNG();
File.WriteAllBytes(listString[index].Replace(Application.dataPath, "Assets"), bytes);
}
AssetDatabase.Refresh();
}
void CropTexture(RectOptions rectOptions)
{
Debug.Log("crop");
for (int i = 0; i < listTexture2D.Count; i++)
{
Debug.Log(listString[i].Replace(Application.dataPath, "Assets"));
RectInput.width = widthCrop;
RectInput.height = heightCrop;
listTexture2D[i] = CropWithRect(rectOptions, listTexture2D[i], RectInput, xMod, yMod);
byte[] bytes = listTexture2D[i].EncodeToPNG();
File.WriteAllBytes(listString[i].Replace(Application.dataPath, "Assets"), bytes);
}
AssetDatabase.Refresh();
}
private static Texture2D CropWithRect(RectOptions rectOptions, Texture2D texture, Rect r, int xMod, int yMod)
{
if (r.height < 0 || r.width < 0)
{
return texture;
}
Texture2D result = new Texture2D((int)r.width, (int)r.height);
if (r.width != 0 && r.height != 0)
{
float xRect = r.x;
float yRect = r.y;
float widthRect = r.width;
float heightRect = r.height;
switch (rectOptions)
{
case RectOptions.Center:
xRect = (texture.width - r.width) / 2;
yRect = (texture.height - r.height) / 2;
break;
case RectOptions.BottomRight:
xRect = texture.width - r.width;
break;
case RectOptions.BottomLeft:
break;
case RectOptions.TopLeft:
yRect = texture.height - r.height;
break;
case RectOptions.TopRight:
xRect = texture.width - r.width;
yRect = texture.height - r.height;
break;
case RectOptions.Custom:
float tempWidth = texture.width - r.width - xMod;
float tempHeight = texture.height - r.height - yMod;
xRect = tempWidth > texture.width ? 0 : tempWidth;
yRect = tempHeight > texture.height ? 0 : tempHeight;
break;
}
if (texture.width < r.x + r.width || texture.height < r.y + r.height || xRect > r.x + texture.width || yRect > r.y + texture.height || xRect < 0 || yRect < 0 || r.width < 0 || r.height < 0)
{
//EditorUtility.DisplayDialog("Set value crop", "Set value crop (Width and Height > 0) less than origin texture size \n" + texture.name + " wrong size", "ReSet");
return texture;
}
result.SetPixels(texture.GetPixels(Mathf.FloorToInt(xRect), Mathf.FloorToInt(yRect), Mathf.FloorToInt(widthRect), Mathf.FloorToInt(heightRect)));
result.Apply();
}
return result;
}
private void LoadInfoFile()
{
listTexturesOrigin.Clear();
listTexturesOrigin.AddRange(listTexture2D);
stringFileInfo = "Total files: " + listTexture2D.Count + "\n";
for (int i = 0; i < listTexture2D.Count; i++)
{
switch (optionsChoose)
{
case Options.Crop:
RectInput.width = widthCrop;
RectInput.height = heightCrop;
listTexture2D[i] = CropWithRect(rectOptions, listTexture2D[i], RectInput, xMod, yMod);
break;
case Options.Resize:
//TextureScale.Bilinear(listTexture2D[i], widthResize, heightResize);
break;
}
}
}
}
public class TextureScale
{
public class ThreadData
{
public int start;
public int end;
public ThreadData(int s, int e)
{
start = s;
end = e;
}
}
private static Color[] texColors;
private static Color[] newColors;
private static int w;
private static float ratioX;
private static float ratioY;
private static int w2;
private static int finishCount;
private static Mutex mutex;
public static void Point(Texture2D tex, int newWidth, int newHeight)
{
ThreadedScale(tex, newWidth, newHeight, false);
}
public static void Bilinear(Texture2D tex, int newWidth, int newHeight)
{
ThreadedScale(tex, newWidth, newHeight, true);
}
private static void ThreadedScale(Texture2D tex, int newWidth, int newHeight, bool useBilinear)
{
texColors = tex.GetPixels();
newColors = new Color[newWidth * newHeight];
if (useBilinear)
{
ratioX = 1.0f / ((float)newWidth / (tex.width - 1));
ratioY = 1.0f / ((float)newHeight / (tex.height - 1));
}
else
{
ratioX = ((float)tex.width) / newWidth;
ratioY = ((float)tex.height) / newHeight;
}
w = tex.width;
w2 = newWidth;
var cores = Mathf.Min(SystemInfo.processorCount, newHeight);
var slice = newHeight / cores;
finishCount = 0;
if (mutex == null)
{
mutex = new Mutex(false);
}
if (cores > 1)
{
int i = 0;
ThreadData threadData;
for (i = 0; i < cores - 1; i++)
{
threadData = new ThreadData(slice * i, slice * (i + 1));
ParameterizedThreadStart ts = useBilinear ? new ParameterizedThreadStart(BilinearScale) : new ParameterizedThreadStart(PointScale);
Thread thread = new Thread(ts);
thread.Start(threadData);
}
threadData = new ThreadData(slice * i, newHeight);
if (useBilinear)
{
BilinearScale(threadData);
}
else
{
PointScale(threadData);
}
while (finishCount < cores)
{
Thread.Sleep(1);
}
}
else
{
ThreadData threadData = new ThreadData(0, newHeight);
if (useBilinear)
{
BilinearScale(threadData);
}
else
{
PointScale(threadData);
}
}
tex.Resize(newWidth, newHeight);
tex.SetPixels(newColors);
tex.Apply();
texColors = null;
newColors = null;
}
public static void BilinearScale(System.Object obj)
{
ThreadData threadData = (ThreadData)obj;
for (var y = threadData.start; y < threadData.end; y++)
{
int yFloor = (int)Mathf.Floor(y * ratioY);
var y1 = yFloor * w;
var y2 = (yFloor + 1) * w;
var yw = y * w2;
for (var x = 0; x < w2; x++)
{
int xFloor = (int)Mathf.Floor(x * ratioX);
var xLerp = x * ratioX - xFloor;
newColors[yw + x] = ColorLerpUnclamped(ColorLerpUnclamped(texColors[y1 + xFloor], texColors[y1 + xFloor + 1], xLerp),
ColorLerpUnclamped(texColors[y2 + xFloor], texColors[y2 + xFloor + 1], xLerp),
y * ratioY - yFloor);
}
}
mutex.WaitOne();
finishCount++;
mutex.ReleaseMutex();
}
public static void PointScale(System.Object obj)
{
ThreadData threadData = (ThreadData)obj;
for (var y = threadData.start; y < threadData.end; y++)
{
var thisY = (int)(ratioY * y) * w;
var yw = y * w2;
for (var x = 0; x < w2; x++)
{
newColors[yw + x] = texColors[(int)(thisY + ratioX * x)];
}
}
mutex.WaitOne();
finishCount++;
mutex.ReleaseMutex();
}
private static Color ColorLerpUnclamped(Color c1, Color c2, float value)
{
return new Color(c1.r + (c2.r - c1.r) * value,
c1.g + (c2.g - c1.g) * value,
c1.b + (c2.b - c1.b) * value,
c1.a + (c2.a - c1.a) * value);
}
}
public class TextureTools
{
public static Texture2D ResampleAndCrop(Texture2D source, int targetWidth, int targetHeight)
{
int sourceWidth = source.width;
int sourceHeight = source.height;
float sourceAspect = (float)sourceWidth / sourceHeight;
float targetAspect = (float)targetWidth / targetHeight;
int xOffset = 0;
int yOffset = 0;
float factor = 1;
if (sourceAspect > targetAspect)
{ // crop width
factor = (float)targetHeight / sourceHeight;
xOffset = (int)((sourceWidth - sourceHeight * targetAspect) * 0.5f);
}
else
{ // crop height
factor = (float)targetWidth / sourceWidth;
yOffset = (int)((sourceHeight - sourceWidth / targetAspect) * 0.5f);
}
Color32[] data = source.GetPixels32();
Color32[] data2 = new Color32[targetWidth * targetHeight];
for (int y = 0; y < targetHeight; y++)
{
for (int x = 0; x < targetWidth; x++)
{
float xPos = xOffset + x / factor;
float yPos = yOffset + y / factor;
if (xPos > sourceWidth - 1)
xPos = sourceWidth - 1;
if (yPos > sourceHeight - 1)
yPos = sourceHeight - 1;
int x1 = (int)xPos; int x2 = x1 + 1;
int y1 = (int)yPos; int y2 = y1 + 1;
float fx = xPos - x1;
float fy = yPos - y1;
y1 *= sourceWidth;
y2 *= sourceWidth;
var c11 = data[x1 + y1];
var c12 = data[x1 + y2];
var c21 = data[x2 + y1];
var c22 = data[x2 + y2];
float f11 = (1 - fx) * (1 - fy);
float f12 = (1 - fx) * fy;
float f21 = fx * (1 - fy);
float f22 = fx * fy;
float r = c11.r * f11 + c12.r * f12 + c21.r * f21 + c22.r * f22;
float g = c11.g * f11 + c12.g * f12 + c21.g * f21 + c22.g * f22;
float b = c11.b * f11 + c12.b * f12 + c21.b * f21 + c22.b * f22;
float a = c11.a * f11 + c12.a * f12 + c21.a * f21 + c22.a * f22;
int index = x + y * targetWidth;
data2[index].r = (byte)r;
data2[index].g = (byte)g;
data2[index].b = (byte)b;
data2[index].a = (byte)a;
}
}
var tex = new Texture2D(targetWidth, targetHeight);
tex.SetPixels32(data2);
tex.Apply(true);
return tex;
}
}