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
|
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System;
using UnityEditorInternal;
namespace XSToonDynamicPenetration
{
public class XSGradientEditor : EditorWindow
{
public List<int> gradients_index = new List<int>(new int[1] { 0 });
public List<Gradient> gradients = new List<Gradient>(5);
public Texture2D tex;
private string finalFilePath;
private bool isLinear = false;
private bool manualMaterial = false;
private enum Resolutions
{
Tiny64x8 = 64,
Small128x8 = 128,
Medium256x8 = 256,
Large512x8 = 512
}
private Resolutions res = Resolutions.Tiny64x8;
public static Material focusedMat;
private Material oldFocusedMat;
private Texture oldTexture;
private string rampProperty = "_Ramp";
private ReorderableList grad_index_reorderable;
private bool reorder;
private static GUIContent iconToolbarPlus;
private static GUIContent iconToolbarMinus;
private static GUIStyle preButton;
private static GUIStyle buttonBackground;
private bool changed;
private int loadGradIndex;
private XSMultiGradient xsmg;
private Vector2 scrollPos;
private bool dHelpText = false;
//private bool dAdvanced = false;
[MenuItem("Tools/Xiexe/XSToon/Gradient Editor")]
static public void Init()
{
XSGradientEditor window = EditorWindow.GetWindow<XSGradientEditor>(false, "XSToon: Gradient Editor", true);
window.minSize = new Vector2(450, 390);
}
public void OnGUI()
{
changed = false;
if (focusedMat != null)
{
XSStyles.ShurikenHeader("Current Material: " + focusedMat.name);
}
else
{
XSStyles.ShurikenHeader("Current Material: None");
}
if (preButton == null)
{
iconToolbarPlus = EditorGUIUtility.IconContent("Toolbar Plus", "Add Gradient");
iconToolbarMinus = EditorGUIUtility.IconContent("Toolbar Minus", "Remove Gradient");
preButton = new GUIStyle("RL FooterButton");
buttonBackground = new GUIStyle("RL Header");
}
if (gradients.Count == 0)
{
gradients.Add(new Gradient());
gradients.Add(new Gradient());
gradients.Add(new Gradient());
gradients.Add(new Gradient());
gradients.Add(new Gradient());
}
if (grad_index_reorderable == null)
{
makeReorderedList();
}
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
Rect r = EditorGUILayout.GetControlRect();
float rightEdge = r.xMax;
float leftEdge = rightEdge - 48f;
r = new Rect(leftEdge, r.y, rightEdge - leftEdge, r.height);
if (Event.current.type == EventType.Repaint) buttonBackground.Draw(r, false, false, false, false);
leftEdge += 18f;
EditorGUI.BeginDisabledGroup(gradients_index.Count == 5);
bool addE = GUI.Button(new Rect(leftEdge + 4, r.y, 25, 13), iconToolbarPlus, preButton);
EditorGUI.EndDisabledGroup();
EditorGUI.BeginDisabledGroup(gradients_index.Count == 1);
bool removeE = GUI.Button(new Rect(leftEdge - 19, r.y, 25, 13), iconToolbarMinus, preButton);
EditorGUI.EndDisabledGroup();
if (addE)
{
grad_index_reorderable.index++;
int wat = 0;
for (int i = 0; i < 5; i++)
{
if (!gradients_index.Contains(i))
{
wat = i;
break;
}
}
gradients_index.Add(wat);
changed = true;
}
if (removeE)
{
gradients_index.Remove(gradients_index[gradients_index.Count - 1]);
grad_index_reorderable.index--;
changed = true;
}
GUIStyle button = new GUIStyle(EditorStyles.miniButton);
button.normal = !reorder ? EditorStyles.miniButton.normal : EditorStyles.miniButton.onNormal;
if (GUILayout.Button(new GUIContent("Reorder", "Don't use Reorder if you want to undo a gradient change"), button, GUILayout.ExpandWidth(false)))
{
reorder = !reorder;
}
GUILayout.EndHorizontal();
SerializedObject serializedObject = new SerializedObject(this);
if (reorder)
{
grad_index_reorderable.DoLayoutList();
}
else
{
SerializedProperty colorGradients = serializedObject.FindProperty("gradients");
if (colorGradients.arraySize == 5)
{
for (int i = 0; i < gradients_index.Count; i++)
{
Rect _r = EditorGUILayout.GetControlRect();
_r.x += 16f;
_r.width -= 2f + 16f;
_r.height += 5f;
_r.y += 2f + (3f * i);
EditorGUI.PropertyField(_r, colorGradients.GetArrayElementAtIndex(gradients_index[i]), new GUIContent(""));
}
GUILayout.Space(Mathf.Lerp(9f, 24f, gradients_index.Count / 5f));
}
}
if (serializedObject.ApplyModifiedProperties()) changed = true;
if (oldFocusedMat != focusedMat)
{
changed = true;
if (this.oldTexture != null)
{
if (this.oldTexture == EditorGUIUtility.whiteTexture) this.oldTexture = null;
oldFocusedMat.SetTexture(rampProperty, this.oldTexture);
this.oldTexture = null;
}
oldFocusedMat = focusedMat;
}
Resolutions oldRes = res;
res = (Resolutions)EditorGUILayout.EnumPopup("Resolution: ", res);
if (oldRes != res) changed = true;
int width = (int)res;
int height = 30;
if (gradients_index.Count == 1)
{
height = 8;
}
else
{
height = 150;
}
if (tex == null)
{
tex = new Texture2D(width, height, TextureFormat.RGBA32, false);
}
bool old_isLinear = isLinear;
drawAdvancedOptions();
if (old_isLinear != isLinear)
{
changed = true;
}
if (manualMaterial)
{
focusedMat = (Material)EditorGUILayout.ObjectField(new GUIContent("", ""), focusedMat, typeof(Material), true);
}
if (focusedMat != null)
{
if (focusedMat.HasProperty("_Ramp"))
{
rampProperty = "_Ramp";
}
else
{
rampProperty = EditorGUILayout.TextField("Ramp Property Name", rampProperty);
if (!focusedMat.HasProperty(rampProperty))
{
GUILayout.Label("Property not found!");
}
}
}
if (changed)
{
updateTexture(width, height);
if (focusedMat != null)
{
if (focusedMat.HasProperty(rampProperty))
{
if (this.oldTexture == null)
{
if (focusedMat.GetTexture(rampProperty) == null)
{
this.oldTexture = EditorGUIUtility.whiteTexture;
}
else
{
this.oldTexture = focusedMat.GetTexture(rampProperty);
}
}
tex.wrapMode = TextureWrapMode.Clamp;
tex.Apply(false, false);
focusedMat.SetTexture(rampProperty, tex);
}
}
}
XSStyles.Separator();
drawMGInputOutput();
if (GUILayout.Button("Save Ramp"))
{
finalFilePath = XSStyles.findAssetPath(finalFilePath);
string path = EditorUtility.SaveFilePanel("Save Ramp as PNG", finalFilePath + "/Textures/Shadow Ramps/Generated", "gradient", "png");
if (path.Length != 0)
{
updateTexture(width, height);
bool success = GenTexture(tex, path);
if (success)
{
if (focusedMat != null)
{
string s = path.Substring(path.IndexOf("Assets"));
Texture ramp = AssetDatabase.LoadAssetAtPath<Texture>(s);
if (ramp != null)
{
focusedMat.SetTexture(rampProperty, ramp);
this.oldTexture = null;
}
}
}
}
}
drawHelpText();
}
Gradient reflessGradient(Gradient old_grad)
{
Gradient grad = new Gradient();
grad.SetKeys(old_grad.colorKeys, old_grad.alphaKeys);
grad.mode = old_grad.mode;
return grad;
}
List<int> reflessIndexes(List<int> old_indexes)
{
List<int> indexes = new List<int>();
for (int i = 0; i < old_indexes.Count; i++)
{
indexes.Add(old_indexes[i]);
}
return indexes;
}
void makeReorderedList()
{
grad_index_reorderable = new ReorderableList(gradients_index, typeof(int), true, false, false, false);
grad_index_reorderable.headerHeight = 0f;
grad_index_reorderable.footerHeight = 0f;
grad_index_reorderable.showDefaultBackground = true;
grad_index_reorderable.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
{
if (gradients.Count == 5)
{
Type editorGui = typeof(EditorGUI);
MethodInfo mi = editorGui.GetMethod("GradientField", BindingFlags.NonPublic | BindingFlags.Static, null, new Type[2] { typeof(Rect), typeof(Gradient) }, null);
mi.Invoke(this, new object[2] { rect, gradients[gradients_index[index]] });
if (Event.current.type == EventType.Repaint)
{
changed = true;
}
}
};
grad_index_reorderable.onChangedCallback = (ReorderableList list) =>
{
changed = true;
};
}
void OnDestroy()
{
if (focusedMat != null)
{
if (this.oldTexture != null)
{
if (this.oldTexture == EditorGUIUtility.whiteTexture)
{
this.oldTexture = null;
}
focusedMat.SetTexture(rampProperty, this.oldTexture);
this.oldTexture = null;
}
focusedMat = null;
}
}
void updateTexture(int width, int height)
{
tex = new Texture2D(width, height, TextureFormat.RGBA32, false);
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
if (gradients_index.Count != 1)
{
int gradNum = Mathf.FloorToInt(y / 30f);
gradNum = Mathf.Abs(gradNum - 5) - 1;
if (gradNum >= gradients_index.Count)
{
tex.SetPixel(x, y, Color.white);
}
else
{
if (gradients[gradients_index[gradNum]] != null)
{
Color grad_col = gradients[gradients_index[gradNum]].Evaluate((float)x / (float)width);
tex.SetPixel(x, y, isLinear ? grad_col.gamma : grad_col);
}
else
{
tex.SetPixel(x, y, Color.white);
}
}
}
else
{
Color grad_col = gradients[gradients_index[0]].Evaluate((float)x / (float)width);
tex.SetPixel(x, y, isLinear ? grad_col.gamma : grad_col);
}
}
}
}
bool GenTexture(Texture2D tex, string path)
{
var pngData = tex.EncodeToPNG();
if (pngData != null)
{
File.WriteAllBytes(path, pngData);
AssetDatabase.Refresh();
return ChangeImportSettings(path);
}
return false;
}
bool ChangeImportSettings(string path)
{
string s = path.Substring(path.LastIndexOf("Assets"));
TextureImporter texture = (TextureImporter)TextureImporter.GetAtPath(s);
if (texture != null)
{
texture.wrapMode = TextureWrapMode.Clamp;
texture.maxTextureSize = 512;
texture.mipmapEnabled = false;
texture.textureCompression = TextureImporterCompression.Uncompressed;
// texture.sRGBTexture = !isLinear; // We already do the conversion in tex.SetPixel
texture.SaveAndReimport();
AssetDatabase.Refresh();
return true;
// shadowRamp = (Texture)Resources.Load(path);
// Debug.LogWarning(shadowRamp.ToString());
}
else
{
Debug.Log("Asset Path is Null, can't set to Clamped.\n You'll need to do it manually.");
}
return false;
}
void drawMGInputOutput()
{
GUILayout.BeginHorizontal();
XSMultiGradient old_xsmg = xsmg;
xsmg = (XSMultiGradient)EditorGUILayout.ObjectField("MultiGradient Preset", xsmg, typeof(XSMultiGradient), false, null);
if (xsmg != old_xsmg)
{
if (xsmg != null)
{
this.gradients = xsmg.gradients;
this.gradients_index = xsmg.order;
makeReorderedList();
}
else
{
List<Gradient> new_Grads = new List<Gradient>();
for (int i = 0; i < this.gradients.Count; i++)
{
new_Grads.Add(reflessGradient(this.gradients[i]));
}
this.gradients = new_Grads;
this.gradients_index = reflessIndexes(this.gradients_index);
makeReorderedList();
}
changed = true;
}
if (GUILayout.Button("Save New", EditorStyles.miniButton, GUILayout.ExpandWidth(false)))
{
finalFilePath = XSStyles.findAssetPath(finalFilePath);
string path = EditorUtility.SaveFilePanel("Save MultiGradient", (finalFilePath + "/Textures/Shadow Ramps/MGPresets"), "MultiGradient", "asset");
if (path.Length != 0)
{
path = path.Substring(Application.dataPath.Length - "Assets".Length);
XSMultiGradient _xsmg = ScriptableObject.CreateInstance<XSMultiGradient>();
_xsmg.uniqueName = Path.GetFileNameWithoutExtension(path);
foreach (Gradient grad in gradients)
{
_xsmg.gradients.Add(reflessGradient(grad));
}
_xsmg.order.AddRange(gradients_index.ToArray());
xsmg = _xsmg;
AssetDatabase.CreateAsset(_xsmg, path);
this.gradients = xsmg.gradients;
this.gradients_index = xsmg.order;
makeReorderedList();
AssetDatabase.SaveAssets();
}
}
GUILayout.EndHorizontal();
}
void drawAdvancedOptions()
{
GUILayout.BeginHorizontal();
isLinear = GUILayout.Toggle(isLinear, "Make Linear Texture");
manualMaterial = GUILayout.Toggle(manualMaterial, "Manual Material");
GUILayout.EndHorizontal();
}
void drawHelpText()
{
XSStyles.Separator();
dHelpText = XSStyles.ShurikenFoldout("Information", dHelpText);
if(dHelpText)
{
scrollPos = EditorGUILayout.BeginScrollView(scrollPos);
XSStyles.HelpBox("You can use this to create a custom shadow ramp in realtime. \nIf you do not save, the ramp will be reverted back to what it was previously. \n\n - Click the Gradient box. \n - Choose resolution of the texture. \n - Save.", MessageType.Info);
XSStyles.HelpBox("Ramp textures support up to 5 ramps in one texture. That means you can have up to 5 ramps on a single material. You will need to author a ramp mask to choose which ramp to sample from. \n\nA texture that is fully black would sample from the bottom ramp, a texture that is fully white would sample from the top ramp, and a texture that is half gray would sample from the middle ramp. \n\n A quick tip would be that you can sample from each of the 5 ramps with 0, 0.25, 0.5, 0.75, and 1 on the texture. \n\nThe order of the gradients on the UI is the order that they will be on the texture.", MessageType.Info);
EditorGUILayout.EndScrollView();
}
}
}
}
|