-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gradient.cs
102 lines (80 loc) · 2.79 KB
/
Gradient.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
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
using UnityEngine;
[RequireComponent(typeof(Camera))]
public class Gradient : MonoBehaviour
{
public Color lightColor;
public Color darkColor;
public Color darkestColor;
public int gradientLayer = 7;
public float stepSize = 0.05f;
Color topColor;
Color bottomColor;
Color topTargetColor;
Color bottomTargetColor;
Mesh gradientMesh;
void Awake()
{
gradientLayer = Mathf.Clamp(gradientLayer, 0, 31);
GetComponent<Camera>().clearFlags = CameraClearFlags.Depth;
GetComponent<Camera>().cullingMask = GetComponent<Camera>().cullingMask & ~(1 << gradientLayer);
Camera gradientCamera = new GameObject("Gradient Camera", typeof(Camera)).GetComponent<Camera>();
gradientCamera.depth = GetComponent<Camera>().depth - 1;
gradientCamera.cullingMask = 1 << gradientLayer;
gradientMesh = new Mesh();
gradientMesh.vertices = new Vector3[4] { new Vector3(-100f, .577f, 1f), new Vector3(100f, .577f, 1f), new Vector3(-100f, -.577f, 1f), new Vector3(100f, -.577f, 1f) };
gradientMesh.colors = new Color[4] { lightColor, lightColor, darkColor, darkColor };
topColor = lightColor;
bottomColor = darkColor;
topTargetColor = lightColor;
bottomTargetColor = darkColor;
gradientMesh.triangles = new int[6] { 0, 1, 2, 1, 3, 2 };
Material gradientMaterial = new Material("Shader \"Vertex Color Only\"{Subshader{BindChannels{Bind \"vertex\", vertex Bind \"color\", color}Pass{}}}");
GameObject BackGround = new GameObject("BackGround", typeof(MeshFilter), typeof(MeshRenderer));
((MeshFilter)BackGround.GetComponent(typeof(MeshFilter))).mesh = gradientMesh;
BackGround.GetComponent<Renderer>().material = gradientMaterial;
BackGround.layer = gradientLayer;
}
public void DeepenGradient()
{
topTargetColor = bottomColor;
bottomTargetColor.r -= stepSize;
if (bottomTargetColor.r < darkestColor.r)
bottomTargetColor.r = darkestColor.r;
bottomTargetColor.g -= stepSize;
if (bottomTargetColor.g < darkestColor.g)
bottomTargetColor.g = darkestColor.g;
bottomTargetColor.b -= stepSize;
if (bottomTargetColor.b < darkestColor.b)
bottomTargetColor.b = darkestColor.b;
topTargetColor.a = 1.0f;
bottomTargetColor.a = 1.0f;
}
public void LightenGradient()
{
bottomTargetColor = darkColor;
topTargetColor = lightColor;
}
void MaintainGradient()
{
bool bChangedColors = false;
if (topColor != topTargetColor)
{
topColor = Color.Lerp(topColor, topTargetColor, Time.deltaTime * stepSize);
bChangedColors = true;
}
if (bottomColor != bottomTargetColor)
{
bottomColor = Color.Lerp(bottomColor, bottomTargetColor, Time.deltaTime * stepSize);
bChangedColors = true;
}
if (bChangedColors)
{
Color[] colors = new Color[4] { topColor, topColor, bottomColor, bottomColor };
gradientMesh.colors = colors;
}
}
void Update()
{
MaintainGradient();
}
}