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
|
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
using Debug = UnityEngine.Debug;
//Made by Dreadrith#3238
//Discord: https://discord.gg/ZsPfrGn
//Github: https://github.com/Dreadrith/DreadScripts
//Gumroad: https://gumroad.com/dreadrith
//Ko-fi: https://ko-fi.com/dreadrith
namespace DreadScripts
{
public class GradientFlood : EditorWindow
{
#region Automated Variables
private static float modifiedBoundRange;
private static Texture2D titleTexture;
#endregion
#region Input
public static Texture2D pathTexture;
public static Color startPixelsColor = Color.red;
public static Color limitPixelsColor = Color.white;
public static GradientType gradientType = GradientType.DataGradient;
public static Color tintColor = Color.white;
public static Gradient gradientColor = new Gradient();
public static float gradientDistribution = 1;
public static float startColorTolerance = 0.05f;
public static float limitColorTolerance = 0.05f;
public static float rangeLowerBound;
public static float rangeUpperBound = 255;
public static bool invertGradient;
public static bool loopGradient;
public static bool applyGradientAlpha;
#endregion
public event EventHandler TextureGenerated;
public class TextureGeneratedEventArgs : EventArgs
{
public Texture2D generated_texture;
public Texture2D original_texture;
}
public enum GradientType
{
DataGradient,
TintedGradient,
GradientGradient
}
[MenuItem("Tools/Poi/Gradient Flood")]
private static void showWindow()
{
EditorWindow w = GetWindow<GradientFlood>(false, "Gradient Flood", true);
if (!titleTexture)
{
titleTexture = GetColors((Texture2D)EditorGUIUtility.IconContent("Texture2D Icon").image, 16, 16, out _);
titleTexture.Apply();
}
w.titleContent.image = titleTexture;
w.minSize = new Vector2(423, 253);
}
private void OnGUI()
{
GUIStyle centeredTitle = new GUIStyle("boldlabel") {alignment = TextAnchor.MiddleCenter, fontSize = 16};
using (new GUILayout.VerticalScope("helpbox"))
{
GUILayout.Label("Texture", centeredTitle);
using (new GUILayout.HorizontalScope())
{
GUILayout.FlexibleSpace();
EditorGUIUtility.labelWidth = 1;
pathTexture = (Texture2D) EditorGUILayout.ObjectField(string.Empty, pathTexture, typeof(Texture2D), false, GUILayout.Width(80), GUILayout.Height(80));
EditorGUIUtility.labelWidth = 0;
GUILayout.FlexibleSpace();
}
using (new GUILayout.HorizontalScope("box"))
gradientType = (GradientType)EditorGUILayout.EnumPopup("Gradient Type", gradientType);
switch (gradientType)
{
case GradientType.TintedGradient:
using (new GUILayout.HorizontalScope("box"))
tintColor = EditorGUILayout.ColorField("Tint", tintColor);
break;
case GradientType.GradientGradient:
using (new GUILayout.HorizontalScope("box"))
gradientColor = EditorGUILayout.GradientField("Gradient", gradientColor);
break;
}
if (gradientType > 0)
{
using (new GUILayout.HorizontalScope("box"))
{
GUILayout.Label("Color Range");
EditorGUI.BeginChangeCheck();
rangeLowerBound = EditorGUILayout.DelayedIntField((int) rangeLowerBound, GUI.skin.label, GUILayout.Width(28));
EditorGUILayout.MinMaxSlider(ref rangeLowerBound, ref rangeUpperBound, 0, 255);
rangeUpperBound = EditorGUILayout.DelayedIntField((int) rangeUpperBound, GUI.skin.label, GUILayout.Width(28));
if (EditorGUI.EndChangeCheck())
{
rangeUpperBound = Mathf.Clamp((int) rangeUpperBound, 0, 255);
rangeLowerBound = Mathf.Max(0, Mathf.Min(rangeUpperBound, rangeLowerBound));
}
}
}
using (new GUILayout.HorizontalScope("box"))
{
using (new GUILayout.HorizontalScope())
GUILayout.Label("Tolerance");
using (new GUILayout.HorizontalScope())
{
EditorGUIUtility.labelWidth = 40;
startColorTolerance = EditorGUILayout.Slider("Start", startColorTolerance, 0, 1);
limitColorTolerance = EditorGUILayout.Slider("Limit", limitColorTolerance, 0, 1);
EditorGUIUtility.labelWidth = 0;
}
}
using (new GUILayout.HorizontalScope("box"))
{
invertGradient = EditorGUILayout.Toggle("Invert Gradient", invertGradient);
if (gradientType > 0) applyGradientAlpha = EditorGUILayout.Toggle("Apply Gradient Alpha", applyGradientAlpha);
}
using (new GUILayout.HorizontalScope("box"))
{
loopGradient = EditorGUILayout.Toggle("Loop Gradient", loopGradient);
if (loopGradient) gradientDistribution = EditorGUILayout.FloatField("Distribution", gradientDistribution);
}
using (new EditorGUI.DisabledScope(!pathTexture))
if (GUILayout.Button("Fill"))
{
Texture2D generated = GenerateFilledTexture(pathTexture);
TextureGeneratedEventArgs args = new TextureGeneratedEventArgs();
args.original_texture = pathTexture;
args.generated_texture = generated;
TextureGenerated?.Invoke(this, args);
}
}
Credit();
}
public static Texture2D GenerateFilledTexture(Texture2D texture)
{
if (!loopGradient) gradientDistribution = 1;
if (gradientType == GradientType.DataGradient)
{
rangeLowerBound = 1;
rangeUpperBound = 255;
}
int width = texture.width;
modifiedBoundRange = rangeUpperBound / 255 - rangeLowerBound / 255;
Texture2D gradientTexture = GetColors(texture, out Color[] ogColors);
GradientFillPixel[] gradientPixels = new GradientFillPixel[ogColors.Length];
Queue<GradientFillPixel> pixelsToExpand = new Queue<GradientFillPixel>();
for (int i = 0; i < ogColors.Length; i++)
{
gradientPixels[i] = new GradientFillPixel(ogColors[i], i);
if (!gradientPixels[i].isLimit && gradientPixels[i].isFilled) pixelsToExpand.Enqueue(gradientPixels[i]);
}
if (pixelsToExpand.Count == 0)
Debug.LogWarning("<color=red>[GPFiller]</color> No start pixels were found! If start pixels exist, try adjusting the color tolerance.");
bool IsValidFillIndex(int index, out GradientFillPixel pixelToFill)
{
if (index >= 0 && index < ogColors.Length)
{
pixelToFill = gradientPixels[index];
if (!pixelToFill.isLimit && !pixelToFill.isFilled)
{
pixelsToExpand.Enqueue(pixelToFill);
return true;
}
}
pixelToFill = null;
return false;
}
void FillIndex(GradientFillPixel fillerPixel, int index)
{
if (!IsValidFillIndex(index, out GradientFillPixel nextPixel)) return;
nextPixel.gradientValue = fillerPixel.gradientValue + gradientDistribution;
nextPixel.isFilled = true;
}
while (pixelsToExpand.Any())
{
var pixel = pixelsToExpand.Dequeue();
bool isTopMost = pixel.arrayIndex < width;
bool isBottomMost = pixel.arrayIndex >= ogColors.Length - width;
bool isLeftMost = pixel.arrayIndex % width == 0;
bool isRightMost = (pixel.arrayIndex + 1) % width == 0;
if (!isRightMost)
FillIndex(pixel, pixel.arrayIndex + 1);
if (!isLeftMost)
FillIndex(pixel, pixel.arrayIndex - 1);
if (!isBottomMost)
FillIndex(pixel, pixel.arrayIndex + width);
if (!isTopMost)
FillIndex(pixel, pixel.arrayIndex - width);
if (!isBottomMost && !isRightMost)
FillIndex(pixel, pixel.arrayIndex + width + 1);
if (!isBottomMost && !isLeftMost)
FillIndex(pixel, pixel.arrayIndex + width - 1);
if (!isTopMost && !isRightMost)
FillIndex(pixel, pixel.arrayIndex - width + 1);
if (!isTopMost && !isLeftMost)
FillIndex(pixel, pixel.arrayIndex - width - 1);
}
float maxGradientValue = gradientPixels.Max(p => p.gradientValue);
if (gradientType == GradientType.DataGradient) maxGradientValue /= 4;
for (int i = 0; i < ogColors.Length; i++)
{
var f = gradientPixels[i].GetFloatValue(maxGradientValue);
if (f == 0) ogColors[i] = Color.clear;
else
{
if (gradientType == GradientType.TintedGradient)
ogColors[i] = new Color(f * tintColor.r, f * tintColor.g, f * tintColor.b, gradientPixels[i].isLimit ? 1 : (applyGradientAlpha ? f : 1) * tintColor.a);
else if (gradientType == GradientType.GradientGradient)
{
Color gradColor = gradientColor.Evaluate(f);
gradColor.a = gradientPixels[i].isLimit ? 1 : (applyGradientAlpha ? f : 1) * gradColor.a;
ogColors[i] = gradColor;
}
else
{
if (f <= 1) ogColors[i] = new Color(f % 1, 0, 0, 0);
else if (f <= 2) ogColors[i] = new Color(0, f%1, 0, 0);
else if (f <= 3) ogColors[i] = new Color(0, 0, f % 1, 0);
else ogColors[i] = new Color(0, 0, 0, f % 1);
}
}
}
gradientTexture.SetPixels(ogColors);
gradientTexture.Apply();
string assetPath = AssetDatabase.GetAssetPath(pathTexture);
string ext = Path.GetExtension(assetPath);
byte[] data;
switch (ext)
{
case ".jpeg" when !applyGradientAlpha && gradientType != GradientType.DataGradient:
case ".jpg" when !applyGradientAlpha && gradientType != GradientType.DataGradient:
ext = ".jpg";
data = gradientTexture.EncodeToJPG(100);
break;
case ".tga":
data = gradientTexture.EncodeToTGA();
break;
default:
ext = ".png";
data = gradientTexture.EncodeToPNG();
break;
}
DestroyImmediate(gradientTexture);
string savePath = AssetDatabase.GenerateUniqueAssetPath($"{Path.GetDirectoryName(assetPath)}/{pathTexture.name}{ext}");
SaveTexture(data, savePath);
CopyTextureSettings(assetPath, savePath);
return AssetDatabase.LoadAssetAtPath<Texture2D>(savePath);
}
public static Texture2D GetColors(Texture2D texture, int width, int height, out Color[] Colors, bool unloadTempTexture = false)
{
//Thanks to
//https://gamedev.stackexchange.com/questions/92285/unity3d-resize-texture-without-corruption
texture.filterMode = FilterMode.Point;
RenderTexture rt = RenderTexture.GetTemporary(width, height);
rt.filterMode = FilterMode.Point;
RenderTexture.active = rt;
Graphics.Blit(texture, rt);
Texture2D newTexture = new Texture2D(width, height);
newTexture.ReadPixels(new Rect(0, 0, width, height), 0, 0);
Color[] myColors = newTexture.GetPixels();
RenderTexture.active = null;
/////////////////////
Colors = myColors;
if (unloadTempTexture)
{
DestroyImmediate(newTexture);
return null;
}
return newTexture;
}
public static Texture2D GetColors(Texture2D texture, out Color[] Colors, bool unloadTempTexture = false)
{
return GetColors(texture, texture.width, texture.height, out Colors, unloadTempTexture);
}
private static void CopyTextureSettings(string from, string to)
{
TextureImporter source = (TextureImporter)AssetImporter.GetAtPath(from);
TextureImporterSettings sourceSettings = new TextureImporterSettings();
source.ReadTextureSettings(sourceSettings);
TextureImporter destination = (TextureImporter)AssetImporter.GetAtPath(to);
destination.SetTextureSettings(sourceSettings);
destination.maxTextureSize = source.maxTextureSize;
destination.textureCompression = source.textureCompression;
destination.crunchedCompression = source.crunchedCompression;
destination.SaveAndReimport();
}
private static void SaveTexture(byte[] textureEncoding, string path)
{
using (System.IO.FileStream stream = System.IO.File.Create(path))
stream.Write(textureEncoding, 0, textureEncoding.Length);
AssetDatabase.Refresh();
EditorGUIUtility.PingObject(AssetDatabase.LoadMainAssetAtPath(path));
}
public class GradientFillPixel
{
public readonly bool isLimit;
public bool isFilled;
public float gradientValue;
public int arrayIndex;
public GradientFillPixel(Color pixelColor, int index)
{
isLimit = Mathf.Abs(pixelColor.r - limitPixelsColor.r) < limitColorTolerance &&
Mathf.Abs(pixelColor.g - limitPixelsColor.g) < limitColorTolerance &&
Mathf.Abs(pixelColor.b - limitPixelsColor.b) < limitColorTolerance;
isFilled = Mathf.Abs(pixelColor.r - startPixelsColor.r) < startColorTolerance &&
Mathf.Abs(pixelColor.g - startPixelsColor.g) < startColorTolerance &&
Mathf.Abs(pixelColor.b - startPixelsColor.b) < startColorTolerance;
gradientValue = (int)rangeLowerBound;
arrayIndex = index;
}
public float GetFloatValue(float maxGradientValue)
{
if (isLimit || !isFilled) return 0;
float floatValue = !loopGradient ? gradientValue / maxGradientValue : (gradientValue % 255 * (gradientType == GradientType.DataGradient ? 4 : 1)) / 255f;
float adjustedValue = floatValue * modifiedBoundRange + rangeLowerBound / 255;
return invertGradient ? (gradientType == GradientType.DataGradient ? 4 : 1) - adjustedValue : adjustedValue;
}
}
private static void Credit()
{
using (new GUILayout.HorizontalScope())
{
GUILayout.FlexibleSpace();
if (GUILayout.Button("Made By Dreadrith#3238", "boldlabel"))
Application.OpenURL("https://linktr.ee/Dreadrith");
}
}
}
}
|