AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
HoverLight.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License.
3 
4 using System.Collections.Generic;
5 using UnityEngine;
6 
7 namespace HoloToolkit.Unity
8 {
13  [ExecuteInEditMode]
14  public class HoverLight : MonoBehaviour
15  {
16  [SerializeField]
17  [Range(0.0f, 1.0f)]
18  private float radius = 0.15f;
19  [SerializeField]
20  private Color color = new Color(0.3f, 0.3f, 0.3f, 1.0f);
21 
22  // Three hover lights are supported at this time.
23  private const int hoverLightCount = 3;
24  private const int hoverLightDataSize = 2;
25  private static List<HoverLight> activeHoverLights = new List<HoverLight>(hoverLightCount);
26  private static Vector4[] hoverLightData = new Vector4[hoverLightCount * hoverLightDataSize];
27  private static int _HoverLightDataID;
28  private static int lastHoverLightUpdate = -1;
29 
30  public float Radius
31  {
32  get
33  {
34  return radius;
35  }
36 
37  set
38  {
39  radius = value;
40  }
41  }
42 
43  public Color Color
44  {
45  get
46  {
47  return color;
48  }
49 
50  set
51  {
52  color = value;
53  }
54  }
55 
56  private void OnEnable()
57  {
58  AddHoverLight(this);
59  }
60 
61  private void OnDisable()
62  {
63  RemoveHoverLight(this);
64  UpdateHoverLights(true);
65  }
66 
67 #if UNITY_EDITOR
68  private void Update()
69  {
70  if (Application.isPlaying)
71  {
72  return;
73  }
74 
75  Initialize();
76  UpdateHoverLights();
77  }
78 #endif
79 
80  private void LateUpdate()
81  {
82  UpdateHoverLights();
83  }
84 
85  private void OnDrawGizmosSelected()
86  {
87  if (!enabled)
88  {
89  return;
90  }
91 
92  Gizmos.color = Color;
93  Gizmos.DrawWireSphere(transform.position, Radius);
94  Gizmos.DrawIcon(transform.position + Vector3.right * Radius, string.Empty, false);
95  Gizmos.DrawIcon(transform.position + Vector3.left * Radius, string.Empty, false);
96  Gizmos.DrawIcon(transform.position + Vector3.up * Radius, string.Empty, false);
97  Gizmos.DrawIcon(transform.position + Vector3.down * Radius, string.Empty, false);
98  Gizmos.DrawIcon(transform.position + Vector3.forward * Radius, string.Empty, false);
99  Gizmos.DrawIcon(transform.position + Vector3.back * Radius, string.Empty, false);
100  }
101 
102  private static void AddHoverLight(HoverLight light)
103  {
104  if (activeHoverLights.Count >= hoverLightCount)
105  {
106  Debug.LogWarningFormat("Max hover light count ({0}) exceeded.", hoverLightCount);
107  }
108 
109  activeHoverLights.Add(light);
110  }
111 
112  private static void RemoveHoverLight(HoverLight light)
113  {
114  activeHoverLights.Remove(light);
115  }
116 
117  private static void Initialize()
118  {
119  _HoverLightDataID = Shader.PropertyToID("_HoverLightData");
120  }
121 
122  private static void UpdateHoverLights(bool forceUpdate = false)
123  {
124  if (lastHoverLightUpdate == -1)
125  {
126  Initialize();
127  }
128 
129  if (!forceUpdate && (Time.frameCount == lastHoverLightUpdate))
130  {
131  return;
132  }
133 
134  if (activeHoverLights.Count > 1)
135  {
136  Shader.EnableKeyword("_MULTI_HOVER_LIGHT");
137  }
138  else
139  {
140  Shader.DisableKeyword("_MULTI_HOVER_LIGHT");
141  }
142 
143  for (int i = 0; i < hoverLightCount; ++i)
144  {
145  HoverLight light = (i >= activeHoverLights.Count) ? null : activeHoverLights[i];
146  int dataIndex = i * hoverLightDataSize;
147 
148  if (light)
149  {
150  hoverLightData[dataIndex] = new Vector4(light.transform.position.x,
151  light.transform.position.y,
152  light.transform.position.z,
153  light.Radius);
154  hoverLightData[dataIndex + 1] = new Vector4(light.Color.r,
155  light.Color.g,
156  light.Color.b,
157  1.0f);
158  }
159  else
160  {
161  hoverLightData[dataIndex] = Vector4.zero;
162  hoverLightData[dataIndex + 1] = Vector4.zero;
163  }
164  }
165 
166  Shader.SetGlobalVectorArray(_HoverLightDataID, hoverLightData);
167 
168  lastHoverLightUpdate = Time.frameCount;
169  }
170  }
171 
172 #if UNITY_EDITOR
173  [UnityEditor.CustomEditor(typeof(HoverLight))]
174  public class HoverLightEditor : UnityEditor.Editor
175  {
176  private bool HasFrameBounds() { return true; }
177 
178  private Bounds OnGetFrameBounds()
179  {
180  HoverLight light = target as HoverLight;
181  return new Bounds(light.transform.position, Vector3.one * light.Radius);
182  }
183  }
184 #endif
185 }
Utility component to animate and visualize a light that can be used with the "MixedRealityToolkit/Sta...
Definition: HoverLight.cs:14