-
Notifications
You must be signed in to change notification settings - Fork 450
/
Copy pathGradientTextureMaker.cs
61 lines (50 loc) · 1.93 KB
/
GradientTextureMaker.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
// returns gradient Texture2D (size=256x1)
using UnityEngine;
namespace UnityLibrary
{
public static class GradientTextureMaker
{
const int width = 256;
const int height = 1;
public static Texture2D Create(Color[] colors, TextureWrapMode textureWrapMode = TextureWrapMode.Clamp, FilterMode filterMode = FilterMode.Point, bool isLinear = false, bool hasMipMap = false)
{
if (colors == null || colors.Length == 0)
{
Debug.LogError("No colors assigned");
return null;
}
int length = colors.Length;
if (colors.Length > 8)
{
Debug.LogWarning("Too many colors! maximum is 8, assigned: " + colors.Length);
length = 8;
}
// build gradient from colors
var colorKeys = new GradientColorKey[length];
var alphaKeys = new GradientAlphaKey[length];
float steps = length - 1f;
for (int i = 0; i < length; i++)
{
float step = i / steps;
colorKeys[i].color = colors[i];
colorKeys[i].time = step;
alphaKeys[i].alpha = colors[i].a;
alphaKeys[i].time = step;
}
// create gradient
Gradient gradient = new Gradient();
gradient.SetKeys(colorKeys, alphaKeys);
// create texture
Texture2D outputTex = new Texture2D(width, height, TextureFormat.ARGB32, false, isLinear);
outputTex.wrapMode = textureWrapMode;
outputTex.filterMode = filterMode;
// draw texture
for (int i = 0; i < width; i++)
{
outputTex.SetPixel(i, 0, gradient.Evaluate((float)i / (float)width));
}
outputTex.Apply(false);
return outputTex;
} // BuildGradientTexture
} // class
} // namespcae