AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
ColorTransition.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 UnityEngine;
5 using System.Collections;
6 using System.Collections.Generic;
7 
8 namespace HoloToolkit.Examples.Prototyping
9 {
13  public class ColorTransition : MonoBehaviour
14  {
15  [Tooltip("GameObject with the materials to be color blended - must support material.color")]
16  public GameObject TargetObject;
17 
18  [Tooltip("Length of time to transition colors in seconds")]
19  public float TransitionTime = 0.75f;
20 
21  [Tooltip("Use easing")]
22  public bool SmoothTransition;
23 
27  private struct ColorTransitionData
28  {
29  public Color EndColor;
30  public Color StartColor;
31  public float Percentage;
32  public float Time;
33  public float Count;
34  public Material Material;
35  public string Name;
36  }
37 
38  // array of materials
39  public Material[] Materials { get; set; }
40 
41  // list of data
42  private List<ColorTransitionData> mData;
43 
44  private void Awake()
45  {
46  // set the target game object if not set already
47  if (TargetObject == null)
48  {
49  TargetObject = this.gameObject;
50  }
51 
52  // get the material array
53  if (Materials == null)
54  {
55  Materials = TargetObject.GetComponent<Renderer>().materials;
56  }
57 
58  // add materials to the ColorTransitionData list
59  mData = new List<ColorTransitionData>();
60 
61  for (int i = 0; i < Materials.Length; ++i)
62  {
63  ColorTransitionData data = new ColorTransitionData();
64  data.StartColor = Materials[i].color;
65  data.Percentage = 0;
66  data.Time = TransitionTime;
67  data.Count = TransitionTime;
68  data.Material = Materials[i];
69  data.Name = Materials[i].name;
70 
71  int SpaceIndex = data.Name.IndexOf(" ");
72  if (SpaceIndex > -1)
73  {
74  data.Name = data.Name.Substring(0, SpaceIndex);
75  }
76 
77  mData.Add(data);
78  }
79  }
80 
86  public void StartTransition(Color color, string name = "")
87  {
88 
89  for (int i = 0; i < mData.Count; ++i)
90  {
91  if (mData[i].Name == name || name == "")
92  {
93  ColorTransitionData data = mData[i];
94  data.Count = 0;
95  data.Time = TransitionTime;
96  data.EndColor = color;
97  data.StartColor = data.Material.color;
98 
99  mData[i] = data;
100  }
101  }
102  }
103 
111  private Color GetColorTransition(Color startColor, Color endColor, float percentage)
112  {
113  Color newColor = endColor;
114 
115  if (percentage < 1)
116  {
117  float smoothPercentage = percentage;
118  if (SmoothTransition)
119  {
120  smoothPercentage = -1 * 0.5f * (Mathf.Cos(Mathf.PI * percentage) - 1);
121  }
122 
123  newColor = Color.LerpUnclamped(startColor, endColor, smoothPercentage);
124  }
125 
126  return newColor;
127  }
128 
132  private void Update()
133  {
134  for (int i = 0; i < mData.Count; ++i)
135  {
136  ColorTransitionData data = mData[i];
137 
138  if (data.Count < data.Time)
139  {
140  data.Count = Mathf.Clamp(data.Count + Time.deltaTime, 0, data.Time);
141  data.Material.color = GetColorTransition(data.StartColor, data.EndColor, data.Count / data.Time);
142  mData[i] = data;
143  }
144 
145  }
146  }
147  }
148 }
void StartTransition(Color color, string name="")
Fades the color of a material called by name
A color blending animation component, handles multiple materials