AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
FadeColors.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using System.Collections;
5 using System.Collections.Generic;
6 using UnityEngine;
7 using UnityEngine.Events;
8 
9 namespace HoloToolkit.Examples.Prototyping
10 {
14  public class FadeColors : MonoBehaviour
15  {
23  public enum LerpTypes { Linear, EaseIn, EaseOut, EaseInOut }
24 
25  [Tooltip("Fade ease type")]
26  public GameObject TargetObject;
27 
28  [Tooltip("Fade ease type")]
30 
31  [Tooltip("Time to fade in seconds")]
32  public float LerpTime = 1f;
33 
34  [Tooltip("Run by default? or Status")]
35  public bool IsRunning = false;
36 
37  [Tooltip("Animation is complete!")]
38  public UnityEvent OnComplete;
39 
43  public float GetCurrentAlpha { get { return mCurrentAlpha; } }
44 
45  // animation ticker
46  private float mLerpTimeCounter;
47 
48  // color and alpha values
49  private Color mCachedColor;
50  private float mCurrentAlpha;
51 
52  // component targets
53  private Material mMaterial;
54  private TextMesh mTextMesh;
55 
56  // are we fading in or out?
57  private bool mIsFadingIn = true;
58 
62  private void Awake()
63  {
64  if (TargetObject == null)
65  {
66  TargetObject = this.gameObject;
67  }
68 
69  Renderer renderer = GetComponent<Renderer>();
70  mTextMesh = GetComponent<TextMesh>();
71 
72  if (renderer != null)
73  {
74  mMaterial = renderer.material;
75  }
76  else if (mTextMesh == null)
77  {
78  Debug.LogError("Renderer and TextMesh not found!");
79  Destroy(this);
80  }
81 
82  CacheColor();
83  }
84 
88  public void FadeOut( bool resetStartValue = false)
89  {
90  CacheColor();
91  mCurrentAlpha = mCachedColor.a;
92  mLerpTimeCounter = LerpTime - LerpTime * mCurrentAlpha;
93  IsRunning = true;
94  mIsFadingIn = false;
95 
96  if (resetStartValue && mMaterial != null)
97  {
98  mCachedColor.a = 1;
99  mMaterial.color = mCachedColor;
100  }
101  }
102 
106  public void FadeIn(bool resetStartValue = false)
107  {
108  CacheColor();
109  mCurrentAlpha = mCachedColor.a;
110  mLerpTimeCounter = LerpTime * mCurrentAlpha;
111  IsRunning = true;
112  mIsFadingIn = true;
113 
114  if (resetStartValue && mMaterial != null)
115  {
116  mCachedColor.a = 0;
117  mMaterial.color = mCachedColor;
118  }
119  }
120 
125  public void ResetColor(Color color)
126  {
127  mCachedColor = color;
128  mCurrentAlpha = mCachedColor.a;
129  mLerpTimeCounter = LerpTime * mCurrentAlpha;
130 
131  if (mTextMesh != null)
132  {
133  mTextMesh.color = mCachedColor;
134  }
135  else
136  {
137  mMaterial.color = mCachedColor;
138  }
139  }
140 
145  public void ResetFade(float value)
146  {
147  CacheColor();
148 
149  if (mMaterial != null)
150  {
151  mCachedColor.a = value;
152  mMaterial.color = mCachedColor;
153  }
154 
155  mLerpTimeCounter = LerpTime * mCurrentAlpha;
156  }
157 
161  public void StopRunning()
162  {
163  IsRunning = false;
164  }
165 
169  private void CacheColor()
170  {
171  if (mTextMesh != null)
172  {
173  mCachedColor = mTextMesh.color;
174  }
175  else
176  {
177  if (mMaterial != null)
178  {
179  mCachedColor = mMaterial.color;
180  }
181  else
182  {
183  mCachedColor = new Color();
184  }
185  }
186  }
187 
192  private void SetColor(float percent)
193  {
194  float newAlpha = 0;
195  if (!mIsFadingIn)
196  {
197  percent = 1 - percent;
198  }
199 
200  switch (LerpType)
201  {
202  case LerpTypes.Linear:
203  newAlpha = percent;
204  break;
205  case LerpTypes.EaseIn:
206  newAlpha = QuadEaseIn(0, 1, percent);
207  break;
208  case LerpTypes.EaseOut:
209  newAlpha = QuadEaseOut(0, 1, percent);
210  break;
211  case LerpTypes.EaseInOut:
212  newAlpha = QuadEaseInOut(0, 1, percent);
213  break;
214  default:
215  break;
216  }
217 
218  mCachedColor.a = newAlpha;
219 
220  if (mTextMesh != null)
221  {
222  mTextMesh.color = mCachedColor;
223  }
224  else
225  {
226  mMaterial.color = mCachedColor;
227  }
228  }
229 
230  // easing functions
231  public static float QuadEaseIn(float s, float e, float v)
232  {
233  return e * (v /= 1) * v + s;
234  }
235 
236  public static float QuadEaseOut(float s, float e, float v)
237  {
238  return -e * (v /= 1) * (v - 2) + s;
239  }
240 
241  public static float QuadEaseInOut(float s, float e, float v)
242  {
243  if ((v /= 0.5f) < 1)
244  return e / 2 * v * v + s;
245 
246  return -e / 2 * ((--v) * (v - 2) - 1) + s;
247  }
248 
252  private void Update()
253  {
254  if (IsRunning)
255  {
256  mLerpTimeCounter += Time.deltaTime;
257  float percent = mLerpTimeCounter / LerpTime;
258 
259  SetColor(percent);
260 
261  if (percent >= 1)
262  {
263  IsRunning = false;
264  OnComplete.Invoke();
265  }
266  }
267  }
268 
269  private void OnDestroy()
270  {
271  if (mMaterial != null)
272  {
273  Destroy(mMaterial);
274  }
275  }
276  }
277 }
LerpTypes
The selectable ease types for the animation Linear: steady progress EaseIn: ramp up in speed EaseOut:...
Definition: FadeColors.cs:23
Fades colors (alpha value) on a material or TextMesh, requires a transparent material ...
Definition: FadeColors.cs:14
static float QuadEaseOut(float s, float e, float v)
Definition: FadeColors.cs:236
void FadeOut(bool resetStartValue=false)
Fade out
Definition: FadeColors.cs:88
static float QuadEaseInOut(float s, float e, float v)
Definition: FadeColors.cs:241
static float QuadEaseIn(float s, float e, float v)
Definition: FadeColors.cs:231
void StopRunning()
stop the currently running animation
Definition: FadeColors.cs:161
void ResetColor(Color color)
Update the color of the component
Definition: FadeColors.cs:125
void FadeIn(bool resetStartValue=false)
Fade In
Definition: FadeColors.cs:106