-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLightColorAnimation.cs
More file actions
170 lines (144 loc) · 4.71 KB
/
LightColorAnimation.cs
File metadata and controls
170 lines (144 loc) · 4.71 KB
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
/// <summary>
/// Gives a suite of options for variances in light color and intensity over time.
/// Credit to Harry1960 and the posters in http://forum.unity3d.com/threads/flickering-light.4988/ for the bulk of the code.
/// I added in intermitent animation options to help achieve a more realistic light flicker.
/// </summary>
using UnityEngine;
using System.Collections;
public enum enColorchannels
{
all = 0,
red = 1,
blue = 2,
green = 3
}
public enum enWaveFunctions
{
sinus = 0,
triangle = 1,
square = 2,
sawtooth = 3,
inverted_saw = 4,
noise = 5
}
public class LightColorAnimation : MonoBehaviour
{
public enColorchannels colorChannel = enColorchannels.all;
public enWaveFunctions waveFunction = enWaveFunctions.sinus;
public float offset = 0.0f; // constant offset
public float amplitude = 1.0f; // amplitude of the wave
public float phase = 0.0f; // start point inside on wave cycle
public float frequency = 0.5f; // cycle frequency per second
public bool affectsIntensity = true;
// Set variables regarding intermittency of animation
public bool intermittent = false;
public float smallestNonwaveInterval = 1.0f;
public float largestNonwaveInterval = 5.0f;
public float smallestWaveInterval = 0.1f;
public float largestWaveInterval = 1.0f;
// Keep a copy of the original values
private Color originalColor;
private float originalIntensity;
private float timeSinceLastInterval = 0.0f;
private float nextIntervalTime = 0.0f;
private bool isInInterval = false;
// Use this for initialization
void Start()
{
originalColor = GetComponent<Light>().color;
originalIntensity = GetComponent<Light>().intensity;
}
// Update is called once per frame
void Update()
{
HandleIntermittent();
if (!isInInterval && intermittent)
{
return;
}
Light light = GetComponent<Light>();
if (affectsIntensity)
light.intensity = originalIntensity * EvalWave();
Color o = originalColor;
Color c = GetComponent<Light>().color;
if (colorChannel == enColorchannels.all)
light.color = originalColor * EvalWave();
else
if (colorChannel == enColorchannels.red)
light.color = new Color(o.r * EvalWave(), c.g, c.b, c.a);
else
if (colorChannel == enColorchannels.green)
light.color = new Color(c.r, o.g * EvalWave(), c.b, c.a);
else // blue
light.color = new Color(c.r, c.g, o.b * EvalWave(), c.a);
}
private float EvalWave()
{
float x = (Time.time + phase) * frequency;
float y;
x = x - Mathf.Floor(x); // normalized value (0..1)
if (waveFunction == enWaveFunctions.sinus)
{
y = Mathf.Sin(x * 2f * Mathf.PI);
}
else if (waveFunction == enWaveFunctions.triangle)
{
if (x < 0.5f)
y = 4.0f * x - 1.0f;
else
y = -4.0f * x + 3.0f;
}
else if (waveFunction == enWaveFunctions.square)
{
if (x < 0.5f)
y = 1.0f;
else
y = -1.0f;
}
else if (waveFunction == enWaveFunctions.sawtooth)
{
y = x;
}
else if (waveFunction == enWaveFunctions.inverted_saw)
{
y = 1.0f - x;
}
else if (waveFunction == enWaveFunctions.noise)
{
y = 1f - (Random.value * 2f);
}
else
{
y = 1.0f;
}
return (y * amplitude) + offset;
}
void HandleIntermittent()
{
if (!intermittent){ return; }
timeSinceLastInterval += Time.deltaTime;
// If we aren't in a wave interval, determine when we should be and watch for it
if (!isInInterval)
{
if (timeSinceLastInterval > nextIntervalTime)
{
isInInterval = true;
timeSinceLastInterval = 0;
nextIntervalTime = Random.Range(smallestWaveInterval, largestWaveInterval);
}
}
// If we are in a wave interval, determine when we should not be and wait for it
if (isInInterval)
{
if (timeSinceLastInterval > nextIntervalTime)
{
isInInterval = false;
timeSinceLastInterval = 0;
nextIntervalTime = Random.Range(smallestNonwaveInterval, largestNonwaveInterval);
GetComponent<Light>().color = originalColor;
GetComponent<Light>().intensity = originalIntensity;
return;
}
}
}
}