A game about forced loneliness, made by TACStudios
1using UnityEditor.ShaderGraph.Internal;
2using UnityEditor.Graphing;
3using UnityEngine;
4
5namespace UnityEditor.ShaderGraph
6{
7 static class GradientUtil
8 {
9 public static string GetGradientValue(Gradient gradient, string delimiter = ";")
10 {
11 string colorKeys = "";
12 for (int i = 0; i < 8; i++)
13 {
14 if (i < gradient.colorKeys.Length)
15 colorKeys += $"$precision4({NodeUtils.FloatToShaderValue(gradient.colorKeys[i].color.r)}, " +
16 $"{NodeUtils.FloatToShaderValue(gradient.colorKeys[i].color.g)}, " +
17 $"{NodeUtils.FloatToShaderValue(gradient.colorKeys[i].color.b)}, " +
18 $"{NodeUtils.FloatToShaderValue(gradient.colorKeys[i].time)})";
19 else
20 colorKeys += "$precision4(0, 0, 0, 0)";
21 if (i < 7)
22 colorKeys += ",";
23 }
24
25 string alphaKeys = "";
26 for (int i = 0; i < 8; i++)
27 {
28 if (i < gradient.alphaKeys.Length)
29 alphaKeys += $"$precision2({NodeUtils.FloatToShaderValue(gradient.alphaKeys[i].alpha)}, {NodeUtils.FloatToShaderValue(gradient.alphaKeys[i].time)})";
30 else
31 alphaKeys += "$precision2(0, 0)";
32 if (i < 7)
33 alphaKeys += ",";
34 }
35
36 return $"NewGradient({(int)gradient.mode}, {gradient.colorKeys.Length}, {gradient.alphaKeys.Length}, {colorKeys}, {alphaKeys}){delimiter}";
37 }
38
39 public static string GetGradientForPreview(string name)
40 {
41 string colorKeys = "";
42 for (int i = 0; i < 8; i++)
43 {
44 colorKeys += $"{name}_ColorKey{i}";
45 if (i < 7)
46 colorKeys += ",";
47 }
48
49 string alphaKeys = "";
50 for (int i = 0; i < 8; i++)
51 {
52 alphaKeys += $"{name}_AlphaKey{i}";
53 if (i < 7)
54 alphaKeys += ",";
55 }
56
57 return $"NewGradient({name}_Type, {name}_ColorsLength, {name}_AlphasLength, {colorKeys}, {alphaKeys})";
58 }
59
60 public static void GetGradientPropertiesForPreview(PropertyCollector properties, string name, Gradient value)
61 {
62 properties.AddShaderProperty(new Vector1ShaderProperty()
63 {
64 overrideReferenceName = $"{name}_Type",
65 value = (int)value.mode,
66 generatePropertyBlock = false
67 });
68
69 properties.AddShaderProperty(new Vector1ShaderProperty()
70 {
71 overrideReferenceName = $"{name}_ColorsLength",
72 value = value.colorKeys.Length,
73 generatePropertyBlock = false
74 });
75
76 properties.AddShaderProperty(new Vector1ShaderProperty()
77 {
78 overrideReferenceName = $"{name}_AlphasLength",
79 value = value.alphaKeys.Length,
80 generatePropertyBlock = false
81 });
82
83 for (int i = 0; i < 8; i++)
84 {
85 properties.AddShaderProperty(new Vector4ShaderProperty()
86 {
87 overrideReferenceName = $"{name}_ColorKey{i}",
88 value = i < value.colorKeys.Length ? GradientUtil.ColorKeyToVector(value.colorKeys[i]) : Vector4.zero,
89 generatePropertyBlock = false
90 });
91 }
92
93 for (int i = 0; i < 8; i++)
94 {
95 properties.AddShaderProperty(new Vector2ShaderProperty()
96 {
97 overrideReferenceName = $"{name}_AlphaKey{i}",
98 value = i < value.alphaKeys.Length ? GradientUtil.AlphaKeyToVector(value.alphaKeys[i]) : Vector2.zero,
99 generatePropertyBlock = false
100 });
101 }
102 }
103
104 public static bool CheckEquivalency(Gradient A, Gradient B)
105 {
106 var currentMode = A.mode;
107 var currentColorKeys = A.colorKeys;
108 var currentAlphaKeys = A.alphaKeys;
109
110 var newMode = B.mode;
111 var newColorKeys = B.colorKeys;
112 var newAlphaKeys = B.alphaKeys;
113
114 if (currentMode != newMode || currentColorKeys.Length != newColorKeys.Length || currentAlphaKeys.Length != newAlphaKeys.Length)
115 {
116 return false;
117 }
118 else
119 {
120 for (var i = 0; i < currentColorKeys.Length; i++)
121 {
122 if (currentColorKeys[i].color != newColorKeys[i].color || Mathf.Abs(currentColorKeys[i].time - newColorKeys[i].time) > 1e-9)
123 return false;
124 }
125
126 for (var i = 0; i < currentAlphaKeys.Length; i++)
127 {
128 if (Mathf.Abs(currentAlphaKeys[i].alpha - newAlphaKeys[i].alpha) > 1e-9 || Mathf.Abs(currentAlphaKeys[i].time - newAlphaKeys[i].time) > 1e-9)
129 return false;
130 }
131 }
132 return true;
133 }
134
135 public static Vector4 ColorKeyToVector(GradientColorKey key)
136 {
137 return new Vector4(key.color.r, key.color.g, key.color.b, key.time);
138 }
139
140 public static Vector2 AlphaKeyToVector(GradientAlphaKey key)
141 {
142 return new Vector2(key.alpha, key.time);
143 }
144 }
145}