comparison Orchestland/Assets/LeapMotion/Widgets/Scripts/Dial/FadeTextByRotation.cs @ 3:0030a1b971fb default tip

merge
author Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
date Fri, 17 Jul 2015 23:23:43 +0900
parents f7675884f2a1
children
comparison
equal deleted inserted replaced
2:fdab88fc2cb9 3:0030a1b971fb
1 using UnityEngine;
2 using UnityEngine.UI;
3 using System.Collections;
4
5 /// <summary>
6 /// Fade out text label in dial as it rotates away.
7 /// </summary>
8 /// <remarks>
9 /// Uses the dot product between the parent of the label's forward direction
10 /// and the forward direction of the label, passed through a curve filter
11 /// to determine the opacity to set the text label to.
12 /// </remarks>
13 public class FadeTextByRotation : MonoBehaviour {
14 /// <summary>
15 /// Curve to translate dot product to opacity
16 /// </summary>
17 public AnimationCurve FadeCurve;
18
19 /// <summary>
20 /// Reference for the "forward direction. AutoDiscovered.
21 /// </summary>
22 /// <remarks>
23 /// AutoDiscovery:
24 /// Uses the label's parent's parent (label -> dial center -> panel center)
25 ///
26 /// Autodiscovery can be overrriden by assigning the reference transform in the editor.
27 ///
28 /// "Forward" is assumed to be -z.
29 /// </remarks>
30 public Transform ReferenceTransform_AutoDiscovered;
31
32 /// <summary>
33 /// The starting opacity of the label.
34 /// </summary>
35 private float m_originalLabelOpacity;
36
37 /// <summary>
38 /// Cache a reference to all underlying text labels.
39 /// </summary>
40 /// <remarks>
41 /// We want to cache this both to avoid the extra call
42 /// to GetComponentsInChildren and to avoid the extra array
43 /// alloc on the heap.
44 /// </remarks>
45 private Text[] m_textLabels;
46
47
48 /// <summary>
49 /// Finds and assigns a reference to the reference transform.
50 /// </summary>
51 /// <returns>
52 /// Returns true if the registration was successful or already complete.
53 /// Returns false if the registration failed and the reference transform is still null.
54 /// </returns>
55 private bool registerReferenceTransform() {
56 if (ReferenceTransform_AutoDiscovered != null) {
57 return true;
58 }
59 if (transform.parent == null) { return false; }
60 ReferenceTransform_AutoDiscovered = transform.parent.parent;
61 return ReferenceTransform_AutoDiscovered != null;
62 }
63
64 void Awake() {
65 m_textLabels = GetComponentsInChildren<Text>(true);
66
67 if(m_textLabels.Length == 0) {
68 Debug.LogWarning("No text labels detected. Nothing to fade.");
69 return;
70 }
71
72 // Using a relatively naive selection process here.
73 // As of writing this there is only one label, but writing this
74 // to support [n] labels because it is trivial.
75 m_originalLabelOpacity = m_textLabels[0].color.a;
76 }
77
78 void OnEnable() {
79 if (ReferenceTransform_AutoDiscovered == null) {
80 registerReferenceTransform();
81 }
82 }
83
84 // Update is called once per frame
85 void Update () {
86 // Make sure there is a reference transform to reference.
87 if (ReferenceTransform_AutoDiscovered == null) {
88 bool registered = registerReferenceTransform();
89 if (!registered) {
90 Debug.LogError("No reference transform. Exiting.");
91 return;
92 }
93 }
94
95 // Make sure there are text labels to operate on.
96 if (m_textLabels.Length == 0) {
97 return;
98 }
99
100 float referenceDotDirection = Vector3.Dot(ReferenceTransform_AutoDiscovered.forward, transform.forward);
101 referenceDotDirection = Mathf.Clamp01(referenceDotDirection);
102
103 // We say opacity mod because the actual opacity will be
104 // the original opacity * the opacity mod.
105 // The original opacity is assumed to be the max opacity.
106 float opacityMod = FadeCurve.Evaluate(referenceDotDirection);
107 float goalOpacity = m_originalLabelOpacity * opacityMod;
108
109 // ForEach over an array is memory-optimized in Unity so we can use it.
110 // Usually want to avoid this because of spurious allocs due to the enumerator.
111 foreach(Text textComponent in m_textLabels) {
112 Color textColor = textComponent.color;
113 textColor.a = goalOpacity;
114 textComponent.color = textColor;
115 }
116 }
117 }