-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRampTexture.cs
73 lines (65 loc) · 2.09 KB
/
RampTexture.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
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
[CreateAssetMenu(fileName="RampTexture.asset", menuName="Ramp Texture", order=310)]
public class RampTexture : ScriptableObject
{
public int width = 256;
public int heightPerRamp = 2;
public Gradient[] gradients = new Gradient[]{};
public int count
{
get {return gradients.Length;}
}
public Texture2D texture = null;
#if UNITY_EDITOR
public void UpdateTexture()
{
texture = null;
string assetPath = AssetDatabase.GetAssetPath(this);
var assets = AssetDatabase.LoadAllAssetRepresentationsAtPath(assetPath);
foreach (var asset in assets)
{
if (asset is Texture2D)
{
texture = asset as Texture2D;
break;
}
}
if (texture == null)
{
texture = new Texture2D(4, 4, TextureFormat.ARGB32, false);
texture.name = "RampTexture4x4";
AssetDatabase.AddObjectToAsset(texture, this);
}
if (width <= 0 || heightPerRamp <= 0)
{
texture.Reinitialize(width, heightPerRamp * count, TextureFormat.ARGB32, false);
texture.name = "RampTexture4x4";
}
else
{
texture.Reinitialize(width, heightPerRamp*count, TextureFormat.ARGB32, false);
texture.name = $"RampTexture{width}x{heightPerRamp*count}";
Color[] colors = new Color[width * heightPerRamp * count];
for (int i=0; i<count; ++i)
{
var gradient = gradients[i];
for (int j=0; j<width; ++j)
{
float t = (j+0.5f) / width;
Color color = gradient.Evaluate(t);
for (int k=0; k<heightPerRamp; ++k)
{
colors[(i*heightPerRamp+k)*width+j] = color;
}
}
}
texture.SetPixels(colors);
}
texture.Apply();
AssetDatabase.SaveAssets();
}
#endif
}